aboutsummaryrefslogtreecommitdiffstats
path: root/Llk
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-04 20:11:09 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-05 09:40:42 +0200
commit5b971e8745c6428d9bd7662e1ed2610681c1851e (patch)
tree914269afc3030c0384be6a3ca28345bfcd1d3372 /Llk
parent366ccd14d612fdc172e4ad5bae7bf24eb0d25caa (diff)
downloadCompiler-5b971e8745c6428d9bd7662e1ed2610681c1851e.zip
Compiler-5b971e8745c6428d9bd7662e1ed2610681c1851e.tar.gz
Compiler-5b971e8745c6428d9bd7662e1ed2610681c1851e.tar.bz2
Move to PSR-1 and PSR-2.
Diffstat (limited to 'Llk')
-rw-r--r--Llk/Lexer.php129
-rw-r--r--Llk/Llk.php121
-rw-r--r--Llk/Parser.php397
-rw-r--r--Llk/Rule/Analyzer.php255
-rw-r--r--Llk/Rule/Choice.php11
-rw-r--r--Llk/Rule/Concatenation.php11
-rw-r--r--Llk/Rule/Ekzit.php11
-rw-r--r--Llk/Rule/Entry.php11
-rw-r--r--Llk/Rule/Invocation.php61
-rw-r--r--Llk/Rule/Repetition.php30
-rw-r--r--Llk/Rule/Rule.php111
-rw-r--r--Llk/Rule/Token.php93
-rw-r--r--Llk/Sampler/BoundedExhaustive.php172
-rw-r--r--Llk/Sampler/Coverage.php375
-rw-r--r--Llk/Sampler/Exception.php10
-rw-r--r--Llk/Sampler/Sampler.php70
-rw-r--r--Llk/Sampler/Uniform.php168
-rw-r--r--Llk/TreeNode.php134
18 files changed, 1031 insertions, 1139 deletions
diff --git a/Llk/Lexer.php b/Llk/Lexer.php
index 3e43b22..9999a13 100644
--- a/Llk/Lexer.php
+++ b/Llk/Lexer.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,39 +43,36 @@ use Hoa\Compiler;
*
* PP lexer.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Lexer {
-
+class Lexer
+{
/**
* Lexer state.
*
- * @var \Hoa\Compiler\Llk\Lexer array
+ * @var array
*/
protected $_lexerState = null;
/**
* Text.
*
- * @var \Hoa\Compiler\Llk\Lexer string
+ * @var string
*/
protected $_text = null;
/**
* Tokens.
*
- * @var \Hoa\Compiler\Llk\Lexer array
+ * @var array
*/
protected $_tokens = [];
/**
* Namespace stacks.
*
- * @var \SplStack object
+ * @var \SplStack
*/
protected $_nsStack = null;
@@ -85,14 +82,13 @@ class Lexer {
* Text tokenizer: splits the text in parameter in an ordered array of
* tokens.
*
- * @access protected
* @param string $text Text to tokenize.
* @param array $tokens Tokens to be returned.
* @return array
- * @throw \Hoa\Compiler\Exception\UnrecognizedToken
+ * @throws \Hoa\Compiler\Exception\UnrecognizedToken
*/
- public function lexMe ( $text, Array $tokens ) {
-
+ public function lexMe($text, Array $tokens)
+ {
$this->_text = $text;
$this->_tokens = $tokens;
$this->_nsStack = null;
@@ -102,15 +98,13 @@ class Lexer {
$this->_lexerState = 'default';
$stack = false;
- foreach($this->_tokens as &$tokens) {
-
+ foreach ($this->_tokens as &$tokens) {
$_tokens = [];
- foreach($tokens as $fullLexeme => $regex) {
-
- if(false === strpos($fullLexeme, ':')) {
-
+ foreach ($tokens as $fullLexeme => $regex) {
+ if (false === strpos($fullLexeme, ':')) {
$_tokens[$fullLexeme] = [$regex, null];
+
continue;
}
@@ -125,26 +119,30 @@ class Lexer {
$tokens = $_tokens;
}
- if(true == $stack)
+ if (true == $stack) {
$this->_nsStack = new \SplStack();
+ }
- while($offset < $maxOffset) {
-
+ while ($offset < $maxOffset) {
$nextToken = $this->nextToken($offset);
- if(null === $nextToken)
+ if (null === $nextToken) {
throw new Compiler\Exception\UnrecognizedToken(
'Unrecognized token "%s" at line 1 and column %d:' .
"\n" . '%s' . "\n" .
str_repeat(' ', mb_strlen(substr($text, 0, $offset))) . '↑',
- 0, [
+ 0,
+ [
mb_substr(substr($text, $offset), 0, 1),
$offset + 1,
$text
- ], 1, $offset);
-
- if(true === $nextToken['keep']) {
+ ],
+ 1,
+ $offset
+ );
+ }
+ if (true === $nextToken['keep']) {
$nextToken['offset'] = $offset;
$tokenized[] = $nextToken;
}
@@ -167,60 +165,73 @@ class Lexer {
/**
* Compute the next token recognized at the beginning of the string.
*
- * @access protected
* @param int $offset Offset.
* @return array
- * @throw \Hoa\Compiler\Exception\UnrecognizedToken
+ * @throws \Hoa\Compiler\Exception\UnrecognizedToken
*/
- protected function nextToken ( $offset ) {
-
+ protected function nextToken($offset)
+ {
$tokenArray = &$this->_tokens[$this->_lexerState];
- foreach($tokenArray as $lexeme => $bucket) {
-
+ foreach ($tokenArray as $lexeme => $bucket) {
list($regex, $nextState) = $bucket;
- if(null === $nextState)
+ if (null === $nextState) {
$nextState = $this->_lexerState;
+ }
$out = $this->matchLexeme($lexeme, $regex, $offset);
- if(null !== $out) {
-
+ if (null !== $out) {
$out['namespace'] = $this->_lexerState;
$out['keep'] = 'skip' !== $lexeme;
- if($nextState !== $this->_lexerState) {
-
+ if ($nextState !== $this->_lexerState) {
$shift = false;
- if( null !== $this->_nsStack
- && 0 !== preg_match('#^__shift__(?:\s*\*\s*(\d+))?$#', $nextState, $matches)) {
-
+ if (null !== $this->_nsStack &&
+ 0 !== preg_match('#^__shift__(?:\s*\*\s*(\d+))?$#', $nextState, $matches)) {
$i = isset($matches[1]) ? intval($matches[1]) : 1;
- if($i > ($c = count($this->_nsStack)))
+ if ($i > ($c = count($this->_nsStack))) {
throw new Compiler\Exception\Lexer(
'Cannot shift namespace %d-times, from token ' .
'%s in namespace %s, because the stack ' .
'contains only %d namespaces.',
- 1, [$i, $lexeme, $this->_lexerState, $c]);
-
- while(1 <= $i--)
+ 1,
+ [
+ $i,
+ $lexeme,
+ $this->_lexerState,
+ $c
+ ]
+ );
+ }
+
+ while (1 <= $i--) {
$previousNamespace = $this->_nsStack->pop();
+ }
$nextState = $previousNamespace;
$shift = true;
}
- if(!isset($this->_tokens[$nextState]))
+ if (!isset($this->_tokens[$nextState])) {
throw new Compiler\Exception\Lexer(
'Namespace %s does not exist, called by token %s ' .
'in namespace %s.',
- 2, [$nextState, $lexeme, $this->_lexerState]);
+ 2,
+ [
+ $nextState,
+ $lexeme,
+ $this->_lexerState
+ ]
+ );
+ }
- if(null !== $this->_nsStack && false === $shift)
+ if (null !== $this->_nsStack && false === $shift) {
$this->_nsStack[] = $this->_lexerState;
+ }
$this->_lexerState = $nextState;
}
@@ -235,15 +246,14 @@ class Lexer {
/**
* Check if a given lexeme is matched at the beginning of the text.
*
- * @access protected
* @param string $lexeme Name of the lexeme.
* @param string $regex Regular expression describing the lexeme.
* @param int $offset Offset.
* @return array
- * @throw \Hoa\Compiler\Exception\Lexer
+ * @throws \Hoa\Compiler\Exception\Lexer
*/
- protected function matchLexeme ( $lexeme, $regex, $offset ) {
-
+ protected function matchLexeme($lexeme, $regex, $offset)
+ {
$_regex = str_replace('#', '\#', $regex);
$preg = preg_match(
'#\G(?|' . $_regex . ')#u',
@@ -253,13 +263,18 @@ class Lexer {
$offset
);
- if(0 === $preg)
+ if (0 === $preg) {
return null;
+ }
- if('' === $matches[0])
+ if ('' === $matches[0]) {
throw new Compiler\Exception\Lexer(
'A lexeme must not match an empty value, which is the ' .
- 'case of "%s" (%s).', 3, [$lexeme, $regex]);
+ 'case of "%s" (%s).',
+ 3,
+ [$lexeme, $regex]
+ );
+ }
return [
'token' => $lexeme,
diff --git a/Llk/Llk.php b/Llk/Llk.php
index 206183d..65d17dd 100644
--- a/Llk/Llk.php
+++ b/Llk/Llk.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -50,14 +50,11 @@ use Hoa\Stream;
* skipped token (::token::), kept token (<token>), token unification (token[i])
* and rule unification (rule()[j]).
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Llk {
-
+class Llk
+{
/**
* Load parser from a file that contains the grammar.
* Example:
@@ -115,28 +112,30 @@ class Llk {
* <inner>
* ::lt:: ::slash:: ::tagname[0]:: ::gt::
*
- * @access public
* @param \Hoa\Stream\IStream\In $stream Stream that contains the
* grammar.
* @return \Hoa\Compiler\Llk
- * @throw \Hoa\Compiler\Exception
+ * @throws \Hoa\Compiler\Exception
*/
- public static function load ( Stream\IStream\In $stream ) {
-
+ public static function load(Stream\IStream\In $stream)
+ {
$pp = $stream->readAll();
- if(empty($pp)) {
-
+ if (empty($pp)) {
$message = 'The grammar is empty';
- if($stream instanceof Stream\IStream\Pointable)
- if(0 < $stream->tell())
- $message .= ': the stream ' . $stream->getStreamName() .
- ' is pointable and not rewinded, maybe it ' .
- 'could be the reason';
- else
- $message .= ': nothing to read on the stream ' .
- $stream->getStreamName();
+ if ($stream instanceof Stream\IStream\Pointable) {
+ if (0 < $stream->tell()) {
+ $message .=
+ ': the stream ' . $stream->getStreamName() .
+ ' is pointable and not rewinded, maybe it ' .
+ 'could be the reason';
+ } else {
+ $message .=
+ ': nothing to read on the stream ' .
+ $stream->getStreamName();
+ }
+ }
throw new Compiler\Exception($message . '.', 0);
}
@@ -152,69 +151,68 @@ class Llk {
/**
* Parse PP.
*
- * @access public
* @param string $pp PP.
* @param array $tokens Extracted tokens.
* @param array $rules Extracted raw rules.
* @return void
- * @throw \Hoa\Compiler\Exception
+ * @throws \Hoa\Compiler\Exception
*/
- public static function parsePP ( $pp, &$tokens, &$rules ) {
-
+ public static function parsePP($pp, &$tokens, &$rules)
+ {
$lines = explode("\n", $pp);
$tokens = ['default' => []];
$rules = [];
- for($i = 0, $m = count($lines); $i < $m; ++$i) {
-
+ for ($i = 0, $m = count($lines); $i < $m; ++$i) {
$line = rtrim($lines[$i]);
- if(0 === strlen($line) || '//' == substr($line, 0, 2))
+ if (0 === strlen($line) || '//' == substr($line, 0, 2)) {
continue;
+ }
- if('%' == $line[0]) {
-
- if(0 !== preg_match(
- '#^%skip\s+(?:([^:]+):)?([^\s]+)\s+(.*)$#u',
- $line,
- $matches)) {
-
- if(empty($matches[1]))
+ if ('%' == $line[0]) {
+ if (0 !== preg_match('#^%skip\s+(?:([^:]+):)?([^\s]+)\s+(.*)$#u', $line, $matches)) {
+ if (empty($matches[1])) {
$matches[1] = 'default';
+ }
- if(!isset($tokens[$matches[1]]))
+ if (!isset($tokens[$matches[1]])) {
$tokens[$matches[1]] = [];
+ }
- if(!isset($tokens[$matches[1]]['skip']))
+ if (!isset($tokens[$matches[1]]['skip'])) {
$tokens[$matches[1]]['skip'] = $matches[3];
- else
+ } else {
$tokens[$matches[1]]['skip'] =
'(?:' . $matches[3] . ')|' .
$tokens[$matches[1]]['skip'];
- }
-
- elseif(0 !== preg_match(
- '#^%token\s+(?:([^:]+):)?([^\s]+)\s+(.*?)(?:\s+->\s+(.*))?$#u',
- $line,
- $matches)) {
-
- if(empty($matches[1]))
+ }
+ } elseif (0 !== preg_match('#^%token\s+(?:([^:]+):)?([^\s]+)\s+(.*?)(?:\s+->\s+(.*))?$#u', $line, $matches)) {
+ if (empty($matches[1])) {
$matches[1] = 'default';
+ }
- if(isset($matches[4]) && !empty($matches[4]))
+ if (isset($matches[4]) && !empty($matches[4])) {
$matches[2] = $matches[2] . ':' . $matches[4];
+ }
- if(!isset($tokens[$matches[1]]))
+ if (!isset($tokens[$matches[1]])) {
$tokens[$matches[1]] = [];
+ }
$tokens[$matches[1]][$matches[2]] = $matches[3];
- }
-
- else
+ } else {
throw new Compiler\Exception(
'Unrecognized instructions:' . "\n" .
' %s' . "\n" . 'in file %s at line %d.',
- 1, [$line, $stream->getStreamName(), $i + 1]);
+ 1,
+ [
+ $line,
+ $stream->getStreamName(),
+ $i + 1
+ ]
+ );
+ }
continue;
}
@@ -223,14 +221,12 @@ class Llk {
$rule = null;
++$i;
- while( $i < $m
- && isset($lines[$i][0])
- && (' ' == $lines[$i][0]
- || "\t" == $lines[$i][0]
- || '//' == substr($lines[$i], 0, 2))) {
-
- if('//' == substr($lines[$i], 0, 2)) {
-
+ while ($i < $m &&
+ isset($lines[$i][0]) &&
+ (' ' === $lines[$i][0] ||
+ "\t" === $lines[$i][0] ||
+ '//' === substr($lines[$i], 0, 2))) {
+ if ('//' === substr($lines[$i], 0, 2)) {
++$i;
continue;
@@ -239,8 +235,9 @@ class Llk {
$rule .= ' ' . trim($lines[$i++]);
}
- if(isset($lines[$i][0]))
+ if (isset($lines[$i][0])) {
--$i;
+ }
$rules[$ruleName] = $rule;
}
diff --git a/Llk/Parser.php b/Llk/Parser.php
index 26c3e52..17053eb 100644
--- a/Llk/Parser.php
+++ b/Llk/Parser.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,18 +43,15 @@ use Hoa\Compiler;
*
* PP parser.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Parser {
-
+class Parser
+{
/**
* List of skipped tokens.
*
- * @var \Hoa\Compiler\Llk\Parser array
+ * @var array
*/
protected $_skip = null;
@@ -62,63 +59,63 @@ class Parser {
* Associative array (token name => token regex), to be defined in
* precedence order.
*
- * @var \Hoa\Compiler\Llk\Parser array
+ * @var array
*/
protected $_tokens = null;
/**
* Rules, to be defined as associative array, name => Rule object.
*
- * @var \Hoa\Compiler\Llk\Parser array
+ * @var array
*/
protected $_rules = null;
/**
* Current state of the analyzer.
*
- * @var \Hoa\Compiler\Llk\Parser int
+ * @var int
*/
protected $_currentState = 0;
/**
* Error state of the analyzer (when an error is encountered).
*
- * @var \Hoa\Compiler\Llk\Parser int
+ * @var int
*/
protected $_errorState = 0;
/**
* Current token sequence being analyzed.
*
- * @var \Hoa\Compiler\Llk\Parser array
+ * @var array
*/
protected $_tokenSequence = [];
/**
* Trace of activated rules.
*
- * @var \Hoa\Compiler\Llk\Parser array
+ * @var array
*/
protected $_trace = [];
/**
* Stack of todo list.
*
- * @var \Hoa\Compiler\Llk\Parser array
+ * @var array
*/
protected $_todo = null;
/**
* AST.
*
- * @var \Hoa\Compiler\Llk\TreeNode object
+ * @var \Hoa\Compiler\Llk\TreeNode
*/
protected $_tree = null;
/**
* Current depth while building the trace.
*
- * @var \Hoa\Compiler\Llk\Parser int
+ * @var int
*/
protected $_depth = -1;
@@ -127,14 +124,12 @@ class Parser {
/**
* Construct the parser.
*
- * @access public
* @param array $tokens Tokens.
* @param array $rules Rules.
* @return void
*/
- public function __construct ( Array $tokens = [],
- Array $rules = [] ) {
-
+ public function __construct(Array $tokens = [], Array $rules = [])
+ {
$this->_tokens = $tokens;
$this->_rules = $rules;
@@ -144,15 +139,14 @@ class Parser {
/**
* Parse :-).
*
- * @access public
* @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
- * @throw \Hoa\Compiler\Exception\UnexpectedToken
+ * @throws \Hoa\Compiler\Exception\UnexpectedToken
*/
- public function parse ( $text, $rule = null, $tree = true ) {
-
+ public function parse($text, $rule = null, $tree = true)
+ {
$lexer = new Lexer();
$this->_tokenSequence = $lexer->lexMe($text, $this->_tokens);
$this->_currentState = 0;
@@ -160,61 +154,73 @@ class Parser {
$this->_trace = [];
$this->_todo = [];
- if(false === array_key_exists($rule, $this->_rules))
+ if (false === array_key_exists($rule, $this->_rules)) {
$rule = $this->getRootRule();
+ }
$closeRule = new Rule\Ekzit($rule, 0);
$openRule = new Rule\Entry($rule, 0, [$closeRule]);
$this->_todo = [$closeRule, $openRule];
do {
-
$out = $this->unfold();
- if( null !== $out
- && 'EOF' == $this->getCurrentToken())
+ if (null !== $out &&
+ 'EOF' === $this->getCurrentToken()) {
break;
+ }
- if(false === $this->backtrack()) {
-
+ if (false === $this->backtrack()) {
$token = $this->_tokenSequence[$this->_errorState];
$offset = $token['offset'];
$line = 1;
$column = 1;
- if(!empty($text)) {
-
- if(0 === $offset)
+ if (!empty($text)) {
+ if (0 === $offset) {
$leftnl = 0;
- else
+ } else {
$leftnl = strrpos($text, "\n", -(strlen($text) - $offset) - 1) ?: 0;
+ }
$rightnl = strpos($text, "\n", $offset);
$line = substr_count($text, "\n", 0, $leftnl + 1) + 1;
$column = $offset - $leftnl + (0 === $leftnl);
- if(false !== $rightnl)
+ if (false !== $rightnl) {
$text = trim(substr($text, $leftnl, $rightnl - $leftnl), "\n");
+ }
}
throw new Compiler\Exception\UnexpectedToken(
'Unexpected token "%s" (%s) at line %d and column %d:' .
"\n" . '%s' . "\n" . str_repeat(' ', $column - 1) . '↑',
- 0, [$token['value'], $token['token'], $line, $column, $text],
- $line, $column
+ 0,
+ [
+ $token['value'],
+ $token['token'],
+ $line,
+ $column,
+ $text
+ ],
+ $line,
+ $column
);
}
+ } while (true);
- } while(true);
-
- if(false === $tree)
+ if (false === $tree) {
return true;
+ }
$tree = $this->_buildTree();
- if(!($tree instanceof TreeNode))
+ if (!($tree instanceof TreeNode)) {
throw new Compiler\Exception(
- 'Parsing error: cannot build AST, the trace is corrupted.', 1);
+ 'Parsing error: cannot build AST, the trace is corrupted.',
+ 1
+ );
+ }
return $this->_tree = $tree;
}
@@ -222,33 +228,31 @@ class Parser {
/**
* Unfold trace.
*
- * @access protected
* @return mixed
*/
- protected function unfold ( ) {
-
- while(0 < count($this->_todo)) {
-
+ protected function unfold()
+ {
+ while (0 < count($this->_todo)) {
$rule = array_pop($this->_todo);
- if($rule instanceof Rule\Ekzit) {
-
+ if ($rule instanceof Rule\Ekzit) {
$rule->setDepth($this->_depth);
$this->_trace[] = $rule;
- if(false === $rule->isTransitional())
+ if (false === $rule->isTransitional()) {
--$this->_depth;
- }
- else {
-
+ }
+ } else {
$ruleName = $rule->getRule();
$next = $rule->getData();
$zeRule = $this->_rules[$ruleName];
$out = $this->_parse($zeRule, $next);
- if(false === $out)
- if(false === $this->backtrack())
+ if (false === $out) {
+ if (false === $this->backtrack()) {
return null;
+ }
+ }
}
}
@@ -258,68 +262,68 @@ class Parser {
/**
* Parse current rule.
*
- * @access protected
* @param \Hoa\Compiler\Llk\Rule $zeRule Current rule.
* @param int $next Next rule index.
* @return bool
*/
- protected function _parse ( Rule $zeRule, $next ) {
-
- if($zeRule instanceof Rule\Token) {
-
+ protected function _parse(Rule $zeRule, $next)
+ {
+ if ($zeRule instanceof Rule\Token) {
$name = $this->getCurrentToken();
- if($zeRule->getTokenName() !== $name)
+ if ($zeRule->getTokenName() !== $name) {
return false;
+ }
$value = $this->getCurrentToken('value');
- if(0 <= $unification = $zeRule->getUnificationIndex())
- for($skip = 0, $i = count($this->_trace) - 1; $i >= 0; --$i) {
-
+ if (0 <= $unification = $zeRule->getUnificationIndex()) {
+ for ($skip = 0, $i = count($this->_trace) - 1; $i >= 0; --$i) {
$trace = $this->_trace[$i];
- if($trace instanceof Rule\Entry) {
-
- if(false === $trace->isTransitional()) {
-
- if($trace->getDepth() <= $this->_depth)
+ if ($trace instanceof Rule\Entry) {
+ if (false === $trace->isTransitional()) {
+ if ($trace->getDepth() <= $this->_depth) {
break;
+ }
--$skip;
}
- }
- elseif( $trace instanceof Rule\Ekzit
- && false === $trace->isTransitional())
+ } elseif ($trace instanceof Rule\Ekzit &&
+ false === $trace->isTransitional()) {
$skip += $trace->getDepth() > $this->_depth;
+ }
- if(0 < $skip)
+ if (0 < $skip) {
continue;
+ }
- if( $trace instanceof Rule\Token
- && $unification === $trace->getUnificationIndex()
- && $value != $trace->getValue())
+ if ($trace instanceof Rule\Token &&
+ $unification === $trace->getUnificationIndex() &&
+ $value !== $trace->getValue()) {
return false;
+ }
}
+ }
$namespace = $this->getCurrentToken('namespace');
$zzeRule = clone $zeRule;
$zzeRule->setValue($value);
$zzeRule->setNamespace($namespace);
- if(isset($this->_tokens[$namespace][$name]))
+ if (isset($this->_tokens[$namespace][$name])) {
$zzeRule->setRepresentation($this->_tokens[$namespace][$name]);
- else {
-
- foreach($this->_tokens[$namespace] as $_name => $regex) {
-
- if(false === $pos = strpos($_name, ':'))
+ } else {
+ foreach ($this->_tokens[$namespace] as $_name => $regex) {
+ if (false === $pos = strpos($_name, ':')) {
continue;
+ }
$_name = substr($_name, 0, $pos);
- if($_name === $name)
+ if ($_name === $name) {
break;
+ }
}
$zzeRule->setRepresentation($regex);
@@ -330,11 +334,10 @@ class Parser {
$this->_errorState = ++$this->_currentState;
return true;
- }
- elseif($zeRule instanceof Rule\Concatenation) {
-
- if(false === $zeRule->isTransitional())
+ } elseif ($zeRule instanceof Rule\Concatenation) {
+ if (false === $zeRule->isTransitional()) {
++$this->_depth;
+ }
$this->_trace[] = new Rule\Entry(
$zeRule->getName(),
@@ -344,24 +347,23 @@ class Parser {
);
$content = $zeRule->getContent();
- for($i = count($content) - 1; $i >= 0; --$i) {
-
+ for ($i = count($content) - 1; $i >= 0; --$i) {
$nextRule = $content[$i];
$this->_todo[] = new Rule\Ekzit($nextRule, 0);
$this->_todo[] = new Rule\Entry($nextRule, 0);
}
return true;
- }
- elseif($zeRule instanceof Rule\Choice) {
-
+ } elseif ($zeRule instanceof Rule\Choice) {
$content = $zeRule->getContent();
- if($next >= count($content))
+ if ($next >= count($content)) {
return false;
+ }
- if(false === $zeRule->isTransitional())
+ if (false === $zeRule->isTransitional()) {
++$this->_depth;
+ }
$this->_trace[] = new Rule\Entry(
$zeRule->getName(),
@@ -374,18 +376,16 @@ class Parser {
$this->_todo[] = new Rule\Entry($nextRule, 0);
return true;
- }
- elseif($zeRule instanceof Rule\Repetition) {
-
+ } elseif ($zeRule instanceof Rule\Repetition) {
$nextRule = $zeRule->getContent();
- if(0 === $next) {
-
+ if (0 === $next) {
$name = $zeRule->getName();
$min = $zeRule->getMin();
- if(false === $zeRule->isTransitional())
+ if (false === $zeRule->isTransitional()) {
++$this->_depth;
+ }
$this->_trace[] = new Rule\Entry(
$name,
@@ -400,20 +400,18 @@ class Parser {
$this->_todo
);
- for($i = 0; $i < $min; ++$i) {
-
+ for ($i = 0; $i < $min; ++$i) {
$this->_todo[] = new Rule\Ekzit($nextRule, 0);
$this->_todo[] = new Rule\Entry($nextRule, 0);
}
return true;
- }
- else {
-
+ } else {
$max = $zeRule->getMax();
- if(-1 != $max && $next > $max)
+ if (-1 != $max && $next > $max) {
return false;
+ }
$this->_todo[] = new Rule\Ekzit(
$zeRule->getName(),
@@ -433,34 +431,29 @@ class Parser {
/**
* Backtrack the trace.
*
- * @access protected
* @return bool
*/
- protected function backtrack ( ) {
-
+ protected function backtrack()
+ {
$found = false;
do {
-
$last = array_pop($this->_trace);
- if($last instanceof Rule\Entry) {
-
+ if ($last instanceof Rule\Entry) {
$zeRule = $this->_rules[$last->getRule()];
$found = $zeRule instanceof Rule\Choice;
- }
- elseif($last instanceof Rule\Ekzit) {
-
+ } elseif ($last instanceof Rule\Ekzit) {
$zeRule = $this->_rules[$last->getRule()];
$found = $zeRule instanceof Rule\Repetition;
- }
- elseif($last instanceof Rule\Token)
+ } elseif ($last instanceof Rule\Token) {
--$this->_currentState;
+ }
+ } while (0 < count($this->_trace) && false === $found);
- } while(0 < count($this->_trace) && false === $found);
-
- if(false === $found)
+ if (false === $found) {
return false;
+ }
$rule = $last->getRule();
$next = $last->getData() + 1;
@@ -475,113 +468,107 @@ class Parser {
* Build AST from trace.
* Walk through the trace iteratively and recursively.
*
- * @access protected
* @param int $i Current trace index.
* @param array &$children Collected children.
* @return \Hoa\Compiler\Llk\TreeNode
*/
- protected function _buildTree ( $i = 0, &$children = [] ) {
-
+ protected function _buildTree($i = 0, &$children = [])
+ {
$max = count($this->_trace);
- while($i < $max) {
-
+ while ($i < $max) {
$trace = $this->_trace[$i];
- if($trace instanceof Rule\Entry) {
-
+ if ($trace instanceof Rule\Entry) {
$ruleName = $trace->getRule();
$rule = $this->_rules[$ruleName];
$isRule = false === $trace->isTransitional();
$nextTrace = $this->_trace[$i + 1];
$id = $rule->getNodeId();
- // Optimization: skip empty trace sequence.
- if( $nextTrace instanceof Rule\Ekzit
- && $ruleName == $nextTrace->getRule()) {
-
+ // Optimization: Skip empty trace sequence.
+ if ($nextTrace instanceof Rule\Ekzit &&
+ $ruleName == $nextTrace->getRule()) {
$i += 2;
+
continue;
}
- if(true === $isRule)
+ if (true === $isRule) {
$children[] = $ruleName;
+ }
- if(null !== $id)
+ if (null !== $id) {
$children[] = [
'id' => $id,
'options' => $rule->getNodeOptions()
];
+ }
$i = $this->_buildTree($i + 1, $children);
- if(false === $isRule)
+ if (false === $isRule) {
continue;
+ }
$handle = [];
$cId = null;
$cOptions = [];
do {
-
$pop = array_pop($children);
- if(true === is_object($pop))
+ if (true === is_object($pop)) {
$handle[] = $pop;
- elseif(true === is_array($pop) && null === $cId) {
-
+ } elseif (true === is_array($pop) && null === $cId) {
$cId = $pop['id'];
$cOptions = $pop['options'];
- }
- elseif($ruleName == $pop)
+ } elseif ($ruleName == $pop) {
break;
+ }
+ } while (null !== $pop);
- } while(null !== $pop);
-
- if(null === $cId) {
-
+ if (null === $cId) {
$cId = $rule->getDefaultId();
$cOptions = $rule->getDefaultOptions();
}
- if(null === $cId) {
-
- for($j = count($handle) - 1; $j >= 0; --$j)
+ if (null === $cId) {
+ for ($j = count($handle) - 1; $j >= 0; --$j) {
$children[] = $handle[$j];
+ }
continue;
}
- if( true === in_array('M', $cOptions)
- && true === $this->mergeTree($children, $handle, $cId))
+ if (true === in_array('M', $cOptions) &&
+ true === $this->mergeTree($children, $handle, $cId)) {
continue;
+ }
- if( true === in_array('m', $cOptions)
- && true === $this->mergeTree($children, $handle, $cId, true))
+ if (true === in_array('m', $cOptions) &&
+ true === $this->mergeTree($children, $handle, $cId, true)) {
continue;
+ }
$cTree = new TreeNode($id ?: $cId);
- foreach($handle as $child) {
-
+ foreach ($handle as $child) {
$child->setParent($cTree);
$cTree->prependChild($child);
}
$children[] = $cTree;
- }
- elseif($trace instanceof Rule\Ekzit)
+ } elseif ($trace instanceof Rule\Ekzit) {
return $i + 1;
- else {
-
- if(false === $trace->isKept()) {
-
+ } else {
+ if (false === $trace->isKept()) {
++$i;
continue;
}
- $child = new TreeNode('token', [
+ $child = new TreeNode('token', [
'token' => $trace->getTokenName(),
'value' => $trace->getValue(),
'namespace' => $trace->getNamespace(),
@@ -597,7 +584,6 @@ class Parser {
/**
* Try to merge directly children into an existing node.
*
- * @access protected
* @param array &$children Current children being gathering.
* @param array &$handle Children of the new node.
* @param string $cId Node ID.
@@ -605,28 +591,32 @@ class Parser {
* not.
* @return bool
*/
- protected function mergeTree ( &$children, &$handle, $cId,
- $recursive = false ) {
-
+ protected function mergeTree(
+ &$children,
+ &$handle,
+ $cId,
+ $recursive = false
+ ) {
end($children);
$last = current($children);
- if(!is_object($last))
+ if (!is_object($last)) {
return false;
+ }
- if($cId !== $last->getId())
+ if ($cId !== $last->getId()) {
return false;
+ }
- if(true === $recursive) {
-
- foreach($handle as $child)
+ if (true === $recursive) {
+ foreach ($handle as $child) {
$this->mergeTreeRecursive($last, $child);
+ }
return true;
}
- foreach($handle as $child) {
-
+ foreach ($handle as $child) {
$last->appendChild($child);
$child->setParent($last);
}
@@ -638,17 +628,15 @@ class Parser {
* Merge recursively.
* Please, see self::mergeTree() to know the context.
*
- * @access protected
* @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 ) {
-
+ protected function mergeTreeRecursive(TreeNode $node, TreeNode $newNode)
+ {
$nNId = $newNode->getId();
- if('token' === $nNId) {
-
+ if ('token' === $nNId) {
$node->appendChild($newNode);
$newNode->setParent($node);
@@ -659,16 +647,16 @@ class Parser {
end($children);
$last = current($children);
- if($last->getId() !== $nNId) {
-
+ if ($last->getId() !== $nNId) {
$node->appendChild($newNode);
$newNode->setParent($node);
return;
}
- foreach($newNode->getChildren() as $child)
+ foreach ($newNode->getChildren() as $child) {
$this->mergeTreeRecursive($last, $child);
+ }
return;
}
@@ -676,69 +664,64 @@ class Parser {
/**
* Get current token.
*
- * @access public
* @param string $kind Token informations.
* @return mixed
*/
- public function getCurrentToken ( $kind = 'token' ) {
-
+ public function getCurrentToken($kind = 'token')
+ {
return $this->_tokenSequence[$this->_currentState][$kind];
}
/**
* Get AST.
*
- * @access public
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function getTree ( ) {
-
+ public function getTree()
+ {
return $this->_tree;
}
/**
* Get trace.
*
- * @access public
* @return array
*/
- public function getTrace ( ) {
-
+ public function getTrace()
+ {
return $this->_trace;
}
/**
* Get tokens.
*
- * @access public
* @return array
*/
- public function getTokens ( ) {
-
+ public function getTokens()
+ {
return $this->_tokens;
}
/**
* Get token sequence.
*
- * @access public
* @return array
*/
- public function getTokenSequence ( ) {
-
+ public function getTokenSequence()
+ {
return $this->_tokenSequence;
}
/**
* Get rule by name.
*
- * @access public
* @return \Hoa\Compiler\Llk\Rule
*/
- public function getRule ( $name ) {
-
- if(!isset($this->_rules[$name]))
+ public function getRule($name)
+ {
+ if (!isset($this->_rules[$name])) {
return null;
+ }
return $this->_rules[$name];
}
@@ -746,25 +729,25 @@ class Parser {
/**
* Get rules.
*
- * @access public
* @return array
*/
- public function getRules ( ) {
-
+ public function getRules()
+ {
return $this->_rules;
}
/**
* Get root rule.
*
- * @access public
* @return string
*/
- public function getRootRule ( ) {
-
- foreach($this->_rules as $rule => $_)
- if(!is_int($rule))
+ public function getRootRule()
+ {
+ foreach ($this->_rules as $rule => $_) {
+ if (!is_int($rule)) {
break;
+ }
+ }
return $rule;
}
diff --git a/Llk/Rule/Analyzer.php b/Llk/Rule/Analyzer.php
index f893c52..6b7e3d5 100644
--- a/Llk/Rule/Analyzer.php
+++ b/Llk/Rule/Analyzer.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,46 +43,43 @@ use Hoa\Compiler;
*
* Analyze rules and transform them into atomic rules operations.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Analyzer {
-
+class Analyzer
+{
/**
* Created rules.
*
- * @var \Hoa\Compiler\Llk\Rule\Analyzer array
+ * @var array
*/
protected $_createdRules = null;
/**
* Tokens representing rules.
*
- * @var \Hoa\Compiler\Llk\Rule\Analyzer array
+ * @var array
*/
protected $_tokens = null;
/**
* Rules.
*
- * @var \Hoa\Compiler\Llk\Rule\Analyzer array
+ * @var array
*/
protected $_rules = null;
/**
* Current analyzed rule.
*
- * @var \Hoa\Compiler\Llk\Rule\Analyzer string
+ * @var string
*/
protected $_rule = null;
/**
* Current analyzer state.
*
- * @var \Hoa\Compiler\Llk\Rule\Analyzer int
+ * @var int
*/
protected $_currentState = 0;
@@ -91,12 +88,11 @@ class Analyzer {
/**
* Constructor.
*
- * @access public
* @param array $tokens Tokens.
* @return void
*/
- public function __construct ( Array $tokens ) {
-
+ public function __construct(Array $tokens)
+ {
$this->_tokens = $tokens;
return;
@@ -105,26 +101,25 @@ class Analyzer {
/**
* Get created rules.
*
- * @access public
* @return array
*/
- public function getCreatedRules ( ) {
-
+ public function getCreatedRules()
+ {
return $this->_createdRules;
}
/**
* Build the analyzer of the rules (does not analyze the rules).
*
- * @access protected
* @param array $rules Rule to be analyzed.
* @return void
- * @throw \Hoa\Compiler\Exception
+ * @throws \Hoa\Compiler\Exception
*/
- public function analyzeRules ( Array $rules ) {
-
- if(empty($rules))
+ public function analyzeRules(Array $rules)
+ {
+ if (empty($rules)) {
throw new Compiler\Exception\Rule('No rules specified!', 0);
+ }
$tokens = ['default' =>
[
@@ -149,16 +144,14 @@ class Analyzer {
$this->_createdRules = [];
$this->_rules = $rules;
- foreach($rules as $key => $value) {
-
+ foreach ($rules as $key => $value) {
$lexer = new Compiler\Llk\Lexer();
$this->_tokenSequence = $lexer->lexMe($value, $tokens);
$this->_rule = $value;
$this->_currentState = 0;
$nodeId = null;
- if('#' === $key[0]) {
-
+ if ('#' === $key[0]) {
$nodeId = $key;
$key = substr($key, 1);
}
@@ -166,16 +159,21 @@ class Analyzer {
$pNodeId = $nodeId;
$rule = $this->rule($pNodeId);
- if(null === $rule)
+ if (null === $rule) {
throw new Compiler\Exception(
- 'Error while parsing rule %s.', 1, $key);
+ 'Error while parsing rule %s.',
+ 1,
+ $key
+ );
+ }
$zeRule = $this->_createdRules[$rule];
$zeRule->setName($key);
$zeRule->setPPRepresentation($value);
- if(null !== $nodeId)
+ if (null !== $nodeId) {
$zeRule->setDefaultId($nodeId);
+ }
unset($this->_createdRules[$rule]);
$this->_createdRules[$key] = $zeRule;
@@ -187,58 +185,60 @@ class Analyzer {
/**
* Implementation of “rule”.
*
- * @access protected
* @return mixed
*/
- protected function rule ( &$pNodeId ) {
-
+ protected function rule(&$pNodeId)
+ {
return $this->choice($pNodeId);
}
/**
* Implementation of “choice”.
*
- * @access protected
* @return mixed
*/
- protected function choice ( &$pNodeId ) {
-
+ protected function choice(&$pNodeId)
+ {
$content = [];
// concatenation() …
$nNodeId = $pNodeId;
$rule = $this->concatenation($nNodeId);
- if(null === $rule)
+ if (null === $rule) {
return null;
+ }
- if(null !== $nNodeId)
+ if (null !== $nNodeId) {
$this->_createdRules[$rule]->setNodeId($nNodeId);
+ }
$content[] = $rule;
$others = false;
// … ( ::or:: concatenation() )*
- while('or' == $this->getCurrentToken()) {
-
+ while ('or' === $this->getCurrentToken()) {
$this->consumeToken();
$others = true;
$nNodeId = $pNodeId;
$rule = $this->concatenation($nNodeId);
- if(null === $rule)
+ if (null === $rule) {
return null;
+ }
- if(null !== $nNodeId)
+ if (null !== $nNodeId) {
$this->_createdRules[$rule]->setNodeId($nNodeId);
+ }
$content[] = $rule;
}
$pNodeId = null;
- if(false === $others)
+ if (false === $others) {
return $rule;
+ }
$name = count($this->_createdRules) + 1;
$this->_createdRules[$name] = new Choice($name, $content, null);
@@ -249,31 +249,31 @@ class Analyzer {
/**
* Implementation of “concatenation”.
*
- * @access protected
* @return mixed
*/
- protected function concatenation ( &$pNodeId ) {
-
+ protected function concatenation(&$pNodeId)
+ {
$content = [];
// repetition() …
$rule = $this->repetition($pNodeId);
- if(null === $rule)
+ if (null === $rule) {
return null;
+ }
$content[] = $rule;
$others = false;
// … repetition()*
- while(null !== $r1 = $this->repetition($pNodeId)) {
-
+ while (null !== $r1 = $this->repetition($pNodeId)) {
$content[] = $r1;
$others = true;
}
- if(false === $others && null === $pNodeId)
+ if (false === $others && null === $pNodeId) {
return $rule;
+ }
$name = count($this->_createdRules) + 1;
$this->_createdRules[$name] = new Concatenation(
@@ -288,38 +288,42 @@ class Analyzer {
/**
* Implementation of “repetition”.
*
- * @access protected
* @return mixed
- * @throw \Hoa\Compiler\Exception
+ * @throws \Hoa\Compiler\Exception
*/
- protected function repetition ( &$pNodeId ) {
+ protected function repetition(&$pNodeId)
+ {
// simple() …
$content = $this->simple($pNodeId);
- if(null === $content)
+ if (null === $content) {
return null;
+ }
// … quantifier()?
- switch($this->getCurrentToken()) {
+ switch ($this->getCurrentToken()) {
case 'zero_or_one':
$min = 0;
$max = 1;
$this->consumeToken();
- break;
+
+ break;
case 'one_or_more':
$min = 1;
$max = -1;
$this->consumeToken();
- break;
+
+ break;
case 'zero_or_more':
$min = 0;
$max = -1;
$this->consumeToken();
- break;
+
+ break;
case 'n_to_m':
$handle = trim($this->getCurrentToken('value'), '{}');
@@ -327,42 +331,49 @@ class Analyzer {
$min = (int) trim($nm[0]);
$max = (int) trim($nm[1]);
$this->consumeToken();
- break;
+
+ break;
case 'zero_to_m':
$min = 0;
$max = (int) trim($this->getCurrentToken('value'), '{,}');
$this->consumeToken();
- break;
+
+ break;
case 'n_or_more':
$min = (int) trim($this->getCurrentToken('value'), '{,}');
$max = -1;
$this->consumeToken();
- break;
+
+ break;
case 'exactly_n':
$handle = trim($this->getCurrentToken('value'), '{}');
$min = (int) $handle;
$max = $min;
$this->consumeToken();
- break;
+
+ break;
}
// … <node>?
- if('node' == $this->getCurrentToken()) {
-
+ if ('node' === $this->getCurrentToken()) {
$pNodeId = $this->getCurrentToken('value');
$this->consumeToken();
}
- if(!isset($min))
+ if (!isset($min)) {
return $content;
+ }
- if(-1 != $max && $max < $min)
+ if (-1 != $max && $max < $min) {
throw new Compiler\Exception(
'Upper bound of iteration must be greater of ' .
- 'equal to lower bound', 2);
+ 'equal to lower bound',
+ 2
+ );
+ }
$name = count($this->_createdRules) + 1;
$this->_createdRules[$name] = new Repetition(
@@ -379,56 +390,59 @@ class Analyzer {
/**
* Implementation of “simple”.
*
- * @access protected
* @return mixed
- * @throw \Hoa\Compiler\Exception
- * @throw \Hoa\Compiler\Exception\Rule
+ * @throws \Hoa\Compiler\Exception
+ * @throws \Hoa\Compiler\Exception\Rule
*/
- protected function simple ( &$pNodeId ) {
-
- if('capturing_' == $this->getCurrentToken()) {
-
+ protected function simple(&$pNodeId)
+ {
+ if ('capturing_' === $this->getCurrentToken()) {
$this->consumeToken();
$rule = $this->choice($pNodeId);
- if(null === $rule)
+ if (null === $rule) {
return null;
+ }
- if('_capturing' != $this->getCurrentToken())
+ if ('_capturing' != $this->getCurrentToken()) {
return null;
+ }
$this->consumeToken();
return $rule;
}
- if('skipped' == $this->getCurrentToken()) {
-
+ if ('skipped' === $this->getCurrentToken()) {
$tokenName = trim($this->getCurrentToken('value'), ':');
- if(']' == substr($tokenName, -1)) {
-
+ if (']' === substr($tokenName, -1)) {
$uId = (int) substr($tokenName, strpos($tokenName, '[') + 1, -1);
$tokenName = substr($tokenName, 0, strpos($tokenName, '['));
- }
- else
+ } else {
$uId = -1;
+ }
$exists = false;
- foreach($this->_tokens as $namespace => $tokens)
- foreach($tokens as $token => $value)
- if( $token === $tokenName
- || substr($token, 0, strpos($token, ':')) === $tokenName) {
-
+ foreach ($this->_tokens as $namespace => $tokens) {
+ foreach ($tokens as $token => $value) {
+ if ($token === $tokenName ||
+ substr($token, 0, strpos($token, ':')) === $tokenName) {
$exists = true;
+
break 2;
}
+ }
+ }
- if(false == $exists)
+ if (false == $exists) {
throw new Compiler\Exception(
'Token ::%s:: does not exist in%s.',
- 3, [$tokenName, $this->_rule]);
+ 3,
+ [$tokenName, $this->_rule]
+ );
+ }
$name = count($this->_createdRules) + 1;
$this->_createdRules[$name] = new Token(
@@ -442,33 +456,36 @@ class Analyzer {
return $name;
}
- if('kept' == $this->getCurrentToken()) {
-
+ if ('kept' === $this->getCurrentToken()) {
$tokenName = trim($this->getCurrentToken('value'), '<>');
- if(']' == substr($tokenName, -1)) {
-
+ if (']' === substr($tokenName, -1)) {
$uId = (int) substr($tokenName, strpos($tokenName, '[') + 1, -1);
$tokenName = substr($tokenName, 0, strpos($tokenName, '['));
- }
- else
+ } else {
$uId = -1;
+ }
$exists = false;
- foreach($this->_tokens as $namespace => $tokens)
- foreach($tokens as $token => $value)
- if( $token === $tokenName
+ foreach ($this->_tokens as $namespace => $tokens) {
+ foreach ($tokens as $token => $value) {
+ if ($token === $tokenName
|| substr($token, 0, strpos($token, ':')) === $tokenName) {
-
$exists = true;
+
break 2;
}
+ }
+ }
- if(false == $exists)
+ if (false == $exists) {
throw new Compiler\Exception(
'Token <%s> does not exist in%s.',
- 4, [$tokenName, $this->_rule]);
+ 4,
+ [$tokenName, $this->_rule]
+ );
+ }
$name = count($this->_createdRules) + 1;
$token = new Token(
@@ -484,28 +501,29 @@ class Analyzer {
return $name;
}
- if('named' == $this->getCurrentToken()) {
-
+ if ('named' === $this->getCurrentToken()) {
$tokenName = rtrim($this->getCurrentToken('value'), '()');
- if( false === array_key_exists( $tokenName, $this->_rules)
- && false === array_key_exists('#' . $tokenName, $this->_rules))
+ if (false === array_key_exists($tokenName, $this->_rules) &&
+ false === array_key_exists('#' . $tokenName, $this->_rules)) {
throw new Compiler\Exception\Rule(
'Rule %s() does not exist.',
- 5, $tokenName);
-
- if( 0 == $this->_currentState
- && 'EOF' == $this->getNextToken()) {
+ 5,
+ $tokenName
+ );
+ }
+ if (0 == $this->_currentState &&
+ 'EOF' === $this->getNextToken()) {
$name = count($this->_createdRules) + 1;
$this->_createdRules[$name] = new Concatenation(
$name,
[$tokenName],
null
);
- }
- else
+ } else {
$name = $tokenName;
+ }
$this->consumeToken();
@@ -518,35 +536,32 @@ class Analyzer {
/**
* Get current token informations.
*
- * @access public
* @param string $kind Token information.
* @return string
*/
- public function getCurrentToken ( $kind = 'token' ) {
-
+ public function getCurrentToken($kind = 'token')
+ {
return $this->_tokenSequence[$this->_currentState][$kind];
}
/**
* Get next token informations.
*
- * @access public
* @param string $kind Token information.
* @return string
*/
- public function getNextToken ( $kind = 'token' ) {
-
+ public function getNextToken($kind = 'token')
+ {
return $this->_tokenSequence[$this->_currentState + 1][$kind];
}
/**
* Consume the current token and move to the next one.
*
- * @access public
* @return int
*/
- public function consumeToken ( ) {
-
+ public function consumeToken()
+ {
return ++$this->_currentState;
}
}
diff --git a/Llk/Rule/Choice.php b/Llk/Rule/Choice.php
index 5f8ab02..1cd6b44 100644
--- a/Llk/Rule/Choice.php
+++ b/Llk/Rule/Choice.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -41,10 +41,9 @@ namespace Hoa\Compiler\Llk\Rule;
*
* The choice rule.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Choice extends Rule { }
+class Choice extends Rule
+{
+}
diff --git a/Llk/Rule/Concatenation.php b/Llk/Rule/Concatenation.php
index 2d2cc2d..7cd1f50 100644
--- a/Llk/Rule/Concatenation.php
+++ b/Llk/Rule/Concatenation.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -41,10 +41,9 @@ namespace Hoa\Compiler\Llk\Rule;
*
* The concatenation rule.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Concatenation extends Rule { }
+class Concatenation extends Rule
+{
+}
diff --git a/Llk/Rule/Ekzit.php b/Llk/Rule/Ekzit.php
index 458beae..e900747 100644
--- a/Llk/Rule/Ekzit.php
+++ b/Llk/Rule/Ekzit.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -40,10 +40,9 @@ namespace Hoa\Compiler\Llk\Rule;
*
* The ekzit (exit) rule.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Ekzit extends Invocation { }
+class Ekzit extends Invocation
+{
+}
diff --git a/Llk/Rule/Entry.php b/Llk/Rule/Entry.php
index b6919b6..974a031 100644
--- a/Llk/Rule/Entry.php
+++ b/Llk/Rule/Entry.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -41,10 +41,9 @@ namespace Hoa\Compiler\Llk\Rule;
*
* The entry rule.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Entry extends Invocation { }
+class Entry extends Invocation
+{
+}
diff --git a/Llk/Rule/Invocation.php b/Llk/Rule/Invocation.php
index 09c7beb..4f820d3 100644
--- a/Llk/Rule/Invocation.php
+++ b/Llk/Rule/Invocation.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -41,39 +41,36 @@ namespace Hoa\Compiler\Llk\Rule;
*
* Parent of entry and ekzit rules.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-abstract class Invocation {
-
+abstract class Invocation
+{
/**
* Rule.
*
- * @var \Hoa\Compiler\Llk\Rule\Invocation string
+ * @var string
*/
protected $_rule = null;
/**
* Data.
*
- * @var \Hoa\Compiler\Llk\Rule\Invocation mixed
+ * @var mixed
*/
protected $_data = null;
/**
* Piece of todo sequence.
*
- * @var \Hoa\Compiler\Llk\Rule\Invocation array
+ * @var array
*/
protected $_todo = null;
/**
* Depth in the trace.
*
- * @var \Hoa\Compiler\Llk\Rule\Invocation int
+ * @var int
*/
protected $_depth = -1;
@@ -81,7 +78,7 @@ abstract class Invocation {
* Whether the rule is transitional or not (i.e. not declared in the grammar
* but created by the analyzer).
*
- * @var \Hoa\Compiler\Llk\Rule\Invocation bool
+ * @var bool
*/
protected $_transitional = false;
@@ -90,16 +87,18 @@ abstract class Invocation {
/**
* Constructor.
*
- * @access public
* @param string $rule Rule name.
* @param mixed $data Data.
* @param array $todo Todo.
* @param int $depth Depth.
* @return void
*/
- public function __construct ( $rule, $data, Array $todo = null,
- $depth = -1 ) {
-
+ public function __construct(
+ $rule,
+ $data,
+ Array $todo = null,
+ $depth = -1
+ ) {
$this->_rule = $rule;
$this->_data = $data;
$this->_todo = $todo;
@@ -112,45 +111,41 @@ abstract class Invocation {
/**
* Get rule name.
*
- * @access public
* @return string
*/
- public function getRule ( ) {
-
+ public function getRule()
+ {
return $this->_rule;
}
/**
* Get data.
*
- * @access public
* @return mixed
*/
- public function getData ( ) {
-
+ public function getData()
+ {
return $this->_data;
}
/**
* Get todo sequence.
*
- * @access public
* @return array
*/
- public function getTodo ( ) {
-
+ public function getTodo()
+ {
return $this->_todo;
}
/**
* Set depth in trace.
*
- * @access public
* @parma int $depth Depth.
* @return int
*/
- public function setDepth ( $depth) {
-
+ public function setDepth($depth)
+ {
$old = $this->_depth;
$this->_depth = $depth;
@@ -160,22 +155,20 @@ abstract class Invocation {
/**
* Get depth in trace.
*
- * @access public
* @return int
*/
- public function getDepth ( ) {
-
+ public function getDepth()
+ {
return $this->_depth;
}
/**
* Check whether the rule is transitional or not.
*
- * @access public
* @return bool
*/
- public function isTransitional ( ) {
-
+ public function isTransitional()
+ {
return $this->_transitional;
}
}
diff --git a/Llk/Rule/Repetition.php b/Llk/Rule/Repetition.php
index ddde59a..b7dad89 100644
--- a/Llk/Rule/Repetition.php
+++ b/Llk/Rule/Repetition.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -41,25 +41,22 @@ namespace Hoa\Compiler\Llk\Rule;
*
* The repetition rule.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Repetition extends Rule {
-
+class Repetition extends Rule
+{
/**
* Minimum bound.
*
- * @var \Hoa\Compiler\Llk\Rule\Repetition int
+ * @var int
*/
protected $_min = 0;
/**
* Maximum bound.
*
- * @var \Hoa\Compiler\Llk\Rule\Repetition int
+ * @var int
*/
protected $_max = 0;
@@ -68,7 +65,6 @@ class Repetition extends Rule {
/**
* Constructor.
*
- * @access public
* @param string $name Name.
* @param int $min Minimum bound.
* @param int $max Maximum bound.
@@ -76,8 +72,8 @@ class Repetition extends Rule {
* @param string $nodeId Node ID.
* @return void
*/
- public function __construct ( $name, $min, $max, $content, $nodeId ) {
-
+ public function __construct($name, $min, $max, $content, $nodeId)
+ {
parent::__construct($name, $content, $nodeId);
$this->_min = $min;
$this->_max = $max;
@@ -88,22 +84,20 @@ class Repetition extends Rule {
/**
* Get minimum bound.
*
- * @access public
* @return int
*/
- public function getMin ( ) {
-
+ public function getMin()
+ {
return $this->_min;
}
/**
* Get maximum bound.
*
- * @access public
* @return int
*/
- public function getMax ( ) {
-
+ public function getMax()
+ {
return $this->_max;
}
}
diff --git a/Llk/Rule/Rule.php b/Llk/Rule/Rule.php
index 0e765cb..91b4087 100644
--- a/Llk/Rule/Rule.php
+++ b/Llk/Rule/Rule.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,67 +43,64 @@ use Hoa\Core;
*
* Rule parent.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-abstract class Rule {
-
+abstract class Rule
+{
/**
* Rule name.
*
- * @var \Hoa\Compiler\Llk\Rule string
+ * @var string
*/
protected $_name = null;
/**
* Rule content.
*
- * @var \Hoa\Compiler\Llk\Rule mixed
+ * @var mixed
*/
protected $_content = null;
/**
* Node ID.
*
- * @var \Hoa\Compiler\Llk\Rule string
+ * @var string
*/
protected $_nodeId = null;
/**
* Node options.
*
- * @var \Hoa\Compiler\Llk\Rule array
+ * @var array
*/
protected $_nodeOptions = [];
/**
* Default ID.
*
- * @var \Hoa\Compiler\Llk\Rule string
+ * @var string
*/
protected $_defaultId = null;
/**
* Default options.
*
- * @var \Hoa\Compiler\Llk\Rule array
+ * @var array
*/
protected $_defaultOptions = [];
/**
* For non-transitional rule: PP representation.
*
- * @var \Hoa\Compiler\Llk\Rule string
+ * @var string
*/
protected $_pp = null;
/**
* Whether the rule is transitional or not (i.e. not declared in the grammar
* but created by the analyzer).
*
- * @var \Hoa\Compiler\Llk\Rule bool
+ * @var bool
*/
protected $_transitional = true;
@@ -112,14 +109,13 @@ abstract class Rule {
/**
* Constructor.
*
- * @access public
* @param string $name Name.
* @param mixed $content Content.
* @param string $nodeId Node ID.
* @return void
*/
- public function __construct ( $name, $content, $nodeId = null ) {
-
+ public function __construct($name, $content, $nodeId = null)
+ {
$this->setName($name);
$this->setContent($content);
$this->setNodeId($nodeId);
@@ -130,12 +126,11 @@ abstract class Rule {
/**
* Set rule name.
*
- * @access public
* @param string $name Rule name.
* @return string
*/
- public function setName ( $name ) {
-
+ public function setName($name)
+ {
$old = $this->_name;
$this->_name = $name;
@@ -145,22 +140,20 @@ abstract class Rule {
/**
* Get rule name.
*
- * @access public
* @return string
*/
- public function getName ( ) {
-
+ public function getName()
+ {
return $this->_name;
}
/**
* Set rule content.
*
- * @access public
* @return mixed
*/
- protected function setContent ( $content ) {
-
+ protected function setContent($content)
+ {
$old = $this->_content;
$this->_content = $content;
@@ -170,32 +163,27 @@ abstract class Rule {
/**
* Get rule content.
*
- * @access public
* @return mixed
*/
- public function getContent ( ) {
-
+ public function getContent()
+ {
return $this->_content;
}
/**
* Set node ID.
*
- * @access public
* @param string $nodeId Node ID.
* @return string
*/
- public function setNodeId ( $nodeId ) {
-
+ public function setNodeId($nodeId)
+ {
$old = $this->_nodeId;
- if(false !== $pos = strpos($nodeId, ':')) {
-
+ if (false !== $pos = strpos($nodeId, ':')) {
$this->_nodeId = substr($nodeId, 0, $pos);
$this->_nodeOptions = str_split(substr($nodeId, $pos + 1));
- }
- else {
-
+ } else {
$this->_nodeId = $nodeId;
$this->_nodeOptions = [];
}
@@ -206,43 +194,37 @@ abstract class Rule {
/**
* Get node ID.
*
- * @access public
* @return string
*/
- public function getNodeId ( ) {
-
+ public function getNodeId()
+ {
return $this->_nodeId;
}
/**
* Get node options.
*
- * @access public
* @retrun array
*/
- public function getNodeOptions ( ) {
-
+ public function getNodeOptions()
+ {
return $this->_nodeOptions;
}
/**
* Set default ID.
*
- * @access public
* @param string $defaultId Default ID.
* @return string
*/
- public function setDefaultId ( $defaultId ) {
-
+ public function setDefaultId($defaultId)
+ {
$old = $this->_defaultId;
- if(false !== $pos = strpos($defaultId, ':')) {
-
+ if (false !== $pos = strpos($defaultId, ':')) {
$this->_defaultId = substr($defaultId, 0, $pos);
$this->_defaultOptions = str_split(substr($defaultId, $pos + 1));
- }
- else {
-
+ } else {
$this->_defaultId = $defaultId;
$this->_defaultOptions = [];
}
@@ -253,34 +235,31 @@ abstract class Rule {
/**
* Get default ID.
*
- * @access public
* @return string
*/
- public function getDefaultId ( ) {
-
+ public function getDefaultId()
+ {
return $this->_defaultId;
}
/**
* Get default options.
*
- * @access public
* @return array
*/
- public function getDefaultOptions ( ) {
-
+ public function getDefaultOptions()
+ {
return $this->_defaultOptions;
}
/**
* Set PP representation of the rule.
*
- * @access public
* @param string $pp PP representation.
* @return string
*/
- public function setPPRepresentation ( $pp ) {
-
+ public function setPPRepresentation($pp)
+ {
$old = $this->_pp;
$this->_pp = $pp;
$this->_transitional = false;
@@ -291,22 +270,20 @@ abstract class Rule {
/**
* Get PP representation of the rule.
*
- * @access public
* @return string
*/
- public function getPPRepresentation ( ) {
-
+ public function getPPRepresentation()
+ {
return $this->_pp;
}
/**
* Check whether the rule is transitional or not.
*
- * @access public
* @return bool
*/
- public function isTransitional ( ) {
-
+ public function isTransitional()
+ {
return $this->_transitional;
}
}
diff --git a/Llk/Rule/Token.php b/Llk/Rule/Token.php
index b59e2fc..584e20f 100644
--- a/Llk/Rule/Token.php
+++ b/Llk/Rule/Token.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -44,67 +44,64 @@ use Hoa\File;
*
* The token rule.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Token extends Rule {
-
+class Token extends Rule
+{
/**
* LL(k) compiler of hoa://Library/Regex/Grammar.pp
*
- * @var \Hoa\Compiler\Llk object
+ * @var \Hoa\Compiler\Llk
*/
protected static $_regexCompiler = null;
/**
* Token name.
*
- * @var \Hoa\Compiler\Llk\Rule\Token string
+ * @var string
*/
protected $_tokenName = null;
/**
* Namespace.
*
- * @var \Hoa\Compiler\Llk\Rule\Token string
+ * @var string
*/
protected $_namespace = null;
/**
* Token representation.
*
- * @var \Hoa\Compiler\Llk\Rule\Token string
+ * @var string
*/
protected $_regex = null;
/**
* AST of the regex.
*
- * @var \Hoa\Compiler\Llk\TreeNode object
+ * @var \Hoa\Compiler\Llk\TreeNode
*/
protected $_ast = null;
/**
* Token value.
*
- * @var \Hoa\Compiler\Llk\Rule\Token string
+ * @var string
*/
protected $_value = null;
/**
* Whether the token is kept or not in the AST.
*
- * @var \Hoa\Compiler\Llk\Rule\Token bool
+ * @var bool
*/
protected $_kept = false;
/**
* Unification index.
*
- * @var \Hoa\Compiler\Llk\Rule\Token int
+ * @var int
*/
protected $_unification = -1;
@@ -113,15 +110,14 @@ class Token extends Rule {
/**
* Constructor.
*
- * @access public
* @param string $name Name.
* @param string $tokenName Token name.
* @param string $nodeId Node ID.
* @param int $unification Unification index.
* @return void
*/
- public function __construct ( $name, $tokenName, $nodeId, $unification ) {
-
+ public function __construct($name, $tokenName, $nodeId, $unification)
+ {
parent::__construct($name, null, $nodeId);
$this->_tokenName = $tokenName;
$this->_unification = $unification;
@@ -132,23 +128,21 @@ class Token extends Rule {
/**
* Get token name.
*
- * @access public
* @return string
*/
- public function getTokenName ( ) {
-
+ public function getTokenName()
+ {
return $this->_tokenName;
}
/**
* Set token namespace.
*
- * @access public
* @param string $namespace Namespace.
* @return string
*/
- public function setNamespace ( $namespace ) {
-
+ public function setNamespace($namespace)
+ {
$old = $this->_namespace;
$this->_namespace = $namespace;
@@ -158,23 +152,21 @@ class Token extends Rule {
/**
* Get token namespace.
*
- * @access public
* @return string
*/
- public function getNamespace ( ) {
-
+ public function getNamespace()
+ {
return $this->_namespace;
}
/**
* Set representation.
*
- * @access public
* @param string $regex Representation.
* @return string
*/
- public function setRepresentation ( $regex ) {
-
+ public function setRepresentation($regex)
+ {
$old = $this->_regex;
$this->_regex = $regex;
@@ -184,34 +176,32 @@ class Token extends Rule {
/**
* Get token representation.
*
- * @access public
* @return string
*/
- public function getRepresentation ( ) {
-
+ public function getRepresentation()
+ {
return $this->_regex;
}
/**
* Get AST of the token representation.
*
- * @access public
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function getAST ( ) {
-
- if(null === static::$_regexCompiler) {
-
+ public function getAST()
+ {
+ if (null === static::$_regexCompiler) {
$stream = new File\Read('hoa://Library/Regex/Grammar.pp');
$stream->rewind();
static::$_regexCompiler = Compiler\Llk::load($stream);
}
- if(null === $this->_ast)
+ if (null === $this->_ast) {
$this->_ast = static::$_regexCompiler->parse(
$this->getRepresentation()
);
+ }
return $this->_ast;
}
@@ -219,12 +209,11 @@ class Token extends Rule {
/**
* Set token value.
*
- * @access public
* @param string $value Value.
* @return string
*/
- public function setValue ( $value ) {
-
+ public function setValue($value)
+ {
$old = $this->_value;
$this->_value = $value;
@@ -234,23 +223,21 @@ class Token extends Rule {
/**
* Get token value.
*
- * @access public
* @return string
*/
- public function getValue ( ) {
-
+ public function getValue()
+ {
return $this->_value;
}
/**
* Set whether the token is kept or not in the AST.
*
- * @access public
* @param bool $kept Kept.
* @return bool
*/
- public function setKept ( $kept ) {
-
+ public function setKept($kept)
+ {
$old = $this->_kept;
$this->_kept = $kept;
@@ -260,22 +247,20 @@ class Token extends Rule {
/**
* Check whether the token is kept in the AST or not.
*
- * @access public
* @return bool
*/
- public function isKept ( ) {
-
+ public function isKept()
+ {
return $this->_kept;
}
/**
* Get unification index.
*
- * @access public
* @return int
*/
- public function getUnificationIndex ( ) {
-
+ public function getUnificationIndex()
+ {
return $this->_unification;
}
}
diff --git a/Llk/Sampler/BoundedExhaustive.php b/Llk/Sampler/BoundedExhaustive.php
index e20c405..09db057 100644
--- a/Llk/Sampler/BoundedExhaustive.php
+++ b/Llk/Sampler/BoundedExhaustive.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -48,47 +48,45 @@ use Hoa\Visitor;
* This algorithm is based on multiset (set with repetition).
* Repetition unfolding: upper bound of + and * is set to n.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
class BoundedExhaustive
extends Sampler
- implements Iterator {
-
+ implements Iterator
+{
/**
* Stack of rules to explore.
*
- * @var \Hoa\Compiler\Llk\Sampler\BoundedExhaustive array
+ * @var array
*/
protected $_todo = null;
/**
* Stack of rules that have already been covered.
*
- * @var \Hoa\Compiler\Llk\Sampler\BoundedExhaustive array
+ * @var array
*/
protected $_trace = null;
/**
* Current iterator key.
*
- * @var \Hoa\Compiler\Llk\Sampler\BoundedExhaustive int
+ * @var int
*/
protected $_key = -1;
/**
* Current iterator value.
*
- * @var \Hoa\Compiler\Llk\Sampler\BoundedExhaustive string
+ * @var string
*/
protected $_current = null;
/**
* Bound.
*
- * @var \Hoa\Compiler\Llk\Sampler\BoundedExhaustive int
+ * @var int
*/
protected $_length = 5;
@@ -97,15 +95,15 @@ class BoundedExhaustive
/**
* Construct a generator.
*
- * @access public
* @param \Hoa\Compiler\Llk\Parser $compiler Compiler/parser.
* @param \Hoa\Visitor\Visit $tokenSampler Token sampler.
* @return void
*/
- public function __construct ( Compiler\Llk\Parser $compiler,
- Visitor\Visit $tokenSampler,
- $length = 5 ) {
-
+ public function __construct(
+ Compiler\Llk\Parser $compiler,
+ Visitor\Visit $tokenSampler,
+ $length = 5
+ ) {
parent::__construct($compiler, $tokenSampler);
$this->setLength($length);
@@ -115,44 +113,40 @@ class BoundedExhaustive
/**
* Get the current iterator value.
*
- * @access public
* @return string
*/
- public function current ( ) {
-
+ public function current()
+ {
return $this->_current;
}
/**
* Get the current iterator key.
*
- * @access public
* @return int
*/
- public function key ( ) {
-
+ public function key()
+ {
return $this->_key;
}
/**
* Useless here.
*
- * @access public
* @return void
*/
- public function next ( ) {
-
+ public function next()
+ {
return;
}
/**
* Rewind the internal iterator pointer.
*
- * @access public
* @return void
*/
- public function rewind ( ) {
-
+ public function rewind()
+ {
$ruleName = $this->_rootRuleName;
$this->_current = null;
$this->_key = -1;
@@ -169,19 +163,21 @@ class BoundedExhaustive
/**
* Compute the current iterator value, i.e. generate a new solution.
*
- * @access public
* @return bool
*/
- public function valid ( ) {
-
- if(false === $this->unfold())
+ public function valid()
+ {
+ if (false === $this->unfold()) {
return false;
+ }
$handle = null;
- foreach($this->_trace as $trace)
- if($trace instanceof Compiler\Llk\Rule\Token)
+ foreach ($this->_trace as $trace) {
+ if ($trace instanceof Compiler\Llk\Rule\Token) {
$handle .= $this->generateToken($trace);
+ }
+ }
++$this->_key;
$this->_current = $handle;
@@ -192,26 +188,24 @@ class BoundedExhaustive
/**
* Unfold rules from the todo stack.
*
- * @access protected
* @return bool
*/
- protected function unfold ( ) {
-
- while(0 < count($this->_todo)) {
-
+ protected function unfold()
+ {
+ while (0 < count($this->_todo)) {
$pop = array_pop($this->_todo);
- if($pop instanceof Compiler\Llk\Rule\Ekzit)
+ if ($pop instanceof Compiler\Llk\Rule\Ekzit) {
$this->_trace[] = $pop;
- else {
-
+ } else {
$ruleName = $pop->getRule();
$next = $pop->getData();
$rule = $this->_rules[$ruleName];
$out = $this->boundedExhaustive($rule, $next);
- if(true !== $out && true !== $this->backtrack())
+ if (true !== $out && true !== $this->backtrack()) {
return false;
+ }
}
}
@@ -221,19 +215,16 @@ class BoundedExhaustive
/**
* The bounded-exhaustive algorithm.
*
- * @access protected
* @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, $next)
+ {
$content = $rule->getContent();
- if($rule instanceof Compiler\Llk\Rule\Repetition) {
-
- if(0 === $next) {
-
+ if ($rule instanceof Compiler\Llk\Rule\Repetition) {
+ if (0 === $next) {
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
$rule->getMin()
@@ -246,8 +237,7 @@ class BoundedExhaustive
$this->_todo
);
- for($i = 0, $min = $rule->getMin(); $i < $min; ++$i) {
-
+ for ($i = 0, $min = $rule->getMin(); $i < $min; ++$i) {
$this->_todo[] = new Compiler\Llk\Rule\Ekzit(
$content,
0
@@ -257,19 +247,20 @@ class BoundedExhaustive
0
);
}
- }
- else {
-
+ } else {
$nbToken = 0;
- foreach($this->_trace as $trace)
- if($trace instanceof Compiler\Llk\Rule\Token)
+ foreach ($this->_trace as $trace) {
+ if ($trace instanceof Compiler\Llk\Rule\Token) {
++$nbToken;
+ }
+ }
$max = $rule->getMax();
- if(-1 != $max && $next > $max)
+ if (-1 != $max && $next > $max) {
return false;
+ }
$this->_todo[] = new Compiler\Llk\Rule\Ekzit(
$rule->getName(),
@@ -287,11 +278,10 @@ class BoundedExhaustive
}
return true;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Choice) {
-
- if(count($content) <= $next)
+ } elseif ($rule instanceof Compiler\Llk\Rule\Choice) {
+ if (count($content) <= $next) {
return false;
+ }
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
@@ -309,16 +299,13 @@ class BoundedExhaustive
);
return true;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Concatenation) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
$next
);
- for($i = count($content) - 1; $i >= 0; --$i) {
-
+ for ($i = count($content) - 1; $i >= 0; --$i) {
$nextRule = $content[$i];
$this->_todo[] = new Compiler\Llk\Rule\Ekzit(
$nextRule,
@@ -331,17 +318,18 @@ class BoundedExhaustive
}
return true;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Token) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
$nbToken = 0;
- foreach($this->_trace as $trace)
- if($trace instanceof Compiler\Llk\Rule\Token)
+ foreach ($this->_trace as $trace) {
+ if ($trace instanceof Compiler\Llk\Rule\Token) {
++$nbToken;
+ }
+ }
- if($nbToken >= $this->getLength())
+ if ($nbToken >= $this->getLength()) {
return false;
+ }
$this->_trace[] = $rule;
array_pop($this->_todo);
@@ -355,32 +343,27 @@ class BoundedExhaustive
/**
* Backtrack to the previous choice-point.
*
- * @access protected
* @return bool
*/
- protected function backtrack ( ) {
-
+ protected function backtrack()
+ {
$found = false;
do {
-
$last = array_pop($this->_trace);
- if($last instanceof Compiler\Llk\Rule\Entry) {
-
+ if ($last instanceof Compiler\Llk\Rule\Entry) {
$rule = $this->_rules[$last->getRule()];
$found = $rule instanceof Compiler\Llk\Rule\Choice;
- }
- elseif($last instanceof Compiler\Llk\Rule\Ekzit) {
-
+ } elseif ($last instanceof Compiler\Llk\Rule\Ekzit) {
$rule = $this->_rules[$last->getRule()];
$found = $rule instanceof Compiler\Llk\Rule\Repetition;
}
+ } while (0 < count($this->_trace) && false === $found);
- } while(0 < count($this->_trace) && false === $found);
-
- if(false === $found)
+ if (false === $found) {
return false;
+ }
$rule = $last->getRule();
$next = $last->getData() + 1;
@@ -397,15 +380,19 @@ class BoundedExhaustive
/**
* Set upper-bound, the maximum data length.
*
- * @access public
* @param int $length Length.
* @return int
+ * @throws \Hoa\Compiler\Exception
*/
- public function setLength ( $length ) {
-
- if(0 >= $length)
+ public function setLength($length)
+ {
+ if (0 >= $length) {
throw new Exception(
- 'Length must be greater than 0, given %d.', 0, $length);
+ 'Length must be greater than 0, given %d.',
+ 0,
+ $length
+ );
+ }
$old = $this->_length;
$this->_length = $length;
@@ -416,11 +403,10 @@ class BoundedExhaustive
/**
* Get upper-bound.
*
- * @access public
* @return int
*/
- public function getLength ( ) {
-
+ public function getLength()
+ {
return $this->_length;
}
}
diff --git a/Llk/Sampler/Coverage.php b/Llk/Sampler/Coverage.php
index 158ca55..aebde16 100644
--- a/Llk/Sampler/Coverage.php
+++ b/Llk/Sampler/Coverage.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -56,33 +56,31 @@ use Hoa\Iterator;
* • + is unfolded 1 or 2 times;
* • {x,y} is unfolded x, x + 1, y - 1 and y times.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
class Coverage
extends Sampler
- implements Iterator {
-
+ implements Iterator
+{
/**
* Stack of rules to explore.
*
- * @var \Hoa\Compiler\Llk\Sampler\Coverage array
+ * @var array
*/
protected $_todo = null;
/**
* Stack of rules that have already been covered.
*
- * @var \Hoa\Compiler\Llk\Sampler\Coverage array
+ * @var array
*/
protected $_trace = null;
/**
* Produced test cases.
*
- * @var \Hoa\Compiler\Llk\Sampler\Coverage array
+ * @var array
*/
protected $_tests = null;
@@ -90,21 +88,21 @@ class Coverage
* Covered rules: ruleName to structure that contains the choice point and
* 0 for uncovered, 1 for covered, -1 for failed and .5 for in progress.
*
- * @var \Hoa\Compiler\Llk\Sampler\Coverage array
+ * @var array
*/
protected $_coveredRules = null;
/**
* Current iterator key.
*
- * @var \Hoa\Compiler\Llk\Sampler\Coverage int
+ * @var int
*/
protected $_key = -1;
/**
* Current iterator value.
*
- * @var \Hoa\Compiler\Llk\Sampler\Coverage string
+ * @var string
*/
protected $_current = null;
@@ -113,75 +111,69 @@ class Coverage
/**
* Get the current iterator value.
*
- * @access public
* @return string
*/
- public function current ( ) {
-
+ public function current()
+ {
return $this->_current;
}
/**
* Get the current iterator key.
*
- * @access public
* @return int
*/
- public function key ( ) {
-
+ public function key()
+ {
return $this->_key;
}
/**
* Useless here.
*
- * @access public
* @return void
*/
- public function next ( ) {
-
+ public function next()
+ {
return;
}
/**
* Rewind the internal iterator pointer.
*
- * @access public
* @return void
*/
- public function rewind ( ) {
-
+ public function rewind()
+ {
$this->_key = -1;
$this->_current = null;
$this->_tests = [];
$this->_coveredRules = [];
- foreach($this->_rules as $name => $rule) {
-
+ foreach ($this->_rules as $name => $rule) {
$this->_coveredRules[$name] = [];
- if($rule instanceof Compiler\Llk\Rule\Repetition) {
-
+ if ($rule instanceof Compiler\Llk\Rule\Repetition) {
$min = $rule->getMin();
$min1 = $min + 1;
$max = -1 == $rule->getMax() ? 2 : $rule->getMax();
$max1 = $max - 1;
- if($min == $max)
+ if ($min == $max) {
$this->_coveredRules[$name][$min] = 0;
- else {
-
+ } else {
$this->_coveredRules[$name][$min] = 0;
$this->_coveredRules[$name][$min1] = 0;
$this->_coveredRules[$name][$max1] = 0;
$this->_coveredRules[$name][$max] = 0;
}
- }
- elseif($rule instanceof Compiler\Llk\Rule\Choice)
- for($i = 0, $max = count($rule->getContent()); $i < $max; ++$i)
+ } elseif ($rule instanceof Compiler\Llk\Rule\Choice) {
+ for ($i = 0, $max = count($rule->getContent()); $i < $max; ++$i) {
$this->_coveredRules[$name][$i] = 0;
- else
+ }
+ } else {
$this->_coveredRules[$name][0] = 0;
+ }
}
return;
@@ -190,16 +182,16 @@ class Coverage
/**
* Compute the current iterator value, i.e. generate a new solution.
*
- * @access public
* @return bool
*/
- public function valid ( ) {
-
+ public function valid()
+ {
$ruleName = $this->_rootRuleName;
- if( true !== in_array(0, $this->_coveredRules[$ruleName])
- && true !== in_array(.5, $this->_coveredRules[$ruleName]))
+ if (true !== in_array(0, $this->_coveredRules[$ruleName]) &&
+ true !== in_array(.5, $this->_coveredRules[$ruleName])) {
return false;
+ }
$this->_trace = [];
$this->_todo = [new Compiler\Llk\Rule\Entry(
@@ -209,23 +201,29 @@ class Coverage
$result = $this->unfold();
- if(true !== $result)
+ if (true !== $result) {
return false;
+ }
$handle = null;
- foreach($this->_trace as $trace)
- if($trace instanceof Compiler\Llk\Rule\Token)
+ foreach ($this->_trace as $trace) {
+ if ($trace instanceof Compiler\Llk\Rule\Token) {
$handle .= $this->generateToken($trace);
+ }
+ }
++$this->_key;
$this->_current = $handle;
$this->_tests[] = $this->_trace;
- foreach($this->_coveredRules as $key => $value)
- foreach($value as $k => $v)
- if(-1 == $v)
+ foreach ($this->_coveredRules as $key => $value) {
+ foreach ($value as $k => $v) {
+ if (-1 == $v) {
$this->_coveredRules[$key][$k] = 0;
+ }
+ }
+ }
return true;
}
@@ -233,26 +231,22 @@ class Coverage
/**
* Unfold rules from the todo stack.
*
- * @access protected
* @return bool
*/
- protected function unfold ( ) {
-
- while(0 < count($this->_todo)) {
-
+ protected function unfold()
+ {
+ while (0 < count($this->_todo)) {
$pop = array_pop($this->_todo);
- if($pop instanceof Compiler\Llk\Rule\Ekzit) {
-
+ if ($pop instanceof Compiler\Llk\Rule\Ekzit) {
$this->_trace[] = $pop;
$this->updateCoverage($pop);
- }
- else {
-
+ } else {
$out = $this->coverage($this->_rules[$pop->getRule()]);
- if(true !== $out && true !== $this->backtrack())
+ if (true !== $out && true !== $this->backtrack()) {
return false;
+ }
}
}
@@ -262,40 +256,40 @@ class Coverage
/**
* The coverage algorithm.
*
- * @access protected
* @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)
+ {
$content = $rule->getContent();
- if($rule instanceof Compiler\Llk\Rule\Repetition) {
-
+ if ($rule instanceof Compiler\Llk\Rule\Repetition) {
$uncovered = [];
$inprogress = [];
$already = [];
- foreach($this->_coveredRules[$rule->getName()] as $child => $value)
- if(0 == $value || .5 == $value)
+ foreach ($this->_coveredRules[$rule->getName()] as $child => $value) {
+ if (0 == $value || .5 == $value) {
$uncovered[] = $child;
- elseif(-1 == $value)
+ } elseif (-1 == $value) {
$inprogress[] = $child;
- else
+ } else {
$already[] = $child;
+ }
+ }
- if(empty($uncovered)) {
-
- if(empty($already))
+ if (empty($uncovered)) {
+ if (empty($already)) {
$rand = $inprogress[rand(
0,
count($inprogress) - 1
)];
- else
+ } else {
$rand = $already[rand(
0,
count($already) - 1
)];
+ }
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
@@ -307,35 +301,35 @@ class Coverage
$rand
);
- if($this->_rules[$content] instanceof Compiler\Llk\Rule\Token)
- for($i = 0; $i < $rand; ++$i)
+ if ($this->_rules[$content] instanceof Compiler\Llk\Rule\Token) {
+ for ($i = 0; $i < $rand; ++$i) {
$this->_todo[] = new Compiler\Llk\Rule\Entry(
$content,
$this->_coveredRules,
$this->_todo
);
- else {
-
+ }
+ } else {
$sequence = $this->extract([$content]);
- if(null === $sequence)
+ if (null === $sequence) {
return null;
+ }
- for($i = 0; $i < $rand; ++$i)
- foreach($sequence as $seq) {
-
+ for ($i = 0; $i < $rand; ++$i) {
+ foreach ($sequence as $seq) {
$this->_trace[] = $seq;
- if($seq instanceof Compiler\Llk\Rule\Ekzit)
+ if ($seq instanceof Compiler\Llk\Rule\Ekzit) {
$this->updateCoverage($seq);
+ }
}
+ }
}
- }
- else {
-
- $rand = $uncovered[rand(0, count($uncovered) - 1)];
+ } else {
+ $rand = $uncovered[rand(0, count($uncovered) - 1)];
$this->_coveredRules[$rule->getName()][$rand] = -1;
- $this->_trace[] = new Compiler\Llk\Rule\Entry(
+ $this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
$this->_coveredRules,
$this->_todo
@@ -345,32 +339,32 @@ class Coverage
$rand
);
- for($i= 0 ; $i < $rand; ++$i)
+ for ($i= 0 ; $i < $rand; ++$i) {
$this->_todo[] = new Compiler\Llk\Rule\Entry(
$content,
$this->_coveredRules,
$this->_todo
);
+ }
}
return true;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Choice) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Choice) {
$uncovered = [];
$inprogress = [];
$already = [];
- foreach($this->_coveredRules[$rule->getName()] as $child => $value)
- if(0 == $value || .5 == $value)
+ foreach ($this->_coveredRules[$rule->getName()] as $child => $value) {
+ if (0 == $value || .5 == $value) {
$uncovered[] = $child;
- elseif(-1 == $value)
+ } elseif (-1 == $value) {
$inprogress[] = $child;
- else
+ } else {
$already[] = $child;
+ }
+ }
- if(empty($uncovered)) {
-
+ if (empty($uncovered)) {
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
$this->_coveredRules,
@@ -378,35 +372,35 @@ class Coverage
);
$sequence = $this->extract($content);
- if(null === $sequence)
+ if (null === $sequence) {
return null;
+ }
- foreach($sequence as $seq) {
-
+ foreach ($sequence as $seq) {
$this->_trace[] = $seq;
- if($seq instanceof Compiler\Llk\Rule\Ekzit)
+ if ($seq instanceof Compiler\Llk\Rule\Ekzit) {
$this->updateCoverage($seq);
+ }
}
- if(empty($already))
+ if (empty($already)) {
$rand = $inprogress[rand(
0,
count($inprogress) - 1
)];
- else
+ } else {
$rand = $already[rand(
0,
count($already) - 1
)];
+ }
$this->_todo[] = new Compiler\Llk\Rule\Ekzit(
$rule->getName(),
$rand
);
- }
- else {
-
+ } else {
$rand = $uncovered[rand(0, count($uncovered) - 1)];
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
@@ -414,7 +408,7 @@ class Coverage
$this->_todo
);
$this->_coveredRules[$rule->getName()][$rand] = -1;
- $this->_todo[] = new Compiler\Llk\Rule\Ekzit(
+ $this->_todo[] = new Compiler\Llk\Rule\Ekzit(
$rule->getName(),
$rand
);
@@ -426,11 +420,9 @@ class Coverage
}
return true;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Concatenation) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
$this->_coveredRules[$rule->getName()][0] = -1;
- $this->_trace[] = new Compiler\Llk\Rule\Entry(
+ $this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
false
);
@@ -439,17 +431,16 @@ class Coverage
false
);
- for($i = count($content) - 1; $i >= 0; --$i)
+ for ($i = count($content) - 1; $i >= 0; --$i) {
$this->_todo[] = new Compiler\Llk\Rule\Entry(
$content[$i],
false,
$this->_todo
);
+ }
return true;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Token) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
$this->_trace[] = new Compiler\Llk\Rule\Entry(
$rule->getName(),
false
@@ -469,63 +460,59 @@ class Coverage
/**
* Extract a given sequence from existing traces.
*
- * @access protected
* @param array $rules Rules to consider.
* @return array
*/
- protected function extract ( Array $rules ) {
-
+ protected function extract(Array $rules)
+ {
$out = [];
- foreach($rules as $rule)
- foreach($this->_tests as $test) {
-
+ foreach ($rules as $rule) {
+ foreach ($this->_tests as $test) {
$opened = 0;
- foreach($test as $t) {
-
- if( $t instanceof Compiler\Llk\Rule\Entry
- && $t->getRule() == $rule)
+ foreach ($test as $t) {
+ if ($t instanceof Compiler\Llk\Rule\Entry &&
+ $t->getRule() == $rule) {
++$opened;
+ }
- if(0 < $opened) {
-
+ if (0 < $opened) {
$out[] = $t;
- if( $t instanceof Compiler\Llk\Rule\Ekzit
- && $t->getRule() == $rule) {
-
+ if ($t instanceof Compiler\Llk\Rule\Ekzit &&
+ $t->getRule() == $rule) {
--$opened;
- if(0 === $opened)
+ if (0 === $opened) {
return $out;
+ }
}
}
}
}
+ }
- foreach($rules as $rule) {
-
+ foreach ($rules as $rule) {
$out = [];
$closed = 0;
- foreach($this->_trace as $t) {
-
- if( $t instanceof Compiler\Llk\Rule\Ekzit
- && $t->getRule() == $rule)
+ foreach ($this->_trace as $t) {
+ if ($t instanceof Compiler\Llk\Rule\Ekzit &&
+ $t->getRule() == $rule) {
++$closed;
+ }
- if(0 < $closed) {
-
+ if (0 < $closed) {
$out[] = $t;
- if( $t instanceof Compiler\Llk\Rule\Ekzit
- && $t->getRule() == $rule) {
-
+ if ($t instanceof Compiler\Llk\Rule\Ekzit &&
+ $t->getRule() == $rule) {
--$closed;
- if(0 === $closed)
+ if (0 === $closed) {
return array_reverse($out);
+ }
}
}
}
@@ -537,27 +524,25 @@ class Coverage
/**
* Backtrack to the previous choice-point.
*
- * @access protected
* @return bool
*/
- protected function backtrack ( ) {
-
+ protected function backtrack()
+ {
$found = false;
do {
-
$pop = array_pop($this->_trace);
- if($pop instanceof Compiler\Llk\Rule\Entry) {
-
+ if ($pop instanceof Compiler\Llk\Rule\Entry) {
$rule = $this->_rules[$pop->getRule()];
- $found = $rule instanceof Compiler\Llk\Rule\Choice
- || $rule instanceof Compiler\Llk\Rule\Repetition;
+ $found = $rule instanceof Compiler\Llk\Rule\Choice ||
+ $rule instanceof Compiler\Llk\Rule\Repetition;
}
- } while(0 < count($this->_trace) && false === $found);
+ } while (0 < count($this->_trace) && false === $found);
- if(false === $found)
+ if (false === $found) {
return false;
+ }
$ruleName = $pop->getRule();
$this->_covered = $pop->getData();
@@ -574,57 +559,54 @@ class Coverage
/**
* Update coverage of a rule.
*
- * @access protected
* @param \Hoa\Compiler\Llk\Rule\Ekzit $rule Rule to consider.
* @return void
*/
- protected function updateCoverage ( Compiler\Llk\Rule\Ekzit $Rule ) {
-
+ protected function updateCoverage(Compiler\Llk\Rule\Ekzit $Rule)
+ {
$ruleName = $Rule->getRule();
$child = $Rule->getData();
$rule = $this->_rules[$ruleName];
$content = $rule->getContent();
- if($rule instanceof Compiler\Llk\Rule\Repetition) {
-
- if(0 === $child)
+ if ($rule instanceof Compiler\Llk\Rule\Repetition) {
+ if (0 === $child) {
$this->_coveredRules[$ruleName][$child] = 1;
- else {
-
- if( true === $this->allCovered($content)
- || true === $this->checkRuleRoot($content)) {
-
+ } else {
+ if (true === $this->allCovered($content) ||
+ true === $this->checkRuleRoot($content)) {
$this->_coveredRules[$ruleName][$child] = 1;
- foreach($this->_coveredRules[$ruleName] as $child => $value)
- if(.5 == $value)
+ foreach ($this->_coveredRules[$ruleName] as $child => $value) {
+ if (.5 == $value) {
$this->_coveredRules[$ruleName][$child] = 1;
- }
- else
+ }
+ }
+ } else {
$this->_coveredRules[$ruleName][$child] = .5;
+ }
}
- }
- elseif($rule instanceof Compiler\Llk\Rule\Choice) {
-
- if( true === $this->allCovered($content[$child])
- || true === $this->checkRuleRoot($content[$child]))
+ } elseif ($rule instanceof Compiler\Llk\Rule\Choice) {
+ if (true === $this->allCovered($content[$child]) ||
+ true === $this->checkRuleRoot($content[$child])) {
$this->_coveredRules[$ruleName][$child] = 1;
- else
+ } else {
$this->_coveredRules[$ruleName][$child] = .5;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Concatenation) {
-
+ }
+ } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
$isCovered = true;
- for($i = count($content) - 1; $i >= 0 && true === $isCovered; --$i)
- if( false === $this->allCovered($content[$i])
- && false === $this->checkRuleRoot($content[$i]))
+ for ($i = count($content) - 1; $i >= 0 && true === $isCovered; --$i) {
+ if (false === $this->allCovered($content[$i]) &&
+ false === $this->checkRuleRoot($content[$i])) {
$isCovered = false;
+ }
+ }
$this->_coveredRules[$ruleName][0] = true === $isCovered ? 1 : .5;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Token)
+ } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
$this->_coveredRules[$ruleName][0] = 1;
+ }
return;
}
@@ -632,15 +614,16 @@ class Coverage
/**
* Check if all rules have been entirely covered.
*
- * @access protected
* @param string $ruleName Rule name.
* @return bool
*/
- protected function allCovered ( $ruleName ) {
-
- foreach($this->_coveredRules[$ruleName] as $value)
- if(1 !== $value)
+ protected function allCovered($ruleName)
+ {
+ foreach ($this->_coveredRules[$ruleName] as $value) {
+ if (1 !== $value) {
return false;
+ }
+ }
return true;
}
@@ -648,31 +631,29 @@ class Coverage
/**
* Check if a rule is a root rule that is currently being processed.
*
- * @access protected
* @param string $ruleName Rule name.
* @return bool
*/
- protected function checkRuleRoot ( $ruleName ) {
-
- if(true === $this->_rules[$ruleName]->isTransitional())
+ protected function checkRuleRoot($ruleName)
+ {
+ if (true === $this->_rules[$ruleName]->isTransitional()) {
return false;
+ }
$i = count($this->_trace) - 1;
$nb = 0;
- while($i >= 0) {
-
+ while ($i >= 0) {
$lastRule = $this->_trace[$i];
- if($lastRule instanceof Compiler\Llk\Rule\Entry) {
-
- if($lastRule->getRule() == $ruleName)
+ if ($lastRule instanceof Compiler\Llk\Rule\Entry) {
+ if ($lastRule->getRule() == $ruleName) {
++$nb;
- }
- elseif($lastRule instanceof Compiler\Llk\Rule\Ekzit) {
-
- if($lastRule->getRule() == $ruleName)
+ }
+ } elseif ($lastRule instanceof Compiler\Llk\Rule\Ekzit) {
+ if ($lastRule->getRule() == $ruleName) {
--$nb;
+ }
}
--$i;
diff --git a/Llk/Sampler/Exception.php b/Llk/Sampler/Exception.php
index 8081123..cb2b8ca 100644
--- a/Llk/Sampler/Exception.php
+++ b/Llk/Sampler/Exception.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,9 +43,9 @@ use Hoa\Compiler;
*
* Extending the \Hoa\Compiler\Exception class.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Exception extends Compiler\Exception { }
+class Exception extends Compiler\Exception
+{
+}
diff --git a/Llk/Sampler/Sampler.php b/Llk/Sampler/Sampler.php
index c899fd7..c5e5cf2 100644
--- a/Llk/Sampler/Sampler.php
+++ b/Llk/Sampler/Sampler.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -45,53 +45,50 @@ use Hoa\Visitor;
*
* Sampler parent.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-abstract class Sampler {
-
+abstract class Sampler
+{
/**
* Compiler.
*
- * @var \Hoa\Compiler\Llk\Parser object
+ * @var \Hoa\Compiler\Llk\Parser
*/
protected $_compiler = null;
/**
* Tokens.
*
- * @var \Hoa\Compiler\Llk\Sampler array
+ * @var array
*/
protected $_tokens = null;
/**
* All rules (from the compiler).
*
- * @var \Hoa\Compiler\Llk\Sampler array
+ * @var array
*/
protected $_rules = null;
/**
* Token sampler.
*
- * @var \Hoa\Visitor\Visit object
+ * @var \Hoa\Visitor\Visit
*/
protected $_tokenSampler = null;
/**
* Root rule name.
*
- * @var \Hoa\Compiler\Llk\Sampler string
+ * @var string
*/
protected $_rootRuleName = null;
/**
* Current token namespace.
*
- * @var \Hoa\Compiler\Llk\Sampler string
+ * @var string
*/
protected $_currentNamespace = 'default';
@@ -100,14 +97,14 @@ abstract class Sampler {
/**
* Construct a generator.
*
- * @access public
* @param \Hoa\Compiler\Llk\Parser $compiler Compiler/parser.
* @param \Hoa\Visitor\Visit $tokenSampler Token sampler.
* @return void
*/
- public function __construct ( Compiler\Llk\Parser $compiler,
- Visitor\Visit $tokenSampler ) {
-
+ public function __construct(
+ Compiler\Llk\Parser $compiler,
+ Visitor\Visit $tokenSampler
+ ) {
$this->_compiler = $compiler;
$this->_tokens = $compiler->getTokens();
$this->_rules = $compiler->getRules();
@@ -120,11 +117,10 @@ abstract class Sampler {
/**
* Get compiler.
*
- * @access public
* @return \Hoa\Compiler\Llk\Parser
*/
- public function getCompiler ( ) {
-
+ public function getCompiler()
+ {
return $this->_compiler;
}
@@ -132,36 +128,34 @@ abstract class Sampler {
* Complete a token (namespace and representation).
* It returns the next namespace.
*
- * @access public
* @param \Hoa\Compiler\Llk\Rule\Token $token Token.
* @return string
*/
- protected function completeToken ( Compiler\Llk\Rule\Token $token ) {
-
- if(null !== $token->getRepresentation())
+ protected function completeToken(Compiler\Llk\Rule\Token $token)
+ {
+ if (null !== $token->getRepresentation()) {
return $this->_currentNamespace;
+ }
$name = $token->getTokenName();
$token->setNamespace($this->_currentNamespace);
$toNamespace = $this->_currentNamespace;
- if(isset($this->_tokens[$this->_currentNamespace][$name])) {
-
+ if (isset($this->_tokens[$this->_currentNamespace][$name])) {
$token->setRepresentation(
$this->_tokens[$this->_currentNamespace][$name]
);
- }
- else {
-
- foreach($this->_tokens[$this->_currentNamespace] as $_name => $regex) {
-
- if(false === strpos($_name, ':'))
+ } else {
+ foreach ($this->_tokens[$this->_currentNamespace] as $_name => $regex) {
+ if (false === strpos($_name, ':')) {
continue;
+ }
list($_name, $toNamespace) = explode(':', $_name, 2);
- if($_name === $name)
+ if ($_name === $name) {
break;
+ }
}
$token->setRepresentation($regex);
@@ -173,12 +167,11 @@ abstract class Sampler {
/**
* Set current token namespace.
*
- * @access public
* @param string $namespace Token namespace.
* @return string
*/
- protected function setCurrentNamespace ( $namespace ) {
-
+ protected function setCurrentNamespace($namespace)
+ {
$old = $this->_currentNamespace;
$this->_currentNamespace = $namespace;
@@ -189,12 +182,11 @@ abstract class Sampler {
* Generate a token value.
* Complete and set next token namespace.
*
- * @access protected
* @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)
+ {
$toNamespace = $this->completeToken($token);
$this->setCurrentNamespace($toNamespace);
diff --git a/Llk/Sampler/Uniform.php b/Llk/Sampler/Uniform.php
index 2ae7582..08f3513 100644
--- a/Llk/Sampler/Uniform.php
+++ b/Llk/Sampler/Uniform.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -49,24 +49,22 @@ use Hoa\Visitor;
* which guide the exploration.
* Repetition unfolding: upper bound of + and * is set to n.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @copyright Copyright © 2007-2015 Ivan Enderlin, Frédéric Dadeau.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-class Uniform extends Sampler {
-
+class Uniform extends Sampler
+{
/**
* Data (pre-computing).
*
- * @var \Hoa\Compiler\Llk\Sampler\Uniform array
+ * @var array
*/
protected $_data = [];
/**
* Bound.
*
- * @var \Hoa\Compiler\Llk\Sampler\Uniform int
+ * @var int
*/
protected $_length = 5;
@@ -75,19 +73,20 @@ class Uniform extends Sampler {
/**
* Construct a generator.
*
- * @access public
* @param \Hoa\Compiler\Llk\Parser $compiler Compiler/parser.
* @param \Hoa\Visitor\Visit $tokenSampler Token sampler.
* @return void
*/
- public function __construct ( Compiler\Llk\Parser $compiler,
- Visitor\Visit $tokenSampler,
- $length = 5 ) {
-
+ public function __construct(
+ Compiler\Llk\Parser $compiler,
+ Visitor\Visit $tokenSampler,
+ $length = 5
+ ) {
parent::__construct($compiler, $tokenSampler);
- foreach($this->_rules as $name => $_)
+ foreach ($this->_rules as $name => $_) {
$this->_data[$name] = [];
+ }
$this->setLength($length);
$this->_sampler = new Math\Sampler\Random();
@@ -98,15 +97,13 @@ class Uniform extends Sampler {
/**
* The random and uniform algorithm.
*
- * @access public
* @param \Hoa\Compiler\Llk\Rule $rule Rule to start.
* @param int $n Size.
* @return string
*/
- public function uniform ( Compiler\Llk\Rule $rule = null, $n = -1 ) {
-
- if(null === $rule && -1 === $n) {
-
+ public function uniform(Compiler\Llk\Rule $rule = null, $n = -1)
+ {
+ if (null === $rule && -1 === $n) {
$rule = $this->_rules[$this->_rootRuleName];
$n = $this->getLength();
}
@@ -114,59 +111,58 @@ class Uniform extends Sampler {
$data = &$this->_data[$rule->getName()][$n];
$computed = $data['n'];
- if(0 === $n || 0 === $computed)
+ if (0 === $n || 0 === $computed) {
return null;
+ }
- if($rule instanceof Compiler\Llk\Rule\Choice) {
-
+ if ($rule instanceof Compiler\Llk\Rule\Choice) {
$children = $rule->getContent();
$stat = [];
- foreach($children as $c => $child)
+ foreach ($children as $c => $child) {
$stat[$c] = $this->_data[$child][$n]['n'];
+ }
$i = $this->_sampler->getInteger(1, $computed);
- for($e = 0, $b = $stat[$e], $max = count($stat) - 1;
+ for ($e = 0, $b = $stat[$e], $max = count($stat) - 1;
$e < $max && $i > $b;
$b += $stat[++$e]);
return $this->uniform($this->_rules[$children[$e]], $n);
- }
- elseif($rule instanceof Compiler\Llk\Rule\Concatenation) {
-
- $children = $rule->getContent();
- $out = null;
+ } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
+ $children = $rule->getContent();
+ $out = null;
$Γ = $data['Γ'];
$γ = $Γ[$this->_sampler->getInteger(0, count($Γ) - 1)];
- foreach($children as $i => $child)
+ foreach ($children as $i => $child) {
$out .= $this->uniform($this->_rules[$child], $γ[$i]);
+ }
return $out;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Repetition){
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Repetition) {
$out = null;
$stat = &$data['xy'];
$child = $this->_rules[$rule->getContent()];
$b = 0;
$i = $this->_sampler->getInteger(1, $computed);
- foreach($stat as $α => $st)
- if($i <= $b += $st['n'])
+ foreach ($stat as $α => $st) {
+ if ($i <= $b += $st['n']) {
break;
+ }
+ }
$Γ = &$st['Γ'];
$γ = &$Γ[$this->_sampler->getInteger(0, count($Γ) - 1)];
- for($j = 0; $j < $α; ++$j)
+ for ($j = 0; $j < $α; ++$j) {
$out .= $this->uniform($child, $γ[$j]);
+ }
return $out;
- }
- elseif($rule instanceof Compiler\Llk\Rule\Token) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
return $this->generateToken($rule);
}
@@ -176,87 +172,83 @@ class Uniform extends Sampler {
/**
* Recursive method applied to our problematic.
*
- * @access public
* @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 ) {
-
- if(null === $rule || -1 === $n)
+ public function count(Compiler\Llk\Rule $rule = null, $n = -1)
+ {
+ if (null === $rule || -1 === $n) {
return 0;
+ }
$ruleName = $rule->getName();
- if(isset($this->_data[$ruleName][$n]))
+ if (isset($this->_data[$ruleName][$n])) {
return $this->_data[$ruleName][$n]['n'];
+ }
$this->_data[$ruleName][$n] = ['n' => 0];
$out = &$this->_data[$ruleName][$n]['n'];
$rule = $this->_rules[$ruleName];
- if($rule instanceof Compiler\Llk\Rule\Choice) {
-
- foreach($rule->getContent() as $child)
+ if ($rule instanceof Compiler\Llk\Rule\Choice) {
+ foreach ($rule->getContent() as $child) {
$out += $this->count($this->_rules[$child], $n);
- }
- elseif($rule instanceof Compiler\Llk\Rule\Concatenation) {
-
+ }
+ } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
$children = $rule->getContent();
$Γ = new Math\Combinatorics\Combination\Gamma(
count($children),
$n
);
$this->_data[$ruleName][$n]['Γ'] = [];
- $handle = &$this->_data[$ruleName][$n]['Γ'];
-
- foreach($Γ as $γ) {
+ $handle = &$this->_data[$ruleName][$n]['Γ'];
+ foreach ($Γ as $γ) {
$oout = 1;
- foreach($γ as $α => $_γ)
+ foreach ($γ as $α => $_γ) {
$oout *= $this->count($this->_rules[$children[$α]], $_γ);
+ }
- if(0 !== $oout)
+ if (0 !== $oout) {
$handle[] = $γ;
+ }
$out += $oout;
}
- }
- elseif($rule instanceof Compiler\Llk\Rule\Repetition) {
-
+ } elseif ($rule instanceof Compiler\Llk\Rule\Repetition) {
$this->_data[$ruleName][$n]['xy'] = [];
- $handle = &$this->_data[$ruleName][$n]['xy'];
- $child = $this->_rules[$rule->getContent()];
- $x = $rule->getMin();
- $y = $rule->getMax();
+ $handle = &$this->_data[$ruleName][$n]['xy'];
+ $child = $this->_rules[$rule->getContent()];
+ $x = $rule->getMin();
+ $y = $rule->getMax();
- if(-1 === $y)
+ if (-1 === $y) {
$y = $n;
- else
+ } else {
$y = min($n, $y);
+ }
- if(0 === $x && $x === $y)
+ if (0 === $x && $x === $y) {
$out = 1;
- else
- for($α = $x; $α <= $y; ++$α) {
-
+ } else {
+ for ($α = $x; $α <= $y; ++$α) {
$ut = 0;
$handle[$α] = ['n' => 0, 'Γ' => []];
- $Γ = new Math\Combinatorics\Combination\Gamma(
- $α,
- $n
- );
-
- foreach($Γ as $γ) {
+ $Γ = new Math\Combinatorics\Combination\Gamma($α, $n);
+ foreach ($Γ as $γ) {
$oout = 1;
- foreach($γ as $β => $_γ)
+ foreach ($γ as $β => $_γ) {
$oout *= $this->count($child, $_γ);
+ }
- if(0 !== $oout)
+ if (0 !== $oout) {
$handle[$α]['Γ'][] = $γ;
+ }
$ut += $oout;
}
@@ -264,9 +256,10 @@ class Uniform extends Sampler {
$handle[$α]['n'] = $ut;
$out += $ut;
}
- }
- elseif($rule instanceof Compiler\Llk\Rule\Token)
+ }
+ } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
$out = Math\Util::δ($n, 1);
+ }
return $out;
}
@@ -274,15 +267,19 @@ class Uniform extends Sampler {
/**
* Set upper-bound, the maximum data length.
*
- * @access public
* @param int $length Length.
* @return int
+ * @throws \Hoa\Compiler\Exception
*/
- public function setLength ( $length ) {
-
- if(0 >= $length)
+ public function setLength($length)
+ {
+ if (0 >= $length) {
throw new Exception(
- 'Length must be greater than 0, given %d.', 0, $length);
+ 'Length must be greater than 0, given %d.',
+ 0,
+ $length
+ );
+ }
$old = $this->_length;
$this->_length = $length;
@@ -297,11 +294,10 @@ class Uniform extends Sampler {
/**
* Get upper-bound.
*
- * @access public
* @return int
*/
- public function getLength ( ) {
-
+ public function getLength()
+ {
return $this->_length;
}
}
diff --git a/Llk/TreeNode.php b/Llk/TreeNode.php
index d5cc1ff..e0e49fb 100644
--- a/Llk/TreeNode.php
+++ b/Llk/TreeNode.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,46 +43,43 @@ use Hoa\Visitor;
*
* Provide a generic node for the AST produced by LL(k) parser.
*
- * @author Frédéric Dadeau <frederic.dadeau@femto-st.fr>
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Frédéric Dadeau, Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class TreeNode implements Visitor\Element {
-
+class TreeNode implements Visitor\Element
+{
/**
* ID (should be something like #ruleName or token).
*
- * @var \Hoa\Compiler\Llk\TreeNode string
+ * @var string
*/
protected $_id = null;
/**
* Value of the node (non-null for token nodes).
*
- * @var \Hoa\Compiler\Llk\TreeNode string
+ * @var string
*/
protected $_value = null;
/**
* Children.
*
- * @var \Hoa\Compiler\Llk\TreeNode array
+ * @var array
*/
protected $_children = null;
/**
* Parent.
*
- * @var \Hoa\Compiler\Llk\TreeNode object
+ * @var \Hoa\Compiler\Llk\TreeNode
*/
protected $_parent = null;
/**
* Attached data.
*
- * @var \Hoa\Compiler\Llk\TreeNode array
+ * @var array
*/
protected $_data = [];
@@ -91,23 +88,25 @@ class TreeNode implements Visitor\Element {
/**
* Constructor.
*
- * @access public
* @param string $id ID.
* @param string $value Value.
* @param array $children Children.
* @param \Hoa\Compiler\Llk\TreeNode $parent Parent.
* @return void
*/
- public function __construct ( $id, $value = null,
- Array $children = [],
- TreeNode $parent = null ) {
-
+ public function __construct(
+ $id,
+ $value = null,
+ Array $children = [],
+ TreeNode $parent = null
+ ) {
$this->setId($id);
$this->setValue($value);
$this->setChildren($children);
- if(null !== $parent)
+ if (null !== $parent) {
$this->setParent($parent);
+ }
return;
}
@@ -115,12 +114,11 @@ class TreeNode implements Visitor\Element {
/**
* Set ID.
*
- * @access public
* @param string $id ID.
* @return string
*/
- public function setId ( $id ) {
-
+ public function setId($id)
+ {
$old = $this->_id;
$this->_id = $id;
@@ -130,23 +128,21 @@ class TreeNode implements Visitor\Element {
/**
* Get ID.
*
- * @access public
* @return string
*/
- public function getId ( ) {
-
+ public function getId()
+ {
return $this->_id;
}
/**
* Set value.
*
- * @access public
* @param array $value Value (token & value).
* @return array
*/
- public function setValue ( $value ) {
-
+ public function setValue($value)
+ {
$old = $this->_value;
$this->_value = $value;
@@ -156,56 +152,51 @@ class TreeNode implements Visitor\Element {
/**
* Get value.
*
- * @access public
* @return array
*/
- public function getValue ( ) {
-
+ public function getValue()
+ {
return $this->_value;
}
/**
* Get value token.
*
- * @access public
* @return string
*/
- public function getValueToken ( ) {
-
+ public function getValueToken()
+ {
return $this->_value['token'];
}
/**
* Get value value.
*
- * @access public
* @return string
*/
- public function getValueValue ( ) {
-
+ public function getValueValue()
+ {
return $this->_value['value'];
}
/**
* Check if the node represents a token or not.
*
- * @access public
* @return bool
*/
- public function isToken ( ) {
-
+ public function isToken()
+ {
return null !== $this->_value;
}
/**
* Prepend a child.
*
- * @access public
* @param \Hoa\Compiler\Llk\TreeNode $child Child.
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function prependChild ( TreeNode $child ) {
-
+ public function prependChild(TreeNode $child)
+ {
array_unshift($this->_children, $child);
return $this;
@@ -214,12 +205,11 @@ class TreeNode implements Visitor\Element {
/**
* Append a child.
*
- * @access public
* @param \Hoa\Compiler\Llk\TreeNode $child Child.
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function appendChild ( TreeNode $child ) {
-
+ public function appendChild(TreeNode $child)
+ {
$this->_children[] = $child;
return $this;
@@ -228,11 +218,10 @@ class TreeNode implements Visitor\Element {
/**
* Set children.
*
- * @access public
* @return array
*/
- public function setChildren ( Array $children ) {
-
+ public function setChildren(Array $children)
+ {
$old = $this->_children;
$this->_children = $children;
@@ -242,57 +231,52 @@ class TreeNode implements Visitor\Element {
/**
* Get child.
*
- * @access public
* @param int $i Index.
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function getChild ( $i ) {
-
+ public function getChild($i)
+ {
return $this->_children[$i];
}
/**
* Get children.
*
- * @access public
* @return array
*/
- public function getChildren ( ) {
-
+ public function getChildren()
+ {
return $this->_children;
}
/**
* Get number of children.
*
- * @access public
* @return int
*/
- public function getChildrenNumber ( ) {
-
+ public function getChildrenNumber()
+ {
return count($this->_children);
}
/**
* Check if a child exists.
*
- * @access public
* @return bool
*/
- public function childExists ( $i ) {
-
+ public function childExists($i)
+ {
return array_key_exists($i, $this->_children);
}
/**
* Set parent.
*
- * @access public
* @param \Hoa\Compiler\Llk\TreeNode $parent Parent.
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function setParent ( TreeNode $parent ) {
-
+ public function setParent(TreeNode $parent)
+ {
$old = $this->_parent;
$this->_parent = $parent;
@@ -302,48 +286,46 @@ class TreeNode implements Visitor\Element {
/**
* Get parent.
*
- * @access public
* @return \Hoa\Compiler\Llk\TreeNode
*/
- public function getParent ( ) {
-
+ public function getParent()
+ {
return $this->_parent;
}
/**
* Get data.
*
- * @access public
* @return array
*/
- public function &getData ( ) {
-
+ public function &getData()
+ {
return $this->_data;
}
/**
* Accept a visitor.
*
- * @access public
* @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,
- &$handle = null, $eldnah = null ) {
-
+ public function accept(
+ Visitor\Visit $visitor,
+ &$handle = null,
+ $eldnah = null
+ ) {
return $visitor->visit($this, $handle, $eldnah);
}
/**
* Remove circular reference to the parent (help the garbage collector).
*
- * @access public
* @return void
*/
- public function __destruct ( ) {
-
+ public function __destruct()
+ {
unset($this->_parent);
return;