Skip to content

Commit c23e73b

Browse files
committed
Merge remote-tracking branch 'origin/lua-scanner-faster' into lua-scanner
2 parents 39517be + 8979cc6 commit c23e73b

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

lib/coderay/scanners/lua.rb

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ def setup
5757
def scan_tokens(encoder, options)
5858
@encoder = encoder
5959
@options = options
60-
61-
send(:"handle_state_#@state") until eos?
62-
63-
@encoder
64-
end
65-
66-
def handle_state_initial
60+
61+
until eos?
62+
case state
63+
64+
when :initial
6765
if match = scan(/\-\-\[\=*\[/) #--[[ long (possibly multiline) comment ]]
6866
@num_equals = match.count("=") # Number must match for comment end
6967
@encoder.begin_group(:comment)
@@ -146,9 +144,8 @@ def handle_state_initial
146144
# (tables can contain full expressions in parts).
147145
# If this is the case, return to :table scanning state.
148146
@state = :table if @state == :initial && @brace_depth >= 1
149-
end
150-
151-
def handle_state_function_expected
147+
148+
when :function_expected
152149
if match = scan(/\(.*?\)/m) # x = function() # "Anonymous" function without explicit name
153150
@encoder.text_token(match, :operator)
154151
@state = :initial
@@ -163,9 +160,8 @@ def handle_state_function_expected
163160
@encoder.text_token(getch, :error)
164161
@state = :initial
165162
end
166-
end
167163

168-
def handle_state_goto_label_expected
164+
when :goto_label_expected
169165
if match = scan(/[a-zA-Z_][a-zA-Z0-9_]*/)
170166
@encoder.text_token(match, :label)
171167
@state = :initial
@@ -174,9 +170,8 @@ def handle_state_goto_label_expected
174170
else
175171
@encoder.text_token(getch, :error)
176172
end
177-
end
178-
179-
def handle_state_local_var_expected
173+
174+
when :local_var_expected
180175
if match = scan(/function/) # local function ...
181176
@encoder.text_token(match, :keyword)
182177
@state = :function_expected
@@ -198,9 +193,8 @@ def handle_state_local_var_expected
198193
else
199194
@encoder.text_token(getch, :error)
200195
end
201-
end
202-
203-
def handle_state_long_comment
196+
197+
when :long_comment
204198
if match = scan(/.*?(?=\]={#@num_equals}\])/m)
205199
@encoder.text_token(match, :content)
206200

@@ -212,9 +206,8 @@ def handle_state_long_comment
212206
end
213207
@encoder.end_group(:comment)
214208
@state = :initial
215-
end
216-
217-
def handle_state_long_string
209+
210+
when :long_string
218211
if match = scan(/.*?(?=\]={#@num_equals}\])/m) # Long strings do not interpret any escape sequences
219212
@encoder.text_token(match, :content)
220213

@@ -226,9 +219,8 @@ def handle_state_long_string
226219
end
227220
@encoder.end_group(:string)
228221
@state = :initial
229-
end
230-
231-
def handle_state_string
222+
223+
when :string
232224
if match = scan(/[^\\#@start_delim\n]+/) # Everything except \ and the start delimiter character is string content (newlines are only allowed if preceeded by \ or \z)
233225
@encoder.text_token(match, :content)
234226
elsif match = scan(/\\(?:['"abfnrtv\\]|z\s*|x\h\h|\d{1,3}|\n)/m)
@@ -244,9 +236,8 @@ def handle_state_string
244236
else
245237
@encoder.text_token(getch, :error)
246238
end
247-
end
248-
249-
def handle_state_table
239+
240+
when :table
250241
if match = scan(/[,;]/)
251242
@encoder.text_token(match, :operator)
252243
elsif match = scan(/[a-zA-Z_][a-zA-Z0-9_]* (?=\s*=)/x)
@@ -262,6 +253,13 @@ def handle_state_table
262253
# advances the pointer).
263254
@state = :initial
264255
end
256+
else
257+
raise
258+
end
259+
260+
end
261+
262+
@encoder
265263
end
266264

267265
end

0 commit comments

Comments
 (0)