Skip to content

Commit

Permalink
Decoder::error() improved "Unexpected end" message
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 18, 2021
1 parent fa283d8 commit 43ae661
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Neon/Decoder.php
Expand Up @@ -381,16 +381,16 @@ private function cbString(array $m): string
}


private function error(string $message = "Unexpected '%s'")
private function error(string $message = null)
{
$last = $this->tokens[$this->pos] ?? null;
$offset = $last ? $last[1] : strlen($this->input);
$text = substr($this->input, 0, $offset);
$line = substr_count($text, "\n");
$col = $offset - strrpos("\n" . $text, "\n") + 1;
$token = $last
? str_replace("\n", '<new line>', substr($last[0], 0, 40))
: 'end';
throw new Exception(str_replace('%s', $token, $message) . " on line $line, column $col.");
$message = $message ?? 'Unexpected ' . ($last
? "'" . str_replace("\n", '<new line>', substr($last[0], 0, 40)) . "'"
: 'end');
throw new Exception("$message on line $line, column $col.");
}
}

0 comments on commit 43ae661

Please sign in to comment.