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

Conversation

fancyweb
Copy link
Contributor
Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets #44014
License MIT
Doc PR -

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 😃

@fabpot
Copy link
Member
fabpot commented Nov 16, 2021

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)) {
Copy link
Contributor Author

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.

@stof
Copy link
Member
stof commented Nov 16, 2021

this misses tests using the _ separator

@@ -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)) {
Copy link
Contributor Author

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)

fabpot added a commit that referenced this pull request Nov 17, 2021
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
Copy link
Member
@nicolas-grekas nicolas-grekas left a 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)

@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0