Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Core Grammars:
- enh(json) add json5 support [Kerry Shetline][]
- fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][]
- fix(csharp) Support digit separators [te-ing][]
- fix(kotlin) Improve number literal parsing[Florian Freitag][]

Documentation:

Expand Down
31 changes: 25 additions & 6 deletions src/languages/kotlin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
Category: common
*/

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

export default function(hljs) {
const KEYWORDS = {
keyword:
Expand Down Expand Up @@ -100,10 +98,31 @@ 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_MODE = NUMERIC;
const decimalDigits = '[0-9](_*[0-9])*';
const frac = `\\.(${decimalDigits})`;
const KOTLIN_NUMBER_MODE = {
className: 'number',
variants: [
// DecimalFloatingPointLiteral
// including ExponentPart
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
`[eE][+-]?(${decimalDigits})[fF]?\\b` },
// excluding ExponentPart
{ begin: `\\b(${decimalDigits})((${frac})[fF]?\\b|\\.([fF]\\b)?)` },
{ begin: `(${frac})[fF]?\\b` },
{ begin: `\\b(${decimalDigits})[fF]\\b` },

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

// HexIntegerLiteral
{ begin: `\\b0[xX]([0-9a-fA-F](_*[0-9a-fA-F])*)[uU]?L?\\b` },

// BinaryIntegerLiteral
{ begin: '\\b0[bB][01](_*[01])*[uU]?L?\\b' },
],
relevance: 0
};
const KOTLIN_NESTED_COMMENT = hljs.COMMENT(
'/\\*', '\\*/',
{ contains: [ hljs.C_BLOCK_COMMENT_MODE ] }
Expand Down
45 changes: 45 additions & 0 deletions test/markup/kotlin/numbers.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<span class="hljs-comment">// Postive cases</span>
<span class="hljs-number">1</span>
<span class="hljs-number">123</span>
<span class="hljs-number">123L</span>
<span class="hljs-number">123f</span>
<span class="hljs-number">123F</span>
<span class="hljs-number">123u</span>
<span class="hljs-number">123U</span>
<span class="hljs-number">123UL</span>
<span class="hljs-number">123uL</span>
<span class="hljs-number">1.23</span>
<span class="hljs-number">1.23f</span>
<span class="hljs-number">1.23F</span>
<span class="hljs-number">.123</span>
<span class="hljs-number">.123f</span>
<span class="hljs-number">.123F</span>
<span class="hljs-number">0b10101</span>
<span class="hljs-number">0B10101</span>
<span class="hljs-number">0b10101u</span>
<span class="hljs-number">0B10101u</span>
<span class="hljs-number">0b10101U</span>
<span class="hljs-number">0B10101U</span>
<span class="hljs-number">0b10101L</span>
<span class="hljs-number">0B10101L</span>
<span class="hljs-number">0b10101uL</span>
<span class="hljs-number">0B10101uL</span>
<span class="hljs-number">0b10101UL</span>
<span class="hljs-number">0B10101UL</span>
<span class="hljs-number">0xff8842</span>
<span class="hljs-number">0Xff8842</span>
<span class="hljs-number">0xff8842u</span>
<span class="hljs-number">0Xff8842u</span>
<span class="hljs-number">0xff8842U</span>
<span class="hljs-number">0Xff8842U</span>
<span class="hljs-number">0xff8842L</span>
<span class="hljs-number">0Xff8842L</span>
<span class="hljs-number">0xff8842uL</span>
<span class="hljs-number">0Xff8842uL</span>
<span class="hljs-number">0xff8842UL</span>
<span class="hljs-number">0Xff8842UL</span>

<span class="hljs-comment">// Negative cases</span>
123l
123d
123D
45 changes: 45 additions & 0 deletions test/markup/kotlin/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Postive cases
1
123
123L
123f
123F
123u
123U
123UL
123uL
1.23
1.23f
1.23F
.123
.123f
.123F
0b10101
0B10101
0b10101u
0B10101u
0b10101U
0B10101U
0b10101L
0B10101L
0b10101uL
0B10101uL
0b10101UL
0B10101UL
0xff8842
0Xff8842
0xff8842u
0Xff8842u
0xff8842U
0Xff8842U
0xff8842L
0Xff8842L
0xff8842uL
0Xff8842uL
0xff8842UL
0Xff8842UL

// Negative cases
123l
123d
123D