@@ -57,13 +57,11 @@ def setup
57
57
def scan_tokens ( encoder , options )
58
58
@encoder = encoder
59
59
@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
67
65
if match = scan ( /\- \- \[ \= *\[ / ) #--[[ long (possibly multiline) comment ]]
68
66
@num_equals = match . count ( "=" ) # Number must match for comment end
69
67
@encoder . begin_group ( :comment )
@@ -146,9 +144,8 @@ def handle_state_initial
146
144
# (tables can contain full expressions in parts).
147
145
# If this is the case, return to :table scanning state.
148
146
@state = :table if @state == :initial && @brace_depth >= 1
149
- end
150
-
151
- def handle_state_function_expected
147
+
148
+ when :function_expected
152
149
if match = scan ( /\( .*?\) /m ) # x = function() # "Anonymous" function without explicit name
153
150
@encoder . text_token ( match , :operator )
154
151
@state = :initial
@@ -163,9 +160,8 @@ def handle_state_function_expected
163
160
@encoder . text_token ( getch , :error )
164
161
@state = :initial
165
162
end
166
- end
167
163
168
- def handle_state_goto_label_expected
164
+ when :goto_label_expected
169
165
if match = scan ( /[a-zA-Z_][a-zA-Z0-9_]*/ )
170
166
@encoder . text_token ( match , :label )
171
167
@state = :initial
@@ -174,9 +170,8 @@ def handle_state_goto_label_expected
174
170
else
175
171
@encoder . text_token ( getch , :error )
176
172
end
177
- end
178
-
179
- def handle_state_local_var_expected
173
+
174
+ when :local_var_expected
180
175
if match = scan ( /function/ ) # local function ...
181
176
@encoder . text_token ( match , :keyword )
182
177
@state = :function_expected
@@ -198,9 +193,8 @@ def handle_state_local_var_expected
198
193
else
199
194
@encoder . text_token ( getch , :error )
200
195
end
201
- end
202
-
203
- def handle_state_long_comment
196
+
197
+ when :long_comment
204
198
if match = scan ( /.*?(?=\] ={#@num_equals }\] )/m )
205
199
@encoder . text_token ( match , :content )
206
200
@@ -212,9 +206,8 @@ def handle_state_long_comment
212
206
end
213
207
@encoder . end_group ( :comment )
214
208
@state = :initial
215
- end
216
-
217
- def handle_state_long_string
209
+
210
+ when :long_string
218
211
if match = scan ( /.*?(?=\] ={#@num_equals }\] )/m ) # Long strings do not interpret any escape sequences
219
212
@encoder . text_token ( match , :content )
220
213
@@ -226,9 +219,8 @@ def handle_state_long_string
226
219
end
227
220
@encoder . end_group ( :string )
228
221
@state = :initial
229
- end
230
-
231
- def handle_state_string
222
+
223
+ when :string
232
224
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)
233
225
@encoder . text_token ( match , :content )
234
226
elsif match = scan ( /\\ (?:['"abfnrtv\\ ]|z\s *|x\h \h |\d {1,3}|\n )/m )
@@ -244,9 +236,8 @@ def handle_state_string
244
236
else
245
237
@encoder . text_token ( getch , :error )
246
238
end
247
- end
248
-
249
- def handle_state_table
239
+
240
+ when :table
250
241
if match = scan ( /[,;]/ )
251
242
@encoder . text_token ( match , :operator )
252
243
elsif match = scan ( /[a-zA-Z_][a-zA-Z0-9_]* (?=\s *=)/x )
@@ -262,6 +253,13 @@ def handle_state_table
262
253
# advances the pointer).
263
254
@state = :initial
264
255
end
256
+ else
257
+ raise
258
+ end
259
+
260
+ end
261
+
262
+ @encoder
265
263
end
266
264
267
265
end
0 commit comments