Skip to content

Commit 06f7ec2

Browse files
committed
added Position to Exception
1 parent 39d477e commit 06f7ec2

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

src/Neon/Exception.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,13 @@
1515
*/
1616
class Exception extends \Exception
1717
{
18+
public ?Position /*readonly*/ $position = null;
19+
20+
21+
public function __construct(string $message, ?Position $position = null, ?\Throwable $previous = null)
22+
{
23+
$message .= $position ? ' ' . $position : '';
24+
$this->position = $position;
25+
parent::__construct($message, 0, $previous);
26+
}
1827
}

src/Neon/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function tokenize(string $input): TokenStream
7070
$stream = new TokenStream($tokens);
7171
if ($position->offset !== strlen($input)) {
7272
$s = str_replace("\n", '\n', substr($input, $position->offset, 40));
73-
$stream->error("Unexpected '$s'", count($tokens) - 1);
73+
throw new Exception("Unexpected '$s'", $position);
7474
}
7575

7676
return $stream;

src/Neon/Node/StringNode.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function toValue(): string
3333
}
3434

3535

36-
public static function parse(string $s): string
36+
public static function parse(string $s, Nette\Neon\Position $position): string
3737
{
3838
if (preg_match('#^...\n++([\t ]*+)#', $s, $m)) { // multiline
3939
$res = substr($s, 3, -3);
@@ -52,14 +52,14 @@ public static function parse(string $s): string
5252

5353
return preg_replace_callback(
5454
'#\\\(?:ud[89ab][0-9a-f]{2}\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|.)#i',
55-
function (array $m): string {
55+
function (array $m) use ($position): string {
5656
$sq = $m[0];
5757
if (isset(self::EscapeSequences[$sq[1]])) {
5858
return self::EscapeSequences[$sq[1]];
5959
} elseif ($sq[1] === 'u' && strlen($sq) >= 6) {
60-
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq");
60+
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq", $position);
6161
} else {
62-
throw new Nette\Neon\Exception("Invalid escaping sequence $sq");
62+
throw new Nette\Neon\Exception("Invalid escaping sequence $sq", $position);
6363
}
6464
},
6565
$res,

src/Neon/Parser.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,9 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
131131
private function parseValue(): Node
132132
{
133133
if ($token = $this->stream->tryConsume(Token::String)) {
134-
try {
135-
$node = new Node\StringNode(Node\StringNode::parse($token->text));
136-
$this->injectPos($node, $this->stream->getIndex() - 1);
137-
} catch (Exception $e) {
138-
$this->stream->error($e->getMessage(), $this->stream->getIndex() - 1);
139-
}
134+
$node = new Node\StringNode(Node\StringNode::parse($token->text, $token->position));
135+
$this->injectPos($node, $this->stream->getIndex() - 1);
136+
140137
} elseif ($token = $this->stream->tryConsume(Token::Literal)) {
141138
$pos = $this->stream->getIndex() - 1;
142139
$node = new Node\LiteralNode(Node\LiteralNode::parse($token->text, $this->stream->is(':', '=')));

src/Neon/TokenStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ public function error(?string $message = null, ?int $pos = null): void
7878
$message ??= 'Unexpected ' . ($token->type === Token::End
7979
? 'end'
8080
: "'" . str_replace("\n", '<new line>', substr($token->text, 0, 40)) . "'");
81-
throw new Exception("$message $token->position");
81+
throw new Exception($message, $token->position);
8282
}
8383
}

0 commit comments

Comments
 (0)