Skip to content
Permalink
Browse files
JS: Improved number pattern (#3149)
  • Loading branch information
RunDevelopment committed Oct 14, 2021
1 parent 3c61c8f commit 5a24cbffd76d29c055808283aea6386031a1680d
Showing with 127 additions and 4 deletions.
  1. +28 −1 components/prism-javascript.js
  2. +1 −1 components/prism-javascript.min.js
  3. +28 −1 prism.js
  4. +70 −1 tests/languages/javascript/number_feature.test
@@ -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}|\?\?=?|\?\.?|[~:]/
});

Some generated files are not rendered by default. Learn more.

@@ -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}|\?\?=?|\?\.?|[~:]/
});

@@ -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;

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

[
@@ -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", ";"]
]

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

0 comments on commit 5a24cbf

Please sign in to comment.