parent
11a06ccced
commit
dc55db2a9d
@ -87,7 +87,8 @@ class Lexer
|
||||
$cursor += strlen($match[0]);
|
||||
} else {
|
||||
// unlexable
|
||||
throw new SyntaxError(sprintf('Unexpected character "%s"', $expression[$cursor]), $cursor);
|
||||
$message = sprintf('Unexpected character "%s"', $expression[$cursor]);
|
||||
throw new SyntaxError($message, $cursor, $expression);
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,7 +96,7 @@ class Lexer
|
||||
|
||||
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);
|
||||
|
@ -13,8 +13,14 @@ namespace Symfony\Component\ExpressionLanguage;
|
||||
|
||||
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);
|
||||
}
|
||||
$message .= '.';
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
|
@ -18,14 +18,43 @@ use Symfony\Component\ExpressionLanguage\TokenStream;
|
||||
|
||||
class LexerTest extends 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()
|
||||
|
Reference in New Issue
Block a user