Skip to content

[ExpressionLanguage] Lexer should provide expression information #19449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ public function tokenize($expression)
$cursor += strlen($match[0]);
} else {
// unlexable
throw new SyntaxError(sprintf('Unexpected character "%s"', $expression[$cursor]), $cursor);
$message = sprintf('Unexpected character "%s"', $expression[$cursor]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the $message variable, as done before

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be fixed

throw new SyntaxError($message, $cursor, $expression);
}
}

$tokens[] = new Token(Token::EOF_TYPE, null, $cursor + 1);

if (!empty($brackets)) {
list($expect, $cur) = array_pop($brackets);
throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur);
throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
}

return new TokenStream($tokens);
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/SyntaxError.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

class SyntaxError extends \LogicException
{
public function __construct($message, $cursor = 0)
public function __construct($message, $cursor = 0, $expression = '')
{
parent::__construct(sprintf('%s around position %d.', $message, $cursor));
$message = sprintf('%s around position %d', $message, $cursor);
if ($expression) {
$message = sprintf('%s for expression "%s"', $message, $expression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using backticks rather than quotes to encore the expression, because quotes can appear inside the expression itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
$message .= '.';

parent::__construct($message);
}
}
33 changes: 31 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,43 @@

class LexerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Lexer
*/
private $lexer;

protected function setUp()
{
$this->lexer = new Lexer();
}

/**
* @dataProvider getTokenizeData
*/
public function testTokenize($tokens, $expression)
{
$tokens[] = new Token('end of expression', null, strlen($expression) + 1);
$lexer = new Lexer();
$this->assertEquals(new TokenStream($tokens), $lexer->tokenize($expression));
$this->assertEquals(new TokenStream($tokens), $this->lexer->tokenize($expression));
}

/**
* @expectedException Symfony\Component\ExpressionLanguage\SyntaxError
* @expectedExceptionMessage Unexpected character "'" around position 33 for expression "service(faulty.expression.example').dummyMethod()".
*/
public function testTokenizeThrowsErrorWithMessage()
{
$expression = "service(faulty.expression.example').dummyMethod()";
$this->lexer->tokenize($expression);
}

/**
* @expectedException Symfony\Component\ExpressionLanguage\SyntaxError
* @expectedExceptionMessage Unclosed "(" around position 7 for expression "service(unclosed.expression.dummyMethod()".
*/
public function testTokenizeThrowsErrorOnUnclosedBrace()
{
$expression = 'service(unclosed.expression.dummyMethod()';
$this->lexer->tokenize($expression);
}

public function getTokenizeData()
Expand Down