Skip to content

Commit dc4fe08

Browse files
committed
added Position to Exception
1 parent f05f232 commit dc4fe08

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
@@ -73,7 +73,7 @@ public function tokenize(string $input): TokenStream
7373
$stream = new TokenStream($tokens);
7474
if ($position->offset !== strlen($input)) {
7575
$s = str_replace("\n", '\n', substr($input, $position->offset, 40));
76-
$stream->error("Unexpected '$s'", count($tokens) - 1);
76+
throw new Exception("Unexpected '$s'", $position);
7777
}
7878

7979
return $stream;

src/Neon/Node/StringNode.php

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

3737

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

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

src/Neon/Parser.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,9 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
133133
private function parseValue(): Node
134134
{
135135
if ($token = $this->stream->tryConsume(Token::String)) {
136-
try {
137-
$node = new Node\StringNode(Node\StringNode::parse($token->text));
138-
$this->injectPos($node, $this->stream->getIndex() - 1);
139-
} catch (Exception $e) {
140-
$this->stream->error($e->getMessage(), $this->stream->getIndex() - 1);
141-
}
136+
$node = new Node\StringNode(Node\StringNode::parse($token->text, $token->position));
137+
$this->injectPos($node, $this->stream->getIndex() - 1);
138+
142139
} elseif ($token = $this->stream->tryConsume(Token::Literal)) {
143140
$pos = $this->stream->getIndex() - 1;
144141
$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
@@ -80,6 +80,6 @@ public function error(?string $message = null, ?int $pos = null): void
8080
$message ??= 'Unexpected ' . ($token->type === Token::End
8181
? 'end'
8282
: "'" . str_replace("\n", '<new line>', substr($token->text, 0, 40)) . "'");
83-
throw new Exception("$message $token->position");
83+
throw new Exception($message, $token->position);
8484
}
8585
}

0 commit comments

Comments
 (0)