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

Update Scala lexer to output more differentiated token types. #1040

Merged
merged 1 commit into from
May 30, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions lib/rouge/lexers/scala.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,38 @@ class Scala < RegexLexer
rule %r(/\*), Comment::Multiline, :comment

rule /@#{idrest}/, Name::Decorator

rule /(def)(\s+)(#{idrest}|#{op}+|`[^`]+`)(\s*)/ do
groups Keyword, Text, Name::Function, Text
end

rule /(val)(\s+)(#{idrest}|#{op}+|`[^`]+`)(\s*)/ do
groups Keyword, Text, Name::Variable, Text
end

rule /(this)(\n*)(\.)(#{idrest})/ do
groups Keyword, Text, Operator, Name::Property
end

rule /(#{idrest}|_)(\n*)(\.)(#{idrest})/ do
groups Name::Variable, Text, Operator, Name::Property
end

rule /#{upper}#{idrest}\b/, Name::Class

rule /(#{idrest})(#{whitespace}*)(\()/ do
groups Name::Function, Text, Operator
end

rule /(\.)(#{idrest})/ do
groups Operator, Name::Property
end

rule %r(
(#{keywords.join("|")})\b|
(<[%:-]|=>|>:|[#=@_\u21D2\u2190])(\b|(?=\s)|$)
)x, Keyword
rule /:(?!#{op})/, Keyword, :type
rule /#{upper}#{idrest}\b/, Name::Class
rule /:(?!#{op})/, Keyword, :type
rule /(true|false|null)\b/, Keyword::Constant
rule /(import|package)(\s+)/ do
groups Keyword, Text
Expand Down
8 changes: 8 additions & 0 deletions spec/visual/samples/scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ String
val constant = true

import more.stuff._

def methodChaining(s: String): String = {
val n = s.length.toInt
s.foreach(print)
s
.take(n)
.toLowerCase
}
}

abstract case class Foo[+A, B <: List[A]](a: A) {
Expand Down