-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
Would love to have this in Twig as well. |
@@ -40,11 +40,11 @@ public function tokenize(string $expression) | |||
continue; | |||
} | |||
|
|||
if (preg_match('/[0-9]+(?:\.[0-9]+)?([Ee][\+\-][0-9]+)?/A', $expression, $match, 0, $cursor)) { | |||
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)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW this regex was computed from this comment in the PHP source code: https://github.com/php/php-src/blob/172d469b7bc351efbcdf54052be3de289b2fdd62/Zend/zend_language_scanner.l#L1359 which is also in the PHP doc.
this misses tests using the |
a7bf00d
to
aa35fc6
Compare
@@ -40,11 +40,11 @@ public function tokenize(string $expression) | |||
continue; | |||
} | |||
|
|||
if (preg_match('/[0-9]+(?:\.[0-9]+)?([Ee][\+\-][0-9]+)?/A', $expression, $match, 0, $cursor)) { | |||
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)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to add to a negative lookahead ((?!\.)
) because the base pattern from the PHP source code matches decimals finishing at the point (eg: 8.
) and it collides with the range operator (eg: 0..9
would be interpreted as number 0
+ number 0.9
instead of number 0
+ operator ..
+ number 9
)
This PR was merged into the 4.4 branch. Discussion ---------- [ExpressionLanguage] Fix LexerTest number types | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | #44073 (comment) | License | MIT | Doc PR | - Very minor but it's better like this. Commits ------- bdf9adc [ExpressionLanguage] Fix LexerTest number types
…mals with no leading zero
7ce3d61
to
bb56b2e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I updated the code a bit)
Thank you @fancyweb. |
I understand that the supported Expression Language syntax is, and can be different than the PHP syntax, however, like @epixian, I would have expected
.123
to work since it's a common notation.Supporting the numeric literal separator
_
looks good to me for the readability (the expression language is still code IMO). As most of us are PHP devs, we know this syntax.Supporting those new syntaxes doesn't really bring more complexity in the code and it allows us to realign with the PHP behavior (thus having a guide) which is not something mandatory but something a random user would expect I think, that's why it improves the DX IMHO.
We could make the change in Twig too for the same reasons.
We could also support the hexadecimal, binary and octal syntaxes but let's wait for someone that actually needs it 😃