Skip to content
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

JS: Improved number pattern #3149

Merged
merged 1 commit into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion components/prism-javascript.js
Expand Up @@ -18,7 +18,34 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
'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)?)+)?/,
'number': {
pattern: RegExp(
/(^|[^\w$])/.source +
'(?:' +
(
// constant
/NaN|Infinity/.source +
'|' +
// binary integer
/0[bB][01]+(?:_[01]+)*n?/.source +
'|' +
// octal integer
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
'|' +
// hexadecimal integer
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
'|' +
// decimal bigint
/\d+(?:_\d+)*n/.source +
'|' +
// decimal number (integer or float) but no bigint
/(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/.source
) +
')' +
/(?![\w$])/.source
),
lookbehind: true
},
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
});

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

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

29 changes: 28 additions & 1 deletion prism.js
Expand Up @@ -1567,7 +1567,34 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
'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)?)+)?/,
'number': {
pattern: RegExp(
/(^|[^\w$])/.source +
'(?:' +
(
// constant
/NaN|Infinity/.source +
'|' +
// binary integer
/0[bB][01]+(?:_[01]+)*n?/.source +
'|' +
// octal integer
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
'|' +
// hexadecimal integer
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
'|' +
// decimal bigint
/\d+(?:_\d+)*n/.source +
'|' +
// decimal number (integer or float) but no bigint
/(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/.source
) +
')' +
/(?![\w$])/.source
),
lookbehind: true
},
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
});

Expand Down
71 changes: 70 additions & 1 deletion tests/languages/javascript/number_feature.test
Expand Up @@ -23,6 +23,39 @@ Infinity
0.000_001
1e10_000

0xf_f_f_f_ffn
0xf_f_f_f_f
0b1_1_1_1
0b0_0_1_0_1_0_1n
0o1_1_3_2
0o1_1_3_2n
2.2_4_54e64_33
2.2_4_54e+64_33
2.2_4_54e-64_33
3_4_5
3_4_5n
3_4_5.5_5_6

// not numbers
$1234;
$0xFF;
$0b0101;
$1234n;
$0xFFn;
$0b0101n;
_1234;
_0xFF;
_0b0101;
_1234n;
_0xFFn;
_0b0101n;
abc1234;
abc0xFF;
abc0b0101;
abc1234n;
abc0xFFn;
abc0b0101n;

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

[
Expand All @@ -35,18 +68,54 @@ Infinity
["number", "0o571"],
["number", "0xbabe"],
["number", "0xBABE"],

["number", "NaN"],
["number", "Infinity"],

["number", "123n"],
["number", "0x123n"],

["number", "1_000_000_000_000"],
["number", "1_000_000.220_720"],
["number", "0b0101_0110_0011_1000"],
["number", "0o12_34_56"],
["number", "0x40_76_38_6A_73"],
["number", "4_642_473_943_484_686_707n"],
["number", "0.000_001"],
["number", "1e10_000"]
["number", "1e10_000"],

["number", "0xf_f_f_f_ffn"],
["number", "0xf_f_f_f_f"],
["number", "0b1_1_1_1"],
["number", "0b0_0_1_0_1_0_1n"],
["number", "0o1_1_3_2"],
["number", "0o1_1_3_2n"],
["number", "2.2_4_54e64_33"],
["number", "2.2_4_54e+64_33"],
["number", "2.2_4_54e-64_33"],
["number", "3_4_5"],
["number", "3_4_5n"],
["number", "3_4_5.5_5_6"],

["comment", "// not numbers"],
"\r\n$1234", ["punctuation", ";"],
"\r\n$0xFF", ["punctuation", ";"],
"\r\n$0b0101", ["punctuation", ";"],
"\r\n$1234n", ["punctuation", ";"],
"\r\n$0xFFn", ["punctuation", ";"],
"\r\n$0b0101n", ["punctuation", ";"],
"\r\n_1234", ["punctuation", ";"],
"\r\n_0xFF", ["punctuation", ";"],
"\r\n_0b0101", ["punctuation", ";"],
"\r\n_1234n", ["punctuation", ";"],
"\r\n_0xFFn", ["punctuation", ";"],
"\r\n_0b0101n", ["punctuation", ";"],
"\r\nabc1234", ["punctuation", ";"],
"\r\nabc0xFF", ["punctuation", ";"],
"\r\nabc0b0101", ["punctuation", ";"],
"\r\nabc1234n", ["punctuation", ";"],
"\r\nabc0xFFn", ["punctuation", ";"],
"\r\nabc0b0101n", ["punctuation", ";"]
]

----------------------------------------------------
Expand Down