Skip to content

Commit fb8c553

Browse files
author
Kirill Nesmeyanov
committed
Simplify exception messages: Do not print source content if it is identical to failed token content
1 parent 5afaba9 commit fb8c553

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Exception/ParseException.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ final public function __construct(string $message, int $code = 0, ?\Throwable $p
3232
* @param int<0, max> $offset
3333
* @throws SourceExceptionInterface
3434
*/
35-
public static function fromUnexpectedToken(string $char, string $statement, int $offset): static
35+
public static function fromUnexpectedToken(string $token, string $statement, int $offset): static
3636
{
37-
$message = \vsprintf('Syntax error, unexpected %s in %s %s', [
38-
Formatter::token($char),
39-
Formatter::source($statement),
37+
$message = \vsprintf('Syntax error, unexpected %s%s %s', [
38+
Formatter::token($token),
39+
$token === $statement ? '' : ' in ' . Formatter::source($statement),
4040
Formatter::suffix($statement, $offset),
4141
]);
4242

@@ -51,9 +51,9 @@ public static function fromUnexpectedToken(string $char, string $statement, int
5151
*/
5252
public static function fromUnrecognizedToken(string $token, string $statement, int $offset): static
5353
{
54-
$message = \vsprintf('Syntax error, unrecognized %s in %s %s', [
54+
$message = \vsprintf('Syntax error, unrecognized %s%s %s', [
5555
Formatter::token($token),
56-
Formatter::source($statement),
56+
$token === $statement ? '' : ' in ' . Formatter::source($statement),
5757
Formatter::suffix($statement, $offset),
5858
]);
5959

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private function unexpectedTokenError(UnexpectedTokenException $e, ReadableInter
200200
$token = $e->getToken();
201201

202202
return ParseException::fromUnexpectedToken(
203-
char: $token->getValue(),
203+
token: $token->getValue(),
204204
statement: $source->getContents(),
205205
offset: $token->getOffset(),
206206
);

0 commit comments

Comments
 (0)