Skip to content

Commit 01af04e

Browse files
authored
PHP: Numeral syntax improvements (#2701)
Improves numeral literal handling in the Prism PHP component in two ways: 1. Since PHP 7.4, it is possible to use [underscore numeric separator](https://php.watch/versions/7.4/underscore_numeric_separator). Prism correctly handles this for decimal numbers, but not for binary, octal, and hex numerals although PHP supports the underscore separator for binary, octal, and hex numeric literals as well. 2. In upcoming PHP 8.1, PHP supports an [explicit octal numeral prefix `0o`/`0O`](https://php.watch/versions/8.1/explicit-octal-notation). The existing `0` prefix is not removed. This commit updates the PHP number regex to accommodate both cases above, and expands the tests for new syntax.
1 parent 0e61a7e commit 01af04e

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

components/prism-php.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/\b[A-Z_][A-Z0-9_]*\b(?!\s*\()/,
1616
/\b(?:null)\b/i,
1717
];
18-
var number = /\b0b[01]+\b|\b0x[\da-f]+\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+)(?:e[+-]?\d+)?/i;
18+
var number = /\b0b[01]+(?:_[01]+)*\b|\b0o[0-7]+(?:_[0-7]+)*\b|\b0x[\da-f]+(?:_[\da-f]+)*\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)?|\B\.\d+)(?:e[+-]?\d+)?/i;
1919
var operator = /<?=>|\?\?=?|\.{3}|\??->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||<<|>>|[?~]|[/^|%*&<>.+-]=?/;
2020
var punctuation = /[{}\[\](),:;]/;
2121

components/prism-php.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/php/number_feature.test

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
0x539
88
0x1A
99
0123
10+
0o123
11+
0O123
12+
0b1111_0000_111
13+
0o1111_0000_123
14+
0O1111_0000_123
15+
01111_0000_123
16+
0xAAAA_FFF_0123
1017

1118
----------------------------------------------------
1219

@@ -19,9 +26,16 @@
1926
["number", "0b10100111001"],
2027
["number", "0x539"],
2128
["number", "0x1A"],
22-
["number", "0123"]
29+
["number", "0123"],
30+
["number", "0o123"],
31+
["number", "0O123"],
32+
["number", "0b1111_0000_111"],
33+
["number", "0o1111_0000_123"],
34+
["number", "0O1111_0000_123"],
35+
["number", "01111_0000_123"],
36+
["number", "0xAAAA_FFF_0123"]
2337
]
2438

2539
----------------------------------------------------
2640

27-
Checks for numbers.
41+
Checks for numbers.

0 commit comments

Comments
 (0)