Skip to content

[ExpressionLanguage] Add comment support to expression language #54978

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

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support for null-coalescing unknown variables
* Add support for comments using `/*` & `*/`

7.1
---
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function tokenize(string $expression): TokenStream
// strings
$tokens[] = new Token(Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)), $cursor + 1);
$cursor += \strlen($match[0]);
} elseif (preg_match('{/\*.*?\*/}A', $expression, $match, 0, $cursor)) {
// comments
$cursor += \strlen($match[0]);
} elseif (preg_match('/(?<=^|[\s(])starts with(?=[\s(])|(?<=^|[\s(])ends with(?=[\s(])|(?<=^|[\s(])contains(?=[\s(])|(?<=^|[\s(])matches(?=[\s(])|(?<=^|[\s(])not in(?=[\s(])|(?<=^|[\s(])not(?=[\s(])|(?<=^|[\s(])and(?=[\s(])|\=\=\=|\!\=\=|(?<=^|[\s(])or(?=[\s(])|\|\||&&|\=\=|\!\=|\>\=|\<\=|(?<=^|[\s(])in(?=[\s(])|\.\.|\*\*|\!|\||\^|&|\<|\>|\+|\-|~|\*|\/|%/A', $expression, $match, 0, $cursor)) {
// operators
$tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,43 @@ public function testRegisterAfterCompile($registerCallback)
$registerCallback($el);
}

public static function validCommentProvider()
{
yield ['1 /* comment */ + 1'];
yield ['1 /* /* comment with spaces */'];
yield ['1 /** extra stars **/ + 1'];
yield ["/* multi\nline */ 'foo'"];
}

/**
* @dataProvider validCommentProvider
*/
public function testLintAllowsComments($expression)
{
$el = new ExpressionLanguage();
$el->lint($expression, []);

$this->expectNotToPerformAssertions();
}

public static function invalidCommentProvider()
{
yield ['1 + no start */'];
yield ['1 /* no closing'];
yield ['1 /* double closing */ */'];
}

/**
* @dataProvider invalidCommentProvider
*/
public function testLintThrowsOnInvalidComments($expression)
{
$el = new ExpressionLanguage();

$this->expectException(SyntaxError::class);
$el->lint($expression, []);
}

public function testLintDoesntThrowOnValidExpression()
{
$el = new ExpressionLanguage();
Expand All @@ -477,6 +514,13 @@ public function testLintThrowsOnInvalidExpression()
$el->lint('node.', ['node']);
}

public function testCommentsIgnored()
{
$expressionLanguage = new ExpressionLanguage();
$this->assertSame(3, $expressionLanguage->evaluate('1 /* foo */ + 2'));
$this->assertSame('(1 + 2)', $expressionLanguage->compile('1 /* foo */ + 2'));
}

public static function getRegisterCallbacks()
{
return [
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,34 @@ public static function getTokenizeData()
],
'-.7_189e+10',
],
[
[
new Token('number', 65536, 1),
],
'65536 /* this is 2^16 */',
],
[
[
new Token('number', 2, 1),
new Token('operator', '*', 21),
new Token('number', 4, 23),
],
'2 /* /* comment1 */ * 4',
],
[
[
new Token('string', '/* this is', 1),
new Token('operator', '~', 14),
new Token('string', 'not a comment */', 16),
],
'"/* this is" ~ "not a comment */"',
],
[
[
new Token('string', '/* this is not a comment */', 1),
],
'"/* this is not a comment */"',
],
];
}
}
Loading