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

Improve support for single quotes in Haskell lexer #1524

Merged
merged 5 commits into from
Jun 3, 2020
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
46 changes: 27 additions & 19 deletions lib/rouge/lexers/haskell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def self.detect?(text)
end

reserved = %w(
_ case class data default deriving do else if in
infix[lr]? instance let newtype of then type where
_ case class data default deriving do else if in infix infixl infixr
instance let newtype of then type where
)

ascii = %w(
Expand Down Expand Up @@ -54,14 +54,31 @@ def self.detect?(text)
state :root do
mixin :basic

rule %r/\bimport\b/, Keyword::Reserved, :import
rule %r/\bmodule\b/, Keyword::Reserved, :module
rule %r/\b(?:#{reserved.join('|')})\b/, Keyword::Reserved
# not sure why, but ^ doesn't work here
# rule %r/^[_a-z][\w']*/, Name::Function
rule %r/[_a-z][\w']*/, Name
rule %r/[A-Z][\w']*/, Keyword::Type
rule %r/'[A-Z]\w+'?/, Keyword::Type # promoted data constructor
rule %r/'(?=(?:.|\\\S+)')/, Str::Char, :character
rule %r/"/, Str, :string

rule %r/\d+e[+-]?\d+/i, Num::Float
rule %r/\d+\.\d+(e[+-]?\d+)?/i, Num::Float
rule %r/0o[0-7]+/i, Num::Oct
rule %r/0x[\da-f]+/i, Num::Hex
rule %r/\d+/, Num::Integer

rule %r/[\w']+/ do |m|
match = m[0]
if match == "import"
token Keyword::Reserved
push :import
elsif match == "module"
token Keyword::Reserved
push :module
elsif reserved.include?(match)
token Keyword::Reserved
elsif match =~ /\A'?[A-Z]/
token Keyword::Type
else
token Name
end
end

# lambda operator
rule %r(\\(?![:!#\$\%&*+.\\/<=>?@^\|~-]+)), Name::Function
Expand All @@ -72,15 +89,6 @@ def self.detect?(text)
# other operators
rule %r([:!#\$\%&*+.\\/<=>?@^\|~-]+), Operator

rule %r/\d+e[+-]?\d+/i, Num::Float
rule %r/\d+\.\d+(e[+-]?\d+)?/i, Num::Float
rule %r/0o[0-7]+/i, Num::Oct
rule %r/0x[\da-f]+/i, Num::Hex
rule %r/\d+/, Num::Integer

rule %r/'/, Str::Char, :character
rule %r/"/, Str, :string

rule %r/\[\s*\]/, Keyword::Type
rule %r/\(\s*\)/, Name::Builtin

Expand Down
2 changes: 2 additions & 0 deletions spec/visual/samples/haskell
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,5 @@ quasi3 = [here|Newlines
are part of
the quotation
|]

[ H.label [ H.for' hName, H.class' "col-sm-2 col-form-label" ] [ text msg ]