1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-10-08 22:52:04 +02:00

fix(Kotlin): Number literals (#1139)

There are some quite a few ways numbers can be expressed in Kotlin which
previously weren't supported and with this commit conforms to the
grammar described in the [Kotlin Language
Specification](https://kotlinlang.org/spec/syntax-and-grammar.html#literals).

### Separators
Like Java Kotlin allows any amount of separators inside number literals,
as long as they don't appear at the start or end.

```Kotlin
9_000
1__2
1_2.3_4E5_6
```

### Plus and minus in E-Notation is optional

While plus and minus _can_ occur after the `e` or `E` it's not required.

```kotlin
123.123e123
123.123E123
123.123e+123
123.123E+123
123.123e-123
123.123E-123
```

### Binary Literals
Just as in Java there are binary literals which start with the `0b` or
`0B` prefix.

```Kotlin
0b011010
0B011010
```

### Suffixes
Kotlin allows the suffixes `f`, `F`, `L`, `u`, `U`, `uL`, `UL`, though
notably the previously supported suffix `l` is not allowed.

```Kotlin
123u
123U
123L
123uL
123UL
123f
0003f
0b01L
0xFF8800L
0XFF8800L
0b01uL
0xFF8800uL
0XFF8800uL
0b01UL
0xFF8800UL
0XFF8800UL
```
This commit is contained in:
Florian Freitag
2025-10-06 15:31:33 +02:00
committed by GitHub
parent 9c8da0f9f5
commit 1074a2a32d
3 changed files with 79 additions and 23 deletions

View File

@@ -113,9 +113,7 @@
<rule pattern="%=|&amp;&amp;|\*=|\+\+|\+=|--|-=|-&gt;|\.\.|\/=|::|&lt;=|==|&gt;=|!!|!=|\|\||\?[:.]">
<token type="Operator"/>
</rule>
<rule pattern="[~!%^&amp;*()+=|\[\]:;,.&lt;&gt;\/?-]">
<token type="Punctuation"/>
</rule>
<rule pattern="[{}]">
<token type="Punctuation"/>
</rule>
@@ -137,8 +135,20 @@
<rule pattern="&#39;\\.&#39;|&#39;[^\\]&#39;">
<token type="LiteralStringChar"/>
</rule>
<rule pattern="0[xX][0-9a-fA-F]+[Uu]?[Ll]?|[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)?[fF]?[Uu]?[Ll]?">
<token type="LiteralNumber"/>
<rule pattern="0[xX][0-9a-fA-F]+(_+[0-9a-fA-F]+)*[uU]?L?">
<token type="LiteralNumberHex"/>
</rule>
<rule pattern="0[bB][01]+(_+[01]+)*[uU]?L?">
<token type="LiteralNumberBin"/>
</rule>
<rule pattern="[0-9]+(_+[0-9]+)*\.[0-9]+(_+[0-9]+)*([eE][+-]?[0-9]+(_+[0-9]+)*)?[fF]?|\.[0-9]+(_+[0-9]+)*([eE][+-]?[0-9]+(_+[0-9]+)*)?[fF]?|[0-9]+(_+[0-9]+)*[eE][+-]?[0-9]+(_+[0-9]+)*[fF]?|[0-9]+(_+[0-9]+)*[fF]">
<token type="LiteralNumberFloat"/>
</rule>
<rule pattern="[0-9]+(_+[0-9]+)*[uU]?L?">
<token type="LiteralNumberInteger"/>
</rule>
<rule pattern="[~!%^&amp;*()+=|\[\]:;,.&lt;&gt;\/?-]">
<token type="Punctuation"/>
</rule>
<rule pattern="(companion)(\s+)(object)">
<bygroups>

View File

@@ -10,7 +10,9 @@ fun main(args: Array<String>) {
StringBuilder().apply {
append(multiline)
}
val unsigned = 0x00UL + 123u + 76.54
val ints = 9_000 + 1__2 + 0b0110_10 + 0B011010
val floats = 1.0 + .45 + 1_2.3_4E5_6 + 56f + 90F + 1.2f
val unsigned = 0x00UL + 123u
}
/*
*/

View File

@@ -56,19 +56,63 @@
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"val"},
{"type":"Text","value":" "},
{"type":"NameProperty","value":"ints"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"9_000"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1__2"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberBin","value":"0b0110_10"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberBin","value":"0B011010"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"val"},
{"type":"Text","value":" "},
{"type":"NameProperty","value":"floats"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"1.0"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":".45"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"1_2.3_4E5_6"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"56f"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"90F"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"1.2f"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"val"},
{"type":"Text","value":" "},
{"type":"NameProperty","value":"unsigned"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"0x00UL"},
{"type":"LiteralNumberHex","value":"0x00UL"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"123u"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"76.54"},
{"type":"LiteralNumberInteger","value":"123u"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
@@ -94,7 +138,7 @@
{"type":"Text","value":" "},
{"type":"Operator","value":"?:"},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"0"},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"fun"},
{"type":"Text","value":" "},
@@ -120,11 +164,11 @@
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"LiteralNumber","value":"1"},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"+"},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"2"},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
@@ -275,7 +319,7 @@
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"123"},
{"type":"LiteralNumberInteger","value":"123"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"println"},
{"type":"Punctuation","value":"("},
@@ -320,7 +364,7 @@
{"type":"Text","value":" "},
{"type":"Operator","value":"%="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"2"},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"a"},
{"type":"Text","value":" "},
@@ -332,27 +376,27 @@
{"type":"Text","value":" "},
{"type":"Operator","value":"*="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"2"},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"a"},
{"type":"Operator","value":"++"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"b"},
{"type":"Operator","value":"+="},
{"type":"LiteralNumber","value":"2"},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"b"},
{"type":"Operator","value":"--"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"a"},
{"type":"Operator","value":"-="},
{"type":"LiteralNumber","value":"1"},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"a"},
{"type":"Text","value":" "},
{"type":"Operator","value":"/="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"2"},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":"\n\t"},
{"type":"Name","value":"a"},
{"type":"Text","value":" "},
@@ -562,7 +606,7 @@
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"0"},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Text","value":"\n\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
@@ -594,7 +638,7 @@
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumber","value":"123"},
{"type":"LiteralNumberInteger","value":"123"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"class"},
{"type":"Text","value":" "},