parser 2.7.1.4 → 3.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parser.rb +1 -1
  3. data/lib/parser/all.rb +2 -1
  4. data/lib/parser/ast/processor.rb +3 -7
  5. data/lib/parser/base.rb +7 -5
  6. data/lib/parser/builders/default.rb +178 -29
  7. data/lib/parser/context.rb +4 -0
  8. data/lib/parser/current.rb +20 -11
  9. data/lib/parser/current_arg_stack.rb +5 -2
  10. data/lib/parser/lexer.rb +23780 -0
  11. data/lib/parser/macruby.rb +6149 -0
  12. data/lib/parser/max_numparam_stack.rb +13 -5
  13. data/lib/parser/messages.rb +3 -0
  14. data/lib/parser/meta.rb +7 -6
  15. data/lib/parser/ruby18.rb +5667 -0
  16. data/lib/parser/ruby19.rb +6092 -0
  17. data/lib/parser/ruby20.rb +6527 -0
  18. data/lib/parser/ruby21.rb +6578 -0
  19. data/lib/parser/ruby22.rb +6613 -0
  20. data/lib/parser/ruby23.rb +6624 -0
  21. data/lib/parser/ruby24.rb +6694 -0
  22. data/lib/parser/ruby25.rb +6662 -0
  23. data/lib/parser/ruby26.rb +6676 -0
  24. data/lib/parser/ruby27.rb +7862 -0
  25. data/lib/parser/ruby28.rb +8047 -0
  26. data/lib/parser/ruby30.rb +8060 -0
  27. data/lib/parser/ruby31.rb +8075 -0
  28. data/lib/parser/rubymotion.rb +6086 -0
  29. data/lib/parser/runner.rb +9 -4
  30. data/lib/parser/source/buffer.rb +50 -27
  31. data/lib/parser/source/comment.rb +14 -1
  32. data/lib/parser/source/comment/associator.rb +18 -5
  33. data/lib/parser/source/map/{endless_definition.rb → method_definition.rb} +5 -3
  34. data/lib/parser/source/range.rb +2 -2
  35. data/lib/parser/source/tree_rewriter.rb +27 -0
  36. data/lib/parser/static_environment.rb +4 -0
  37. data/lib/parser/variables_stack.rb +4 -0
  38. data/lib/parser/version.rb +1 -1
  39. data/parser.gemspec +1 -17
  40. metadata +9 -97
  41. data/.gitignore +0 -34
  42. data/.travis.yml +0 -40
  43. data/.yardopts +0 -21
  44. data/CHANGELOG.md +0 -1116
  45. data/CONTRIBUTING.md +0 -17
  46. data/Gemfile +0 -10
  47. data/README.md +0 -309
  48. data/Rakefile +0 -167
  49. data/ci/run_rubocop_specs +0 -14
  50. data/doc/AST_FORMAT.md +0 -2283
  51. data/doc/CUSTOMIZATION.md +0 -37
  52. data/doc/INTERNALS.md +0 -21
  53. data/doc/css/.gitkeep +0 -0
  54. data/doc/css/common.css +0 -68
  55. data/lib/parser/lexer.rl +0 -2550
  56. data/lib/parser/macruby.y +0 -2208
  57. data/lib/parser/ruby18.y +0 -1936
  58. data/lib/parser/ruby19.y +0 -2185
  59. data/lib/parser/ruby20.y +0 -2363
  60. data/lib/parser/ruby21.y +0 -2364
  61. data/lib/parser/ruby22.y +0 -2371
  62. data/lib/parser/ruby23.y +0 -2377
  63. data/lib/parser/ruby24.y +0 -2415
  64. data/lib/parser/ruby25.y +0 -2412
  65. data/lib/parser/ruby26.y +0 -2420
  66. data/lib/parser/ruby27.y +0 -2950
  67. data/lib/parser/ruby28.y +0 -3043
  68. data/lib/parser/rubymotion.y +0 -2192
  69. data/test/bug_163/fixtures/input.rb +0 -5
  70. data/test/bug_163/fixtures/output.rb +0 -5
  71. data/test/bug_163/rewriter.rb +0 -20
  72. data/test/helper.rb +0 -103
  73. data/test/parse_helper.rb +0 -323
  74. data/test/racc_coverage_helper.rb +0 -133
  75. data/test/test_ast_processor.rb +0 -32
  76. data/test/test_base.rb +0 -31
  77. data/test/test_current.rb +0 -31
  78. data/test/test_diagnostic.rb +0 -95
  79. data/test/test_diagnostic_engine.rb +0 -59
  80. data/test/test_encoding.rb +0 -99
  81. data/test/test_lexer.rb +0 -3617
  82. data/test/test_lexer_stack_state.rb +0 -78
  83. data/test/test_meta.rb +0 -12
  84. data/test/test_parse_helper.rb +0 -80
  85. data/test/test_parser.rb +0 -9780
  86. data/test/test_runner_parse.rb +0 -56
  87. data/test/test_runner_rewrite.rb +0 -47
  88. data/test/test_source_buffer.rb +0 -165
  89. data/test/test_source_comment.rb +0 -36
  90. data/test/test_source_comment_associator.rb +0 -399
  91. data/test/test_source_map.rb +0 -14
  92. data/test/test_source_range.rb +0 -192
  93. data/test/test_source_rewriter.rb +0 -541
  94. data/test/test_source_rewriter_action.rb +0 -46
  95. data/test/test_source_tree_rewriter.rb +0 -361
  96. data/test/test_static_environment.rb +0 -45
  97. data/test/using_tree_rewriter/fixtures/input.rb +0 -3
  98. data/test/using_tree_rewriter/fixtures/output.rb +0 -3
  99. data/test/using_tree_rewriter/using_tree_rewriter.rb +0 -9
data/ci/run_rubocop_specs DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -eux
4
-
5
- bundle install
6
- rake generate
7
-
8
- git clone https://github.com/rubocop-hq/rubocop.git --depth=1
9
- cd rubocop
10
- export BUNDLE_GEMFILE=Gemfile
11
- echo "gem 'parser', path: '../'" > Gemfile.local
12
-
13
- bundle install
14
- bundle exec rake spec
data/doc/AST_FORMAT.md DELETED
@@ -1,2283 +0,0 @@
1
- AST and Source Location
2
- =======================
3
-
4
- ## Literals
5
-
6
- ### Singletons
7
-
8
- Format:
9
-
10
- ~~~
11
- (true)
12
- "true"
13
- ~~~~ expression
14
-
15
- (false)
16
- "false"
17
- ~~~~~ expression
18
-
19
- (nil)
20
- "nil"
21
- ~~~ expression
22
- ~~~
23
-
24
- ### Integer
25
-
26
- Format:
27
-
28
- ~~~
29
- (int 123)
30
- "123"
31
- ~~~ expression
32
-
33
- (int -123)
34
- "-123"
35
- ^ operator
36
- ~~~ expression
37
-
38
- (int 1)
39
- "__LINE__"
40
- ~~~~~~~~ expression
41
- ~~~
42
-
43
- ### Float
44
-
45
- Format:
46
-
47
- ~~~
48
- (float 1.0)
49
- "1.0"
50
- ~~~ expression
51
-
52
- (float -1.0)
53
- "-1.0"
54
- ^ operator
55
- ~~~~ expression
56
- ~~~
57
-
58
- ### Complex
59
-
60
- Format:
61
-
62
- ~~~
63
- (complex (0+1i))
64
- "1i"
65
- ~~ expression
66
-
67
- (complex (0+(1/1)*i))
68
- "1ri"
69
- ~~~ expression
70
- ~~~
71
-
72
- ### Rational
73
-
74
- Format:
75
-
76
- ~~~
77
- (rational (2/1))
78
- "2.0r"
79
- ~~~~ expression
80
- ~~~
81
-
82
- ### String
83
-
84
- #### Plain
85
-
86
- Format:
87
-
88
- ~~~
89
- (str "foo")
90
- "'foo'"
91
- ^ begin
92
- ^ end
93
- ~~~~~ expresion
94
-
95
- (string "foo.rb")
96
- "__FILE__"
97
- ~~~~~~~~ expression
98
- ~~~
99
-
100
- #### With interpolation
101
-
102
- Format:
103
-
104
- ~~~
105
- (dstr (str "foo") (begin (lvar bar)) (str "baz"))
106
- '"foo#{bar}baz"'
107
- ^ begin ^ end
108
- ~~~~~~~~~~~~~~ expression
109
- ^^ begin (begin)
110
- ^ end (begin)
111
- ^^^^^^ expression (begin)
112
- ~~~
113
-
114
- #### Here document
115
-
116
- Format:
117
-
118
- ~~~
119
- (str "foo\nbar\n")
120
- '<<HERE␊foo␊bar␊HERE'
121
- ~~~~~~ expression
122
- ~~~~~~~~ heredoc_body
123
- ~~~~ heredoc_end
124
- ~~~
125
-
126
- ### Symbol
127
-
128
- #### Plain
129
-
130
- Format:
131
-
132
- ~~~
133
- (sym :foo)
134
- ":foo"
135
- ~~~~ expresion
136
-
137
- ":'foo'"
138
- ^ begin
139
- ^ end
140
- ~~~~~~ expression
141
- ~~~
142
-
143
- #### With interpolation
144
-
145
- Format:
146
-
147
- ~~~
148
- (dsym (str "foo") (lvar bar) (str "baz"))
149
- ':"foo#{bar}baz"'
150
- ^ begin ^ end
151
- ~~~~~~~~~~~~~~~ expression
152
- ~~~
153
-
154
- ### Execute-string
155
-
156
- #### Plain
157
-
158
- Format:
159
-
160
- ~~~
161
- (xstr (str "foo") (lvar bar))
162
- "`foo#{bar}`"
163
- ^ begin ^ end
164
- ~~~~~~~~~~~ expression
165
- ~~~
166
-
167
- #### Here document
168
-
169
- Format:
170
-
171
- ~~~
172
- (xstr (str "foo\nbar\n"))
173
- "<<`HERE`␊foo␊bar␊HERE"
174
- ~~~~~~~~ expression
175
- ~~~~~~~~ heredoc_body
176
- ~~~~ heredoc_end
177
- ~~~
178
-
179
- ### Regexp
180
-
181
- #### Options
182
-
183
- Format:
184
-
185
- ~~~
186
- (regopt :i :m)
187
- "im"
188
- ~~ expression
189
- ~~~
190
-
191
- #### Regexp
192
-
193
- Format:
194
-
195
- ~~~
196
- (regexp (str "foo") (lvar :bar) (regopt :i))
197
- "/foo#{bar}/i"
198
- ^ begin ^ end
199
- ~~~~~~~~~~~ expression
200
- ~~~
201
-
202
- ### Array
203
-
204
- #### Plain
205
-
206
- Format:
207
-
208
- ~~~
209
- (array (int 1) (int 2))
210
-
211
- "[1, 2]"
212
- ^ begin
213
- ^ end
214
- ~~~~~~ expression
215
- ~~~
216
-
217
- #### Splat
218
-
219
- Can also be used in argument lists: `foo(bar, *baz)`
220
-
221
- Format:
222
-
223
- ~~~
224
- (splat (lvar :foo))
225
- "*foo"
226
- ^ operator
227
- ~~~~ expression
228
- ~~~
229
-
230
- #### With interpolation
231
-
232
- Format:
233
-
234
- ~~~
235
- (array (int 1) (splat (lvar :foo)) (int 2))
236
-
237
- "[1, *foo, 2]"
238
- ^ begin ^ end
239
- ~~~~~~~~~~~~ expression
240
- ~~~
241
-
242
- ### Hash
243
-
244
- #### Pair
245
-
246
- ##### With hashrocket
247
-
248
- Format:
249
-
250
- ~~~
251
- (pair (int 1) (int 2))
252
- "1 => 2"
253
- ~~ operator
254
- ~~~~~~ expression
255
- ~~~
256
-
257
- ##### With label (1.9)
258
-
259
- Format:
260
-
261
- ~~~
262
- (pair (sym :answer) (int 42))
263
- "answer: 42"
264
- ^ operator (pair)
265
- ~~~~~~ expression (sym)
266
- ~~~~~~~~~~ expression (pair)
267
- ~~~
268
-
269
- #### Plain
270
-
271
- Format:
272
-
273
- ~~~
274
- (hash (pair (int 1) (int 2)) (pair (int 3) (int 4)))
275
- "{1 => 2, 3 => 4}"
276
- ^ begin ^ end
277
- ~~~~~~~~~~~~~~~~ expression
278
- ~~~
279
-
280
- #### Keyword splat (2.0)
281
-
282
- Can also be used in argument lists: `foo(bar, **baz)`
283
-
284
- Format:
285
-
286
- ~~~
287
- (kwsplat (lvar :foo))
288
- "**foo"
289
- ~~ operator
290
- ~~~~~ expression
291
- ~~~
292
-
293
- #### With interpolation (2.0)
294
-
295
- Format:
296
-
297
- ~~~
298
- (hash (pair (sym :foo) (int 2)) (kwsplat (lvar :bar)))
299
- "{ foo: 2, **bar }"
300
- ^ begin ^ end
301
- ~~~~~~~~~~~~~~~~~ expression
302
- ~~~
303
-
304
- ### Range
305
-
306
- #### Inclusive
307
-
308
- Format:
309
-
310
- ~~~
311
- (irange (int 1) (int 2))
312
- "1..2"
313
- ~~ operator
314
- ~~~~ expression
315
- ~~~
316
-
317
- #### Exclusive
318
-
319
- Format:
320
-
321
- ~~~
322
- (erange (int 1) (int 2))
323
- "1...2"
324
- ~~~ operator
325
- ~~~~~ expression
326
- ~~~
327
-
328
-
329
- ### Endless (2.6)
330
-
331
- Format:
332
-
333
- ~~~
334
- (irange (int 1) nil)
335
- "1.."
336
- ~~ operator
337
- ~~~ expression
338
-
339
- (erange (int 1) nil)
340
- "1..."
341
- ~~~ operator
342
- ~~~~ expression
343
- ~~~
344
-
345
- ### Beginless (2.7)
346
-
347
- Format:
348
-
349
- ~~~
350
- (irange nil (int 1))
351
- "..1"
352
- ~~ operator
353
- ~~~ expression
354
-
355
- (erange nil (int 1))
356
- "...1"
357
- ~~~ operator
358
- ~~~~ expression
359
- ~~~
360
-
361
- ## Access
362
-
363
- ### Self
364
-
365
- Format:
366
-
367
- ~~~
368
- (self)
369
- "self"
370
- ~~~~ expression
371
- ~~~
372
-
373
- ### Local variable
374
-
375
- Format:
376
-
377
- ~~~
378
- (lvar :foo)
379
- "foo"
380
- ~~~ expression
381
- ~~~
382
-
383
- ### Instance variable
384
-
385
- Format:
386
-
387
- ~~~
388
- (ivar :@foo)
389
- "@foo"
390
- ~~~~ expression
391
- ~~~
392
-
393
- ### Class variable
394
-
395
- Format:
396
-
397
- ~~~
398
- (cvar :@@foo)
399
- "@@foo"
400
- ~~~~~ expression
401
- ~~~
402
-
403
- ### Global variable
404
-
405
- #### Regular global variable
406
-
407
- Format:
408
-
409
- ~~~
410
- (gvar :$foo)
411
- "$foo"
412
- ~~~~ expression
413
- ~~~
414
-
415
- #### Regular expression capture groups
416
-
417
- Format:
418
-
419
- ~~~
420
- (nth-ref 1)
421
- "$1"
422
- ~~ expression
423
- ~~~
424
-
425
- #### Regular expression back-references
426
-
427
- Format:
428
-
429
- ~~~
430
- (back-ref :$&)
431
- "$&"
432
- ~~ expression
433
- (back-ref :$`)
434
- "$`"
435
- (back-ref :$')
436
- "$'"
437
- (back-ref :$+)
438
- "$+"
439
- ~~~
440
-
441
- ### Constant
442
-
443
- #### Top-level constant
444
-
445
- Format:
446
-
447
- ~~~
448
- (const (cbase) :Foo)
449
- "::Foo"
450
- ~~~ name
451
- ~~ double_colon
452
- ~~~~~ expression
453
- ~~~
454
-
455
- #### Scoped constant
456
-
457
- Format:
458
-
459
- ~~~
460
- (const (lvar :a) :Foo)
461
- "a::Foo"
462
- ~~~ name
463
- ~~ double_colon
464
- ~~~~~~ expression
465
- ~~~
466
-
467
- #### Unscoped constant
468
-
469
- Format:
470
-
471
- ~~~
472
- (const nil :Foo)
473
- "Foo"
474
- ~~~ name
475
- ~~~ expression
476
- ~~~
477
-
478
- ### defined?
479
-
480
- Format:
481
-
482
- ~~~
483
- (defined? (lvar :a))
484
- "defined? a"
485
- ~~~~~~~~ keyword
486
- ~~~~~~~~~~ expression
487
-
488
- "defined?(a)"
489
- ~~~~~~~~ keyword
490
- ^ begin
491
- ^ end
492
- ~~~~~~~~~~~ expression
493
- ~~~
494
-
495
- ## Assignment
496
-
497
- ### To local variable
498
-
499
- Format:
500
-
501
- ~~~
502
- (lvasgn :foo (lvar :bar))
503
- "foo = bar"
504
- ^ operator
505
- ~~~~~~~~~ expression
506
- ~~~
507
-
508
- ### To instance variable
509
-
510
- Format:
511
-
512
- ~~~
513
- (ivasgn :@foo (lvar :bar))
514
- "@foo = bar"
515
- ^ operator
516
- ~~~~~~~~~~ expression
517
- ~~~
518
-
519
- ### To class variable
520
-
521
- Format:
522
-
523
- ~~~
524
- (cvasgn :@@foo (lvar :bar))
525
- "@@foo = bar"
526
- ^ operator
527
- ~~~~~~~~~~~ expression
528
- ~~~
529
-
530
- ### To global variable
531
-
532
- Format:
533
-
534
- ~~~
535
- (gvasgn :$foo (lvar :bar))
536
- "$foo = bar"
537
- ^ operator
538
- ~~~~~~~~~~ expression
539
- ~~~
540
-
541
- ### To constant
542
-
543
- #### Top-level constant
544
-
545
- Format:
546
-
547
- ~~~
548
- (casgn (cbase) :Foo (int 1))
549
- "::Foo = 1"
550
- ~~~ name
551
- ~ operator
552
- ~~~~~~~ expression
553
- ~~~
554
-
555
- #### Scoped constant
556
-
557
- Format:
558
-
559
- ~~~
560
- (casgn (lvar :a) :Foo (int 1))
561
- "a::Foo = 1"
562
- ~~~ name
563
- ~ operator
564
- ~~~~~~~~ expression
565
- ~~~
566
-
567
- #### Unscoped constant
568
-
569
- Format:
570
-
571
- ~~~
572
- (casgn nil :Foo (int 1))
573
- "Foo = 1"
574
- ~~~ name
575
- ~ operator
576
- ~~~~~~~ expression
577
- ~~~
578
-
579
- ### To attribute
580
-
581
- Format:
582
-
583
- ~~~
584
- (send (self) :foo= (int 1))
585
- "self.foo = 1"
586
- ^ dot
587
- ~~~ selector
588
- ^ operator
589
- ~~~~~~~~~~~~ expression
590
- ~~~
591
-
592
- ### To attribute, using "safe navigation operator"
593
-
594
- Format:
595
-
596
- ~~~
597
- (csend (self) :foo= (int 1))
598
- "self&.foo = 1"
599
- ^^ dot
600
- ~~~ selector
601
- ^ operator
602
- ~~~~~~~~~~~~~ expression
603
- ~~~
604
-
605
- ### Multiple assignment
606
-
607
- #### Multiple left hand side
608
-
609
- Format:
610
-
611
- ~~~
612
- (mlhs (lvasgn :a) (lvasgn :b))
613
- "a, b"
614
- ~~~~ expression
615
- "(a, b)"
616
- ^ begin
617
- ^ end
618
- ~~~~~~ expression
619
- ~~~
620
-
621
- #### Assignment
622
-
623
- Rule of thumb: every node inside `(mlhs)` is "incomplete"; to make
624
- it "complete", one could imagine that a corresponding node from the
625
- mrhs is "appended" to the node in question. This applies both to
626
- side-effect free assignments (`lvasgn`, etc) and side-effectful
627
- assignments (`send`).
628
-
629
- Format:
630
-
631
- ~~~
632
- (masgn (mlhs (lvasgn :foo) (lvasgn :bar)) (array (int 1) (int 2)))
633
- "foo, bar = 1, 2"
634
- ^ operator
635
- ~~~~~~~~~~~~~~~ expression
636
-
637
- (masgn (mlhs (ivasgn :@a) (cvasgn :@@b)) (array (splat (lvar :c))))
638
- "@a, @@b = *c"
639
-
640
- (masgn (mlhs (lvasgn :a) (mlhs (lvasgn :b)) (lvasgn :c)) (lvar :d))
641
- "a, (b, c) = d"
642
-
643
- (masgn (mlhs (send (self) :a=) (send (self) :[]= (int 1))) (lvar :a))
644
- "self.a, self[1] = a"
645
- ~~~
646
-
647
- ### Binary operator-assignment
648
-
649
- Binary operator-assignment features the same "incomplete assignments" and "incomplete calls" as [multiple assignment](#assignment-1).
650
-
651
- #### Variable binary operator-assignment
652
-
653
- Format:
654
-
655
- ~~~
656
- (op-asgn (lvasgn :a) :+ (int 1))
657
- "a += 1"
658
- ~~ operator
659
- ~~~~~~ expression
660
-
661
- (op-asgn (ivasgn :a) :+ (int 1))
662
- "@a += 1"
663
- ~~~
664
-
665
- #### Method binary operator-assignment
666
-
667
- Format:
668
-
669
- ~~~
670
- (op-asgn (send (ivar :@a) :b) :+ (int 1))
671
- "@a.b += 1"
672
- ~ selector (send)
673
- ~~~~ expression (send)
674
- ~~ operator (op-asgn)
675
- ~~~~~~~~~ expression (op-asgn)
676
-
677
- (op-asgn (send (ivar :@a) :[] (int 0) (int 1))) :+ (int 1))
678
- "@a[0, 1] += 1"
679
- ~~~~~~ selector (send)
680
- ~~~~~~~~ expression (send)
681
- ~~ operator (op-asgn)
682
- ~~~~~~~~~~~~~ expression (op-asgn)
683
- ~~~
684
-
685
- ### Logical operator-assignment
686
-
687
- Logical operator-assignment features the same "incomplete assignments" and "incomplete calls" as [multiple assignment](#assignment-1).
688
-
689
- #### Variable logical operator-assignment
690
-
691
- Format:
692
-
693
- ~~~
694
- (or-asgn (ivasgn :@a) (int 1))
695
- "@a ||= 1"
696
- ~~~ operator
697
- ~~~~~~~~ expression
698
-
699
- (and-asgn (lvasgn :a) (int 1))
700
- "a &&= 1"
701
- ~~~ operator
702
- ~~~~~~~ expression
703
- ~~~
704
-
705
- #### Method logical operator-assignment
706
-
707
- Format:
708
-
709
- ~~~
710
- (or-asgn (send (ivar :@foo) :bar) (int 1))
711
- "@foo.bar ||= 1"
712
- ~~~ selector (send)
713
- ~~~~~~~~ expr (send)
714
- ~~~ operator (or-asgn)
715
- ~~~~~~~~~~~~~~ expression (or-asgn)
716
-
717
- (and-asgn (send (lvar :@foo) :bar) (int 1))
718
- "foo.bar &&= 1"
719
- ~~~ selector (send)
720
- ~~~~~~~ expr (send)
721
- ~~~ operator (and-asgn)
722
- ~~~~~~~~~~~~~ expression (and-asgn)
723
-
724
- (or-asgn (send (ivar :@foo) :[] (int 1) (int 2)) (int 1))
725
- "@foo[1, 2] ||= 1"
726
- ~~~~~~ selector (send)
727
- ~~~~~~~~~~ expr (send)
728
- ~~~ operator (or-asgn)
729
- ~~~~~~~~~~~~~~~~ expression (or-asgn)
730
-
731
- ~~~
732
-
733
- ### Right-hand assignment
734
-
735
- Format:
736
-
737
- ~~~
738
- (rasgn (int 1) (lvasgn :a))
739
- "1 => a"
740
- ~~~~~~ expression
741
- ~~ operator
742
- ~~~
743
-
744
- #### Multiple right-hand assignment
745
-
746
- Format:
747
-
748
- ~~~
749
- (mrasgn (send (int 13) :divmod (int 5)) (mlhs (lvasgn :a) (lvasgn :b)))
750
- "13.divmod(5) => a,b"
751
- ~~~~~~~~~~~~~~~~~~~ expression
752
- ^^ operator
753
- ~~~
754
-
755
- ## Class and module definition
756
-
757
- ### Module
758
-
759
- Format:
760
-
761
- ~~~
762
- (module (const nil :Foo) (nil))
763
- "module Foo; end"
764
- ~~~~~~ keyword
765
- ~~~ end
766
- ~~~
767
-
768
- ### Class
769
-
770
- Format:
771
-
772
- ~~~
773
- (class (const nil :Foo) (const nil :Bar) (nil))
774
- "class Foo < Bar; end"
775
- ~~~~~ keyword ~~~ end
776
- ~ operator
777
- ~~~~~~~~~~~~~~~~~~~~ expression
778
-
779
- (class (const nil :Foo) nil (nil))
780
- "class Foo; end"
781
- ~~~~~ keyword
782
- ~~~ end
783
- ~~~~~~~~~~~~~~ expression
784
- ~~~
785
-
786
- ### Singleton class
787
-
788
- Format:
789
-
790
- ~~~
791
- (sclass (lvar :a) (nil))
792
- "class << a; end"
793
- ~~~~~ keyword
794
- ~~ operator
795
- ~~~ end
796
- ~~~~~~~~~~~~~~~ expression
797
- ~~~
798
-
799
- ## Method (un)definition
800
-
801
- ### Instance methods
802
-
803
- Format:
804
-
805
- ~~~
806
- (def :foo (args) nil)
807
- "def foo; end"
808
- ~~~ keyword
809
- ~~~ name
810
- ~~~ end
811
- ~~~~~~~~~~~~ expression
812
- ~~~
813
-
814
- ### Singleton methods
815
-
816
- Format:
817
-
818
- ~~~
819
- (defs (self) :foo (args) nil)
820
- "def self.foo; end"
821
- ~~~ keyword
822
- ~~~ name
823
- ~~~ end
824
- ~~~~~~~~~~~~~~~~~ expression
825
- ~~~
826
-
827
- ### "Endless" method
828
-
829
- Format:
830
-
831
- ~~~
832
- (def_e :foo (args) (int 42))
833
- "def foo() = 42"
834
- ~~~ keyword
835
- ~~~ name
836
- ^ assignment
837
- ~~~~~~~~~~~~~~ expression
838
- ~~~
839
-
840
-
841
- ### "Endless" singleton method
842
-
843
- Format:
844
-
845
- ~~~
846
- (defs_e (self) :foo (args) (int 42))
847
- "def self.foo() = 42"
848
- ~~~ keyword
849
- ~~~ name
850
- ^ assignment
851
- ~~~~~~~~~~~~~~~~~~~ expression
852
- ~~~
853
-
854
- ### Undefinition
855
-
856
- Format:
857
-
858
- ~~~
859
- (undef (sym :foo) (sym :bar) (dsym (str "foo") (int 1)))
860
- "undef foo :bar :"foo#{1}""
861
- ~~~~~ keyword
862
- ~~~~~~~~~~~~~~~~~~~~~~~~~ expression
863
- ~~~
864
-
865
- ## Aliasing
866
-
867
- ### Method aliasing
868
-
869
- Format:
870
-
871
- ~~~
872
- (alias (sym :foo) (dsym (str "foo") (int 1)))
873
- "alias foo :"foo#{1}""
874
- ~~~~~ keyword
875
- ~~~~~~~~~~~~~~~~~~~~ expression
876
- ~~~
877
-
878
- ### Global variable aliasing
879
-
880
- Format:
881
-
882
- ~~~
883
- (alias (gvar :$foo) (gvar :$bar))
884
- "alias $foo $bar"
885
- ~~~~~ keyword
886
- ~~~~~~~~~~~~~~~ expression
887
-
888
- (alias (gvar :$foo) (back-ref :$&))
889
- "alias $foo $&"
890
- ~~~~~ keyword
891
- ~~~~~~~~~~~~~~~ expression
892
- ~~~
893
-
894
- ## Formal arguments
895
-
896
- Format:
897
-
898
- ~~~
899
- (args (arg :foo))
900
- "(foo)"
901
- ~~~~~ expression
902
- ~~~
903
-
904
- ### Required argument
905
-
906
- Format:
907
-
908
- ~~~
909
- (arg :foo)
910
- "foo"
911
- ~~~ expression
912
- ~~~ name
913
- ~~~
914
-
915
- ### Optional argument
916
-
917
- Format:
918
-
919
- ~~~
920
- (optarg :foo (int 1))
921
- "foo = 1"
922
- ~~~~~~~ expression
923
- ^ operator
924
- ~~~ name
925
- ~~~
926
-
927
- ### Named splat argument
928
-
929
- Format:
930
-
931
- ~~~
932
- (restarg :foo)
933
- "*foo"
934
- ~~~~ expression
935
- ~~~ name
936
- ~~~
937
-
938
- Begin of the `expression` points to `*`.
939
-
940
- ### Unnamed splat argument
941
-
942
- Format:
943
-
944
- ~~~
945
- (restarg)
946
- "*"
947
- ^ expression
948
- ~~~
949
-
950
- ### Block argument
951
-
952
- Format:
953
-
954
- ~~~
955
- (blockarg :foo)
956
- "&foo"
957
- ~~~ name
958
- ~~~~ expression
959
- ~~~
960
-
961
- Begin of the `expression` points to `&`.
962
-
963
- ### Auto-expanding proc argument (1.9)
964
-
965
- In Ruby 1.9 and later, when a proc-like closure (i.e. a closure
966
- created by capturing a block or with the `proc` method, but not
967
- with the `->{}` syntax or the `lambda` method) has exactly one
968
- argument, and it is called with more than one argument, the behavior
969
- is as if the array of all arguments was instead passed as the sole
970
- argument. This behavior can be prevented by adding a comma after
971
- the sole argument (e.g. `|foo,|`).
972
-
973
- Format:
974
-
975
- ~~~
976
- (procarg0 (arg :foo))
977
- "|foo|"
978
- ~~~ expression
979
-
980
- (procarg0 (arg :foo) (arg :bar))
981
- "|(foo, bar)|"
982
- ~ begin
983
- ~ end
984
- ~~~~~~~~~~ expression
985
- ~~~
986
-
987
- ### Expression arguments
988
-
989
- Ruby 1.8 allows to use arbitrary expressions as block arguments,
990
- such as `@var` or `foo.bar`. Such expressions should be treated as
991
- if they were on the lhs of a multiple assignment.
992
-
993
- Format:
994
-
995
- ~~~
996
- (args (arg_expr (ivasgn :@bar)))
997
- "|@bar|"
998
-
999
- (args (arg_expr (send (send nil :foo) :a=)))
1000
- "|foo.a|"
1001
-
1002
- (args (restarg_expr (ivasgn :@bar)))
1003
- "|*@bar|"
1004
-
1005
- (args (blockarg_expr (ivasgn :@bar)))
1006
- "|&@bar|"
1007
- ~~~
1008
-
1009
- ### Block shadow arguments
1010
-
1011
- Format:
1012
-
1013
- ~~~
1014
- (args (shadowarg :foo) (shadowarg :bar))
1015
- "|; foo, bar|"
1016
- ~~~
1017
-
1018
- ### Decomposition
1019
-
1020
- Format:
1021
-
1022
- ~~~
1023
- (def :f (args (arg :a) (mlhs (arg :foo) (restarg :bar))))
1024
- "def f(a, (foo, *bar)); end"
1025
- ^ begin ^ end
1026
- ~~~~~~~~~~~ expression
1027
- ~~~
1028
-
1029
- ### Required keyword argument
1030
-
1031
- Format:
1032
-
1033
- ~~~
1034
- (kwarg :foo)
1035
- "foo:"
1036
- ~~~~ expression
1037
- ~~~~ name
1038
- ~~~
1039
-
1040
- ### Optional keyword argument
1041
-
1042
- Format:
1043
-
1044
- ~~~
1045
- (kwoptarg :foo (int 1))
1046
- "foo: 1"
1047
- ~~~~~~ expression
1048
- ~~~~ name
1049
- ~~~
1050
-
1051
- ### Named keyword splat argument
1052
-
1053
- Format:
1054
-
1055
- ~~~
1056
- (kwrestarg :foo)
1057
- "**foo"
1058
- ~~~~~ expression
1059
- ~~~ name
1060
- ~~~
1061
-
1062
- ### Unnamed keyword splat argument
1063
-
1064
- Format:
1065
-
1066
- ~~~
1067
- (kwrestarg)
1068
- "**"
1069
- ~~ expression
1070
- ~~~
1071
-
1072
- ### Keyword nil argument
1073
-
1074
- Format:
1075
-
1076
- ~~~
1077
- (kwnilarg)
1078
- "**nil"
1079
- ~~~ name
1080
- ~~~~~ expression
1081
- ~~~
1082
-
1083
- ### Objective-C arguments
1084
-
1085
- MacRuby includes a few more syntactic "arguments" whose name becomes
1086
- the part of the Objective-C method name, despite looking like Ruby 2.0
1087
- keyword arguments, and are thus treated differently.
1088
-
1089
- #### Objective-C label-like keyword argument
1090
-
1091
- Format:
1092
-
1093
- ~~~
1094
- (objc-kwarg :a :b)
1095
- "a: b"
1096
- ~ keyword
1097
- ~ operator
1098
- ~ argument
1099
- ~~~~ expression
1100
- ~~~
1101
-
1102
- #### Objective-C pair-like keyword argument
1103
-
1104
- Format:
1105
-
1106
- ~~~
1107
- (objc-kwarg :a :b)
1108
- "a => b"
1109
- ~ keyword
1110
- ~~ operator
1111
- ~ argument
1112
- ~~~~~~ expression
1113
- ~~~
1114
-
1115
- #### Objective-C keyword splat argument
1116
-
1117
- Format:
1118
-
1119
- ~~~
1120
- (objc-restarg (objc-kwarg :foo))
1121
- "(*a: b)"
1122
- ~ objc-kwarg.keyword
1123
- ~ objc-kwarg.operator
1124
- ~ objc-kwarg.argument
1125
- ~ operator
1126
- ~~~~~ expression
1127
- ~~~
1128
-
1129
- Note that these splat arguments will only be parsed inside parentheses,
1130
- e.g. in the following code:
1131
-
1132
- ~~~
1133
- def f((*a: b)); end
1134
- ~~~
1135
-
1136
- However, the following code results in a parse error:
1137
-
1138
- ~~~
1139
- def f(*a: b); end
1140
- ~~~
1141
-
1142
- ## Numbered parameters
1143
-
1144
- ### Block with numbered parameters
1145
-
1146
- Ruby 2.7 introduced a feature called "numbered parameters".
1147
- Numbered and ordinal parameters are mutually exclusive, so if the block
1148
- has only numbered parameters it also has a different AST node.
1149
-
1150
- Note that the second child represents a total number of numbered parameters.
1151
-
1152
- Format:
1153
-
1154
- ~~~
1155
- s(:numblock,
1156
- s(:send, nil, :proc), 3,
1157
- s(:send,
1158
- s(:lvar, :_1), :+,
1159
- s(:lvar, :_3)))
1160
- "proc { _1 + _3 }"
1161
- ~ begin ~ end
1162
- ~~~~~~~~~~~~~~~~ expression
1163
- ~~~
1164
-
1165
- ## Forward arguments
1166
-
1167
- ### Method definition accepting only forwarding arguments
1168
-
1169
- Ruby 2.7 introduced a feature called "arguments forwarding".
1170
- When a method takes any arguments for forwarding them in the future
1171
- the whole `args` node gets replaced with `forward-args` node.
1172
-
1173
- Format if `emit_forward_arg` compatibility flag is disabled:
1174
-
1175
- ~~~
1176
- (def :foo
1177
- (forward-args) nil)
1178
- "def foo(...); end"
1179
- ~ end
1180
- ~ begin
1181
- ~~~~~ expression
1182
- ~~~
1183
-
1184
- However, Ruby 2.8 added support for leading arguments before `...`, and so
1185
- it can't be used as a replacement of the `(args)` node anymore. To solve it
1186
- `emit_forward_arg` should be enabled.
1187
-
1188
- Format if `emit_forward_arg` compatibility flag is enabled:
1189
-
1190
- ~~~
1191
- (def :foo
1192
- (args
1193
- (forward-arg)) nil)
1194
- "def foo(...); end"
1195
- ~ begin (args)
1196
- ~ end (args)
1197
- ~~~~~ expression (args)
1198
- ~~~ expression (forward_arg)
1199
- ~~~
1200
-
1201
- Note that the node is called `forward_arg` when emitted separately.
1202
-
1203
- ### Method call taking arguments of the currently forwarding method
1204
-
1205
- Format:
1206
-
1207
- ~~~
1208
- (send nil :foo
1209
- (forwarded-args))
1210
- "foo(...)"
1211
- ~~~ expression
1212
- ~~~
1213
-
1214
- ## Send
1215
-
1216
- ### To self
1217
-
1218
- Format:
1219
-
1220
- ~~~
1221
- (send nil :foo (lvar :bar))
1222
- "foo(bar)"
1223
- ~~~ selector
1224
- ^ begin
1225
- ^ end
1226
- ~~~~~~~~ expression
1227
- ~~~
1228
-
1229
- ### To receiver
1230
-
1231
- Format:
1232
-
1233
- ~~~
1234
- (send (lvar :foo) :bar (int 1))
1235
- "foo.bar(1)"
1236
- ^ dot
1237
- ~~~ selector
1238
- ^ begin
1239
- ^ end
1240
- ~~~~~~~~~~ expression
1241
-
1242
- (send (lvar :foo) :+ (int 1))
1243
- "foo + 1"
1244
- ^ selector
1245
- ~~~~~~~ expression
1246
-
1247
- (send (lvar :foo) :-@)
1248
- "-foo"
1249
- ^ selector
1250
- ~~~~ expression
1251
-
1252
- (send (lvar :foo) :a= (int 1))
1253
- "foo.a = 1"
1254
- ~ selector
1255
- ^ operator
1256
- ~~~~~~~~~ expression
1257
- ~~~
1258
-
1259
- ### To superclass
1260
-
1261
- Format of super with arguments:
1262
-
1263
- ~~~
1264
- (super (lvar :a))
1265
- "super a"
1266
- ~~~~~ keyword
1267
- ~~~~~~~ expression
1268
-
1269
- (super)
1270
- "super()"
1271
- ^ begin
1272
- ^ end
1273
- ~~~~~ keyword
1274
- ~~~~~~~ expression
1275
- ~~~
1276
-
1277
- Format of super without arguments (**z**ero-arity):
1278
-
1279
- ~~~
1280
- (zsuper)
1281
- "super"
1282
- ~~~~~ keyword
1283
- ~~~~~ expression
1284
- ~~~
1285
-
1286
- ### To block argument
1287
-
1288
- Format:
1289
-
1290
- ~~~
1291
- (yield (lvar :foo))
1292
- "yield(foo)"
1293
- ~~~~~ keyword
1294
- ^ begin
1295
- ^ end
1296
- ~~~~~~~~~~ expression
1297
- ~~~
1298
-
1299
- ### Indexing
1300
-
1301
- Format:
1302
-
1303
- ~~~
1304
- (index (lvar :foo) (int 1))
1305
- "foo[1]"
1306
- ^ begin
1307
- ^ end
1308
- ~~~~~~ expression
1309
-
1310
- (indexasgn (lvar :bar) (int 1) (int 2) (lvar :baz))
1311
- "bar[1, 2] = baz"
1312
- ^ begin
1313
- ^ end
1314
- ^ operator
1315
- ~~~~~~~~~~~~~~~ expression
1316
-
1317
- ~~~
1318
-
1319
- ### Passing a literal block
1320
-
1321
- ~~~
1322
- (block (send nil :foo) (args (arg :bar)) (begin ...))
1323
- "foo do |bar|; end"
1324
- ~~ begin
1325
- ~~~ end
1326
- ~~~~~~~~~~~~~ expression
1327
- ~~~
1328
-
1329
- ### Passing expression as block
1330
-
1331
- Used when passing expression as block `foo(&bar)`
1332
-
1333
- ~~~
1334
- (send nil :foo (int 1) (block-pass (lvar :foo)))
1335
- "foo(1, &foo)"
1336
- ^ operator
1337
- ~~~~ expression
1338
- ~~~
1339
-
1340
- ### "Stabby lambda"
1341
-
1342
- ~~~
1343
- (block (lambda) (args) nil)
1344
- "-> {}"
1345
- ~~ lambda.expression
1346
- ~~~
1347
-
1348
- ### "Safe navigation operator"
1349
-
1350
- ~~~
1351
- (csend (send nil :foo) :bar)
1352
- "foo&.bar"
1353
- ~~ dot
1354
- ~~~
1355
-
1356
- ### Objective-C variadic send
1357
-
1358
- MacRuby allows to pass a variadic amount of arguments via the last
1359
- keyword "argument". Semantically, these, together with the pair value
1360
- of the last pair in the hash implicitly passed as the last argument,
1361
- form an array, which replaces the pair value. Despite that, the node
1362
- is called `objc-varargs` to distinguish it from a literal array passed
1363
- as a value.
1364
-
1365
- ~~~
1366
- (send nil :foo (int 1) (hash (pair (sym :bar) (objc-varargs (int 1) (int 2) (nil)))))
1367
- "foo(1, bar: 2, 3, nil)"
1368
- ~~~~~~~~~ expression (array)
1369
- ~~~
1370
-
1371
- ## Control flow
1372
-
1373
- ### Logical operators
1374
-
1375
- #### Binary (and or && ||)
1376
-
1377
- Format:
1378
-
1379
- ~~~
1380
- (and (lvar :foo) (lvar :bar))
1381
- "foo and bar"
1382
- ~~~ operator
1383
- ~~~~~~~~~~~ expression
1384
- ~~~
1385
-
1386
- ~~~
1387
- (or (lvar :foo) (lvar :bar))
1388
- "foo or bar"
1389
- ~~ operator
1390
- ~~~~~~~~~~ expression
1391
- ~~~
1392
-
1393
- #### Unary (! not) (1.8)
1394
-
1395
- Format:
1396
-
1397
- ~~~
1398
- (not (lvar :foo))
1399
- "!foo"
1400
- ^ operator
1401
- "not foo"
1402
- ~~~ operator
1403
- ~~~
1404
-
1405
- ### Branching
1406
-
1407
- #### Without else
1408
-
1409
- Format:
1410
-
1411
- ~~~
1412
- (if (lvar :cond) (lvar :iftrue) nil)
1413
- "if cond then iftrue; end"
1414
- ~~ keyword
1415
- ~~~~ begin
1416
- ~~~ end
1417
- ~~~~~~~~~~~~~~~~~~~~~~~~ expression
1418
-
1419
- "if cond; iftrue; end"
1420
- ~~ keyword
1421
- ~~~ end
1422
- ~~~~~~~~~~~~~~~~~~~~ expression
1423
-
1424
- "iftrue if cond"
1425
- ~~ keyword
1426
- ~~~~~~~~~~~~~~ expression
1427
-
1428
- (if (lvar :cond) nil (lvar :iftrue))
1429
- "unless cond then iftrue; end"
1430
- ~~~~~~ keyword
1431
- ~~~~ begin
1432
- ~~~ end
1433
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1434
-
1435
- "unless cond; iftrue; end"
1436
- ~~~~~~ keyword
1437
- ~~~ end
1438
- ~~~~~~~~~~~~~~~~~~~~~~~~ expression
1439
-
1440
- "iftrue unless cond"
1441
- ~~~~~~ keyword
1442
- ~~~~~~~~~~~~~~~~~~ expression
1443
- ~~~
1444
-
1445
- #### With else
1446
-
1447
- Format:
1448
-
1449
- ~~~
1450
- (if (lvar :cond) (lvar :iftrue) (lvar :iffalse))
1451
- "if cond then iftrue; else; iffalse; end"
1452
- ~~ keyword
1453
- ~~~~ begin
1454
- ~~~~ else
1455
- ~~~ end
1456
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1457
-
1458
- "if cond; iftrue; else; iffalse; end"
1459
- ~~ keyword
1460
- ~~~~ else
1461
- ~~~ end
1462
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1463
-
1464
- (if (lvar :cond) (lvar :iffalse) (lvar :iftrue))
1465
- "unless cond then iftrue; else; iffalse; end"
1466
- ~~~~~~ keyword
1467
- ~~~~ begin
1468
- ~~~~ else
1469
- ~~~ end
1470
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1471
-
1472
- "unless cond; iftrue; else; iffalse; end"
1473
- ~~~~~~ keyword
1474
- ~~~~ else
1475
- ~~~ end
1476
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1477
- ~~~
1478
-
1479
- #### With elsif
1480
-
1481
- Format:
1482
-
1483
- ~~~
1484
- (if (lvar :cond1) (int 1) (if (lvar :cond2 (int 2) (int 3))))
1485
- "if cond1; 1; elsif cond2; 2; else 3; end"
1486
- ~~ keyword (left)
1487
- ~~~~~ else (left)
1488
- ~~~ end (left)
1489
- ~~~~~ keyword (right)
1490
- ~~~~ else (right)
1491
- ~~~ end (right)
1492
- ~~~
1493
-
1494
- #### Ternary
1495
-
1496
- Format:
1497
-
1498
- ~~~
1499
- (if (lvar :cond) (lvar :iftrue) (lvar :iffalse))
1500
- "cond ? iftrue : iffalse"
1501
- ^ question
1502
- ^ colon
1503
- ~~~~~~~~~~~~~~~~~~~~~~~ expression
1504
- ~~~
1505
-
1506
- ### Case matching
1507
-
1508
- #### When clause
1509
-
1510
- Format:
1511
-
1512
- ~~~
1513
- (when (regexp "foo" (regopt)) (begin (lvar :bar)))
1514
- "when /foo/ then bar"
1515
- ~~~~ keyword
1516
- ~~~~ begin
1517
- ~~~~~~~~~~~~~~~~~~~ expression
1518
-
1519
- (when (int 1) (int 2) (send nil :meth))
1520
- "when 1, 2; meth"
1521
-
1522
- (when (int 1) (splat (lvar :foo)) (send nil :meth))
1523
- "when 1, *foo; meth"
1524
-
1525
- (when (splat (lvar :foo)) (send nil :meth))
1526
- "when *foo; meth"
1527
- ~~~
1528
-
1529
- #### Case-expression clause
1530
-
1531
- ##### Without else
1532
-
1533
- Format:
1534
-
1535
- ~~~
1536
- (case (lvar :foo) (when (str "bar") (lvar :bar)) nil)
1537
- "case foo; when "bar"; bar; end"
1538
- ~~~~ keyword ~~~ end
1539
- ~~~
1540
-
1541
- ##### With else
1542
-
1543
- Format:
1544
-
1545
- ~~~
1546
- (case (lvar :foo) (when (str "bar") (lvar :bar)) (lvar :baz))
1547
- "case foo; when "bar"; bar; else baz; end"
1548
- ~~~~ keyword ~~~~ else ~~~ end
1549
- ~~~
1550
-
1551
- #### Case-conditions clause
1552
-
1553
- ##### Without else
1554
-
1555
- Format:
1556
-
1557
- ~~~
1558
- (case nil (when (lvar :bar) (lvar :bar)) nil)
1559
- "case; when bar; bar; end"
1560
- ~~~~ keyword ~~~ end
1561
- ~~~
1562
-
1563
- ##### With else
1564
-
1565
- Format:
1566
-
1567
- ~~~
1568
- (case nil (when (lvar :bar) (lvar :bar)) (lvar :baz))
1569
- "case; when bar; bar; else baz; end"
1570
- ~~~~ keyword ~~~~ else ~~~ end
1571
-
1572
- (case nil (lvar :baz))
1573
- "case; else baz; end"
1574
- ~~~~ keyword
1575
- ~~~~ else
1576
- ~~~ end
1577
- ~~~
1578
-
1579
- ### Looping
1580
-
1581
- #### With precondition
1582
-
1583
- Format:
1584
-
1585
- ~~~
1586
- (while (lvar :condition) (send nil :foo))
1587
- "while condition do foo; end"
1588
- ~~~~~ keyword
1589
- ~~ begin
1590
- ~~~ end
1591
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1592
-
1593
- "while condition; foo; end"
1594
- ~~~~~ keyword
1595
- ~~~ end
1596
- ~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1597
-
1598
- "foo while condition"
1599
- ~~~~~ keyword
1600
- ~~~~~~~~~~~~~~~~~~~ expression
1601
-
1602
- (until (lvar :condition) (send nil :foo))
1603
- "until condition do foo; end"
1604
- ~~~~~ keyword
1605
- ~~ begin
1606
- ~~~ end
1607
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1608
-
1609
- (until (lvar :condition) (send nil :foo))
1610
- "until condition; foo; end"
1611
- ~~~~~ keyword
1612
- ~~~ end
1613
- ~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1614
-
1615
- "foo until condition"
1616
- ~~~~~ keyword
1617
- ~~~~~~~~~~~~~~~~~~~ expression
1618
- ~~~
1619
-
1620
- #### With postcondition
1621
-
1622
- Format:
1623
-
1624
- ~~~
1625
- (while-post (lvar :condition) (kwbegin (send nil :foo)))
1626
- "begin; foo; end while condition"
1627
- ~~~~~ begin (begin)
1628
- ~~~ end (begin)
1629
- ~~~~~ keyword (while-post)
1630
-
1631
- (until-post (lvar :condition) (kwbegin (send nil :foo)))
1632
- "begin; foo; end until condition"
1633
- ~~~~~ begin (begin)
1634
- ~~~ end (begin)
1635
- ~~~~~ keyword (until-post)
1636
- ~~~
1637
-
1638
- #### For-in
1639
-
1640
- Format:
1641
-
1642
- ~~~
1643
- (for (lvasgn :a) (lvar :array) (send nil :p (lvar :a)))
1644
- "for a in array do p a; end"
1645
- ~~~ keyword
1646
- ~~ in
1647
- ~~ begin
1648
- ~~~ end
1649
-
1650
- "for a in array; p a; end"
1651
- ~~~ keyword
1652
- ~~ in
1653
- ~~~ end
1654
-
1655
- (for
1656
- (mlhs (lvasgn :a) (lvasgn :b)) (lvar :array)
1657
- (send nil :p (lvar :a) (lvar :b)))
1658
- "for a, b in array; p a, b; end"
1659
- ~~~
1660
-
1661
- #### Break
1662
-
1663
- Format:
1664
-
1665
- ~~~
1666
- (break (int 1))
1667
- "break 1"
1668
- ~~~~~ keyword
1669
- ~~~~~~~ expression
1670
- ~~~
1671
-
1672
- #### Next
1673
-
1674
- Format:
1675
-
1676
- ~~~
1677
- (next (int 1))
1678
- "next 1"
1679
- ~~~~ keyword
1680
- ~~~~~~ expression
1681
- ~~~
1682
-
1683
- #### Redo
1684
-
1685
- Format:
1686
-
1687
- ~~~
1688
- (redo)
1689
- "redo"
1690
- ~~~~ keyword
1691
- ~~~~ expression
1692
- ~~~
1693
-
1694
- ### Return
1695
-
1696
- Format:
1697
-
1698
- ~~~
1699
- (return (lvar :foo))
1700
- "return(foo)"
1701
- ~~~~~~ keyword
1702
- ~~~~~~~~~~~ expression
1703
- ~~~
1704
-
1705
- ### Exception handling
1706
-
1707
- #### Rescue body
1708
-
1709
- Format:
1710
-
1711
- ~~~
1712
- (resbody (array (const nil :Exception) (const nil :A)) (lvasgn :bar) (int 1))
1713
- "rescue Exception, A => bar; 1"
1714
- ~~~~~~ keyword ~~ assoc
1715
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1716
-
1717
- "rescue Exception, A => bar then 1"
1718
- ~~~~~~ keyword ~~ assoc
1719
- ~~~~ begin
1720
-
1721
- (resbody (array (const nil :Exception)) (ivasgn :bar) (int 1))
1722
- "rescue Exception => @bar; 1"
1723
- ~~~~~~ keyword ~~ assoc
1724
-
1725
- (resbody nil (lvasgn :bar) (int 1))
1726
- "rescue => bar; 1"
1727
- ~~~~~~ keyword
1728
- ~~ assoc
1729
-
1730
- (resbody nil nil (int 1))
1731
- "rescue; 1"
1732
- ~~~~~~ keyword
1733
- ~~~
1734
-
1735
- #### Rescue statement
1736
-
1737
- ##### Without else
1738
-
1739
- Format:
1740
-
1741
- ~~~
1742
- (begin
1743
- (rescue (send nil :foo) (resbody ...) (resbody ...) nil))
1744
- "begin; foo; rescue Exception; rescue; end"
1745
- ~~~~~ begin ~~~ end
1746
- ~~~~~~~~~~~~~~~~~ expression (rescue.resbody/1)
1747
- ~~~~~~~ expression (rescue.resbody/2)
1748
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (rescue)
1749
- ~~~
1750
-
1751
- ##### With else
1752
-
1753
- Format:
1754
-
1755
- ~~~
1756
- (begin
1757
- (rescue (send nil :foo) (resbody ...) (resbody ...) (true)))
1758
- "begin; foo; rescue Exception; rescue; else true end"
1759
- ~~~~~ begin ~~~~ else (rescue)
1760
- ~~~ end
1761
- ~~~
1762
-
1763
- #### Ensure statement
1764
-
1765
- Format:
1766
-
1767
- ~~~
1768
- (begin
1769
- (ensure (send nil :foo) (send nil :bar))
1770
- "begin; foo; ensure; bar; end"
1771
- ~~~~~ begin ~~~~~~ keyword (ensure)
1772
- ~~~ end
1773
- ~~~
1774
-
1775
- #### Rescue with ensure
1776
-
1777
- Format:
1778
-
1779
- ~~~
1780
- (begin
1781
- (ensure
1782
- (rescue (send nil :foo) (resbody ...) (int 1))
1783
- (send nil :bar))
1784
- "begin; foo; rescue; nil; else; 1; ensure; bar; end"
1785
- ~~~~~ begin
1786
- ~~~~ else (ensure.rescue)
1787
- ~~~~~~~~~~~~~~~~~~~~~ expression (rescue)
1788
- ~~~~~~ keyword (ensure)
1789
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (ensure)
1790
- ~~~ end
1791
- ~~~
1792
-
1793
- #### Retry
1794
-
1795
- Format:
1796
-
1797
- ~~~
1798
- (retry)
1799
- "retry"
1800
- ~~~~~ keyword
1801
- ~~~~~ expression
1802
- ~~~
1803
-
1804
- ### BEGIN and END
1805
-
1806
- Format:
1807
-
1808
- ~~~
1809
- (preexe (send nil :puts (str "foo")))
1810
- "BEGIN { puts "foo" }"
1811
- ~~~~~ keyword
1812
- ^ begin ^ end
1813
- ~~~~~~~~~~~~~~~~~~~~ expression
1814
-
1815
- (postexe (send nil :puts (str "bar")))
1816
- "END { puts "bar" }"
1817
- ~~~ keyword
1818
- ^ begin ^ end
1819
- ~~~~~~~~~~~~~~~~~~ expression
1820
- ~~~
1821
-
1822
- ## Miscellanea
1823
-
1824
- ### Flip-flops
1825
-
1826
- Format:
1827
-
1828
- ~~~
1829
- (iflipflop (lvar :a) (lvar :b))
1830
- "if a..b; end"
1831
- ~~ operator
1832
- ~~~~ expression
1833
-
1834
- (eflipflop (lvar :a) (lvar :b))
1835
- "if a...b; end"
1836
- ~~~ operator
1837
- ~~~~~ expression
1838
- ~~~
1839
-
1840
- ### Implicit matches
1841
-
1842
- Format:
1843
-
1844
- ~~~
1845
- (match-current-line (regexp (str "a") (regopt)))
1846
- "if /a/; end"
1847
- ~~~ expression
1848
- ~~~
1849
-
1850
- ### Local variable injecting matches
1851
-
1852
- Format:
1853
-
1854
- ~~~
1855
- (match-with-lvasgn (regexp (str "(?<match>bar)") (regopt)) (lvar :baz))
1856
- "/(?<match>bar)/ =~ baz"
1857
- ~~ selector
1858
- ~~~~~~~~~~~~~~~~~~~~~~ expression
1859
- ~~~
1860
-
1861
- ## Special constants
1862
-
1863
- ### File
1864
-
1865
- Format:
1866
-
1867
- ~~~
1868
- (__FILE__)
1869
- "__FILE__"
1870
- ~~~~~~~~ expression
1871
- ~~~
1872
-
1873
- ### Line
1874
-
1875
- Format:
1876
-
1877
- ~~~
1878
- (__LINE__)
1879
- "__LINE__"
1880
- ~~~~~~~~ expression
1881
- ~~~
1882
-
1883
- ### Encoding
1884
-
1885
- Format:
1886
-
1887
- ~~~
1888
- (__ENCODING__)
1889
- "__ENCODING__"
1890
- ~~~~~~~~~~~~ expression
1891
- ~~~
1892
-
1893
- ## Pattern matching
1894
-
1895
- ### Using `in` modifier
1896
-
1897
- Format:
1898
-
1899
- ~~~
1900
- (in-match
1901
- (int 1)
1902
- (match-var :a))
1903
- "1 in a"
1904
- ~~ operator
1905
- ~~~~~~ expression
1906
- ~~~
1907
-
1908
- ### Case with pattern matching
1909
-
1910
- #### Without else
1911
-
1912
- Format:
1913
-
1914
- ~~~
1915
- (case-match
1916
- (str "str")
1917
- (in-pattern
1918
- (match-var :foo)
1919
- (lvar :bar)) nil)
1920
- "case "str"; in foo; bar; end"
1921
- ~~~~ keyword ~~~ end
1922
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1923
- ~~~
1924
-
1925
- #### With else
1926
-
1927
- Format:
1928
-
1929
- ~~~
1930
- (case-match,
1931
- (str "str")
1932
- (in-pattern
1933
- (match-var :foo)
1934
- (lvar :bar))
1935
- (lvar :baz))
1936
- "case "str"; in foo; bar; else; baz; end"
1937
- ~~~~ keyword ~~~~ else ~~~ end
1938
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1939
- ~~~
1940
-
1941
- #### With empty else
1942
-
1943
- Empty `else` differs from the missing (or _implicit_) `else` for pattern matching, since
1944
- the latter one raises a `NoMatchingPattern` exception. Thus, we need a way to distinguish this
1945
- two cases in the resulting AST.
1946
-
1947
- Format:
1948
-
1949
- ~~~
1950
- (case-match,
1951
- (str "str")
1952
- (in-pattern
1953
- (match-var :foo)
1954
- (lvar :bar))
1955
- (empty-else))
1956
- "case "str"; in foo; bar; else; end"
1957
- ~~~~ keyword ~~~~ else
1958
- ~~~ end
1959
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1960
- ~~~
1961
-
1962
- ### In clause
1963
-
1964
- Format:
1965
-
1966
- ~~~
1967
- (in-pattern
1968
- (match-var :foo)
1969
- (lvar :bar))
1970
- "in foo then bar"
1971
- ~~ keyword
1972
- ~~~~ begin
1973
- ~~~~~~~~~~~~~~~ expression
1974
- ~~~
1975
-
1976
- ### If guard
1977
-
1978
- This guard runs after matching, so it's not an `if` modifier.
1979
-
1980
- Format:
1981
-
1982
- ~~~
1983
- (in-pattern
1984
- (match-var :foo)
1985
- (if-guard
1986
- (lvar :bar)) nil)
1987
- "in foo if bar"
1988
- ~~ keyword
1989
- ~~~~~~ expression
1990
- ~~~
1991
-
1992
- ### Unless guard
1993
-
1994
- This guard runs after matching, so it's not an `unless` modifier.
1995
-
1996
- Format:
1997
-
1998
- ~~~
1999
- (in-pattern
2000
- (match-var :foo)
2001
- (unless-guard
2002
- (lvar :bar)) nil)
2003
- "in foo unless bar"
2004
- ~~~~~~ keyword
2005
- ~~~~~~~~~~ expression
2006
- ~~~
2007
-
2008
- ### Match variable
2009
-
2010
- Format:
2011
-
2012
- ~~~
2013
- (match-var :foo)
2014
- "in foo"
2015
- ~~~ name
2016
- ~~~ expression
2017
- ~~~
2018
-
2019
- ### Match rest
2020
-
2021
- #### With name
2022
-
2023
- Format:
2024
-
2025
- ~~~
2026
- (match-rest
2027
- (match-var :foo))
2028
- "in *foo"
2029
- ~ operator
2030
- ~~~~ expression
2031
- ~~~
2032
-
2033
- #### Without name
2034
-
2035
- Format:
2036
-
2037
- ~~~
2038
- (match-rest)
2039
- "in *"
2040
- ~ operator
2041
- ~ expression
2042
- ~~~
2043
-
2044
- ### Pin operator
2045
-
2046
- Format:
2047
-
2048
- ~~~
2049
- (pin
2050
- (lvar :foo))
2051
- "in ^foo"
2052
- ~ selector
2053
- ~~~~ expression
2054
- ~~~
2055
-
2056
- ### Match alternative
2057
-
2058
- Format:
2059
-
2060
- ~~~
2061
- (match-alt
2062
- (pin
2063
- (lvar :foo))
2064
- (int 1))
2065
- "in ^foo | 1"
2066
- ~ operator
2067
- ~~~~~~~~ expression
2068
- ~~~
2069
-
2070
- ### Match with alias
2071
-
2072
- Format:
2073
-
2074
- ~~~
2075
- (match-as
2076
- (int 1)
2077
- (match-var :foo))
2078
- "in 1 => foo"
2079
- ~~ operator
2080
- ~~~~~~~~ expression
2081
- ~~~
2082
-
2083
- ### Match using array pattern
2084
-
2085
- #### Explicit
2086
-
2087
- Format:
2088
-
2089
- ~~~
2090
- (array-pattern
2091
- (pin
2092
- (lvar :foo))
2093
- (match-var :bar))
2094
- "in [^foo, bar]"
2095
- ~ begin ~ end
2096
- ~~~~~~~~~~~ expression
2097
- ~~~
2098
-
2099
- #### Explicit with tail
2100
-
2101
- Adding a trailing comma in the end works as `, *`
2102
-
2103
- Format:
2104
-
2105
- ~~~
2106
- (array-pattern-with-tail
2107
- (pin
2108
- (lvar :foo))
2109
- (match-var :bar))
2110
- "in [^foo, bar,]"
2111
- ~ begin ~ end
2112
- ~~~~~~~~~~~~ expression
2113
- ~~~
2114
-
2115
- #### Implicit
2116
-
2117
- Format:
2118
-
2119
- ~~~
2120
- (array-pattern
2121
- (pin
2122
- (lvar :foo))
2123
- (match-var :bar))
2124
- "in ^foo, bar"
2125
- ~~~~~~~~~ expression
2126
- ~~~
2127
-
2128
- #### Implicit with tail
2129
-
2130
- Format:
2131
-
2132
- Adding a trailing comma in the end works as `, *`,
2133
- so a single item match with comma gets interpreted as an array.
2134
-
2135
- ~~~
2136
- (array-pattern-with-tail
2137
- (match-var :foo))
2138
- "in foo,"
2139
- ~~~~ expression
2140
- ~~~
2141
-
2142
- ### Matching using hash pattern
2143
-
2144
- #### Explicit
2145
-
2146
- Format:
2147
-
2148
- ~~~
2149
- (hash-pattern
2150
- (pair
2151
- (sym :a)
2152
- (int 10)))
2153
- "in { a: 10 }"
2154
- ~ begin ~ end
2155
- ~~~~~~~~~ expression
2156
- ~~~
2157
-
2158
- #### Implicit
2159
-
2160
- Format:
2161
-
2162
- ~~~
2163
- (hash-pattern
2164
- (pair
2165
- (sym :a)
2166
- (int 10)))
2167
- "in a: 10"
2168
- ~~~~~ expression
2169
- ~~~
2170
-
2171
- #### Assignment using hash pattern
2172
-
2173
- Format:
2174
-
2175
- ~~~
2176
- (hash-pattern
2177
- (match-var :a))
2178
- "in a:"
2179
- ~ name (match-var)
2180
- ~~ expression (match-var)
2181
- ~~~
2182
-
2183
- #### Nil hash pattern
2184
-
2185
- Format:
2186
- ~~~
2187
- (hash-pattern
2188
- (match-nil-pattern))
2189
- "in **nil"
2190
- ~~~~~ expression (match-nil-pattern)
2191
- ~~~ name (match-nil-pattern)
2192
- ~~~
2193
-
2194
- ### Matching using find pattern
2195
-
2196
- Format:
2197
-
2198
- ~~~
2199
- (find-pattern
2200
- (match-rest
2201
- (match-var :a))
2202
- (int 42)
2203
- (match-rest))
2204
- "in [*, 42, *]"
2205
- ~ begin
2206
- ~ end
2207
- ~~~~~~~~~~ expression
2208
- ~~~
2209
-
2210
- Note that it can be used as a top-level pattern only when used in a `case` statement. In that case `begin` and `end` are empty.
2211
-
2212
- ### Matching using const pattern
2213
-
2214
- #### With array pattern
2215
-
2216
- Format:
2217
-
2218
- ~~~
2219
- (const-pattern
2220
- (const nil :X)
2221
- (array-pattern
2222
- (pin
2223
- (lvar :foo))
2224
- (match-var :bar)))
2225
- "in X[^foo bar]"
2226
- ~ begin (const-pattern)
2227
- ~ end (const-pattern)
2228
- ~~~~~~~~~~~~ expression (const-pattern)
2229
- ~ name (const-pattern.const)
2230
- ~ expression (const-pattern.const)
2231
- ~~~
2232
-
2233
- #### With hash pattern
2234
-
2235
- Format:
2236
-
2237
- ~~~
2238
- (const-pattern
2239
- (const nil :X)
2240
- (hash-pattern
2241
- (match-var :foo)
2242
- (match-var :bar)))
2243
- "in X[foo:, bar:]"
2244
- ~ begin (const-pattern)
2245
- ~ end (const-pattern)
2246
- ~~~~~~~~~~~~~ expression (const-pattern)
2247
- ~ name (const-pattern.const)
2248
- ~ expression (const-pattern.const)
2249
- ~~~
2250
-
2251
- #### With array pattern without elements
2252
-
2253
- Format:
2254
-
2255
- ~~~
2256
- (const-pattern
2257
- (const nil :X)
2258
- (array-pattern))
2259
- "in X[]"
2260
- ~ begin (const-pattern)
2261
- ~ end (const-pattern)
2262
- ~~~ expression (const-pattern)
2263
- ~ name (const-pattern.const)
2264
- ~ expression (const-pattern.const)
2265
- ~~ expression (const-pattern.array_pattern)
2266
- ~~~
2267
-
2268
- #### With find pattern
2269
-
2270
- Format:
2271
-
2272
- ~~~
2273
- (const-pattern
2274
- (const nil :X)
2275
- (find-pattern
2276
- (match-rest)
2277
- (int 42)
2278
- (match-rest)))
2279
- "in X[*, 42, *]"
2280
- ~ begin
2281
- ~ end
2282
- ~~~~~~~~~~~ expression
2283
- ~~~