Skip to content

Commit 3dd6336

Browse files
committed
added Position to Exception
1 parent 4535ee9 commit 3dd6336

5 files changed

Lines changed: 18 additions & 12 deletions

File tree

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 readonly ?Position $position;
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
@@ -36,7 +36,7 @@ public function toValue(): string
3636
}
3737

3838

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

5656
return preg_replace_callback(
5757
'#\\\(?:ud[89ab][0-9a-f]{2}\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|.)#i',
58-
function (array $m): string {
58+
function (array $m) use ($position): string {
5959
$sq = $m[0];
6060
if (isset(self::EscapeSequences[$sq[1]])) {
6161
return self::EscapeSequences[$sq[1]];
6262
} elseif ($sq[1] === 'u' && strlen($sq) >= 6) {
63-
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq");
63+
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq", $position);
6464
} else {
65-
throw new Nette\Neon\Exception("Invalid escaping sequence $sq");
65+
throw new Nette\Neon\Exception("Invalid escaping sequence $sq", $position);
6666
}
6767
},
6868
$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
@@ -79,6 +79,6 @@ public function error(?string $message = null, ?int $pos = null): never
7979
$message ??= 'Unexpected ' . ($token->type === Token::End
8080
? 'end'
8181
: "'" . str_replace("\n", '<new line>', substr($token->text, 0, 40)) . "'");
82-
throw new Exception("$message $token->position");
82+
throw new Exception($message, $token->position);
8383
}
8484
}

0 commit comments

Comments
 (0)