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

Commit

Permalink
Enhance exception context, fix PCRE issue #6
Browse files Browse the repository at this point in the history
[TASK] Add more context to exceptions

* enhances `UnrecognizedToken` exception, see hoaproject/Compiler#111
* enhances `UnexpectedToken` exception, see hoaproject/Compiler#112

[BUGFIX] Allow colons in PCRE part of default namespace

See: hoaproject/Compiler#113
  • Loading branch information
sanmai authored Oct 7, 2020
2 parents 55577fe + e93b372 commit 7ca878c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
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

0 comments on commit 7ca878c

Please sign in to comment.