aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2017-12-27 15:16:50 +0100
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2017-12-27 15:16:50 +0100
commit7a92c716902a48b5d380e89d7acb3a4a2ee9f395 (patch)
treeb8795bce994617c9358320133b94c54e753e46fd
parentb675e53267a3302ea4000c751a0172596f0e92d8 (diff)
parent6cbc1ea6081b5966183312117677882af5734c27 (diff)
downloadCompiler-7a92c716902a48b5d380e89d7acb3a4a2ee9f395.zip
Compiler-7a92c716902a48b5d380e89d7acb3a4a2ee9f395.tar.gz
Compiler-7a92c716902a48b5d380e89d7acb3a4a2ee9f395.tar.bz2
Merge branch 'master' into ci
-rw-r--r--CHANGELOG.md5
-rw-r--r--Llk/Parser.php14
-rw-r--r--Llk/Rule/Token.php31
-rw-r--r--Llk/TreeNode.php13
-rw-r--r--Test/Unit/Llk/Rule/Token.php47
-rw-r--r--Test/Unit/Llk/TreeNode.php20
-rw-r--r--composer.json1
7 files changed, 123 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c018394..a130f83 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+# 3.17.08.08
+
+## Fixes
+
+ * `llk/parser` Use current token if no error token. (Kirill Nesmeyanov, 2017-08-08T09:35:08+02:00)
# 3.17.01.10
* Quality: Fix CS. (Ivan Enderlin, 2017-01-10T10:25:16+01:00)
diff --git a/Llk/Parser.php b/Llk/Parser.php
index 7223067..ec35d1d 100644
--- a/Llk/Parser.php
+++ b/Llk/Parser.php
@@ -187,6 +187,11 @@ class Parser
if (false === $this->backtrack()) {
$token = $this->_errorToken;
+
+ if (null === $this->_errorToken) {
+ $token = $this->_tokenSequence->current();
+ }
+
$offset = $token['offset'];
$line = 1;
$column = 1;
@@ -319,9 +324,14 @@ class Parser
}
}
- $namespace = $this->_tokenSequence->current()['namespace'];
+ $current = $this->_tokenSequence->current();
+
+ $namespace = $current['namespace'];
+ $offset = $current['offset'];
+
$zzeRule = clone $zeRule;
$zzeRule->setValue($value);
+ $zzeRule->setOffset($offset);
$zzeRule->setNamespace($namespace);
if (isset($this->_tokens[$namespace][$name])) {
@@ -590,7 +600,9 @@ class Parser
'token' => $trace->getTokenName(),
'value' => $trace->getValue(),
'namespace' => $trace->getNamespace(),
+ 'offset' => $trace->getOffset()
]);
+
$children[] = $child;
++$i;
}
diff --git a/Llk/Rule/Token.php b/Llk/Rule/Token.php
index a0a23b4..bd49564 100644
--- a/Llk/Rule/Token.php
+++ b/Llk/Rule/Token.php
@@ -105,7 +105,12 @@ class Token extends Rule
*/
protected $_unification = -1;
-
+ /**
+ * Token offset.
+ *
+ * @var int
+ */
+ protected $_offset = 0;
/**
* Constructor.
@@ -238,6 +243,30 @@ class Token extends Rule
}
/**
+ * Set token offset.
+ *
+ * @param int $offset Offset.
+ * @return int
+ */
+ public function setOffset($offset)
+ {
+ $old = $this->_offset;
+ $this->_offset = $offset;
+
+ return $old;
+ }
+
+ /**
+ * Get token offset.
+ *
+ * @return int
+ */
+ public function getOffset()
+ {
+ return $this->_offset;
+ }
+
+ /**
* Set whether the token is kept or not in the AST.
*
* @param bool $kept Kept.
diff --git a/Llk/TreeNode.php b/Llk/TreeNode.php
index 1cfb7fa..1ea4e78 100644
--- a/Llk/TreeNode.php
+++ b/Llk/TreeNode.php
@@ -189,6 +189,19 @@ class TreeNode implements Visitor\Element
}
/**
+ * Get token offset.
+ *
+ * @return int
+ */
+ public function getOffset()
+ {
+ return
+ isset($this->_value['offset'])
+ ? $this->_value['offset']
+ : 0;
+ }
+
+ /**
* Check if the node represents a token or not.
*
* @return bool
diff --git a/Test/Unit/Llk/Rule/Token.php b/Test/Unit/Llk/Rule/Token.php
index 6ddb1f2..2a66fc1 100644
--- a/Test/Unit/Llk/Rule/Token.php
+++ b/Test/Unit/Llk/Rule/Token.php
@@ -240,12 +240,12 @@ class Token extends Test\Unit\Suite
{
$this
->given(
- $name = 'foo',
- $tokenName = 'bar',
- $nodeId = 'baz',
- $unification = 0,
- $value = 'qux',
- $token = new SUT($name, $tokenName, $nodeId, $unification),
+ $name = 'foo',
+ $tokenName = 'bar',
+ $nodeId = 'baz',
+ $unification = 0,
+ $value = 'qux',
+ $token = new SUT($name, $tokenName, $nodeId, $unification),
$token->setValue($value)
)
->when($result = $token->getValue())
@@ -254,6 +254,41 @@ class Token extends Test\Unit\Suite
->isEqualTo($value);
}
+ public function case_set_offset()
+ {
+ $this
+ ->given(
+ $name = 'foo',
+ $tokenName = 'bar',
+ $nodeId = 'baz',
+ $unification = 0,
+ $offset = 42,
+ $token = new SUT($name, $tokenName, $nodeId, $unification)
+ )
+ ->when($result = $token->setOffset($offset))
+ ->then
+ ->integer($result)
+ ->isZero();
+ }
+
+ public function case_get_offset()
+ {
+ $this
+ ->given(
+ $name = 'foo',
+ $tokenName = 'bar',
+ $nodeId = 'baz',
+ $unification = 0,
+ $offset = 42,
+ $token = new SUT($name, $tokenName, $nodeId, $unification),
+ $token->setOffset($offset)
+ )
+ ->when($result = $token->getOffset())
+ ->then
+ ->integer($result)
+ ->isEqualTo($offset);
+ }
+
public function case_set_kept()
{
$this
diff --git a/Test/Unit/Llk/TreeNode.php b/Test/Unit/Llk/TreeNode.php
index 0233e28..2c911cc 100644
--- a/Test/Unit/Llk/TreeNode.php
+++ b/Test/Unit/Llk/TreeNode.php
@@ -229,6 +229,26 @@ class TreeNode extends Test\Unit\Suite
->isNull();
}
+ public function case_get_offset()
+ {
+ $this
+ ->given($node = new SUT('foo', ['offset' => 42]))
+ ->when($result = $node->getOffset())
+ ->then
+ ->integer($result)
+ ->isEqualTo(42);
+ }
+
+ public function case_get_offset_undefined()
+ {
+ $this
+ ->given($node = new SUT('foo', ['bar']))
+ ->when($result = $node->getOffset())
+ ->then
+ ->integer($result)
+ ->isZero();
+ }
+
public function case_is_token()
{
$this
diff --git a/composer.json b/composer.json
index a6518f2..b202433 100644
--- a/composer.json
+++ b/composer.json
@@ -27,6 +27,7 @@
"source": "https://central.hoa-project.net/Resource/Library/Compiler"
},
"require": {
+ "php" : ">=5.5.0",
"hoa/consistency": "~1.0",
"hoa/exception" : "~1.0",
"hoa/file" : "~1.0",