Skip to content

[ExpressionLanguage] Add support for logical xor operator #58341

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
Oct 1, 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 @@ -8,6 +8,7 @@ CHANGELOG
* Add support for comments using `/*` & `*/`
* Allow passing any iterable as `$providers` list to `ExpressionLanguage` constructor
* Add support for `<<`, `>>`, and `~` bitwise operators
* Add support for logical `xor` operator

7.1
---
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function tokenize(string $expression): TokenStream
} 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)) {
} 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(])xor(?=[\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);
$cursor += \strlen($match[0]);
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function evaluate(array $functions, array $values): mixed
case 'or':
case '||':
return $left || $this->nodes['right']->evaluate($functions, $values);
case 'xor':
return $left xor $this->nodes['right']->evaluate($functions, $values);
case 'and':
case '&&':
return $left && $this->nodes['right']->evaluate($functions, $values);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(
$this->binaryOperators = [
'or' => ['precedence' => 10, 'associativity' => self::OPERATOR_LEFT],
'||' => ['precedence' => 10, 'associativity' => self::OPERATOR_LEFT],
'xor' => ['precedence' => 12, 'associativity' => self::OPERATOR_LEFT],
'and' => ['precedence' => 15, 'associativity' => self::OPERATOR_LEFT],
'&&' => ['precedence' => 15, 'associativity' => self::OPERATOR_LEFT],
'|' => ['precedence' => 16, 'associativity' => self::OPERATOR_LEFT],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
throw new Exception('This script must be run from the command line.');
}

$operators = ['not', '!', 'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'contains', 'starts with', 'ends with', 'matches', '**', '<<', '>>'];
$operators = ['not', '!', 'or', '||', 'xor', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'contains', 'starts with', 'ends with', 'matches', '**', '<<', '>>'];
$operators = array_combine($operators, array_map('strlen', $operators));
arsort($operators);

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ public static function getTokenizeData()
],
'"/* this is not a comment */"',
],
[
[
new Token('name', 'foo', 1),
new Token('operator', 'xor', 5),
new Token('name', 'bar', 9),
],
'foo xor bar',
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function getEvaluateData(): array
return [
[true, new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))],
[true, new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))],
[false, new BinaryNode('xor', new ConstantNode(true), new ConstantNode(true))],
[false, new BinaryNode('and', new ConstantNode(true), new ConstantNode(false))],
[false, new BinaryNode('&&', new ConstantNode(true), new ConstantNode(false))],

Expand Down Expand Up @@ -86,6 +87,7 @@ public static function getCompileData(): array
return [
['(true || false)', new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))],
['(true || false)', new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))],
['(true xor true)', new BinaryNode('xor', new ConstantNode(true), new ConstantNode(true))],
['(true && false)', new BinaryNode('and', new ConstantNode(true), new ConstantNode(false))],
['(true && false)', new BinaryNode('&&', new ConstantNode(true), new ConstantNode(false))],

Expand Down Expand Up @@ -140,6 +142,7 @@ public static function getDumpData(): array
return [
['(true or false)', new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))],
['(true || false)', new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))],
['(true xor true)', new BinaryNode('xor', new ConstantNode(true), new ConstantNode(true))],
['(true and false)', new BinaryNode('and', new ConstantNode(true), new ConstantNode(false))],
['(true && false)', new BinaryNode('&&', new ConstantNode(true), new ConstantNode(false))],

Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ public static function getParseData()
'not foo or foo.not',
['foo'],
],
[
new Node\BinaryNode(
'xor',
new Node\NameNode('foo'),
new Node\NameNode('bar'),
),
'foo xor bar',
['foo', 'bar'],
],
[
new Node\BinaryNode('..', new Node\ConstantNode(0), new Node\ConstantNode(3)),
'0..3',
Expand Down
Loading