-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support underscores in number literals #4487
Conversation
LGTM on 3.x |
d8a7262
to
621a8cd
Compare
@fabpot updated to target 3.x, mention syntax in documentation & add a line in changelog |
src/Lexer.php
Outdated
@@ -44,7 +44,7 @@ class Lexer | |||
public const STATE_INTERPOLATION = 4; | |||
|
|||
public const REGEX_NAME = '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A'; | |||
public const REGEX_NUMBER = '/[0-9]+(?:\.[0-9]+)?([Ee][\+\-][0-9]+)?/A'; | |||
public const REGEX_NUMBER = '/[0-9]+(?:_[0-9]+)*(?:\.[0-9]+(?:_[0-9]+)*)?([Ee][\+\-][0-9]+)?/A'; |
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.
Maybe we can do the same as in https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/ExpressionLanguage/Lexer.php#L42-L43 where the exponent can also accept _
.
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.
What about doing the same changes as we’ve done on Symfony? Both the regex and the logic. |
Yep i have been interrupted earlier.. will finish later today! |
Update: i missread things.. i'm sorry. |
Update: i missread the scanner code.. 🤕 |
@fabpot it took time to hit me ... it should be as you expected now. Only thing missing is the "float without integer" syntax (ie What do you think we handle this in following PR ? So, i had to update a bit the symfony regep, to disallow the "decimal part without integer part". public const REGEX_NUMBER = '/
(?(DEFINE)(?P<LNUM>[0-9]+(_[0-9]+)*))
- (?:\.(?&LNUM)|(?&LNUM)(?:\.(?!\.)(?&LNUM)?)?)(?:[eE][+-]?(?&LNUM))?/Ax';
+ (?:(?&LNUM)(?:\.(?!\.)(?&LNUM)?)?)(?:[eE][+-]?(?&LNUM))?/Ax'; The following tests pass with the current state of this PR, but would fail if we kept the original Symfony regexp. ❌ nested.definedArray.0
Pretty much the same thing/cause {{ nested.definedArray.0 is defined ? 'ok' : 'ko' }} ❌
I guess we are in the same situation you explained me quite recently (with a block name "cb-1") Not sure how would fix this though 🤷♂️ -- |
6cf2ea7
to
da4d966
Compare
Thank you @smnandre. |
https://www.php.net/manual/en/language.types.integer.php
This PR replicates that behaviour, using the regexp to match the literals and then remove the "_".
I'm targeting Twig4 but maybe 3.x would be ok?
I cannot think of a real case that