@@ -177,12 +177,23 @@ class Lexer
177
177
WORDS_SEP : :tSPACE
178
178
}
179
179
180
- private_constant :TYPES
180
+ # These constants represent flags in our lex state. We really, really
181
+ # don't want to be using them and we really, really don't want to be
182
+ # exposing them as part of our public API. Unfortunately, we don't have
183
+ # another way of matching the exact tokens that the parser gem expects
184
+ # without them. We should find another way to do this, but in the
185
+ # meantime we'll hide them from the documentation and mark them as
186
+ # private constants.
187
+ EXPR_BEG = 0x1 # :nodoc:
188
+ EXPR_LABEL = 0x400 # :nodoc:
189
+
190
+ private_constant :TYPES , :EXPR_BEG , :EXPR_LABEL
181
191
182
192
# The Parser::Source::Buffer that the tokens were lexed from.
183
193
attr_reader :source_buffer
184
194
185
- # An array of prism tokens that we lexed.
195
+ # An array of tuples that contain prism tokens and their associated lex
196
+ # state when they were lexed.
186
197
attr_reader :lexed
187
198
188
199
# A hash that maps offsets in bytes to offsets in characters.
@@ -205,9 +216,9 @@ def to_a
205
216
index = 0
206
217
207
218
while index < lexed . length
208
- token , = lexed [ index ]
219
+ token , state = lexed [ index ]
209
220
index += 1
210
- next if token . type == : IGNORED_NEWLINE || token . type == :EOF
221
+ next if %i[ IGNORED_NEWLINE __END__ EOF ] . include? ( token . type )
211
222
212
223
type = TYPES . fetch ( token . type )
213
224
value = token . value
@@ -218,13 +229,13 @@ def to_a
218
229
value . delete_prefix! ( "?" )
219
230
when :tCOMMENT
220
231
if token . type == :EMBDOC_BEGIN
221
- until ( next_token = lexed [ index ] ) && next_token . type == :EMBDOC_END
232
+ until ( next_token = lexed [ index ] [ 0 ] ) && next_token . type == :EMBDOC_END
222
233
value += next_token . value
223
234
index += 1
224
235
end
225
236
226
237
value += next_token . value
227
- location = Range . new ( source_buffer , offset_cache [ token . location . start_offset ] , offset_cache [ lexed [ index ] . location . end_offset ] )
238
+ location = Range . new ( source_buffer , offset_cache [ token . location . start_offset ] , offset_cache [ lexed [ index ] [ 0 ] . location . end_offset ] )
228
239
index += 1
229
240
else
230
241
value . chomp!
@@ -247,6 +258,8 @@ def to_a
247
258
value . chomp! ( ":" )
248
259
when :tLABEL_END
249
260
value . chomp! ( ":" )
261
+ when :tLCURLY
262
+ type = :tLBRACE if state == EXPR_BEG | EXPR_LABEL
250
263
when :tNTH_REF
251
264
value = Integer ( value . delete_prefix ( "$" ) )
252
265
when :tOP_ASGN
@@ -256,13 +269,13 @@ def to_a
256
269
when :tSPACE
257
270
value = nil
258
271
when :tSTRING_BEG
259
- if [ "\" " , "'" ] . include? ( value ) && ( next_token = lexed [ index ] ) && next_token . type == :STRING_END
272
+ if [ "\" " , "'" ] . include? ( value ) && ( next_token = lexed [ index ] [ 0 ] ) && next_token . type == :STRING_END
260
273
next_location = token . location . join ( next_token . location )
261
274
type = :tSTRING
262
275
value = ""
263
276
location = Range . new ( source_buffer , offset_cache [ next_location . start_offset ] , offset_cache [ next_location . end_offset ] )
264
277
index += 1
265
- elsif [ "\" " , "'" ] . include? ( value ) && ( next_token = lexed [ index ] ) && next_token . type == :STRING_CONTENT && ( next_next_token = lexed [ index + 1 ] ) && next_next_token . type == :STRING_END
278
+ elsif [ "\" " , "'" ] . include? ( value ) && ( next_token = lexed [ index ] [ 0 ] ) && next_token . type == :STRING_CONTENT && ( next_next_token = lexed [ index + 1 ] [ 0 ] ) && next_next_token . type == :STRING_END
266
279
next_location = token . location . join ( next_next_token . location )
267
280
type = :tSTRING
268
281
value = next_token . value
@@ -280,7 +293,7 @@ def to_a
280
293
location = Range . new ( source_buffer , offset_cache [ token . location . start_offset ] , offset_cache [ token . location . start_offset + 1 ] )
281
294
end
282
295
when :tSYMBEG
283
- if ( next_token = lexed [ index ] ) && next_token . type != :STRING_CONTENT && next_token . type != :EMBEXPR_BEGIN && next_token . type != :EMBVAR
296
+ if ( next_token = lexed [ index ] [ 0 ] ) && next_token . type != :STRING_CONTENT && next_token . type != :EMBEXPR_BEGIN && next_token . type != :EMBVAR
284
297
next_location = token . location . join ( next_token . location )
285
298
type = :tSYMBOL
286
299
value = next_token . value
0 commit comments