Skip to content

Commit

Permalink
PHP: Numeral syntax improvements (#2701)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ayesh committed Jan 6, 2021
1 parent 0e61a7e commit 01af04e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/prism-php.js
Expand Up @@ -15,7 +15,7 @@
/\b[A-Z_][A-Z0-9_]*\b(?!\s*\()/,
/\b(?:null)\b/i,
];
var number = /\b0b[01]+\b|\b0x[\da-f]+\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+)(?:e[+-]?\d+)?/i;
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;
var operator = /<?=>|\?\?=?|\.{3}|\??->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||<<|>>|[?~]|[/^|%*&<>.+-]=?/;
var punctuation = /[{}\[\](),:;]/;

Expand Down
2 changes: 1 addition & 1 deletion components/prism-php.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions tests/languages/php/number_feature.test
Expand Up @@ -7,6 +7,13 @@
0x539
0x1A
0123
0o123
0O123
0b1111_0000_111
0o1111_0000_123
0O1111_0000_123
01111_0000_123
0xAAAA_FFF_0123

----------------------------------------------------

Expand All @@ -19,9 +26,16 @@
["number", "0b10100111001"],
["number", "0x539"],
["number", "0x1A"],
["number", "0123"]
["number", "0123"],
["number", "0o123"],
["number", "0O123"],
["number", "0b1111_0000_111"],
["number", "0o1111_0000_123"],
["number", "0O1111_0000_123"],
["number", "01111_0000_123"],
["number", "0xAAAA_FFF_0123"]
]

----------------------------------------------------

Checks for numbers.
Checks for numbers.

0 comments on commit 01af04e

Please sign in to comment.