Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 776642c

Browse files
committed
Complete the injection of the expression in all syntax errors
1 parent dc55db2 commit 776642c

File tree

6 files changed

+34
-24
lines changed

6 files changed

+34
-24
lines changed

src/Symfony/Component/ExpressionLanguage/Lexer.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public function tokenize($expression)
5959
} elseif (false !== strpos(')]}', $expression[$cursor])) {
6060
// closing bracket
6161
if (empty($brackets)) {
62-
throw new SyntaxError(sprintf('Unexpected "%s"', $expression[$cursor]), $cursor);
62+
throw new SyntaxError(sprintf('Unexpected "%s"', $expression[$cursor]), $cursor, $expression);
6363
}
6464

6565
list($expect, $cur) = array_pop($brackets);
6666
if ($expression[$cursor] != strtr($expect, '([{', ')]}')) {
67-
throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur);
67+
throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
6868
}
6969

7070
$tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
@@ -87,8 +87,7 @@ public function tokenize($expression)
8787
$cursor += strlen($match[0]);
8888
} else {
8989
// unlexable
90-
$message = sprintf('Unexpected character "%s"', $expression[$cursor]);
91-
throw new SyntaxError($message, $cursor, $expression);
90+
throw new SyntaxError(sprintf('Unexpected character "%s"', $expression[$cursor]), $cursor, $expression);
9291
}
9392
}
9493

@@ -99,6 +98,6 @@ public function tokenize($expression)
9998
throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
10099
}
101100

102-
return new TokenStream($tokens);
101+
return new TokenStream($tokens, $expression);
103102
}
104103
}

src/Symfony/Component/ExpressionLanguage/Parser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function parse(TokenStream $stream, $names = array())
9999

100100
$node = $this->parseExpression();
101101
if (!$stream->isEOF()) {
102-
throw new SyntaxError(sprintf('Unexpected token "%s" of value "%s"', $stream->current->type, $stream->current->value), $stream->current->cursor);
102+
throw new SyntaxError(sprintf('Unexpected token "%s" of value "%s"', $stream->current->type, $stream->current->value), $stream->current->cursor, $stream->getExpression());
103103
}
104104

105105
return $node;
@@ -195,13 +195,13 @@ public function parsePrimaryExpression()
195195
default:
196196
if ('(' === $this->stream->current->value) {
197197
if (false === isset($this->functions[$token->value])) {
198-
throw new SyntaxError(sprintf('The function "%s" does not exist', $token->value), $token->cursor);
198+
throw new SyntaxError(sprintf('The function "%s" does not exist', $token->value), $token->cursor, $this->stream->getExpression());
199199
}
200200

201201
$node = new Node\FunctionNode($token->value, $this->parseArguments());
202202
} else {
203203
if (!in_array($token->value, $this->names, true)) {
204-
throw new SyntaxError(sprintf('Variable "%s" is not valid', $token->value), $token->cursor);
204+
throw new SyntaxError(sprintf('Variable "%s" is not valid', $token->value), $token->cursor, $this->stream->getExpression());
205205
}
206206

207207
// is the name used in the compiled code different
@@ -227,7 +227,7 @@ public function parsePrimaryExpression()
227227
} elseif ($token->test(Token::PUNCTUATION_TYPE, '{')) {
228228
$node = $this->parseHashExpression();
229229
} else {
230-
throw new SyntaxError(sprintf('Unexpected token "%s" of value "%s"', $token->type, $token->value), $token->cursor);
230+
throw new SyntaxError(sprintf('Unexpected token "%s" of value "%s"', $token->type, $token->value), $token->cursor, $this->stream->getExpression());
231231
}
232232
}
233233

@@ -289,7 +289,7 @@ public function parseHashExpression()
289289
} else {
290290
$current = $this->stream->current;
291291

292-
throw new SyntaxError(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s"', $current->type, $current->value), $current->cursor);
292+
throw new SyntaxError(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s"', $current->type, $current->value), $current->cursor, $this->stream->getExpression());
293293
}
294294

295295
$this->stream->expect(Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
@@ -327,7 +327,7 @@ public function parsePostfixExpression($node)
327327
// As a result, if $token is NOT an operator OR $token->value is NOT a valid property or method name, an exception shall be thrown.
328328
($token->type !== Token::OPERATOR_TYPE || !preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A', $token->value))
329329
) {
330-
throw new SyntaxError('Expected name', $token->cursor);
330+
throw new SyntaxError('Expected name', $token->cursor, $this->stream->getExpression());
331331
}
332332

333333
$arg = new Node\ConstantNode($token->value);
@@ -345,7 +345,7 @@ public function parsePostfixExpression($node)
345345
$node = new Node\GetAttrNode($node, $arg, $arguments, $type);
346346
} elseif ('[' === $token->value) {
347347
if ($node instanceof Node\GetAttrNode && Node\GetAttrNode::METHOD_CALL === $node->attributes['type'] && PHP_VERSION_ID < 50400) {
348-
throw new SyntaxError('Array calls on a method call is only supported on PHP 5.4+', $token->cursor);
348+
throw new SyntaxError('Array calls on a method call is only supported on PHP 5.4+', $token->cursor, $this->stream->getExpression());
349349
}
350350

351351
$this->stream->next();

src/Symfony/Component/ExpressionLanguage/SyntaxError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct($message, $cursor = 0, $expression = '')
1717
{
1818
$message = sprintf('%s around position %d', $message, $cursor);
1919
if ($expression) {
20-
$message = sprintf('%s for expression "%s"', $message, $expression);
20+
$message = sprintf('%s for expression `%s`', $message, $expression);
2121
}
2222
$message .= '.';
2323

src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ protected function setUp()
3434
public function testTokenize($tokens, $expression)
3535
{
3636
$tokens[] = new Token('end of expression', null, strlen($expression) + 1);
37-
$this->assertEquals(new TokenStream($tokens), $this->lexer->tokenize($expression));
37+
$this->assertEquals(new TokenStream($tokens, $expression), $this->lexer->tokenize($expression));
3838
}
3939

4040
/**
41-
* @expectedException Symfony\Component\ExpressionLanguage\SyntaxError
42-
* @expectedExceptionMessage Unexpected character "'" around position 33 for expression "service(faulty.expression.example').dummyMethod()".
41+
* @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
42+
* @expectedExceptionMessage Unexpected character "'" around position 33 for expression `service(faulty.expression.example').dummyMethod()`.
4343
*/
4444
public function testTokenizeThrowsErrorWithMessage()
4545
{
@@ -48,8 +48,8 @@ public function testTokenizeThrowsErrorWithMessage()
4848
}
4949

5050
/**
51-
* @expectedException Symfony\Component\ExpressionLanguage\SyntaxError
52-
* @expectedExceptionMessage Unclosed "(" around position 7 for expression "service(unclosed.expression.dummyMethod()".
51+
* @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
52+
* @expectedExceptionMessage Unclosed "(" around position 7 for expression `service(unclosed.expression.dummyMethod()`.
5353
*/
5454
public function testTokenizeThrowsErrorOnUnclosedBrace()
5555
{

src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ParserTest extends TestCase
2020
{
2121
/**
2222
* @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
23-
* @expectedExceptionMessage Variable "foo" is not valid around position 1.
23+
* @expectedExceptionMessage Variable "foo" is not valid around position 1 for expression `foo`.
2424
*/
2525
public function testParseWithInvalidName()
2626
{
@@ -31,7 +31,7 @@ public function testParseWithInvalidName()
3131

3232
/**
3333
* @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
34-
* @expectedExceptionMessage Variable "foo" is not valid around position 1.
34+
* @expectedExceptionMessage Variable "foo" is not valid around position 1 for expression `foo`.
3535
*/
3636
public function testParseWithZeroInNames()
3737
{

src/Symfony/Component/ExpressionLanguage/TokenStream.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ class TokenStream
2222

2323
private $tokens;
2424
private $position = 0;
25+
private $expression;
2526

2627
/**
2728
* Constructor.
2829
*
29-
* @param array $tokens An array of tokens
30+
* @param array $tokens An array of tokens
31+
* @param string $expression
3032
*/
31-
public function __construct(array $tokens)
33+
public function __construct(array $tokens, $expression = '')
3234
{
3335
$this->tokens = $tokens;
3436
$this->current = $tokens[0];
37+
$this->expression = $expression;
3538
}
3639

3740
/**
@@ -50,7 +53,7 @@ public function __toString()
5053
public function next()
5154
{
5255
if (!isset($this->tokens[$this->position])) {
53-
throw new SyntaxError('Unexpected end of expression', $this->current->cursor);
56+
throw new SyntaxError('Unexpected end of expression', $this->current->cursor, $this->expression);
5457
}
5558

5659
++$this->position;
@@ -69,7 +72,7 @@ public function expect($type, $value = null, $message = null)
6972
{
7073
$token = $this->current;
7174
if (!$token->test($type, $value)) {
72-
throw new SyntaxError(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message.'. ' : '', $token->type, $token->value, $type, $value ? sprintf(' with value "%s"', $value) : ''), $token->cursor);
75+
throw new SyntaxError(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message.'. ' : '', $token->type, $token->value, $type, $value ? sprintf(' with value "%s"', $value) : ''), $token->cursor, $this->expression);
7376
}
7477
$this->next();
7578
}
@@ -83,4 +86,12 @@ public function isEOF()
8386
{
8487
return $this->current->type === Token::EOF_TYPE;
8588
}
89+
90+
/**
91+
* @return string
92+
*/
93+
public function getExpression()
94+
{
95+
return $this->expression;
96+
}
8697
}

0 commit comments

Comments
 (0)