8000 [ExpressionLanguage] Support lexing numbers with underscores and decimals with no leading zero by fancyweb · Pull Request #44073 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ExpressionLanguage] Support lexing numbers with underscores and decimals with no leading zero #44073

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
Feb 24, 2022
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
6 changes: 6 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.1
-----

* Support lexing numbers with the numeric literal separator `_`
* Support lexing decimals with no leading zero

5.1.0
-----

Expand Down
12 ch 8000 anges: 6 additions & 6 deletions src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function tokenize(string $expression): TokenStream
continue;
}

if (preg_match('/[0-9]+(?:\.[0-9]+)?([Ee][\+\-][0-9]+)?/A', $expression, $match, 0, $cursor)) {
if (preg_match('/
(?(DEFINE)(?P<LNUM>[0-9]+(_[0-9]+)*))
(?:\.(?&LNUM)|(?&LNUM)(?:\.(?!\.)(?&LNUM)?)?)(?:[eE][+-]?(?&LNUM))?/Ax',
$expression, $match, 0, $cursor)
) {
// numbers
$number = (float) $match[0]; // floats
if (preg_match('/^[0-9]+$/', $match[0]) && $number <= \PHP_INT_MAX) {
$number = (int) $match[0]; // integers lower than the maximum
}
$tokens[] = new Token(Token::NUMBER_TYPE, $number, $cursor + 1);
$tokens[] = new Token(Token::NUMBER_TYPE, 0 + str_replace('_', '', $match[0]), $cursor + 1);
$cursor += \strlen($match[0]);
} elseif (str_contains('([{', $expression[$cursor])) {
// opening bracket
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ public function getTokenizeData()
],
'foo.not in [bar]',
],
[
[new Token('number', 0.787, 1)],
'0.787',
],
[
[new Token('number', 0.1234, 1)],
'.1234',
],
[
[new Token('number', 188165.1178, 1)],
'188_165.1_178',
],
[
[
new Token('operator', '-', 1),
new Token('number', 7189000000.0, 2),
],
'-.7_189e+10',
],
];
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public function getParseData()
new Node\BinaryNode('..', new Node\ConstantNode(0), new Node\ConstantNode(3)),
'0..3',
],
[
new Node\BinaryNode('+', new Node\ConstantNode(0), new Node\ConstantNode(0.1)),
'0+.1',
],
];
}

Expand Down
0