parser 2.7.1.0 → 2.7.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +10 -11
  3. data/CHANGELOG.md +70 -1
  4. data/README.md +2 -2
  5. data/Rakefile +2 -1
  6. data/doc/AST_FORMAT.md +107 -3
  7. data/lib/parser.rb +1 -0
  8. data/lib/parser/all.rb +1 -0
  9. data/lib/parser/ast/processor.rb +2 -0
  10. data/lib/parser/base.rb +6 -5
  11. data/lib/parser/builders/default.rb +146 -19
  12. data/lib/parser/context.rb +1 -0
  13. data/lib/parser/current.rb +9 -0
  14. data/lib/parser/diagnostic.rb +1 -1
  15. data/lib/parser/diagnostic/engine.rb +1 -2
  16. data/lib/parser/lexer.rb +23770 -0
  17. data/lib/parser/lexer.rl +15 -1
  18. data/lib/parser/macruby.rb +6149 -0
  19. data/lib/parser/macruby.y +15 -5
  20. data/lib/parser/max_numparam_stack.rb +1 -1
  21. data/lib/parser/messages.rb +17 -0
  22. data/lib/parser/meta.rb +3 -3
  23. data/lib/parser/ruby18.rb +5663 -0
  24. data/lib/parser/ruby18.y +3 -1
  25. data/lib/parser/ruby19.rb +6092 -0
  26. data/lib/parser/ruby19.y +15 -5
  27. data/lib/parser/ruby20.rb +6527 -0
  28. data/lib/parser/ruby20.y +15 -5
  29. data/lib/parser/ruby21.rb +6578 -0
  30. data/lib/parser/ruby21.y +10 -3
  31. data/lib/parser/ruby22.rb +6613 -0
  32. data/lib/parser/ruby22.y +10 -3
  33. data/lib/parser/ruby23.rb +6624 -0
  34. data/lib/parser/ruby23.y +10 -3
  35. data/lib/parser/ruby24.rb +6694 -0
  36. data/lib/parser/ruby24.y +10 -3
  37. data/lib/parser/ruby25.rb +6662 -0
  38. data/lib/parser/ruby25.y +10 -3
  39. data/lib/parser/ruby26.rb +6676 -0
  40. data/lib/parser/ruby26.y +10 -3
  41. data/lib/parser/ruby27.rb +7803 -0
  42. data/lib/parser/ruby27.y +16 -8
  43. data/lib/parser/ruby30.rb +8052 -0
  44. data/lib/parser/ruby30.y +3048 -0
  45. data/lib/parser/rubymotion.rb +6086 -0
  46. data/lib/parser/rubymotion.y +15 -5
  47. data/lib/parser/runner.rb +26 -2
  48. data/lib/parser/runner/ruby_rewrite.rb +2 -2
  49. data/lib/parser/source/buffer.rb +3 -1
  50. data/lib/parser/source/comment.rb +1 -1
  51. data/lib/parser/source/comment/associator.rb +14 -4
  52. data/lib/parser/source/map/method_definition.rb +25 -0
  53. data/lib/parser/source/range.rb +19 -3
  54. data/lib/parser/source/tree_rewriter.rb +115 -12
  55. data/lib/parser/source/tree_rewriter/action.rb +135 -26
  56. data/lib/parser/tree_rewriter.rb +1 -2
  57. data/lib/parser/version.rb +1 -1
  58. data/parser.gemspec +6 -2
  59. data/test/helper.rb +49 -6
  60. data/test/parse_helper.rb +49 -40
  61. data/test/test_ast_processor.rb +32 -0
  62. data/test/test_base.rb +1 -1
  63. data/test/test_current.rb +2 -0
  64. data/test/test_diagnostic.rb +6 -7
  65. data/test/test_diagnostic_engine.rb +5 -8
  66. data/test/test_lexer.rb +17 -8
  67. data/test/test_meta.rb +12 -0
  68. data/test/test_parse_helper.rb +12 -9
  69. data/test/test_parser.rb +612 -56
  70. data/test/test_runner_parse.rb +22 -1
  71. data/test/test_runner_rewrite.rb +1 -1
  72. data/test/test_source_buffer.rb +4 -1
  73. data/test/test_source_comment.rb +2 -2
  74. data/test/test_source_comment_associator.rb +47 -15
  75. data/test/test_source_map.rb +1 -2
  76. data/test/test_source_range.rb +29 -9
  77. data/test/test_source_rewriter.rb +4 -4
  78. data/test/test_source_rewriter_action.rb +2 -2
  79. data/test/test_source_tree_rewriter.rb +201 -13
  80. metadata +19 -13
  81. data/.gitignore +0 -33
@@ -0,0 +1,3048 @@
1
+ class Parser::Ruby30
2
+
3
+ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
4
+ kTHEN kELSIF kELSE kCASE kWHEN kWHILE kUNTIL kFOR kBREAK kNEXT
5
+ kREDO kRETRY kIN kDO kDO_COND kDO_BLOCK kDO_LAMBDA kRETURN kYIELD kSUPER
6
+ kSELF kNIL kTRUE kFALSE kAND kOR kNOT kIF_MOD kUNLESS_MOD kWHILE_MOD
7
+ kUNTIL_MOD kRESCUE_MOD kALIAS kDEFINED klBEGIN klEND k__LINE__
8
+ k__FILE__ k__ENCODING__ tIDENTIFIER tFID tGVAR tIVAR tCONSTANT
9
+ tLABEL tCVAR tNTH_REF tBACK_REF tSTRING_CONTENT tINTEGER tFLOAT
10
+ tUPLUS tUMINUS tUNARY_NUM tPOW tCMP tEQ tEQQ tNEQ
11
+ tGEQ tLEQ tANDOP tOROP tMATCH tNMATCH tDOT tDOT2 tDOT3 tAREF
12
+ tASET tLSHFT tRSHFT tCOLON2 tCOLON3 tOP_ASGN tASSOC tLPAREN
13
+ tLPAREN2 tRPAREN tLPAREN_ARG tLBRACK tLBRACK2 tRBRACK tLBRACE
14
+ tLBRACE_ARG tSTAR tSTAR2 tAMPER tAMPER2 tTILDE tPERCENT tDIVIDE
15
+ tDSTAR tPLUS tMINUS tLT tGT tPIPE tBANG tCARET tLCURLY tRCURLY
16
+ tBACK_REF2 tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tREGEXP_OPT
17
+ tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG tSTRING_DBEG
18
+ tSTRING_DVAR tSTRING_END tSTRING_DEND tSTRING tSYMBOL
19
+ tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA tLAMBEG tCHARACTER
20
+ tRATIONAL tIMAGINARY tLABEL_END tANDDOT tBDOT2 tBDOT3
21
+
22
+ prechigh
23
+ right tBANG tTILDE tUPLUS
24
+ right tPOW
25
+ right tUNARY_NUM tUMINUS
26
+ left tSTAR2 tDIVIDE tPERCENT
27
+ left tPLUS tMINUS
28
+ left tLSHFT tRSHFT
29
+ left tAMPER2
30
+ left tPIPE tCARET
31
+ left tGT tGEQ tLT tLEQ
32
+ nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
33
+ left tANDOP
34
+ left tOROP
35
+ nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
36
+ right tEH tCOLON
37
+ left kRESCUE_MOD
38
+ right tEQL tOP_ASGN
39
+ nonassoc kDEFINED
40
+ right kNOT
41
+ left kOR kAND
42
+ nonassoc kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD kIN
43
+ nonassoc tLBRACE_ARG
44
+ nonassoc tLOWEST
45
+ preclow
46
+
47
+ rule
48
+
49
+ program: top_compstmt
50
+
51
+ top_compstmt: top_stmts opt_terms
52
+ {
53
+ result = @builder.compstmt(val[0])
54
+ }
55
+
56
+ top_stmts: # nothing
57
+ {
58
+ result = []
59
+ }
60
+ | top_stmt
61
+ {
62
+ result = [ val[0] ]
63
+ }
64
+ | top_stmts terms top_stmt
65
+ {
66
+ result = val[0] << val[2]
67
+ }
68
+ | error top_stmt
69
+ {
70
+ result = [ val[1] ]
71
+ }
72
+
73
+ top_stmt: stmt
74
+ | klBEGIN begin_block
75
+ {
76
+ result = @builder.preexe(val[0], *val[1])
77
+ }
78
+
79
+ begin_block: tLCURLY top_compstmt tRCURLY
80
+ {
81
+ result = val
82
+ }
83
+
84
+ bodystmt: compstmt opt_rescue opt_else opt_ensure
85
+ {
86
+ rescue_bodies = val[1]
87
+ else_t, else_ = val[2]
88
+ ensure_t, ensure_ = val[3]
89
+
90
+ if rescue_bodies.empty? && !else_t.nil?
91
+ diagnostic :error, :useless_else, nil, else_t
92
+ end
93
+
94
+ result = @builder.begin_body(val[0],
95
+ rescue_bodies,
96
+ else_t, else_,
97
+ ensure_t, ensure_)
98
+ }
99
+
100
+ compstmt: stmts opt_terms
101
+ {
102
+ result = @builder.compstmt(val[0])
103
+ }
104
+
105
+ stmts: # nothing
106
+ {
107
+ result = []
108
+ }
109
+ | stmt_or_begin
110
+ {
111
+ result = [ val[0] ]
112
+ }
113
+ | stmts terms stmt_or_begin
114
+ {
115
+ result = val[0] << val[2]
116
+ }
117
+ | error stmt
118
+ {
119
+ result = [ val[1] ]
120
+ }
121
+
122
+ stmt_or_begin: stmt
123
+ | klBEGIN begin_block
124
+ {
125
+ diagnostic :error, :begin_in_method, nil, val[0]
126
+ }
127
+
128
+ stmt: kALIAS fitem
129
+ {
130
+ @lexer.state = :expr_fname
131
+ }
132
+ fitem
133
+ {
134
+ result = @builder.alias(val[0], val[1], val[3])
135
+ }
136
+ | kALIAS tGVAR tGVAR
137
+ {
138
+ result = @builder.alias(val[0],
139
+ @builder.gvar(val[1]),
140
+ @builder.gvar(val[2]))
141
+ }
142
+ | kALIAS tGVAR tBACK_REF
143
+ {
144
+ result = @builder.alias(val[0],
145
+ @builder.gvar(val[1]),
146
+ @builder.back_ref(val[2]))
147
+ }
148
+ | kALIAS tGVAR tNTH_REF
149
+ {
150
+ diagnostic :error, :nth_ref_alias, nil, val[2]
151
+ }
152
+ | kUNDEF undef_list
153
+ {
154
+ result = @builder.undef_method(val[0], val[1])
155
+ }
156
+ | stmt kIF_MOD expr_value
157
+ {
158
+ result = @builder.condition_mod(val[0], nil,
159
+ val[1], val[2])
160
+ }
161
+ | stmt kUNLESS_MOD expr_value
162
+ {
163
+ result = @builder.condition_mod(nil, val[0],
164
+ val[1], val[2])
165
+ }
166
+ | stmt kWHILE_MOD expr_value
167
+ {
168
+ result = @builder.loop_mod(:while, val[0], val[1], val[2])
169
+ }
170
+ | stmt kUNTIL_MOD expr_value
171
+ {
172
+ result = @builder.loop_mod(:until, val[0], val[1], val[2])
173
+ }
174
+ | stmt kRESCUE_MOD stmt
175
+ {
176
+ rescue_body = @builder.rescue_body(val[1],
177
+ nil, nil, nil,
178
+ nil, val[2])
179
+
180
+ result = @builder.begin_body(val[0], [ rescue_body ])
181
+ }
182
+ | klEND tLCURLY compstmt tRCURLY
183
+ {
184
+ result = @builder.postexe(val[0], val[1], val[2], val[3])
185
+ }
186
+ | command_asgn
187
+ | mlhs tEQL command_call
188
+ {
189
+ result = @builder.multi_assign(val[0], val[1], val[2])
190
+ }
191
+ | lhs tEQL mrhs
192
+ {
193
+ result = @builder.assign(val[0], val[1],
194
+ @builder.array(nil, val[2], nil))
195
+ }
196
+ | mlhs tEQL mrhs_arg kRESCUE_MOD stmt
197
+ {
198
+ rescue_body = @builder.rescue_body(val[3],
199
+ nil, nil, nil,
200
+ nil, val[4])
201
+ begin_body = @builder.begin_body(val[2], [ rescue_body ])
202
+
203
+ result = @builder.multi_assign(val[0], val[1], begin_body)
204
+ }
205
+ | mlhs tEQL mrhs_arg
206
+ {
207
+ result = @builder.multi_assign(val[0], val[1], val[2])
208
+ }
209
+ | rassign
210
+ | expr
211
+
212
+ rassign: arg_value tASSOC lhs
213
+ {
214
+ result = @builder.rassign(val[0], val[1], val[2])
215
+ }
216
+ | arg_value tASSOC mlhs
217
+ {
218
+ result = @builder.multi_rassign(val[0], val[1], val[2])
219
+ }
220
+ | rassign tASSOC lhs
221
+ {
222
+ result = @builder.rassign(val[0], val[1], val[2])
223
+ }
224
+ | rassign tASSOC mlhs
225
+ {
226
+ result = @builder.multi_rassign(val[0], val[1], val[2])
227
+ }
228
+
229
+ command_asgn: lhs tEQL command_rhs
230
+ {
231
+ result = @builder.assign(val[0], val[1], val[2])
232
+ }
233
+ | var_lhs tOP_ASGN command_rhs
234
+ {
235
+ result = @builder.op_assign(val[0], val[1], val[2])
236
+ }
237
+ | primary_value tLBRACK2 opt_call_args rbracket tOP_ASGN command_rhs
238
+ {
239
+ result = @builder.op_assign(
240
+ @builder.index(
241
+ val[0], val[1], val[2], val[3]),
242
+ val[4], val[5])
243
+ }
244
+ | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
245
+ {
246
+ result = @builder.op_assign(
247
+ @builder.call_method(
248
+ val[0], val[1], val[2]),
249
+ val[3], val[4])
250
+ }
251
+ | primary_value call_op tCONSTANT tOP_ASGN command_rhs
252
+ {
253
+ result = @builder.op_assign(
254
+ @builder.call_method(
255
+ val[0], val[1], val[2]),
256
+ val[3], val[4])
257
+ }
258
+ | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
259
+ {
260
+ const = @builder.const_op_assignable(
261
+ @builder.const_fetch(val[0], val[1], val[2]))
262
+ result = @builder.op_assign(const, val[3], val[4])
263
+ }
264
+ | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
265
+ {
266
+ result = @builder.op_assign(
267
+ @builder.call_method(
268
+ val[0], val[1], val[2]),
269
+ val[3], val[4])
270
+ }
271
+ | backref tOP_ASGN command_rhs
272
+ {
273
+ @builder.op_assign(val[0], val[1], val[2])
274
+ }
275
+
276
+ command_rhs: command_call =tOP_ASGN
277
+ | command_call kRESCUE_MOD stmt
278
+ {
279
+ rescue_body = @builder.rescue_body(val[1],
280
+ nil, nil, nil,
281
+ nil, val[2])
282
+
283
+ result = @builder.begin_body(val[0], [ rescue_body ])
284
+ }
285
+ | command_asgn
286
+
287
+ expr: command_call
288
+ | expr kAND expr
289
+ {
290
+ result = @builder.logical_op(:and, val[0], val[1], val[2])
291
+ }
292
+ | expr kOR expr
293
+ {
294
+ result = @builder.logical_op(:or, val[0], val[1], val[2])
295
+ }
296
+ | kNOT opt_nl expr
297
+ {
298
+ result = @builder.not_op(val[0], nil, val[2], nil)
299
+ }
300
+ | tBANG command_call
301
+ {
302
+ result = @builder.not_op(val[0], nil, val[1], nil)
303
+ }
304
+ | arg kIN
305
+ {
306
+ @lexer.state = :expr_beg
307
+ @lexer.command_start = false
308
+ pattern_variables.push
309
+
310
+ result = @lexer.in_kwarg
311
+ @lexer.in_kwarg = true
312
+ }
313
+ p_expr
314
+ {
315
+ @lexer.in_kwarg = val[2]
316
+ result = @builder.in_match(val[0], val[1], val[3])
317
+ }
318
+ | arg =tLBRACE_ARG
319
+
320
+ expr_value: expr
321
+
322
+ expr_value_do: {
323
+ @lexer.cond.push(true)
324
+ }
325
+ expr_value do
326
+ {
327
+ @lexer.cond.pop
328
+ result = [ val[1], val[2] ]
329
+ }
330
+
331
+ def_name: fname
332
+ {
333
+ @static_env.extend_static
334
+ @lexer.cmdarg.push(false)
335
+ @lexer.cond.push(false)
336
+ @current_arg_stack.push(nil)
337
+
338
+ result = val[0]
339
+ }
340
+
341
+ defn_head: kDEF def_name
342
+ {
343
+ @context.push(:def)
344
+
345
+ result = [ val[0], val[1] ]
346
+ }
347
+
348
+ defs_head: kDEF singleton dot_or_colon
349
+ {
350
+ @lexer.state = :expr_fname
351
+ }
352
+ def_name
353
+ {
354
+ @context.push(:defs)
355
+
356
+ result = [ val[0], val[1], val[2], val[4] ]
357
+ }
358
+
359
+
360
+ command_call: command
361
+ | block_command
362
+
363
+ block_command: block_call
364
+ | block_call dot_or_colon operation2 command_args
365
+ {
366
+ result = @builder.call_method(val[0], val[1], val[2],
367
+ nil, val[3], nil)
368
+ }
369
+
370
+ cmd_brace_block: tLBRACE_ARG
371
+ {
372
+ @context.push(:block)
373
+ }
374
+ brace_body tRCURLY
375
+ {
376
+ result = [ val[0], *val[2], val[3] ]
377
+ @context.pop
378
+ }
379
+
380
+ fcall: operation
381
+
382
+ command: fcall command_args =tLOWEST
383
+ {
384
+ result = @builder.call_method(nil, nil, val[0],
385
+ nil, val[1], nil)
386
+ }
387
+ | fcall command_args cmd_brace_block
388
+ {
389
+ method_call = @builder.call_method(nil, nil, val[0],
390
+ nil, val[1], nil)
391
+
392
+ begin_t, args, body, end_t = val[2]
393
+ result = @builder.block(method_call,
394
+ begin_t, args, body, end_t)
395
+ }
396
+ | primary_value call_op operation2 command_args =tLOWEST
397
+ {
398
+ result = @builder.call_method(val[0], val[1], val[2],
399
+ nil, val[3], nil)
400
+ }
401
+ | primary_value call_op operation2 command_args cmd_brace_block
402
+ {
403
+ method_call = @builder.call_method(val[0], val[1], val[2],
404
+ nil, val[3], nil)
405
+
406
+ begin_t, args, body, end_t = val[4]
407
+ result = @builder.block(method_call,
408
+ begin_t, args, body, end_t)
409
+ }
410
+ | primary_value tCOLON2 operation2 command_args =tLOWEST
411
+ {
412
+ result = @builder.call_method(val[0], val[1], val[2],
413
+ nil, val[3], nil)
414
+ }
415
+ | primary_value tCOLON2 operation2 command_args cmd_brace_block
416
+ {
417
+ method_call = @builder.call_method(val[0], val[1], val[2],
418
+ nil, val[3], nil)
419
+
420
+ begin_t, args, body, end_t = val[4]
421
+ result = @builder.block(method_call,
422
+ begin_t, args, body, end_t)
423
+ }
424
+ | kSUPER command_args
425
+ {
426
+ result = @builder.keyword_cmd(:super, val[0],
427
+ nil, val[1], nil)
428
+ }
429
+ | kYIELD command_args
430
+ {
431
+ result = @builder.keyword_cmd(:yield, val[0],
432
+ nil, val[1], nil)
433
+ }
434
+ | k_return call_args
435
+ {
436
+ result = @builder.keyword_cmd(:return, val[0],
437
+ nil, val[1], nil)
438
+ }
439
+ | kBREAK call_args
440
+ {
441
+ result = @builder.keyword_cmd(:break, val[0],
442
+ nil, val[1], nil)
443
+ }
444
+ | kNEXT call_args
445
+ {
446
+ result = @builder.keyword_cmd(:next, val[0],
447
+ nil, val[1], nil)
448
+ }
449
+
450
+ mlhs: mlhs_basic
451
+ {
452
+ result = @builder.multi_lhs(nil, val[0], nil)
453
+ }
454
+ | tLPAREN mlhs_inner rparen
455
+ {
456
+ result = @builder.begin(val[0], val[1], val[2])
457
+ }
458
+
459
+ mlhs_inner: mlhs_basic
460
+ {
461
+ result = @builder.multi_lhs(nil, val[0], nil)
462
+ }
463
+ | tLPAREN mlhs_inner rparen
464
+ {
465
+ result = @builder.multi_lhs(val[0], val[1], val[2])
466
+ }
467
+
468
+ mlhs_basic: mlhs_head
469
+ | mlhs_head mlhs_item
470
+ {
471
+ result = val[0].
472
+ push(val[1])
473
+ }
474
+ | mlhs_head tSTAR mlhs_node
475
+ {
476
+ result = val[0].
477
+ push(@builder.splat(val[1], val[2]))
478
+ }
479
+ | mlhs_head tSTAR mlhs_node tCOMMA mlhs_post
480
+ {
481
+ result = val[0].
482
+ push(@builder.splat(val[1], val[2])).
483
+ concat(val[4])
484
+ }
485
+ | mlhs_head tSTAR
486
+ {
487
+ result = val[0].
488
+ push(@builder.splat(val[1]))
489
+ }
490
+ | mlhs_head tSTAR tCOMMA mlhs_post
491
+ {
492
+ result = val[0].
493
+ push(@builder.splat(val[1])).
494
+ concat(val[3])
495
+ }
496
+ | tSTAR mlhs_node
497
+ {
498
+ result = [ @builder.splat(val[0], val[1]) ]
499
+ }
500
+ | tSTAR mlhs_node tCOMMA mlhs_post
501
+ {
502
+ result = [ @builder.splat(val[0], val[1]),
503
+ *val[3] ]
504
+ }
505
+ | tSTAR
506
+ {
507
+ result = [ @builder.splat(val[0]) ]
508
+ }
509
+ | tSTAR tCOMMA mlhs_post
510
+ {
511
+ result = [ @builder.splat(val[0]),
512
+ *val[2] ]
513
+ }
514
+
515
+ mlhs_item: mlhs_node
516
+ | tLPAREN mlhs_inner rparen
517
+ {
518
+ result = @builder.begin(val[0], val[1], val[2])
519
+ }
520
+
521
+ mlhs_head: mlhs_item tCOMMA
522
+ {
523
+ result = [ val[0] ]
524
+ }
525
+ | mlhs_head mlhs_item tCOMMA
526
+ {
527
+ result = val[0] << val[1]
528
+ }
529
+
530
+ mlhs_post: mlhs_item
531
+ {
532
+ result = [ val[0] ]
533
+ }
534
+ | mlhs_post tCOMMA mlhs_item
535
+ {
536
+ result = val[0] << val[2]
537
+ }
538
+
539
+ mlhs_node: user_variable
540
+ {
541
+ result = @builder.assignable(val[0])
542
+ }
543
+ | keyword_variable
544
+ {
545
+ result = @builder.assignable(val[0])
546
+ }
547
+ | primary_value tLBRACK2 opt_call_args rbracket
548
+ {
549
+ result = @builder.index_asgn(val[0], val[1], val[2], val[3])
550
+ }
551
+ | primary_value call_op tIDENTIFIER
552
+ {
553
+ if (val[1][0] == :anddot)
554
+ diagnostic :error, :csend_in_lhs_of_masgn, nil, val[1]
555
+ end
556
+
557
+ result = @builder.attr_asgn(val[0], val[1], val[2])
558
+ }
559
+ | primary_value tCOLON2 tIDENTIFIER
560
+ {
561
+ result = @builder.attr_asgn(val[0], val[1], val[2])
562
+ }
563
+ | primary_value call_op tCONSTANT
564
+ {
565
+ if (val[1][0] == :anddot)
566
+ diagnostic :error, :csend_in_lhs_of_masgn, nil, val[1]
567
+ end
568
+
569
+ result = @builder.attr_asgn(val[0], val[1], val[2])
570
+ }
571
+ | primary_value tCOLON2 tCONSTANT
572
+ {
573
+ result = @builder.assignable(
574
+ @builder.const_fetch(val[0], val[1], val[2]))
575
+ }
576
+ | tCOLON3 tCONSTANT
577
+ {
578
+ result = @builder.assignable(
579
+ @builder.const_global(val[0], val[1]))
580
+ }
581
+ | backref
582
+ {
583
+ result = @builder.assignable(val[0])
584
+ }
585
+
586
+ lhs: user_variable
587
+ {
588
+ result = @builder.assignable(val[0])
589
+ }
590
+ | keyword_variable
591
+ {
592
+ result = @builder.assignable(val[0])
593
+ }
594
+ | primary_value tLBRACK2 opt_call_args rbracket
595
+ {
596
+ result = @builder.index_asgn(val[0], val[1], val[2], val[3])
597
+ }
598
+ | primary_value call_op tIDENTIFIER
599
+ {
600
+ result = @builder.attr_asgn(val[0], val[1], val[2])
601
+ }
602
+ | primary_value tCOLON2 tIDENTIFIER
603
+ {
604
+ result = @builder.attr_asgn(val[0], val[1], val[2])
605
+ }
606
+ | primary_value call_op tCONSTANT
607
+ {
608
+ result = @builder.attr_asgn(val[0], val[1], val[2])
609
+ }
610
+ | primary_value tCOLON2 tCONSTANT
611
+ {
612
+ result = @builder.assignable(
613
+ @builder.const_fetch(val[0], val[1], val[2]))
614
+ }
615
+ | tCOLON3 tCONSTANT
616
+ {
617
+ result = @builder.assignable(
618
+ @builder.const_global(val[0], val[1]))
619
+ }
620
+ | backref
621
+ {
622
+ result = @builder.assignable(val[0])
623
+ }
624
+
625
+ cname: tIDENTIFIER
626
+ {
627
+ diagnostic :error, :module_name_const, nil, val[0]
628
+ }
629
+ | tCONSTANT
630
+
631
+ cpath: tCOLON3 cname
632
+ {
633
+ result = @builder.const_global(val[0], val[1])
634
+ }
635
+ | cname
636
+ {
637
+ result = @builder.const(val[0])
638
+ }
639
+ | primary_value tCOLON2 cname
640
+ {
641
+ result = @builder.const_fetch(val[0], val[1], val[2])
642
+ }
643
+
644
+ fname: tIDENTIFIER | tCONSTANT | tFID
645
+ | op
646
+ | reswords
647
+
648
+ fitem: fname
649
+ {
650
+ result = @builder.symbol_internal(val[0])
651
+ }
652
+ | symbol
653
+
654
+ undef_list: fitem
655
+ {
656
+ result = [ val[0] ]
657
+ }
658
+ | undef_list tCOMMA
659
+ {
660
+ @lexer.state = :expr_fname
661
+ }
662
+ fitem
663
+ {
664
+ result = val[0] << val[3]
665
+ }
666
+
667
+ op: tPIPE | tCARET | tAMPER2 | tCMP | tEQ | tEQQ
668
+ | tMATCH | tNMATCH | tGT | tGEQ | tLT | tLEQ
669
+ | tNEQ | tLSHFT | tRSHFT | tPLUS | tMINUS | tSTAR2
670
+ | tSTAR | tDIVIDE | tPERCENT | tPOW | tBANG | tTILDE
671
+ | tUPLUS | tUMINUS | tAREF | tASET | tDSTAR | tBACK_REF2
672
+
673
+ reswords: k__LINE__ | k__FILE__ | k__ENCODING__ | klBEGIN | klEND
674
+ | kALIAS | kAND | kBEGIN | kBREAK | kCASE
675
+ | kCLASS | kDEF | kDEFINED | kDO | kELSE
676
+ | kELSIF | kEND | kENSURE | kFALSE | kFOR
677
+ | kIN | kMODULE | kNEXT | kNIL | kNOT
678
+ | kOR | kREDO | kRESCUE | kRETRY | kRETURN
679
+ | kSELF | kSUPER | kTHEN | kTRUE | kUNDEF
680
+ | kWHEN | kYIELD | kIF | kUNLESS | kWHILE
681
+ | kUNTIL
682
+
683
+ arg: lhs tEQL arg_rhs
684
+ {
685
+ result = @builder.assign(val[0], val[1], val[2])
686
+ }
687
+ | var_lhs tOP_ASGN arg_rhs
688
+ {
689
+ result = @builder.op_assign(val[0], val[1], val[2])
690
+ }
691
+ | primary_value tLBRACK2 opt_call_args rbracket tOP_ASGN arg_rhs
692
+ {
693
+ result = @builder.op_assign(
694
+ @builder.index(
695
+ val[0], val[1], val[2], val[3]),
696
+ val[4], val[5])
697
+ }
698
+ | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
699
+ {
700
+ result = @builder.op_assign(
701
+ @builder.call_method(
702
+ val[0], val[1], val[2]),
703
+ val[3], val[4])
704
+ }
705
+ | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
706
+ {
707
+ result = @builder.op_assign(
708
+ @builder.call_method(
709
+ val[0], val[1], val[2]),
710
+ val[3], val[4])
711
+ }
712
+ | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
713
+ {
714
+ result = @builder.op_assign(
715
+ @builder.call_method(
716
+ val[0], val[1], val[2]),
717
+ val[3], val[4])
718
+ }
719
+ | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
720
+ {
721
+ const = @builder.const_op_assignable(
722
+ @builder.const_fetch(val[0], val[1], val[2]))
723
+ result = @builder.op_assign(const, val[3], val[4])
724
+ }
725
+ | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
726
+ {
727
+ const = @builder.const_op_assignable(
728
+ @builder.const_global(val[0], val[1]))
729
+ result = @builder.op_assign(const, val[2], val[3])
730
+ }
731
+ | backref tOP_ASGN arg_rhs
732
+ {
733
+ result = @builder.op_assign(val[0], val[1], val[2])
734
+ }
735
+ | arg tDOT2 arg
736
+ {
737
+ result = @builder.range_inclusive(val[0], val[1], val[2])
738
+ }
739
+ | arg tDOT3 arg
740
+ {
741
+ result = @builder.range_exclusive(val[0], val[1], val[2])
742
+ }
743
+ | arg tDOT2
744
+ {
745
+ result = @builder.range_inclusive(val[0], val[1], nil)
746
+ }
747
+ | arg tDOT3
748
+ {
749
+ result = @builder.range_exclusive(val[0], val[1], nil)
750
+ }
751
+ | tBDOT2 arg
752
+ {
753
+ result = @builder.range_inclusive(nil, val[0], val[1])
754
+ }
755
+ | tBDOT3 arg
756
+ {
757
+ result = @builder.range_exclusive(nil, val[0], val[1])
758
+ }
759
+ | arg tPLUS arg
760
+ {
761
+ result = @builder.binary_op(val[0], val[1], val[2])
762
+ }
763
+ | arg tMINUS arg
764
+ {
765
+ result = @builder.binary_op(val[0], val[1], val[2])
766
+ }
767
+ | arg tSTAR2 arg
768
+ {
769
+ result = @builder.binary_op(val[0], val[1], val[2])
770
+ }
771
+ | arg tDIVIDE arg
772
+ {
773
+ result = @builder.binary_op(val[0], val[1], val[2])
774
+ }
775
+ | arg tPERCENT arg
776
+ {
777
+ result = @builder.binary_op(val[0], val[1], val[2])
778
+ }
779
+ | arg tPOW arg
780
+ {
781
+ result = @builder.binary_op(val[0], val[1], val[2])
782
+ }
783
+ | tUNARY_NUM simple_numeric tPOW arg
784
+ {
785
+ result = @builder.unary_op(val[0],
786
+ @builder.binary_op(
787
+ val[1], val[2], val[3]))
788
+ }
789
+ | tUPLUS arg
790
+ {
791
+ result = @builder.unary_op(val[0], val[1])
792
+ }
793
+ | tUMINUS arg
794
+ {
795
+ result = @builder.unary_op(val[0], val[1])
796
+ }
797
+ | arg tPIPE arg
798
+ {
799
+ result = @builder.binary_op(val[0], val[1], val[2])
800
+ }
801
+ | arg tCARET arg
802
+ {
803
+ result = @builder.binary_op(val[0], val[1], val[2])
804
+ }
805
+ | arg tAMPER2 arg
806
+ {
807
+ result = @builder.binary_op(val[0], val[1], val[2])
808
+ }
809
+ | arg tCMP arg
810
+ {
811
+ result = @builder.binary_op(val[0], val[1], val[2])
812
+ }
813
+ | rel_expr =tCMP
814
+ | arg tEQ arg
815
+ {
816
+ result = @builder.binary_op(val[0], val[1], val[2])
817
+ }
818
+ | arg tEQQ arg
819
+ {
820
+ result = @builder.binary_op(val[0], val[1], val[2])
821
+ }
822
+ | arg tNEQ arg
823
+ {
824
+ result = @builder.binary_op(val[0], val[1], val[2])
825
+ }
826
+ | arg tMATCH arg
827
+ {
828
+ result = @builder.match_op(val[0], val[1], val[2])
829
+ }
830
+ | arg tNMATCH arg
831
+ {
832
+ result = @builder.binary_op(val[0], val[1], val[2])
833
+ }
834
+ | tBANG arg
835
+ {
836
+ result = @builder.not_op(val[0], nil, val[1], nil)
837
+ }
838
+ | tTILDE arg
839
+ {
840
+ result = @builder.unary_op(val[0], val[1])
841
+ }
842
+ | arg tLSHFT arg
843
+ {
844
+ result = @builder.binary_op(val[0], val[1], val[2])
845
+ }
846
+ | arg tRSHFT arg
847
+ {
848
+ result = @builder.binary_op(val[0], val[1], val[2])
849
+ }
850
+ | arg tANDOP arg
851
+ {
852
+ result = @builder.logical_op(:and, val[0], val[1], val[2])
853
+ }
854
+ | arg tOROP arg
855
+ {
856
+ result = @builder.logical_op(:or, val[0], val[1], val[2])
857
+ }
858
+ | kDEFINED opt_nl arg
859
+ {
860
+ result = @builder.keyword_cmd(:defined?, val[0], nil, [ val[2] ], nil)
861
+ }
862
+ | arg tEH arg opt_nl tCOLON arg
863
+ {
864
+ result = @builder.ternary(val[0], val[1],
865
+ val[2], val[4], val[5])
866
+ }
867
+ | defn_head f_paren_args tEQL arg
868
+ {
869
+ _def_t, name_t = val[0]
870
+
871
+ if name_t[0].end_with?('=')
872
+ diagnostic :error, :endless_setter, nil, name_t
873
+ end
874
+
875
+ result = @builder.def_endless_method(*val[0],
876
+ val[1], val[2], val[3])
877
+
878
+ @lexer.cmdarg.pop
879
+ @lexer.cond.pop
880
+ @static_env.unextend
881
+ @context.pop
882
+ @current_arg_stack.pop
883
+ }
884
+ | defn_head f_paren_args tEQL arg kRESCUE_MOD arg
885
+ {
886
+ rescue_body = @builder.rescue_body(val[4],
887
+ nil, nil, nil,
888
+ nil, val[5])
889
+
890
+ method_body = @builder.begin_body(val[3], [ rescue_body ])
891
+
892
+ result = @builder.def_endless_method(*val[0],
893
+ val[1], val[2], method_body)
894
+
895
+ @lexer.cmdarg.pop
896
+ @lexer.cond.pop
897
+ @static_env.unextend
898
+ @context.pop
899
+ @current_arg_stack.pop
900
+ }
901
+ | defs_head f_paren_args tEQL arg
902
+ {
903
+ result = @builder.def_endless_singleton(*val[0],
904
+ val[1], val[2], val[3])
905
+
906
+ @lexer.cmdarg.pop
907
+ @lexer.cond.pop
908
+ @static_env.unextend
909
+ @context.pop
910
+ @current_arg_stack.pop
911
+ }
912
+ | defs_head f_paren_args tEQL arg kRESCUE_MOD arg
913
+ {
914
+ rescue_body = @builder.rescue_body(val[4],
915
+ nil, nil, nil,
916
+ nil, val[5])
917
+
918
+ method_body = @builder.begin_body(val[3], [ rescue_body ])
919
+
920
+ result = @builder.def_endless_singleton(*val[0],
921
+ val[1], val[2], method_body)
922
+
923
+ @lexer.cmdarg.pop
924
+ @lexer.cond.pop
925
+ @static_env.unextend
926
+ @context.pop
927
+ @current_arg_stack.pop
928
+ }
929
+ | primary
930
+
931
+ relop: tGT | tLT | tGEQ | tLEQ
932
+
933
+ rel_expr: arg relop arg =tGT
934
+ {
935
+ result = @builder.binary_op(val[0], val[1], val[2])
936
+ }
937
+ | rel_expr relop arg =tGT
938
+ {
939
+ result = @builder.binary_op(val[0], val[1], val[2])
940
+ }
941
+
942
+ arg_value: arg
943
+
944
+ aref_args: none
945
+ | args trailer
946
+ | args tCOMMA assocs trailer
947
+ {
948
+ result = val[0] << @builder.associate(nil, val[2], nil)
949
+ }
950
+ | assocs trailer
951
+ {
952
+ result = [ @builder.associate(nil, val[0], nil) ]
953
+ }
954
+
955
+ arg_rhs: arg =tOP_ASGN
956
+ | arg kRESCUE_MOD arg
957
+ {
958
+ rescue_body = @builder.rescue_body(val[1],
959
+ nil, nil, nil,
960
+ nil, val[2])
961
+
962
+ result = @builder.begin_body(val[0], [ rescue_body ])
963
+ }
964
+
965
+ paren_args: tLPAREN2 opt_call_args rparen
966
+ {
967
+ result = val
968
+ }
969
+ | tLPAREN2 args tCOMMA args_forward rparen
970
+ {
971
+ unless @static_env.declared_forward_args?
972
+ diagnostic :error, :unexpected_token, { :token => 'tBDOT3' } , val[3]
973
+ end
974
+
975
+ result = [val[0], [*val[1], @builder.forwarded_args(val[3])], val[4]]
976
+ }
977
+ | tLPAREN2 args_forward rparen
978
+ {
979
+ unless @static_env.declared_forward_args?
980
+ diagnostic :error, :unexpected_token, { :token => 'tBDOT3' } , val[1]
981
+ end
982
+
983
+ result = [val[0], [@builder.forwarded_args(val[1])], val[2]]
984
+ }
985
+
986
+ opt_paren_args: # nothing
987
+ {
988
+ result = [ nil, [], nil ]
989
+ }
990
+ | paren_args
991
+
992
+ opt_call_args: # nothing
993
+ {
994
+ result = []
995
+ }
996
+ | call_args
997
+ | args tCOMMA
998
+ | args tCOMMA assocs tCOMMA
999
+ {
1000
+ result = val[0] << @builder.associate(nil, val[2], nil)
1001
+ }
1002
+ | assocs tCOMMA
1003
+ {
1004
+ result = [ @builder.associate(nil, val[0], nil) ]
1005
+ }
1006
+
1007
+ call_args: command
1008
+ {
1009
+ result = [ val[0] ]
1010
+ }
1011
+ | args opt_block_arg
1012
+ {
1013
+ result = val[0].concat(val[1])
1014
+ }
1015
+ | assocs opt_block_arg
1016
+ {
1017
+ result = [ @builder.associate(nil, val[0], nil) ]
1018
+ result.concat(val[1])
1019
+ }
1020
+ | args tCOMMA assocs opt_block_arg
1021
+ {
1022
+ assocs = @builder.associate(nil, val[2], nil)
1023
+ result = val[0] << assocs
1024
+ result.concat(val[3])
1025
+ }
1026
+ | block_arg
1027
+ {
1028
+ result = [ val[0] ]
1029
+ }
1030
+
1031
+ command_args: {
1032
+ # When branch gets invoked by RACC's lookahead
1033
+ # and command args start with '[' or '('
1034
+ # we need to put `true` to the cmdarg stack
1035
+ # **before** `false` pushed by lexer
1036
+ # m [], n
1037
+ # ^
1038
+ # Right here we have cmdarg [...0] because
1039
+ # lexer pushed it on '['
1040
+ # We need to modify cmdarg stack to [...10]
1041
+ #
1042
+ # For all other cases (like `m n` or `m n, []`) we simply put 1 to the stack
1043
+ # and later lexer pushes corresponding bits on top of it.
1044
+ last_token = @last_token[0]
1045
+ lookahead = last_token == :tLBRACK || last_token == :tLPAREN_ARG
1046
+
1047
+ if lookahead
1048
+ top = @lexer.cmdarg.pop
1049
+ @lexer.cmdarg.push(true)
1050
+ @lexer.cmdarg.push(top)
1051
+ else
1052
+ @lexer.cmdarg.push(true)
1053
+ end
1054
+ }
1055
+ call_args
1056
+ {
1057
+ # call_args can be followed by tLBRACE_ARG (that does cmdarg.push(0) in the lexer)
1058
+ # but the push must be done after cmdarg.pop() in the parser.
1059
+ # So this code does cmdarg.pop() to pop 0 pushed by tLBRACE_ARG,
1060
+ # cmdarg.pop() to pop 1 pushed by command_args,
1061
+ # and cmdarg.push(0) to restore back the flag set by tLBRACE_ARG.
1062
+ last_token = @last_token[0]
1063
+ lookahead = last_token == :tLBRACE_ARG
1064
+ if lookahead
1065
+ top = @lexer.cmdarg.pop
1066
+ @lexer.cmdarg.pop
1067
+ @lexer.cmdarg.push(top)
1068
+ else
1069
+ @lexer.cmdarg.pop
1070
+ end
1071
+
1072
+ result = val[1]
1073
+ }
1074
+
1075
+ block_arg: tAMPER arg_value
1076
+ {
1077
+ result = @builder.block_pass(val[0], val[1])
1078
+ }
1079
+
1080
+ opt_block_arg: tCOMMA block_arg
1081
+ {
1082
+ result = [ val[1] ]
1083
+ }
1084
+ | # nothing
1085
+ {
1086
+ result = []
1087
+ }
1088
+
1089
+ args: arg_value
1090
+ {
1091
+ result = [ val[0] ]
1092
+ }
1093
+ | tSTAR arg_value
1094
+ {
1095
+ result = [ @builder.splat(val[0], val[1]) ]
1096
+ }
1097
+ | args tCOMMA arg_value
1098
+ {
1099
+ result = val[0] << val[2]
1100
+ }
1101
+ | args tCOMMA tSTAR arg_value
1102
+ {
1103
+ result = val[0] << @builder.splat(val[2], val[3])
1104
+ }
1105
+
1106
+ mrhs_arg: mrhs
1107
+ {
1108
+ result = @builder.array(nil, val[0], nil)
1109
+ }
1110
+ | arg_value
1111
+
1112
+ mrhs: args tCOMMA arg_value
1113
+ {
1114
+ result = val[0] << val[2]
1115
+ }
1116
+ | args tCOMMA tSTAR arg_value
1117
+ {
1118
+ result = val[0] << @builder.splat(val[2], val[3])
1119
+ }
1120
+ | tSTAR arg_value
1121
+ {
1122
+ result = [ @builder.splat(val[0], val[1]) ]
1123
+ }
1124
+
1125
+ primary: literal
1126
+ | strings
1127
+ | xstring
1128
+ | regexp
1129
+ | words
1130
+ | qwords
1131
+ | symbols
1132
+ | qsymbols
1133
+ | var_ref
1134
+ | backref
1135
+ | tFID
1136
+ {
1137
+ result = @builder.call_method(nil, nil, val[0])
1138
+ }
1139
+ | kBEGIN
1140
+ {
1141
+ @lexer.cmdarg.push(false)
1142
+ }
1143
+ bodystmt kEND
1144
+ {
1145
+ @lexer.cmdarg.pop
1146
+
1147
+ result = @builder.begin_keyword(val[0], val[2], val[3])
1148
+ }
1149
+ | tLPAREN_ARG stmt
1150
+ {
1151
+ @lexer.state = :expr_endarg
1152
+ }
1153
+ rparen
1154
+ {
1155
+ result = @builder.begin(val[0], val[1], val[3])
1156
+ }
1157
+ | tLPAREN_ARG
1158
+ {
1159
+ @lexer.state = :expr_endarg
1160
+ }
1161
+ opt_nl tRPAREN
1162
+ {
1163
+ result = @builder.begin(val[0], nil, val[3])
1164
+ }
1165
+ | tLPAREN compstmt tRPAREN
1166
+ {
1167
+ result = @builder.begin(val[0], val[1], val[2])
1168
+ }
1169
+ | primary_value tCOLON2 tCONSTANT
1170
+ {
1171
+ result = @builder.const_fetch(val[0], val[1], val[2])
1172
+ }
1173
+ | tCOLON3 tCONSTANT
1174
+ {
1175
+ result = @builder.const_global(val[0], val[1])
1176
+ }
1177
+ | tLBRACK aref_args tRBRACK
1178
+ {
1179
+ result = @builder.array(val[0], val[1], val[2])
1180
+ }
1181
+ | tLBRACE assoc_list tRCURLY
1182
+ {
1183
+ result = @builder.associate(val[0], val[1], val[2])
1184
+ }
1185
+ | k_return
1186
+ {
1187
+ result = @builder.keyword_cmd(:return, val[0])
1188
+ }
1189
+ | kYIELD tLPAREN2 call_args rparen
1190
+ {
1191
+ result = @builder.keyword_cmd(:yield, val[0], val[1], val[2], val[3])
1192
+ }
1193
+ | kYIELD tLPAREN2 rparen
1194
+ {
1195
+ result = @builder.keyword_cmd(:yield, val[0], val[1], [], val[2])
1196
+ }
1197
+ | kYIELD
1198
+ {
1199
+ result = @builder.keyword_cmd(:yield, val[0])
1200
+ }
1201
+ | kDEFINED opt_nl tLPAREN2 expr rparen
1202
+ {
1203
+ result = @builder.keyword_cmd(:defined?, val[0],
1204
+ val[2], [ val[3] ], val[4])
1205
+ }
1206
+ | kNOT tLPAREN2 expr rparen
1207
+ {
1208
+ result = @builder.not_op(val[0], val[1], val[2], val[3])
1209
+ }
1210
+ | kNOT tLPAREN2 rparen
1211
+ {
1212
+ result = @builder.not_op(val[0], val[1], nil, val[2])
1213
+ }
1214
+ | fcall brace_block
1215
+ {
1216
+ method_call = @builder.call_method(nil, nil, val[0])
1217
+
1218
+ begin_t, args, body, end_t = val[1]
1219
+ result = @builder.block(method_call,
1220
+ begin_t, args, body, end_t)
1221
+ }
1222
+ | method_call
1223
+ | method_call brace_block
1224
+ {
1225
+ begin_t, args, body, end_t = val[1]
1226
+ result = @builder.block(val[0],
1227
+ begin_t, args, body, end_t)
1228
+ }
1229
+ | lambda
1230
+ | kIF expr_value then compstmt if_tail kEND
1231
+ {
1232
+ else_t, else_ = val[4]
1233
+ result = @builder.condition(val[0], val[1], val[2],
1234
+ val[3], else_t,
1235
+ else_, val[5])
1236
+ }
1237
+ | kUNLESS expr_value then compstmt opt_else kEND
1238
+ {
1239
+ else_t, else_ = val[4]
1240
+ result = @builder.condition(val[0], val[1], val[2],
1241
+ else_, else_t,
1242
+ val[3], val[5])
1243
+ }
1244
+ | kWHILE expr_value_do compstmt kEND
1245
+ {
1246
+ result = @builder.loop(:while, val[0], *val[1], val[2], val[3])
1247
+ }
1248
+ | kUNTIL expr_value_do compstmt kEND
1249
+ {
1250
+ result = @builder.loop(:until, val[0], *val[1], val[2], val[3])
1251
+ }
1252
+ | kCASE expr_value opt_terms case_body kEND
1253
+ {
1254
+ *when_bodies, (else_t, else_body) = *val[3]
1255
+
1256
+ result = @builder.case(val[0], val[1],
1257
+ when_bodies, else_t, else_body,
1258
+ val[4])
1259
+ }
1260
+ | kCASE opt_terms case_body kEND
1261
+ {
1262
+ *when_bodies, (else_t, else_body) = *val[2]
1263
+
1264
+ result = @builder.case(val[0], nil,
1265
+ when_bodies, else_t, else_body,
1266
+ val[3])
1267
+ }
1268
+ | kCASE expr_value opt_terms p_case_body kEND
1269
+ {
1270
+ *in_bodies, (else_t, else_body) = *val[3]
1271
+
1272
+ result = @builder.case_match(val[0], val[1],
1273
+ in_bodies, else_t, else_body,
1274
+ val[4])
1275
+ }
1276
+ | kFOR for_var kIN expr_value_do compstmt kEND
1277
+ {
1278
+ result = @builder.for(val[0], val[1], val[2], *val[3], val[4], val[5])
1279
+ }
1280
+ | kCLASS cpath superclass
1281
+ {
1282
+ @static_env.extend_static
1283
+ @lexer.cmdarg.push(false)
1284
+ @lexer.cond.push(false)
1285
+ @context.push(:class)
1286
+ }
1287
+ bodystmt kEND
1288
+ {
1289
+ unless @context.class_definition_allowed?
1290
+ diagnostic :error, :class_in_def, nil, val[0]
1291
+ end
1292
+
1293
+ lt_t, superclass = val[2]
1294
+ result = @builder.def_class(val[0], val[1],
1295
+ lt_t, superclass,
1296
+ val[4], val[5])
1297
+
1298
+ @lexer.cmdarg.pop
1299
+ @lexer.cond.pop
1300
+ @static_env.unextend
1301
+ @context.pop
1302
+ }
1303
+ | kCLASS tLSHFT expr term
1304
+ {
1305
+ @static_env.extend_static
1306
+ @lexer.cmdarg.push(false)
1307
+ @lexer.cond.push(false)
1308
+ @context.push(:sclass)
1309
+ }
1310
+ bodystmt kEND
1311
+ {
1312
+ result = @builder.def_sclass(val[0], val[1], val[2],
1313
+ val[5], val[6])
1314
+
1315
+ @lexer.cmdarg.pop
1316
+ @lexer.cond.pop
1317
+ @static_env.unextend
1318
+ @context.pop
1319
+ }
1320
+ | kMODULE cpath
1321
+ {
1322
+ @static_env.extend_static
1323
+ @lexer.cmdarg.push(false)
1324
+ @context.push(:module)
1325
+ }
1326
+ bodystmt kEND
1327
+ {
1328
+ unless @context.module_definition_allowed?
1329
+ diagnostic :error, :module_in_def, nil, val[0]
1330
+ end
1331
+
1332
+ result = @builder.def_module(val[0], val[1],
1333
+ val[3], val[4])
1334
+
1335
+ @lexer.cmdarg.pop
1336
+ @static_env.unextend
1337
+ @context.pop
1338
+ }
1339
+ | defn_head f_arglist bodystmt kEND
1340
+ {
1341
+ result = @builder.def_method(*val[0], val[1],
1342
+ val[2], val[3])
1343
+
1344
+ @lexer.cmdarg.pop
1345
+ @lexer.cond.pop
1346
+ @static_env.unextend
1347
+ @context.pop
1348
+ @current_arg_stack.pop
1349
+ }
1350
+ | defs_head f_arglist bodystmt kEND
1351
+ {
1352
+ result = @builder.def_singleton(*val[0], val[1],
1353
+ val[2], val[3])
1354
+
1355
+ @lexer.cmdarg.pop
1356
+ @lexer.cond.pop
1357
+ @static_env.unextend
1358
+ @context.pop
1359
+ @current_arg_stack.pop
1360
+ }
1361
+ | kBREAK
1362
+ {
1363
+ result = @builder.keyword_cmd(:break, val[0])
1364
+ }
1365
+ | kNEXT
1366
+ {
1367
+ result = @builder.keyword_cmd(:next, val[0])
1368
+ }
1369
+ | kREDO
1370
+ {
1371
+ result = @builder.keyword_cmd(:redo, val[0])
1372
+ }
1373
+ | kRETRY
1374
+ {
1375
+ result = @builder.keyword_cmd(:retry, val[0])
1376
+ }
1377
+
1378
+ primary_value: primary
1379
+
1380
+ k_return: kRETURN
1381
+ {
1382
+ if @context.in_class?
1383
+ diagnostic :error, :invalid_return, nil, val[0]
1384
+ end
1385
+ }
1386
+
1387
+ then: term
1388
+ | kTHEN
1389
+ | term kTHEN
1390
+ {
1391
+ result = val[1]
1392
+ }
1393
+
1394
+ do: term
1395
+ | kDO_COND
1396
+
1397
+ if_tail: opt_else
1398
+ | kELSIF expr_value then compstmt if_tail
1399
+ {
1400
+ else_t, else_ = val[4]
1401
+ result = [ val[0],
1402
+ @builder.condition(val[0], val[1], val[2],
1403
+ val[3], else_t,
1404
+ else_, nil),
1405
+ ]
1406
+ }
1407
+
1408
+ opt_else: none
1409
+ | kELSE compstmt
1410
+ {
1411
+ result = val
1412
+ }
1413
+
1414
+ for_var: lhs
1415
+ | mlhs
1416
+
1417
+ f_marg: f_norm_arg
1418
+ {
1419
+ result = @builder.arg(val[0])
1420
+ }
1421
+ | tLPAREN f_margs rparen
1422
+ {
1423
+ result = @builder.multi_lhs(val[0], val[1], val[2])
1424
+ }
1425
+
1426
+ f_marg_list: f_marg
1427
+ {
1428
+ result = [ val[0] ]
1429
+ }
1430
+ | f_marg_list tCOMMA f_marg
1431
+ {
1432
+ result = val[0] << val[2]
1433
+ }
1434
+
1435
+ f_margs: f_marg_list
1436
+ | f_marg_list tCOMMA f_rest_marg
1437
+ {
1438
+ result = val[0].
1439
+ push(val[2])
1440
+ }
1441
+ | f_marg_list tCOMMA f_rest_marg tCOMMA f_marg_list
1442
+ {
1443
+ result = val[0].
1444
+ push(val[2]).
1445
+ concat(val[4])
1446
+ }
1447
+ | f_rest_marg
1448
+ {
1449
+ result = [ val[0] ]
1450
+ }
1451
+ | f_rest_marg tCOMMA f_marg_list
1452
+ {
1453
+ result = [ val[0], *val[2] ]
1454
+ }
1455
+
1456
+ f_rest_marg: tSTAR f_norm_arg
1457
+ {
1458
+ result = @builder.restarg(val[0], val[1])
1459
+ }
1460
+ | tSTAR
1461
+ {
1462
+ result = @builder.restarg(val[0])
1463
+ }
1464
+
1465
+ f_any_kwrest: f_kwrest
1466
+ | f_no_kwarg
1467
+
1468
+ block_args_tail: f_block_kwarg tCOMMA f_kwrest opt_f_block_arg
1469
+ {
1470
+ result = val[0].concat(val[2]).concat(val[3])
1471
+ }
1472
+ | f_block_kwarg opt_f_block_arg
1473
+ {
1474
+ result = val[0].concat(val[1])
1475
+ }
1476
+ | f_any_kwrest opt_f_block_arg
1477
+ {
1478
+ result = val[0].concat(val[1])
1479
+ }
1480
+ | f_block_arg
1481
+ {
1482
+ result = [ val[0] ]
1483
+ }
1484
+
1485
+ opt_block_args_tail:
1486
+ tCOMMA block_args_tail
1487
+ {
1488
+ result = val[1]
1489
+ }
1490
+ | # nothing
1491
+ {
1492
+ result = []
1493
+ }
1494
+
1495
+ excessed_comma: tCOMMA
1496
+
1497
+ block_param: f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg opt_block_args_tail
1498
+ {
1499
+ result = val[0].
1500
+ concat(val[2]).
1501
+ concat(val[4]).
1502
+ concat(val[5])
1503
+ }
1504
+ | f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1505
+ {
1506
+ result = val[0].
1507
+ concat(val[2]).
1508
+ concat(val[4]).
1509
+ concat(val[6]).
1510
+ concat(val[7])
1511
+ }
1512
+ | f_arg tCOMMA f_block_optarg opt_block_args_tail
1513
+ {
1514
+ result = val[0].
1515
+ concat(val[2]).
1516
+ concat(val[3])
1517
+ }
1518
+ | f_arg tCOMMA f_block_optarg tCOMMA f_arg opt_block_args_tail
1519
+ {
1520
+ result = val[0].
1521
+ concat(val[2]).
1522
+ concat(val[4]).
1523
+ concat(val[5])
1524
+ }
1525
+ | f_arg tCOMMA f_rest_arg opt_block_args_tail
1526
+ {
1527
+ result = val[0].
1528
+ concat(val[2]).
1529
+ concat(val[3])
1530
+ }
1531
+ | f_arg excessed_comma
1532
+ | f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1533
+ {
1534
+ result = val[0].
1535
+ concat(val[2]).
1536
+ concat(val[4]).
1537
+ concat(val[5])
1538
+ }
1539
+ | f_arg opt_block_args_tail
1540
+ {
1541
+ if val[1].empty? && val[0].size == 1
1542
+ result = [@builder.procarg0(val[0][0])]
1543
+ else
1544
+ result = val[0].concat(val[1])
1545
+ end
1546
+ }
1547
+ | f_block_optarg tCOMMA f_rest_arg opt_block_args_tail
1548
+ {
1549
+ result = val[0].
1550
+ concat(val[2]).
1551
+ concat(val[3])
1552
+ }
1553
+ | f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1554
+ {
1555
+ result = val[0].
1556
+ concat(val[2]).
1557
+ concat(val[4]).
1558
+ concat(val[5])
1559
+ }
1560
+ | f_block_optarg opt_block_args_tail
1561
+ {
1562
+ result = val[0].
1563
+ concat(val[1])
1564
+ }
1565
+ | f_block_optarg tCOMMA f_arg opt_block_args_tail
1566
+ {
1567
+ result = val[0].
1568
+ concat(val[2]).
1569
+ concat(val[3])
1570
+ }
1571
+ | f_rest_arg opt_block_args_tail
1572
+ {
1573
+ result = val[0].
1574
+ concat(val[1])
1575
+ }
1576
+ | f_rest_arg tCOMMA f_arg opt_block_args_tail
1577
+ {
1578
+ result = val[0].
1579
+ concat(val[2]).
1580
+ concat(val[3])
1581
+ }
1582
+ | block_args_tail
1583
+
1584
+ opt_block_param: # nothing
1585
+ {
1586
+ result = @builder.args(nil, [], nil)
1587
+ }
1588
+ | block_param_def
1589
+ {
1590
+ @lexer.state = :expr_value
1591
+ }
1592
+
1593
+ block_param_def: tPIPE opt_bv_decl tPIPE
1594
+ {
1595
+ @max_numparam_stack.has_ordinary_params!
1596
+ @current_arg_stack.set(nil)
1597
+ result = @builder.args(val[0], val[1], val[2])
1598
+ }
1599
+ | tPIPE block_param opt_bv_decl tPIPE
1600
+ {
1601
+ @max_numparam_stack.has_ordinary_params!
1602
+ @current_arg_stack.set(nil)
1603
+ result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1604
+ }
1605
+
1606
+ opt_bv_decl: opt_nl
1607
+ {
1608
+ result = []
1609
+ }
1610
+ | opt_nl tSEMI bv_decls opt_nl
1611
+ {
1612
+ result = val[2]
1613
+ }
1614
+
1615
+ bv_decls: bvar
1616
+ {
1617
+ result = [ val[0] ]
1618
+ }
1619
+ | bv_decls tCOMMA bvar
1620
+ {
1621
+ result = val[0] << val[2]
1622
+ }
1623
+
1624
+ bvar: tIDENTIFIER
1625
+ {
1626
+ @static_env.declare val[0][0]
1627
+ result = @builder.shadowarg(val[0])
1628
+ }
1629
+ | f_bad_arg
1630
+
1631
+ lambda: tLAMBDA
1632
+ {
1633
+ @static_env.extend_dynamic
1634
+ @max_numparam_stack.push
1635
+ @context.push(:lambda)
1636
+ }
1637
+ f_larglist
1638
+ {
1639
+ @context.pop
1640
+ @lexer.cmdarg.push(false)
1641
+ }
1642
+ lambda_body
1643
+ {
1644
+ lambda_call = @builder.call_lambda(val[0])
1645
+ args = @max_numparam_stack.has_numparams? ? @builder.numargs(@max_numparam_stack.top) : val[2]
1646
+ begin_t, body, end_t = val[4]
1647
+
1648
+ @max_numparam_stack.pop
1649
+ @static_env.unextend
1650
+ @lexer.cmdarg.pop
1651
+
1652
+ result = @builder.block(lambda_call,
1653
+ begin_t, args, body, end_t)
1654
+ }
1655
+
1656
+ f_larglist: tLPAREN2 f_args opt_bv_decl tRPAREN
1657
+ {
1658
+ @max_numparam_stack.has_ordinary_params!
1659
+ result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1660
+ }
1661
+ | f_args
1662
+ {
1663
+ if val[0].any?
1664
+ @max_numparam_stack.has_ordinary_params!
1665
+ end
1666
+ result = @builder.args(nil, val[0], nil)
1667
+ }
1668
+
1669
+ lambda_body: tLAMBEG
1670
+ {
1671
+ @context.push(:lambda)
1672
+ }
1673
+ compstmt tRCURLY
1674
+ {
1675
+ result = [ val[0], val[2], val[3] ]
1676
+ @context.pop
1677
+ }
1678
+ | kDO_LAMBDA
1679
+ {
1680
+ @context.push(:lambda)
1681
+ }
1682
+ bodystmt kEND
1683
+ {
1684
+ result = [ val[0], val[2], val[3] ]
1685
+ @context.pop
1686
+ }
1687
+
1688
+ do_block: kDO_BLOCK
1689
+ {
1690
+ @context.push(:block)
1691
+ }
1692
+ do_body kEND
1693
+ {
1694
+ result = [ val[0], *val[2], val[3] ]
1695
+ @context.pop
1696
+ }
1697
+
1698
+ block_call: command do_block
1699
+ {
1700
+ begin_t, block_args, body, end_t = val[1]
1701
+ result = @builder.block(val[0],
1702
+ begin_t, block_args, body, end_t)
1703
+ }
1704
+ | block_call dot_or_colon operation2 opt_paren_args
1705
+ {
1706
+ lparen_t, args, rparen_t = val[3]
1707
+ result = @builder.call_method(val[0], val[1], val[2],
1708
+ lparen_t, args, rparen_t)
1709
+ }
1710
+ | block_call dot_or_colon operation2 opt_paren_args brace_block
1711
+ {
1712
+ lparen_t, args, rparen_t = val[3]
1713
+ method_call = @builder.call_method(val[0], val[1], val[2],
1714
+ lparen_t, args, rparen_t)
1715
+
1716
+ begin_t, args, body, end_t = val[4]
1717
+ result = @builder.block(method_call,
1718
+ begin_t, args, body, end_t)
1719
+ }
1720
+ | block_call dot_or_colon operation2 command_args do_block
1721
+ {
1722
+ method_call = @builder.call_method(val[0], val[1], val[2],
1723
+ nil, val[3], nil)
1724
+
1725
+ begin_t, args, body, end_t = val[4]
1726
+ result = @builder.block(method_call,
1727
+ begin_t, args, body, end_t)
1728
+ }
1729
+
1730
+ method_call: fcall paren_args
1731
+ {
1732
+ lparen_t, args, rparen_t = val[1]
1733
+ result = @builder.call_method(nil, nil, val[0],
1734
+ lparen_t, args, rparen_t)
1735
+ }
1736
+ | primary_value call_op operation2 opt_paren_args
1737
+ {
1738
+ lparen_t, args, rparen_t = val[3]
1739
+ result = @builder.call_method(val[0], val[1], val[2],
1740
+ lparen_t, args, rparen_t)
1741
+ }
1742
+ | primary_value tCOLON2 operation2 paren_args
1743
+ {
1744
+ lparen_t, args, rparen_t = val[3]
1745
+ result = @builder.call_method(val[0], val[1], val[2],
1746
+ lparen_t, args, rparen_t)
1747
+ }
1748
+ | primary_value tCOLON2 operation3
1749
+ {
1750
+ result = @builder.call_method(val[0], val[1], val[2])
1751
+ }
1752
+ | primary_value call_op paren_args
1753
+ {
1754
+ lparen_t, args, rparen_t = val[2]
1755
+ result = @builder.call_method(val[0], val[1], nil,
1756
+ lparen_t, args, rparen_t)
1757
+ }
1758
+ | primary_value tCOLON2 paren_args
1759
+ {
1760
+ lparen_t, args, rparen_t = val[2]
1761
+ result = @builder.call_method(val[0], val[1], nil,
1762
+ lparen_t, args, rparen_t)
1763
+ }
1764
+ | kSUPER paren_args
1765
+ {
1766
+ lparen_t, args, rparen_t = val[1]
1767
+ result = @builder.keyword_cmd(:super, val[0],
1768
+ lparen_t, args, rparen_t)
1769
+ }
1770
+ | kSUPER
1771
+ {
1772
+ result = @builder.keyword_cmd(:zsuper, val[0])
1773
+ }
1774
+ | primary_value tLBRACK2 opt_call_args rbracket
1775
+ {
1776
+ result = @builder.index(val[0], val[1], val[2], val[3])
1777
+ }
1778
+
1779
+ brace_block: tLCURLY
1780
+ {
1781
+ @context.push(:block)
1782
+ }
1783
+ brace_body tRCURLY
1784
+ {
1785
+ result = [ val[0], *val[2], val[3] ]
1786
+ @context.pop
1787
+ }
1788
+ | kDO
1789
+ {
1790
+ @context.push(:block)
1791
+ }
1792
+ do_body kEND
1793
+ {
1794
+ result = [ val[0], *val[2], val[3] ]
1795
+ @context.pop
1796
+ }
1797
+
1798
+ brace_body: {
1799
+ @static_env.extend_dynamic
1800
+ @max_numparam_stack.push
1801
+ }
1802
+ opt_block_param compstmt
1803
+ {
1804
+ args = @max_numparam_stack.has_numparams? ? @builder.numargs(@max_numparam_stack.top) : val[1]
1805
+ result = [ args, val[2] ]
1806
+
1807
+ @max_numparam_stack.pop
1808
+ @static_env.unextend
1809
+ }
1810
+
1811
+ do_body: {
1812
+ @static_env.extend_dynamic
1813
+ @max_numparam_stack.push
1814
+ }
1815
+ {
1816
+ @lexer.cmdarg.push(false)
1817
+ }
1818
+ opt_block_param bodystmt
1819
+ {
1820
+ args = @max_numparam_stack.has_numparams? ? @builder.numargs(@max_numparam_stack.top) : val[2]
1821
+ result = [ args, val[3] ]
1822
+
1823
+ @max_numparam_stack.pop
1824
+ @static_env.unextend
1825
+ @lexer.cmdarg.pop
1826
+ }
1827
+
1828
+ case_body: kWHEN args then compstmt cases
1829
+ {
1830
+ result = [ @builder.when(val[0], val[1], val[2], val[3]),
1831
+ *val[4] ]
1832
+ }
1833
+
1834
+ cases: opt_else
1835
+ {
1836
+ result = [ val[0] ]
1837
+ }
1838
+ | case_body
1839
+
1840
+ p_case_body: kIN
1841
+ {
1842
+ @lexer.state = :expr_beg
1843
+ @lexer.command_start = false
1844
+ @pattern_variables.push
1845
+ @pattern_hash_keys.push
1846
+
1847
+ result = @lexer.in_kwarg
1848
+ @lexer.in_kwarg = true
1849
+ }
1850
+ p_top_expr then
1851
+ {
1852
+ @lexer.in_kwarg = val[1]
1853
+ }
1854
+ compstmt p_cases
1855
+ {
1856
+ result = [ @builder.in_pattern(val[0], *val[2], val[3], val[5]),
1857
+ *val[6] ]
1858
+ }
1859
+
1860
+ p_cases: opt_else
1861
+ {
1862
+ result = [ val[0] ]
1863
+ }
1864
+ | p_case_body
1865
+
1866
+ p_top_expr: p_top_expr_body
1867
+ {
1868
+ result = [ val[0], nil ]
1869
+ }
1870
+ | p_top_expr_body kIF_MOD expr_value
1871
+ {
1872
+ result = [ val[0], @builder.if_guard(val[1], val[2]) ]
1873
+ }
1874
+ | p_top_expr_body kUNLESS_MOD expr_value
1875
+ {
1876
+ result = [ val[0], @builder.unless_guard(val[1], val[2]) ]
1877
+ }
1878
+
1879
+ p_top_expr_body: p_expr
1880
+ | p_expr tCOMMA
1881
+ {
1882
+ # array patterns that end with comma
1883
+ # like 1, 2,
1884
+ # must be emitted as `array_pattern_with_tail`
1885
+ item = @builder.match_with_trailing_comma(val[0], val[1])
1886
+ result = @builder.array_pattern(nil, [ item ], nil)
1887
+ }
1888
+ | p_expr tCOMMA p_args
1889
+ {
1890
+ result = @builder.array_pattern(nil, [val[0]].concat(val[2]), nil)
1891
+ }
1892
+ | p_find
1893
+ {
1894
+ result = @builder.find_pattern(nil, val[0], nil)
1895
+ }
1896
+ | p_args_tail
1897
+ {
1898
+ result = @builder.array_pattern(nil, val[0], nil)
1899
+ }
1900
+ | p_kwargs
1901
+ {
1902
+ result = @builder.hash_pattern(nil, val[0], nil)
1903
+ }
1904
+
1905
+ p_expr: p_as
1906
+
1907
+ p_as: p_expr tASSOC p_variable
1908
+ {
1909
+ result = @builder.match_as(val[0], val[1], val[2])
1910
+ }
1911
+ | p_alt
1912
+
1913
+ p_alt: p_alt tPIPE p_expr_basic
1914
+ {
1915
+ result = @builder.match_alt(val[0], val[1], val[2])
1916
+ }
1917
+ | p_expr_basic
1918
+
1919
+ p_lparen: tLPAREN2
1920
+ {
1921
+ result = val[0]
1922
+ @pattern_hash_keys.push
1923
+ }
1924
+
1925
+ p_lbracket: tLBRACK2
1926
+ {
1927
+ result = val[0]
1928
+ @pattern_hash_keys.push
1929
+ }
1930
+
1931
+ p_expr_basic: p_value
1932
+ | p_const p_lparen p_args rparen
1933
+ {
1934
+ @pattern_hash_keys.pop
1935
+ pattern = @builder.array_pattern(nil, val[2], nil)
1936
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1937
+ }
1938
+ | p_const p_lparen p_find rparen
1939
+ {
1940
+ @pattern_hash_keys.pop
1941
+ pattern = @builder.find_pattern(nil, val[2], nil)
1942
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1943
+ }
1944
+ | p_const p_lparen p_kwargs rparen
1945
+ {
1946
+ @pattern_hash_keys.pop
1947
+ pattern = @builder.hash_pattern(nil, val[2], nil)
1948
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1949
+ }
1950
+ | p_const tLPAREN2 rparen
1951
+ {
1952
+ pattern = @builder.array_pattern(val[1], nil, val[2])
1953
+ result = @builder.const_pattern(val[0], val[1], pattern, val[2])
1954
+ }
1955
+ | p_const p_lbracket p_args rbracket
1956
+ {
1957
+ @pattern_hash_keys.pop
1958
+ pattern = @builder.array_pattern(nil, val[2], nil)
1959
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1960
+ }
1961
+ | p_const p_lbracket p_find rbracket
1962
+ {
1963
+ @pattern_hash_keys.pop
1964
+ pattern = @builder.find_pattern(nil, val[2], nil)
1965
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1966
+ }
1967
+ | p_const p_lbracket p_kwargs rbracket
1968
+ {
1969
+ @pattern_hash_keys.pop
1970
+ pattern = @builder.hash_pattern(nil, val[2], nil)
1971
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1972
+ }
1973
+ | p_const tLBRACK2 rbracket
1974
+ {
1975
+ pattern = @builder.array_pattern(val[1], nil, val[2])
1976
+ result = @builder.const_pattern(val[0], val[1], pattern, val[2])
1977
+ }
1978
+ | tLBRACK p_args rbracket
1979
+ {
1980
+ result = @builder.array_pattern(val[0], val[1], val[2])
1981
+ }
1982
+ | tLBRACK p_find rbracket
1983
+ {
1984
+ result = @builder.find_pattern(val[0], val[1], val[2])
1985
+ }
1986
+ | tLBRACK rbracket
1987
+ {
1988
+ result = @builder.array_pattern(val[0], [], val[1])
1989
+ }
1990
+ | tLBRACE
1991
+ {
1992
+ @pattern_hash_keys.push
1993
+ result = @lexer.in_kwarg
1994
+ @lexer.in_kwarg = false
1995
+ }
1996
+ p_kwargs rbrace
1997
+ {
1998
+ @pattern_hash_keys.pop
1999
+ @lexer.in_kwarg = val[1]
2000
+ result = @builder.hash_pattern(val[0], val[2], val[3])
2001
+ }
2002
+ | tLBRACE rbrace
2003
+ {
2004
+ result = @builder.hash_pattern(val[0], [], val[1])
2005
+ }
2006
+ | tLPAREN
2007
+ {
2008
+ @pattern_hash_keys.push
2009
+ }
2010
+ p_expr rparen
2011
+ {
2012
+ @pattern_hash_keys.pop
2013
+ result = @builder.begin(val[0], val[2], val[3])
2014
+ }
2015
+
2016
+ p_args: p_expr
2017
+ {
2018
+ result = [ val[0] ]
2019
+ }
2020
+ | p_args_head
2021
+ {
2022
+ result = val[0]
2023
+ }
2024
+ | p_args_head p_arg
2025
+ {
2026
+ result = [ *val[0], val[1] ]
2027
+ }
2028
+ | p_args_head tSTAR tIDENTIFIER
2029
+ {
2030
+ match_rest = @builder.match_rest(val[1], val[2])
2031
+ result = [ *val[0], match_rest ]
2032
+ }
2033
+ | p_args_head tSTAR tIDENTIFIER tCOMMA p_args_post
2034
+ {
2035
+ match_rest = @builder.match_rest(val[1], val[2])
2036
+ result = [ *val[0], match_rest, *val[4] ]
2037
+ }
2038
+ | p_args_head tSTAR
2039
+ {
2040
+ result = [ *val[0], @builder.match_rest(val[1]) ]
2041
+ }
2042
+ | p_args_head tSTAR tCOMMA p_args_post
2043
+ {
2044
+ result = [ *val[0], @builder.match_rest(val[1]), *val[3] ]
2045
+ }
2046
+ | p_args_tail
2047
+
2048
+ p_args_head: p_arg tCOMMA
2049
+ {
2050
+ # array patterns that end with comma
2051
+ # like [1, 2,]
2052
+ # must be emitted as `array_pattern_with_tail`
2053
+ item = @builder.match_with_trailing_comma(val[0], val[1])
2054
+ result = [ item ]
2055
+ }
2056
+ | p_args_head p_arg tCOMMA
2057
+ {
2058
+ # array patterns that end with comma
2059
+ # like [1, 2,]
2060
+ # must be emitted as `array_pattern_with_tail`
2061
+ last_item = @builder.match_with_trailing_comma(val[1], val[2])
2062
+ result = [ *val[0], last_item ]
2063
+ }
2064
+
2065
+ p_args_tail: p_rest
2066
+ {
2067
+ result = [ val[0] ]
2068
+ }
2069
+ | p_rest tCOMMA p_args_post
2070
+ {
2071
+ result = [ val[0], *val[2] ]
2072
+ }
2073
+
2074
+ p_find: p_rest tCOMMA p_args_post tCOMMA p_rest
2075
+ {
2076
+ result = [ val[0], *val[2], val[4] ]
2077
+ }
2078
+
2079
+ p_rest: tSTAR tIDENTIFIER
2080
+ {
2081
+ result = @builder.match_rest(val[0], val[1])
2082
+ }
2083
+ | tSTAR
2084
+ {
2085
+ result = @builder.match_rest(val[0])
2086
+ }
2087
+
2088
+ p_args_post: p_arg
2089
+ {
2090
+ result = [ val[0] ]
2091
+ }
2092
+ | p_args_post tCOMMA p_arg
2093
+ {
2094
+ result = [ *val[0], val[2] ]
2095
+ }
2096
+
2097
+ p_arg: p_expr
2098
+
2099
+ p_kwargs: p_kwarg tCOMMA p_any_kwrest
2100
+ {
2101
+ result = [ *val[0], *val[2] ]
2102
+ }
2103
+ | p_kwarg
2104
+ {
2105
+ result = val[0]
2106
+ }
2107
+ | p_kwarg tCOMMA
2108
+ {
2109
+ result = val[0]
2110
+ }
2111
+ | p_any_kwrest
2112
+ {
2113
+ result = val[0]
2114
+ }
2115
+
2116
+ p_kwarg: p_kw
2117
+ {
2118
+ result = [ val[0] ]
2119
+ }
2120
+ | p_kwarg tCOMMA p_kw
2121
+ {
2122
+ result = [ *val[0], val[2] ]
2123
+ }
2124
+
2125
+ p_kw: p_kw_label p_expr
2126
+ {
2127
+ result = @builder.match_pair(*val[0], val[1])
2128
+ }
2129
+ | p_kw_label
2130
+ {
2131
+ result = @builder.match_label(*val[0])
2132
+ }
2133
+
2134
+ p_kw_label: tLABEL
2135
+ {
2136
+ result = [:label, val[0]]
2137
+ }
2138
+ | tSTRING_BEG string_contents tLABEL_END
2139
+ {
2140
+ result = [:quoted, [val[0], val[1], val[2]]]
2141
+ }
2142
+
2143
+ p_kwrest: kwrest_mark tIDENTIFIER
2144
+ {
2145
+ result = [ @builder.match_rest(val[0], val[1]) ]
2146
+ }
2147
+ | kwrest_mark
2148
+ {
2149
+ result = [ @builder.match_rest(val[0], nil) ]
2150
+ }
2151
+
2152
+ p_kwnorest: kwrest_mark kNIL
2153
+ {
2154
+ result = [ @builder.match_nil_pattern(val[0], val[1]) ]
2155
+ }
2156
+
2157
+ p_any_kwrest: p_kwrest
2158
+ | p_kwnorest
2159
+
2160
+ p_value: p_primitive
2161
+ | p_primitive tDOT2 p_primitive
2162
+ {
2163
+ result = @builder.range_inclusive(val[0], val[1], val[2])
2164
+ }
2165
+ | p_primitive tDOT3 p_primitive
2166
+ {
2167
+ result = @builder.range_exclusive(val[0], val[1], val[2])
2168
+ }
2169
+ | p_primitive tDOT2
2170
+ {
2171
+ result = @builder.range_inclusive(val[0], val[1], nil)
2172
+ }
2173
+ | p_primitive tDOT3
2174
+ {
2175
+ result = @builder.range_exclusive(val[0], val[1], nil)
2176
+ }
2177
+ | p_variable
2178
+ | p_var_ref
2179
+ | p_const
2180
+ | tBDOT2 p_primitive
2181
+ {
2182
+ result = @builder.range_inclusive(nil, val[0], val[1])
2183
+ }
2184
+ | tBDOT3 p_primitive
2185
+ {
2186
+ result = @builder.range_exclusive(nil, val[0], val[1])
2187
+ }
2188
+
2189
+ p_primitive: literal
2190
+ | strings
2191
+ | xstring
2192
+ | regexp
2193
+ | words
2194
+ | qwords
2195
+ | symbols
2196
+ | qsymbols
2197
+ | keyword_variable
2198
+ {
2199
+ result = @builder.accessible(val[0])
2200
+ }
2201
+ | lambda
2202
+
2203
+ p_variable: tIDENTIFIER
2204
+ {
2205
+ result = @builder.match_var(val[0])
2206
+ }
2207
+
2208
+ p_var_ref: tCARET tIDENTIFIER
2209
+ {
2210
+ name = val[1][0]
2211
+ unless static_env.declared?(name)
2212
+ diagnostic :error, :undefined_lvar, { :name => name }, val[1]
2213
+ end
2214
+
2215
+ lvar = @builder.accessible(@builder.ident(val[1]))
2216
+ result = @builder.pin(val[0], lvar)
2217
+ }
2218
+
2219
+ p_const: tCOLON3 cname
2220
+ {
2221
+ result = @builder.const_global(val[0], val[1])
2222
+ }
2223
+ | p_const tCOLON2 cname
2224
+ {
2225
+ result = @builder.const_fetch(val[0], val[1], val[2])
2226
+ }
2227
+ | tCONSTANT
2228
+ {
2229
+ result = @builder.const(val[0])
2230
+ }
2231
+
2232
+ opt_rescue: kRESCUE exc_list exc_var then compstmt opt_rescue
2233
+ {
2234
+ assoc_t, exc_var = val[2]
2235
+
2236
+ if val[1]
2237
+ exc_list = @builder.array(nil, val[1], nil)
2238
+ end
2239
+
2240
+ result = [ @builder.rescue_body(val[0],
2241
+ exc_list, assoc_t, exc_var,
2242
+ val[3], val[4]),
2243
+ *val[5] ]
2244
+ }
2245
+ |
2246
+ {
2247
+ result = []
2248
+ }
2249
+
2250
+ exc_list: arg_value
2251
+ {
2252
+ result = [ val[0] ]
2253
+ }
2254
+ | mrhs
2255
+ | none
2256
+
2257
+ exc_var: tASSOC lhs
2258
+ {
2259
+ result = [ val[0], val[1] ]
2260
+ }
2261
+ | none
2262
+
2263
+ opt_ensure: kENSURE compstmt
2264
+ {
2265
+ result = [ val[0], val[1] ]
2266
+ }
2267
+ | none
2268
+
2269
+ literal: numeric
2270
+ | symbol
2271
+
2272
+ strings: string
2273
+ {
2274
+ result = @builder.string_compose(nil, val[0], nil)
2275
+ }
2276
+
2277
+ string: string1
2278
+ {
2279
+ result = [ val[0] ]
2280
+ }
2281
+ | string string1
2282
+ {
2283
+ result = val[0] << val[1]
2284
+ }
2285
+
2286
+ string1: tSTRING_BEG string_contents tSTRING_END
2287
+ {
2288
+ string = @builder.string_compose(val[0], val[1], val[2])
2289
+ result = @builder.dedent_string(string, @lexer.dedent_level)
2290
+ }
2291
+ | tSTRING
2292
+ {
2293
+ string = @builder.string(val[0])
2294
+ result = @builder.dedent_string(string, @lexer.dedent_level)
2295
+ }
2296
+ | tCHARACTER
2297
+ {
2298
+ result = @builder.character(val[0])
2299
+ }
2300
+
2301
+ xstring: tXSTRING_BEG xstring_contents tSTRING_END
2302
+ {
2303
+ string = @builder.xstring_compose(val[0], val[1], val[2])
2304
+ result = @builder.dedent_string(string, @lexer.dedent_level)
2305
+ }
2306
+
2307
+ regexp: tREGEXP_BEG regexp_contents tSTRING_END tREGEXP_OPT
2308
+ {
2309
+ opts = @builder.regexp_options(val[3])
2310
+ result = @builder.regexp_compose(val[0], val[1], val[2], opts)
2311
+ }
2312
+
2313
+ words: tWORDS_BEG word_list tSTRING_END
2314
+ {
2315
+ result = @builder.words_compose(val[0], val[1], val[2])
2316
+ }
2317
+
2318
+ word_list: # nothing
2319
+ {
2320
+ result = []
2321
+ }
2322
+ | word_list word tSPACE
2323
+ {
2324
+ result = val[0] << @builder.word(val[1])
2325
+ }
2326
+
2327
+ word: string_content
2328
+ {
2329
+ result = [ val[0] ]
2330
+ }
2331
+ | word string_content
2332
+ {
2333
+ result = val[0] << val[1]
2334
+ }
2335
+
2336
+ symbols: tSYMBOLS_BEG symbol_list tSTRING_END
2337
+ {
2338
+ result = @builder.symbols_compose(val[0], val[1], val[2])
2339
+ }
2340
+
2341
+ symbol_list: # nothing
2342
+ {
2343
+ result = []
2344
+ }
2345
+ | symbol_list word tSPACE
2346
+ {
2347
+ result = val[0] << @builder.word(val[1])
2348
+ }
2349
+
2350
+ qwords: tQWORDS_BEG qword_list tSTRING_END
2351
+ {
2352
+ result = @builder.words_compose(val[0], val[1], val[2])
2353
+ }
2354
+
2355
+ qsymbols: tQSYMBOLS_BEG qsym_list tSTRING_END
2356
+ {
2357
+ result = @builder.symbols_compose(val[0], val[1], val[2])
2358
+ }
2359
+
2360
+ qword_list: # nothing
2361
+ {
2362
+ result = []
2363
+ }
2364
+ | qword_list tSTRING_CONTENT tSPACE
2365
+ {
2366
+ result = val[0] << @builder.string_internal(val[1])
2367
+ }
2368
+
2369
+ qsym_list: # nothing
2370
+ {
2371
+ result = []
2372
+ }
2373
+ | qsym_list tSTRING_CONTENT tSPACE
2374
+ {
2375
+ result = val[0] << @builder.symbol_internal(val[1])
2376
+ }
2377
+
2378
+ string_contents: # nothing
2379
+ {
2380
+ result = []
2381
+ }
2382
+ | string_contents string_content
2383
+ {
2384
+ result = val[0] << val[1]
2385
+ }
2386
+
2387
+ xstring_contents: # nothing
2388
+ {
2389
+ result = []
2390
+ }
2391
+ | xstring_contents string_content
2392
+ {
2393
+ result = val[0] << val[1]
2394
+ }
2395
+
2396
+ regexp_contents: # nothing
2397
+ {
2398
+ result = []
2399
+ }
2400
+ | regexp_contents string_content
2401
+ {
2402
+ result = val[0] << val[1]
2403
+ }
2404
+
2405
+ string_content: tSTRING_CONTENT
2406
+ {
2407
+ result = @builder.string_internal(val[0])
2408
+ }
2409
+ | tSTRING_DVAR string_dvar
2410
+ {
2411
+ result = val[1]
2412
+ }
2413
+ | tSTRING_DBEG
2414
+ {
2415
+ @lexer.cmdarg.push(false)
2416
+ @lexer.cond.push(false)
2417
+ }
2418
+ compstmt tSTRING_DEND
2419
+ {
2420
+ @lexer.cmdarg.pop
2421
+ @lexer.cond.pop
2422
+
2423
+ result = @builder.begin(val[0], val[2], val[3])
2424
+ }
2425
+
2426
+ string_dvar: tGVAR
2427
+ {
2428
+ result = @builder.gvar(val[0])
2429
+ }
2430
+ | tIVAR
2431
+ {
2432
+ result = @builder.ivar(val[0])
2433
+ }
2434
+ | tCVAR
2435
+ {
2436
+ result = @builder.cvar(val[0])
2437
+ }
2438
+ | backref
2439
+
2440
+ symbol: ssym
2441
+ | dsym
2442
+
2443
+ ssym: tSYMBOL
2444
+ {
2445
+ @lexer.state = :expr_end
2446
+ result = @builder.symbol(val[0])
2447
+ }
2448
+
2449
+ dsym: tSYMBEG string_contents tSTRING_END
2450
+ {
2451
+ @lexer.state = :expr_end
2452
+ result = @builder.symbol_compose(val[0], val[1], val[2])
2453
+ }
2454
+
2455
+ numeric: simple_numeric
2456
+ {
2457
+ result = val[0]
2458
+ }
2459
+ | tUNARY_NUM simple_numeric =tLOWEST
2460
+ {
2461
+ if @builder.respond_to? :negate
2462
+ # AST builder interface compatibility
2463
+ result = @builder.negate(val[0], val[1])
2464
+ else
2465
+ result = @builder.unary_num(val[0], val[1])
2466
+ end
2467
+ }
2468
+
2469
+ simple_numeric: tINTEGER
2470
+ {
2471
+ @lexer.state = :expr_end
2472
+ result = @builder.integer(val[0])
2473
+ }
2474
+ | tFLOAT
2475
+ {
2476
+ @lexer.state = :expr_end
2477
+ result = @builder.float(val[0])
2478
+ }
2479
+ | tRATIONAL
2480
+ {
2481
+ @lexer.state = :expr_end
2482
+ result = @builder.rational(val[0])
2483
+ }
2484
+ | tIMAGINARY
2485
+ {
2486
+ @lexer.state = :expr_end
2487
+ result = @builder.complex(val[0])
2488
+ }
2489
+
2490
+ user_variable: tIDENTIFIER
2491
+ {
2492
+ result = @builder.ident(val[0])
2493
+ }
2494
+ | tIVAR
2495
+ {
2496
+ result = @builder.ivar(val[0])
2497
+ }
2498
+ | tGVAR
2499
+ {
2500
+ result = @builder.gvar(val[0])
2501
+ }
2502
+ | tCONSTANT
2503
+ {
2504
+ result = @builder.const(val[0])
2505
+ }
2506
+ | tCVAR
2507
+ {
2508
+ result = @builder.cvar(val[0])
2509
+ }
2510
+
2511
+ keyword_variable: kNIL
2512
+ {
2513
+ result = @builder.nil(val[0])
2514
+ }
2515
+ | kSELF
2516
+ {
2517
+ result = @builder.self(val[0])
2518
+ }
2519
+ | kTRUE
2520
+ {
2521
+ result = @builder.true(val[0])
2522
+ }
2523
+ | kFALSE
2524
+ {
2525
+ result = @builder.false(val[0])
2526
+ }
2527
+ | k__FILE__
2528
+ {
2529
+ result = @builder.__FILE__(val[0])
2530
+ }
2531
+ | k__LINE__
2532
+ {
2533
+ result = @builder.__LINE__(val[0])
2534
+ }
2535
+ | k__ENCODING__
2536
+ {
2537
+ result = @builder.__ENCODING__(val[0])
2538
+ }
2539
+
2540
+ var_ref: user_variable
2541
+ {
2542
+ if (node = val[0]) && node.type == :ident
2543
+ name = node.children[0]
2544
+
2545
+ if name =~ /\A_[1-9]\z/ && !static_env.declared?(name) && context.in_dynamic_block?
2546
+ # definitely an implicit param
2547
+ location = node.loc.expression
2548
+
2549
+ if max_numparam_stack.has_ordinary_params?
2550
+ diagnostic :error, :ordinary_param_defined, nil, [nil, location]
2551
+ end
2552
+
2553
+ raw_context = context.stack.dup
2554
+ raw_max_numparam_stack = max_numparam_stack.stack.dup
2555
+
2556
+ # ignore current block scope
2557
+ raw_context.pop
2558
+ raw_max_numparam_stack.pop
2559
+
2560
+ raw_context.reverse_each do |outer_scope|
2561
+ if outer_scope == :block || outer_scope == :lambda
2562
+ outer_scope_has_numparams = raw_max_numparam_stack.pop > 0
2563
+
2564
+ if outer_scope_has_numparams
2565
+ diagnostic :error, :numparam_used_in_outer_scope, nil, [nil, location]
2566
+ else
2567
+ # for now it's ok, but an outer scope can also be a block
2568
+ # with numparams, so we need to continue
2569
+ end
2570
+ else
2571
+ # found an outer scope that can't have numparams
2572
+ # like def/class/etc
2573
+ break
2574
+ end
2575
+ end
2576
+
2577
+ static_env.declare(name)
2578
+ max_numparam_stack.register(name[1].to_i)
2579
+ end
2580
+ end
2581
+
2582
+ result = @builder.accessible(val[0])
2583
+ }
2584
+ | keyword_variable
2585
+ {
2586
+ result = @builder.accessible(val[0])
2587
+ }
2588
+
2589
+ var_lhs: user_variable
2590
+ {
2591
+ result = @builder.assignable(val[0])
2592
+ }
2593
+ | keyword_variable
2594
+ {
2595
+ result = @builder.assignable(val[0])
2596
+ }
2597
+
2598
+ backref: tNTH_REF
2599
+ {
2600
+ result = @builder.nth_ref(val[0])
2601
+ }
2602
+ | tBACK_REF
2603
+ {
2604
+ result = @builder.back_ref(val[0])
2605
+ }
2606
+
2607
+ superclass: tLT
2608
+ {
2609
+ @lexer.state = :expr_value
2610
+ }
2611
+ expr_value term
2612
+ {
2613
+ result = [ val[0], val[2] ]
2614
+ }
2615
+ | # nothing
2616
+ {
2617
+ result = nil
2618
+ }
2619
+
2620
+ f_paren_args: tLPAREN2 f_args rparen
2621
+ {
2622
+ result = @builder.args(val[0], val[1], val[2])
2623
+
2624
+ @lexer.state = :expr_value
2625
+ }
2626
+ | tLPAREN2 f_arg tCOMMA args_forward rparen
2627
+ {
2628
+ args = [ *val[1], @builder.forward_arg(val[3]) ]
2629
+ result = @builder.args(val[0], args, val[4])
2630
+
2631
+ @static_env.declare_forward_args
2632
+ }
2633
+ | tLPAREN2 args_forward rparen
2634
+ {
2635
+ result = @builder.forward_only_args(val[0], val[1], val[2])
2636
+ @static_env.declare_forward_args
2637
+
2638
+ @lexer.state = :expr_value
2639
+ }
2640
+
2641
+ f_arglist: f_paren_args
2642
+ | {
2643
+ result = @lexer.in_kwarg
2644
+ @lexer.in_kwarg = true
2645
+ }
2646
+ f_args term
2647
+ {
2648
+ @lexer.in_kwarg = val[0]
2649
+ result = @builder.args(nil, val[1], nil)
2650
+ }
2651
+
2652
+ args_tail: f_kwarg tCOMMA f_kwrest opt_f_block_arg
2653
+ {
2654
+ result = val[0].concat(val[2]).concat(val[3])
2655
+ }
2656
+ | f_kwarg opt_f_block_arg
2657
+ {
2658
+ result = val[0].concat(val[1])
2659
+ }
2660
+ | f_any_kwrest opt_f_block_arg
2661
+ {
2662
+ result = val[0].concat(val[1])
2663
+ }
2664
+ | f_block_arg
2665
+ {
2666
+ result = [ val[0] ]
2667
+ }
2668
+
2669
+ opt_args_tail: tCOMMA args_tail
2670
+ {
2671
+ result = val[1]
2672
+ }
2673
+ | # nothing
2674
+ {
2675
+ result = []
2676
+ }
2677
+
2678
+ f_args: f_arg tCOMMA f_optarg tCOMMA f_rest_arg opt_args_tail
2679
+ {
2680
+ result = val[0].
2681
+ concat(val[2]).
2682
+ concat(val[4]).
2683
+ concat(val[5])
2684
+ }
2685
+ | f_arg tCOMMA f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2686
+ {
2687
+ result = val[0].
2688
+ concat(val[2]).
2689
+ concat(val[4]).
2690
+ concat(val[6]).
2691
+ concat(val[7])
2692
+ }
2693
+ | f_arg tCOMMA f_optarg opt_args_tail
2694
+ {
2695
+ result = val[0].
2696
+ concat(val[2]).
2697
+ concat(val[3])
2698
+ }
2699
+ | f_arg tCOMMA f_optarg tCOMMA f_arg opt_args_tail
2700
+ {
2701
+ result = val[0].
2702
+ concat(val[2]).
2703
+ concat(val[4]).
2704
+ concat(val[5])
2705
+ }
2706
+ | f_arg tCOMMA f_rest_arg opt_args_tail
2707
+ {
2708
+ result = val[0].
2709
+ concat(val[2]).
2710
+ concat(val[3])
2711
+ }
2712
+ | f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2713
+ {
2714
+ result = val[0].
2715
+ concat(val[2]).
2716
+ concat(val[4]).
2717
+ concat(val[5])
2718
+ }
2719
+ | f_arg opt_args_tail
2720
+ {
2721
+ result = val[0].
2722
+ concat(val[1])
2723
+ }
2724
+ | f_optarg tCOMMA f_rest_arg opt_args_tail
2725
+ {
2726
+ result = val[0].
2727
+ concat(val[2]).
2728
+ concat(val[3])
2729
+ }
2730
+ | f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2731
+ {
2732
+ result = val[0].
2733
+ concat(val[2]).
2734
+ concat(val[4]).
2735
+ concat(val[5])
2736
+ }
2737
+ | f_optarg opt_args_tail
2738
+ {
2739
+ result = val[0].
2740
+ concat(val[1])
2741
+ }
2742
+ | f_optarg tCOMMA f_arg opt_args_tail
2743
+ {
2744
+ result = val[0].
2745
+ concat(val[2]).
2746
+ concat(val[3])
2747
+ }
2748
+ | f_rest_arg opt_args_tail
2749
+ {
2750
+ result = val[0].
2751
+ concat(val[1])
2752
+ }
2753
+ | f_rest_arg tCOMMA f_arg opt_args_tail
2754
+ {
2755
+ result = val[0].
2756
+ concat(val[2]).
2757
+ concat(val[3])
2758
+ }
2759
+ | args_tail
2760
+ {
2761
+ result = val[0]
2762
+ }
2763
+ | # nothing
2764
+ {
2765
+ result = []
2766
+ }
2767
+
2768
+ args_forward: tBDOT3
2769
+ {
2770
+ result = val[0]
2771
+ }
2772
+
2773
+ f_bad_arg: tCONSTANT
2774
+ {
2775
+ diagnostic :error, :argument_const, nil, val[0]
2776
+ }
2777
+ | tIVAR
2778
+ {
2779
+ diagnostic :error, :argument_ivar, nil, val[0]
2780
+ }
2781
+ | tGVAR
2782
+ {
2783
+ diagnostic :error, :argument_gvar, nil, val[0]
2784
+ }
2785
+ | tCVAR
2786
+ {
2787
+ diagnostic :error, :argument_cvar, nil, val[0]
2788
+ }
2789
+
2790
+ f_norm_arg: f_bad_arg
2791
+ | tIDENTIFIER
2792
+ {
2793
+ @static_env.declare val[0][0]
2794
+
2795
+ @max_numparam_stack.has_ordinary_params!
2796
+
2797
+ result = val[0]
2798
+ }
2799
+
2800
+ f_arg_asgn: f_norm_arg
2801
+ {
2802
+ @current_arg_stack.set(val[0][0])
2803
+ result = val[0]
2804
+ }
2805
+
2806
+ f_arg_item: f_arg_asgn
2807
+ {
2808
+ @current_arg_stack.set(0)
2809
+ result = @builder.arg(val[0])
2810
+ }
2811
+ | tLPAREN f_margs rparen
2812
+ {
2813
+ result = @builder.multi_lhs(val[0], val[1], val[2])
2814
+ }
2815
+
2816
+ f_arg: f_arg_item
2817
+ {
2818
+ result = [ val[0] ]
2819
+ }
2820
+ | f_arg tCOMMA f_arg_item
2821
+ {
2822
+ result = val[0] << val[2]
2823
+ }
2824
+
2825
+ f_label: tLABEL
2826
+ {
2827
+ check_kwarg_name(val[0])
2828
+
2829
+ @static_env.declare val[0][0]
2830
+
2831
+ @max_numparam_stack.has_ordinary_params!
2832
+
2833
+ @current_arg_stack.set(val[0][0])
2834
+
2835
+ result = val[0]
2836
+ }
2837
+
2838
+ f_kw: f_label arg_value
2839
+ {
2840
+ @current_arg_stack.set(nil)
2841
+ result = @builder.kwoptarg(val[0], val[1])
2842
+ }
2843
+ | f_label
2844
+ {
2845
+ @current_arg_stack.set(nil)
2846
+ result = @builder.kwarg(val[0])
2847
+ }
2848
+
2849
+ f_block_kw: f_label primary_value
2850
+ {
2851
+ result = @builder.kwoptarg(val[0], val[1])
2852
+ }
2853
+ | f_label
2854
+ {
2855
+ result = @builder.kwarg(val[0])
2856
+ }
2857
+
2858
+ f_block_kwarg: f_block_kw
2859
+ {
2860
+ result = [ val[0] ]
2861
+ }
2862
+ | f_block_kwarg tCOMMA f_block_kw
2863
+ {
2864
+ result = val[0] << val[2]
2865
+ }
2866
+
2867
+ f_kwarg: f_kw
2868
+ {
2869
+ result = [ val[0] ]
2870
+ }
2871
+ | f_kwarg tCOMMA f_kw
2872
+ {
2873
+ result = val[0] << val[2]
2874
+ }
2875
+
2876
+ kwrest_mark: tPOW | tDSTAR
2877
+
2878
+ f_no_kwarg: kwrest_mark kNIL
2879
+ {
2880
+ result = [ @builder.kwnilarg(val[0], val[1]) ]
2881
+ }
2882
+
2883
+ f_kwrest: kwrest_mark tIDENTIFIER
2884
+ {
2885
+ @static_env.declare val[1][0]
2886
+
2887
+ result = [ @builder.kwrestarg(val[0], val[1]) ]
2888
+ }
2889
+ | kwrest_mark
2890
+ {
2891
+ result = [ @builder.kwrestarg(val[0]) ]
2892
+ }
2893
+
2894
+ f_opt: f_arg_asgn tEQL arg_value
2895
+ {
2896
+ @current_arg_stack.set(0)
2897
+ result = @builder.optarg(val[0], val[1], val[2])
2898
+ }
2899
+
2900
+ f_block_opt: f_arg_asgn tEQL primary_value
2901
+ {
2902
+ @current_arg_stack.set(0)
2903
+ result = @builder.optarg(val[0], val[1], val[2])
2904
+ }
2905
+
2906
+ f_block_optarg: f_block_opt
2907
+ {
2908
+ result = [ val[0] ]
2909
+ }
2910
+ | f_block_optarg tCOMMA f_block_opt
2911
+ {
2912
+ result = val[0] << val[2]
2913
+ }
2914
+
2915
+ f_optarg: f_opt
2916
+ {
2917
+ result = [ val[0] ]
2918
+ }
2919
+ | f_optarg tCOMMA f_opt
2920
+ {
2921
+ result = val[0] << val[2]
2922
+ }
2923
+
2924
+ restarg_mark: tSTAR2 | tSTAR
2925
+
2926
+ f_rest_arg: restarg_mark tIDENTIFIER
2927
+ {
2928
+ @static_env.declare val[1][0]
2929
+
2930
+ result = [ @builder.restarg(val[0], val[1]) ]
2931
+ }
2932
+ | restarg_mark
2933
+ {
2934
+ result = [ @builder.restarg(val[0]) ]
2935
+ }
2936
+
2937
+ blkarg_mark: tAMPER2 | tAMPER
2938
+
2939
+ f_block_arg: blkarg_mark tIDENTIFIER
2940
+ {
2941
+ @static_env.declare val[1][0]
2942
+
2943
+ result = @builder.blockarg(val[0], val[1])
2944
+ }
2945
+
2946
+ opt_f_block_arg: tCOMMA f_block_arg
2947
+ {
2948
+ result = [ val[1] ]
2949
+ }
2950
+ |
2951
+ {
2952
+ result = []
2953
+ }
2954
+
2955
+ singleton: var_ref
2956
+ | tLPAREN2 expr rparen
2957
+ {
2958
+ result = val[1]
2959
+ }
2960
+
2961
+ assoc_list: # nothing
2962
+ {
2963
+ result = []
2964
+ }
2965
+ | assocs trailer
2966
+
2967
+ assocs: assoc
2968
+ {
2969
+ result = [ val[0] ]
2970
+ }
2971
+ | assocs tCOMMA assoc
2972
+ {
2973
+ result = val[0] << val[2]
2974
+ }
2975
+
2976
+ assoc: arg_value tASSOC arg_value
2977
+ {
2978
+ result = @builder.pair(val[0], val[1], val[2])
2979
+ }
2980
+ | tLABEL arg_value
2981
+ {
2982
+ result = @builder.pair_keyword(val[0], val[1])
2983
+ }
2984
+ | tSTRING_BEG string_contents tLABEL_END arg_value
2985
+ {
2986
+ result = @builder.pair_quoted(val[0], val[1], val[2], val[3])
2987
+ }
2988
+ | tDSTAR arg_value
2989
+ {
2990
+ result = @builder.kwsplat(val[0], val[1])
2991
+ }
2992
+
2993
+ operation: tIDENTIFIER | tCONSTANT | tFID
2994
+ operation2: tIDENTIFIER | tCONSTANT | tFID | op
2995
+ operation3: tIDENTIFIER | tFID | op
2996
+ dot_or_colon: call_op | tCOLON2
2997
+ call_op: tDOT
2998
+ {
2999
+ result = [:dot, val[0][1]]
3000
+ }
3001
+ | tANDDOT
3002
+ {
3003
+ result = [:anddot, val[0][1]]
3004
+ }
3005
+ opt_terms: | terms
3006
+ opt_nl: | tNL
3007
+ rparen: opt_nl tRPAREN
3008
+ {
3009
+ result = val[1]
3010
+ }
3011
+ rbracket: opt_nl tRBRACK
3012
+ {
3013
+ result = val[1]
3014
+ }
3015
+ rbrace: opt_nl tRCURLY
3016
+ {
3017
+ result = val[1]
3018
+ }
3019
+ trailer: | tNL | tCOMMA
3020
+
3021
+ term: tSEMI
3022
+ {
3023
+ yyerrok
3024
+ }
3025
+ | tNL
3026
+
3027
+ terms: term
3028
+ | terms tSEMI
3029
+
3030
+ none: # nothing
3031
+ {
3032
+ result = nil
3033
+ }
3034
+ end
3035
+
3036
+ ---- header
3037
+
3038
+ require 'parser'
3039
+
3040
+ ---- inner
3041
+
3042
+ def version
3043
+ 30
3044
+ end
3045
+
3046
+ def default_encoding
3047
+ Encoding::UTF_8
3048
+ end