Skip to content

Commit 3fc2f3b

Browse files
committed
[ExpressionLanguage] Use script to generate regex
1 parent 8b7714b commit 3fc2f3b

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Symfony/Component/ExpressionLanguage/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function tokenize(string $expression): TokenStream
7272
} elseif (preg_match('{/\*.*?\*/}A', $expression, $match, 0, $cursor)) {
7373
// comments
7474
$cursor += \strlen($match[0]);
75-
} 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)) {
75+
} 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)) {
7676
// operators
7777
$tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
7878
$cursor += \strlen($match[0]);

src/Symfony/Component/ExpressionLanguage/Resources/bin/generate_operator_regex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
throw new Exception('This script must be run from the command line.');
1414
}
1515

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

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,20 @@ public static function getTokenizeData()
184184
],
185185
];
186186
}
187+
188+
public function testOperatorRegexWasGeneratedWithScript()
189+
{
190+
ob_start();
191+
try {
192+
require $script = \dirname(__DIR__).'/Resources/bin/generate_operator_regex.php';
193+
} finally {
194+
$output = ob_get_clean();
195+
}
196+
197+
self::assertStringContainsString(
198+
$output,
199+
file_get_contents((new \ReflectionClass(Lexer::class))->getFileName()),
200+
\sprintf('You need to run "%s" to generate the operator regex.', $script),
201+
);
202+
}
187203
}

0 commit comments

Comments
 (0)