diff --git a/Bin/Pp.php b/Bin/Pp.php index 9e6a5954..ecfd3b0f 100644 --- a/Bin/Pp.php +++ b/Bin/Pp.php @@ -1,5 +1,7 @@ lexMe($data, $compiler->getTokens()); @@ -244,8 +234,6 @@ protected function printTokenSequence(Compiler\Llk\Parser $compiler, $data) /** * The command usage. - * - * @return int */ public function usage() { diff --git a/Exception/Exception.php b/Source/Exception/Exception.php similarity index 92% rename from Exception/Exception.php rename to Source/Exception/Exception.php index 900a3fc9..2cfe4aa5 100644 --- a/Exception/Exception.php +++ b/Source/Exception/Exception.php @@ -1,5 +1,7 @@ column; } diff --git a/Exception/Lexer.php b/Source/Exception/Lexer.php similarity index 95% rename from Exception/Lexer.php rename to Source/Exception/Lexer.php index ea1d01f8..1eaa8526 100644 --- a/Exception/Lexer.php +++ b/Source/Exception/Lexer.php @@ -1,5 +1,7 @@ column; } diff --git a/Ll1.php b/Source/Ll1.php similarity index 90% rename from Ll1.php rename to Source/Ll1.php index 53c2cc98..36b846ca 100644 --- a/Ll1.php +++ b/Source/Ll1.php @@ -1,5 +1,7 @@ _initialLine; $this->_initialLine = $line; @@ -703,11 +676,8 @@ public function setInitialLine($line) /** * Set tokens to skip. - * - * @param array $skip Skip. - * @return array */ - public function setSkip(array $skip) + public function setSkip(array $skip): array { $old = $this->_skip; $this->_skip = $skip; @@ -718,11 +688,8 @@ public function setSkip(array $skip) /** * Set tokens. - * - * @param array $tokens Tokens. - * @return array */ - public function setTokens(array $tokens) + public function setTokens(array $tokens): array { $old = $this->_tokens; $this->_tokens = $tokens; @@ -732,11 +699,8 @@ public function setTokens(array $tokens) /** * Set states. - * - * @param array $states States. - * @return array */ - public function setStates(array $states) + public function setStates(array $states): array { $old = $this->_states; $this->_states = $states; @@ -746,11 +710,8 @@ public function setStates(array $states) /** * Set terminal states. - * - * @param array $terminal Terminal states. - * @return array */ - public function setTerminal(array $terminal) + public function setTerminal(array $terminal): array { $old = $this->_terminal; $this->_terminal = $terminal; @@ -760,11 +721,8 @@ public function setTerminal(array $terminal) /** * Set transitions table. - * - * @param array $transitions Transitions table. - * @return array */ - public function setTransitions(array $transitions) + public function setTransitions(array $transitions): array { $old = $this->_transitions; $this->_transitions = $transitions; @@ -774,11 +732,8 @@ public function setTransitions(array $transitions) /** * Set actions table. - * - * @param array $actions Actions table. - * @return array */ - public function setActions(array $actions) + public function setActions(array $actions): array { foreach ($actions as $e => $automata) { foreach ($automata as $i => $state) { @@ -798,11 +753,8 @@ public function setActions(array $actions) /** * Set names of automata. - * - * @param array $names Names of automata. - * @return array */ - public function setNames(array $names) + public function setNames(array $names): array { $old = $this->_names; $this->_names = $names; @@ -812,90 +764,72 @@ public function setNames(array $names) /** * Get initial line. - * - * @return int */ - public function getInitialLine() + public function getInitialLine(): int { return $this->_initialLine; } /** * Get skip tokens. - * - * @return array */ - public function getSkip() + public function getSkip(): array { return $this->_skip; } /** * Get tokens. - * - * @return array */ - public function getTokens() + public function getTokens(): array { return $this->_tokens; } /** * Get states. - * - * @return array */ - public function getStates() + public function getStates(): array { return $this->_states; } /** * Get terminal states. - * - * @return array */ - public function getTerminal() + public function getTerminal(): array { return $this->_terminal; } /** * Get transitions table. - * - * @return array */ - public function getTransitions() + public function getTransitions(): array { return $this->_transitions; } /** * Get actions table. - * - * @return array */ - public function getActions() + public function getActions(): array { return $this->_actions; } /** * Get names of automata. - * - * @return array */ - public function getNames() + public function getNames(): array { return $this->_names; } /** * Enable cache - * - * @return bool */ - public static function enableCache() + public static function enableCache(): bool { $old = self::$_cacheEnabled; self::$_cacheEnabled = true; @@ -905,10 +839,8 @@ public static function enableCache() /** * Disable cache - * - * @return bool */ - public static function disableCache() + public static function disableCache(): bool { $old = self::$_cacheEnabled; self::$_cacheEnabled = false; @@ -918,8 +850,6 @@ public static function disableCache() /** * Transform automatas into DOT language. - * - * @return void */ public function __toString() { diff --git a/Llk/Lexer.php b/Source/Llk/Lexer.php similarity index 90% rename from Llk/Lexer.php rename to Source/Llk/Lexer.php index 68513678..4cb2ea87 100644 --- a/Llk/Lexer.php +++ b/Source/Llk/Lexer.php @@ -1,5 +1,7 @@ _text = $text; $this->_tokens = $tokens; @@ -182,12 +174,8 @@ public function lexMe($text, array $tokens) /** * Compute the next token recognized at the beginning of the string. - * - * @param int $offset Offset. - * @return array - * @throws \Hoa\Compiler\Exception\Lexer */ - protected function nextToken($offset) + protected function nextToken(int $offset): ?array { $tokenArray = &$this->_tokens[$this->_lexerState]; @@ -263,14 +251,8 @@ protected function nextToken($offset) /** * Check if a given lexeme is matched at the beginning of the text. - * - * @param string $lexeme Name of the lexeme. - * @param string $regex Regular expression describing the lexeme. - * @param int $offset Offset. - * @return array - * @throws \Hoa\Compiler\Exception\Lexer */ - protected function matchLexeme($lexeme, $regex, $offset) + protected function matchLexeme(string $lexeme, string $regex, int $offset): ?array { $_regex = str_replace('#', '\#', $regex); $preg = preg_match( diff --git a/Llk/Llk.php b/Source/Llk/Llk.php similarity index 91% rename from Llk/Llk.php rename to Source/Llk/Llk.php index c4eb9482..9ca78dd1 100644 --- a/Llk/Llk.php +++ b/Source/Llk/Llk.php @@ -1,5 +1,7 @@ readAll(); @@ -96,12 +92,8 @@ public static function load(Stream\IStream\In $stream) * The generated PHP code will load the same in-memory parser. The state * will be reset. The parser will be saved as a class, named after * `$className`. To retrieve the parser, one must instanciate this class. - * - * @param \Hoa\Compiler\Llk\Parser $parser Parser to save. - * @param string $className Parser classname. - * @return string */ - public static function save(Parser $parser, $className) + public static function save(Parser $parser, string $className): string { $out = null; $outTokens = null; @@ -251,16 +243,8 @@ public static function save(Parser $parser, $className) /** * Parse the grammar description language. - * - * @param string $pp Grammar description. - * @param array $tokens Extracted tokens. - * @param array $rules Extracted raw rules. - * @param array $pragmas Extracted raw pragmas. - * @param string $streamName The name of the stream containing the grammar. - * @return void - * @throws \Hoa\Compiler\Exception */ - public static function parsePP($pp, &$tokens, &$rules, &$pragmas, $streamName) + public static function parsePP(string $pp, ?array &$tokens, ?array &$rules, ?array &$pragmas, string $streamName) { $lines = explode("\n", $pp); $pragmas = []; @@ -376,4 +360,4 @@ public static function parsePP($pp, &$tokens, &$rules, &$pragmas, $streamName) /** * Flex entity. */ -Consistency::flexEntity('Hoa\Compiler\Llk\Llk'); +Consistency::flexEntity(Llk::class); diff --git a/Llk/Llk.pp b/Source/Llk/Llk.pp similarity index 97% rename from Llk/Llk.pp rename to Source/Llk/Llk.pp index 43cf29c5..d34f09fc 100644 --- a/Llk/Llk.pp +++ b/Source/Llk/Llk.pp @@ -35,9 +35,6 @@ // // Provide grammar for the LL(k) parser. // -// @copyright Copyright © 2007-2017, Hoa community. -// @license New BSD License -// %skip space \s diff --git a/Llk/Parser.php b/Source/Llk/Parser.php similarity index 87% rename from Llk/Parser.php rename to Source/Llk/Parser.php index ec35d1d2..3cc4c9c9 100644 --- a/Llk/Parser.php +++ b/Source/Llk/Parser.php @@ -1,5 +1,7 @@ token regex), to be defined in @@ -69,19 +68,19 @@ class Parser * * @var array */ - protected $_tokens = null; + protected $_tokens = []; /** * Rules, to be defined as associative array, name => Rule object. * * @var array */ - protected $_rules = null; + protected $_rules = []; /** * Lexer iterator. * - * @var \Hoa\Iterator\Lookahead + * @var Iterator\Lookahead */ protected $_tokenSequence = null; @@ -90,7 +89,7 @@ class Parser * * @var array */ - protected $_errorToken = null; + protected $_errorToken = []; /** * Trace of activated rules. @@ -104,12 +103,12 @@ class Parser * * @var array */ - protected $_todo = null; + protected $_todo = []; /** * AST. * - * @var \Hoa\Compiler\Llk\TreeNode + * @var TreeNode */ protected $_tree = null; @@ -124,10 +123,6 @@ class Parser /** * Construct the parser. - * - * @param array $tokens Tokens. - * @param array $rules Rules. - * @param array $pragmas Pragmas. */ public function __construct( array $tokens = [], @@ -143,14 +138,8 @@ public function __construct( /** * Parse :-). - * - * @param string $text Text to parse. - * @param string $rule The axiom, i.e. root rule. - * @param bool $tree Whether build tree or not. - * @return mixed - * @throws \Hoa\Compiler\Exception\UnexpectedToken */ - public function parse($text, $rule = null, $tree = true) + public function parse(string $text, ?string $rule = null, bool $tree = true) { $k = 1024; @@ -180,7 +169,7 @@ public function parse($text, $rule = null, $tree = true) do { $out = $this->unfold(); - if (null !== $out && + if (false !== $out && 'EOF' === $this->_tokenSequence->current()['token']) { break; } @@ -247,10 +236,8 @@ public function parse($text, $rule = null, $tree = true) /** * Unfold trace. - * - * @return mixed */ - protected function unfold() + private function unfold(): bool { while (0 < count($this->_todo)) { $rule = array_pop($this->_todo); @@ -269,7 +256,7 @@ protected function unfold() $out = $this->_parse($zeRule, $next); if (false === $out && false === $this->backtrack()) { - return null; + return false; } } } @@ -279,12 +266,8 @@ protected function unfold() /** * Parse current rule. - * - * @param \Hoa\Compiler\Llk\Rule $zeRule Current rule. - * @param int $next Next rule index. - * @return bool */ - protected function _parse(Rule $zeRule, $next) + private function _parse(Rule $zeRule, int $next): bool { if ($zeRule instanceof Rule\Token) { $name = $this->_tokenSequence->current()['token']; @@ -454,10 +437,8 @@ protected function _parse(Rule $zeRule, $next) /** * Backtrack the trace. - * - * @return bool */ - protected function backtrack() + private function backtrack(): bool { $found = false; @@ -495,12 +476,8 @@ protected function backtrack() /** * Build AST from trace. * Walk through the trace iteratively and recursively. - * - * @param int $i Current trace index. - * @param array &$children Collected children. - * @return \Hoa\Compiler\Llk\TreeNode */ - protected function _buildTree($i = 0, &$children = []) + private function _buildTree(int $i = 0, &$children = []) { $max = count($this->_trace); @@ -613,20 +590,13 @@ protected function _buildTree($i = 0, &$children = []) /** * Try to merge directly children into an existing node. - * - * @param array &$children Current children being gathering. - * @param array &$handle Children of the new node. - * @param string $cId Node ID. - * @param bool $recursive Whether we should merge recursively or - * not. - * @return bool */ protected function mergeTree( &$children, &$handle, - $cId, - $recursive = false - ) { + string $cId, + bool $recursive = false + ): bool { end($children); $last = current($children); @@ -657,10 +627,6 @@ protected function mergeTree( /** * Merge recursively. * Please, see self::mergeTree() to know the context. - * - * @param \Hoa\Compiler\Llk\TreeNode $node Node that receives. - * @param \Hoa\Compiler\Llk\TreeNode $newNode Node to merge. - * @return void */ protected function mergeTreeRecursive(TreeNode $node, TreeNode $newNode) { @@ -693,61 +659,48 @@ protected function mergeTreeRecursive(TreeNode $node, TreeNode $newNode) /** * Get AST. - * - * @return \Hoa\Compiler\Llk\TreeNode */ - public function getTree() + public function getTree(): ?TreeNode { return $this->_tree; } /** * Get trace. - * - * @return array */ - public function getTrace() + public function getTrace(): array { return $this->_trace; } /** * Get pragmas. - * - * @return array */ - public function getPragmas() + public function getPragmas(): array { return $this->_pragmas; } /** * Get tokens. - * - * @return array */ - public function getTokens() + public function getTokens(): array { return $this->_tokens; } /** * Get the lexer iterator. - * - * @return \Hoa\Iterator\Buffer */ - public function getTokenSequence() + public function getTokenSequence(): ?Iterator\Buffer { return $this->_tokenSequence; } /** * Get rule by name. - * - * @param string $name Rule name. - * @return \Hoa\Compiler\Llk\Rule */ - public function getRule($name) + public function getRule($name): ?Rule { if (!isset($this->_rules[$name])) { return null; @@ -758,21 +711,21 @@ public function getRule($name) /** * Get rules. - * - * @return array */ - public function getRules() + public function getRules(): array { return $this->_rules; } /** * Get root rule. - * - * @return string */ - public function getRootRule() + public function getRootRule(): ?string { + if (empty($this->_rules)) { + return null; + } + foreach ($this->_rules as $rule => $_) { if (!is_int($rule)) { break; diff --git a/Llk/Rule/Analyzer.php b/Source/Llk/Rule/Analyzer.php similarity index 93% rename from Llk/Rule/Analyzer.php rename to Source/Llk/Rule/Analyzer.php index 5156cef3..9bb06b74 100644 --- a/Llk/Rule/Analyzer.php +++ b/Source/Llk/Rule/Analyzer.php @@ -1,5 +1,7 @@ _tokens as $namespace => $tokens) { foreach ($tokens as $token => $value) { if ($token === $tokenName || - substr($token, 0, strpos($token, ':')) === $tokenName) { + substr($token, 0, (int) strpos($token, ':')) === $tokenName) { $exists = true; break 2; @@ -470,8 +450,8 @@ protected function simple(&$pNodeId) foreach ($this->_tokens as $namespace => $tokens) { foreach ($tokens as $token => $value) { - if ($token === $tokenName - || substr($token, 0, strpos($token, ':')) === $tokenName) { + if ($token === $tokenName || + substr($token, 0, (int) strpos($token, ':')) === $tokenName) { $exists = true; break 2; diff --git a/Llk/Rule/Choice.php b/Source/Llk/Rule/Choice.php similarity index 95% rename from Llk/Rule/Choice.php rename to Source/Llk/Rule/Choice.php index 99d4ff45..e5207125 100644 --- a/Llk/Rule/Choice.php +++ b/Source/Llk/Rule/Choice.php @@ -1,5 +1,7 @@ _rule = $rule; $this->_data = $data; - $this->_todo = $todo; + $this->_todo = $todo ?? []; $this->_depth = $depth; $this->_transitional = is_int($rule); @@ -109,8 +103,6 @@ public function __construct( /** * Get rule name. - * - * @return string */ public function getRule() { @@ -119,8 +111,6 @@ public function getRule() /** * Get data. - * - * @return mixed */ public function getData() { @@ -129,21 +119,16 @@ public function getData() /** * Get todo sequence. - * - * @return array */ - public function getTodo() + public function getTodo(): array { return $this->_todo; } /** * Set depth in trace. - * - * @param int $depth Depth. - * @return int */ - public function setDepth($depth) + public function setDepth(int $depth): int { $old = $this->_depth; $this->_depth = $depth; @@ -153,20 +138,16 @@ public function setDepth($depth) /** * Get depth in trace. - * - * @return int */ - public function getDepth() + public function getDepth(): int { return $this->_depth; } /** * Check whether the rule is transitional or not. - * - * @return bool */ - public function isTransitional() + public function isTransitional(): bool { return $this->_transitional; } diff --git a/Llk/Rule/Repetition.php b/Source/Llk/Rule/Repetition.php similarity index 82% rename from Llk/Rule/Repetition.php rename to Source/Llk/Rule/Repetition.php index a653272f..c180006b 100644 --- a/Llk/Rule/Repetition.php +++ b/Source/Llk/Rule/Repetition.php @@ -1,5 +1,7 @@ _min; } /** * Get maximum bound. - * - * @return int */ - public function getMax() + public function getMax(): int { return $this->_max; } /** * Check whether the maximum repetition is unbounded. - * - * @return bool */ - public function isInfinite() + public function isInfinite(): bool { return -1 === $this->getMax(); } diff --git a/Llk/Rule/Rule.php b/Source/Llk/Rule/Rule.php similarity index 77% rename from Llk/Rule/Rule.php rename to Source/Llk/Rule/Rule.php index 1a0b5666..e2719bf1 100644 --- a/Llk/Rule/Rule.php +++ b/Source/Llk/Rule/Rule.php @@ -1,5 +1,7 @@ setName($name); $this->setChildren($children); @@ -125,9 +120,6 @@ public function __construct($name, $children, $nodeId = null) /** * Set rule name. - * - * @param string $name Rule name. - * @return string */ public function setName($name) { @@ -139,8 +131,6 @@ public function setName($name) /** * Get rule name. - * - * @return string */ public function getName() { @@ -149,9 +139,6 @@ public function getName() /** * Set rule's children. - * - * @param mixed $children Children. - * @return mixed */ protected function setChildren($children) { @@ -163,8 +150,6 @@ protected function setChildren($children) /** * Get rule's children. - * - * @return mixed */ public function getChildren() { @@ -173,20 +158,17 @@ public function getChildren() /** * Set node ID. - * - * @param string $nodeId Node ID. - * @return string */ - public function setNodeId($nodeId) + public function setNodeId(?string $nodeId): ?string { $old = $this->_nodeId; - if (false !== $pos = strpos($nodeId, ':')) { - $this->_nodeId = substr($nodeId, 0, $pos); - $this->_nodeOptions = str_split(substr($nodeId, $pos + 1)); - } else { + if (empty($nodeId) || false === $pos = strpos($nodeId, ':')) { $this->_nodeId = $nodeId; $this->_nodeOptions = []; + } else { + $this->_nodeId = substr($nodeId, 0, $pos); + $this->_nodeOptions = str_split(substr($nodeId, $pos + 1)); } return $old; @@ -194,31 +176,24 @@ public function setNodeId($nodeId) /** * Get node ID. - * - * @return string */ - public function getNodeId() + public function getNodeId(): ?string { return $this->_nodeId; } /** * Get node options. - * - * @retrun array */ - public function getNodeOptions() + public function getNodeOptions(): array { return $this->_nodeOptions; } /** * Set default ID. - * - * @param string $defaultId Default ID. - * @return string */ - public function setDefaultId($defaultId) + public function setDefaultId(string $defaultId): ?string { $old = $this->_defaultId; @@ -235,31 +210,24 @@ public function setDefaultId($defaultId) /** * Get default ID. - * - * @return string */ - public function getDefaultId() + public function getDefaultId(): ?string { return $this->_defaultId; } /** * Get default options. - * - * @return array */ - public function getDefaultOptions() + public function getDefaultOptions(): array { return $this->_defaultOptions; } /** * Set PP representation of the rule. - * - * @param string $pp PP representation. - * @return string */ - public function setPPRepresentation($pp) + public function setPPRepresentation(string $pp): ?string { $old = $this->_pp; $this->_pp = $pp; @@ -270,20 +238,16 @@ public function setPPRepresentation($pp) /** * Get PP representation of the rule. - * - * @return string */ - public function getPPRepresentation() + public function getPPRepresentation(): ?string { return $this->_pp; } /** * Check whether the rule is transitional or not. - * - * @return bool */ - public function isTransitional() + public function isTransitional(): bool { return $this->_transitional; } @@ -292,4 +256,4 @@ public function isTransitional() /** * Flex entity. */ -Consistency::flexEntity('Hoa\Compiler\Llk\Rule\Rule'); +Consistency::flexEntity(Rule::class); diff --git a/Llk/Rule/Token.php b/Source/Llk/Rule/Token.php similarity index 74% rename from Llk/Rule/Token.php rename to Source/Llk/Rule/Token.php index bd49564a..7081cb0a 100644 --- a/Llk/Rule/Token.php +++ b/Source/Llk/Rule/Token.php @@ -1,5 +1,7 @@ _tokenName; } /** * Set token namespace. - * - * @param string $namespace Namespace. - * @return string */ - public function setNamespace($namespace) + public function setNamespace(string $namespace): ?string { $old = $this->_namespace; $this->_namespace = $namespace; @@ -163,21 +151,16 @@ public function setNamespace($namespace) /** * Get token namespace. - * - * @return string */ - public function getNamespace() + public function getNamespace(): ?string { return $this->_namespace; } /** * Set representation. - * - * @param string $regex Representation. - * @return string */ - public function setRepresentation($regex) + public function setRepresentation(string $regex): ?string { $old = $this->_regex; $this->_regex = $regex; @@ -187,20 +170,16 @@ public function setRepresentation($regex) /** * Get token representation. - * - * @return string */ - public function getRepresentation() + public function getRepresentation(): ?string { return $this->_regex; } /** * Get AST of the token representation. - * - * @return \Hoa\Compiler\Llk\TreeNode */ - public function getAST() + public function getAST(): Compiler\Llk\TreeNode { if (null === static::$_regexCompiler) { $stream = new File\Read('hoa://Library/Regex/Grammar.pp'); @@ -220,11 +199,8 @@ public function getAST() /** * Set token value. - * - * @param string $value Value. - * @return string */ - public function setValue($value) + public function setValue(string $value): ?string { $old = $this->_value; $this->_value = $value; @@ -234,21 +210,16 @@ public function setValue($value) /** * Get token value. - * - * @return string */ - public function getValue() + public function getValue(): ?string { return $this->_value; } /** * Set token offset. - * - * @param int $offset Offset. - * @return int */ - public function setOffset($offset) + public function setOffset(int $offset): int { $old = $this->_offset; $this->_offset = $offset; @@ -258,21 +229,16 @@ public function setOffset($offset) /** * Get token offset. - * - * @return int */ - public function getOffset() + public function getOffset(): int { return $this->_offset; } /** * Set whether the token is kept or not in the AST. - * - * @param bool $kept Kept. - * @return bool */ - public function setKept($kept) + public function setKept(bool $kept): bool { $old = $this->_kept; $this->_kept = $kept; @@ -282,20 +248,16 @@ public function setKept($kept) /** * Check whether the token is kept in the AST or not. - * - * @return bool */ - public function isKept() + public function isKept(): bool { return $this->_kept; } /** * Get unification index. - * - * @return int */ - public function getUnificationIndex() + public function getUnificationIndex(): int { return $this->_unification; } diff --git a/Llk/Sampler/BoundedExhaustive.php b/Source/Llk/Sampler/BoundedExhaustive.php similarity index 88% rename from Llk/Sampler/BoundedExhaustive.php rename to Source/Llk/Sampler/BoundedExhaustive.php index 3dca3c43..1c630086 100644 --- a/Llk/Sampler/BoundedExhaustive.php +++ b/Source/Llk/Sampler/BoundedExhaustive.php @@ -1,5 +1,7 @@ setLength($length); + $this->setLength($maximumLength); return; } /** * Get the current iterator value. - * - * @return string */ - public function current() + public function current(): ?string { return $this->_current; } /** * Get the current iterator key. - * - * @return int */ - public function key() + public function key(): int { return $this->_key; } /** * Useless here. - * - * @return void */ public function next() { @@ -140,8 +129,6 @@ public function next() /** * Rewind the internal iterator pointer. - * - * @return void */ public function rewind() { @@ -160,10 +147,8 @@ public function rewind() /** * Compute the current iterator value, i.e. generate a new solution. - * - * @return bool */ - public function valid() + public function valid(): bool { if (false === $this->unfold()) { return false; @@ -185,10 +170,8 @@ public function valid() /** * Unfold rules from the todo stack. - * - * @return bool */ - protected function unfold() + protected function unfold(): bool { while (0 < count($this->_todo)) { $pop = array_pop($this->_todo); @@ -212,12 +195,8 @@ protected function unfold() /** * The bounded-exhaustive algorithm. - * - * @param \Hoa\Compiler\Llk\Rule $rule Rule to cover. - * @param int $next Next rule. - * @return bool */ - protected function boundedExhaustive(Compiler\Llk\Rule $rule, $next) + protected function boundedExhaustive(Compiler\Llk\Rule $rule, int $next): bool { $children = $rule->getChildren(); @@ -322,10 +301,8 @@ protected function boundedExhaustive(Compiler\Llk\Rule $rule, $next) /** * Backtrack to the previous choice-point. - * - * @return bool */ - protected function backtrack() + protected function backtrack(): bool { $found = false; @@ -359,12 +336,8 @@ protected function backtrack() /** * Set upper-bound, the maximum data length. - * - * @param int $length Length. - * @return int - * @throws \Hoa\Compiler\Exception */ - public function setLength($length) + public function setLength(int $length): int { if (0 >= $length) { throw new Exception( @@ -382,10 +355,8 @@ public function setLength($length) /** * Get upper-bound. - * - * @return int */ - public function getLength() + public function getLength(): int { return $this->_length; } diff --git a/Llk/Sampler/Coverage.php b/Source/Llk/Sampler/Coverage.php similarity index 93% rename from Llk/Sampler/Coverage.php rename to Source/Llk/Sampler/Coverage.php index 1755ca3b..c7b8ed77 100644 --- a/Llk/Sampler/Coverage.php +++ b/Source/Llk/Sampler/Coverage.php @@ -1,5 +1,7 @@ _current; } /** * Get the current iterator key. - * - * @return int */ - public function key() + public function key(): int { return $this->_key; } /** * Useless here. - * - * @return void */ public function next() { @@ -138,8 +131,6 @@ public function next() /** * Rewind the internal iterator pointer. - * - * @return void */ public function rewind() { @@ -179,10 +170,8 @@ public function rewind() /** * Compute the current iterator value, i.e. generate a new solution. - * - * @return bool */ - public function valid() + public function valid(): bool { $ruleName = $this->_rootRuleName; @@ -228,10 +217,8 @@ public function valid() /** * Unfold rules from the todo stack. - * - * @return bool */ - protected function unfold() + protected function unfold(): bool { while (0 < count($this->_todo)) { $pop = array_pop($this->_todo); @@ -253,11 +240,8 @@ protected function unfold() /** * The coverage algorithm. - * - * @param \Hoa\Compiler\Llk\Rule $rule Rule to cover. - * @return bool */ - protected function coverage(Compiler\Llk\Rule $rule) + protected function coverage(Compiler\Llk\Rule $rule): bool { $children = $rule->getChildren(); @@ -457,11 +441,8 @@ protected function coverage(Compiler\Llk\Rule $rule) /** * Extract a given sequence from existing traces. - * - * @param array $rules Rules to consider. - * @return array */ - protected function extract(array $rules) + protected function extract(array $rules): ?array { $out = []; @@ -521,10 +502,8 @@ protected function extract(array $rules) /** * Backtrack to the previous choice-point. - * - * @return bool */ - protected function backtrack() + protected function backtrack(): bool { $found = false; @@ -556,9 +535,6 @@ protected function backtrack() /** * Update coverage of a rule. - * - * @param \Hoa\Compiler\Llk\Rule\Ekzit $rule Rule to consider. - * @return void */ protected function updateCoverage(Compiler\Llk\Rule\Ekzit $Rule) { @@ -611,11 +587,8 @@ protected function updateCoverage(Compiler\Llk\Rule\Ekzit $Rule) /** * Check if all rules have been entirely covered. - * - * @param string $ruleName Rule name. - * @return bool */ - protected function allCovered($ruleName) + protected function allCovered($ruleName): bool { foreach ($this->_coveredRules[$ruleName] as $value) { if (1 !== $value) { @@ -628,11 +601,8 @@ protected function allCovered($ruleName) /** * Check if a rule is a root rule that is currently being processed. - * - * @param string $ruleName Rule name. - * @return bool */ - protected function checkRuleRoot($ruleName) + protected function checkRuleRoot($ruleName): bool { if (true === $this->_rules[$ruleName]->isTransitional()) { return false; diff --git a/Llk/Sampler/Exception.php b/Source/Llk/Sampler/Exception.php similarity index 95% rename from Llk/Sampler/Exception.php rename to Source/Llk/Sampler/Exception.php index 453ab777..bba9631f 100644 --- a/Llk/Sampler/Exception.php +++ b/Source/Llk/Sampler/Exception.php @@ -1,5 +1,7 @@ _compiler; } /** * Get the AST of the current namespace skip token. - * - * @return \Hoa\Compiler\Llk\TreeNode */ - protected function getSkipTokenAST() + protected function getSkipTokenAST(): Compiler\Llk\TreeNode { if (!isset($this->_skipTokenAST[$this->_currentNamespace])) { $token = new Compiler\Llk\Rule\Token( @@ -157,11 +149,8 @@ protected function getSkipTokenAST() /** * Complete a token (namespace and representation). * It returns the next namespace. - * - * @param \Hoa\Compiler\Llk\Rule\Token $token Token. - * @return string */ - protected function completeToken(Compiler\Llk\Rule\Token $token) + protected function completeToken(Compiler\Llk\Rule\Token $token): string { if (null !== $token->getRepresentation()) { return $this->_currentNamespace; @@ -196,11 +185,8 @@ protected function completeToken(Compiler\Llk\Rule\Token $token) /** * Set current token namespace. - * - * @param string $namespace Token namespace. - * @return string */ - protected function setCurrentNamespace($namespace) + protected function setCurrentNamespace(string $namespace): string { $old = $this->_currentNamespace; $this->_currentNamespace = $namespace; @@ -211,11 +197,8 @@ protected function setCurrentNamespace($namespace) /** * Generate a token value. * Complete and set next token namespace. - * - * @param \Hoa\Compiler\Llk\Rule\Token $token Token. - * @return string */ - protected function generateToken(Compiler\Llk\Rule\Token $token) + protected function generateToken(Compiler\Llk\Rule\Token $token): string { $toNamespace = $this->completeToken($token); $this->setCurrentNamespace($toNamespace); @@ -233,4 +216,4 @@ protected function generateToken(Compiler\Llk\Rule\Token $token) /** * Flex entity. */ -Consistency::flexEntity('Hoa\Compiler\Llk\Sampler\Sampler'); +Consistency::flexEntity(Sampler::class); diff --git a/Llk/Sampler/Uniform.php b/Source/Llk/Sampler/Uniform.php similarity index 90% rename from Llk/Sampler/Uniform.php rename to Source/Llk/Sampler/Uniform.php index f15ac9dc..151080c8 100644 --- a/Llk/Sampler/Uniform.php +++ b/Source/Llk/Sampler/Uniform.php @@ -1,5 +1,7 @@ _rules[$this->_rootRuleName]; @@ -170,12 +162,8 @@ public function uniform(Compiler\Llk\Rule $rule = null, $n = -1) /** * Recursive method applied to our problematic. - * - * @param \Hoa\Compiler\Llk\Rule $rule Rule to start. - * @param int $n Size. - * @return int */ - public function count(Compiler\Llk\Rule $rule = null, $n = -1) + public function count(Compiler\Llk\Rule $rule = null, int $n = -1): int { if (null === $rule || -1 === $n) { return 0; @@ -265,12 +253,8 @@ public function count(Compiler\Llk\Rule $rule = null, $n = -1) /** * Set upper-bound, the maximum data length. - * - * @param int $length Length. - * @return int - * @throws \Hoa\Compiler\Exception */ - public function setLength($length) + public function setLength(int $length): int { if (0 >= $length) { throw new Exception( @@ -292,10 +276,8 @@ public function setLength($length) /** * Get upper-bound. - * - * @return int */ - public function getLength() + public function getLength(): int { return $this->_length; } diff --git a/Llk/TreeNode.php b/Source/Llk/TreeNode.php similarity index 73% rename from Llk/TreeNode.php rename to Source/Llk/TreeNode.php index 1ea4e781..23bb1934 100644 --- a/Llk/TreeNode.php +++ b/Source/Llk/TreeNode.php @@ -1,5 +1,7 @@ _id; $this->_id = $id; @@ -130,21 +126,16 @@ public function setId($id) /** * Get ID. - * - * @return string */ - public function getId() + public function getId(): string { return $this->_id; } /** * Set value. - * - * @param array $value Value (token & value). - * @return array */ - public function setValue(array $value) + public function setValue(array $value): ?array { $old = $this->_value; $this->_value = $value; @@ -154,20 +145,16 @@ public function setValue(array $value) /** * Get value. - * - * @return array */ - public function getValue() + public function getValue(): ?array { return $this->_value; } /** * Get value token. - * - * @return string */ - public function getValueToken() + public function getValueToken(): ?string { return isset($this->_value['token']) @@ -177,10 +164,8 @@ public function getValueToken() /** * Get value value. - * - * @return string */ - public function getValueValue() + public function getValueValue(): ?string { return isset($this->_value['value']) @@ -190,10 +175,8 @@ public function getValueValue() /** * Get token offset. - * - * @return int */ - public function getOffset() + public function getOffset(): int { return isset($this->_value['offset']) @@ -203,21 +186,16 @@ public function getOffset() /** * Check if the node represents a token or not. - * - * @return bool */ - public function isToken() + public function isToken(): bool { return !empty($this->_value); } /** * Prepend a child. - * - * @param \Hoa\Compiler\Llk\TreeNode $child Child. - * @return \Hoa\Compiler\Llk\TreeNode */ - public function prependChild(TreeNode $child) + public function prependChild(self $child): self { array_unshift($this->_children, $child); @@ -226,11 +204,8 @@ public function prependChild(TreeNode $child) /** * Append a child. - * - * @param \Hoa\Compiler\Llk\TreeNode $child Child. - * @return \Hoa\Compiler\Llk\TreeNode */ - public function appendChild(TreeNode $child) + public function appendChild(self $child): self { $this->_children[] = $child; @@ -239,11 +214,8 @@ public function appendChild(TreeNode $child) /** * Set children. - * - * @param array $children Children. - * @return array */ - public function setChildren(array $children) + public function setChildren(array $children): array { $old = $this->_children; $this->_children = $children; @@ -253,11 +225,8 @@ public function setChildren(array $children) /** * Get child. - * - * @param int $i Index. - * @return \Hoa\Compiler\Llk\TreeNode */ - public function getChild($i) + public function getChild(int $i): ?TreeNode { return true === $this->childExists($i) @@ -267,42 +236,32 @@ public function getChild($i) /** * Get children. - * - * @return array */ - public function getChildren() + public function getChildren(): array { return $this->_children; } /** * Get number of children. - * - * @return int */ - public function getChildrenNumber() + public function getChildrenNumber(): int { return count($this->_children); } /** * Check if a child exists. - * - * @param int $i Index. - * @return bool */ - public function childExists($i) + public function childExists(int $i): bool { return array_key_exists($i, $this->_children); } /** * Set parent. - * - * @param \Hoa\Compiler\Llk\TreeNode $parent Parent. - * @return \Hoa\Compiler\Llk\TreeNode */ - public function setParent(TreeNode $parent) + public function setParent(self $parent): ?self { $old = $this->_parent; $this->_parent = $parent; @@ -312,31 +271,22 @@ public function setParent(TreeNode $parent) /** * Get parent. - * - * @return \Hoa\Compiler\Llk\TreeNode */ - public function getParent() + public function getParent(): ?self { return $this->_parent; } /** * Get data. - * - * @return array */ - public function &getData() + public function &getData(): array { return $this->_data; } /** * Accept a visitor. - * - * @param \Hoa\Visitor\Visit $visitor Visitor. - * @param mixed &$handle Handle (reference). - * @param mixed $eldnah Handle (no reference). - * @return mixed */ public function accept( Visitor\Visit $visitor, @@ -348,8 +298,6 @@ public function accept( /** * Remove circular reference to the parent (help the garbage collector). - * - * @return void */ public function __destruct() { diff --git a/Visitor/Dump.php b/Source/Visitor/Dump.php similarity index 87% rename from Visitor/Dump.php rename to Source/Visitor/Dump.php index f739b8b2..68d851e2 100644 --- a/Visitor/Dump.php +++ b/Source/Visitor/Dump.php @@ -1,5 +1,7 @@ given( - $stream = new File\Read('hoa://Library/Compiler/Llk/Llk.pp'), + $stream = new File\Read('hoa://Library/Compiler/Source/Llk/Llk.pp'), $parser = SUT::load($stream) ) ->when($result = SUT::save($parser, 'Foobar')) diff --git a/Test/Unit/Llk/Rule/Choice.php b/Test/Unit/Llk/Rule/Choice.php index a2ba2211..8fe8a546 100644 --- a/Test/Unit/Llk/Rule/Choice.php +++ b/Test/Unit/Llk/Rule/Choice.php @@ -1,5 +1,7 @@ isEqualTo($rule) ->string($result->getData()) ->isEqualTo($data) - ->variable($result->getTodo()) - ->isNull() + ->array($result->getTodo()) + ->isEmpty() ->integer($result->getDepth()) ->isEqualTo(-1) ->boolean($result->isTransitional()) diff --git a/Test/Unit/Llk/Rule/Repetition.php b/Test/Unit/Llk/Rule/Repetition.php index a11523da..9afdd535 100644 --- a/Test/Unit/Llk/Rule/Repetition.php +++ b/Test/Unit/Llk/Rule/Repetition.php @@ -1,5 +1,7 @@ =5.5.0", - "hoa/consistency": "~1.0", - "hoa/exception" : "~1.0", - "hoa/file" : "~1.0", - "hoa/iterator" : "~2.0", - "hoa/math" : "~1.0", - "hoa/protocol" : "~1.0", - "hoa/regex" : "~1.0", - "hoa/visitor" : "~2.0" + "php" : ">=7.1", + "hoa/consistency": "dev-master", + "hoa/exception" : "dev-master", + "hoa/file" : "dev-master", + "hoa/iterator" : "dev-master", + "hoa/math" : "dev-master", + "hoa/protocol" : "dev-master", + "hoa/regex" : "dev-master", + "hoa/visitor" : "dev-master" }, "require-dev": { - "hoa/json": "~2.0", - "hoa/test": "~2.0" + "hoa/json": "dev-master", + "hoa/test": "dev-master" }, "autoload": { "psr-4": { - "Hoa\\Compiler\\": "." + "Hoa\\Compiler\\" : "Source", + "Hoa\\Compiler\\Bin\\" : "Bin", + "Hoa\\Compiler\\Test\\": "Test" } }, "extra": {