diff --git a/atoum/Unit/Llk/Llk.php b/atoum/Unit/Llk/Llk.php index c2a35b96..a2eff0fc 100644 --- a/atoum/Unit/Llk/Llk.php +++ b/atoum/Unit/Llk/Llk.php @@ -217,7 +217,8 @@ public function case_parse_tokens() '%token foobar1 bazqux1' . "\n" . '%token sourceNS1:foobar2 bazqux2' . "\n" . '%token sourceNS2:foobar3 bazqux3 -> destinationNS' . "\n" . - '%token foobar4 barqux4 -> destinationNS' + '%token foobar4 barqux4 -> destinationNS' . "\n" . + '%token foobar5 ns:bar -> destinationNS' ) ->when($result = SUT::parsePP($pp, $tokens, $rules, $pragmas, 'streamFoo')) ->then @@ -227,7 +228,8 @@ public function case_parse_tokens() ->isEqualTo([ 'default' => [ 'foobar1' => 'bazqux1', - 'foobar4:destinationNS' => 'barqux4' + 'foobar4:destinationNS' => 'barqux4', + 'foobar5:destinationNS' => 'ns:bar', ], 'sourceNS1' => [ 'foobar2' => 'bazqux2' diff --git a/src/Llk/Lexer.php b/src/Llk/Lexer.php index c0f6c0bd..a6d03c5e 100644 --- a/src/Llk/Lexer.php +++ b/src/Llk/Lexer.php @@ -155,14 +155,25 @@ public function lexMe($text, array $tokens): \Generator $nextToken = $this->nextToken($offset); if (null === $nextToken) { + $line = 1; + $column = $offset; + $offsetText = substr($text, 0, $offset); + $pointerSpacing = mb_strlen($offsetText); + $previousLineBreak = strrpos($offsetText, "\n"); + if ($previousLineBreak !== false) { + $line = substr_count($offsetText, "\n") + 1; + $column = mb_strlen($offsetText) - $previousLineBreak - 1; + $pointerSpacing = $column; + } throw new Compiler\Exception\UnrecognizedToken( - 'Unrecognized token "%s" at line 1 and column %d:' . + 'Unrecognized token "%s" at line %d and column %d:' . "\n" . '%s' . "\n" . - str_repeat(' ', mb_strlen(substr($text, 0, $offset))) . '↑', + str_repeat(' ', $pointerSpacing) . '↑', 0, [ mb_substr(substr($text, $offset), 0, 1), - $offset + 1, + $line, + $column + 1, $text ], 1, diff --git a/src/Llk/Parser.php b/src/Llk/Parser.php index c8d46282..4b8b273b 100644 --- a/src/Llk/Parser.php +++ b/src/Llk/Parser.php @@ -215,11 +215,12 @@ final public function parse(string $text, string $rule = null, bool $tree = true } throw new Compiler\Exception\UnexpectedToken( - 'Unexpected token "%s" (%s) at line %d and column %d:' . + 'Unexpected token "%s" (%s:%s) at line %d and column %d:' . "\n" . '%s' . "\n" . str_repeat(' ', $column - 1) . '↑', 0, [ $token['value'], + $token['namespace'], $token['token'], $line, $column,