ruby_parser 3.14.2 → 3.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2657 @@
1
+ # -*- racc -*-
2
+
3
+ class Ruby27Parser
4
+
5
+ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
6
+ kTHEN kELSIF kELSE kCASE kWHEN kWHILE kUNTIL kFOR kBREAK kNEXT
7
+ kREDO kRETRY kIN kDO kDO_COND kDO_BLOCK kDO_LAMBDA kRETURN kYIELD kSUPER
8
+ kSELF kNIL kTRUE kFALSE kAND kOR kNOT kIF_MOD kUNLESS_MOD kWHILE_MOD
9
+ kUNTIL_MOD kRESCUE_MOD kALIAS kDEFINED klBEGIN klEND k__LINE__
10
+ k__FILE__ k__ENCODING__ tIDENTIFIER tFID tGVAR tIVAR tCONSTANT
11
+ tLABEL tCVAR tNTH_REF tBACK_REF tSTRING_CONTENT tINTEGER tFLOAT
12
+ tREGEXP_END tUPLUS tUMINUS tUMINUS_NUM tPOW tCMP tEQ tEQQ tNEQ
13
+ tGEQ tLEQ tANDOP tOROP tMATCH tNMATCH tDOT tDOT2 tDOT3 tAREF
14
+ tASET tLSHFT tRSHFT tCOLON2 tCOLON3 tOP_ASGN tASSOC tLPAREN
15
+ tLPAREN2 tRPAREN tLPAREN_ARG tLBRACK tLBRACK2 tRBRACK tLBRACE
16
+ tLBRACE_ARG tSTAR tSTAR2 tAMPER tAMPER2 tTILDE tPERCENT tDIVIDE
17
+ tPLUS tMINUS tLT tGT tPIPE tBANG tCARET tLCURLY tRCURLY
18
+ tBACK_REF2 tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG
19
+ tWORDS_BEG tQWORDS_BEG tSTRING_DBEG tSTRING_DVAR tSTRING_END
20
+ tSTRING tSYMBOL tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA
21
+ tLAMBEG tDSTAR tCHAR tSYMBOLS_BEG tQSYMBOLS_BEG tSTRING_DEND tUBANG
22
+ tRATIONAL tIMAGINARY
23
+ tLABEL_END
24
+ tLONELY
25
+
26
+ preclow
27
+ nonassoc tLOWEST
28
+ nonassoc tLBRACE_ARG
29
+ nonassoc kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD
30
+ left kOR kAND
31
+ right kNOT
32
+ nonassoc kDEFINED
33
+ right tEQL tOP_ASGN
34
+ left kRESCUE_MOD
35
+ right tEH tCOLON
36
+ nonassoc tDOT2 tDOT3
37
+ left tOROP
38
+ left tANDOP
39
+ nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
40
+ left tGT tGEQ tLT tLEQ
41
+ left tPIPE tCARET
42
+ left tAMPER2
43
+ left tLSHFT tRSHFT
44
+ left tPLUS tMINUS
45
+ left tSTAR2 tDIVIDE tPERCENT # TODO: tSTAR2 -> tMULT
46
+ right tUMINUS_NUM tUMINUS
47
+ right tPOW
48
+ right tBANG tTILDE tUPLUS
49
+ prechigh
50
+
51
+ rule
52
+
53
+ program: {
54
+ self.lexer.lex_state = EXPR_BEG
55
+ }
56
+ top_compstmt
57
+ {
58
+ result = new_compstmt val
59
+ }
60
+
61
+ top_compstmt: top_stmts opt_terms
62
+ {
63
+ stmt, _ = val
64
+ result = stmt
65
+ }
66
+
67
+ top_stmts: none
68
+ | top_stmt
69
+ | top_stmts terms top_stmt
70
+ {
71
+ result = self.block_append val[0], val[2]
72
+ }
73
+ | error top_stmt
74
+
75
+ top_stmt: stmt
76
+ | klBEGIN
77
+ {
78
+ if (self.in_def || self.in_single > 0) then
79
+ debug20 1
80
+ yyerror "BEGIN in method"
81
+ end
82
+ self.env.extend
83
+ }
84
+ begin_block
85
+ {
86
+ (_, lineno), _, iter = val
87
+ iter.line lineno
88
+
89
+ (_, preexe,) = iter
90
+ preexe.line lineno
91
+
92
+ result = iter
93
+ }
94
+
95
+ begin_block: tLCURLY { result = lexer.lineno } top_compstmt tRCURLY
96
+ {
97
+ _, line, stmt, _ = val
98
+ result = new_iter s(:preexe).line(line), 0, stmt
99
+ }
100
+
101
+ bodystmt: compstmt opt_rescue k_else
102
+ {
103
+ res = _values[-2]
104
+ yyerror "else without rescue is useless" unless res
105
+ }
106
+ compstmt
107
+ opt_ensure
108
+ {
109
+ body, resc, _, _, els, ens = val
110
+
111
+ result = new_body [body, resc, els, ens]
112
+ }
113
+ | compstmt opt_rescue opt_ensure
114
+ {
115
+ body, resc, ens = val
116
+
117
+ result = new_body [body, resc, nil, ens]
118
+ }
119
+
120
+ compstmt: stmts opt_terms
121
+ {
122
+ result = new_compstmt val
123
+ }
124
+
125
+ stmts: none
126
+ | stmt_or_begin # TODO: newline_node ?
127
+ | stmts terms stmt_or_begin
128
+ {
129
+ result = self.block_append val[0], val[2]
130
+ }
131
+ | error stmt
132
+ {
133
+ result = val[1]
134
+ debug20 2, val, result
135
+ }
136
+
137
+ stmt_or_begin: stmt
138
+ | klBEGIN
139
+ {
140
+ yyerror "BEGIN is permitted only at toplevel"
141
+ }
142
+
143
+ stmt: kALIAS fitem
144
+ {
145
+ lexer.lex_state = EXPR_FNAME
146
+ }
147
+ fitem
148
+ {
149
+ (_, line), lhs, _, rhs = val
150
+ result = s(:alias, lhs, rhs).line(line).line line
151
+ }
152
+ | kALIAS tGVAR tGVAR
153
+ {
154
+ (_, line), lhs, rhs = val
155
+ result = s(:valias, lhs.to_sym, rhs.to_sym).line line
156
+ }
157
+ | kALIAS tGVAR tBACK_REF
158
+ {
159
+ (_, line), lhs, rhs = val
160
+ result = s(:valias, lhs.to_sym, :"$#{rhs}").line line
161
+ }
162
+ | kALIAS tGVAR tNTH_REF
163
+ {
164
+ yyerror "can't make alias for the number variables"
165
+ }
166
+ | kUNDEF undef_list
167
+ {
168
+ result = val[1]
169
+ }
170
+ | stmt kIF_MOD expr_value
171
+ {
172
+ t, _, c = val
173
+ result = new_if c, t, nil
174
+ }
175
+ | stmt kUNLESS_MOD expr_value
176
+ {
177
+ f, _, c = val
178
+ result = new_if c, nil, f
179
+ }
180
+ | stmt kWHILE_MOD expr_value
181
+ {
182
+ e, _, c = val
183
+ result = new_while e, c, true
184
+ }
185
+ | stmt kUNTIL_MOD expr_value
186
+ {
187
+ e, _, c = val
188
+ result = new_until e, c, true
189
+ }
190
+ | stmt kRESCUE_MOD stmt
191
+ {
192
+ body, _, resbody = val
193
+
194
+ resbody = new_resbody s(:array).line(resbody.line), resbody
195
+ result = new_rescue body, resbody
196
+ }
197
+ | klEND tLCURLY compstmt tRCURLY
198
+ {
199
+ (_, line), _, stmt, _ = val
200
+
201
+ if (self.in_def || self.in_single > 0) then
202
+ debug20 3
203
+ yyerror "END in method; use at_exit"
204
+ end
205
+
206
+ result = new_iter s(:postexe).line(line), 0, stmt
207
+ }
208
+ | command_asgn
209
+ | mlhs tEQL command_call
210
+ {
211
+ result = new_masgn val[0], val[2], :wrap
212
+ }
213
+ | lhs tEQL mrhs
214
+ {
215
+ lhs, _, rhs = val
216
+ result = new_assign lhs, s(:svalue, rhs).line(rhs.line)
217
+ }
218
+ | mlhs tEQL mrhs_arg
219
+ {
220
+ result = new_masgn val[0], val[2]
221
+ }
222
+ | expr
223
+
224
+ command_asgn: lhs tEQL command_rhs
225
+ {
226
+ result = new_assign val[0], val[2]
227
+ }
228
+ # | lhs tEQL command_asgn
229
+ # {
230
+ # result = new_assign val[0], val[2]
231
+ # }
232
+ | var_lhs tOP_ASGN command_rhs
233
+ {
234
+ result = new_op_asgn val
235
+ }
236
+ | primary_value tLBRACK2 opt_call_args rbracket tOP_ASGN command_rhs
237
+ {
238
+ result = new_op_asgn1 val
239
+ }
240
+ | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
241
+ {
242
+ prim, _, id, opasgn, rhs = val
243
+ result = s(:op_asgn, prim, rhs, id.to_sym, opasgn.to_sym)
244
+ if val[1] == '&.'
245
+ result.sexp_type = :safe_op_asgn
246
+ end
247
+ result.line = val[0].line
248
+ }
249
+ | primary_value call_op tCONSTANT tOP_ASGN command_rhs
250
+ {
251
+ result = s(:op_asgn, val[0], val[4], val[2].to_sym, val[3].to_sym)
252
+ if val[1] == '&.'
253
+ result.sexp_type = :safe_op_asgn
254
+ end
255
+ result.line = val[0].line
256
+ }
257
+ | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
258
+ {
259
+ lhs1, _, lhs2, op, rhs = val
260
+
261
+ result = s(:op_asgn, lhs1, rhs, lhs2.to_sym, op.to_sym)
262
+ }
263
+ | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
264
+ {
265
+ lhs1, _, lhs2, op, rhs = val
266
+
267
+ result = s(:op_asgn, lhs1, rhs, lhs2.to_sym, op.to_sym)
268
+ }
269
+ | backref tOP_ASGN command_rhs
270
+ {
271
+ self.backref_assign_error val[0]
272
+ }
273
+
274
+ command_rhs: command_call =tOP_ASGN
275
+ {
276
+ expr, = val
277
+ result = value_expr expr
278
+ }
279
+ | command_call kRESCUE_MOD stmt
280
+ {
281
+ expr, (_, line), resbody = val
282
+
283
+ expr = value_expr expr
284
+ ary = s(:array).line line
285
+ result = new_rescue(expr, new_resbody(ary, resbody))
286
+ }
287
+ | command_asgn
288
+
289
+ expr: command_call
290
+ | expr kAND expr
291
+ {
292
+ lhs, _, rhs = val
293
+ result = logical_op :and, lhs, rhs
294
+ }
295
+ | expr kOR expr
296
+ {
297
+ lhs, _, rhs = val
298
+ result = logical_op :or, lhs, rhs
299
+ }
300
+ | kNOT opt_nl expr
301
+ {
302
+ (_, line), _, expr = val
303
+ result = new_call(expr, :"!").line line
304
+ # REFACTOR: call_uni_op
305
+ }
306
+ | tBANG command_call
307
+ {
308
+ _, cmd = val
309
+ result = new_call(cmd, :"!").line cmd.line
310
+ # TODO: fix line number to tBANG... but causes BAD shift/reduce conflict
311
+ # REFACTOR: call_uni_op -- see parse26.y
312
+ }
313
+ | arg
314
+
315
+ expr_value: expr
316
+ {
317
+ result = value_expr(val[0])
318
+ }
319
+
320
+ expr_value_do: {
321
+ lexer.cond.push true
322
+ }
323
+ expr_value do
324
+ {
325
+ lexer.cond.pop
326
+ }
327
+ {
328
+ _, expr, _, _ = val
329
+ result = expr
330
+ }
331
+
332
+ command_call: command
333
+ | block_command
334
+
335
+ block_command: block_call
336
+ | block_call call_op2 operation2 command_args
337
+ {
338
+ blk, _, msg, args = val
339
+ result = new_call(blk, msg.to_sym, args).line blk.line
340
+ }
341
+
342
+ cmd_brace_block: tLBRACE_ARG
343
+ {
344
+ # self.env.extend(:dynamic)
345
+ result = self.lexer.lineno
346
+ }
347
+ brace_body tRCURLY
348
+ {
349
+ _, line, body, _ = val
350
+
351
+ result = body
352
+ result.line = line
353
+
354
+ # self.env.unextend
355
+ }
356
+
357
+ fcall: operation
358
+ {
359
+ msg, = val
360
+ result = new_call(nil, msg.to_sym).line lexer.lineno
361
+ }
362
+
363
+ command: fcall command_args =tLOWEST
364
+ {
365
+ call, args = val
366
+ result = call.concat args.sexp_body
367
+ }
368
+ | fcall command_args cmd_brace_block
369
+ {
370
+ call, args, block = val
371
+
372
+ result = call.concat args.sexp_body
373
+
374
+ if block then
375
+ block_dup_check result, block
376
+
377
+ result, operation = block, result
378
+ result.insert 1, operation
379
+ end
380
+ }
381
+ | primary_value call_op operation2 command_args =tLOWEST
382
+ {
383
+ lhs, callop, op, args = val
384
+ result = new_call lhs, op.to_sym, args, callop
385
+ }
386
+ | primary_value call_op operation2 command_args cmd_brace_block
387
+ {
388
+ recv, _, msg, args, block = val
389
+ call = new_call recv, msg.to_sym, args, val[1]
390
+
391
+ block_dup_check call, block
392
+
393
+ block.insert 1, call
394
+ result = block
395
+ }
396
+ | primary_value tCOLON2 operation2 command_args =tLOWEST
397
+ {
398
+ result = new_call val[0], val[2].to_sym, val[3]
399
+ }
400
+ | primary_value tCOLON2 operation2 command_args cmd_brace_block
401
+ {
402
+ recv, _, msg, args, block = val
403
+ call = new_call recv, msg.to_sym, args
404
+
405
+ block_dup_check call, block
406
+
407
+ block.insert 1, call
408
+ result = block
409
+ }
410
+ | kSUPER command_args
411
+ {
412
+ result = new_super val[1]
413
+ }
414
+ | kYIELD command_args
415
+ {
416
+ (_, line), args = val
417
+ result = new_yield args
418
+ result.line line # TODO: push to new_yield
419
+ }
420
+ | k_return call_args
421
+ {
422
+ line = val[0].last
423
+ result = s(:return, ret_args(val[1])).line(line)
424
+ }
425
+ | kBREAK call_args
426
+ {
427
+ (_, line), args = val
428
+ result = s(:break, ret_args(args)).line line
429
+ }
430
+ | kNEXT call_args
431
+ {
432
+ line = val[0].last
433
+ result = s(:next, ret_args(val[1])).line(line)
434
+ }
435
+
436
+ mlhs: mlhs_basic
437
+ | tLPAREN mlhs_inner rparen
438
+ {
439
+ result = val[1]
440
+ }
441
+
442
+ mlhs_inner: mlhs_basic
443
+ | tLPAREN mlhs_inner rparen
444
+ {
445
+ _, arg, _ = val
446
+ l = arg.line
447
+
448
+ result = s(:masgn, s(:array, arg).line(l)).line l
449
+ }
450
+
451
+ mlhs_basic: mlhs_head
452
+ {
453
+ head, = val
454
+ result = s(:masgn, head).line head.line
455
+ }
456
+ | mlhs_head mlhs_item
457
+ {
458
+ lhs, rhs = val
459
+ result = s(:masgn, lhs << rhs.compact).line lhs.line
460
+ }
461
+ | mlhs_head tSTAR mlhs_node
462
+ {
463
+ head, _, tail = val
464
+ head << s(:splat, tail).line(tail.line)
465
+ result = s(:masgn, head).line head.line
466
+ }
467
+ | mlhs_head tSTAR mlhs_node tCOMMA mlhs_post
468
+ {
469
+ ary1, _, splat, _, ary2 = val
470
+
471
+ result = list_append ary1, s(:splat, splat).line(splat.line)
472
+ result.concat ary2.sexp_body
473
+ result = s(:masgn, result).line result.line
474
+ }
475
+ | mlhs_head tSTAR
476
+ {
477
+ head, _ = val
478
+ l = head.line
479
+ result = s(:masgn, head << s(:splat).line(l)).line l
480
+ }
481
+ | mlhs_head tSTAR tCOMMA mlhs_post
482
+ {
483
+ head, _, _, post = val
484
+ ary = list_append head, s(:splat).line(head.line)
485
+ ary.concat post.sexp_body
486
+ result = s(:masgn, ary).line ary.line
487
+ }
488
+ | tSTAR mlhs_node
489
+ {
490
+ _, node = val
491
+ l = node.line
492
+ splat = s(:splat, node).line l
493
+ ary = s(:array, splat).line l
494
+ result = s(:masgn, ary).line l
495
+ }
496
+ | tSTAR mlhs_node tCOMMA mlhs_post
497
+ {
498
+ _, node, _, post = val
499
+
500
+ splat = s(:splat, node).line node.line
501
+ ary = s(:array, splat).line splat.line
502
+ ary.concat post.sexp_body
503
+ result = s(:masgn, ary).line ary.line
504
+ }
505
+ | tSTAR
506
+ {
507
+ l = lexer.lineno
508
+ result = s(:masgn, s(:array, s(:splat).line(l)).line(l)).line l
509
+ }
510
+ | tSTAR tCOMMA mlhs_post
511
+ {
512
+ _, _, post = val
513
+ l = post.line
514
+
515
+ splat = s(:splat).line l
516
+ ary = s(:array, splat, *post.sexp_body).line l
517
+ result = s(:masgn, ary).line l
518
+ }
519
+
520
+ mlhs_item: mlhs_node
521
+ | tLPAREN mlhs_inner rparen
522
+ {
523
+ result = val[1]
524
+ }
525
+
526
+ mlhs_head: mlhs_item tCOMMA
527
+ {
528
+ lhs, _ = val
529
+ result = s(:array, lhs).line lhs.line
530
+ }
531
+ | mlhs_head mlhs_item tCOMMA
532
+ {
533
+ result = val[0] << val[1].compact
534
+ }
535
+
536
+ mlhs_post: mlhs_item
537
+ {
538
+ item, = val
539
+ result = s(:array, item).line item.line
540
+ }
541
+ | mlhs_post tCOMMA mlhs_item
542
+ {
543
+ result = list_append val[0], val[2]
544
+ }
545
+
546
+ mlhs_node: user_variable
547
+ {
548
+ result = self.assignable val[0]
549
+ }
550
+ | keyword_variable
551
+ {
552
+ result = self.assignable val[0]
553
+ }
554
+ | primary_value tLBRACK2 opt_call_args rbracket
555
+ {
556
+ result = self.aryset val[0], val[2]
557
+ }
558
+ | primary_value call_op tIDENTIFIER
559
+ {
560
+ result = new_attrasgn val[0], val[2], val[1]
561
+ }
562
+ | primary_value tCOLON2 tIDENTIFIER
563
+ {
564
+ recv, _, id = val
565
+ result = new_attrasgn recv, id
566
+ }
567
+ | primary_value call_op tCONSTANT
568
+ {
569
+ result = new_attrasgn val[0], val[2], val[1]
570
+ }
571
+ | primary_value tCOLON2 tCONSTANT
572
+ {
573
+ if (self.in_def || self.in_single > 0) then
574
+ debug20 7
575
+ yyerror "dynamic constant assignment"
576
+ end
577
+
578
+ expr, _, id = val
579
+ l = expr.line
580
+
581
+ result = s(:const, s(:colon2, expr, id.to_sym).line(l), nil).line l
582
+ }
583
+ | tCOLON3 tCONSTANT
584
+ {
585
+ if (self.in_def || self.in_single > 0) then
586
+ debug20 8
587
+ yyerror "dynamic constant assignment"
588
+ end
589
+
590
+ _, id = val
591
+ l = lexer.lineno
592
+
593
+ result = s(:const, nil, s(:colon3, id.to_sym).line(l)).line l
594
+ }
595
+ | backref
596
+ {
597
+ self.backref_assign_error val[0]
598
+ }
599
+
600
+ lhs: user_variable
601
+ {
602
+ line = lexer.lineno
603
+ result = self.assignable val[0]
604
+ result.line = line
605
+ }
606
+ | keyword_variable
607
+ {
608
+ line = lexer.lineno
609
+ result = self.assignable val[0]
610
+ result.line = line
611
+ debug20 9, val, result
612
+ }
613
+ | primary_value tLBRACK2 opt_call_args rbracket
614
+ {
615
+ lhs, _, args, _ = val
616
+ result = self.aryset lhs, args
617
+ }
618
+ | primary_value call_op tIDENTIFIER # REFACTOR
619
+ {
620
+ lhs, op, id = val
621
+ result = new_attrasgn lhs, id, op
622
+ }
623
+ | primary_value tCOLON2 tIDENTIFIER
624
+ {
625
+ lhs, _, id = val
626
+ result = new_attrasgn lhs, id
627
+ }
628
+ | primary_value call_op tCONSTANT # REFACTOR?
629
+ {
630
+ result = new_attrasgn val[0], val[2], val[1]
631
+ }
632
+ | primary_value tCOLON2 tCONSTANT
633
+ {
634
+ expr, _, id = val
635
+
636
+ if (self.in_def || self.in_single > 0) then
637
+ debug20 10
638
+ yyerror "dynamic constant assignment"
639
+ end
640
+
641
+ l = expr.line
642
+ result = s(:const, s(:colon2, expr, id.to_sym).line(l)).line l
643
+ }
644
+ | tCOLON3 tCONSTANT
645
+ {
646
+ _, id = val
647
+
648
+ if (self.in_def || self.in_single > 0) then
649
+ debug20 11
650
+ yyerror "dynamic constant assignment"
651
+ end
652
+
653
+ l = lexer.lineno
654
+ result = s(:const, s(:colon3, id.to_sym).line(l)).line l
655
+ }
656
+ | backref
657
+ {
658
+ self.backref_assign_error val[0]
659
+ }
660
+
661
+ cname: tIDENTIFIER
662
+ {
663
+ yyerror "class/module name must be CONSTANT"
664
+ }
665
+ | tCONSTANT
666
+
667
+ cpath: tCOLON3 cname
668
+ {
669
+ _, name = val
670
+ result = s(:colon3, name.to_sym).line lexer.lineno
671
+ }
672
+ | cname
673
+ {
674
+ result = val[0].to_sym
675
+ }
676
+ | primary_value tCOLON2 cname
677
+ {
678
+ pval, _, name = val
679
+
680
+ result = s(:colon2, pval, name.to_sym)
681
+ result.line pval.line
682
+ }
683
+
684
+ fname: tIDENTIFIER | tCONSTANT | tFID
685
+ | op
686
+ {
687
+ lexer.lex_state = EXPR_END
688
+ result = val[0]
689
+ }
690
+
691
+ | reswords
692
+ {
693
+ (sym, _line), = val
694
+ lexer.lex_state = EXPR_END
695
+ result = sym
696
+ }
697
+
698
+ fsym: fname | symbol
699
+
700
+ fitem: fsym
701
+ {
702
+ id, = val
703
+ result = s(:lit, id.to_sym).line lexer.lineno
704
+ }
705
+ | dsym
706
+
707
+ undef_list: fitem
708
+ {
709
+ result = new_undef val[0]
710
+ }
711
+ |
712
+ undef_list tCOMMA
713
+ {
714
+ lexer.lex_state = EXPR_FNAME
715
+ }
716
+ fitem
717
+ {
718
+ result = new_undef val[0], val[3]
719
+ }
720
+
721
+ op: tPIPE | tCARET | tAMPER2 | tCMP | tEQ | tEQQ
722
+ | tMATCH | tNMATCH | tGT | tGEQ | tLT | tLEQ
723
+ | tNEQ | tLSHFT | tRSHFT | tPLUS | tMINUS | tSTAR2
724
+ | tSTAR | tDIVIDE | tPERCENT | tPOW | tDSTAR | tBANG | tTILDE
725
+ | tUPLUS | tUMINUS | tAREF | tASET | tBACK_REF2
726
+ # TODO: tUBANG dead?
727
+ | tUBANG
728
+
729
+ reswords: k__LINE__ | k__FILE__ | k__ENCODING__ | klBEGIN | klEND
730
+ | kALIAS | kAND | kBEGIN | kBREAK | kCASE
731
+ | kCLASS | kDEF | kDEFINED | kDO | kELSE
732
+ | kELSIF | kEND | kENSURE | kFALSE | kFOR
733
+ | kIN | kMODULE | kNEXT | kNIL | kNOT
734
+ | kOR | kREDO | kRESCUE | kRETRY | kRETURN
735
+ | kSELF | kSUPER | kTHEN | kTRUE | kUNDEF
736
+ | kWHEN | kYIELD | kIF | kUNLESS | kWHILE
737
+ | kUNTIL
738
+
739
+ arg: lhs tEQL arg_rhs
740
+ {
741
+ result = new_assign val[0], val[2]
742
+ }
743
+ | var_lhs tOP_ASGN arg_rhs
744
+ {
745
+ result = new_op_asgn val
746
+ }
747
+ | primary_value tLBRACK2 opt_call_args rbracket tOP_ASGN arg_rhs
748
+ {
749
+ result = new_op_asgn1 val
750
+ }
751
+ | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
752
+ {
753
+ result = new_op_asgn2 val
754
+ }
755
+ | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
756
+ {
757
+ result = new_op_asgn2 val
758
+ }
759
+ | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
760
+ {
761
+ lhs, _, id, op, rhs = val
762
+
763
+ result = s(:op_asgn, lhs, rhs, id.to_sym, op.to_sym).line lhs.line
764
+ }
765
+ | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
766
+ {
767
+ lhs1, _, lhs2, op, rhs = val
768
+
769
+ lhs = s(:colon2, lhs1, lhs2.to_sym).line lhs1.line
770
+ result = new_const_op_asgn [lhs, op, rhs]
771
+ }
772
+ | tCOLON3 tCONSTANT
773
+ {
774
+ result = self.lexer.lineno
775
+ }
776
+ tOP_ASGN arg_rhs
777
+ {
778
+ _, lhs, line, op, rhs = val
779
+
780
+ lhs = s(:colon3, lhs.to_sym).line line
781
+ result = new_const_op_asgn [lhs, op, rhs]
782
+ }
783
+ | backref tOP_ASGN arg_rhs
784
+ {
785
+ # TODO: lhs = var_field val[0]
786
+ asgn = new_op_asgn val
787
+ result = self.backref_assign_error asgn
788
+ }
789
+ | arg tDOT2 arg
790
+ {
791
+ v1, v2 = val[0], val[2]
792
+ if v1.node_type == :lit and v2.node_type == :lit and Integer === v1.last and Integer === v2.last then
793
+ result = s(:lit, (v1.last)..(v2.last)).line v1.line
794
+ else
795
+ result = s(:dot2, v1, v2).line v1.line
796
+ end
797
+ }
798
+ | arg tDOT3 arg
799
+ {
800
+ v1, v2 = val[0], val[2]
801
+ if v1.node_type == :lit and v2.node_type == :lit and Integer === v1.last and Integer === v2.last then
802
+ result = s(:lit, (v1.last)...(v2.last)).line v1.line
803
+ else
804
+ result = s(:dot3, v1, v2).line v1.line
805
+ end
806
+ }
807
+ | arg tDOT2
808
+ {
809
+ v1, _ = val
810
+ v2 = nil
811
+
812
+ result = s(:dot2, v1, v2).line v1.line
813
+ }
814
+ | arg tDOT3
815
+ {
816
+ v1, _ = val
817
+ v2 = nil
818
+
819
+ result = s(:dot3, v1, v2).line v1.line
820
+ }
821
+ | arg tPLUS arg
822
+ {
823
+ result = new_call val[0], :+, argl(val[2])
824
+ }
825
+ | arg tMINUS arg
826
+ {
827
+ result = new_call val[0], :-, argl(val[2])
828
+ }
829
+ | arg tSTAR2 arg # TODO: rename
830
+ {
831
+ result = new_call val[0], :*, argl(val[2])
832
+ }
833
+ | arg tDIVIDE arg
834
+ {
835
+ result = new_call val[0], :"/", argl(val[2])
836
+ }
837
+ | arg tPERCENT arg
838
+ {
839
+ result = new_call val[0], :"%", argl(val[2])
840
+ }
841
+ | arg tPOW arg
842
+ {
843
+ result = new_call val[0], :**, argl(val[2])
844
+ }
845
+ | tUMINUS_NUM simple_numeric tPOW arg
846
+ {
847
+ lit = s(:lit, val[1]).line lexer.lineno
848
+ result = new_call(new_call(lit, :"**", argl(val[3])), :"-@")
849
+
850
+ }
851
+ | tUPLUS arg
852
+ {
853
+ result = new_call val[1], :"+@"
854
+ }
855
+ | tUMINUS arg
856
+ {
857
+ result = new_call val[1], :"-@"
858
+ }
859
+ | arg tPIPE arg
860
+ {
861
+ result = new_call val[0], :"|", argl(val[2])
862
+ }
863
+ | arg tCARET arg
864
+ {
865
+ result = new_call val[0], :"^", argl(val[2])
866
+ }
867
+ | arg tAMPER2 arg
868
+ {
869
+ result = new_call val[0], :"&", argl(val[2])
870
+ }
871
+ | arg tCMP arg
872
+ {
873
+ result = new_call val[0], :"<=>", argl(val[2])
874
+ }
875
+ | rel_expr =tCMP
876
+ | arg tEQ arg
877
+ {
878
+ result = new_call val[0], :"==", argl(val[2])
879
+ }
880
+ | arg tEQQ arg
881
+ {
882
+ result = new_call val[0], :"===", argl(val[2])
883
+ }
884
+ | arg tNEQ arg
885
+ {
886
+ result = new_call val[0], :"!=", argl(val[2])
887
+ }
888
+ | arg tMATCH arg
889
+ {
890
+ lhs, _, rhs = val
891
+ result = new_match lhs, rhs
892
+ }
893
+ | arg tNMATCH arg
894
+ {
895
+ lhs, _, rhs = val
896
+ result = s(:not, new_match(lhs, rhs)).line lhs.line
897
+ }
898
+ | tBANG arg
899
+ {
900
+ _, arg = val
901
+ result = new_call arg, :"!"
902
+ result.line arg.line
903
+ }
904
+ | tTILDE arg
905
+ {
906
+ result = new_call value_expr(val[1]), :"~"
907
+ }
908
+ | arg tLSHFT arg
909
+ {
910
+ val[0] = value_expr val[0]
911
+ val[2] = value_expr val[2]
912
+ result = new_call val[0], :"\<\<", argl(val[2])
913
+ }
914
+ | arg tRSHFT arg
915
+ {
916
+ val[0] = value_expr val[0]
917
+ val[2] = value_expr val[2]
918
+ result = new_call val[0], :">>", argl(val[2])
919
+ }
920
+ | arg tANDOP arg
921
+ {
922
+ result = logical_op :and, val[0], val[2]
923
+ }
924
+ | arg tOROP arg
925
+ {
926
+ result = logical_op :or, val[0], val[2]
927
+ }
928
+ | kDEFINED opt_nl arg
929
+ {
930
+ (_, line), _, arg = val
931
+ result = s(:defined, arg).line line
932
+ }
933
+ | arg tEH arg opt_nl tCOLON arg
934
+ {
935
+ c, _, t, _, _, f = val
936
+ result = s(:if, c, t, f).line c.line
937
+ }
938
+ | primary
939
+
940
+ relop: tGT
941
+ | tLT
942
+ | tGEQ
943
+ | tLEQ
944
+
945
+ rel_expr: arg relop arg =tGT
946
+ {
947
+ lhs, op, rhs = val
948
+ result = new_call lhs, op.to_sym, argl(rhs)
949
+ }
950
+ | rel_expr relop arg =tGT
951
+ {
952
+ lhs, op, rhs = val
953
+ warn "comparison '%s' after comparison", op
954
+ result = new_call lhs, op.to_sym, argl(rhs)
955
+ }
956
+
957
+ arg_value: arg
958
+ {
959
+ result = value_expr(val[0])
960
+ }
961
+
962
+ aref_args: none
963
+ | args trailer
964
+ {
965
+ result = args [val[0]]
966
+ }
967
+ | args tCOMMA assocs trailer
968
+ {
969
+ result = args [val[0], array_to_hash(val[2])]
970
+ }
971
+ | assocs trailer
972
+ {
973
+ result = args [array_to_hash(val[0])]
974
+ }
975
+
976
+ arg_rhs: arg =tOP_ASGN
977
+ | arg kRESCUE_MOD arg
978
+ {
979
+ body, (_, line), resbody = val
980
+ body = value_expr body
981
+ resbody = remove_begin resbody
982
+
983
+ ary = s(:array).line line
984
+ result = new_rescue(body, new_resbody(ary, resbody))
985
+ }
986
+
987
+ paren_args: tLPAREN2 opt_call_args rparen
988
+ {
989
+ _, args, _ = val
990
+ result = args
991
+ }
992
+
993
+ opt_paren_args: none
994
+ | paren_args
995
+
996
+ opt_call_args: none
997
+ | call_args
998
+ | args tCOMMA
999
+ {
1000
+ result = args val
1001
+ }
1002
+ | args tCOMMA assocs tCOMMA
1003
+ {
1004
+ result = args [val[0], array_to_hash(val[2])]
1005
+ }
1006
+ | assocs tCOMMA
1007
+ {
1008
+ result = args [array_to_hash(val[0])]
1009
+ }
1010
+
1011
+ call_args: command
1012
+ {
1013
+ warning "parenthesize argument(s) for future version"
1014
+ result = call_args val
1015
+ }
1016
+ | args opt_block_arg
1017
+ {
1018
+ result = call_args val
1019
+ }
1020
+ | assocs opt_block_arg
1021
+ {
1022
+ result = call_args [array_to_hash(val[0]), val[1]]
1023
+ }
1024
+ | args tCOMMA assocs opt_block_arg
1025
+ {
1026
+ result = call_args [val[0], array_to_hash(val[2]), val[3]]
1027
+ }
1028
+ | block_arg
1029
+ {
1030
+ result = call_args val
1031
+ }
1032
+
1033
+ command_args: {
1034
+ # parse26.y line 2200
1035
+
1036
+ # If call_args starts with a open paren '(' or
1037
+ # '[', look-ahead reading of the letters calls
1038
+ # CMDARG_PUSH(0), but the push must be done
1039
+ # after CMDARG_PUSH(1). So this code makes them
1040
+ # consistent by first cancelling the premature
1041
+ # CMDARG_PUSH(0), doing CMDARG_PUSH(1), and
1042
+ # finally redoing CMDARG_PUSH(0).
1043
+
1044
+ result = yychar = self.last_token_type.first
1045
+ lookahead = [:tLPAREN, :tLPAREN_ARG, :tLPAREN2, :tLBRACK, :tLBRACK2].include?(yychar)
1046
+ lexer.cmdarg.pop if lookahead
1047
+ lexer.cmdarg.push true
1048
+ lexer.cmdarg.push false if lookahead
1049
+ }
1050
+ call_args
1051
+ {
1052
+ yychar, args = val
1053
+
1054
+ # call_args can be followed by tLBRACE_ARG (that
1055
+ # does CMDARG_PUSH(0) in the lexer) but the push
1056
+ # must be done after CMDARG_POP() in the parser.
1057
+ # So this code does CMDARG_POP() to pop 0 pushed
1058
+ # by tLBRACE_ARG, CMDARG_POP() to pop 1 pushed
1059
+ # by command_args, and CMDARG_PUSH(0) to restore
1060
+ # back the flag set by tLBRACE_ARG.
1061
+
1062
+ lookahead = [:tLBRACE_ARG].include?(yychar)
1063
+ lexer.cmdarg.pop if lookahead
1064
+ lexer.cmdarg.pop
1065
+ lexer.cmdarg.push false if lookahead
1066
+ result = args
1067
+ }
1068
+
1069
+ block_arg: tAMPER arg_value
1070
+ {
1071
+ _, arg = val
1072
+ result = s(:block_pass, arg).line arg.line
1073
+ }
1074
+
1075
+ opt_block_arg: tCOMMA block_arg
1076
+ {
1077
+ result = val[1]
1078
+ }
1079
+ | none
1080
+
1081
+ args: arg_value
1082
+ {
1083
+ arg, = val
1084
+ lineno = arg.line || lexer.lineno # HACK
1085
+
1086
+ result = s(:array, arg).line lineno
1087
+ }
1088
+ | tSTAR arg_value
1089
+ {
1090
+ _, arg = val
1091
+ result = s(:array, s(:splat, arg).line(arg.line)).line arg.line
1092
+ }
1093
+ | args tCOMMA arg_value
1094
+ {
1095
+ args, _, id = val
1096
+ result = self.list_append args, id
1097
+ }
1098
+ | args tCOMMA tSTAR arg_value
1099
+ {
1100
+ # TODO: the line number from tSTAR has been dropped
1101
+ args, _, _, id = val
1102
+ line = lexer.lineno
1103
+ result = self.list_append args, s(:splat, id).line(line)
1104
+ }
1105
+
1106
+ mrhs_arg: mrhs
1107
+ {
1108
+ result = new_masgn_arg val[0]
1109
+ }
1110
+ | arg_value
1111
+ {
1112
+ result = new_masgn_arg val[0], :wrap
1113
+ }
1114
+
1115
+ mrhs: args tCOMMA arg_value
1116
+ {
1117
+ result = val[0] << val[2]
1118
+ }
1119
+ | args tCOMMA tSTAR arg_value
1120
+ {
1121
+ # TODO: make all tXXXX terminals include lexer.lineno
1122
+ arg, _, _, splat = val
1123
+ result = self.arg_concat arg, splat
1124
+ }
1125
+ | tSTAR arg_value
1126
+ {
1127
+ _, arg = val
1128
+ result = s(:splat, arg).line arg.line
1129
+ }
1130
+
1131
+ primary: literal
1132
+ | strings
1133
+ | xstring
1134
+ | regexp
1135
+ | words
1136
+ | qwords
1137
+ | symbols
1138
+ | qsymbols
1139
+ | var_ref
1140
+ | backref
1141
+ | tFID
1142
+ {
1143
+ msg, = val
1144
+ result = new_call nil, msg.to_sym
1145
+ }
1146
+ | k_begin
1147
+ {
1148
+ lexer.cmdarg.push false
1149
+ result = self.lexer.lineno
1150
+ }
1151
+ bodystmt k_end
1152
+ {
1153
+ lexer.cmdarg.pop
1154
+ result = new_begin val
1155
+ }
1156
+ | tLPAREN_ARG
1157
+ {
1158
+ lexer.lex_state = EXPR_ENDARG
1159
+ result = lexer.lineno
1160
+ }
1161
+ rparen
1162
+ {
1163
+ _, line, _ = val
1164
+ result = s(:begin).line line
1165
+ }
1166
+ | tLPAREN_ARG
1167
+ stmt
1168
+ {
1169
+ lexer.lex_state = EXPR_ENDARG
1170
+ }
1171
+ rparen
1172
+ {
1173
+ _, stmt, _, _, = val
1174
+ # warning "(...) interpreted as grouped expression"
1175
+ result = stmt
1176
+ }
1177
+ | tLPAREN compstmt tRPAREN
1178
+ {
1179
+ _, stmt, _ = val
1180
+ result = stmt
1181
+ result ||= s(:nil).line lexer.lineno
1182
+ result.paren = true
1183
+ }
1184
+ | primary_value tCOLON2 tCONSTANT
1185
+ {
1186
+ expr, _, id = val
1187
+
1188
+ result = s(:colon2, expr, id.to_sym).line expr.line
1189
+ }
1190
+ | tCOLON3 tCONSTANT
1191
+ {
1192
+ _, id = val
1193
+
1194
+ result = s(:colon3, id.to_sym).line lexer.lineno
1195
+ }
1196
+ | tLBRACK { result = lexer.lineno } aref_args tRBRACK
1197
+ {
1198
+ _, line, args, _ = val
1199
+ result = args || s(:array)
1200
+ result.sexp_type = :array # aref_args is :args
1201
+ result.line line
1202
+ }
1203
+ | tLBRACE
1204
+ {
1205
+ result = self.lexer.lineno
1206
+ }
1207
+ assoc_list tRCURLY
1208
+ {
1209
+ result = new_hash val
1210
+ }
1211
+ | k_return
1212
+ {
1213
+ (_, line), = val
1214
+ result = s(:return).line line
1215
+ }
1216
+ | kYIELD tLPAREN2 call_args rparen
1217
+ {
1218
+ result = new_yield val[2]
1219
+ }
1220
+ | kYIELD tLPAREN2 rparen
1221
+ {
1222
+ result = new_yield
1223
+ }
1224
+ | kYIELD
1225
+ {
1226
+ result = new_yield
1227
+ }
1228
+ | kDEFINED opt_nl tLPAREN2 expr rparen
1229
+ {
1230
+ (_, line), _, _, arg, _ = val
1231
+
1232
+ result = s(:defined, arg).line line
1233
+ }
1234
+ | kNOT tLPAREN2 expr rparen
1235
+ {
1236
+ _, _, lhs, _ = val
1237
+ result = new_call lhs, :"!"
1238
+ }
1239
+ | kNOT tLPAREN2 rparen
1240
+ {
1241
+ debug20 14, val, result
1242
+ }
1243
+ | fcall brace_block
1244
+ {
1245
+ call, iter = val
1246
+
1247
+ iter.insert 1, call
1248
+ result = iter
1249
+ # FIX: probably not: call.line = iter.line
1250
+ }
1251
+ | method_call
1252
+ | method_call brace_block
1253
+ {
1254
+ call, iter = val[0], val[1]
1255
+ block_dup_check call, iter
1256
+ iter.insert 1, call # FIX
1257
+ result = iter
1258
+ }
1259
+ | tLAMBDA lambda
1260
+ {
1261
+ result = val[1] # TODO: fix lineno
1262
+ }
1263
+ | k_if expr_value then compstmt if_tail k_end
1264
+ {
1265
+ _, c, _, t, f, _ = val
1266
+ result = new_if c, t, f
1267
+ }
1268
+ | k_unless expr_value then compstmt opt_else k_end
1269
+ {
1270
+ _, c, _, t, f, _ = val
1271
+ result = new_if c, f, t
1272
+ }
1273
+ | k_while expr_value_do compstmt k_end
1274
+ {
1275
+ _, cond, body, _ = val
1276
+ result = new_while body, cond, true
1277
+ }
1278
+ | k_until expr_value_do compstmt k_end
1279
+ {
1280
+ _, cond, body, _ = val
1281
+ result = new_until body, cond, true
1282
+ }
1283
+ | k_case expr_value opt_terms case_body k_end
1284
+ {
1285
+ (_, line), expr, _, body, _ = val
1286
+ result = new_case expr, body, line
1287
+ }
1288
+ | k_case opt_terms case_body k_end
1289
+ {
1290
+ (_, line), _, body, _ = val
1291
+ result = new_case nil, body, line
1292
+ }
1293
+ | k_for for_var kIN expr_value_do compstmt k_end
1294
+ {
1295
+ _, var, _, iter, body, _ = val
1296
+ result = new_for iter, var, body
1297
+ }
1298
+ | k_class
1299
+ {
1300
+ result = self.lexer.lineno
1301
+ }
1302
+ cpath superclass
1303
+ {
1304
+ self.comments.push self.lexer.comments
1305
+ if (self.in_def || self.in_single > 0) then
1306
+ yyerror "class definition in method body"
1307
+ end
1308
+ self.env.extend
1309
+ }
1310
+ bodystmt k_end
1311
+ {
1312
+ result = new_class val
1313
+ self.env.unextend
1314
+ self.lexer.comments # we don't care about comments in the body
1315
+ }
1316
+ | k_class tLSHFT
1317
+ {
1318
+ result = self.lexer.lineno
1319
+ }
1320
+ expr
1321
+ {
1322
+ result = self.in_def
1323
+ self.in_def = false
1324
+ }
1325
+ term
1326
+ {
1327
+ result = self.in_single
1328
+ self.in_single = 0
1329
+ self.env.extend
1330
+ }
1331
+ bodystmt k_end
1332
+ {
1333
+ result = new_sclass val
1334
+ self.env.unextend
1335
+ self.lexer.comments # we don't care about comments in the body
1336
+ }
1337
+ | k_module
1338
+ {
1339
+ result = self.lexer.lineno
1340
+ }
1341
+ cpath
1342
+ {
1343
+ self.comments.push self.lexer.comments
1344
+ yyerror "module definition in method body" if
1345
+ self.in_def or self.in_single > 0
1346
+
1347
+ self.env.extend
1348
+ }
1349
+ bodystmt k_end
1350
+ {
1351
+ result = new_module val
1352
+ self.env.unextend
1353
+ self.lexer.comments # we don't care about comments in the body
1354
+ }
1355
+ | k_def fname
1356
+ {
1357
+ result = self.in_def
1358
+
1359
+ self.in_def = true # group = local_push
1360
+ self.env.extend
1361
+ lexer.cmdarg.push false
1362
+ lexer.cond.push false
1363
+
1364
+ self.comments.push self.lexer.comments
1365
+ }
1366
+ f_arglist bodystmt { result = lexer.lineno } k_end
1367
+ {
1368
+ in_def = val[2]
1369
+
1370
+ result = new_defn val
1371
+
1372
+ lexer.cond.pop # group = local_pop
1373
+ lexer.cmdarg.pop
1374
+ self.env.unextend
1375
+ self.in_def = in_def
1376
+
1377
+ self.lexer.comments # we don't care about comments in the body
1378
+ }
1379
+ | k_def singleton dot_or_colon
1380
+ {
1381
+ lexer.lex_state = EXPR_FNAME
1382
+ }
1383
+ fname
1384
+ {
1385
+ result = [self.in_def, lexer.lineno]
1386
+
1387
+ self.in_single += 1 # TODO: remove?
1388
+
1389
+ self.in_def = true # local_push
1390
+ self.env.extend
1391
+ lexer.cmdarg.push false
1392
+ lexer.cond.push false
1393
+
1394
+ lexer.lex_state = EXPR_ENDFN|EXPR_LABEL
1395
+ self.comments.push self.lexer.comments
1396
+ }
1397
+ f_arglist bodystmt k_end
1398
+ {
1399
+ _, _recv, _, _, _name, (in_def, _lineno), _args, _body, _ = val
1400
+
1401
+ result = new_defs val
1402
+
1403
+ lexer.cond.pop # group = local_pop
1404
+ lexer.cmdarg.pop
1405
+ self.env.unextend
1406
+ self.in_def = in_def
1407
+
1408
+ self.in_single -= 1
1409
+
1410
+ # TODO: restore cur_arg ? what's cur_arg?
1411
+
1412
+ self.lexer.comments # we don't care about comments in the body
1413
+ }
1414
+ | kBREAK
1415
+ {
1416
+ (_, line), = val
1417
+ result = s(:break).line line
1418
+ }
1419
+ | kNEXT
1420
+ {
1421
+ (_, line), = val
1422
+ result = s(:next).line line
1423
+ }
1424
+ | kREDO
1425
+ {
1426
+ (_, line), = val
1427
+ result = s(:redo).line line
1428
+ }
1429
+ | kRETRY
1430
+ {
1431
+ (_, line), = val
1432
+ result = s(:retry).line line
1433
+ }
1434
+
1435
+ primary_value: primary
1436
+ {
1437
+ result = value_expr(val[0])
1438
+ }
1439
+
1440
+ # These are really stupid
1441
+ k_begin: kBEGIN
1442
+ k_if: kIF
1443
+ k_unless: kUNLESS
1444
+ k_while: kWHILE
1445
+ k_until: kUNTIL
1446
+ k_case: kCASE
1447
+ k_for: kFOR
1448
+ k_class: kCLASS
1449
+ k_module: kMODULE
1450
+ k_def: kDEF
1451
+ k_do: kDO
1452
+ k_do_block: kDO_BLOCK
1453
+ k_rescue: kRESCUE
1454
+ k_ensure: kENSURE
1455
+ k_when: kWHEN
1456
+ k_else: kELSE
1457
+ k_elsif: kELSIF
1458
+ k_end: kEND
1459
+ k_return: kRETURN
1460
+
1461
+ then: term
1462
+ | kTHEN
1463
+ | term kTHEN
1464
+
1465
+ do: term
1466
+ | kDO_COND
1467
+
1468
+ if_tail: opt_else
1469
+ | k_elsif expr_value then compstmt if_tail
1470
+ {
1471
+ (_, line), c, _, t, rest = val
1472
+
1473
+ result = s(:if, c, t, rest).line line
1474
+ }
1475
+
1476
+ opt_else: none
1477
+ | kELSE compstmt
1478
+ {
1479
+ result = val[1]
1480
+ }
1481
+
1482
+ for_var: lhs
1483
+ | mlhs
1484
+ {
1485
+ val[0].delete_at 1 if val[0][1].nil? # HACK
1486
+ }
1487
+
1488
+ f_marg: f_norm_arg
1489
+ | tLPAREN f_margs rparen
1490
+ {
1491
+ result = val[1]
1492
+ }
1493
+
1494
+ f_marg_list: f_marg
1495
+ {
1496
+ sym, = val
1497
+
1498
+ result = s(:array, sym).line lexer.lineno
1499
+ }
1500
+ | f_marg_list tCOMMA f_marg
1501
+ {
1502
+ result = list_append val[0], val[2]
1503
+ }
1504
+
1505
+ f_margs: f_marg_list
1506
+ {
1507
+ args, = val
1508
+
1509
+ result = block_var args
1510
+ }
1511
+ | f_marg_list tCOMMA tSTAR f_norm_arg
1512
+ {
1513
+ args, _, _, splat = val
1514
+
1515
+ result = block_var args, "*#{splat}".to_sym
1516
+ }
1517
+ | f_marg_list tCOMMA tSTAR f_norm_arg tCOMMA f_marg_list
1518
+ {
1519
+ args, _, _, splat, _, args2 = val
1520
+
1521
+ result = block_var args, "*#{splat}".to_sym, args2
1522
+ }
1523
+ | f_marg_list tCOMMA tSTAR
1524
+ {
1525
+ args, _, _ = val
1526
+
1527
+ result = block_var args, :*
1528
+ }
1529
+ | f_marg_list tCOMMA tSTAR tCOMMA f_marg_list
1530
+ {
1531
+ args, _, _, _, args2 = val
1532
+
1533
+ result = block_var args, :*, args2
1534
+ }
1535
+ | tSTAR f_norm_arg
1536
+ {
1537
+ _, splat = val
1538
+
1539
+ result = block_var :"*#{splat}"
1540
+ }
1541
+ | tSTAR f_norm_arg tCOMMA f_marg_list
1542
+ {
1543
+ _, splat, _, args = val
1544
+
1545
+ result = block_var :"*#{splat}", args
1546
+ }
1547
+ | tSTAR
1548
+ {
1549
+ result = block_var :*
1550
+ }
1551
+ | tSTAR tCOMMA f_marg_list
1552
+ {
1553
+ _, _, args = val
1554
+
1555
+ result = block_var :*, args
1556
+ }
1557
+
1558
+ block_args_tail: f_block_kwarg tCOMMA f_kwrest opt_f_block_arg
1559
+ {
1560
+ result = call_args val
1561
+ }
1562
+ | f_block_kwarg opt_f_block_arg
1563
+ {
1564
+ result = call_args val
1565
+ }
1566
+ | f_kwrest opt_f_block_arg
1567
+ {
1568
+ result = call_args val
1569
+ }
1570
+ | f_block_arg
1571
+ {
1572
+ line = lexer.lineno
1573
+ result = call_args val # TODO: push line down
1574
+ result.line line
1575
+ }
1576
+
1577
+ opt_block_args_tail: tCOMMA block_args_tail
1578
+ {
1579
+ result = args val
1580
+ }
1581
+ | none
1582
+
1583
+ block_param: f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg opt_block_args_tail
1584
+ {
1585
+ result = args val
1586
+ }
1587
+ | f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1588
+ {
1589
+ result = args val
1590
+ }
1591
+ | f_arg tCOMMA f_block_optarg opt_block_args_tail
1592
+ {
1593
+ result = args val
1594
+ }
1595
+ | f_arg tCOMMA f_block_optarg tCOMMA f_arg opt_block_args_tail
1596
+ {
1597
+ result = args val
1598
+ }
1599
+ | f_arg tCOMMA f_rest_arg opt_block_args_tail
1600
+ {
1601
+ result = args val
1602
+ }
1603
+ | f_arg tCOMMA
1604
+ {
1605
+ result = args(val) << nil
1606
+ }
1607
+ | f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1608
+ {
1609
+ result = args val
1610
+ }
1611
+ | f_arg opt_block_args_tail
1612
+ {
1613
+ result = args val
1614
+ }
1615
+ | f_block_optarg tCOMMA f_rest_arg opt_block_args_tail
1616
+ {
1617
+ result = args val
1618
+ }
1619
+ | f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1620
+ {
1621
+ result = args val
1622
+ }
1623
+ | f_block_optarg opt_block_args_tail
1624
+ {
1625
+ result = args val
1626
+ }
1627
+ | f_block_optarg tCOMMA f_arg opt_block_args_tail
1628
+ {
1629
+ result = args val
1630
+ }
1631
+ | f_rest_arg opt_block_args_tail
1632
+ {
1633
+ result = args val
1634
+ }
1635
+ | f_rest_arg tCOMMA f_arg opt_block_args_tail
1636
+ {
1637
+ result = args val
1638
+ }
1639
+ | block_args_tail
1640
+ {
1641
+ result = args val
1642
+ }
1643
+
1644
+ opt_block_param: none { result = 0 }
1645
+ | block_param_def
1646
+ {
1647
+ self.lexer.command_start = true
1648
+ }
1649
+
1650
+ block_param_def: tPIPE opt_bv_decl tPIPE
1651
+ {
1652
+ # TODO: current_arg = 0
1653
+ result = args val
1654
+ }
1655
+ | tOROP
1656
+ {
1657
+ result = s(:args).line lexer.lineno
1658
+ }
1659
+ | tPIPE block_param opt_bv_decl tPIPE
1660
+ {
1661
+ # TODO: current_arg = 0
1662
+ result = args val
1663
+ }
1664
+
1665
+ opt_bv_decl: opt_nl
1666
+ | opt_nl tSEMI bv_decls opt_nl
1667
+ {
1668
+ result = args val
1669
+ }
1670
+
1671
+ bv_decls: bvar
1672
+ {
1673
+ result = args val
1674
+ }
1675
+ | bv_decls tCOMMA bvar
1676
+ {
1677
+ result = args val
1678
+ }
1679
+
1680
+ bvar: tIDENTIFIER
1681
+ {
1682
+ id, = val
1683
+ line = lexer.lineno
1684
+ result = s(:shadow, id.to_sym).line line
1685
+ }
1686
+ | f_bad_arg
1687
+
1688
+ lambda: {
1689
+ self.env.extend :dynamic
1690
+ result = [lexer.lineno, lexer.lpar_beg]
1691
+ lexer.paren_nest += 1
1692
+ lexer.lpar_beg = lexer.paren_nest
1693
+ }
1694
+ f_larglist
1695
+ {
1696
+ lexer.cmdarg.push false
1697
+ }
1698
+ lambda_body
1699
+ {
1700
+ (line, lpar), args, _cmdarg, body = val
1701
+ lexer.lpar_beg = lpar
1702
+
1703
+ lexer.cmdarg.pop
1704
+
1705
+ call = s(:lambda).line line
1706
+ result = new_iter call, args, body
1707
+ result.line = line
1708
+ self.env.unextend # TODO: dynapush & dynapop
1709
+ }
1710
+
1711
+ f_larglist: tLPAREN2 f_args opt_bv_decl rparen
1712
+ {
1713
+ result = args val
1714
+ }
1715
+ | f_args
1716
+ {
1717
+ result = val[0]
1718
+ result = 0 if result == s(:args)
1719
+ }
1720
+
1721
+ lambda_body: tLAMBEG compstmt tRCURLY
1722
+ {
1723
+ result = val[1]
1724
+ }
1725
+ | kDO_LAMBDA bodystmt kEND
1726
+ {
1727
+ result = val[1]
1728
+ }
1729
+
1730
+ do_block: k_do_block do_body kEND
1731
+ {
1732
+ (_, line), iter, _ = val
1733
+ result = iter.line line
1734
+ }
1735
+
1736
+ block_call: command do_block
1737
+ {
1738
+ # TODO:
1739
+ ## if (nd_type($1) == NODE_YIELD) {
1740
+ ## compile_error(PARSER_ARG "block given to yield");
1741
+
1742
+ syntax_error "Both block arg and actual block given." if
1743
+ val[0].block_pass?
1744
+
1745
+ val = invert_block_call val if inverted? val
1746
+
1747
+ cmd, blk = val
1748
+
1749
+ result = blk
1750
+ result.insert 1, cmd
1751
+ }
1752
+ | block_call call_op2 operation2 opt_paren_args
1753
+ {
1754
+ result = new_call val[0], val[2].to_sym, val[3]
1755
+ }
1756
+ | block_call call_op2 operation2 opt_paren_args brace_block
1757
+ {
1758
+ iter1, _, name, args, iter2 = val
1759
+
1760
+ call = new_call iter1, name.to_sym, args
1761
+ iter2.insert 1, call
1762
+
1763
+ result = iter2
1764
+ }
1765
+ | block_call call_op2 operation2 command_args do_block
1766
+ {
1767
+ iter1, _, name, args, iter2 = val
1768
+
1769
+ call = new_call iter1, name.to_sym, args
1770
+ iter2.insert 1, call
1771
+
1772
+ result = iter2
1773
+ }
1774
+
1775
+ method_call: fcall
1776
+ {
1777
+ result = self.lexer.lineno
1778
+ }
1779
+ paren_args
1780
+ {
1781
+ call, lineno, args = val
1782
+
1783
+ result = call.concat args.sexp_body if args
1784
+ result.line lineno
1785
+ }
1786
+ | primary_value call_op operation2 opt_paren_args
1787
+ {
1788
+ result = new_call val[0], val[2].to_sym, val[3], val[1]
1789
+ }
1790
+ | primary_value tCOLON2 operation2 paren_args
1791
+ {
1792
+ result = new_call val[0], val[2].to_sym, val[3]
1793
+ }
1794
+ | primary_value tCOLON2 operation3
1795
+ {
1796
+ result = new_call val[0], val[2].to_sym
1797
+ }
1798
+ | primary_value call_op paren_args
1799
+ {
1800
+ result = new_call val[0], :call, val[2], val[1]
1801
+ }
1802
+ | primary_value tCOLON2 paren_args
1803
+ {
1804
+ result = new_call val[0], :call, val[2]
1805
+ }
1806
+ | kSUPER paren_args
1807
+ {
1808
+ result = new_super val[1]
1809
+ }
1810
+ | kSUPER
1811
+ {
1812
+ result = s(:zsuper).line lexer.lineno
1813
+ }
1814
+ | primary_value tLBRACK2 opt_call_args rbracket
1815
+ {
1816
+ result = new_aref val
1817
+ }
1818
+
1819
+ brace_block: tLCURLY
1820
+ {
1821
+ self.env.extend :dynamic
1822
+ result = self.lexer.lineno
1823
+ }
1824
+ brace_body tRCURLY
1825
+ {
1826
+ _, line, body, _ = val
1827
+
1828
+ result = body
1829
+ result.line = line
1830
+
1831
+ self.env.unextend
1832
+ }
1833
+ | k_do
1834
+ {
1835
+ self.env.extend :dynamic
1836
+ result = self.lexer.lineno
1837
+ }
1838
+ do_body kEND
1839
+ {
1840
+ _, line, body, _ = val
1841
+
1842
+ result = body
1843
+ result.line = line
1844
+
1845
+ self.env.unextend
1846
+ }
1847
+
1848
+ brace_body: { self.env.extend :dynamic; result = self.lexer.lineno }
1849
+ { result = lexer.cmdarg.store(false) }
1850
+ opt_block_param compstmt
1851
+ {
1852
+ line, cmdarg, param, cmpstmt = val
1853
+
1854
+ result = new_brace_body param, cmpstmt, line
1855
+ self.env.unextend
1856
+ lexer.cmdarg.restore cmdarg
1857
+ lexer.cmdarg.pop # because of: cmdarg_stack >> 1 ?
1858
+ }
1859
+
1860
+ do_body: { self.env.extend :dynamic; result = self.lexer.lineno }
1861
+ { lexer.cmdarg.push false }
1862
+ opt_block_param
1863
+ bodystmt
1864
+ {
1865
+ line, _cmdarg, param, cmpstmt = val
1866
+
1867
+ result = new_do_body param, cmpstmt, line
1868
+ lexer.cmdarg.pop
1869
+ self.env.unextend
1870
+ }
1871
+
1872
+ case_body: k_when
1873
+ {
1874
+ result = self.lexer.lineno
1875
+ }
1876
+ args then compstmt cases
1877
+ {
1878
+ result = new_when(val[2], val[4])
1879
+ result.line = val[1]
1880
+ result << val[5] if val[5]
1881
+ }
1882
+
1883
+ cases: opt_else | case_body
1884
+
1885
+ opt_rescue: k_rescue exc_list exc_var then compstmt opt_rescue
1886
+ {
1887
+ (_, line), klasses, var, _, body, rest = val
1888
+
1889
+ klasses ||= s(:array)
1890
+ klasses << new_assign(var, s(:gvar, :"$!").line(var.line)) if var
1891
+ klasses.line line
1892
+
1893
+ result = new_resbody(klasses, body)
1894
+ result << rest if rest # UGH, rewritten above
1895
+ }
1896
+ |
1897
+ {
1898
+ result = nil
1899
+ }
1900
+
1901
+ exc_list: arg_value
1902
+ {
1903
+ arg, = val
1904
+ result = s(:array, arg).line arg.line
1905
+ }
1906
+ | mrhs
1907
+ | none
1908
+
1909
+ exc_var: tASSOC lhs
1910
+ {
1911
+ result = val[1]
1912
+ }
1913
+ | none
1914
+
1915
+ opt_ensure: k_ensure compstmt
1916
+ {
1917
+ (_, line), body = val
1918
+
1919
+ result = body || s(:nil).line(line)
1920
+ }
1921
+ | none
1922
+
1923
+ literal: numeric
1924
+ {
1925
+ line = lexer.lineno
1926
+ result = s(:lit, val[0])
1927
+ result.line = line
1928
+ }
1929
+ | symbol
1930
+ {
1931
+ line = lexer.lineno
1932
+ result = s(:lit, val[0])
1933
+ result.line = line
1934
+ }
1935
+ | dsym
1936
+
1937
+ strings: string
1938
+ {
1939
+ str, = val
1940
+ str = s(:dstr, str.value) if str.sexp_type == :evstr
1941
+ result = str
1942
+ }
1943
+
1944
+ string: tCHAR
1945
+ {
1946
+ debug20 23, val, result
1947
+ }
1948
+ | string1
1949
+ | string string1
1950
+ {
1951
+ result = self.literal_concat val[0], val[1]
1952
+ }
1953
+
1954
+ string1: tSTRING_BEG string_contents tSTRING_END
1955
+ {
1956
+ _, str, (_, func) = val
1957
+
1958
+ str = dedent str if func =~ RubyLexer::STR_FUNC_ICNTNT
1959
+
1960
+ result = str
1961
+ }
1962
+ | tSTRING
1963
+ {
1964
+ result = new_string val
1965
+ }
1966
+
1967
+ xstring: tXSTRING_BEG xstring_contents tSTRING_END
1968
+ {
1969
+ result = new_xstring val
1970
+ # TODO: dedent?!?! SERIOUSLY?!?
1971
+ }
1972
+
1973
+ regexp: tREGEXP_BEG regexp_contents tREGEXP_END
1974
+ {
1975
+ result = new_regexp val
1976
+ }
1977
+
1978
+ words: tWORDS_BEG tSPACE tSTRING_END
1979
+ {
1980
+ result = s(:array).line lexer.lineno
1981
+ }
1982
+ | tWORDS_BEG word_list tSTRING_END
1983
+ {
1984
+ result = val[1]
1985
+ }
1986
+
1987
+ word_list: none
1988
+ {
1989
+ result = new_word_list
1990
+ }
1991
+ | word_list word tSPACE
1992
+ {
1993
+ result = val[0].dup << new_word_list_entry(val)
1994
+ }
1995
+
1996
+ word: string_content
1997
+ | word string_content
1998
+ {
1999
+ result = self.literal_concat val[0], val[1]
2000
+ }
2001
+
2002
+ symbols: tSYMBOLS_BEG tSPACE tSTRING_END
2003
+ {
2004
+ result = s(:array).line lexer.lineno
2005
+ }
2006
+ | tSYMBOLS_BEG { result = lexer.lineno } symbol_list tSTRING_END
2007
+ {
2008
+ _, line, list, _, = val
2009
+ list.line = line
2010
+ result = list
2011
+ }
2012
+
2013
+ symbol_list: none
2014
+ {
2015
+ result = new_symbol_list.line lexer.lineno
2016
+ }
2017
+ | symbol_list word tSPACE
2018
+ {
2019
+ list, * = val
2020
+ result = list.dup << new_symbol_list_entry(val)
2021
+ }
2022
+
2023
+ qwords: tQWORDS_BEG tSPACE tSTRING_END
2024
+ {
2025
+ result = s(:array).line lexer.lineno
2026
+ }
2027
+ | tQWORDS_BEG qword_list tSTRING_END
2028
+ {
2029
+ result = val[1]
2030
+ }
2031
+
2032
+ qsymbols: tQSYMBOLS_BEG tSPACE tSTRING_END
2033
+ {
2034
+ result = s(:array).line lexer.lineno # FIX
2035
+ }
2036
+ | tQSYMBOLS_BEG qsym_list tSTRING_END
2037
+ {
2038
+ result = val[1]
2039
+ }
2040
+
2041
+ qword_list: none
2042
+ {
2043
+ result = new_qword_list
2044
+ }
2045
+ | qword_list tSTRING_CONTENT tSPACE
2046
+ {
2047
+ result = val[0].dup << new_qword_list_entry(val)
2048
+ }
2049
+
2050
+ qsym_list: none
2051
+ {
2052
+ result = new_qsym_list
2053
+ }
2054
+ | qsym_list tSTRING_CONTENT tSPACE
2055
+ {
2056
+ result = val[0].dup << new_qsym_list_entry(val)
2057
+ }
2058
+
2059
+ string_contents: none
2060
+ {
2061
+ result = s(:str, "").line lexer.lineno
2062
+ }
2063
+ | string_contents string_content
2064
+ {
2065
+ v1, v2 = val
2066
+ result = literal_concat v1, v2
2067
+ }
2068
+
2069
+ xstring_contents: none
2070
+ {
2071
+ result = nil
2072
+ }
2073
+ | xstring_contents string_content
2074
+ {
2075
+ v1, v2 = val
2076
+ result = literal_concat v1, v2
2077
+ }
2078
+
2079
+ regexp_contents: none
2080
+ {
2081
+ result = nil
2082
+ }
2083
+ | regexp_contents string_content
2084
+ {
2085
+ v1, v2 = val
2086
+ result = literal_concat v1, v2
2087
+ }
2088
+
2089
+ string_content: tSTRING_CONTENT
2090
+ {
2091
+ result = new_string val
2092
+ }
2093
+ | tSTRING_DVAR
2094
+ {
2095
+ result = lexer.lex_strterm
2096
+
2097
+ lexer.lex_strterm = nil
2098
+ lexer.lex_state = EXPR_BEG
2099
+ }
2100
+ string_dvar
2101
+ {
2102
+ _, strterm, str = val
2103
+ lexer.lex_strterm = strterm
2104
+ result = s(:evstr, str).line str.line
2105
+ }
2106
+ | tSTRING_DBEG
2107
+ {
2108
+ result = [lexer.lex_strterm,
2109
+ lexer.brace_nest,
2110
+ lexer.string_nest, # TODO: remove
2111
+ lexer.lex_state,
2112
+ lexer.lineno,
2113
+ ]
2114
+
2115
+ lexer.cmdarg.push false
2116
+ lexer.cond.push false
2117
+
2118
+ lexer.lex_strterm = nil
2119
+ lexer.brace_nest = 0
2120
+ lexer.string_nest = 0
2121
+
2122
+ lexer.lex_state = EXPR_BEG
2123
+ }
2124
+ compstmt
2125
+ tSTRING_DEND
2126
+ {
2127
+ _, memo, stmt, _ = val
2128
+
2129
+ lex_strterm, brace_nest, string_nest, oldlex_state, line = memo
2130
+ # TODO: heredoc_indent
2131
+
2132
+ lexer.lex_strterm = lex_strterm
2133
+ lexer.brace_nest = brace_nest
2134
+ lexer.string_nest = string_nest
2135
+
2136
+ lexer.cmdarg.pop
2137
+ lexer.cond.pop
2138
+
2139
+ lexer.lex_state = oldlex_state
2140
+
2141
+ case stmt
2142
+ when Sexp then
2143
+ case stmt.sexp_type
2144
+ when :str, :dstr, :evstr then
2145
+ result = stmt
2146
+ else
2147
+ result = s(:evstr, stmt).line line
2148
+ end
2149
+ when nil then
2150
+ result = s(:evstr).line line
2151
+ else
2152
+ debug20 25
2153
+ raise "unknown string body: #{stmt.inspect}"
2154
+ end
2155
+ }
2156
+
2157
+ string_dvar: tGVAR { result = s(:gvar, val[0].to_sym).line lexer.lineno }
2158
+ | tIVAR { result = s(:ivar, val[0].to_sym).line lexer.lineno }
2159
+ | tCVAR { result = s(:cvar, val[0].to_sym).line lexer.lineno }
2160
+ | backref
2161
+
2162
+ symbol: tSYMBEG sym
2163
+ {
2164
+ lexer.lex_state = EXPR_END
2165
+ result = val[1].to_sym
2166
+ }
2167
+ | tSYMBOL
2168
+ {
2169
+ result = val[0].to_sym
2170
+ }
2171
+
2172
+ sym: fname | tIVAR | tGVAR | tCVAR
2173
+
2174
+ dsym: tSYMBEG xstring_contents tSTRING_END
2175
+ {
2176
+ _, result, _ = val
2177
+
2178
+ lexer.lex_state = EXPR_END
2179
+
2180
+ result ||= s(:str, "").line lexer.lineno
2181
+
2182
+ case result.sexp_type
2183
+ when :dstr then
2184
+ result.sexp_type = :dsym
2185
+ when :str then
2186
+ result = s(:lit, result.last.to_sym).line result.line
2187
+ when :evstr then
2188
+ result = s(:dsym, "", result).line result.line
2189
+ else
2190
+ debug20 26, val, result
2191
+ end
2192
+ }
2193
+
2194
+ numeric: simple_numeric
2195
+ | tUMINUS_NUM simple_numeric
2196
+ {
2197
+ result = -val[1] # TODO: pt_testcase
2198
+ }
2199
+
2200
+ simple_numeric: tINTEGER
2201
+ | tFLOAT
2202
+ | tRATIONAL
2203
+ | tIMAGINARY
2204
+
2205
+ user_variable: tIDENTIFIER
2206
+ | tIVAR
2207
+ | tGVAR
2208
+ | tCONSTANT
2209
+ | tCVAR
2210
+
2211
+ keyword_variable: kNIL { result = s(:nil).line lexer.lineno }
2212
+ | kSELF { result = s(:self).line lexer.lineno }
2213
+ | kTRUE { result = s(:true).line lexer.lineno }
2214
+ | kFALSE { result = s(:false).line lexer.lineno }
2215
+ | k__FILE__ { result = s(:str, self.file).line lexer.lineno }
2216
+ | k__LINE__ { result = s(:lit, lexer.lineno).line lexer.lineno }
2217
+ | k__ENCODING__
2218
+ {
2219
+ l = lexer.lineno
2220
+ result =
2221
+ if defined? Encoding then
2222
+ s(:colon2, s(:const, :Encoding).line(l), :UTF_8).line l
2223
+ else
2224
+ s(:str, "Unsupported!").line l
2225
+ end
2226
+ }
2227
+
2228
+ var_ref: user_variable
2229
+ {
2230
+ var = val[0]
2231
+ result = Sexp === var ? var : self.gettable(var)
2232
+ }
2233
+ | keyword_variable
2234
+ {
2235
+ var = val[0]
2236
+ result = Sexp === var ? var : self.gettable(var)
2237
+ }
2238
+
2239
+ var_lhs: user_variable
2240
+ {
2241
+ result = self.assignable val[0]
2242
+ }
2243
+ | keyword_variable
2244
+ {
2245
+ result = self.assignable val[0]
2246
+ debug20 29, val, result
2247
+ }
2248
+
2249
+ backref: tNTH_REF { result = s(:nth_ref, val[0]).line lexer.lineno }
2250
+ | tBACK_REF { result = s(:back_ref, val[0]).line lexer.lineno }
2251
+
2252
+ superclass: tLT
2253
+ {
2254
+ lexer.lex_state = EXPR_BEG
2255
+ lexer.command_start = true
2256
+ }
2257
+ expr_value term
2258
+ {
2259
+ result = val[2]
2260
+ }
2261
+ | none
2262
+ {
2263
+ result = nil
2264
+ }
2265
+
2266
+ f_arglist: tLPAREN2 f_args rparen
2267
+ {
2268
+ result = val[1]
2269
+ self.lexer.lex_state = EXPR_BEG
2270
+ self.lexer.command_start = true
2271
+ }
2272
+ | {
2273
+ result = self.in_kwarg
2274
+ self.in_kwarg = true
2275
+ self.lexer.lex_state |= EXPR_LABEL
2276
+ }
2277
+ f_args term
2278
+ {
2279
+ kwarg, args, _ = val
2280
+
2281
+ self.in_kwarg = kwarg
2282
+ result = args
2283
+ lexer.lex_state = EXPR_BEG
2284
+ lexer.command_start = true
2285
+ }
2286
+
2287
+ args_tail: f_kwarg tCOMMA f_kwrest opt_f_block_arg
2288
+ {
2289
+ result = args val
2290
+ }
2291
+ | f_kwarg opt_f_block_arg
2292
+ {
2293
+ result = args val
2294
+ }
2295
+ | f_kwrest opt_f_block_arg
2296
+ {
2297
+ result = args val
2298
+ }
2299
+ | f_block_arg
2300
+
2301
+ opt_args_tail: tCOMMA args_tail
2302
+ {
2303
+ result = val[1]
2304
+ }
2305
+ |
2306
+ {
2307
+ result = nil
2308
+ }
2309
+
2310
+ f_args: f_arg tCOMMA f_optarg tCOMMA f_rest_arg opt_args_tail
2311
+ {
2312
+ result = args val
2313
+ }
2314
+ | f_arg tCOMMA f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2315
+ {
2316
+ result = args val
2317
+ }
2318
+ | f_arg tCOMMA f_optarg opt_args_tail
2319
+ {
2320
+ result = args val
2321
+ }
2322
+ | f_arg tCOMMA f_optarg tCOMMA f_arg opt_args_tail
2323
+ {
2324
+ result = args val
2325
+ }
2326
+ | f_arg tCOMMA f_rest_arg opt_args_tail
2327
+ {
2328
+ result = args val
2329
+ }
2330
+ | f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2331
+ {
2332
+ result = args val
2333
+ }
2334
+ | f_arg opt_args_tail
2335
+ {
2336
+ result = args val
2337
+ }
2338
+ | f_optarg tCOMMA f_rest_arg opt_args_tail
2339
+ {
2340
+ result = args val
2341
+ }
2342
+ | f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2343
+ {
2344
+ result = args val
2345
+ }
2346
+ | f_optarg opt_args_tail
2347
+ {
2348
+ result = args val
2349
+ }
2350
+ | f_optarg tCOMMA f_arg opt_args_tail
2351
+ {
2352
+ result = args val
2353
+ }
2354
+ | f_rest_arg opt_args_tail
2355
+ {
2356
+ result = args val
2357
+ }
2358
+ | f_rest_arg tCOMMA f_arg opt_args_tail
2359
+ {
2360
+ result = args val
2361
+ }
2362
+ | args_tail
2363
+ {
2364
+ result = args val
2365
+ }
2366
+ |
2367
+ {
2368
+ result = args val
2369
+ }
2370
+
2371
+ f_bad_arg: tCONSTANT
2372
+ {
2373
+ yyerror "formal argument cannot be a constant"
2374
+ }
2375
+ | tIVAR
2376
+ {
2377
+ yyerror "formal argument cannot be an instance variable"
2378
+ }
2379
+ | tGVAR
2380
+ {
2381
+ yyerror "formal argument cannot be a global variable"
2382
+ }
2383
+ | tCVAR
2384
+ {
2385
+ yyerror "formal argument cannot be a class variable"
2386
+ }
2387
+
2388
+ f_norm_arg: f_bad_arg
2389
+ | tIDENTIFIER
2390
+ {
2391
+ identifier = val[0].to_sym
2392
+ self.env[identifier] = :lvar
2393
+
2394
+ result = identifier
2395
+ }
2396
+
2397
+ f_arg_asgn: f_norm_arg
2398
+
2399
+ f_arg_item: f_arg_asgn
2400
+ | tLPAREN f_margs rparen
2401
+ {
2402
+ result = val[1]
2403
+ }
2404
+
2405
+ f_arg: f_arg_item
2406
+ {
2407
+ arg, = val
2408
+
2409
+ case arg
2410
+ when Symbol then
2411
+ result = s(:args, arg).line lexer.lineno
2412
+ when Sexp then
2413
+ result = arg
2414
+ else
2415
+ debug20 32
2416
+ raise "Unknown f_arg type: #{val.inspect}"
2417
+ end
2418
+ }
2419
+ | f_arg tCOMMA f_arg_item
2420
+ {
2421
+ list, _, item = val
2422
+
2423
+ if list.sexp_type == :args then
2424
+ result = list
2425
+ else
2426
+ result = s(:args, list).line list.line
2427
+ end
2428
+
2429
+ result << item
2430
+ }
2431
+
2432
+ f_label: tLABEL
2433
+
2434
+ f_kw: f_label arg_value
2435
+ {
2436
+ # TODO: new_kw_arg
2437
+ (label, line), arg = val
2438
+
2439
+ identifier = label.to_sym
2440
+ self.env[identifier] = :lvar
2441
+
2442
+ kwarg = s(:kwarg, identifier, arg).line line
2443
+ result = s(:array, kwarg).line line
2444
+ }
2445
+ | f_label
2446
+ {
2447
+ (label, line), = val
2448
+
2449
+ id = label.to_sym
2450
+ self.env[id] = :lvar
2451
+
2452
+ result = s(:array, s(:kwarg, id).line(line)).line line
2453
+ }
2454
+
2455
+ f_block_kw: f_label primary_value
2456
+ {
2457
+ # TODO: new_kw_arg
2458
+ (label, line), expr = val
2459
+ id = label.to_sym
2460
+ self.env[id] = :lvar
2461
+
2462
+ result = s(:array, s(:kwarg, id, expr).line(line)).line line
2463
+ }
2464
+ | f_label
2465
+ {
2466
+ # TODO: new_kw_arg
2467
+ (label, line), = val
2468
+ id = label.to_sym
2469
+ self.env[id] = :lvar
2470
+
2471
+ result = s(:array, s(:kwarg, id).line(line)).line line
2472
+ }
2473
+
2474
+ f_block_kwarg: f_block_kw
2475
+ | f_block_kwarg tCOMMA f_block_kw
2476
+ {
2477
+ list, _, item = val
2478
+ result = list << item.last
2479
+ }
2480
+
2481
+ f_kwarg: f_kw
2482
+ | f_kwarg tCOMMA f_kw
2483
+ {
2484
+ result = args val
2485
+ }
2486
+
2487
+ kwrest_mark: tPOW
2488
+ | tDSTAR
2489
+
2490
+ f_kwrest: kwrest_mark tIDENTIFIER
2491
+ {
2492
+ name = val[1].to_sym
2493
+ self.assignable name
2494
+ result = :"**#{name}"
2495
+ }
2496
+ | kwrest_mark
2497
+ {
2498
+ result = :"**"
2499
+ }
2500
+
2501
+ f_opt: f_arg_asgn tEQL arg_value
2502
+ {
2503
+ result = self.assignable val[0], val[2]
2504
+ # TODO: detect duplicate names
2505
+ }
2506
+
2507
+ f_block_opt: f_arg_asgn tEQL primary_value
2508
+ {
2509
+ result = self.assignable val[0], val[2]
2510
+ }
2511
+
2512
+ f_block_optarg: f_block_opt
2513
+ {
2514
+ optblk, = val
2515
+ result = s(:block, optblk).line optblk.line
2516
+ }
2517
+ | f_block_optarg tCOMMA f_block_opt
2518
+ {
2519
+ optarg, _, optblk = val
2520
+ result = optarg
2521
+ result << optblk
2522
+ }
2523
+
2524
+ f_optarg: f_opt
2525
+ {
2526
+ opt, = val
2527
+ result = s(:block, opt).line opt.line
2528
+ }
2529
+ | f_optarg tCOMMA f_opt
2530
+ {
2531
+ result = self.block_append val[0], val[2]
2532
+ }
2533
+
2534
+ restarg_mark: tSTAR2 | tSTAR
2535
+
2536
+ f_rest_arg: restarg_mark tIDENTIFIER
2537
+ {
2538
+ # TODO: differs from parse.y - needs tests
2539
+ name = val[1].to_sym
2540
+ self.assignable name
2541
+ result = :"*#{name}"
2542
+ }
2543
+ | restarg_mark
2544
+ {
2545
+ name = :"*"
2546
+ self.env[name] = :lvar
2547
+ result = name
2548
+ }
2549
+
2550
+ blkarg_mark: tAMPER2 | tAMPER
2551
+
2552
+ f_block_arg: blkarg_mark tIDENTIFIER
2553
+ {
2554
+ identifier = val[1].to_sym
2555
+
2556
+ self.env[identifier] = :lvar
2557
+ result = "&#{identifier}".to_sym
2558
+ }
2559
+
2560
+ opt_f_block_arg: tCOMMA f_block_arg
2561
+ {
2562
+ result = val[1]
2563
+ }
2564
+ |
2565
+ {
2566
+ result = nil
2567
+ }
2568
+
2569
+ singleton: var_ref
2570
+ | tLPAREN2
2571
+ {
2572
+ lexer.lex_state = EXPR_BEG
2573
+ }
2574
+ expr rparen
2575
+ {
2576
+ result = val[2]
2577
+ yyerror "Can't define single method for literals." if
2578
+ result.sexp_type == :lit
2579
+ }
2580
+
2581
+ assoc_list: none
2582
+ {
2583
+ result = s(:array).line lexer.lineno
2584
+ }
2585
+ | assocs trailer
2586
+
2587
+ assocs: assoc
2588
+ | assocs tCOMMA assoc
2589
+ {
2590
+ list = val[0].dup
2591
+ more = val[2].sexp_body
2592
+ list.push(*more) unless more.empty?
2593
+ result = list
2594
+ result.sexp_type = :hash
2595
+ }
2596
+
2597
+ assoc: arg_value tASSOC arg_value
2598
+ {
2599
+ v1, _, v2 = val
2600
+ result = s(:array, v1, v2).line v1.line
2601
+ }
2602
+ | tLABEL arg_value
2603
+ {
2604
+ (label, line), arg = val
2605
+
2606
+ lit = s(:lit, label.to_sym).line line
2607
+ result = s(:array, lit, arg).line line
2608
+ }
2609
+ | tSTRING_BEG string_contents tLABEL_END arg_value
2610
+ {
2611
+ _, sym, _, value = val
2612
+ sym.sexp_type = :dsym
2613
+ result = s(:array, sym, value).line sym.line
2614
+ }
2615
+ | tDSTAR arg_value
2616
+ {
2617
+ _, arg = val
2618
+ line = arg.line
2619
+ result = s(:array, s(:kwsplat, arg).line(line)).line line
2620
+ }
2621
+
2622
+ operation: tIDENTIFIER | tCONSTANT | tFID
2623
+ operation2: tIDENTIFIER | tCONSTANT | tFID | op
2624
+ operation3: tIDENTIFIER | tFID | op
2625
+ dot_or_colon: tDOT | tCOLON2
2626
+ call_op: tDOT
2627
+ | tLONELY # TODO: rename tANDDOT?
2628
+
2629
+ call_op2: call_op
2630
+ | tCOLON2
2631
+
2632
+ opt_terms: | terms
2633
+ opt_nl: | tNL
2634
+ rparen: opt_nl tRPAREN
2635
+ rbracket: opt_nl tRBRACK
2636
+ trailer: | tNL | tCOMMA
2637
+
2638
+ term: tSEMI { yyerrok }
2639
+ | tNL
2640
+
2641
+ terms: term
2642
+ | terms tSEMI { yyerrok }
2643
+
2644
+ none: { result = nil; }
2645
+ end
2646
+
2647
+ ---- inner
2648
+
2649
+ require "ruby_lexer"
2650
+ require "ruby_parser_extras"
2651
+ include RubyLexer::State::Values
2652
+
2653
+ # :stopdoc:
2654
+
2655
+ # Local Variables: **
2656
+ # racc-token-length-max:14 **
2657
+ # End: **