Skip to content

Commit 24fcd0e

Browse files
committed
fix(kotlin) number literals
Previously kotlin resused the java number parsing but there are significant differnces in how the two languages express numbers causing false-negatives and false-positives.
1 parent 5697ae5 commit 24fcd0e

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Core Grammars:
2626
- enh(json) add json5 support [Kerry Shetline][]
2727
- fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][]
2828
- fix(csharp) Support digit separators [te-ing][]
29+
- fix(kotlin) Improve number literal parsing[Florian Freitag][]
2930

3031
Documentation:
3132

src/languages/kotlin.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
Category: common
77
*/
88

9-
import { NUMERIC } from "./lib/java.js";
10-
119
export default function(hljs) {
1210
const KEYWORDS = {
1311
keyword:
@@ -100,10 +98,31 @@ export default function(hljs) {
10098
]
10199
};
102100

103-
// https://kotlinlang.org/docs/reference/whatsnew11.html#underscores-in-numeric-literals
104-
// According to the doc above, the number mode of kotlin is the same as java 8,
105-
// so the code below is copied from java.js
106-
const KOTLIN_NUMBER_MODE = NUMERIC;
101+
const decimalDigits = '[0-9](_*[0-9])*';
102+
const frac = `\\.(${decimalDigits})`;
103+
const KOTLIN_NUMBER_MODE = {
104+
className: 'number',
105+
variants: [
106+
// DecimalFloatingPointLiteral
107+
// including ExponentPart
108+
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
109+
`[eE][+-]?(${decimalDigits})[fF]?\\b` },
110+
// excluding ExponentPart
111+
{ begin: `\\b(${decimalDigits})((${frac})[fF]?\\b|\\.([fF]\\b)?)` },
112+
{ begin: `(${frac})[fF]?\\b` },
113+
{ begin: `\\b(${decimalDigits})[fF]\\b` },
114+
115+
// DecimalIntegerLiteral
116+
{ begin: '\\b(0|[1-9](_*[0-9])*)[uU]?L?\\b' },
117+
118+
// HexIntegerLiteral
119+
{ begin: `\\b0[xX]([0-9a-fA-F](_*[0-9a-fA-F])*)[uU]?L?\\b` },
120+
121+
// BinaryIntegerLiteral
122+
{ begin: '\\b0[bB][01](_*[01])*[uU]?L?\\b' },
123+
],
124+
relevance: 0
125+
};
107126
const KOTLIN_NESTED_COMMENT = hljs.COMMENT(
108127
'/\\*', '\\*/',
109128
{ contains: [ hljs.C_BLOCK_COMMENT_MODE ] }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<span class="hljs-comment">// Postive cases</span>
2+
<span class="hljs-number">1</span>
3+
<span class="hljs-number">123</span>
4+
<span class="hljs-number">123L</span>
5+
<span class="hljs-number">123f</span>
6+
<span class="hljs-number">123F</span>
7+
<span class="hljs-number">123u</span>
8+
<span class="hljs-number">123U</span>
9+
<span class="hljs-number">123UL</span>
10+
<span class="hljs-number">123uL</span>
11+
<span class="hljs-number">1.23</span>
12+
<span class="hljs-number">1.23f</span>
13+
<span class="hljs-number">1.23F</span>
14+
<span class="hljs-number">.123</span>
15+
<span class="hljs-number">.123f</span>
16+
<span class="hljs-number">.123F</span>
17+
<span class="hljs-number">0b10101</span>
18+
<span class="hljs-number">0B10101</span>
19+
<span class="hljs-number">0b10101u</span>
20+
<span class="hljs-number">0B10101U</span>
21+
<span class="hljs-number">0b10101L</span>
22+
<span class="hljs-number">0B10101uL</span>
23+
<span class="hljs-number">0B10101UL</span>
24+
<span class="hljs-number">0xff8842</span>
25+
<span class="hljs-number">0xff8842u</span>
26+
<span class="hljs-number">0xff8842U</span>
27+
<span class="hljs-number">0xff8842L</span>
28+
<span class="hljs-number">0xff8842uL</span>
29+
<span class="hljs-number">0xff8842UL</span>
30+
31+
<span class="hljs-comment">// Negative cases</span>
32+
123l
33+
123d
34+
123D

test/markup/kotlin/numbers.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Postive cases
2+
1
3+
123
4+
123L
5+
123f
6+
123F
7+
123u
8+
123U
9+
123UL
10+
123uL
11+
1.23
12+
1.23f
13+
1.23F
14+
.123
15+
.123f
16+
.123F
17+
0b10101
18+
0B10101
19+
0b10101u
20+
0B10101U
21+
0b10101L
22+
0B10101uL
23+
0B10101UL
24+
0xff8842
25+
0xff8842u
26+
0xff8842U
27+
0xff8842L
28+
0xff8842uL
29+
0xff8842UL
30+
31+
// Negative cases
32+
123l
33+
123d
34+
123D

0 commit comments

Comments
 (0)