Skip to content

Commit

Permalink
fix(kotlin) fix poly backtracking issue
Browse files Browse the repository at this point in the history
- Use same numeric mode rules as for Java
  • Loading branch information
joshgoebel committed Dec 3, 2020
1 parent dee6434 commit 02ca487
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 54 deletions.
38 changes: 3 additions & 35 deletions src/languages/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Category: common, enterprise
Website: https://www.java.com/
*/

import { NUMERIC } from "./lib/java.js";

export default function(hljs) {
var JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
var GENERIC_IDENT_RE = JAVA_IDENT_RE + '(<' + JAVA_IDENT_RE + '(\\s*,\\s*' + JAVA_IDENT_RE + ')*>)?';
Expand All @@ -25,41 +27,7 @@ export default function(hljs) {
},
]
};

// https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10
var decimalDigits = '[0-9](_*[0-9])*';
var frac = `\\.(${decimalDigits})`;
var hexDigits = '[0-9a-fA-F](_*[0-9a-fA-F])*';
var NUMBER = {
className: 'number',
variants: [
// DecimalFloatingPointLiteral
// including ExponentPart
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
`[eE][+-]?(${decimalDigits})[fFdD]?\\b` },
// excluding ExponentPart
{ begin: `\\b(${decimalDigits})((${frac})[fFdD]?\\b|\\.([fFdD]\\b)?)` },
{ begin: `(${frac})[fFdD]?\\b` },
{ begin: `\\b(${decimalDigits})[fFdD]\\b` },

// HexadecimalFloatingPointLiteral
{ begin: `\\b0[xX]((${hexDigits})\\.?|(${hexDigits})?\\.(${hexDigits}))` +
`[pP][+-]?(${decimalDigits})[fFdD]?\\b` },

// DecimalIntegerLiteral
{ begin: '\\b(0|[1-9](_*[0-9])*)[lL]?\\b' },

// HexIntegerLiteral
{ begin: `\\b0[xX](${hexDigits})[lL]?\\b` },

// OctalIntegerLiteral
{ begin: '\\b0(_*[0-7])*[lL]?\\b' },

// BinaryIntegerLiteral
{ begin: '\\b0[bB][01](_*[01])*[lL]?\\b' },
],
relevance: 0
};
const NUMBER = NUMERIC;

return {
name: 'Java',
Expand Down
22 changes: 3 additions & 19 deletions src/languages/kotlin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Category: common
*/

import { NUMERIC } from "./lib/java.js";

export default function(hljs) {
const KEYWORDS = {
keyword:
Expand Down Expand Up @@ -104,25 +106,7 @@ export default function(hljs) {
// https://kotlinlang.org/docs/reference/whatsnew11.html#underscores-in-numeric-literals
// According to the doc above, the number mode of kotlin is the same as java 8,
// so the code below is copied from java.js
const KOTLIN_NUMBER_RE = '\\b' +
'(' +
'0[bB]([01]+[01_]+[01]+|[01]+)' + // 0b...
'|' +
'0[xX]([a-fA-F0-9]+[a-fA-F0-9_]+[a-fA-F0-9]+|[a-fA-F0-9]+)' + // 0x...
'|' +
'(' +
'([\\d]+[\\d_]+[\\d]+|[\\d]+)(\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))?' +
'|' +
'\\.([\\d]+[\\d_]+[\\d]+|[\\d]+)' +
')' +
'([eE][-+]?\\d+)?' + // octal, decimal, float
')' +
'[lLfF]?';
const KOTLIN_NUMBER_MODE = {
className: 'number',
begin: KOTLIN_NUMBER_RE,
relevance: 0
};
const KOTLIN_NUMBER_MODE = NUMERIC;
const KOTLIN_NESTED_COMMENT = hljs.COMMENT(
'/\\*', '\\*/',
{
Expand Down
35 changes: 35 additions & 0 deletions src/languages/lib/java.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10
var decimalDigits = '[0-9](_*[0-9])*';
var frac = `\\.(${decimalDigits})`;
var hexDigits = '[0-9a-fA-F](_*[0-9a-fA-F])*';
export var NUMERIC = {
className: 'number',
variants: [
// DecimalFloatingPointLiteral
// including ExponentPart
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
`[eE][+-]?(${decimalDigits})[fFdD]?\\b` },
// excluding ExponentPart
{ begin: `\\b(${decimalDigits})((${frac})[fFdD]?\\b|\\.([fFdD]\\b)?)` },
{ begin: `(${frac})[fFdD]?\\b` },
{ begin: `\\b(${decimalDigits})[fFdD]\\b` },

// HexadecimalFloatingPointLiteral
{ begin: `\\b0[xX]((${hexDigits})\\.?|(${hexDigits})?\\.(${hexDigits}))` +
`[pP][+-]?(${decimalDigits})[fFdD]?\\b` },

// DecimalIntegerLiteral
{ begin: '\\b(0|[1-9](_*[0-9])*)[lL]?\\b' },

// HexIntegerLiteral
{ begin: `\\b0[xX](${hexDigits})[lL]?\\b` },

// OctalIntegerLiteral
{ begin: '\\b0(_*[0-7])*[lL]?\\b' },

// BinaryIntegerLiteral
{ begin: '\\b0[bB][01](_*[01])*[lL]?\\b' },
],
relevance: 0
};

0 comments on commit 02ca487

Please sign in to comment.