ruby_parser-legacy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ruby_parser-legacy might be problematic. Click here for more details.

@@ -0,0 +1,8 @@
1
+ require "minitest/autorun"
2
+ require "ruby_parser/legacy"
3
+
4
+ class TestLegacy < Minitest::Test
5
+ def test_sanity
6
+ skip "write tests or I will kneecap you"
7
+ end
8
+ end
@@ -0,0 +1,2984 @@
1
+ # encoding: US-ASCII
2
+
3
+ require "minitest/autorun"
4
+ require "ruby_lexer"
5
+ require "ruby_parser"
6
+
7
+ class TestRubyLexer < Minitest::Test
8
+ attr_accessor :processor, :lex, :parser_class, :lex_state
9
+
10
+ alias :lexer :lex # lets me copy/paste code from parser
11
+ alias :lexer= :lex=
12
+
13
+ def setup
14
+ self.lex_state = :expr_beg
15
+ setup_lexer_class RubyParser::V19
16
+ end
17
+
18
+ def setup_lexer input, exp_sexp = nil
19
+ setup_new_parser
20
+ lex.ss = RPStringScanner.new(input)
21
+ lex.lex_state = self.lex_state
22
+ end
23
+
24
+ def setup_new_parser
25
+ self.processor = parser_class.new
26
+ self.lex = processor.lexer
27
+ end
28
+
29
+ def setup_lexer_class parser_class
30
+ self.parser_class = parser_class
31
+ setup_new_parser
32
+ setup_lexer "blah blah"
33
+ end
34
+
35
+ def assert_lex input, exp_sexp, *args, &b
36
+ setup_lexer input
37
+ assert_parse input, exp_sexp if exp_sexp
38
+
39
+ b.call if b
40
+
41
+ args.each_slice(5) do |token, value, state, paren, brace|
42
+ assert_next_lexeme token, value, state, paren, brace
43
+ end
44
+
45
+ refute_lexeme
46
+ end
47
+
48
+ def assert_lex3 input, exp_sexp, *args, &block
49
+ args = args.each_slice(3).map { |a, b, c| [a, b, c, nil, nil] }.flatten
50
+
51
+ assert_lex(input, exp_sexp, *args, &block)
52
+ end
53
+
54
+ def ruby18
55
+ RubyParser::V18 === lexer.parser
56
+ end
57
+
58
+ def refute_lex input, *args # TODO: re-sort
59
+ args = args.each_slice(2).map { |a, b| [a, b, nil, nil, nil] }.flatten
60
+
61
+ assert_raises RubyParser::SyntaxError do
62
+ assert_lex(input, nil, *args)
63
+ end
64
+ end
65
+
66
+ def assert_lex_fname name, type, end_state = :expr_arg # TODO: swap name/type
67
+ assert_lex3("def #{name} ",
68
+ nil,
69
+
70
+ :kDEF, "def", :expr_fname,
71
+ type, name, end_state)
72
+ end
73
+
74
+ def assert_next_lexeme token=nil, value=nil, state=nil, paren=nil, brace=nil
75
+ adv = @lex.next_token
76
+
77
+ assert adv, "no more tokens"
78
+
79
+ act_token, act_value = adv
80
+
81
+ msg = message {
82
+ act = [act_token, act_value, @lex.lex_state,
83
+ @lex.paren_nest, @lex.brace_nest]
84
+ exp = [token, value, state, paren, brace]
85
+ "#{exp.inspect} vs #{act.inspect}"
86
+ }
87
+
88
+ act_value = act_value.first if Array === act_value
89
+
90
+ assert_equal token, act_token, msg
91
+ case value
92
+ when Float then
93
+ assert_in_epsilon value, act_value, 0.001, msg
94
+ when NilClass then
95
+ assert_nil act_value, msg
96
+ else
97
+ assert_equal value, act_value, msg
98
+ end
99
+ assert_equal state, @lex.lex_state, msg if state
100
+ assert_equal paren, @lex.paren_nest, msg if paren
101
+ assert_equal brace, @lex.brace_nest, msg if brace
102
+ end
103
+
104
+ def assert_parse input, exp_sexp
105
+ assert_equal exp_sexp, processor.class.new.parse(input)
106
+ end
107
+
108
+ def assert_read_escape expected, input
109
+ @lex.ss.string = input
110
+ assert_equal expected, @lex.read_escape, input
111
+ end
112
+
113
+ def assert_read_escape_bad input # TODO: rename refute_read_escape
114
+ @lex.ss.string = input
115
+ assert_raises RubyParser::SyntaxError do
116
+ @lex.read_escape
117
+ end
118
+ end
119
+
120
+ def refute_lexeme
121
+ x = y = @lex.next_token
122
+
123
+ refute x, "not empty: #{y.inspect}"
124
+ end
125
+
126
+ ## Utility Methods:
127
+
128
+ def emulate_string_interpolation
129
+ lex_strterm = lexer.lex_strterm
130
+ string_nest = lexer.string_nest
131
+ brace_nest = lexer.brace_nest
132
+
133
+ lexer.string_nest = 0
134
+ lexer.brace_nest = 0
135
+ lexer.cond.push false
136
+ lexer.cmdarg.push false
137
+
138
+ lexer.lex_strterm = nil
139
+ lexer.lex_state = :expr_beg
140
+
141
+ yield
142
+
143
+ lexer.lex_state = :expr_endarg
144
+ assert_next_lexeme :tRCURLY, "}", :expr_endarg, 0 # 1.9 specific
145
+
146
+ lexer.lex_strterm = lex_strterm
147
+ lexer.lex_state = :expr_beg
148
+ lexer.string_nest = string_nest
149
+ lexer.brace_nest = brace_nest
150
+
151
+ lexer.cond.lexpop
152
+ lexer.cmdarg.lexpop
153
+ end
154
+
155
+ ## Tests:
156
+
157
+ def test_next_token
158
+ assert_equal [:tIDENTIFIER, "blah"], @lex.next_token
159
+ assert_equal [:tIDENTIFIER, "blah"], @lex.next_token
160
+ assert_nil @lex.next_token
161
+ end
162
+
163
+ def test_unicode_ident
164
+ s = "@\u1088\u1077\u1093\u1072"
165
+ assert_lex3(s.dup, nil, :tIVAR, s.dup, :expr_end)
166
+ end
167
+
168
+ def test_read_escape
169
+ assert_read_escape "\\", "\\"
170
+ assert_read_escape "\n", "n"
171
+ assert_read_escape "\t", "t"
172
+ assert_read_escape "\r", "r"
173
+ assert_read_escape "\f", "f"
174
+ assert_read_escape "\13", "v"
175
+ assert_read_escape "\0", "0"
176
+ assert_read_escape "\07", "a"
177
+ assert_read_escape "\007", "a"
178
+ assert_read_escape "\033", "e"
179
+ assert_read_escape "\377", "377"
180
+ assert_read_escape "\377", "xff"
181
+ assert_read_escape "\010", "b"
182
+ assert_read_escape " ", "s"
183
+ assert_read_escape "q", "q" # plain vanilla escape
184
+
185
+ assert_read_escape "8", "8" # ugh... mri... WHY?!?
186
+ assert_read_escape "9", "9" # ugh... mri... WHY?!?
187
+
188
+ assert_read_escape "$", "444" # ugh
189
+ end
190
+
191
+ def test_read_escape_c
192
+ assert_read_escape "\030", "C-x"
193
+ assert_read_escape "\030", "cx"
194
+ assert_read_escape "\230", 'C-\M-x'
195
+ assert_read_escape "\230", 'c\M-x'
196
+
197
+ assert_read_escape "\177", "C-?"
198
+ assert_read_escape "\177", "c?"
199
+ end
200
+
201
+ def test_read_escape_errors
202
+ assert_read_escape_bad ""
203
+
204
+ assert_read_escape_bad "M"
205
+ assert_read_escape_bad "M-"
206
+ assert_read_escape_bad "Mx"
207
+
208
+ assert_read_escape_bad "Cx"
209
+ assert_read_escape_bad "C"
210
+ assert_read_escape_bad "C-"
211
+
212
+ assert_read_escape_bad "c"
213
+ end
214
+
215
+ def test_read_escape_m
216
+ assert_read_escape "\370", "M-x"
217
+ assert_read_escape "\230", 'M-\C-x'
218
+ assert_read_escape "\230", 'M-\cx'
219
+ end
220
+
221
+ def test_yylex_ambiguous_uminus
222
+ assert_lex3("m -3",
223
+ nil,
224
+ :tIDENTIFIER, "m", :expr_cmdarg,
225
+ :tUMINUS_NUM, "-", :expr_beg,
226
+ :tINTEGER, 3, :expr_end)
227
+
228
+ # TODO: verify warning
229
+ end
230
+
231
+ def test_yylex_ambiguous_uplus
232
+ assert_lex3("m +3",
233
+ nil,
234
+ :tIDENTIFIER, "m", :expr_cmdarg,
235
+ :tINTEGER, 3, :expr_end)
236
+
237
+ # TODO: verify warning
238
+ end
239
+
240
+ def test_yylex_and
241
+ assert_lex3("&", nil, :tAMPER, "&", :expr_beg)
242
+ end
243
+
244
+ def test_yylex_and2
245
+ assert_lex3("&&", nil, :tANDOP, "&&", :expr_beg)
246
+ end
247
+
248
+ def test_yylex_and2_equals
249
+ assert_lex3("&&=", nil, :tOP_ASGN, "&&", :expr_beg)
250
+ end
251
+
252
+ def test_yylex_and_dot
253
+ setup_lexer_class RubyParser::V23
254
+
255
+ assert_lex3("&.", nil, :tLONELY, "&.", :expr_dot)
256
+ end
257
+
258
+ def test_yylex_and_dot_call
259
+ setup_lexer_class RubyParser::V23
260
+
261
+ assert_lex3("x&.y", nil,
262
+ :tIDENTIFIER, "x", :expr_cmdarg,
263
+ :tLONELY, "&.", :expr_dot,
264
+ :tIDENTIFIER, "y")
265
+ end
266
+
267
+ def test_yylex_and_dot_call_newline
268
+ setup_lexer_class Ruby23Parser
269
+
270
+ assert_lex3("x\n&.y", nil,
271
+ :tIDENTIFIER, "x", :expr_cmdarg,
272
+ :tLONELY, "&.", :expr_dot,
273
+ :tIDENTIFIER, "y")
274
+ end
275
+
276
+ def test_yylex_and_arg
277
+ self.lex_state = :expr_arg
278
+
279
+ assert_lex3(" &y",
280
+ nil,
281
+ :tAMPER, "&", :expr_beg,
282
+ :tIDENTIFIER, "y", :expr_arg)
283
+ end
284
+
285
+ def test_yylex_and_equals
286
+ assert_lex3("&=", nil, :tOP_ASGN, "&", :expr_beg)
287
+ end
288
+
289
+ def test_yylex_and_expr
290
+ self.lex_state = :expr_arg
291
+
292
+ assert_lex3("x & y",
293
+ nil,
294
+ :tIDENTIFIER, "x", :expr_cmdarg,
295
+ :tAMPER2, "&", :expr_beg,
296
+ :tIDENTIFIER, "y", :expr_arg)
297
+ end
298
+
299
+ def test_yylex_and_meth
300
+ assert_lex_fname "&", :tAMPER2
301
+ end
302
+
303
+ def test_yylex_assoc
304
+ assert_lex3("=>", nil, :tASSOC, "=>", :expr_beg)
305
+ end
306
+
307
+ def test_yylex_label__18
308
+ setup_lexer_class RubyParser::V18
309
+
310
+ assert_lex3("{a:",
311
+ nil,
312
+ :tLBRACE, "{", :expr_beg,
313
+ :tIDENTIFIER, "a", :expr_arg,
314
+ :tSYMBEG, ":", :expr_fname)
315
+ end
316
+
317
+ def test_yylex_label_in_params__18
318
+ setup_lexer_class RubyParser::V18
319
+
320
+ assert_lex3("foo(a:",
321
+ nil,
322
+ :tIDENTIFIER, "foo", :expr_cmdarg,
323
+ :tLPAREN2, "(", :expr_beg,
324
+ :tIDENTIFIER, "a", :expr_cmdarg,
325
+ :tSYMBEG, ":", :expr_fname)
326
+ end
327
+
328
+ def test_yylex_label__19
329
+ setup_lexer_class RubyParser::V19
330
+
331
+ assert_lex3("{a:",
332
+ nil,
333
+ :tLBRACE, "{", :expr_beg,
334
+ :tLABEL, "a", :expr_labeled)
335
+ end
336
+
337
+ def test_yylex_label_in_params__19
338
+ setup_lexer_class RubyParser::V19
339
+
340
+ assert_lex3("foo(a:",
341
+ nil,
342
+ :tIDENTIFIER, "foo", :expr_cmdarg,
343
+ :tLPAREN2, "(", :expr_beg,
344
+ :tLABEL, "a", :expr_labeled)
345
+ end
346
+
347
+ def test_yylex_paren_string_parens_interpolated
348
+ setup_lexer('%((#{b}#{d}))',
349
+ s(:dstr,
350
+ "(",
351
+ s(:evstr, s(:call, nil, :b)),
352
+ s(:evstr, s(:call, nil, :d)),
353
+ s(:str, ")")))
354
+
355
+ assert_next_lexeme :tSTRING_BEG, "%)", :expr_beg, 0, 0
356
+ assert_next_lexeme :tSTRING_CONTENT, "(", :expr_beg, 0, 0
357
+ assert_next_lexeme :tSTRING_DBEG, nil, :expr_beg, 0, 0
358
+
359
+ emulate_string_interpolation do
360
+ assert_next_lexeme :tIDENTIFIER, "b", :expr_cmdarg, 0, 0
361
+ end
362
+
363
+ assert_next_lexeme :tSTRING_DBEG, nil, :expr_beg, 0, 0
364
+
365
+ emulate_string_interpolation do
366
+ assert_next_lexeme :tIDENTIFIER, "d", :expr_cmdarg, 0, 0
367
+ end
368
+
369
+ assert_next_lexeme :tSTRING_CONTENT, ")", :expr_beg, 0, 0
370
+ assert_next_lexeme :tSTRING_END, ")", :expr_end, 0, 0
371
+
372
+ refute_lexeme
373
+ end
374
+
375
+ def test_yylex_paren_string_interpolated_regexp
376
+ setup_lexer('%( #{(/abcd/)} )',
377
+ s(:dstr, " ", s(:evstr, s(:lit, /abcd/)), s(:str, " ")))
378
+
379
+ assert_next_lexeme :tSTRING_BEG, "%)", :expr_beg, 0, 0
380
+ assert_next_lexeme :tSTRING_CONTENT, " ", :expr_beg, 0, 0
381
+ assert_next_lexeme :tSTRING_DBEG, nil, :expr_beg, 0, 0
382
+
383
+ emulate_string_interpolation do
384
+ assert_next_lexeme :tLPAREN, "(", :expr_beg, 1, 0
385
+ assert_next_lexeme :tREGEXP_BEG, "/", :expr_beg, 1, 0
386
+ assert_next_lexeme :tSTRING_CONTENT, "abcd", :expr_beg, 1, 0
387
+ assert_next_lexeme :tREGEXP_END, "", :expr_end, 1, 0
388
+ assert_next_lexeme :tRPAREN, ")", :expr_endfn, 0, 0
389
+ end
390
+
391
+ assert_next_lexeme :tSTRING_CONTENT, " ", :expr_beg, 0, 0
392
+ assert_next_lexeme :tSTRING_END, ")", :expr_end, 0, 0
393
+
394
+ refute_lexeme
395
+ end
396
+
397
+ def test_yylex_not_at_defn__20
398
+ setup_lexer_class RubyParser::V20
399
+
400
+ assert_lex("def +@; end",
401
+ s(:defn, :+@, s(:args), s(:nil)),
402
+
403
+ :kDEF, "def", :expr_fname, 0, 0,
404
+ :tUPLUS, "+@", :expr_arg, 0, 0,
405
+ :tSEMI, ";", :expr_beg, 0, 0,
406
+ :kEND, "end", :expr_end, 0, 0)
407
+
408
+ assert_lex("def !@; end",
409
+ s(:defn, :"!@", s(:args), s(:nil)),
410
+
411
+ :kDEF, "def", :expr_fname, 0, 0,
412
+ :tUBANG, "!@", :expr_arg, 0, 0,
413
+ :tSEMI, ";", :expr_beg, 0, 0,
414
+ :kEND, "end", :expr_end, 0, 0)
415
+ end
416
+
417
+ def test_yylex_not_at_ivar
418
+ assert_lex("!@ivar",
419
+ s(:call, s(:ivar, :@ivar), :"!"),
420
+
421
+ :tBANG, "!", :expr_beg, 0, 0,
422
+ :tIVAR, "@ivar", :expr_end, 0, 0)
423
+ end
424
+
425
+ def test_yylex_number_times_ident_times_return_number
426
+ assert_lex("1 * b * 3",
427
+ s(:call,
428
+ s(:call, s(:lit, 1), :*, s(:call, nil, :b)),
429
+ :*, s(:lit, 3)),
430
+
431
+ :tINTEGER, 1, :expr_end, 0, 0,
432
+ :tSTAR2, "*", :expr_beg, 0, 0,
433
+ :tIDENTIFIER, "b", :expr_arg, 0, 0,
434
+ :tSTAR2, "*", :expr_beg, 0, 0,
435
+ :tINTEGER, 3, :expr_end, 0, 0)
436
+
437
+ assert_lex("1 * b *\n 3",
438
+ s(:call,
439
+ s(:call, s(:lit, 1), :*, s(:call, nil, :b)),
440
+ :*, s(:lit, 3)),
441
+
442
+ :tINTEGER, 1, :expr_end, 0, 0,
443
+ :tSTAR2, "*", :expr_beg, 0, 0,
444
+ :tIDENTIFIER, "b", :expr_arg, 0, 0,
445
+ :tSTAR2, "*", :expr_beg, 0, 0,
446
+ :tINTEGER, 3, :expr_end, 0, 0)
447
+ end
448
+
449
+ def test_yylex_paren_string_parens_interpolated_regexp
450
+ setup_lexer('%((#{(/abcd/)}))',
451
+ s(:dstr, "(", s(:evstr, s(:lit, /abcd/)), s(:str, ")")))
452
+
453
+ assert_next_lexeme :tSTRING_BEG, "%)", :expr_beg, 0, 0
454
+ assert_next_lexeme :tSTRING_CONTENT, "(", :expr_beg, 0, 0
455
+
456
+ assert_next_lexeme :tSTRING_DBEG, nil, :expr_beg, 0, 0
457
+
458
+ emulate_string_interpolation do
459
+ assert_next_lexeme :tLPAREN, "(", :expr_beg, 1, 0
460
+ assert_next_lexeme :tREGEXP_BEG, "/", :expr_beg, 1, 0
461
+ assert_next_lexeme :tSTRING_CONTENT, "abcd", :expr_beg, 1, 0
462
+ assert_next_lexeme :tREGEXP_END, "", :expr_end, 1, 0
463
+ assert_next_lexeme :tRPAREN, ")", :expr_endfn, 0, 0
464
+ end
465
+
466
+ assert_next_lexeme :tSTRING_CONTENT, ")", :expr_beg, 0, 0
467
+ assert_next_lexeme :tSTRING_END, ")", :expr_end, 0, 0
468
+
469
+ refute_lexeme
470
+ end
471
+
472
+ def test_yylex_method_parens_chevron
473
+ assert_lex("a()<<1",
474
+ s(:call, s(:call, nil, :a), :<<, s(:lit, 1)),
475
+ :tIDENTIFIER, "a", :expr_cmdarg, 0, 0,
476
+ :tLPAREN2, "(", :expr_beg, 1, 0,
477
+ :tRPAREN, ")", :expr_endfn, 0, 0,
478
+ :tLSHFT, "<<" , :expr_beg, 0, 0,
479
+ :tINTEGER, 1, :expr_end, 0, 0)
480
+ end
481
+
482
+ def test_yylex_lambda_args__20
483
+ setup_lexer_class RubyParser::V20
484
+
485
+ assert_lex("-> (a) { }",
486
+ s(:iter, s(:call, nil, :lambda),
487
+ s(:args, :a)),
488
+
489
+ :tLAMBDA, nil, :expr_endfn, 0, 0,
490
+ :tLPAREN2, "(", :expr_beg, 1, 0,
491
+ :tIDENTIFIER, "a", :expr_arg, 1, 0,
492
+ :tRPAREN, ")", :expr_endfn, 0, 0,
493
+ :tLCURLY, "{", :expr_beg, 0, 1,
494
+ :tRCURLY, "}", :expr_endarg, 0, 0)
495
+ end
496
+
497
+ def test_yylex_lambda_as_args_with_block__20
498
+ setup_lexer_class RubyParser::V20
499
+
500
+ assert_lex3("a -> do end do end",
501
+ nil,
502
+ :tIDENTIFIER, "a", :expr_cmdarg,
503
+ :tLAMBDA, nil, :expr_endfn,
504
+ :kDO, "do", :expr_beg,
505
+ :kEND, "end", :expr_end,
506
+ :kDO, "do", :expr_beg,
507
+ :kEND, "end", :expr_end)
508
+ end
509
+
510
+ def test_yylex_lambda_args_opt__20
511
+ setup_lexer_class RubyParser::V20
512
+
513
+ assert_lex("-> (a=nil) { }",
514
+ s(:iter, s(:call, nil, :lambda),
515
+ s(:args, s(:lasgn, :a, s(:nil)))),
516
+
517
+ :tLAMBDA, nil, :expr_endfn, 0, 0,
518
+ :tLPAREN2, "(", :expr_beg, 1, 0,
519
+ :tIDENTIFIER, "a", :expr_arg, 1, 0,
520
+ :tEQL, "=", :expr_beg, 1, 0,
521
+ :kNIL, "nil", :expr_end, 1, 0,
522
+ :tRPAREN, ")", :expr_endfn, 0, 0,
523
+ :tLCURLY, "{", :expr_beg, 0, 1,
524
+ :tRCURLY, "}", :expr_endarg, 0, 0)
525
+ end
526
+
527
+ def test_yylex_lambda_hash__20
528
+ setup_lexer_class RubyParser::V20
529
+
530
+ assert_lex("-> (a={}) { }",
531
+ s(:iter, s(:call, nil, :lambda),
532
+ s(:args, s(:lasgn, :a, s(:hash)))),
533
+
534
+ :tLAMBDA, nil, :expr_endfn, 0, 0,
535
+ :tLPAREN2, "(", :expr_beg, 1, 0,
536
+ :tIDENTIFIER, "a", :expr_arg, 1, 0,
537
+ :tEQL, "=", :expr_beg, 1, 0,
538
+ :tLBRACE, "{", :expr_beg, 1, 1,
539
+ :tRCURLY, "}", :expr_endarg, 1, 0,
540
+ :tRPAREN, ")", :expr_endfn, 0, 0,
541
+ :tLCURLY, "{", :expr_beg, 0, 1,
542
+ :tRCURLY, "}", :expr_endarg, 0, 0)
543
+ end
544
+
545
+ def test_yylex_iter_array_curly
546
+ assert_lex("f :a, [:b] { |c, d| }", # yes, this is bad code
547
+ s(:iter,
548
+ s(:call, nil, :f, s(:lit, :a), s(:array, s(:lit, :b))),
549
+ s(:args, :c, :d)),
550
+
551
+ :tIDENTIFIER, "f", :expr_cmdarg, 0, 0,
552
+ :tSYMBOL, "a", :expr_end, 0, 0,
553
+ :tCOMMA, ",", :expr_beg, 0, 0,
554
+ :tLBRACK, "[", :expr_beg, 1, 0,
555
+ :tSYMBOL, "b", :expr_end, 1, 0,
556
+ :tRBRACK, "]", :expr_endarg, 0, 0,
557
+ :tLBRACE_ARG, "{", :expr_beg, 0, 1,
558
+ :tPIPE, "|", :expr_beg, 0, 1,
559
+ :tIDENTIFIER, "c", :expr_arg, 0, 1,
560
+ :tCOMMA, ",", :expr_beg, 0, 1,
561
+ :tIDENTIFIER, "d", :expr_arg, 0, 1,
562
+ :tPIPE, "|", :expr_beg, 0, 1,
563
+ :tRCURLY, "}", :expr_endarg, 0, 0)
564
+ end
565
+
566
+ def test_yylex_const_call_same_name
567
+ assert_lex("X = a { }; b { f :c }",
568
+ s(:block,
569
+ s(:cdecl, :X, s(:iter, s(:call, nil, :a), 0)),
570
+ s(:iter,
571
+ s(:call, nil, :b),
572
+ 0,
573
+ s(:call, nil, :f, s(:lit, :c)))),
574
+
575
+ :tCONSTANT, "X", :expr_cmdarg, 0, 0,
576
+ :tEQL, "=", :expr_beg, 0, 0,
577
+ :tIDENTIFIER, "a", :expr_arg, 0, 0,
578
+ :tLCURLY, "{", :expr_beg, 0, 1,
579
+ :tRCURLY, "}", :expr_endarg, 0, 0,
580
+ :tSEMI, ";", :expr_beg, 0, 0,
581
+
582
+ :tIDENTIFIER, "b", :expr_cmdarg, 0, 0,
583
+ :tLCURLY, "{", :expr_beg, 0, 1,
584
+ :tIDENTIFIER, "f", :expr_cmdarg, 0, 1, # different
585
+ :tSYMBOL, "c", :expr_end, 0, 1,
586
+ :tRCURLY, "}", :expr_endarg, 0, 0)
587
+
588
+ assert_lex("X = a { }; b { X :c }",
589
+ s(:block,
590
+ s(:cdecl, :X, s(:iter, s(:call, nil, :a), 0)),
591
+ s(:iter,
592
+ s(:call, nil, :b),
593
+ 0,
594
+ s(:call, nil, :X, s(:lit, :c)))),
595
+
596
+ :tCONSTANT, "X", :expr_cmdarg, 0, 0,
597
+ :tEQL, "=", :expr_beg, 0, 0,
598
+ :tIDENTIFIER, "a", :expr_arg, 0, 0,
599
+ :tLCURLY, "{", :expr_beg, 0, 1,
600
+ :tRCURLY, "}", :expr_endarg, 0, 0,
601
+ :tSEMI, ";", :expr_beg, 0, 0,
602
+
603
+ :tIDENTIFIER, "b", :expr_cmdarg, 0, 0,
604
+ :tLCURLY, "{", :expr_beg, 0, 1,
605
+ :tCONSTANT, "X", :expr_cmdarg, 0, 1, # same
606
+ :tSYMBOL, "c", :expr_end, 0, 1,
607
+ :tRCURLY, "}", :expr_endarg, 0, 0)
608
+ end
609
+
610
+ def test_yylex_lasgn_call_same_name
611
+ assert_lex("a = b.c :d => 1",
612
+ s(:lasgn, :a,
613
+ s(:call, s(:call, nil, :b), :c,
614
+ s(:hash, s(:lit, :d), s(:lit, 1)))),
615
+
616
+ :tIDENTIFIER, "a", :expr_cmdarg, 0, 0,
617
+ :tEQL, "=", :expr_beg, 0, 0,
618
+ :tIDENTIFIER, "b", :expr_arg, 0, 0,
619
+ :tDOT, ".", :expr_dot, 0, 0,
620
+ :tIDENTIFIER, "c", :expr_arg, 0, 0, # different
621
+ :tSYMBOL, "d", :expr_end, 0, 0,
622
+ :tASSOC, "=>", :expr_beg, 0, 0,
623
+ :tINTEGER, 1, :expr_end, 0, 0)
624
+
625
+ assert_lex("a = b.a :d => 1",
626
+ s(:lasgn, :a,
627
+ s(:call, s(:call, nil, :b), :a,
628
+ s(:hash, s(:lit, :d), s(:lit, 1)))),
629
+
630
+ :tIDENTIFIER, "a", :expr_cmdarg, 0, 0,
631
+ :tEQL, "=", :expr_beg, 0, 0,
632
+ :tIDENTIFIER, "b", :expr_arg, 0, 0,
633
+ :tDOT, ".", :expr_dot, 0, 0,
634
+ :tIDENTIFIER, "a", :expr_arg, 0, 0, # same as lvar
635
+ :tSYMBOL, "d", :expr_end, 0, 0,
636
+ :tASSOC, "=>", :expr_beg, 0, 0,
637
+ :tINTEGER, 1, :expr_end, 0, 0)
638
+ end
639
+
640
+ def test_yylex_back_ref
641
+ assert_lex3("[$&, $`, $', $+]",
642
+ nil,
643
+ :tLBRACK, "[", :expr_beg,
644
+ :tBACK_REF, :&, :expr_end, :tCOMMA, ",", :expr_beg,
645
+ :tBACK_REF, :"`", :expr_end, :tCOMMA, ",", :expr_beg,
646
+ :tBACK_REF, :"'", :expr_end, :tCOMMA, ",", :expr_beg,
647
+ :tBACK_REF, :+, :expr_end,
648
+ :tRBRACK, "]", :expr_endarg)
649
+ end
650
+
651
+ def test_yylex_backslash
652
+ assert_lex3("1 \\\n+ 2",
653
+ nil,
654
+ :tINTEGER, 1, :expr_end,
655
+ :tPLUS, "+", :expr_beg,
656
+ :tINTEGER, 2, :expr_end)
657
+ end
658
+
659
+ def test_yylex_backslash_bad
660
+ refute_lex("1 \\ + 2", :tINTEGER, 1)
661
+ end
662
+
663
+ def test_yylex_backtick
664
+ assert_lex3("`ls`",
665
+ nil,
666
+ :tXSTRING_BEG, "`", :expr_beg,
667
+ :tSTRING_CONTENT, "ls", :expr_beg,
668
+ :tSTRING_END, "`", :expr_end)
669
+ end
670
+
671
+ def test_yylex_backtick_cmdarg
672
+ self.lex_state = :expr_dot
673
+
674
+ # \n ensures expr_cmd (TODO: why?)
675
+ assert_lex3("\n`", nil, :tBACK_REF2, "`", :expr_cmdarg)
676
+ end
677
+
678
+ def test_yylex_backtick_dot
679
+ self.lex_state = :expr_dot
680
+
681
+ assert_lex3("a.`(3)",
682
+ nil,
683
+ :tIDENTIFIER, "a", :expr_cmdarg,
684
+ :tDOT, ".", :expr_dot,
685
+ :tBACK_REF2, "`", :expr_arg,
686
+ :tLPAREN2, "(", :expr_beg,
687
+ :tINTEGER, 3, :expr_end,
688
+ :tRPAREN, ")", :expr_endfn)
689
+ end
690
+
691
+ def test_yylex_backtick_method
692
+ self.lex_state = :expr_fname
693
+
694
+ assert_lex3("`",
695
+ nil,
696
+ :tBACK_REF2, "`", :expr_end)
697
+ end
698
+
699
+ def test_yylex_bad_char
700
+ refute_lex(" \010 ")
701
+ end
702
+
703
+ def test_yylex_bang
704
+ assert_lex3("!", nil, :tBANG, "!", :expr_beg)
705
+ end
706
+
707
+ def test_yylex_bang_equals
708
+ assert_lex3("!=", nil, :tNEQ, "!=", :expr_beg)
709
+ end
710
+
711
+ def test_yylex_bang_tilde
712
+ assert_lex3("!~", nil, :tNMATCH, "!~", :expr_beg)
713
+ end
714
+
715
+ def test_yylex_block_bug_1
716
+ assert_lex3("a do end",
717
+ s(:iter, s(:call, nil, :a), 0),
718
+
719
+ :tIDENTIFIER, "a", :expr_cmdarg,
720
+ :kDO, "do", :expr_beg,
721
+ :kEND, "end", :expr_end)
722
+ end
723
+
724
+ def test_yylex_block_bug_2
725
+ assert_lex3("a = 1\na do\nend",
726
+ s(:block,
727
+ s(:lasgn, :a, s(:lit, 1)),
728
+ s(:iter, s(:call, nil, :a), 0)),
729
+
730
+ :tIDENTIFIER, "a", :expr_cmdarg,
731
+ :tEQL, "=", :expr_beg,
732
+ :tINTEGER, 1, :expr_end,
733
+ :tNL, nil, :expr_beg,
734
+ :tIDENTIFIER, "a", :expr_cmdarg,
735
+ :kDO, "do", :expr_beg,
736
+ :kEND, "end", :expr_end)
737
+ end
738
+
739
+ def test_yylex_block_bug_3
740
+ assert_lex3("a { }",
741
+ s(:iter, s(:call, nil, :a), 0),
742
+
743
+ :tIDENTIFIER, "a", :expr_cmdarg, # verified
744
+ :tLCURLY, "{", :expr_beg, # TODO: expr_beg|expr_label
745
+ :tRCURLY, "}", :expr_endarg)
746
+ end
747
+
748
+ def test_yylex_carat
749
+ assert_lex3("^", nil, :tCARET, "^", :expr_beg)
750
+ end
751
+
752
+ def test_yylex_carat_equals
753
+ assert_lex3("^=", nil, :tOP_ASGN, "^", :expr_beg)
754
+ end
755
+
756
+ def test_yylex_colon2
757
+ assert_lex3("A::B",
758
+ nil,
759
+ :tCONSTANT, "A", :expr_cmdarg,
760
+ :tCOLON2, "::", :expr_dot,
761
+ :tCONSTANT, "B", :expr_arg)
762
+ end
763
+
764
+ def test_yylex_colon2_argh
765
+ assert_lex3("module X::Y\n c\nend",
766
+ nil,
767
+ :kMODULE, "module", :expr_value,
768
+ :tCONSTANT, "X", :expr_arg,
769
+ :tCOLON2, "::", :expr_dot,
770
+ :tCONSTANT, "Y", :expr_arg,
771
+ :tNL, nil, :expr_beg,
772
+ :tIDENTIFIER, "c", :expr_cmdarg,
773
+ :tNL, nil, :expr_beg,
774
+ :kEND, "end", :expr_end)
775
+ end
776
+
777
+ def test_yylex_colon3
778
+ assert_lex3("::Array",
779
+ nil,
780
+ :tCOLON3, "::", :expr_beg,
781
+ :tCONSTANT, "Array", :expr_arg)
782
+ end
783
+
784
+ def test_yylex_comma
785
+ assert_lex3(",", nil, :tCOMMA, ",", :expr_beg)
786
+ end
787
+
788
+ def test_yylex_comment
789
+ assert_lex3("1 # one\n# two\n2",
790
+ nil,
791
+ :tINTEGER, 1, :expr_end,
792
+ :tNL, nil, :expr_beg,
793
+ :tINTEGER, 2, :expr_end)
794
+
795
+ assert_equal "# one\n# two\n", @lex.comments
796
+ end
797
+
798
+ def test_yylex_comment_begin
799
+ assert_lex3("=begin\nblah\nblah\n=end\n42",
800
+ nil,
801
+ :tINTEGER, 42, :expr_end)
802
+
803
+ assert_equal "=begin\nblah\nblah\n=end\n", @lex.comments
804
+ end
805
+
806
+ def test_yylex_comment_begin_bad
807
+ refute_lex("=begin\nblah\nblah\n")
808
+
809
+ assert_equal "", @lex.comments
810
+ end
811
+
812
+ def test_yylex_comment_begin_not_comment
813
+ assert_lex3("beginfoo = 5\np x \\\n=beginfoo",
814
+ nil,
815
+ :tIDENTIFIER, "beginfoo", :expr_cmdarg,
816
+ :tEQL, "=", :expr_beg,
817
+ :tINTEGER, 5, :expr_end,
818
+ :tNL, nil, :expr_beg,
819
+ :tIDENTIFIER, "p", :expr_cmdarg,
820
+ :tIDENTIFIER, "x", :expr_arg,
821
+ :tEQL, "=", :expr_beg,
822
+ :tIDENTIFIER, "beginfoo", :expr_arg)
823
+ end
824
+
825
+ def test_yylex_comment_begin_space
826
+ assert_lex3("=begin blah\nblah\n=end\n", nil)
827
+
828
+ assert_equal "=begin blah\nblah\n=end\n", @lex.comments
829
+ end
830
+
831
+ def test_yylex_comment_end_space_and_text
832
+ assert_lex3("=begin blah\nblah\n=end blab\n", nil)
833
+
834
+ assert_equal "=begin blah\nblah\n=end blab\n", @lex.comments
835
+ end
836
+
837
+ def test_yylex_comment_eos
838
+ assert_lex3("# comment", nil)
839
+ end
840
+
841
+ def test_yylex_constant
842
+ assert_lex3("ArgumentError", nil, :tCONSTANT, "ArgumentError", :expr_cmdarg)
843
+ end
844
+
845
+ def test_yylex_constant_semi
846
+ assert_lex3("ArgumentError;",
847
+ nil,
848
+ :tCONSTANT, "ArgumentError", :expr_cmdarg,
849
+ :tSEMI, ";", :expr_beg)
850
+ end
851
+
852
+ def test_yylex_cvar
853
+ assert_lex3("@@blah", nil, :tCVAR, "@@blah", :expr_end)
854
+ end
855
+
856
+ def test_yylex_cvar_bad
857
+ assert_raises RubyParser::SyntaxError do
858
+ assert_lex3("@@1", nil)
859
+ end
860
+ end
861
+
862
+ def test_yylex_def_bad_name
863
+ self.lex_state = :expr_fname
864
+ refute_lex("def [ ", :kDEF, "def")
865
+ end
866
+
867
+ def test_yylex_div
868
+ assert_lex3("a / 2",
869
+ nil,
870
+ :tIDENTIFIER, "a", :expr_cmdarg,
871
+ :tDIVIDE, "/", :expr_beg,
872
+ :tINTEGER, 2, :expr_end)
873
+ end
874
+
875
+ def test_yylex_div_equals
876
+ assert_lex3("a /= 2",
877
+ nil,
878
+ :tIDENTIFIER, "a", :expr_cmdarg,
879
+ :tOP_ASGN, "/", :expr_beg,
880
+ :tINTEGER, 2, :expr_end)
881
+ end
882
+
883
+ def test_yylex_do
884
+ assert_lex3("x do 42 end",
885
+ nil,
886
+ :tIDENTIFIER, "x", :expr_cmdarg,
887
+ :kDO, "do", :expr_beg,
888
+ :tINTEGER, 42, :expr_end,
889
+ :kEND, "end", :expr_end)
890
+ end
891
+
892
+ def test_yylex_do_block
893
+ self.lex_state = :expr_endarg
894
+
895
+ assert_lex3("x.y do 42 end",
896
+ nil,
897
+ :tIDENTIFIER, "x", :expr_end,
898
+ :tDOT, ".", :expr_dot,
899
+ :tIDENTIFIER, "y", :expr_arg,
900
+ :kDO_BLOCK, "do", :expr_beg,
901
+ :tINTEGER, 42, :expr_end,
902
+ :kEND, "end", :expr_end) do
903
+ @lex.cmdarg.push true
904
+ end
905
+ end
906
+
907
+ def test_yylex_do_block2
908
+ self.lex_state = :expr_endarg
909
+
910
+ assert_lex3("do 42 end",
911
+ nil,
912
+ :kDO_BLOCK, "do", :expr_beg,
913
+ :tINTEGER, 42, :expr_end,
914
+ :kEND, "end", :expr_end)
915
+ end
916
+
917
+ def test_yylex_is_your_spacebar_broken?
918
+ assert_lex3(":a!=:b",
919
+ nil,
920
+ :tSYMBOL, "a", :expr_end,
921
+ :tNEQ, "!=", :expr_beg,
922
+ :tSYMBOL, "b", :expr_end)
923
+ end
924
+
925
+ def test_yylex_do_cond
926
+ assert_lex3("x do 42 end",
927
+ nil,
928
+ :tIDENTIFIER, "x", :expr_cmdarg,
929
+ :kDO_COND, "do", :expr_beg,
930
+ :tINTEGER, 42, :expr_end,
931
+ :kEND, "end", :expr_end) do
932
+ @lex.cond.push true
933
+ end
934
+ end
935
+
936
+ def test_yylex_dollar_bad
937
+ e = refute_lex("$%")
938
+ assert_includes(e.message, "is not allowed as a global variable name")
939
+ end
940
+
941
+ def test_yylex_dollar_eos
942
+ assert_lex3("$", nil, "$", "$", :expr_end) # FIX: wtf is this?!?
943
+ end
944
+
945
+ def test_yylex_dot # HINT message sends
946
+ assert_lex3(".", nil, :tDOT, ".", :expr_dot)
947
+ end
948
+
949
+ def test_yylex_dot2
950
+ assert_lex3("..", nil, :tDOT2, "..", :expr_beg)
951
+ end
952
+
953
+ def test_yylex_dot3
954
+ assert_lex3("...", nil, :tDOT3, "...", :expr_beg)
955
+ end
956
+
957
+ def test_yylex_equals
958
+ # FIX: this sucks
959
+ assert_lex3("=", nil, :tEQL, "=", :expr_beg)
960
+ end
961
+
962
+ def test_yylex_equals2
963
+ assert_lex3("==", nil, :tEQ, "==", :expr_beg)
964
+ end
965
+
966
+ def test_yylex_equals3
967
+ assert_lex3("===", nil, :tEQQ, "===", :expr_beg)
968
+ end
969
+
970
+ def test_yylex_equals_tilde
971
+ assert_lex3("=~", nil, :tMATCH, "=~", :expr_beg)
972
+ end
973
+
974
+ def test_yylex_float
975
+ assert_lex3("1.0", nil, :tFLOAT, 1.0, :expr_end)
976
+ end
977
+
978
+ def test_yylex_float_bad_no_underscores
979
+ refute_lex "1__0.0"
980
+ end
981
+
982
+ def test_yylex_float_bad_no_zero_leading
983
+ refute_lex ".0"
984
+ end
985
+
986
+ def test_yylex_float_bad_trailing_underscore
987
+ refute_lex "123_.0"
988
+ end
989
+
990
+ def test_yylex_float_call
991
+ assert_lex3("1.0.to_s",
992
+ nil,
993
+ :tFLOAT, 1.0, :expr_end,
994
+ :tDOT, ".", :expr_dot,
995
+ :tIDENTIFIER, "to_s", :expr_arg)
996
+ end
997
+
998
+ def test_yylex_float_dot_E
999
+ assert_lex3("1.0E10",
1000
+ nil,
1001
+ :tFLOAT, 10000000000.0, :expr_end)
1002
+ end
1003
+
1004
+ def test_yylex_float_dot_E_neg
1005
+ assert_lex3("-1.0E10",
1006
+ nil,
1007
+ :tUMINUS_NUM, "-", :expr_beg,
1008
+ :tFLOAT, 10000000000.0, :expr_end)
1009
+ end
1010
+
1011
+ def test_yylex_float_dot_e
1012
+ assert_lex3("1.0e10",
1013
+ nil,
1014
+ :tFLOAT, 10000000000.0, :expr_end)
1015
+ end
1016
+
1017
+ def test_yylex_float_dot_e_neg
1018
+ assert_lex3("-1.0e10",
1019
+ nil,
1020
+ :tUMINUS_NUM, "-", :expr_beg,
1021
+ :tFLOAT, 10000000000.0, :expr_end)
1022
+ end
1023
+
1024
+ def test_yylex_float_e
1025
+ assert_lex3("1e10",
1026
+ nil,
1027
+ :tFLOAT, 10000000000.0, :expr_end)
1028
+ end
1029
+
1030
+ def test_yylex_float_e_bad_double_e
1031
+ assert_lex3("1e2e3",
1032
+ nil,
1033
+ :tFLOAT, 100, :expr_end,
1034
+ :tIDENTIFIER, "e3", :expr_end)
1035
+ end
1036
+
1037
+ def test_yylex_float_if_modifier
1038
+ assert_lex3("1e2if",
1039
+ nil,
1040
+ :tFLOAT, 100, :expr_end,
1041
+ :kIF_MOD, "if", :expr_beg)
1042
+ end
1043
+
1044
+ def test_yylex_float_e_bad_trailing_underscore
1045
+ refute_lex "123_e10"
1046
+ end
1047
+
1048
+ def test_yylex_float_e_minus
1049
+ assert_lex3("1e-10", nil, :tFLOAT, 1.0e-10, :expr_end)
1050
+ end
1051
+
1052
+ def test_yylex_float_e_neg
1053
+ assert_lex3("-1e10",
1054
+ nil,
1055
+ :tUMINUS_NUM, "-", :expr_beg,
1056
+ :tFLOAT, 10000000000.0, :expr_end)
1057
+ end
1058
+
1059
+ def test_yylex_float_e_neg_minus
1060
+ assert_lex3("-1e-10",
1061
+ nil,
1062
+ :tUMINUS_NUM, "-", :expr_beg,
1063
+ :tFLOAT, 1.0e-10, :expr_end)
1064
+ end
1065
+
1066
+ def test_yylex_float_e_neg_plus
1067
+ assert_lex3("-1e+10",
1068
+ nil,
1069
+ :tUMINUS_NUM, "-", :expr_beg,
1070
+ :tFLOAT, 10000000000.0, :expr_end)
1071
+ end
1072
+
1073
+ def test_yylex_float_e_plus
1074
+ assert_lex3("1e+10", nil, :tFLOAT, 10000000000.0, :expr_end)
1075
+ end
1076
+
1077
+ def test_yylex_float_e_zero
1078
+ assert_lex3("0e0", nil, :tFLOAT, 0.0, :expr_end)
1079
+ end
1080
+
1081
+ def test_yylex_float_neg
1082
+ assert_lex3("-1.0",
1083
+ nil,
1084
+ :tUMINUS_NUM, "-", :expr_beg,
1085
+ :tFLOAT, 1.0, :expr_end)
1086
+ end
1087
+
1088
+ def test_yylex_ge
1089
+ assert_lex3("a >= 2",
1090
+ nil,
1091
+ :tIDENTIFIER, "a", :expr_cmdarg,
1092
+ :tGEQ, ">=", :expr_beg,
1093
+ :tINTEGER, 2, :expr_end)
1094
+ end
1095
+
1096
+ def test_yylex_global
1097
+ assert_lex3("$blah", nil, :tGVAR, "$blah", :expr_end)
1098
+ end
1099
+
1100
+ def test_yylex_global_backref
1101
+ self.lex_state = :expr_fname
1102
+
1103
+ assert_lex3("$`", nil, :tGVAR, "$`", :expr_end)
1104
+ end
1105
+
1106
+ def test_yylex_global_dash_nothing
1107
+ assert_lex3("$- ", nil, :tGVAR, "$-", :expr_end)
1108
+ end
1109
+
1110
+ def test_yylex_global_dash_something
1111
+ assert_lex3("$-x", nil, :tGVAR, "$-x", :expr_end)
1112
+ end
1113
+
1114
+ def test_yylex_global_number
1115
+ self.lex_state = :expr_fname
1116
+
1117
+ assert_lex3("$1", nil, :tGVAR, "$1", :expr_end)
1118
+ end
1119
+
1120
+ def test_yylex_global_number_big
1121
+ self.lex_state = :expr_fname
1122
+
1123
+ assert_lex3("$1234", nil, :tGVAR, "$1234", :expr_end)
1124
+ end
1125
+
1126
+ def test_yylex_global_other
1127
+ assert_lex3("[$~, $*, $$, $?, $!, $@, $/, $\\, $;, $,, $., $=, $:, $<, $>, $\"]",
1128
+ nil,
1129
+ :tLBRACK, "[", :expr_beg,
1130
+ :tGVAR, "$~", :expr_end, :tCOMMA, ",", :expr_beg,
1131
+ :tGVAR, "$*", :expr_end, :tCOMMA, ",", :expr_beg,
1132
+ :tGVAR, "$$", :expr_end, :tCOMMA, ",", :expr_beg,
1133
+ :tGVAR, "$?", :expr_end, :tCOMMA, ",", :expr_beg,
1134
+ :tGVAR, "$!", :expr_end, :tCOMMA, ",", :expr_beg,
1135
+ :tGVAR, "$@", :expr_end, :tCOMMA, ",", :expr_beg,
1136
+ :tGVAR, "$/", :expr_end, :tCOMMA, ",", :expr_beg,
1137
+ :tGVAR, "$\\", :expr_end, :tCOMMA, ",", :expr_beg,
1138
+ :tGVAR, "$;", :expr_end, :tCOMMA, ",", :expr_beg,
1139
+ :tGVAR, "$,", :expr_end, :tCOMMA, ",", :expr_beg,
1140
+ :tGVAR, "$.", :expr_end, :tCOMMA, ",", :expr_beg,
1141
+ :tGVAR, "$=", :expr_end, :tCOMMA, ",", :expr_beg,
1142
+ :tGVAR, "$:", :expr_end, :tCOMMA, ",", :expr_beg,
1143
+ :tGVAR, "$<", :expr_end, :tCOMMA, ",", :expr_beg,
1144
+ :tGVAR, "$>", :expr_end, :tCOMMA, ",", :expr_beg,
1145
+ :tGVAR, "$\"", :expr_end,
1146
+ :tRBRACK, "]", :expr_endarg)
1147
+ end
1148
+
1149
+ def test_yylex_global_underscore
1150
+ assert_lex3("$_", nil, :tGVAR, "$_", :expr_end)
1151
+ end
1152
+
1153
+ def test_yylex_global_wierd
1154
+ assert_lex3("$__blah", nil, :tGVAR, "$__blah", :expr_end)
1155
+ end
1156
+
1157
+ def test_yylex_global_zero
1158
+ assert_lex3("$0", nil, :tGVAR, "$0", :expr_end)
1159
+ end
1160
+
1161
+ def test_yylex_gt
1162
+ assert_lex3("a > 2",
1163
+ nil,
1164
+ :tIDENTIFIER, "a", :expr_cmdarg,
1165
+ :tGT, ">", :expr_beg,
1166
+ :tINTEGER, 2, :expr_end)
1167
+ end
1168
+
1169
+ def test_yylex_heredoc_backtick
1170
+ assert_lex3("a = <<`EOF`\n blah blah\nEOF\n\n",
1171
+ nil,
1172
+ :tIDENTIFIER, "a", :expr_cmdarg,
1173
+ :tEQL, "=", :expr_beg,
1174
+ :tXSTRING_BEG, "`", :expr_beg,
1175
+ :tSTRING_CONTENT, " blah blah\n", :expr_beg,
1176
+ :tSTRING_END, "EOF", :expr_end,
1177
+ :tNL, nil, :expr_beg)
1178
+ end
1179
+
1180
+ def test_yylex_heredoc_double
1181
+ assert_lex3("a = <<\"EOF\"\n blah blah\nEOF\n\n",
1182
+ nil,
1183
+ :tIDENTIFIER, "a", :expr_cmdarg,
1184
+ :tEQL, "=", :expr_beg,
1185
+ :tSTRING_BEG, "\"", :expr_beg,
1186
+ :tSTRING_CONTENT, " blah blah\n", :expr_beg,
1187
+ :tSTRING_END, "EOF", :expr_end,
1188
+ :tNL, nil, :expr_beg)
1189
+ end
1190
+
1191
+ def test_yylex_heredoc_double_dash
1192
+ assert_lex3("a = <<-\"EOF\"\n blah blah\n EOF\n\n",
1193
+ nil,
1194
+ :tIDENTIFIER, "a", :expr_cmdarg,
1195
+ :tEQL, "=", :expr_beg,
1196
+ :tSTRING_BEG, "\"", :expr_beg,
1197
+ :tSTRING_CONTENT, " blah blah\n", :expr_beg,
1198
+ :tSTRING_END, "EOF", :expr_end,
1199
+ :tNL, nil, :expr_beg)
1200
+ end
1201
+
1202
+ def test_yylex_heredoc_double_squiggly
1203
+ setup_lexer_class Ruby23Parser
1204
+
1205
+ assert_lex3("a = <<~\"EOF\"\n blah blah\n EOF\n\n",
1206
+ nil,
1207
+ :tIDENTIFIER, "a", :expr_cmdarg,
1208
+ :tEQL, "=", :expr_beg,
1209
+ :tSTRING_BEG, "\"", :expr_beg,
1210
+ :tSTRING_CONTENT, "blah blah\n", :expr_beg,
1211
+ :tSTRING_END, "EOF", :expr_end,
1212
+ :tNL, nil, :expr_beg)
1213
+ end
1214
+
1215
+ # mri handles tabs in a pretty specific way:
1216
+ # https://github.com/ruby/ruby/blob/trunk/parse.y#L5925
1217
+ def test_yylex_heredoc_double_squiggly_with_tab_indentation_remaining
1218
+ setup_lexer_class Ruby23Parser
1219
+
1220
+ assert_lex3("a = <<~\"EOF\"\n blah blah\n \tblah blah\n EOF\n\n",
1221
+ nil,
1222
+ :tIDENTIFIER, "a", :expr_cmdarg,
1223
+ :tEQL, "=", :expr_beg,
1224
+ :tSTRING_BEG, "\"", :expr_beg,
1225
+ :tSTRING_CONTENT, "blah blah\n\tblah blah\n", :expr_beg,
1226
+ :tSTRING_END, "EOF", :expr_end,
1227
+ :tNL, nil, :expr_beg)
1228
+ end
1229
+
1230
+ def test_yylex_heredoc_double_squiggly_with_tab_indentation_removed
1231
+ setup_lexer_class Ruby23Parser
1232
+
1233
+ assert_lex3("a = <<~\"EOF\"\n blah blah\n\t blah blah\n EOF\n\n",
1234
+ nil,
1235
+ :tIDENTIFIER, "a", :expr_cmdarg,
1236
+ :tEQL, "=", :expr_beg,
1237
+ :tSTRING_BEG, "\"", :expr_beg,
1238
+ :tSTRING_CONTENT, "blah blah\n blah blah\n", :expr_beg,
1239
+ :tSTRING_END, "EOF", :expr_end,
1240
+ :tNL, nil, :expr_beg)
1241
+ end
1242
+
1243
+ def test_yylex_heredoc_double_eos
1244
+ refute_lex("a = <<\"EOF\"\nblah",
1245
+ :tIDENTIFIER, "a",
1246
+ :tEQL, "=",
1247
+ :tSTRING_BEG, "\"")
1248
+ end
1249
+
1250
+ def test_yylex_heredoc_double_eos_nl
1251
+ refute_lex("a = <<\"EOF\"\nblah\n",
1252
+ :tIDENTIFIER, "a",
1253
+ :tEQL, "=",
1254
+ :tSTRING_BEG, "\"")
1255
+ end
1256
+
1257
+ def test_yylex_heredoc_double_interp
1258
+ assert_lex3("a = <<\"EOF\"\n#x a \#@a b \#$b c \#{3} \nEOF\n\n",
1259
+ nil,
1260
+ :tIDENTIFIER, "a", :expr_cmdarg,
1261
+ :tEQL, "=", :expr_beg,
1262
+ :tSTRING_BEG, "\"", :expr_beg,
1263
+ :tSTRING_CONTENT, "#x a ", :expr_beg,
1264
+ :tSTRING_DVAR, "\#@", :expr_beg,
1265
+ :tSTRING_CONTENT, "@a b ", :expr_beg, # HUH?
1266
+ :tSTRING_DVAR, "\#$", :expr_beg,
1267
+ :tSTRING_CONTENT, "$b c ", :expr_beg, # HUH?
1268
+ :tSTRING_DBEG, "\#{", :expr_beg,
1269
+ :tSTRING_CONTENT, "3} \n", :expr_beg, # HUH?
1270
+ :tSTRING_END, "EOF", :expr_end,
1271
+ :tNL, nil, :expr_beg)
1272
+ end
1273
+
1274
+ def test_yylex_heredoc_empty
1275
+ assert_lex3("<<\"\"\n\#{x}\nblah2\n\n\n",
1276
+ nil,
1277
+ :tSTRING_BEG, "\"", :expr_beg,
1278
+ :tSTRING_DBEG, "\#{", :expr_beg,
1279
+ :tSTRING_CONTENT, "x}\nblah2\n", :expr_beg,
1280
+ :tSTRING_END, "", :expr_end,
1281
+ :tNL, nil, :expr_beg)
1282
+ end
1283
+
1284
+ def test_yylex_heredoc_none
1285
+ assert_lex3("a = <<EOF\nblah\nblah\nEOF\n",
1286
+ nil,
1287
+ :tIDENTIFIER, "a", :expr_cmdarg,
1288
+ :tEQL, "=", :expr_beg,
1289
+ :tSTRING_BEG, "\"", :expr_beg,
1290
+ :tSTRING_CONTENT, "blah\nblah\n", :expr_beg,
1291
+ :tSTRING_END, "EOF", :expr_end,
1292
+ :tNL, nil, :expr_beg)
1293
+ end
1294
+
1295
+ def test_yylex_heredoc_none_bad_eos
1296
+ refute_lex("a = <<EOF",
1297
+ :tIDENTIFIER, "a",
1298
+ :tEQL, "=",
1299
+ :tSTRING_BEG, "\"")
1300
+ end
1301
+
1302
+ def test_yylex_heredoc_none_dash
1303
+ assert_lex3("a = <<-EOF\nblah\nblah\n EOF\n",
1304
+ nil,
1305
+ :tIDENTIFIER, "a", :expr_cmdarg,
1306
+ :tEQL, "=", :expr_beg,
1307
+ :tSTRING_BEG, "\"", :expr_beg,
1308
+ :tSTRING_CONTENT, "blah\nblah\n", :expr_beg,
1309
+ :tSTRING_END, "EOF", :expr_end,
1310
+ :tNL, nil, :expr_beg)
1311
+ end
1312
+
1313
+ def test_yylex_heredoc_none_squiggly
1314
+ setup_lexer_class Ruby23Parser
1315
+
1316
+ assert_lex3("a = <<~EOF\n blah\n blah\n EOF\n",
1317
+ nil,
1318
+ :tIDENTIFIER, "a", :expr_cmdarg,
1319
+ :tEQL, "=", :expr_beg,
1320
+ :tSTRING_BEG, "\"", :expr_beg,
1321
+ :tSTRING_CONTENT, "blah\nblah\n", :expr_beg,
1322
+ :tSTRING_END, "EOF", :expr_end,
1323
+ :tNL, nil, :expr_beg)
1324
+ end
1325
+
1326
+ def test_yylex_heredoc_single
1327
+ assert_lex3("a = <<'EOF'\n blah blah\nEOF\n\n",
1328
+ nil,
1329
+ :tIDENTIFIER, "a", :expr_cmdarg,
1330
+ :tEQL, "=", :expr_beg,
1331
+ :tSTRING_BEG, "\"", :expr_beg,
1332
+ :tSTRING_CONTENT, " blah blah\n", :expr_beg,
1333
+ :tSTRING_END, "EOF", :expr_end,
1334
+ :tNL, nil, :expr_beg)
1335
+ end
1336
+
1337
+ def test_yylex_heredoc_single_bad_eos_body
1338
+ refute_lex("a = <<'EOF'\nblah",
1339
+ :tIDENTIFIER, "a",
1340
+ :tEQL, "=",
1341
+ :tSTRING_BEG, "\"")
1342
+ end
1343
+
1344
+ def test_yylex_heredoc_single_bad_eos_empty
1345
+ refute_lex("a = <<''\n",
1346
+ :tIDENTIFIER, "a",
1347
+ :tEQL, "=",
1348
+ :tSTRING_BEG, "\"")
1349
+ end
1350
+
1351
+ def test_yylex_heredoc_single_bad_eos_term
1352
+ refute_lex("a = <<'EOF",
1353
+ :tIDENTIFIER, "a",
1354
+ :tEQL, "=",
1355
+ :tSTRING_BEG, "\"")
1356
+ end
1357
+
1358
+ def test_yylex_heredoc_single_bad_eos_term_nl
1359
+ refute_lex("a = <<'EOF\ns = 'blah blah'",
1360
+ :tIDENTIFIER, "a",
1361
+ :tEQL, "=",
1362
+ :tSTRING_BEG, "\"")
1363
+ end
1364
+
1365
+ def test_yylex_heredoc_single_dash
1366
+ assert_lex3("a = <<-'EOF'\n blah blah\n EOF\n\n",
1367
+ nil,
1368
+ :tIDENTIFIER, "a", :expr_cmdarg,
1369
+ :tEQL, "=", :expr_beg,
1370
+ :tSTRING_BEG, "\"", :expr_beg,
1371
+ :tSTRING_CONTENT, " blah blah\n", :expr_beg,
1372
+ :tSTRING_END, "EOF", :expr_end,
1373
+ :tNL, nil, :expr_beg)
1374
+ end
1375
+
1376
+ def test_yylex_heredoc_single_squiggly
1377
+ setup_lexer_class Ruby23Parser
1378
+
1379
+ assert_lex3("a = <<~'EOF'\n blah blah\n EOF\n\n",
1380
+ nil,
1381
+ :tIDENTIFIER, "a", :expr_cmdarg,
1382
+ :tEQL, "=", :expr_beg,
1383
+ :tSTRING_BEG, "\"", :expr_beg,
1384
+ :tSTRING_CONTENT, "blah blah\n", :expr_beg,
1385
+ :tSTRING_END, "EOF", :expr_end,
1386
+ :tNL, nil, :expr_beg)
1387
+ end
1388
+
1389
+ def test_yylex_identifier
1390
+ assert_lex3("identifier",
1391
+ nil,
1392
+ :tIDENTIFIER, "identifier", :expr_cmdarg)
1393
+ end
1394
+
1395
+ def test_yylex_identifier_bang
1396
+ assert_lex3("identifier!",
1397
+ nil,
1398
+ :tFID, "identifier!", :expr_cmdarg)
1399
+ end
1400
+
1401
+ def test_yylex_identifier_cmp
1402
+ assert_lex_fname "<=>", :tCMP
1403
+ end
1404
+
1405
+ def test_yylex_identifier_def__18
1406
+ setup_lexer_class RubyParser::V18
1407
+
1408
+ assert_lex_fname "identifier", :tIDENTIFIER, :expr_end
1409
+ end
1410
+
1411
+ def test_yylex_identifier_def__1920
1412
+ setup_lexer_class RubyParser::V19
1413
+
1414
+ assert_lex_fname "identifier", :tIDENTIFIER, :expr_endfn
1415
+ end
1416
+
1417
+ def test_yylex_identifier_eh
1418
+ assert_lex3("identifier?", nil, :tFID, "identifier?", :expr_cmdarg)
1419
+ end
1420
+
1421
+ def test_yylex_identifier_equals_arrow
1422
+ assert_lex3(":blah==>",
1423
+ nil,
1424
+ :tSYMBOL, "blah=", :expr_end,
1425
+ :tASSOC, "=>", :expr_beg)
1426
+ end
1427
+
1428
+ def test_yylex_identifier_equals3
1429
+ assert_lex3(":a===b",
1430
+ nil,
1431
+ :tSYMBOL, "a", :expr_end,
1432
+ :tEQQ, "===", :expr_beg,
1433
+ :tIDENTIFIER, "b", :expr_arg)
1434
+ end
1435
+
1436
+ def test_yylex_identifier_equals_equals_arrow
1437
+ assert_lex3(":a==>b",
1438
+ nil,
1439
+ :tSYMBOL, "a=", :expr_end,
1440
+ :tASSOC, "=>", :expr_beg,
1441
+ :tIDENTIFIER, "b", :expr_arg)
1442
+ end
1443
+
1444
+ def test_yylex_identifier_equals_caret
1445
+ assert_lex_fname "^", :tCARET
1446
+ end
1447
+
1448
+ def test_yylex_identifier_equals_def__18
1449
+ setup_lexer_class RubyParser::V18
1450
+
1451
+ assert_lex_fname "identifier=", :tIDENTIFIER, :expr_end
1452
+ end
1453
+
1454
+ def test_yylex_identifier_equals_def__1920
1455
+ setup_lexer_class RubyParser::V19
1456
+
1457
+ assert_lex_fname "identifier=", :tIDENTIFIER, :expr_endfn
1458
+ end
1459
+
1460
+ def test_yylex_identifier_equals_def2
1461
+ assert_lex_fname "==", :tEQ
1462
+ end
1463
+
1464
+ def test_yylex_identifier_equals_expr
1465
+ self.lex_state = :expr_dot
1466
+ assert_lex3("y = arg",
1467
+ nil,
1468
+ :tIDENTIFIER, "y", :expr_cmdarg,
1469
+ :tEQL, "=", :expr_beg,
1470
+ :tIDENTIFIER, "arg", :expr_arg)
1471
+ end
1472
+
1473
+ def test_yylex_identifier_equals_or
1474
+ assert_lex_fname "|", :tPIPE
1475
+ end
1476
+
1477
+ def test_yylex_identifier_equals_slash
1478
+ assert_lex_fname "/", :tDIVIDE
1479
+ end
1480
+
1481
+ def test_yylex_identifier_equals_tilde
1482
+ self.lex_state = :expr_fname # can only set via parser's defs
1483
+
1484
+ assert_lex3("identifier=~",
1485
+ nil,
1486
+ :tIDENTIFIER, "identifier", :expr_endfn,
1487
+ :tMATCH, "=~", :expr_beg)
1488
+ end
1489
+
1490
+ def test_yylex_identifier_gt
1491
+ assert_lex_fname ">", :tGT
1492
+ end
1493
+
1494
+ def test_yylex_identifier_le
1495
+ assert_lex_fname "<=", :tLEQ
1496
+ end
1497
+
1498
+ def test_yylex_identifier_lt
1499
+ assert_lex_fname "<", :tLT
1500
+ end
1501
+
1502
+ def test_yylex_identifier_tilde
1503
+ assert_lex_fname "~", :tTILDE
1504
+ end
1505
+
1506
+ def test_yylex_index
1507
+ assert_lex_fname "[]", :tAREF
1508
+ end
1509
+
1510
+ def test_yylex_index_equals
1511
+ assert_lex_fname "[]=", :tASET
1512
+ end
1513
+
1514
+ def test_yylex_integer
1515
+ assert_lex3("42", nil, :tINTEGER, 42, :expr_end)
1516
+ end
1517
+
1518
+ def test_yylex_integer_bin
1519
+ assert_lex3("0b101010", nil, :tINTEGER, 42, :expr_end)
1520
+ end
1521
+
1522
+ def test_yylex_integer_bin_bad_none
1523
+ refute_lex "0b "
1524
+ end
1525
+
1526
+ def test_yylex_integer_bin_bad_underscores
1527
+ refute_lex "0b10__01"
1528
+ end
1529
+
1530
+ def test_yylex_integer_dec
1531
+ assert_lex3("42", nil, :tINTEGER, 42, :expr_end)
1532
+ end
1533
+
1534
+ def test_yylex_integer_dec_bad_underscores
1535
+ refute_lex "42__24"
1536
+ end
1537
+
1538
+ def test_yylex_integer_dec_d
1539
+ assert_lex3("0d42", nil, :tINTEGER, 42, :expr_end)
1540
+ end
1541
+
1542
+ def test_yylex_integer_dec_d_bad_none
1543
+ refute_lex "0d"
1544
+ end
1545
+
1546
+ def test_yylex_integer_dec_d_bad_underscores
1547
+ refute_lex "0d42__24"
1548
+ end
1549
+
1550
+ def test_yylex_integer_if_modifier
1551
+ assert_lex3("123if",
1552
+ nil,
1553
+ :tINTEGER, 123, :expr_end,
1554
+ :kIF_MOD, "if", :expr_beg)
1555
+ end
1556
+
1557
+ def test_yylex_question_eh_a__18
1558
+ setup_lexer_class RubyParser::V18
1559
+
1560
+ assert_lex3("?a", nil, :tINTEGER, 97, :expr_end)
1561
+ end
1562
+
1563
+ def test_yylex_question_eh_a__19
1564
+ setup_lexer_class RubyParser::V19
1565
+
1566
+ assert_lex3("?a", nil, :tSTRING, "a", :expr_end)
1567
+ end
1568
+
1569
+ def test_yylex_question_eh_escape_M_escape_C__18
1570
+ setup_lexer_class RubyParser::V18
1571
+
1572
+ assert_lex3("?\\M-\\C-a", nil, :tINTEGER, 129, :expr_end)
1573
+ end
1574
+
1575
+ def test_yylex_question_eh_escape_M_escape_C__19
1576
+ setup_lexer_class RubyParser::V19
1577
+
1578
+ assert_lex3("?\\M-\\C-a", nil, :tSTRING, "\M-\C-a", :expr_end)
1579
+ end
1580
+
1581
+ def test_yylex_integer_hex
1582
+ assert_lex3 "0x2a", nil, :tINTEGER, 42, :expr_end
1583
+ end
1584
+
1585
+ def test_yylex_integer_hex_bad_none
1586
+ refute_lex "0x "
1587
+ end
1588
+
1589
+ def test_yylex_integer_hex_bad_underscores
1590
+ refute_lex "0xab__cd"
1591
+ end
1592
+
1593
+ def test_yylex_integer_oct
1594
+ assert_lex3("052", nil, :tINTEGER, 42, :expr_end)
1595
+ end
1596
+
1597
+ def test_yylex_integer_oct_bad_range
1598
+ refute_lex "08"
1599
+ end
1600
+
1601
+ def test_yylex_integer_oct_bad_range2
1602
+ refute_lex "08"
1603
+ end
1604
+
1605
+ def test_yylex_integer_oct_bad_underscores
1606
+ refute_lex "01__23"
1607
+ end
1608
+
1609
+ def test_yylex_integer_oct_O
1610
+ assert_lex3 "0O52", nil, :tINTEGER, 42, :expr_end
1611
+ end
1612
+
1613
+ def test_yylex_integer_oct_O_bad_range
1614
+ refute_lex "0O8"
1615
+ end
1616
+
1617
+ def test_yylex_integer_oct_O_bad_underscores
1618
+ refute_lex "0O1__23"
1619
+ end
1620
+
1621
+ def test_yylex_integer_oct_O_not_bad_none
1622
+ assert_lex3 "0O ", nil, :tINTEGER, 0, :expr_end
1623
+ end
1624
+
1625
+ def test_yylex_integer_oct_o
1626
+ assert_lex3 "0o52", nil, :tINTEGER, 42, :expr_end
1627
+ end
1628
+
1629
+ def test_yylex_integer_oct_o_bad_range
1630
+ refute_lex "0o8"
1631
+ end
1632
+
1633
+ def test_yylex_integer_oct_o_bad_underscores
1634
+ refute_lex "0o1__23"
1635
+ end
1636
+
1637
+ def test_yylex_integer_oct_o_not_bad_none
1638
+ assert_lex3 "0o ", nil, :tINTEGER, 0, :expr_end
1639
+ end
1640
+
1641
+ def test_yylex_integer_trailing
1642
+ assert_lex3("1.to_s",
1643
+ nil,
1644
+ :tINTEGER, 1, :expr_end,
1645
+ :tDOT, ".", :expr_dot,
1646
+ :tIDENTIFIER, "to_s", :expr_arg)
1647
+ end
1648
+
1649
+ def test_yylex_integer_underscore
1650
+ assert_lex3("4_2", nil, :tINTEGER, 42, :expr_end)
1651
+ end
1652
+
1653
+ def test_yylex_integer_underscore_bad
1654
+ refute_lex "4__2"
1655
+ end
1656
+
1657
+ def test_yylex_integer_zero
1658
+ assert_lex3 "0", nil, :tINTEGER, 0, :expr_end
1659
+ end
1660
+
1661
+ def test_yylex_ivar
1662
+ assert_lex3("@blah", nil, :tIVAR, "@blah", :expr_end)
1663
+ end
1664
+
1665
+ def test_yylex_ivar_bad
1666
+ refute_lex "@1"
1667
+ end
1668
+
1669
+ def test_yylex_ivar_bad_0_length
1670
+ refute_lex "1+@\n", :tINTEGER, 1, :tPLUS, "+", :expr_end
1671
+ end
1672
+
1673
+ def test_yylex_keyword_expr
1674
+ self.lex_state = :expr_endarg
1675
+
1676
+ assert_lex3("if", nil, :kIF_MOD, "if", :expr_beg)
1677
+ end
1678
+
1679
+ def test_yylex_lt
1680
+ assert_lex3("<", nil, :tLT, "<", :expr_beg)
1681
+ end
1682
+
1683
+ def test_yylex_lt2
1684
+ assert_lex3("a << b",
1685
+ nil,
1686
+ :tIDENTIFIER, "a", :expr_cmdarg,
1687
+ :tLSHFT, "<<", :expr_beg,
1688
+ :tIDENTIFIER, "b", :expr_arg)
1689
+ end
1690
+
1691
+ def test_yylex_lt2_equals
1692
+ assert_lex3("a <<= b",
1693
+ nil,
1694
+ :tIDENTIFIER, "a", :expr_cmdarg,
1695
+ :tOP_ASGN, "<<", :expr_beg,
1696
+ :tIDENTIFIER, "b", :expr_arg)
1697
+ end
1698
+
1699
+ def test_yylex_lt_equals
1700
+ assert_lex3("<=", nil, :tLEQ, "<=", :expr_beg)
1701
+ end
1702
+
1703
+ def test_yylex_minus
1704
+ assert_lex3("1 - 2",
1705
+ nil,
1706
+ :tINTEGER, 1, :expr_end,
1707
+ :tMINUS, "-", :expr_beg,
1708
+ :tINTEGER, 2, :expr_end)
1709
+ end
1710
+
1711
+ def test_yylex_minus_equals
1712
+ assert_lex3("-=", nil, :tOP_ASGN, "-", :expr_beg)
1713
+ end
1714
+
1715
+ def test_yylex_minus_method
1716
+ self.lex_state = :expr_fname
1717
+
1718
+ assert_lex3("-", nil, :tMINUS, "-", :expr_arg)
1719
+ end
1720
+
1721
+ def test_yylex_minus_unary_method
1722
+ self.lex_state = :expr_fname
1723
+
1724
+ assert_lex3("-@", nil, :tUMINUS, "-@", :expr_arg)
1725
+ end
1726
+
1727
+ def test_yylex_minus_unary_number
1728
+ assert_lex3("-42",
1729
+ nil,
1730
+ :tUMINUS_NUM, "-", :expr_beg,
1731
+ :tINTEGER, 42, :expr_end)
1732
+ end
1733
+
1734
+ def test_yylex_nth_ref
1735
+ assert_lex3("[$1, $2, $3, $4, $5, $6, $7, $8, $9]",
1736
+ nil,
1737
+ :tLBRACK, "[", :expr_beg,
1738
+ :tNTH_REF, 1, :expr_end, :tCOMMA, ",", :expr_beg,
1739
+ :tNTH_REF, 2, :expr_end, :tCOMMA, ",", :expr_beg,
1740
+ :tNTH_REF, 3, :expr_end, :tCOMMA, ",", :expr_beg,
1741
+ :tNTH_REF, 4, :expr_end, :tCOMMA, ",", :expr_beg,
1742
+ :tNTH_REF, 5, :expr_end, :tCOMMA, ",", :expr_beg,
1743
+ :tNTH_REF, 6, :expr_end, :tCOMMA, ",", :expr_beg,
1744
+ :tNTH_REF, 7, :expr_end, :tCOMMA, ",", :expr_beg,
1745
+ :tNTH_REF, 8, :expr_end, :tCOMMA, ",", :expr_beg,
1746
+ :tNTH_REF, 9, :expr_end,
1747
+ :tRBRACK, "]", :expr_endarg)
1748
+ end
1749
+
1750
+ def test_yylex_open_bracket
1751
+ assert_lex3("(", nil, :tLPAREN, "(", :expr_beg)
1752
+ end
1753
+
1754
+ def test_yylex_open_bracket_cmdarg
1755
+ self.lex_state = :expr_cmdarg
1756
+
1757
+ assert_lex3(" (", nil, :tLPAREN_ARG, "(", :expr_beg)
1758
+ end
1759
+
1760
+ def test_yylex_open_bracket_exprarg__18
1761
+ setup_lexer_class RubyParser::V18
1762
+ self.lex_state = :expr_arg
1763
+
1764
+ assert_lex3(" (", nil, :tLPAREN2, "(", :expr_beg)
1765
+ end
1766
+
1767
+ def test_yylex_open_bracket_exprarg__19
1768
+ setup_lexer_class RubyParser::V19
1769
+ self.lex_state = :expr_arg
1770
+
1771
+ assert_lex3(" (", nil, :tLPAREN_ARG, "(", :expr_beg)
1772
+ end
1773
+
1774
+ def test_yylex_open_curly_bracket
1775
+ assert_lex3("{", nil, :tLBRACE, "{", :expr_beg)
1776
+ end
1777
+
1778
+ def test_yylex_open_curly_bracket_arg
1779
+ self.lex_state = :expr_arg
1780
+
1781
+ assert_lex3("m { 3 }",
1782
+ nil,
1783
+ :tIDENTIFIER, "m", :expr_cmdarg,
1784
+ :tLCURLY, "{", :expr_beg,
1785
+ :tINTEGER, 3, :expr_end,
1786
+ :tRCURLY, "}", :expr_endarg)
1787
+ end
1788
+
1789
+ def test_yylex_open_curly_bracket_block
1790
+ self.lex_state = :expr_endarg # seen m(3)
1791
+
1792
+ assert_lex3("{ 4 }",
1793
+ nil,
1794
+ :tLBRACE_ARG, "{", :expr_beg,
1795
+ :tINTEGER, 4, :expr_end,
1796
+ :tRCURLY, "}", :expr_endarg)
1797
+ end
1798
+
1799
+ def test_yylex_open_square_bracket_arg
1800
+ self.lex_state = :expr_arg
1801
+
1802
+ assert_lex3("m [ 3 ]",
1803
+ nil,
1804
+ :tIDENTIFIER, "m", :expr_cmdarg,
1805
+ :tLBRACK, "[", :expr_beg,
1806
+ :tINTEGER, 3, :expr_end,
1807
+ :tRBRACK, "]", :expr_endarg)
1808
+ end
1809
+
1810
+ def test_yylex_open_square_bracket_ary
1811
+ assert_lex3("[1, 2, 3]",
1812
+ nil,
1813
+ :tLBRACK, "[", :expr_beg,
1814
+ :tINTEGER, 1, :expr_end, :tCOMMA, ",", :expr_beg,
1815
+ :tINTEGER, 2, :expr_end, :tCOMMA, ",", :expr_beg,
1816
+ :tINTEGER, 3, :expr_end,
1817
+ :tRBRACK, "]", :expr_endarg)
1818
+ end
1819
+
1820
+ def test_yylex_open_square_bracket_meth
1821
+ assert_lex3("m[3]",
1822
+ nil,
1823
+ :tIDENTIFIER, "m", :expr_cmdarg,
1824
+ :tLBRACK2, "[", :expr_beg,
1825
+ :tINTEGER, 3, :expr_end,
1826
+ :tRBRACK, "]", :expr_endarg)
1827
+ end
1828
+
1829
+ def test_yylex_or
1830
+ assert_lex3("|", nil, :tPIPE, "|", :expr_beg)
1831
+ end
1832
+
1833
+ def test_yylex_or2
1834
+ assert_lex3("||", nil, :tOROP, "||", :expr_beg)
1835
+ end
1836
+
1837
+ def test_yylex_or2_equals
1838
+ assert_lex3("||=", nil, :tOP_ASGN, "||", :expr_beg)
1839
+ end
1840
+
1841
+ def test_yylex_or_equals
1842
+ assert_lex3("|=", nil, :tOP_ASGN, "|", :expr_beg)
1843
+ end
1844
+
1845
+ def test_yylex_percent
1846
+ assert_lex3("a % 2",
1847
+ nil,
1848
+ :tIDENTIFIER, "a", :expr_cmdarg,
1849
+ :tPERCENT, "%", :expr_beg,
1850
+ :tINTEGER, 2, :expr_end)
1851
+ end
1852
+
1853
+ def test_yylex_percent_equals
1854
+ assert_lex3("a %= 2",
1855
+ nil,
1856
+ :tIDENTIFIER, "a", :expr_cmdarg,
1857
+ :tOP_ASGN, "%", :expr_beg,
1858
+ :tINTEGER, 2, :expr_end)
1859
+ end
1860
+
1861
+ def test_yylex_plus
1862
+ assert_lex3("1 + 1", # TODO lex_state?
1863
+ nil,
1864
+ :tINTEGER, 1, :expr_end,
1865
+ :tPLUS, "+", :expr_beg,
1866
+ :tINTEGER, 1, :expr_end)
1867
+ end
1868
+
1869
+ def test_yylex_plus_equals
1870
+ assert_lex3("+=", nil, :tOP_ASGN, "+", :expr_beg)
1871
+ end
1872
+
1873
+ def test_yylex_plus_method
1874
+ self.lex_state = :expr_fname
1875
+
1876
+ assert_lex3("+", nil, :tPLUS, "+", :expr_arg)
1877
+ end
1878
+
1879
+ def test_yylex_plus_unary_method
1880
+ self.lex_state = :expr_fname
1881
+
1882
+ assert_lex3("+@", nil, :tUPLUS, "+@", :expr_arg)
1883
+ end
1884
+
1885
+ def test_yylex_not_unary_method
1886
+ self.lex_state = :expr_fname
1887
+
1888
+ assert_lex3("!@", nil, :tUBANG, "!@", :expr_arg)
1889
+ end
1890
+
1891
+ def test_yylex_numbers
1892
+ assert_lex3("0b10", nil, :tINTEGER, 2, :expr_end)
1893
+ assert_lex3("0B10", nil, :tINTEGER, 2, :expr_end)
1894
+
1895
+ assert_lex3("0d10", nil, :tINTEGER, 10, :expr_end)
1896
+ assert_lex3("0D10", nil, :tINTEGER, 10, :expr_end)
1897
+
1898
+ assert_lex3("0x10", nil, :tINTEGER, 16, :expr_end)
1899
+ assert_lex3("0X10", nil, :tINTEGER, 16, :expr_end)
1900
+
1901
+ assert_lex3("0o10", nil, :tINTEGER, 8, :expr_end)
1902
+ assert_lex3("0O10", nil, :tINTEGER, 8, :expr_end)
1903
+
1904
+ assert_lex3("0o", nil, :tINTEGER, 0, :expr_end)
1905
+ assert_lex3("0O", nil, :tINTEGER, 0, :expr_end)
1906
+
1907
+ assert_lex3("0", nil, :tINTEGER, 0, :expr_end)
1908
+
1909
+ refute_lex "0x"
1910
+ refute_lex "0X"
1911
+ refute_lex "0b"
1912
+ refute_lex "0B"
1913
+ refute_lex "0d"
1914
+ refute_lex "0D"
1915
+
1916
+ refute_lex "08"
1917
+ refute_lex "09"
1918
+ refute_lex "0o8"
1919
+ refute_lex "0o9"
1920
+ refute_lex "0O8"
1921
+ refute_lex "0O9"
1922
+
1923
+ refute_lex "1_e1"
1924
+ refute_lex "1_.1"
1925
+ refute_lex "1__1"
1926
+ end
1927
+
1928
+ def test_yylex_plus_unary_number
1929
+ assert_lex3("+42", nil, :tINTEGER, 42, :expr_end)
1930
+ end
1931
+
1932
+ def test_yylex_question__18
1933
+ setup_lexer_class RubyParser::V18
1934
+
1935
+ assert_lex3("?*", nil, :tINTEGER, 42, :expr_end)
1936
+ end
1937
+
1938
+ def test_yylex_question__19
1939
+ setup_lexer_class RubyParser::V19
1940
+
1941
+ assert_lex3("?*", nil, :tSTRING, "*", :expr_end)
1942
+ end
1943
+
1944
+ def test_yylex_question_bad_eos
1945
+ refute_lex "?"
1946
+ end
1947
+
1948
+ def test_yylex_question_ws
1949
+ assert_lex3("? ", nil, :tEH, "?", :expr_value)
1950
+ assert_lex3("?\n", nil, :tEH, "?", :expr_value)
1951
+ assert_lex3("?\t", nil, :tEH, "?", :expr_value)
1952
+ assert_lex3("?\v", nil, :tEH, "?", :expr_value)
1953
+ assert_lex3("?\r", nil, :tEH, "?", :expr_value)
1954
+ assert_lex3("?\f", nil, :tEH, "?", :expr_value)
1955
+ end
1956
+
1957
+ def test_yylex_question_ws_backslashed__18
1958
+ setup_lexer_class RubyParser::V18
1959
+
1960
+ assert_lex3("?\\ ", nil, :tINTEGER, 32, :expr_end)
1961
+ assert_lex3("?\\n", nil, :tINTEGER, 10, :expr_end)
1962
+ assert_lex3("?\\t", nil, :tINTEGER, 9, :expr_end)
1963
+ assert_lex3("?\\v", nil, :tINTEGER, 11, :expr_end)
1964
+ assert_lex3("?\\r", nil, :tINTEGER, 13, :expr_end)
1965
+ assert_lex3("?\\f", nil, :tINTEGER, 12, :expr_end)
1966
+ end
1967
+
1968
+ def test_yylex_question_ws_backslashed__19
1969
+ setup_lexer_class RubyParser::V19
1970
+
1971
+ assert_lex3("?\\ ", nil, :tSTRING, " ", :expr_end)
1972
+ assert_lex3("?\\n", nil, :tSTRING, "\n", :expr_end)
1973
+ assert_lex3("?\\t", nil, :tSTRING, "\t", :expr_end)
1974
+ assert_lex3("?\\v", nil, :tSTRING, "\v", :expr_end)
1975
+ assert_lex3("?\\r", nil, :tSTRING, "\r", :expr_end)
1976
+ assert_lex3("?\\f", nil, :tSTRING, "\f", :expr_end)
1977
+ end
1978
+
1979
+ def test_yylex_rbracket
1980
+ assert_lex3("]", nil, :tRBRACK, "]", :expr_endarg)
1981
+ end
1982
+
1983
+ def test_yylex_rcurly
1984
+ assert_lex("}", nil, :tRCURLY, "}", :expr_endarg, 0, 1) do
1985
+ lexer.brace_nest += 2
1986
+ end
1987
+ end
1988
+
1989
+ def test_yylex_regexp
1990
+ assert_lex3("/regexp/",
1991
+ nil,
1992
+ :tREGEXP_BEG, "/", :expr_beg,
1993
+ :tSTRING_CONTENT, "regexp", :expr_beg,
1994
+ :tREGEXP_END, "", :expr_end)
1995
+ end
1996
+
1997
+ def test_yylex_regexp_ambiguous
1998
+ assert_lex3("method /regexp/",
1999
+ nil,
2000
+ :tIDENTIFIER, "method", :expr_cmdarg,
2001
+ :tREGEXP_BEG, "/", :expr_cmdarg,
2002
+ :tSTRING_CONTENT, "regexp", :expr_cmdarg,
2003
+ :tREGEXP_END, "", :expr_end)
2004
+ end
2005
+
2006
+ def test_yylex_regexp_bad
2007
+ refute_lex("/.*/xyz",
2008
+ :tREGEXP_BEG, "/",
2009
+ :tSTRING_CONTENT, ".*")
2010
+ end
2011
+
2012
+ def test_yylex_regexp_escape_C
2013
+ assert_lex3("/regex\\C-x/",
2014
+ nil,
2015
+ :tREGEXP_BEG, "/", :expr_beg,
2016
+ :tSTRING_CONTENT, "regex\\C-x", :expr_beg,
2017
+ :tREGEXP_END, "", :expr_end)
2018
+ end
2019
+
2020
+ def test_yylex_regexp_escape_C_M
2021
+ assert_lex3("/regex\\C-\\M-x/",
2022
+ nil,
2023
+ :tREGEXP_BEG, "/", :expr_beg,
2024
+ :tSTRING_CONTENT, "regex\\C-\\M-x", :expr_beg,
2025
+ :tREGEXP_END, "", :expr_end)
2026
+ end
2027
+
2028
+ def test_yylex_regexp_escape_C_M_craaaazy
2029
+ assert_lex3("/regex\\C-\\\n\\M-x/",
2030
+ nil,
2031
+ :tREGEXP_BEG, "/", :expr_beg,
2032
+ :tSTRING_CONTENT, "regex\\C-\\M-x", :expr_beg,
2033
+ :tREGEXP_END, "", :expr_end)
2034
+ end
2035
+
2036
+ def test_yylex_regexp_escape_C_bad_dash
2037
+ refute_lex '/regex\\Cx/', :tREGEXP_BEG, "/"
2038
+ end
2039
+
2040
+ def test_yylex_regexp_escape_C_bad_dash_eos
2041
+ refute_lex '/regex\\C-/', :tREGEXP_BEG, "/"
2042
+ end
2043
+
2044
+ def test_yylex_regexp_escape_C_bad_dash_eos2
2045
+ refute_lex '/regex\\C-', :tREGEXP_BEG, "/"
2046
+ end
2047
+
2048
+ def test_yylex_regexp_escape_C_bad_eos
2049
+ refute_lex '/regex\\C/', :tREGEXP_BEG, "/"
2050
+ end
2051
+
2052
+ def test_yylex_regexp_escape_C_bad_eos2
2053
+ refute_lex '/regex\\c', :tREGEXP_BEG, "/"
2054
+ end
2055
+
2056
+ def test_yylex_regexp_escape_M
2057
+ assert_lex3("/regex\\M-x/",
2058
+ nil,
2059
+ :tREGEXP_BEG, "/", :expr_beg,
2060
+ :tSTRING_CONTENT, "regex\\M-x", :expr_beg,
2061
+ :tREGEXP_END, "", :expr_end)
2062
+ end
2063
+
2064
+ def test_yylex_regexp_escape_M_C
2065
+ assert_lex3("/regex\\M-\\C-x/",
2066
+ nil,
2067
+ :tREGEXP_BEG, "/", :expr_beg,
2068
+ :tSTRING_CONTENT, "regex\\M-\\C-x", :expr_beg,
2069
+ :tREGEXP_END, "", :expr_end)
2070
+ end
2071
+
2072
+ def test_yylex_regexp_escape_M_bad_dash
2073
+ refute_lex '/regex\\Mx/', :tREGEXP_BEG, "/"
2074
+ end
2075
+
2076
+ def test_yylex_regexp_escape_M_bad_dash_eos
2077
+ refute_lex '/regex\\M-/', :tREGEXP_BEG, "/"
2078
+ end
2079
+
2080
+ def test_yylex_regexp_escape_M_bad_dash_eos2
2081
+ refute_lex '/regex\\M-', :tREGEXP_BEG, "/"
2082
+ end
2083
+
2084
+ def test_yylex_regexp_escape_M_bad_eos
2085
+ refute_lex '/regex\\M/', :tREGEXP_BEG, "/"
2086
+ end
2087
+
2088
+ def test_yylex_regexp_escape_backslash_slash
2089
+ assert_lex3("/\\//",
2090
+ nil,
2091
+ :tREGEXP_BEG, "/", :expr_beg,
2092
+ :tSTRING_CONTENT, "\\/", :expr_beg,
2093
+ :tREGEXP_END, "", :expr_end)
2094
+ end
2095
+
2096
+ def test_yylex_regexp_escape_backslash_terminator
2097
+ assert_lex3("%r%blah\\%blah%",
2098
+ nil,
2099
+ :tREGEXP_BEG, "%r\000", :expr_beg,
2100
+ :tSTRING_CONTENT, "blah\\%blah", :expr_beg,
2101
+ :tREGEXP_END, "", :expr_end)
2102
+ end
2103
+
2104
+ def test_yylex_regexp_escaped_delim
2105
+ assert_lex3("%r!blah(?\\!blah)!",
2106
+ nil,
2107
+ :tREGEXP_BEG, "%r\000", :expr_beg,
2108
+ :tSTRING_CONTENT, "blah(?!blah)", :expr_beg,
2109
+ :tREGEXP_END, "", :expr_end)
2110
+ end
2111
+
2112
+ def test_yylex_regexp_escape_backslash_terminator_meta1
2113
+ assert_lex3("%r{blah\\}blah}",
2114
+ nil,
2115
+ :tREGEXP_BEG, "%r{", :expr_beg, # FIX ?!?
2116
+ :tSTRING_CONTENT, "blah\\}blah", :expr_beg,
2117
+ :tREGEXP_END, "", :expr_end)
2118
+ end
2119
+
2120
+ def test_yylex_regexp_escape_backslash_terminator_meta2
2121
+ assert_lex3("%r/blah\\/blah/",
2122
+ nil,
2123
+ :tREGEXP_BEG, "%r\000", :expr_beg,
2124
+ :tSTRING_CONTENT, "blah\\/blah", :expr_beg,
2125
+ :tREGEXP_END, "", :expr_end)
2126
+ end
2127
+
2128
+ def test_yylex_regexp_escape_backslash_terminator_meta3
2129
+ assert_lex3("%r/blah\\%blah/",
2130
+ nil,
2131
+ :tREGEXP_BEG, "%r\000", :expr_beg,
2132
+ :tSTRING_CONTENT, "blah\\%blah", :expr_beg,
2133
+ :tREGEXP_END, "", :expr_end)
2134
+ end
2135
+
2136
+ def test_yylex_regexp_escape_bad_eos
2137
+ refute_lex '/regex\\', :tREGEXP_BEG, "/"
2138
+ end
2139
+
2140
+ def test_yylex_regexp_escape_bs
2141
+ assert_lex3("/regex\\\\regex/",
2142
+ nil,
2143
+ :tREGEXP_BEG, "/", :expr_beg,
2144
+ :tSTRING_CONTENT, "regex\\\\regex", :expr_beg,
2145
+ :tREGEXP_END, "", :expr_end)
2146
+ end
2147
+
2148
+ def test_yylex_regexp_escape_c
2149
+ assert_lex3("/regex\\cxxx/",
2150
+ nil,
2151
+ :tREGEXP_BEG, "/", :expr_beg,
2152
+ :tSTRING_CONTENT, "regex\\cxxx", :expr_beg,
2153
+ :tREGEXP_END, "", :expr_end)
2154
+ end
2155
+
2156
+ def test_yylex_regexp_escape_c_backslash
2157
+ assert_lex3("/regex\\c\\n/",
2158
+ nil,
2159
+ :tREGEXP_BEG, "/", :expr_beg,
2160
+ :tSTRING_CONTENT, "regex\\c\\n", :expr_beg,
2161
+ :tREGEXP_END, "", :expr_end)
2162
+ end
2163
+
2164
+ def test_yylex_regexp_escape_chars
2165
+ assert_lex3("/re\\tge\\nxp/",
2166
+ nil,
2167
+ :tREGEXP_BEG, "/", :expr_beg,
2168
+ :tSTRING_CONTENT, "re\\tge\\nxp", :expr_beg,
2169
+ :tREGEXP_END, "", :expr_end)
2170
+ end
2171
+
2172
+ def test_yylex_regexp_escape_double_backslash
2173
+ regexp = '/[\\/\\\\]$/'
2174
+ assert_lex3(regexp.dup,
2175
+ nil,
2176
+ :tREGEXP_BEG, "/", :expr_beg,
2177
+ :tSTRING_CONTENT, "[\\/\\\\]$", :expr_beg,
2178
+ :tREGEXP_END, "", :expr_end)
2179
+ end
2180
+
2181
+ def test_yylex_regexp_escape_hex
2182
+ assert_lex3("/regex\\x61xp/",
2183
+ nil,
2184
+ :tREGEXP_BEG, "/", :expr_beg,
2185
+ :tSTRING_CONTENT, "regex\\x61xp", :expr_beg,
2186
+ :tREGEXP_END, "", :expr_end)
2187
+ end
2188
+
2189
+ def test_yylex_regexp_escape_hex_bad
2190
+ refute_lex '/regex\\xzxp/', :tREGEXP_BEG, "/"
2191
+ end
2192
+
2193
+ def test_yylex_regexp_escape_hex_one
2194
+ assert_lex3("/^[\\xd\\xa]{2}/on",
2195
+ nil,
2196
+ :tREGEXP_BEG, "/", :expr_beg,
2197
+ :tSTRING_CONTENT, "^[\\xd\\xa]{2}", :expr_beg,
2198
+ :tREGEXP_END, "on", :expr_end)
2199
+ end
2200
+
2201
+ def test_yylex_regexp_escape_oct1
2202
+ assert_lex3("/regex\\0xp/",
2203
+ nil,
2204
+ :tREGEXP_BEG, "/", :expr_beg,
2205
+ :tSTRING_CONTENT, "regex\\0xp", :expr_beg,
2206
+ :tREGEXP_END, "", :expr_end)
2207
+ end
2208
+
2209
+ def test_yylex_regexp_escape_oct2
2210
+ assert_lex3("/regex\\07xp/",
2211
+ nil,
2212
+ :tREGEXP_BEG, "/", :expr_beg,
2213
+ :tSTRING_CONTENT, "regex\\07xp", :expr_beg,
2214
+ :tREGEXP_END, "", :expr_end)
2215
+ end
2216
+
2217
+ def test_yylex_regexp_escape_oct3
2218
+ assert_lex3("/regex\\10142/",
2219
+ nil,
2220
+ :tREGEXP_BEG, "/", :expr_beg,
2221
+ :tSTRING_CONTENT, "regex\\10142", :expr_beg,
2222
+ :tREGEXP_END, "", :expr_end)
2223
+ end
2224
+
2225
+ def test_yylex_regexp_escape_return
2226
+ assert_lex3("/regex\\\nregex/",
2227
+ nil,
2228
+ :tREGEXP_BEG, "/", :expr_beg,
2229
+ :tSTRING_CONTENT, "regexregex", :expr_beg,
2230
+ :tREGEXP_END, "", :expr_end)
2231
+ end
2232
+
2233
+ def test_yylex_regexp_nm
2234
+ assert_lex3("/.*/nm",
2235
+ nil,
2236
+ :tREGEXP_BEG, "/", :expr_beg,
2237
+ :tSTRING_CONTENT, ".*", :expr_beg,
2238
+ :tREGEXP_END, "nm", :expr_end)
2239
+ end
2240
+
2241
+ def test_yylex_rparen
2242
+ assert_lex3(")", nil, :tRPAREN, ")", :expr_endfn)
2243
+ end
2244
+
2245
+ def test_yylex_rshft
2246
+ assert_lex3("a >> 2",
2247
+ nil,
2248
+ :tIDENTIFIER, "a", :expr_cmdarg,
2249
+ :tRSHFT, ">>", :expr_beg,
2250
+ :tINTEGER, 2, :expr_end)
2251
+ end
2252
+
2253
+ def test_yylex_rshft_equals
2254
+ assert_lex3("a >>= 2",
2255
+ nil,
2256
+ :tIDENTIFIER, "a", :expr_cmdarg,
2257
+ :tOP_ASGN, ">>", :expr_beg,
2258
+ :tINTEGER, 2, :expr_end)
2259
+ end
2260
+
2261
+ def test_yylex_star
2262
+ assert_lex3("a * ",
2263
+ nil,
2264
+ :tIDENTIFIER, "a", :expr_cmdarg,
2265
+ :tSTAR2, "*", :expr_beg)
2266
+ end
2267
+
2268
+ def test_yylex_star2
2269
+ assert_lex3("a ** ",
2270
+ nil,
2271
+ :tIDENTIFIER, "a", :expr_cmdarg,
2272
+ :tPOW, "**", :expr_beg)
2273
+ end
2274
+
2275
+ def test_yylex_star2_equals
2276
+ assert_lex3("a **= ",
2277
+ nil,
2278
+ :tIDENTIFIER, "a", :expr_cmdarg,
2279
+ :tOP_ASGN, "**", :expr_beg)
2280
+ end
2281
+
2282
+ def test_yylex_star_arg
2283
+ self.lex_state = :expr_arg
2284
+
2285
+ assert_lex3(" *a",
2286
+ nil,
2287
+ :tSTAR, "*", :expr_beg,
2288
+ :tIDENTIFIER, "a", :expr_arg)
2289
+ end
2290
+
2291
+ def test_yylex_star_arg_beg
2292
+ self.lex_state = :expr_beg
2293
+
2294
+ assert_lex3("*a",
2295
+ nil,
2296
+ :tSTAR, "*", :expr_beg,
2297
+ :tIDENTIFIER, "a", :expr_arg)
2298
+ end
2299
+
2300
+ def test_yylex_star_arg_beg_fname
2301
+ self.lex_state = :expr_fname
2302
+
2303
+ assert_lex3("*a",
2304
+ nil,
2305
+ :tSTAR2, "*", :expr_arg,
2306
+ :tIDENTIFIER, "a", :expr_arg)
2307
+ end
2308
+
2309
+ def test_yylex_star_arg_beg_fname2
2310
+ self.lex_state = :expr_fname
2311
+
2312
+ assert_lex3("*a",
2313
+ nil,
2314
+ :tSTAR2, "*", :expr_arg,
2315
+ :tIDENTIFIER, "a", :expr_arg)
2316
+ end
2317
+
2318
+ def test_yylex_star_equals
2319
+ assert_lex3("a *= ",
2320
+ nil,
2321
+ :tIDENTIFIER, "a", :expr_cmdarg,
2322
+ :tOP_ASGN, "*", :expr_beg)
2323
+ end
2324
+
2325
+ def test_yylex_string_bad_eos
2326
+ refute_lex('%', :tSTRING_BEG, '%')
2327
+ end
2328
+
2329
+ def test_yylex_string_bad_eos_quote
2330
+ refute_lex('%{nest', :tSTRING_BEG, '%}')
2331
+ end
2332
+
2333
+ def test_yylex_string_double
2334
+ assert_lex3("\"string\"", nil, :tSTRING, "string", :expr_end)
2335
+ end
2336
+
2337
+ def test_yylex_string_double_escape_C
2338
+ assert_lex3("\"\\C-a\"", nil, :tSTRING, "\001", :expr_end)
2339
+ end
2340
+
2341
+ def test_yylex_string_double_escape_C_backslash
2342
+ assert_lex3("\"\\C-\\\\\"",
2343
+ nil,
2344
+ :tSTRING_BEG, "\"", :expr_beg,
2345
+ :tSTRING_CONTENT, "\034", :expr_beg,
2346
+ :tSTRING_END, "\"", :expr_end)
2347
+ end
2348
+
2349
+ def test_yylex_string_double_escape_C_escape
2350
+ assert_lex3("\"\\C-\\M-a\"",
2351
+ nil,
2352
+ :tSTRING_BEG, "\"", :expr_beg,
2353
+ :tSTRING_CONTENT, "\201", :expr_beg,
2354
+ :tSTRING_END, "\"", :expr_end)
2355
+ end
2356
+
2357
+ def test_yylex_string_double_escape_C_question
2358
+ assert_lex3("\"\\C-?\"", nil, :tSTRING, "\177", :expr_end)
2359
+ end
2360
+
2361
+ def test_yylex_string_utf8_simple
2362
+ chr = [0x3024].pack("U")
2363
+
2364
+ assert_lex3('"\u{3024}"',
2365
+ s(:str, chr),
2366
+ :tSTRING, chr, :expr_end)
2367
+ end
2368
+
2369
+ def test_yylex_string_utf8_complex
2370
+ chr = [0x3024].pack("U")
2371
+
2372
+ assert_lex3('"#@a\u{3024}"',
2373
+ s(:dstr, "", s(:evstr, s(:ivar, :@a)), s(:str, chr)),
2374
+ :tSTRING_BEG, '"', :expr_beg,
2375
+ :tSTRING_DVAR, nil, :expr_beg,
2376
+ :tSTRING_CONTENT, "@a"+chr, :expr_beg,
2377
+ :tSTRING_END, '"', :expr_end)
2378
+ end
2379
+
2380
+ def test_yylex_string_double_escape_M
2381
+ chr = "\341"
2382
+ chr.force_encoding("UTF-8") if RubyLexer::HAS_ENC
2383
+
2384
+ assert_lex3("\"\\M-a\"", nil, :tSTRING, chr, :expr_end)
2385
+ end
2386
+
2387
+ def test_why_does_ruby_hate_me?
2388
+ assert_lex3("\"Nl%\\000\\000A\\000\\999\"", # you should be ashamed
2389
+ nil,
2390
+ :tSTRING, ["Nl%","\x00","\x00","A","\x00","999"].join, :expr_end)
2391
+ end
2392
+
2393
+ def test_yylex_string_double_escape_M_backslash
2394
+ assert_lex3("\"\\M-\\\\\"",
2395
+ nil,
2396
+ :tSTRING_BEG, "\"", :expr_beg,
2397
+ :tSTRING_CONTENT, "\334", :expr_beg,
2398
+ :tSTRING_END, "\"", :expr_end)
2399
+ end
2400
+
2401
+ def test_yylex_string_double_escape_M_escape
2402
+ assert_lex3("\"\\M-\\C-a\"",
2403
+ nil,
2404
+ :tSTRING_BEG, "\"", :expr_beg,
2405
+ :tSTRING_CONTENT, "\201", :expr_beg,
2406
+ :tSTRING_END, "\"", :expr_end)
2407
+ end
2408
+
2409
+ def test_yylex_string_double_escape_bs1
2410
+ assert_lex3("\"a\\a\\a\"", nil, :tSTRING, "a\a\a", :expr_end)
2411
+ end
2412
+
2413
+ def test_yylex_string_double_escape_bs2
2414
+ assert_lex3("\"a\\\\a\"", nil, :tSTRING, "a\\a", :expr_end)
2415
+ end
2416
+
2417
+ def test_yylex_string_double_escape_c
2418
+ assert_lex3("\"\\ca\"", nil, :tSTRING, "\001", :expr_end)
2419
+ end
2420
+
2421
+ def test_yylex_string_double_escape_c_backslash
2422
+ assert_lex3("\"\\c\\\"",
2423
+ nil,
2424
+ :tSTRING_BEG, "\"", :expr_beg,
2425
+ :tSTRING_CONTENT, "\034", :expr_beg,
2426
+ :tSTRING_END, "\"", :expr_end)
2427
+ end
2428
+
2429
+ def test_yylex_string_double_escape_c_escape
2430
+ assert_lex3("\"\\c\\M-a\"",
2431
+ nil,
2432
+ :tSTRING_BEG, "\"", :expr_beg,
2433
+ :tSTRING_CONTENT, "\201", :expr_beg,
2434
+ :tSTRING_END, "\"", :expr_end)
2435
+ end
2436
+
2437
+ def test_yylex_string_double_escape_c_question
2438
+ assert_lex3("\"\\c?\"", nil, :tSTRING, "\177", :expr_end)
2439
+ end
2440
+
2441
+ def test_yylex_string_double_escape_chars
2442
+ assert_lex3("\"s\\tri\\ng\"", nil, :tSTRING, "s\tri\ng", :expr_end)
2443
+ end
2444
+
2445
+ def test_yylex_string_double_escape_hex
2446
+ assert_lex3("\"n = \\x61\\x62\\x63\"", nil, :tSTRING, "n = abc", :expr_end)
2447
+ end
2448
+
2449
+ def test_yylex_string_double_escape_octal
2450
+ assert_lex3("\"n = \\101\\102\\103\"", nil, :tSTRING, "n = ABC", :expr_end)
2451
+ end
2452
+
2453
+ def test_yylex_string_double_escape_octal_fucked
2454
+ assert_lex3("\"n = \\444\"", nil, :tSTRING, "n = $", :expr_end)
2455
+ end
2456
+
2457
+ def test_yylex_string_double_interp
2458
+ assert_lex3("\"blah #x a \#@a b \#$b c \#{3} # \"",
2459
+ nil,
2460
+ :tSTRING_BEG, "\"", :expr_beg,
2461
+ :tSTRING_CONTENT, "blah #x a ", :expr_beg,
2462
+ :tSTRING_DVAR, nil, :expr_beg,
2463
+ :tSTRING_CONTENT, "@a b ", :expr_beg,
2464
+ :tSTRING_DVAR, nil, :expr_beg,
2465
+ :tSTRING_CONTENT, "$b c ", :expr_beg,
2466
+ :tSTRING_DBEG, nil, :expr_beg,
2467
+ :tSTRING_CONTENT, "3} # ", :expr_beg,
2468
+ :tSTRING_END, "\"", :expr_end)
2469
+ end
2470
+
2471
+ def test_yylex_string_double_pound_dollar_bad
2472
+ skip if ruby18
2473
+
2474
+ assert_lex3('"#$%"', nil,
2475
+
2476
+ :tSTRING_BEG, "\"", :expr_beg,
2477
+ :tSTRING_CONTENT, '#$%', :expr_beg,
2478
+ :tSTRING_END, "\"", :expr_end)
2479
+ end
2480
+
2481
+ def test_yylex_string_double_nested_curlies
2482
+ assert_lex3("%{nest{one{two}one}nest}",
2483
+ nil,
2484
+ :tSTRING_BEG, "%}", :expr_beg,
2485
+ :tSTRING_CONTENT, "nest{one{two}one}nest", :expr_beg,
2486
+ :tSTRING_END, "}", :expr_end)
2487
+ end
2488
+
2489
+ def test_yylex_string_double_no_interp
2490
+ assert_lex3("\"# blah\"", nil, :tSTRING, "# blah", :expr_end)
2491
+ assert_lex3("\"blah # blah\"", nil, :tSTRING, "blah # blah", :expr_end)
2492
+ end
2493
+
2494
+ def test_yylex_string_escape_x_single
2495
+ assert_lex3("\"\\x0\"", nil, :tSTRING, "\000", :expr_end)
2496
+ end
2497
+
2498
+ def test_yylex_string_pct_i
2499
+ assert_lex3("%i[s1 s2\ns3]",
2500
+ nil,
2501
+ :tQSYMBOLS_BEG, "%i[", :expr_beg,
2502
+ :tSTRING_CONTENT, "s1", :expr_beg,
2503
+ :tSPACE, nil, :expr_beg,
2504
+ :tSTRING_CONTENT, "s2", :expr_beg,
2505
+ :tSPACE, nil, :expr_beg,
2506
+ :tSTRING_CONTENT, "s3", :expr_beg,
2507
+ :tSPACE, nil, :expr_beg,
2508
+ :tSTRING_END, nil, :expr_end)
2509
+ end
2510
+
2511
+ def test_yylex_string_pct_I
2512
+ assert_lex3("%I[s1 s2\ns3]",
2513
+ nil,
2514
+ :tSYMBOLS_BEG, "%I[", :expr_beg,
2515
+ :tSTRING_CONTENT, "s1", :expr_beg,
2516
+ :tSPACE, nil, :expr_beg,
2517
+ :tSTRING_CONTENT, "s2", :expr_beg,
2518
+ :tSPACE, nil, :expr_beg,
2519
+ :tSTRING_CONTENT, "s3", :expr_beg,
2520
+ :tSPACE, nil, :expr_beg,
2521
+ :tSTRING_END, nil, :expr_end)
2522
+ end
2523
+
2524
+ def test_yylex_string_pct_i_extra_space
2525
+ assert_lex3("%i[ s1 s2\ns3 ]",
2526
+ nil,
2527
+ :tQSYMBOLS_BEG, "%i[", :expr_beg,
2528
+ :tSTRING_CONTENT, "s1", :expr_beg,
2529
+ :tSPACE, nil, :expr_beg,
2530
+ :tSTRING_CONTENT, "s2", :expr_beg,
2531
+ :tSPACE, nil, :expr_beg,
2532
+ :tSTRING_CONTENT, "s3", :expr_beg,
2533
+ :tSPACE, nil, :expr_beg,
2534
+ :tSTRING_END, nil, :expr_end)
2535
+ end
2536
+
2537
+ def test_yylex_string_pct_I_extra_space
2538
+ assert_lex3("%I[ s1 s2\ns3 ]",
2539
+ nil,
2540
+ :tSYMBOLS_BEG, "%I[", :expr_beg,
2541
+ :tSTRING_CONTENT, "s1", :expr_beg,
2542
+ :tSPACE, nil, :expr_beg,
2543
+ :tSTRING_CONTENT, "s2", :expr_beg,
2544
+ :tSPACE, nil, :expr_beg,
2545
+ :tSTRING_CONTENT, "s3", :expr_beg,
2546
+ :tSPACE, nil, :expr_beg,
2547
+ :tSTRING_END, nil, :expr_end)
2548
+ end
2549
+
2550
+ def test_yylex_string_pct_q
2551
+ assert_lex3("%q[s1 s2]",
2552
+ nil,
2553
+ :tSTRING_BEG, "%q[", :expr_beg,
2554
+ :tSTRING_CONTENT, "s1 s2", :expr_beg,
2555
+ :tSTRING_END, "]", :expr_end)
2556
+ end
2557
+
2558
+ def test_yylex_string_pct_Q
2559
+ assert_lex3("%Q[s1 s2]",
2560
+ nil,
2561
+ :tSTRING_BEG, "%Q[", :expr_beg,
2562
+ :tSTRING_CONTENT, "s1 s2", :expr_beg,
2563
+ :tSTRING_END, "]", :expr_end)
2564
+ end
2565
+
2566
+ def test_yylex_string_pct_W
2567
+ assert_lex3("%W[s1 s2\ns3]", # TODO: add interpolation to these
2568
+ nil,
2569
+ :tWORDS_BEG, "%W[", :expr_beg,
2570
+ :tSTRING_CONTENT, "s1", :expr_beg,
2571
+ :tSPACE, nil, :expr_beg,
2572
+ :tSTRING_CONTENT, "s2", :expr_beg,
2573
+ :tSPACE, nil, :expr_beg,
2574
+ :tSTRING_CONTENT, "s3", :expr_beg,
2575
+ :tSPACE, nil, :expr_beg,
2576
+ :tSTRING_END, nil, :expr_end)
2577
+ end
2578
+
2579
+ def test_yylex_string_pct_W_bs_nl
2580
+ assert_lex3("%W[s1 \\\ns2]", # TODO: add interpolation to these
2581
+ nil,
2582
+ :tWORDS_BEG, "%W[", :expr_beg,
2583
+ :tSTRING_CONTENT, "s1", :expr_beg,
2584
+ :tSPACE, nil, :expr_beg,
2585
+ :tSTRING_CONTENT, "\ns2", :expr_beg,
2586
+ :tSPACE, nil, :expr_beg,
2587
+ :tSTRING_END, nil, :expr_end)
2588
+ end
2589
+
2590
+ def test_yylex_string_pct_angle
2591
+ assert_lex3("%<blah>",
2592
+ nil,
2593
+ :tSTRING_BEG, "%>", :expr_beg,
2594
+ :tSTRING_CONTENT, "blah", :expr_beg,
2595
+ :tSTRING_END, ">", :expr_end)
2596
+ end
2597
+
2598
+ def test_yylex_string_pct_other
2599
+ assert_lex3("%%blah%",
2600
+ nil,
2601
+ :tSTRING_BEG, "%%", :expr_beg,
2602
+ :tSTRING_CONTENT, "blah", :expr_beg,
2603
+ :tSTRING_END, "%", :expr_end)
2604
+ end
2605
+
2606
+ def test_yylex_string_pct_w
2607
+ refute_lex("%w[s1 s2 ",
2608
+ :tQWORDS_BEG, "%w[",
2609
+ :tSTRING_CONTENT, "s1",
2610
+ :tSPACE, nil,
2611
+ :tSTRING_CONTENT, "s2",
2612
+ :tSPACE, nil)
2613
+ end
2614
+
2615
+ def test_yylex_string_pct_w_bs_nl
2616
+ assert_lex3("%w[s1 \\\ns2]",
2617
+ nil,
2618
+ :tQWORDS_BEG, "%w[", :expr_beg,
2619
+ :tSTRING_CONTENT, "s1", :expr_beg,
2620
+ :tSPACE, nil, :expr_beg,
2621
+ :tSTRING_CONTENT, "\ns2", :expr_beg,
2622
+ :tSPACE, nil, :expr_beg,
2623
+ :tSTRING_END, nil, :expr_end)
2624
+ end
2625
+
2626
+ def test_yylex_string_pct_w_bs_sp
2627
+ assert_lex3("%w[s\\ 1 s\\ 2]",
2628
+ nil,
2629
+ :tQWORDS_BEG, "%w[", :expr_beg,
2630
+ :tSTRING_CONTENT, "s 1", :expr_beg,
2631
+ :tSPACE, nil, :expr_beg,
2632
+ :tSTRING_CONTENT, "s 2", :expr_beg,
2633
+ :tSPACE, nil, :expr_beg,
2634
+ :tSTRING_END, nil, :expr_end)
2635
+ end
2636
+
2637
+ def test_yylex_string_single
2638
+ assert_lex3("'string'", nil, :tSTRING, "string", :expr_end)
2639
+ end
2640
+
2641
+ def test_yylex_string_single_escape_chars
2642
+ assert_lex3("'s\\tri\\ng'", nil, :tSTRING, "s\\tri\\ng", :expr_end)
2643
+ end
2644
+
2645
+ def test_yylex_string_single_nl
2646
+ assert_lex3("'blah\\\nblah'", nil, :tSTRING, "blah\\\nblah", :expr_end)
2647
+ end
2648
+
2649
+ def test_yylex_string_single_escaped_quote
2650
+ assert_lex3("'foo\\'bar'", nil, :tSTRING, "foo'bar", :expr_end)
2651
+ end
2652
+
2653
+ def test_yylex_symbol
2654
+ assert_lex3(":symbol", nil, :tSYMBOL, "symbol", :expr_end)
2655
+ end
2656
+
2657
+ def test_yylex_symbol_zero_byte__18
2658
+ setup_lexer_class RubyParser::V18
2659
+
2660
+ refute_lex(":\"symbol\0\"", :tSYMBEG, ":")
2661
+ end
2662
+
2663
+ def test_yylex_symbol_zero_byte
2664
+ assert_lex(":\"symbol\0\"", nil,
2665
+ :tSYMBOL, "symbol\0", :expr_end)
2666
+ end
2667
+
2668
+ def test_yylex_symbol_double
2669
+ assert_lex3(":\"symbol\"",
2670
+ nil,
2671
+ :tSYMBOL, "symbol", :expr_end)
2672
+ end
2673
+
2674
+ def test_yylex_symbol_double_interp
2675
+ assert_lex3(':"symbol#{1+1}"',
2676
+ nil,
2677
+ :tSYMBEG, ":", :expr_fname,
2678
+ :tSTRING_CONTENT, "symbol", :expr_fname,
2679
+ :tSTRING_DBEG, nil, :expr_fname,
2680
+ :tSTRING_CONTENT, "1+1}", :expr_fname, # HUH? this is BS
2681
+ :tSTRING_END, "\"", :expr_end)
2682
+ end
2683
+
2684
+ def test_yylex_symbol_single
2685
+ assert_lex3(":'symbol'",
2686
+ nil,
2687
+ :tSYMBOL, "symbol", :expr_end)
2688
+ end
2689
+
2690
+ def test_yylex_symbol_single_noninterp
2691
+ assert_lex3(':\'symbol#{1+1}\'',
2692
+ nil,
2693
+ :tSYMBOL, 'symbol#{1+1}', :expr_end)
2694
+ end
2695
+
2696
+ def test_yylex_symbol_single_escape_chars
2697
+ assert_lex3(":'s\\tri\\ng'",
2698
+ nil,
2699
+ :tSYMBOL, "s\\tri\\ng", :expr_end)
2700
+ end
2701
+
2702
+ def test_yylex_string_single_escape_quote_and_backslash
2703
+ assert_lex3(":'foo\\'bar\\\\baz'", nil, :tSYMBOL, "foo'bar\\baz", :expr_end)
2704
+ end
2705
+
2706
+ def test_yylex_ternary1
2707
+ assert_lex3("a ? b : c",
2708
+ nil,
2709
+ :tIDENTIFIER, "a", :expr_cmdarg,
2710
+ :tEH, "?", :expr_value,
2711
+ :tIDENTIFIER, "b", :expr_arg,
2712
+ :tCOLON, ":", :expr_beg,
2713
+ :tIDENTIFIER, "c", :expr_arg)
2714
+
2715
+ assert_lex3("a ?bb : c", # GAH! MATZ!!!
2716
+ nil,
2717
+ :tIDENTIFIER, "a", :expr_cmdarg,
2718
+ :tEH, "?", :expr_beg,
2719
+ :tIDENTIFIER, "bb", :expr_arg,
2720
+ :tCOLON, ":", :expr_beg,
2721
+ :tIDENTIFIER, "c", :expr_arg)
2722
+
2723
+ assert_lex3("42 ?",
2724
+ nil,
2725
+ :tINTEGER, 42, :expr_end,
2726
+ :tEH, "?", :expr_value)
2727
+ end
2728
+
2729
+ def test_yylex_tilde
2730
+ assert_lex3("~", nil, :tTILDE, "~", :expr_beg)
2731
+ end
2732
+
2733
+ def test_yylex_tilde_unary
2734
+ self.lex_state = :expr_fname
2735
+
2736
+ assert_lex3("~@", nil, :tTILDE, "~", :expr_arg)
2737
+ end
2738
+
2739
+ def test_yylex_uminus
2740
+ assert_lex3("-blah",
2741
+ nil,
2742
+ :tUMINUS, "-", :expr_beg,
2743
+ :tIDENTIFIER, "blah", :expr_arg)
2744
+ end
2745
+
2746
+ def test_yylex_underscore
2747
+ assert_lex3("_var", nil, :tIDENTIFIER, "_var", :expr_cmdarg)
2748
+ end
2749
+
2750
+ def test_yylex_underscore_end
2751
+ assert_lex3("__END__\n",
2752
+ nil,
2753
+ RubyLexer::EOF, RubyLexer::EOF, nil)
2754
+ end
2755
+
2756
+ def test_yylex_uplus
2757
+ assert_lex3("+blah",
2758
+ nil,
2759
+ :tUPLUS, "+", :expr_beg,
2760
+ :tIDENTIFIER, "blah", :expr_arg)
2761
+ end
2762
+
2763
+ def test_zbug_float_in_decl
2764
+ assert_lex3("def initialize(u = 0.0, s = 0.0",
2765
+ nil,
2766
+ :kDEF, "def", :expr_fname,
2767
+ :tIDENTIFIER, "initialize", :expr_endfn,
2768
+ :tLPAREN2, "(", :expr_beg,
2769
+ :tIDENTIFIER, "u", :expr_arg,
2770
+ :tEQL, "=", :expr_beg,
2771
+ :tFLOAT, 0.0, :expr_end,
2772
+ :tCOMMA, ",", :expr_beg,
2773
+ :tIDENTIFIER, "s", :expr_arg,
2774
+ :tEQL, "=", :expr_beg,
2775
+ :tFLOAT, 0.0, :expr_end)
2776
+ end
2777
+
2778
+ def test_zbug_id_equals
2779
+ assert_lex3("a = 0.0",
2780
+ nil,
2781
+ :tIDENTIFIER, "a", :expr_cmdarg,
2782
+ :tEQL, "=", :expr_beg,
2783
+ :tFLOAT, 0.0, :expr_end)
2784
+ end
2785
+
2786
+ def test_zbug_no_spaces_in_decl
2787
+ assert_lex3("def initialize(u=0.0,s=0.0",
2788
+ nil,
2789
+ :kDEF, "def", :expr_fname,
2790
+ :tIDENTIFIER, "initialize", :expr_endfn,
2791
+ :tLPAREN2, "(", :expr_beg,
2792
+ :tIDENTIFIER, "u", :expr_arg,
2793
+ :tEQL, "=", :expr_beg,
2794
+ :tFLOAT, 0.0, :expr_end,
2795
+ :tCOMMA, ",", :expr_beg,
2796
+ :tIDENTIFIER, "s", :expr_arg,
2797
+ :tEQL, "=", :expr_beg,
2798
+ :tFLOAT, 0.0, :expr_end)
2799
+ end
2800
+
2801
+ def test_pct_w_backslashes
2802
+ ["\t", "\n", "\r", "\v", "\f"].each do |char|
2803
+ next if !RubyLexer::HAS_ENC and char == "\v"
2804
+
2805
+ assert_lex("%w[foo#{char}bar]",
2806
+ s(:array, s(:str, "foo"), s(:str, "bar")),
2807
+
2808
+ :tQWORDS_BEG, "%w[", :expr_beg, 0, 0,
2809
+ :tSTRING_CONTENT, "foo", :expr_beg, 0, 0,
2810
+ :tSPACE, nil, :expr_beg, 0, 0,
2811
+ :tSTRING_CONTENT, "bar", :expr_beg, 0, 0,
2812
+ :tSPACE, nil, :expr_beg, 0, 0,
2813
+ :tSTRING_END, nil, :expr_end, 0, 0)
2814
+ end
2815
+ end
2816
+
2817
+ def test_yylex_sym_quoted
2818
+ assert_lex(":'a'",
2819
+ s(:lit, :a),
2820
+
2821
+ :tSYMBOL, "a", :expr_end, 0, 0)
2822
+ end
2823
+
2824
+ def test_yylex_hash_colon
2825
+ assert_lex("{a:1}",
2826
+ s(:hash, s(:lit, :a), s(:lit, 1)),
2827
+
2828
+ :tLBRACE, "{", :expr_beg, 0, 1,
2829
+ :tLABEL, "a", :expr_labeled, 0, 1,
2830
+ :tINTEGER, 1, :expr_end, 0, 1,
2831
+ :tRCURLY, "}", :expr_endarg, 0, 0)
2832
+ end
2833
+
2834
+ def test_yylex_hash_colon_quoted_22
2835
+ setup_lexer_class RubyParser::V22
2836
+
2837
+ assert_lex("{'a':1}",
2838
+ s(:hash, s(:lit, :a), s(:lit, 1)),
2839
+
2840
+ :tLBRACE, "{", :expr_beg, 0, 1,
2841
+ :tLABEL, "a", :expr_labeled, 0, 1,
2842
+ :tINTEGER, 1, :expr_end, 0, 1,
2843
+ :tRCURLY, "}", :expr_endarg, 0, 0)
2844
+ end
2845
+
2846
+ def test_yylex_hash_colon_quoted_symbol_22
2847
+ setup_lexer_class RubyParser::V22
2848
+
2849
+ assert_lex("{'abc': :b}",
2850
+ s(:hash, s(:lit, :abc), s(:lit, :b)),
2851
+
2852
+ :tLBRACE, "{", :expr_beg, 0, 1,
2853
+ :tLABEL, "abc", :expr_labeled, 0, 1,
2854
+ :tSYMBOL, "b", :expr_end, 0, 1,
2855
+ :tRCURLY, "}", :expr_endarg, 0, 0)
2856
+ end
2857
+
2858
+ def test_yylex_hash_colon_double_quoted_symbol_22
2859
+ setup_lexer_class RubyParser::V22
2860
+
2861
+ assert_lex('{"abc": :b}',
2862
+ s(:hash, s(:lit, :abc), s(:lit, :b)),
2863
+
2864
+ :tLBRACE, "{", :expr_beg, 0, 1,
2865
+ :tLABEL, "abc", :expr_labeled, 0, 1,
2866
+ :tSYMBOL, "b", :expr_end, 0, 1,
2867
+ :tRCURLY, "}", :expr_endarg, 0, 0)
2868
+ end
2869
+
2870
+ def test_yylex_required_kwarg_no_value_22
2871
+ setup_lexer_class RubyParser::V22
2872
+
2873
+ assert_lex3("def foo a:, b:\nend",
2874
+ nil,
2875
+ :kDEF, "def", :expr_fname,
2876
+ :tIDENTIFIER, "foo", :expr_endfn,
2877
+ :tLABEL, "a", :expr_labeled,
2878
+ :tCOMMA, ",", :expr_beg,
2879
+ :tLABEL, "b", :expr_labeled,
2880
+ :kEND, "end", :expr_end)
2881
+ end
2882
+
2883
+ def test_yylex_hash_colon_double_quoted_with_escapes
2884
+ setup_lexer_class RubyParser::V22
2885
+
2886
+ assert_lex3("{\"s\\tr\\i\\ng\\\\foo\\'bar\":1}",
2887
+ nil,
2888
+
2889
+ :tLBRACE, "{", :expr_beg,
2890
+ :tLABEL, "s\tr\i\ng\\foo'bar", :expr_labeled,
2891
+ :tINTEGER, 1, :expr_end,
2892
+ :tRCURLY, "}", :expr_endarg)
2893
+ end
2894
+
2895
+ def test_yylex_hash_colon_quoted_with_escapes
2896
+ setup_lexer_class RubyParser::V22
2897
+
2898
+ assert_lex3("{'s\\tr\\i\\ng\\\\foo\\'bar':1}",
2899
+ nil,
2900
+
2901
+ :tLBRACE, "{", :expr_beg,
2902
+ :tLABEL, "s\\tr\\i\\ng\\foo'bar", :expr_labeled,
2903
+ :tINTEGER, 1, :expr_end,
2904
+ :tRCURLY, "}", :expr_endarg)
2905
+ end
2906
+
2907
+ def test_ruby21_rational_literal
2908
+ setup_lexer_class RubyParser::V21
2909
+
2910
+ assert_lex3("10r", nil, :tRATIONAL, Rational(10), :expr_end)
2911
+ assert_lex3("0x10r", nil, :tRATIONAL, Rational(16), :expr_end)
2912
+ assert_lex3("0o10r", nil, :tRATIONAL, Rational(8), :expr_end)
2913
+ assert_lex3("0or", nil, :tRATIONAL, Rational(0), :expr_end)
2914
+ assert_lex3("0b10r", nil, :tRATIONAL, Rational(2), :expr_end)
2915
+ assert_lex3("1.5r", nil, :tRATIONAL, Rational(15, 10), :expr_end)
2916
+ assert_lex3("15e3r", nil, :tRATIONAL, Rational(15000), :expr_end)
2917
+ assert_lex3("15e-3r", nil, :tRATIONAL, Rational(15, 1000), :expr_end)
2918
+ assert_lex3("1.5e3r", nil, :tRATIONAL, Rational(1500), :expr_end)
2919
+ assert_lex3("1.5e-3r", nil, :tRATIONAL, Rational(15, 10000), :expr_end)
2920
+
2921
+ assert_lex3("-10r", nil,
2922
+ :tUMINUS_NUM, "-", :expr_beg,
2923
+ :tRATIONAL, Rational(10), :expr_end)
2924
+ end
2925
+
2926
+ def test_ruby21_imaginary_literal
2927
+ setup_lexer_class RubyParser::V21
2928
+
2929
+ assert_lex3("1i", nil, :tIMAGINARY, Complex(0, 1), :expr_end)
2930
+ assert_lex3("0x10i", nil, :tIMAGINARY, Complex(0, 16), :expr_end)
2931
+ assert_lex3("0o10i", nil, :tIMAGINARY, Complex(0, 8), :expr_end)
2932
+ assert_lex3("0oi", nil, :tIMAGINARY, Complex(0, 0), :expr_end)
2933
+ assert_lex3("0b10i", nil, :tIMAGINARY, Complex(0, 2), :expr_end)
2934
+ assert_lex3("1.5i", nil, :tIMAGINARY, Complex(0, 1.5), :expr_end)
2935
+ assert_lex3("15e3i", nil, :tIMAGINARY, Complex(0, 15000), :expr_end)
2936
+ assert_lex3("15e-3i", nil, :tIMAGINARY, Complex(0, 0.015), :expr_end)
2937
+ assert_lex3("1.5e3i", nil, :tIMAGINARY, Complex(0, 1500), :expr_end)
2938
+ assert_lex3("1.5e-3i", nil, :tIMAGINARY, Complex(0, 0.0015), :expr_end)
2939
+
2940
+ assert_lex3("-10i", nil,
2941
+ :tUMINUS_NUM, "-", :expr_beg,
2942
+ :tIMAGINARY, Complex(0, 10), :expr_end)
2943
+ end
2944
+
2945
+ def test_ruby21_rational_imaginary_literal
2946
+ setup_lexer_class RubyParser::V21
2947
+
2948
+ assert_lex3("1ri", nil, :tIMAGINARY, Complex(0, Rational(1)), :expr_end)
2949
+ assert_lex3("0x10ri", nil, :tIMAGINARY, Complex(0, Rational(16)), :expr_end)
2950
+ assert_lex3("0o10ri", nil, :tIMAGINARY, Complex(0, Rational(8)), :expr_end)
2951
+ assert_lex3("0ori", nil, :tIMAGINARY, Complex(0, Rational(0)), :expr_end)
2952
+ assert_lex3("0b10ri", nil, :tIMAGINARY, Complex(0, Rational(2)), :expr_end)
2953
+ assert_lex3("1.5ri", nil, :tIMAGINARY, Complex(0, Rational("1.5")), :expr_end)
2954
+ assert_lex3("15e3ri", nil, :tIMAGINARY, Complex(0, Rational("15e3")), :expr_end)
2955
+ assert_lex3("15e-3ri", nil, :tIMAGINARY, Complex(0, Rational("15e-3")), :expr_end)
2956
+ assert_lex3("1.5e3ri", nil, :tIMAGINARY, Complex(0, Rational("1.5e3")), :expr_end)
2957
+ assert_lex3("1.5e-3ri", nil, :tIMAGINARY, Complex(0, Rational("1.5e-3")), :expr_end)
2958
+
2959
+ assert_lex3("-10ri", nil,
2960
+ :tUMINUS_NUM, "-", :expr_beg,
2961
+ :tIMAGINARY, Complex(0, Rational(10)), :expr_end)
2962
+ end
2963
+
2964
+ def test_ruby21_imaginary_literal_with_succeeding_keyword
2965
+ skip "Currently does not tokenize correctly"
2966
+
2967
+ setup_lexer_class RubyParser::V21
2968
+
2969
+ assert_lex3("1if", nil,
2970
+ :tINTEGER, 1, :expr_end,
2971
+ :kIF_MOD, "if", :expr_beg)
2972
+ assert_lex3("1rif", nil,
2973
+ :tRATIONAL, Rational(1), :expr_end,
2974
+ :kIF_MOD, "if", :expr_beg)
2975
+ assert_lex3("1.0if", nil,
2976
+ :tFLOAT, 1.0, :expr_end,
2977
+ :kIF_MOD, "if", :expr_beg)
2978
+ assert_lex3("1.0rif", nil,
2979
+ :tRATIONAL, Rational("1.0"), :expr_end,
2980
+ :kIF_MOD, "if", :expr_beg)
2981
+
2982
+ flunk
2983
+ end
2984
+ end