8000 [ExpressionLanguage] Support lexing numbers with underscores and deci… · symfony/symfony@aa35fc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa35fc6

Browse files
committed
[ExpressionLanguage] Support lexing numbers with underscores and decimals with no leading zero
1 parent dd8c1dd commit aa35fc6

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/Symfony/Component/ExpressionLanguage/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.1.0
5+
-----
6+
7+
* Support lexing numbers with the numeric literal separator `_`
8+
* Support lexing decimals with no leading zero
9+
410
5.1.0
511
-----
612

src/Symfony/Component/ExpressionLanguage/Lexer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function tokenize(string $expression)
4040
continue;
4141
}
4242

43-
if (preg_match('/[0-9]+(?:\.[0-9]+)?([Ee][\+\-][0-9]+)?/A', $expression, $match, 0, $cursor)) {
43+
if (preg_match('/((?:[0-9]+(?:_[0-9]+)*|(?:(?:[0-9]+(?:_[0-9]+)*)?\.[0-9]+(?:_[0-9]+)*|[0-9]+(?:_[0-9]+)*\.(?!\.)(?:[0-9]+(?:_[0-9]+)*)?))[eE][+-]?[0-9]+(?:_[0-9]+)*|(?:(?:[0-9]+(?:_[0-9]+)*)?\.[0-9]+(?:_[0-9]+)*|[0-9]+(?:_[0-9]+)*\.(?!\.)(?:[0-9]+(?:_[0-9]+)*)?)|[0-9]+(?:_[0-9]+)*)/A', $expression, $match, 0, $cursor)) {
4444
// numbers
45-
$number = (float) $match[0]; // floats
46-
if (preg_match('/^[0-9]+$/', $match[0]) && $number <= \PHP_INT_MAX) {
47-
$number = (int) $match[0]; // integers lower than the maximum
45+
$number = (float) str_replace('_', '', $match[0]); // floats
46+
if (preg_match('/^[0-9]+(_[0-9]+)*$/', $match[0]) && $number <= \PHP_INT_MAX) {
47+
$number = (int) str_replace('_', '', $match[0]); // integers lower than the maximum
4848
}
4949
$tokens[] = new Token(Token::NUMBER_TYPE, $number, $cursor + 1);
5050
$cursor += \strlen($match[0]);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ public function getTokenizeData()
127127
],
128128
'foo.not in [bar]',
129129
],
130+
[
131+
[new Token('number', 0.787, 1)],
132+
'0.787',
133+
],
134+
[
135+
[new Token('number', 0.1234, 1)],
136+
'.1234',
137+
],
138+
[
139+
[new Token('number', 188165.1178, 1)],
140+
'188_165.1_178',
141+
],
142+
[
143+
[
144+
new Token('operator', '-', 1),
145+
new Token('number', 7189000000.0, 2),
146+
],
147+
'-.7_189e+10',
148+
],
130149
];
131150
}
132151
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ public function getParseData()
185185
'not foo or foo.not',
186186
['foo'],
187187
],
188+
[
189+
new Node\BinaryNode('+', new Node\ConstantNode(0), new Node\ConstantNode(0.1)),
190+
'0+.1',
191+
],
188192
];
189193
}
190194

0 commit comments

Comments
 (0)
0