Skip to content

Commit

Permalink
Improve handling of numbers in Kotlin lexer (#1509)
Browse files Browse the repository at this point in the history
This commit improves the handling of numbers in the Kotlin lexer.
Specifically, it:

1. adds support for underscore separators;
2. adds support for binary literals;
3. removes invalid support for the lowercase "l" suffix; and
4. adds support for unsigned literals.
  • Loading branch information
goodhoko committed May 12, 2020
1 parent 1e32f3c commit 03be225
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rouge/lexers/kotlin.rb
Expand Up @@ -67,13 +67,20 @@ class Kotlin < RegexLexer
rule %r'\n', Text
rule %r'::|!!|\?[:.]', Operator
rule %r"(\.\.)", Operator
# Number literals
decDigits = %r"([0-9][0-9_]*[0-9])|[0-9]"
exponent = %r"[eE][+-]?(#{decDigits})"
double = %r"((#{decDigits})?\.#{decDigits}(#{exponent})?)|(#{decDigits}#{exponent})"
rule %r"(#{double}[fF]?)|(#{decDigits}[fF])", Num::Float
rule %r"0[bB]([01][01_]*[01]|[01])[uU]?L?", Num::Bin
rule %r"0[xX]([0-9a-fA-F][0-9a-fA-F_]*[0-9a-fA-F]|[0-9a-fA-F])[uU]?L?", Num::Hex
rule %r"(([1-9][0-9_]*[0-9])|[0-9])[uU]?L?", Num::Integer
rule %r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation
rule %r'[{}]', Punctuation
rule %r'@"(""|[^"])*"'m, Str
rule %r'""".*?"""'m, Str
rule %r'"(\\\\|\\"|[^"\n])*["\n]'m, Str
rule %r"'\\.'|'[^\\]'", Str::Char
rule %r"[0-9](\.[0-9]+)?([eE][+-][0-9]+)?[flFL]?|0[xX][0-9a-fA-F]+[Ll]?", Num
rule %r'(@#{class_name})', Name::Decorator
rule %r'(#{class_name})(<)' do
groups Name::Class, Punctuation
Expand Down
6 changes: 6 additions & 0 deletions spec/visual/samples/kotlin
Expand Up @@ -182,4 +182,10 @@ loop@ for (i in 1..100) {
}
}

val ints = listOf(1, 4_20, 100L, 123_345_678U)
val bins = listOf(0b1, 0B0, 0b10_10_01, 0b10U, 0b10uL)
val hexes = listOf(0xFF, 0Xa, 0x10L, 0X0UL)
val exponents = listOf(1e10, -1E10F, 1.5e10, 1e-10)
val doublesAndFloats = listOf(3.14, .99, 22f, 1_000.000_1, 19.95F)

// comment at EOF (#797)

0 comments on commit 03be225

Please sign in to comment.