From 9ee947c9be018263386e95a19bc37c0b79d5f58f Mon Sep 17 00:00:00 2001 From: Jan Koutny Date: Sun, 3 May 2020 12:04:20 +0100 Subject: [PATCH] Improve parsing of numbers in Kotlin lexer - Adds support for underscore separator. - Add support for binary literals. - Removes invalid support for lowercase "l" suffix. - Adds support for unsigned literals. - Adds support for dot-starting real literals. --- lib/rouge/lexers/kotlin.rb | 9 ++++++++- spec/visual/samples/kotlin | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index d60d6efd24..fe83a2adb0 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -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 diff --git a/spec/visual/samples/kotlin b/spec/visual/samples/kotlin index 1ece9ae72d..fd4892171d 100644 --- a/spec/visual/samples/kotlin +++ b/spec/visual/samples/kotlin @@ -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)