Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parsing of numbers in Kotlin lexer #1509

Merged
merged 1 commit into from May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)