Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Enhance exception context, fix PCRE issue #6

Merged
merged 8 commits into from
Oct 7, 2020
6 changes: 4 additions & 2 deletions atoum/Unit/Llk/Llk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
17 changes: 14 additions & 3 deletions src/Llk/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Llk/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down