Skip to content

Commit

Permalink
added Position to Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2024
1 parent 9fc4d2b commit 9ba3cef
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/Neon/Exception.php
Expand Up @@ -15,4 +15,13 @@
*/
class Exception extends \Exception
{
public ?Position /*readonly*/ $position = null;


public function __construct(string $message, ?Position $position = null, ?\Throwable $previous = null)
{
$message .= $position ? ' ' . $position : '';
$this->position = $position;
parent::__construct($message, 0, $previous);
}
}
2 changes: 1 addition & 1 deletion src/Neon/Lexer.php
Expand Up @@ -70,7 +70,7 @@ public function tokenize(string $input): TokenStream
$stream = new TokenStream($tokens);
if ($position->offset !== strlen($input)) {
$s = str_replace("\n", '\n', substr($input, $position->offset, 40));
$stream->error("Unexpected '$s'", count($tokens) - 1);
throw new Exception("Unexpected '$s'", $position);
}

return $stream;
Expand Down
8 changes: 4 additions & 4 deletions src/Neon/Node/StringNode.php
Expand Up @@ -33,7 +33,7 @@ public function toValue(): string
}


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

return preg_replace_callback(
'#\\\\(?:ud[89ab][0-9a-f]{2}\\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|.)#i',
function (array $m): string {
function (array $m) use ($position): string {
$sq = $m[0];
if (isset(self::EscapeSequences[$sq[1]])) {
return self::EscapeSequences[$sq[1]];
} elseif ($sq[1] === 'u' && strlen($sq) >= 6) {
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq");
return json_decode('"' . $sq . '"') ?? throw new Nette\Neon\Exception("Invalid UTF-8 sequence $sq", $position);
} else {
throw new Nette\Neon\Exception("Invalid escaping sequence $sq");
throw new Nette\Neon\Exception("Invalid escaping sequence $sq", $position);
}
},
$res,
Expand Down
9 changes: 3 additions & 6 deletions src/Neon/Parser.php
Expand Up @@ -131,12 +131,9 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
private function parseValue(): Node
{
if ($token = $this->stream->tryConsume(Token::String)) {
try {
$node = new Node\StringNode(Node\StringNode::parse($token->text));
$this->injectPos($node, $this->stream->getIndex() - 1);
} catch (Exception $e) {
$this->stream->error($e->getMessage(), $this->stream->getIndex() - 1);
}
$node = new Node\StringNode(Node\StringNode::parse($token->text, $token->position));
$this->injectPos($node, $this->stream->getIndex() - 1);

} elseif ($token = $this->stream->tryConsume(Token::Literal)) {
$pos = $this->stream->getIndex() - 1;
$node = new Node\LiteralNode(Node\LiteralNode::parse($token->text, $this->stream->is(':', '=')));
Expand Down
2 changes: 1 addition & 1 deletion src/Neon/TokenStream.php
Expand Up @@ -78,6 +78,6 @@ public function error(?string $message = null, ?int $pos = null): void
$message ??= 'Unexpected ' . ($token->type === Token::End
? 'end'
: "'" . str_replace("\n", '<new line>', substr($token->text, 0, 40)) . "'");
throw new Exception("$message $token->position");
throw new Exception($message, $token->position);
}
}

0 comments on commit 9ba3cef

Please sign in to comment.