From ba972ad1d530fdd69b0a678a2c77220d5a9c29be Mon Sep 17 00:00:00 2001 From: Andrew Lord Date: Mon, 9 Sep 2019 12:09:51 +0100 Subject: [PATCH 1/3] Kotlin: Simplify identifier regexes --- lib/rouge/lexers/kotlin.rb | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index a9d294f40e..03d1d84626 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -24,10 +24,7 @@ class Kotlin < RegexLexer while yield ) - name = %r'@?[_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}][\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}\p{Nd}\p{Pc}\p{Cf}\p{Mn}\p{Mc}]*' - name_backtick = %r'#{name}|`#{name}`' - - id = %r'(#{name_backtick})' + name = %r'@?`?[_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}][\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}\p{Nd}\p{Pc}\p{Cf}\p{Mn}\p{Mc}]*`?' upper = %r'[\p{Lu}]' @@ -55,8 +52,8 @@ class Kotlin < RegexLexer groups Keyword::Declaration, Text push :property end - rule %r/\bfun\b/, Keyword - rule %r/\b(?:#{keywords.join('|')})\b/, Keyword + rule %r'\bfun\b', Keyword + rule %r'\b(?:#{keywords.join('|')})\b', Keyword rule %r'^\s*\[.*?\]', Name::Attribute rule %r'[^\S\n]+', Text rule %r'\\\n', Text # line continuation @@ -73,35 +70,35 @@ class Kotlin < RegexLexer 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/(@#{upper}#{name_backtick})/, Name::Decorator - rule %r'(#{upper}#{name_backtick})(<)' do + rule %r'(@#{upper}#{name})', Name::Decorator + rule %r'(#{upper}#{name})(<)' do groups Name::Class, Punctuation push :generic_parameters end - rule %r'(#{upper}#{name_backtick})', Name::Class - rule %r'(#{name_backtick})(?=\s*[({])', Name::Function - rule id, Name + rule %r'(#{upper}#{name})', Name::Class + rule %r'(#{name})(?=\s*[({])', Name::Function + rule %r'#{name}', Name end state :package do - rule %r/\S+/, Name::Namespace, :pop! + rule %r'\S+', Name::Namespace, :pop! end state :class do - rule id, Name::Class, :pop! + rule %r'#{name}', Name::Class, :pop! end state :function do rule %r'(<)', Punctuation, :generic_parameters rule %r'(\s+)', Text - rule %r'(#{name_backtick})(\.)' do + rule %r'(#{name})(\.)' do groups Name::Class, Punctuation end - rule id, Name::Function, :pop! + rule %r'#{name}', Name::Function, :pop! end state :generic_parameters do - rule id, Name::Class + rule %r'#{name}', Name::Class rule %r'(<)', Punctuation, :generic_parameters rule %r'(,)', Punctuation rule %r'(\s+)', Text @@ -109,14 +106,14 @@ class Kotlin < RegexLexer end state :property do - rule id, Name::Property, :pop! + rule %r'#{name}', Name::Property, :pop! end state :destructure do rule %r'(,)', Punctuation rule %r'(\))', Punctuation, :pop! rule %r'(\s+)', Text - rule id, Name::Property + rule %r'#{name}', Name::Property end state :comment do From 8dbf6d6863546d20809de54d95026261c6c45a2c Mon Sep 17 00:00:00 2001 From: Andrew Lord Date: Mon, 9 Sep 2019 12:22:04 +0100 Subject: [PATCH 2/3] Kotlin: Simplify identifier/name regexes Fix - Annotations, e.g. @Deprecated or @`BacktickedAnnotation` For Name::Class locations, specifically match the class name regex, with a capital letter. --- lib/rouge/lexers/kotlin.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index 03d1d84626..cb36d320dd 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -24,9 +24,10 @@ class Kotlin < RegexLexer while yield ) - name = %r'@?`?[_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}][\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}\p{Nd}\p{Pc}\p{Cf}\p{Mn}\p{Mc}]*`?' + name_chars = %r'[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}\p{Nd}\p{Pc}\p{Cf}\p{Mn}\p{Mc}]*' - upper = %r'[\p{Lu}]' + class_name = %r'`?[\p{Lu}]#{name_chars}`?' + name = %r'`?[_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}]#{name_chars}`?' state :root do rule %r'\b(companion)(\s+)(object)\b' do @@ -70,12 +71,12 @@ class Kotlin < RegexLexer 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'(@#{upper}#{name})', Name::Decorator - rule %r'(#{upper}#{name})(<)' do + rule %r'(@#{class_name})', Name::Decorator + rule %r'(#{class_name})(<)' do groups Name::Class, Punctuation push :generic_parameters end - rule %r'(#{upper}#{name})', Name::Class + rule %r'(#{class_name})', Name::Class rule %r'(#{name})(?=\s*[({])', Name::Function rule %r'#{name}', Name end @@ -85,20 +86,20 @@ class Kotlin < RegexLexer end state :class do - rule %r'#{name}', Name::Class, :pop! + rule %r'#{class_name}', Name::Class, :pop! end state :function do rule %r'(<)', Punctuation, :generic_parameters rule %r'(\s+)', Text - rule %r'(#{name})(\.)' do + rule %r'(#{class_name})(\.)' do groups Name::Class, Punctuation end rule %r'#{name}', Name::Function, :pop! end state :generic_parameters do - rule %r'#{name}', Name::Class + rule %r'#{class_name}', Name::Class rule %r'(<)', Punctuation, :generic_parameters rule %r'(,)', Punctuation rule %r'(\s+)', Text From 618bbad257d03a834f8f103e9129cef222fdd8d5 Mon Sep 17 00:00:00 2001 From: Andrew Lord Date: Mon, 9 Sep 2019 19:21:49 +0100 Subject: [PATCH 3/3] Remove unnecessary interpolation --- lib/rouge/lexers/kotlin.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index cb36d320dd..a42cde9e0f 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -76,9 +76,9 @@ class Kotlin < RegexLexer groups Name::Class, Punctuation push :generic_parameters end - rule %r'(#{class_name})', Name::Class + rule class_name, Name::Class rule %r'(#{name})(?=\s*[({])', Name::Function - rule %r'#{name}', Name + rule name, Name end state :package do @@ -86,7 +86,7 @@ class Kotlin < RegexLexer end state :class do - rule %r'#{class_name}', Name::Class, :pop! + rule class_name, Name::Class, :pop! end state :function do @@ -95,11 +95,11 @@ class Kotlin < RegexLexer rule %r'(#{class_name})(\.)' do groups Name::Class, Punctuation end - rule %r'#{name}', Name::Function, :pop! + rule name, Name::Function, :pop! end state :generic_parameters do - rule %r'#{class_name}', Name::Class + rule class_name, Name::Class rule %r'(<)', Punctuation, :generic_parameters rule %r'(,)', Punctuation rule %r'(\s+)', Text @@ -107,14 +107,14 @@ class Kotlin < RegexLexer end state :property do - rule %r'#{name}', Name::Property, :pop! + rule name, Name::Property, :pop! end state :destructure do rule %r'(,)', Punctuation rule %r'(\))', Punctuation, :pop! rule %r'(\s+)', Text - rule %r'#{name}', Name::Property + rule name, Name::Property end state :comment do