-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support underscores in number literals
``` {{ 1000 == 1_000 ? 'yes' : 'no' }} # now: syntax error # this PR: "yes" ``` > As of PHP 7.4.0, integer literals may contain underscores (_) between digits, for better readability of literals. These underscores are removed by PHP's scanner. https://www.php.net/manual/en/language.types.integer.php This PR replicates this 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 - does not trigger an error currently - would work differently after this PR
- Loading branch information
Showing
3 changed files
with
32 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Twig compile numbers literals with underscores correctly | ||
--TEMPLATE-- | ||
{{ 0_0 is same as 0 ? 'ok' : 'ko' }} | ||
{{ 1_23 is same as 123 ? 'ok' : 'ko' }} | ||
{{ 12_3 is same as 123 ? 'ok' : 'ko' }} | ||
{{ 1_2_3 is same as 123 ? 'ok' : 'ko' }} | ||
{{ -1_2 is same as -12 ? 'ok' : 'ko' }} | ||
{{ 1_2.3_4 is same as 12.34 ? 'ok' : 'ko' }} | ||
{{ -1_2.3_4 is same as -12.34 ? 'ok' : 'ko' }} | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
ok | ||
ok | ||
ok | ||
ok | ||
ok | ||
ok | ||
ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
Twig does not allow to use 2 underscored between digits in numbers | ||
--TEMPLATE-- | ||
{{ 1__2 }} | ||
--DATA-- | ||
return [] | ||
--EXCEPTION-- | ||
Twig\Error\SyntaxError: Unexpected token "name" of value "__2" ("end of print statement" expected) in "index.twig" at line 2. |