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 robustness of Crystal lexer #1644

Merged
merged 3 commits into from
Dec 8, 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
21 changes: 13 additions & 8 deletions lib/rouge/lexers/crystal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def self.detect?(text)
)xi, Str::Symbol

# special symbols
rule %r(:(?:\*\*|[-+]@|[/\%&\|^`~]|\[\]=?|<<|>>|<=?>|<=?|===?)),
Str::Symbol
rule %r(:(?:===|=?~|\[\][=?]?|\*\*=?|\/\/=?|[=^*/+-]=?|&[&*+-]?=?|\|\|?=?|![=~]?|%=?|<=>|<<?=?|>>?=?|\.\.\.?)), Str::Symbol

rule %r/:'(\\\\|\\'|[^'])*'/, Str::Symbol
rule %r/:"/, Str::Symbol, :simple_sym
Expand All @@ -36,7 +35,7 @@ def self.detect?(text)
# %-sigiled strings
# %(abc), %[abc], %<abc>, %.abc., %r.abc., etc
delimiter_map = { '{' => '}', '[' => ']', '(' => ')', '<' => '>' }
rule %r/%([rqswQWxiI])?([^\w\s])/ do |m|
rule %r/%([rqswQWxiI])?([^\w\s}])/ do |m|
open = Regexp.escape(m[2])
close = Regexp.escape(delimiter_map[m[2]] || m[2])
interp = /[rQWxI]/ === m[1]
Expand Down Expand Up @@ -79,9 +78,11 @@ def self.detect?(text)
state :strings do
mixin :symbols
rule %r/\b[a-z_]\w*?[?!]?:\s+/, Str::Symbol, :expr_start
rule %r/'(\\\\|\\'|[^'])*'/, Str::Single
rule %r/"/, Str::Double, :simple_string
rule %r/(?<!\.)`/, Str::Backtick, :simple_backtick
rule %r/(')(\\u[a-fA-F0-9]{4}|\\u\{[a-fA-F0-9]{1,6}\}|\\[abefnrtv])?(\\\\|\\'|[^'])*(')/ do
groups Str::Single, Str::Escape, Str::Single, Str::Single
end
end

state :regex_flags do
Expand Down Expand Up @@ -154,11 +155,13 @@ def self.detect?(text)
mixin :whitespace
rule %r/__END__/, Comment::Preproc, :end_part

rule %r/0_?[0-7]+(?:_[0-7]+)*/, Num::Oct
rule %r/0o[0-7]+(?:_[0-7]+)*/, Num::Oct
rule %r/0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/, Num::Hex
rule %r/0b[01]+(?:_[01]+)*/, Num::Bin
rule %r/\d+\.\d+(e[\+\-]?\d+)?/, Num::Float
rule %r/[\d]+(?:_\d+)*/, Num::Integer
rule %r/\d+\.\d+(e[\+\-]?\d+)?(_f32)?/i, Num::Float
rule %r/\d+(e[\+\-]?\d+)/i, Num::Float
rule %r/\d+_f32/i, Num::Float
rule %r/[\d]+(?:_\d+)*(_[iu]\d+)?/, Num::Integer

rule %r/@\[([^\]]+)\]/, Name::Decorator

Expand All @@ -183,7 +186,7 @@ def self.detect?(text)
groups Keyword, Text, Name::Namespace
end

rule %r/(def\b)(\s*)/ do
rule %r/(def|macro\b)(\s*)/ do
groups Keyword, Text
push :funcname
end
Expand Down Expand Up @@ -214,6 +217,7 @@ def self.detect?(text)
rule %r/[a-zA-Z_]\w*/, Name, :method_call
rule %r/\*\*|\/\/|>=|<=|<=>|<<?|>>?|=~|={3}|!~|&&?|\|\||\./,
Operator, :expr_start
rule %r/{%|%}/, Punctuation
rule %r/[-+\/*%=<>&!^|~]=?/, Operator, :expr_start
rule(/[?]/) { token Punctuation; push :ternary; push :expr_start }
rule %r<[\[({,:\\;/]>, Punctuation, :expr_start
Expand Down Expand Up @@ -346,6 +350,7 @@ def self.detect?(text)
mixin :string_intp
rule %r/\\([\\abefnrstv#"']|x[a-fA-F0-9]{1,2}|[0-7]{1,3})/,
Str::Escape
rule %r/\\u([a-fA-F0-9]{4}|\{[^}]+\})/, Str::Escape
rule %r/\\./, Str::Escape
end

Expand Down
Loading