Skip to content

Commit 5a24cbf

Browse files
authoredOct 14, 2021
JS: Improved number pattern (#3149)
1 parent 3c61c8f commit 5a24cbf

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed
 

‎components/prism-javascript.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,34 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
1818
],
1919
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
2020
'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
21-
'number': /\b(?:(?:0[xX](?:[\dA-Fa-f](?:_[\dA-Fa-f])?)+|0[bB](?:[01](?:_[01])?)+|0[oO](?:[0-7](?:_[0-7])?)+)n?|(?:\d(?:_\d)?)+n|NaN|Infinity)\b|(?:\b(?:\d(?:_\d)?)+\.?(?:\d(?:_\d)?)*|\B\.(?:\d(?:_\d)?)+)(?:[Ee][+-]?(?:\d(?:_\d)?)+)?/,
21+
'number': {
22+
pattern: RegExp(
23+
/(^|[^\w$])/.source +
24+
'(?:' +
25+
(
26+
// constant
27+
/NaN|Infinity/.source +
28+
'|' +
29+
// binary integer
30+
/0[bB][01]+(?:_[01]+)*n?/.source +
31+
'|' +
32+
// octal integer
33+
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
34+
'|' +
35+
// hexadecimal integer
36+
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
37+
'|' +
38+
// decimal bigint
39+
/\d+(?:_\d+)*n/.source +
40+
'|' +
41+
// decimal number (integer or float) but no bigint
42+
/(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/.source
43+
) +
44+
')' +
45+
/(?![\w$])/.source
46+
),
47+
lookbehind: true
48+
},
2249
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
2350
});
2451

‎components/prism-javascript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎prism.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,34 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
15671567
],
15681568
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
15691569
'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
1570-
'number': /\b(?:(?:0[xX](?:[\dA-Fa-f](?:_[\dA-Fa-f])?)+|0[bB](?:[01](?:_[01])?)+|0[oO](?:[0-7](?:_[0-7])?)+)n?|(?:\d(?:_\d)?)+n|NaN|Infinity)\b|(?:\b(?:\d(?:_\d)?)+\.?(?:\d(?:_\d)?)*|\B\.(?:\d(?:_\d)?)+)(?:[Ee][+-]?(?:\d(?:_\d)?)+)?/,
1570+
'number': {
1571+
pattern: RegExp(
1572+
/(^|[^\w$])/.source +
1573+
'(?:' +
1574+
(
1575+
// constant
1576+
/NaN|Infinity/.source +
1577+
'|' +
1578+
// binary integer
1579+
/0[bB][01]+(?:_[01]+)*n?/.source +
1580+
'|' +
1581+
// octal integer
1582+
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
1583+
'|' +
1584+
// hexadecimal integer
1585+
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
1586+
'|' +
1587+
// decimal bigint
1588+
/\d+(?:_\d+)*n/.source +
1589+
'|' +
1590+
// decimal number (integer or float) but no bigint
1591+
/(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/.source
1592+
) +
1593+
')' +
1594+
/(?![\w$])/.source
1595+
),
1596+
lookbehind: true
1597+
},
15711598
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
15721599
});
15731600

‎tests/languages/javascript/number_feature.test

+70-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@ Infinity
2323
0.000_001
2424
1e10_000
2525

26+
0xf_f_f_f_ffn
27+
0xf_f_f_f_f
28+
0b1_1_1_1
29+
0b0_0_1_0_1_0_1n
30+
0o1_1_3_2
31+
0o1_1_3_2n
32+
2.2_4_54e64_33
33+
2.2_4_54e+64_33
34+
2.2_4_54e-64_33
35+
3_4_5
36+
3_4_5n
37+
3_4_5.5_5_6
38+
39+
// not numbers
40+
$1234;
41+
$0xFF;
42+
$0b0101;
43+
$1234n;
44+
$0xFFn;
45+
$0b0101n;
46+
_1234;
47+
_0xFF;
48+
_0b0101;
49+
_1234n;
50+
_0xFFn;
51+
_0b0101n;
52+
abc1234;
53+
abc0xFF;
54+
abc0b0101;
55+
abc1234n;
56+
abc0xFFn;
57+
abc0b0101n;
58+
2659
----------------------------------------------------
2760

2861
[
@@ -35,18 +68,54 @@ Infinity
3568
["number", "0o571"],
3669
["number", "0xbabe"],
3770
["number", "0xBABE"],
71+
3872
["number", "NaN"],
3973
["number", "Infinity"],
74+
4075
["number", "123n"],
4176
["number", "0x123n"],
77+
4278
["number", "1_000_000_000_000"],
4379
["number", "1_000_000.220_720"],
4480
["number", "0b0101_0110_0011_1000"],
4581
["number", "0o12_34_56"],
4682
["number", "0x40_76_38_6A_73"],
4783
["number", "4_642_473_943_484_686_707n"],
4884
["number", "0.000_001"],
49-
["number", "1e10_000"]
85+
["number", "1e10_000"],
86+
87+
["number", "0xf_f_f_f_ffn"],
88+
["number", "0xf_f_f_f_f"],
89+
["number", "0b1_1_1_1"],
90+
["number", "0b0_0_1_0_1_0_1n"],
91+
["number", "0o1_1_3_2"],
92+
["number", "0o1_1_3_2n"],
93+
["number", "2.2_4_54e64_33"],
94+
["number", "2.2_4_54e+64_33"],
95+
["number", "2.2_4_54e-64_33"],
96+
["number", "3_4_5"],
97+
["number", "3_4_5n"],
98+
["number", "3_4_5.5_5_6"],
99+
100+
["comment", "// not numbers"],
101+
"\r\n$1234", ["punctuation", ";"],
102+
"\r\n$0xFF", ["punctuation", ";"],
103+
"\r\n$0b0101", ["punctuation", ";"],
104+
"\r\n$1234n", ["punctuation", ";"],
105+
"\r\n$0xFFn", ["punctuation", ";"],
106+
"\r\n$0b0101n", ["punctuation", ";"],
107+
"\r\n_1234", ["punctuation", ";"],
108+
"\r\n_0xFF", ["punctuation", ";"],
109+
"\r\n_0b0101", ["punctuation", ";"],
110+
"\r\n_1234n", ["punctuation", ";"],
111+
"\r\n_0xFFn", ["punctuation", ";"],
112+
"\r\n_0b0101n", ["punctuation", ";"],
113+
"\r\nabc1234", ["punctuation", ";"],
114+
"\r\nabc0xFF", ["punctuation", ";"],
115+
"\r\nabc0b0101", ["punctuation", ";"],
116+
"\r\nabc1234n", ["punctuation", ";"],
117+
"\r\nabc0xFFn", ["punctuation", ";"],
118+
"\r\nabc0b0101n", ["punctuation", ";"]
50119
]
51120

52121
----------------------------------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.