Skip to content

Commit 02ca487

Browse files
committed
fix(kotlin) fix poly backtracking issue
- Use same numeric mode rules as for Java
1 parent dee6434 commit 02ca487

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

Diff for: src/languages/java.js

+3-35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Category: common, enterprise
55
Website: https://www.java.com/
66
*/
77

8+
import { NUMERIC } from "./lib/java.js";
9+
810
export default function(hljs) {
911
var JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
1012
var GENERIC_IDENT_RE = JAVA_IDENT_RE + '(<' + JAVA_IDENT_RE + '(\\s*,\\s*' + JAVA_IDENT_RE + ')*>)?';
@@ -25,41 +27,7 @@ export default function(hljs) {
2527
},
2628
]
2729
};
28-
29-
// https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10
30-
var decimalDigits = '[0-9](_*[0-9])*';
31-
var frac = `\\.(${decimalDigits})`;
32-
var hexDigits = '[0-9a-fA-F](_*[0-9a-fA-F])*';
33-
var NUMBER = {
34-
className: 'number',
35-
variants: [
36-
// DecimalFloatingPointLiteral
37-
// including ExponentPart
38-
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
39-
`[eE][+-]?(${decimalDigits})[fFdD]?\\b` },
40-
// excluding ExponentPart
41-
{ begin: `\\b(${decimalDigits})((${frac})[fFdD]?\\b|\\.([fFdD]\\b)?)` },
42-
{ begin: `(${frac})[fFdD]?\\b` },
43-
{ begin: `\\b(${decimalDigits})[fFdD]\\b` },
44-
45-
// HexadecimalFloatingPointLiteral
46-
{ begin: `\\b0[xX]((${hexDigits})\\.?|(${hexDigits})?\\.(${hexDigits}))` +
47-
`[pP][+-]?(${decimalDigits})[fFdD]?\\b` },
48-
49-
// DecimalIntegerLiteral
50-
{ begin: '\\b(0|[1-9](_*[0-9])*)[lL]?\\b' },
51-
52-
// HexIntegerLiteral
53-
{ begin: `\\b0[xX](${hexDigits})[lL]?\\b` },
54-
55-
// OctalIntegerLiteral
56-
{ begin: '\\b0(_*[0-7])*[lL]?\\b' },
57-
58-
// BinaryIntegerLiteral
59-
{ begin: '\\b0[bB][01](_*[01])*[lL]?\\b' },
60-
],
61-
relevance: 0
62-
};
30+
const NUMBER = NUMERIC;
6331

6432
return {
6533
name: 'Java',

Diff for: src/languages/kotlin.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
Category: common
77
*/
88

9+
import { NUMERIC } from "./lib/java.js";
10+
911
export default function(hljs) {
1012
const KEYWORDS = {
1113
keyword:
@@ -104,25 +106,7 @@ export default function(hljs) {
104106
// https://kotlinlang.org/docs/reference/whatsnew11.html#underscores-in-numeric-literals
105107
// According to the doc above, the number mode of kotlin is the same as java 8,
106108
// so the code below is copied from java.js
107-
const KOTLIN_NUMBER_RE = '\\b' +
108-
'(' +
109-
'0[bB]([01]+[01_]+[01]+|[01]+)' + // 0b...
110-
'|' +
111-
'0[xX]([a-fA-F0-9]+[a-fA-F0-9_]+[a-fA-F0-9]+|[a-fA-F0-9]+)' + // 0x...
112-
'|' +
113-
'(' +
114-
'([\\d]+[\\d_]+[\\d]+|[\\d]+)(\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))?' +
115-
'|' +
116-
'\\.([\\d]+[\\d_]+[\\d]+|[\\d]+)' +
117-
')' +
118-
'([eE][-+]?\\d+)?' + // octal, decimal, float
119-
')' +
120-
'[lLfF]?';
121-
const KOTLIN_NUMBER_MODE = {
122-
className: 'number',
123-
begin: KOTLIN_NUMBER_RE,
124-
relevance: 0
125-
};
109+
const KOTLIN_NUMBER_MODE = NUMERIC;
126110
const KOTLIN_NESTED_COMMENT = hljs.COMMENT(
127111
'/\\*', '\\*/',
128112
{

Diff for: src/languages/lib/java.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
// https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10
3+
var decimalDigits = '[0-9](_*[0-9])*';
4+
var frac = `\\.(${decimalDigits})`;
5+
var hexDigits = '[0-9a-fA-F](_*[0-9a-fA-F])*';
6+
export var NUMERIC = {
7+
className: 'number',
8+
variants: [
9+
// DecimalFloatingPointLiteral
10+
// including ExponentPart
11+
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
12+
`[eE][+-]?(${decimalDigits})[fFdD]?\\b` },
13+
// excluding ExponentPart
14+
{ begin: `\\b(${decimalDigits})((${frac})[fFdD]?\\b|\\.([fFdD]\\b)?)` },
15+
{ begin: `(${frac})[fFdD]?\\b` },
16+
{ begin: `\\b(${decimalDigits})[fFdD]\\b` },
17+
18+
// HexadecimalFloatingPointLiteral
19+
{ begin: `\\b0[xX]((${hexDigits})\\.?|(${hexDigits})?\\.(${hexDigits}))` +
20+
`[pP][+-]?(${decimalDigits})[fFdD]?\\b` },
21+
22+
// DecimalIntegerLiteral
23+
{ begin: '\\b(0|[1-9](_*[0-9])*)[lL]?\\b' },
24+
25+
// HexIntegerLiteral
26+
{ begin: `\\b0[xX](${hexDigits})[lL]?\\b` },
27+
28+
// OctalIntegerLiteral
29+
{ begin: '\\b0(_*[0-7])*[lL]?\\b' },
30+
31+
// BinaryIntegerLiteral
32+
{ begin: '\\b0[bB][01](_*[01])*[lL]?\\b' },
33+
],
34+
relevance: 0
35+
};

0 commit comments

Comments
 (0)