Skip to content

Commit 9b87907

Browse files
Fix SyntaxErrorException throwing ValueError in PHP 8 (#72)
Co-authored-by: Graham Campbell <[email protected]>
1 parent 14fe6ef commit 9b87907

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/SyntaxErrorException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
$expression
1818
) {
1919
$message = "Syntax error at character {$token['pos']}\n"
20-
. $expression . "\n" . str_repeat(' ', $token['pos']) . "^\n";
20+
. $expression . "\n" . str_repeat(' ', max($token['pos'], 0)) . "^\n";
2121
$message .= !is_array($expectedTypesOrMessage)
2222
? $expectedTypesOrMessage
2323
: $this->createTokenMessage($token, $expectedTypesOrMessage);

tests/ParserTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,24 @@ public function testHandlesEmptyExpressions()
4848
{
4949
(new Parser(new Lexer()))->parse('');
5050
}
51+
52+
/**
53+
* @dataProvider invalidExpressionProvider
54+
* @expectedException \JmesPath\SyntaxErrorException
55+
* @expectedExceptionMessag Syntax error at character 0
56+
*/
57+
public function testHandlesInvalidExpressions($expr)
58+
{
59+
(new Parser(new Lexer()))->parse($expr);
60+
}
61+
62+
public function invalidExpressionProvider()
63+
{
64+
return [
65+
['='],
66+
['<'],
67+
['>'],
68+
['|']
69+
];
70+
}
5171
}

0 commit comments

Comments
 (0)