aboutsummaryrefslogtreecommitdiffstats
path: root/Llk
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2016-01-22 08:43:39 +0100
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2016-01-22 08:43:39 +0100
commit47959bf411fa2ec32e4f6cb54a4cfc3a47135b7c (patch)
treebf38103aa96b72060f228c9da2f9dc3d71800491 /Llk
parent2e358fe956432a1cb5837604d2381cb631b5c0c4 (diff)
downloadCompiler-47959bf411fa2ec32e4f6cb54a4cfc3a47135b7c.zip
Compiler-47959bf411fa2ec32e4f6cb54a4cfc3a47135b7c.tar.gz
Compiler-47959bf411fa2ec32e4f6cb54a4cfc3a47135b7c.tar.bz2
Parser: Use the lexer as an iterator.
Diffstat (limited to 'Llk')
-rw-r--r--Llk/Parser.php55
1 files changed, 28 insertions, 27 deletions
diff --git a/Llk/Parser.php b/Llk/Parser.php
index 339f070..5e5a6ac 100644
--- a/Llk/Parser.php
+++ b/Llk/Parser.php
@@ -37,6 +37,7 @@
namespace Hoa\Compiler\Llk;
use Hoa\Compiler;
+use Hoa\Iterator;
/**
* Class \Hoa\Compiler\Llk\Parser.
@@ -71,25 +72,18 @@ class Parser
protected $_rules = null;
/**
- * Current state of the analyzer.
+ * Lexer iterator.
*
- * @var int
+ * @var \Hoa\Iterator\Lookahead
*/
- protected $_currentState = 0;
+ protected $_tokenSequence = null;
/**
- * Error state of the analyzer (when an error is encountered).
- *
- * @var int
- */
- protected $_errorState = 0;
-
- /**
- * Current token sequence being analyzed.
+ * Possible token causing an error.
*
* @var array
*/
- protected $_tokenSequence = [];
+ protected $_errorToken = null;
/**
* Trace of activated rules.
@@ -148,11 +142,15 @@ class Parser
public function parse($text, $rule = null, $tree = true)
{
$lexer = new Lexer();
- $this->_tokenSequence = $lexer->lexMe($text, $this->_tokens);
- $this->_currentState = 0;
- $this->_errorState = 0;
- $this->_trace = [];
- $this->_todo = [];
+ $this->_tokenSequence = new Iterator\Buffer(
+ $lexer->lexMe($text, $this->_tokens),
+ 1024
+ );
+ $this->_tokenSequence->rewind();
+
+ $this->_errorToken = null;
+ $this->_trace = [];
+ $this->_todo = [];
if (false === array_key_exists($rule, $this->_rules)) {
$rule = $this->getRootRule();
@@ -171,7 +169,7 @@ class Parser
}
if (false === $this->backtrack()) {
- $token = $this->_tokenSequence[$this->_errorState];
+ $token = $this->_errorToken;
$offset = $token['offset'];
$line = 1;
$column = 1;
@@ -330,8 +328,9 @@ class Parser
}
array_pop($this->_todo);
- $this->_trace[] = $zzeRule;
- $this->_errorState = ++$this->_currentState;
+ $this->_trace[] = $zzeRule;
+ $this->_tokenSequence->next();
+ $this->_errorToken = $this->_tokenSequence->current();
return true;
} elseif ($zeRule instanceof Rule\Concatenation) {
@@ -371,9 +370,9 @@ class Parser
$this->_todo,
$this->_depth
);
- $nextRule = $content[$next];
- $this->_todo[] = new Rule\Ekzit($nextRule, 0);
- $this->_todo[] = new Rule\Entry($nextRule, 0);
+ $nextRule = $content[$next];
+ $this->_todo[] = new Rule\Ekzit($nextRule, 0);
+ $this->_todo[] = new Rule\Entry($nextRule, 0);
return true;
} elseif ($zeRule instanceof Rule\Repetition) {
@@ -447,7 +446,7 @@ class Parser
$zeRule = $this->_rules[$last->getRule()];
$found = $zeRule instanceof Rule\Repetition;
} elseif ($last instanceof Rule\Token) {
- --$this->_currentState;
+ $this->_tokenSequence->previous();
}
} while (0 < count($this->_trace) && false === $found);
@@ -669,7 +668,9 @@ class Parser
*/
public function getCurrentToken($kind = 'token')
{
- return $this->_tokenSequence[$this->_currentState][$kind];
+ $token = $this->_tokenSequence->current();
+
+ return $token[$kind];
}
/**
@@ -703,9 +704,9 @@ class Parser
}
/**
- * Get token sequence.
+ * Get the lexer iterator.
*
- * @return array
+ * @return \Hoa\Iterator\Buffer
*/
public function getTokenSequence()
{