Skip to content

Commit 0baa0cb

Browse files
author
jneen
committed
use the keywords api for crystal
1 parent 3061e45 commit 0baa0cb

File tree

1 file changed

+58
-50
lines changed

1 file changed

+58
-50
lines changed

lib/rouge/lexers/crystal.rb

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ def self.detect?(text)
1616
return true if text.shebang? 'crystal'
1717
end
1818

19+
KEYWORDS = Set.new %w(
20+
BEGIN END alias begin break case defined\? do else elsif end
21+
ensure for if ifdef in next redo rescue raise retry return super then
22+
undef unless until when while yield lib fun type of as
23+
24+
abstract union enum
25+
)
26+
27+
KEYWORDS_PSEUDO = Set.new %w(
28+
initialize new loop include extend raise attr_reader attr_writer
29+
attr_accessor alias_method attr catch throw private module_function
30+
public protected true false nil __FILE__ __LINE__
31+
getter getter? getter! property property? property! struct record
32+
)
33+
34+
BUILTIN_GLOBALS = Set.new %w(
35+
abort ancestors at_exit
36+
caller catch chomp chop clone constants
37+
display dup eval exec exit extend fail fork format freeze
38+
getc gets global_variables gsub hash id included_modules
39+
inspect instance_eval instance_method instance_methods
40+
lambda loop method method_missing
41+
methods module_eval object_id open p print printf
42+
proc putc puts raise rand
43+
readline readlines require scan select self send
44+
sleep split sprintf srand sub syscall system
45+
test throw to_a to_s trap warn flag?
46+
)
47+
48+
BUILTIN_METHODS = Set.new %w(
49+
eql? equal? include? is_a? iterator? kind_of? nil?
50+
chomp! chop! exit! gsub! sub!
51+
)
52+
1953
state :symbols do
2054
# symbols
2155
rule %r(
@@ -90,9 +124,11 @@ def self.detect?(text)
90124
end
91125

92126
# double-quoted string and symbol
93-
[[:string, Str::Double, '"'],
94-
[:sym, Str::Symbol, '"'],
95-
[:backtick, Str::Backtick, '`']].each do |name, tok, fin|
127+
[
128+
[:string, Str::Double, '"'],
129+
[:sym, Str::Symbol, '"'],
130+
[:backtick, Str::Backtick, '`']
131+
].each do |name, tok, fin|
96132
state :"simple_#{name}" do
97133
mixin :string_intp_escaped
98134
rule %r/[^\\#{fin}#]+/m, tok
@@ -101,39 +137,6 @@ def self.detect?(text)
101137
end
102138
end
103139

104-
keywords = %w(
105-
BEGIN END alias begin break case defined\? do else elsif end
106-
ensure for if ifdef in next redo rescue raise retry return super then
107-
undef unless until when while yield lib fun type of as
108-
)
109-
110-
keywords_pseudo = %w(
111-
initialize new loop include extend raise attr_reader attr_writer
112-
attr_accessor alias_method attr catch throw private module_function
113-
public protected true false nil __FILE__ __LINE__
114-
getter getter? getter! property property? property! struct record
115-
)
116-
117-
builtins_g = %w(
118-
abort ancestors at_exit
119-
caller catch chomp chop clone constants
120-
display dup eval exec exit extend fail fork format freeze
121-
getc gets global_variables gsub hash id included_modules
122-
inspect instance_eval instance_method instance_methods
123-
lambda loop method method_missing
124-
methods module_eval name object_id open p print printf
125-
proc putc puts raise rand
126-
readline readlines require scan select self send
127-
sleep split sprintf srand sub syscall system
128-
test throw to_a to_s trap warn
129-
)
130-
131-
builtins_q = %w(
132-
eql equal include is_a iterator kind_of nil
133-
)
134-
135-
builtins_b = %w(chomp chop exit gsub sub)
136-
137140
start do
138141
push :expr_start
139142
@heredoc_queue = []
@@ -175,9 +178,6 @@ def self.detect?(text)
175178

176179
mixin :strings
177180

178-
rule %r/(?:#{keywords.join('|')})\b/, Keyword, :expr_start
179-
rule %r/(?:#{keywords_pseudo.join('|')})\b/, Keyword::Pseudo, :expr_start
180-
181181
rule %r(
182182
(module)
183183
(\s+)
@@ -196,25 +196,33 @@ def self.detect?(text)
196196
push :classname
197197
end
198198

199-
rule %r/(?:#{builtins_q.join('|')})[?]/, Name::Builtin, :expr_start
200-
rule %r/(?:#{builtins_b.join('|')})!/, Name::Builtin, :expr_start
201-
rule %r/(?<!\.)(?:#{builtins_g.join('|')})\b/,
202-
Name::Builtin, :method_call
203-
204199
mixin :has_heredocs
205200

206201
# `..` and `...` for ranges must have higher priority than `.`
207202
# Otherwise, they will be parsed as :method_call
208-
rule %r/\.{2,3}/, Operator, :expr_start
203+
rule %r/[.]{2,3}/, Operator, :expr_start
209204

210205
rule %r/[\p{Lu}][\p{L}0-9_]*/, Name::Constant, :method_call
211-
rule %r/(\.|::)(\s*)([\p{Ll}_]\p{Word}*[!?]?|[*%&^`~+-\/\[<>=])/ do
212-
groups Punctuation, Text, Name::Function
213-
push :method_call
206+
keywords %r/(\.|::)(\s*)([\p{Ll}_]\p{Word}*[!?]?|[*%&^`~+-\/\[<>=])/ do
207+
group 3
208+
rule BUILTIN_METHODS do
209+
groups Punctuation, Text, Name::Builtin
210+
push :method_call
211+
end
212+
213+
default do
214+
groups Punctuation, Text, Name::Function
215+
push :method_call
216+
end
217+
end
218+
219+
keywords %r/[\p{L}_]\p{Word}*[?!]?/ do
220+
rule KEYWORDS, Keyword, :expr_start
221+
rule KEYWORDS_PSEUDO, Keyword::Pseudo, :expr_start
222+
rule BUILTIN_GLOBALS, Name::Builtin, :method_call
223+
default Name, :expr_start
214224
end
215225

216-
rule %r/[\p{L}_]\p{Word}*[?!]/, Name, :expr_start
217-
rule %r/[\p{L}_]\p{Word}*/, Name, :method_call
218226
rule %r/\*\*|\/\/|>=|<=|<=>|<<?|>>?|=~|={3}|!~|&&?|\|\||\./,
219227
Operator, :expr_start
220228
rule %r/{%|%}/, Punctuation

0 commit comments

Comments
 (0)