From 417ec70b09b7609a4e7b1e097067d218a71bae70 Mon Sep 17 00:00:00 2001 From: Jen Date: Tue, 12 May 2020 18:16:56 +0100 Subject: [PATCH] Improve handling of numbers in Kotlin lexer (#1509) 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. --- 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)