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

Kotlin: Simplify regexes and name fixes #1326

Merged
merged 3 commits into from
Sep 9, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions lib/rouge/lexers/kotlin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +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_backtick = %r'#{name}|`#{name}`'
name_chars = %r'[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}\p{Nd}\p{Pc}\p{Cf}\p{Mn}\p{Mc}]*'

id = %r'(#{name_backtick})'

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
Expand All @@ -55,8 +53,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
Expand All @@ -73,50 +71,50 @@ 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'(@#{class_name})', Name::Decorator
rule %r'(#{class_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'(#{class_name})', Name::Class
rule %r'(#{name})(?=\s*[({])', Name::Function
rule %r'#{name}', Name
lordcodes marked this conversation as resolved.
Show resolved Hide resolved
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'#{class_name}', Name::Class, :pop!
end

state :function do
rule %r'(<)', Punctuation, :generic_parameters
rule %r'(\s+)', Text
rule %r'(#{name_backtick})(\.)' do
rule %r'(#{class_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'#{class_name}', Name::Class
rule %r'(<)', Punctuation, :generic_parameters
rule %r'(,)', Punctuation
rule %r'(\s+)', Text
rule %r'(>)', Punctuation, :pop!
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
Expand Down