parser 2.7.1.5 → 3.0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parser/all.rb +1 -0
  3. data/lib/parser/ast/processor.rb +3 -0
  4. data/lib/parser/base.rb +1 -0
  5. data/lib/parser/builders/default.rb +126 -13
  6. data/lib/parser/context.rb +4 -0
  7. data/lib/parser/current.rb +16 -7
  8. data/lib/parser/current_arg_stack.rb +5 -2
  9. data/lib/parser/lexer.rb +872 -802
  10. data/lib/parser/max_numparam_stack.rb +12 -4
  11. data/lib/parser/messages.rb +1 -0
  12. data/lib/parser/meta.rb +4 -3
  13. data/lib/parser/ruby18.rb +6 -2
  14. data/lib/parser/ruby27.rb +3763 -3704
  15. data/lib/parser/ruby28.rb +8047 -0
  16. data/lib/parser/ruby30.rb +3657 -3649
  17. data/lib/parser/ruby31.rb +8226 -0
  18. data/lib/parser/runner.rb +6 -1
  19. data/lib/parser/source/buffer.rb +50 -27
  20. data/lib/parser/source/comment.rb +13 -0
  21. data/lib/parser/source/comment/associator.rb +17 -4
  22. data/lib/parser/source/tree_rewriter.rb +27 -0
  23. data/lib/parser/static_environment.rb +4 -0
  24. data/lib/parser/variables_stack.rb +4 -0
  25. data/lib/parser/version.rb +1 -1
  26. data/parser.gemspec +1 -20
  27. metadata +8 -95
  28. data/.travis.yml +0 -41
  29. data/.yardopts +0 -21
  30. data/CHANGELOG.md +0 -1137
  31. data/CONTRIBUTING.md +0 -17
  32. data/Gemfile +0 -10
  33. data/README.md +0 -309
  34. data/Rakefile +0 -167
  35. data/ci/run_rubocop_specs +0 -14
  36. data/doc/AST_FORMAT.md +0 -2284
  37. data/doc/CUSTOMIZATION.md +0 -37
  38. data/doc/INTERNALS.md +0 -21
  39. data/doc/css/.gitkeep +0 -0
  40. data/doc/css/common.css +0 -68
  41. data/lib/parser/lexer.rl +0 -2550
  42. data/lib/parser/macruby.y +0 -2208
  43. data/lib/parser/ruby18.y +0 -1936
  44. data/lib/parser/ruby19.y +0 -2185
  45. data/lib/parser/ruby20.y +0 -2363
  46. data/lib/parser/ruby21.y +0 -2364
  47. data/lib/parser/ruby22.y +0 -2371
  48. data/lib/parser/ruby23.y +0 -2377
  49. data/lib/parser/ruby24.y +0 -2415
  50. data/lib/parser/ruby25.y +0 -2412
  51. data/lib/parser/ruby26.y +0 -2420
  52. data/lib/parser/ruby27.y +0 -2949
  53. data/lib/parser/ruby30.y +0 -3048
  54. data/lib/parser/rubymotion.y +0 -2192
  55. data/test/bug_163/fixtures/input.rb +0 -5
  56. data/test/bug_163/fixtures/output.rb +0 -5
  57. data/test/bug_163/rewriter.rb +0 -20
  58. data/test/helper.rb +0 -103
  59. data/test/parse_helper.rb +0 -328
  60. data/test/racc_coverage_helper.rb +0 -133
  61. data/test/test_ast_processor.rb +0 -32
  62. data/test/test_base.rb +0 -31
  63. data/test/test_current.rb +0 -31
  64. data/test/test_diagnostic.rb +0 -95
  65. data/test/test_diagnostic_engine.rb +0 -59
  66. data/test/test_encoding.rb +0 -99
  67. data/test/test_lexer.rb +0 -3617
  68. data/test/test_lexer_stack_state.rb +0 -78
  69. data/test/test_meta.rb +0 -12
  70. data/test/test_parse_helper.rb +0 -83
  71. data/test/test_parser.rb +0 -9986
  72. data/test/test_runner_parse.rb +0 -56
  73. data/test/test_runner_rewrite.rb +0 -47
  74. data/test/test_source_buffer.rb +0 -165
  75. data/test/test_source_comment.rb +0 -36
  76. data/test/test_source_comment_associator.rb +0 -399
  77. data/test/test_source_map.rb +0 -14
  78. data/test/test_source_range.rb +0 -192
  79. data/test/test_source_rewriter.rb +0 -541
  80. data/test/test_source_rewriter_action.rb +0 -46
  81. data/test/test_source_tree_rewriter.rb +0 -361
  82. data/test/test_static_environment.rb +0 -45
  83. data/test/using_tree_rewriter/fixtures/input.rb +0 -3
  84. data/test/using_tree_rewriter/fixtures/output.rb +0 -3
  85. 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,2284 +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
- (lvasgn :a (int 1))
739
- "1 => a"
740
- ~~~~~~ expression
741
- ~ name
742
- ~~ operator
743
- ~~~
744
-
745
- #### Multiple right-hand assignment
746
-
747
- Format:
748
-
749
- ~~~
750
- (masgn (mlhs (lvasgn :a) (lvasgn :b)) (send (int 13) :divmod (int 5)))
751
- "13.divmod(5) => a,b"
752
- ~~~~~~~~~~~~~~~~~~~ expression
753
- ^^ operator
754
- ~~~
755
-
756
- ## Class and module definition
757
-
758
- ### Module
759
-
760
- Format:
761
-
762
- ~~~
763
- (module (const nil :Foo) (nil))
764
- "module Foo; end"
765
- ~~~~~~ keyword
766
- ~~~ end
767
- ~~~
768
-
769
- ### Class
770
-
771
- Format:
772
-
773
- ~~~
774
- (class (const nil :Foo) (const nil :Bar) (nil))
775
- "class Foo < Bar; end"
776
- ~~~~~ keyword ~~~ end
777
- ~ operator
778
- ~~~~~~~~~~~~~~~~~~~~ expression
779
-
780
- (class (const nil :Foo) nil (nil))
781
- "class Foo; end"
782
- ~~~~~ keyword
783
- ~~~ end
784
- ~~~~~~~~~~~~~~ expression
785
- ~~~
786
-
787
- ### Singleton class
788
-
789
- Format:
790
-
791
- ~~~
792
- (sclass (lvar :a) (nil))
793
- "class << a; end"
794
- ~~~~~ keyword
795
- ~~ operator
796
- ~~~ end
797
- ~~~~~~~~~~~~~~~ expression
798
- ~~~
799
-
800
- ## Method (un)definition
801
-
802
- ### Instance methods
803
-
804
- Format:
805
-
806
- ~~~
807
- (def :foo (args) nil)
808
- "def foo; end"
809
- ~~~ keyword
810
- ~~~ name
811
- ~~~ end
812
- ~~~~~~~~~~~~ expression
813
- ~~~
814
-
815
- ### Singleton methods
816
-
817
- Format:
818
-
819
- ~~~
820
- (defs (self) :foo (args) nil)
821
- "def self.foo; end"
822
- ~~~ keyword
823
- ~~~ name
824
- ~~~ end
825
- ~~~~~~~~~~~~~~~~~ expression
826
- ~~~
827
-
828
- ### "Endless" method
829
-
830
- Format:
831
-
832
- ~~~
833
- (def :foo (args) (int 42))
834
- "def foo() = 42"
835
- ~~~ keyword
836
- ~~~ name
837
- ^ assignment
838
- ~~~~~~~~~~~~~~ expression
839
- ~~~
840
-
841
-
842
- ### "Endless" singleton method
843
-
844
- Format:
845
-
846
- ~~~
847
- (defs (self) :foo (args) (int 42))
848
- "def self.foo() = 42"
849
- ~~~ keyword
850
- ~~~ name
851
- ^ assignment
852
- ~~~~~~~~~~~~~~~~~~~ expression
853
- ~~~
854
-
855
- ### Undefinition
856
-
857
- Format:
858
-
859
- ~~~
860
- (undef (sym :foo) (sym :bar) (dsym (str "foo") (int 1)))
861
- "undef foo :bar :"foo#{1}""
862
- ~~~~~ keyword
863
- ~~~~~~~~~~~~~~~~~~~~~~~~~ expression
864
- ~~~
865
-
866
- ## Aliasing
867
-
868
- ### Method aliasing
869
-
870
- Format:
871
-
872
- ~~~
873
- (alias (sym :foo) (dsym (str "foo") (int 1)))
874
- "alias foo :"foo#{1}""
875
- ~~~~~ keyword
876
- ~~~~~~~~~~~~~~~~~~~~ expression
877
- ~~~
878
-
879
- ### Global variable aliasing
880
-
881
- Format:
882
-
883
- ~~~
884
- (alias (gvar :$foo) (gvar :$bar))
885
- "alias $foo $bar"
886
- ~~~~~ keyword
887
- ~~~~~~~~~~~~~~~ expression
888
-
889
- (alias (gvar :$foo) (back-ref :$&))
890
- "alias $foo $&"
891
- ~~~~~ keyword
892
- ~~~~~~~~~~~~~~~ expression
893
- ~~~
894
-
895
- ## Formal arguments
896
-
897
- Format:
898
-
899
- ~~~
900
- (args (arg :foo))
901
- "(foo)"
902
- ~~~~~ expression
903
- ~~~
904
-
905
- ### Required argument
906
-
907
- Format:
908
-
909
- ~~~
910
- (arg :foo)
911
- "foo"
912
- ~~~ expression
913
- ~~~ name
914
- ~~~
915
-
916
- ### Optional argument
917
-
918
- Format:
919
-
920
- ~~~
921
- (optarg :foo (int 1))
922
- "foo = 1"
923
- ~~~~~~~ expression
924
- ^ operator
925
- ~~~ name
926
- ~~~
927
-
928
- ### Named splat argument
929
-
930
- Format:
931
-
932
- ~~~
933
- (restarg :foo)
934
- "*foo"
935
- ~~~~ expression
936
- ~~~ name
937
- ~~~
938
-
939
- Begin of the `expression` points to `*`.
940
-
941
- ### Unnamed splat argument
942
-
943
- Format:
944
-
945
- ~~~
946
- (restarg)
947
- "*"
948
- ^ expression
949
- ~~~
950
-
951
- ### Block argument
952
-
953
- Format:
954
-
955
- ~~~
956
- (blockarg :foo)
957
- "&foo"
958
- ~~~ name
959
- ~~~~ expression
960
- ~~~
961
-
962
- Begin of the `expression` points to `&`.
963
-
964
- ### Auto-expanding proc argument (1.9)
965
-
966
- In Ruby 1.9 and later, when a proc-like closure (i.e. a closure
967
- created by capturing a block or with the `proc` method, but not
968
- with the `->{}` syntax or the `lambda` method) has exactly one
969
- argument, and it is called with more than one argument, the behavior
970
- is as if the array of all arguments was instead passed as the sole
971
- argument. This behavior can be prevented by adding a comma after
972
- the sole argument (e.g. `|foo,|`).
973
-
974
- Format:
975
-
976
- ~~~
977
- (procarg0 (arg :foo))
978
- "|foo|"
979
- ~~~ expression
980
-
981
- (procarg0 (arg :foo) (arg :bar))
982
- "|(foo, bar)|"
983
- ~ begin
984
- ~ end
985
- ~~~~~~~~~~ expression
986
- ~~~
987
-
988
- ### Expression arguments
989
-
990
- Ruby 1.8 allows to use arbitrary expressions as block arguments,
991
- such as `@var` or `foo.bar`. Such expressions should be treated as
992
- if they were on the lhs of a multiple assignment.
993
-
994
- Format:
995
-
996
- ~~~
997
- (args (arg_expr (ivasgn :@bar)))
998
- "|@bar|"
999
-
1000
- (args (arg_expr (send (send nil :foo) :a=)))
1001
- "|foo.a|"
1002
-
1003
- (args (restarg_expr (ivasgn :@bar)))
1004
- "|*@bar|"
1005
-
1006
- (args (blockarg_expr (ivasgn :@bar)))
1007
- "|&@bar|"
1008
- ~~~
1009
-
1010
- ### Block shadow arguments
1011
-
1012
- Format:
1013
-
1014
- ~~~
1015
- (args (shadowarg :foo) (shadowarg :bar))
1016
- "|; foo, bar|"
1017
- ~~~
1018
-
1019
- ### Decomposition
1020
-
1021
- Format:
1022
-
1023
- ~~~
1024
- (def :f (args (arg :a) (mlhs (arg :foo) (restarg :bar))))
1025
- "def f(a, (foo, *bar)); end"
1026
- ^ begin ^ end
1027
- ~~~~~~~~~~~ expression
1028
- ~~~
1029
-
1030
- ### Required keyword argument
1031
-
1032
- Format:
1033
-
1034
- ~~~
1035
- (kwarg :foo)
1036
- "foo:"
1037
- ~~~~ expression
1038
- ~~~~ name
1039
- ~~~
1040
-
1041
- ### Optional keyword argument
1042
-
1043
- Format:
1044
-
1045
- ~~~
1046
- (kwoptarg :foo (int 1))
1047
- "foo: 1"
1048
- ~~~~~~ expression
1049
- ~~~~ name
1050
- ~~~
1051
-
1052
- ### Named keyword splat argument
1053
-
1054
- Format:
1055
-
1056
- ~~~
1057
- (kwrestarg :foo)
1058
- "**foo"
1059
- ~~~~~ expression
1060
- ~~~ name
1061
- ~~~
1062
-
1063
- ### Unnamed keyword splat argument
1064
-
1065
- Format:
1066
-
1067
- ~~~
1068
- (kwrestarg)
1069
- "**"
1070
- ~~ expression
1071
- ~~~
1072
-
1073
- ### Keyword nil argument
1074
-
1075
- Format:
1076
-
1077
- ~~~
1078
- (kwnilarg)
1079
- "**nil"
1080
- ~~~ name
1081
- ~~~~~ expression
1082
- ~~~
1083
-
1084
- ### Objective-C arguments
1085
-
1086
- MacRuby includes a few more syntactic "arguments" whose name becomes
1087
- the part of the Objective-C method name, despite looking like Ruby 2.0
1088
- keyword arguments, and are thus treated differently.
1089
-
1090
- #### Objective-C label-like keyword argument
1091
-
1092
- Format:
1093
-
1094
- ~~~
1095
- (objc-kwarg :a :b)
1096
- "a: b"
1097
- ~ keyword
1098
- ~ operator
1099
- ~ argument
1100
- ~~~~ expression
1101
- ~~~
1102
-
1103
- #### Objective-C pair-like keyword argument
1104
-
1105
- Format:
1106
-
1107
- ~~~
1108
- (objc-kwarg :a :b)
1109
- "a => b"
1110
- ~ keyword
1111
- ~~ operator
1112
- ~ argument
1113
- ~~~~~~ expression
1114
- ~~~
1115
-
1116
- #### Objective-C keyword splat argument
1117
-
1118
- Format:
1119
-
1120
- ~~~
1121
- (objc-restarg (objc-kwarg :foo))
1122
- "(*a: b)"
1123
- ~ objc-kwarg.keyword
1124
- ~ objc-kwarg.operator
1125
- ~ objc-kwarg.argument
1126
- ~ operator
1127
- ~~~~~ expression
1128
- ~~~
1129
-
1130
- Note that these splat arguments will only be parsed inside parentheses,
1131
- e.g. in the following code:
1132
-
1133
- ~~~
1134
- def f((*a: b)); end
1135
- ~~~
1136
-
1137
- However, the following code results in a parse error:
1138
-
1139
- ~~~
1140
- def f(*a: b); end
1141
- ~~~
1142
-
1143
- ## Numbered parameters
1144
-
1145
- ### Block with numbered parameters
1146
-
1147
- Ruby 2.7 introduced a feature called "numbered parameters".
1148
- Numbered and ordinal parameters are mutually exclusive, so if the block
1149
- has only numbered parameters it also has a different AST node.
1150
-
1151
- Note that the second child represents a total number of numbered parameters.
1152
-
1153
- Format:
1154
-
1155
- ~~~
1156
- s(:numblock,
1157
- s(:send, nil, :proc), 3,
1158
- s(:send,
1159
- s(:lvar, :_1), :+,
1160
- s(:lvar, :_3)))
1161
- "proc { _1 + _3 }"
1162
- ~ begin ~ end
1163
- ~~~~~~~~~~~~~~~~ expression
1164
- ~~~
1165
-
1166
- ## Forward arguments
1167
-
1168
- ### Method definition accepting only forwarding arguments
1169
-
1170
- Ruby 2.7 introduced a feature called "arguments forwarding".
1171
- When a method takes any arguments for forwarding them in the future
1172
- the whole `args` node gets replaced with `forward-args` node.
1173
-
1174
- Format if `emit_forward_arg` compatibility flag is disabled:
1175
-
1176
- ~~~
1177
- (def :foo
1178
- (forward-args) nil)
1179
- "def foo(...); end"
1180
- ~ end
1181
- ~ begin
1182
- ~~~~~ expression
1183
- ~~~
1184
-
1185
- However, Ruby 3.0 added support for leading arguments before `...`, and so
1186
- it can't be used as a replacement of the `(args)` node anymore. To solve it
1187
- `emit_forward_arg` should be enabled.
1188
-
1189
- Format if `emit_forward_arg` compatibility flag is enabled:
1190
-
1191
- ~~~
1192
- (def :foo
1193
- (args
1194
- (forward-arg)) nil)
1195
- "def foo(...); end"
1196
- ~ begin (args)
1197
- ~ end (args)
1198
- ~~~~~ expression (args)
1199
- ~~~ expression (forward_arg)
1200
- ~~~
1201
-
1202
- Note that the node is called `forward_arg` when emitted separately.
1203
-
1204
- ### Method call taking arguments of the currently forwarding method
1205
-
1206
- Format:
1207
-
1208
- ~~~
1209
- (send nil :foo
1210
- (forwarded-args))
1211
- "foo(...)"
1212
- ~~~ expression
1213
- ~~~
1214
-
1215
- ## Send
1216
-
1217
- ### To self
1218
-
1219
- Format:
1220
-
1221
- ~~~
1222
- (send nil :foo (lvar :bar))
1223
- "foo(bar)"
1224
- ~~~ selector
1225
- ^ begin
1226
- ^ end
1227
- ~~~~~~~~ expression
1228
- ~~~
1229
-
1230
- ### To receiver
1231
-
1232
- Format:
1233
-
1234
- ~~~
1235
- (send (lvar :foo) :bar (int 1))
1236
- "foo.bar(1)"
1237
- ^ dot
1238
- ~~~ selector
1239
- ^ begin
1240
- ^ end
1241
- ~~~~~~~~~~ expression
1242
-
1243
- (send (lvar :foo) :+ (int 1))
1244
- "foo + 1"
1245
- ^ selector
1246
- ~~~~~~~ expression
1247
-
1248
- (send (lvar :foo) :-@)
1249
- "-foo"
1250
- ^ selector
1251
- ~~~~ expression
1252
-
1253
- (send (lvar :foo) :a= (int 1))
1254
- "foo.a = 1"
1255
- ~ selector
1256
- ^ operator
1257
- ~~~~~~~~~ expression
1258
- ~~~
1259
-
1260
- ### To superclass
1261
-
1262
- Format of super with arguments:
1263
-
1264
- ~~~
1265
- (super (lvar :a))
1266
- "super a"
1267
- ~~~~~ keyword
1268
- ~~~~~~~ expression
1269
-
1270
- (super)
1271
- "super()"
1272
- ^ begin
1273
- ^ end
1274
- ~~~~~ keyword
1275
- ~~~~~~~ expression
1276
- ~~~
1277
-
1278
- Format of super without arguments (**z**ero-arity):
1279
-
1280
- ~~~
1281
- (zsuper)
1282
- "super"
1283
- ~~~~~ keyword
1284
- ~~~~~ expression
1285
- ~~~
1286
-
1287
- ### To block argument
1288
-
1289
- Format:
1290
-
1291
- ~~~
1292
- (yield (lvar :foo))
1293
- "yield(foo)"
1294
- ~~~~~ keyword
1295
- ^ begin
1296
- ^ end
1297
- ~~~~~~~~~~ expression
1298
- ~~~
1299
-
1300
- ### Indexing
1301
-
1302
- Format:
1303
-
1304
- ~~~
1305
- (index (lvar :foo) (int 1))
1306
- "foo[1]"
1307
- ^ begin
1308
- ^ end
1309
- ~~~~~~ expression
1310
-
1311
- (indexasgn (lvar :bar) (int 1) (int 2) (lvar :baz))
1312
- "bar[1, 2] = baz"
1313
- ^ begin
1314
- ^ end
1315
- ^ operator
1316
- ~~~~~~~~~~~~~~~ expression
1317
-
1318
- ~~~
1319
-
1320
- ### Passing a literal block
1321
-
1322
- ~~~
1323
- (block (send nil :foo) (args (arg :bar)) (begin ...))
1324
- "foo do |bar|; end"
1325
- ~~ begin
1326
- ~~~ end
1327
- ~~~~~~~~~~~~~ expression
1328
- ~~~
1329
-
1330
- ### Passing expression as block
1331
-
1332
- Used when passing expression as block `foo(&bar)`
1333
-
1334
- ~~~
1335
- (send nil :foo (int 1) (block-pass (lvar :foo)))
1336
- "foo(1, &foo)"
1337
- ^ operator
1338
- ~~~~ expression
1339
- ~~~
1340
-
1341
- ### "Stabby lambda"
1342
-
1343
- ~~~
1344
- (block (lambda) (args) nil)
1345
- "-> {}"
1346
- ~~ lambda.expression
1347
- ~~~
1348
-
1349
- ### "Safe navigation operator"
1350
-
1351
- ~~~
1352
- (csend (send nil :foo) :bar)
1353
- "foo&.bar"
1354
- ~~ dot
1355
- ~~~
1356
-
1357
- ### Objective-C variadic send
1358
-
1359
- MacRuby allows to pass a variadic amount of arguments via the last
1360
- keyword "argument". Semantically, these, together with the pair value
1361
- of the last pair in the hash implicitly passed as the last argument,
1362
- form an array, which replaces the pair value. Despite that, the node
1363
- is called `objc-varargs` to distinguish it from a literal array passed
1364
- as a value.
1365
-
1366
- ~~~
1367
- (send nil :foo (int 1) (hash (pair (sym :bar) (objc-varargs (int 1) (int 2) (nil)))))
1368
- "foo(1, bar: 2, 3, nil)"
1369
- ~~~~~~~~~ expression (array)
1370
- ~~~
1371
-
1372
- ## Control flow
1373
-
1374
- ### Logical operators
1375
-
1376
- #### Binary (and or && ||)
1377
-
1378
- Format:
1379
-
1380
- ~~~
1381
- (and (lvar :foo) (lvar :bar))
1382
- "foo and bar"
1383
- ~~~ operator
1384
- ~~~~~~~~~~~ expression
1385
- ~~~
1386
-
1387
- ~~~
1388
- (or (lvar :foo) (lvar :bar))
1389
- "foo or bar"
1390
- ~~ operator
1391
- ~~~~~~~~~~ expression
1392
- ~~~
1393
-
1394
- #### Unary (! not) (1.8)
1395
-
1396
- Format:
1397
-
1398
- ~~~
1399
- (not (lvar :foo))
1400
- "!foo"
1401
- ^ operator
1402
- "not foo"
1403
- ~~~ operator
1404
- ~~~
1405
-
1406
- ### Branching
1407
-
1408
- #### Without else
1409
-
1410
- Format:
1411
-
1412
- ~~~
1413
- (if (lvar :cond) (lvar :iftrue) nil)
1414
- "if cond then iftrue; end"
1415
- ~~ keyword
1416
- ~~~~ begin
1417
- ~~~ end
1418
- ~~~~~~~~~~~~~~~~~~~~~~~~ expression
1419
-
1420
- "if cond; iftrue; end"
1421
- ~~ keyword
1422
- ~~~ end
1423
- ~~~~~~~~~~~~~~~~~~~~ expression
1424
-
1425
- "iftrue if cond"
1426
- ~~ keyword
1427
- ~~~~~~~~~~~~~~ expression
1428
-
1429
- (if (lvar :cond) nil (lvar :iftrue))
1430
- "unless cond then iftrue; end"
1431
- ~~~~~~ keyword
1432
- ~~~~ begin
1433
- ~~~ end
1434
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1435
-
1436
- "unless cond; iftrue; end"
1437
- ~~~~~~ keyword
1438
- ~~~ end
1439
- ~~~~~~~~~~~~~~~~~~~~~~~~ expression
1440
-
1441
- "iftrue unless cond"
1442
- ~~~~~~ keyword
1443
- ~~~~~~~~~~~~~~~~~~ expression
1444
- ~~~
1445
-
1446
- #### With else
1447
-
1448
- Format:
1449
-
1450
- ~~~
1451
- (if (lvar :cond) (lvar :iftrue) (lvar :iffalse))
1452
- "if cond then iftrue; else; iffalse; end"
1453
- ~~ keyword
1454
- ~~~~ begin
1455
- ~~~~ else
1456
- ~~~ end
1457
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1458
-
1459
- "if cond; iftrue; else; iffalse; end"
1460
- ~~ keyword
1461
- ~~~~ else
1462
- ~~~ end
1463
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1464
-
1465
- (if (lvar :cond) (lvar :iffalse) (lvar :iftrue))
1466
- "unless cond then iftrue; else; iffalse; end"
1467
- ~~~~~~ keyword
1468
- ~~~~ begin
1469
- ~~~~ else
1470
- ~~~ end
1471
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1472
-
1473
- "unless cond; iftrue; else; iffalse; end"
1474
- ~~~~~~ keyword
1475
- ~~~~ else
1476
- ~~~ end
1477
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1478
- ~~~
1479
-
1480
- #### With elsif
1481
-
1482
- Format:
1483
-
1484
- ~~~
1485
- (if (lvar :cond1) (int 1) (if (lvar :cond2 (int 2) (int 3))))
1486
- "if cond1; 1; elsif cond2; 2; else 3; end"
1487
- ~~ keyword (left)
1488
- ~~~~~ else (left)
1489
- ~~~ end (left)
1490
- ~~~~~ keyword (right)
1491
- ~~~~ else (right)
1492
- ~~~ end (right)
1493
- ~~~
1494
-
1495
- #### Ternary
1496
-
1497
- Format:
1498
-
1499
- ~~~
1500
- (if (lvar :cond) (lvar :iftrue) (lvar :iffalse))
1501
- "cond ? iftrue : iffalse"
1502
- ^ question
1503
- ^ colon
1504
- ~~~~~~~~~~~~~~~~~~~~~~~ expression
1505
- ~~~
1506
-
1507
- ### Case matching
1508
-
1509
- #### When clause
1510
-
1511
- Format:
1512
-
1513
- ~~~
1514
- (when (regexp "foo" (regopt)) (begin (lvar :bar)))
1515
- "when /foo/ then bar"
1516
- ~~~~ keyword
1517
- ~~~~ begin
1518
- ~~~~~~~~~~~~~~~~~~~ expression
1519
-
1520
- (when (int 1) (int 2) (send nil :meth))
1521
- "when 1, 2; meth"
1522
-
1523
- (when (int 1) (splat (lvar :foo)) (send nil :meth))
1524
- "when 1, *foo; meth"
1525
-
1526
- (when (splat (lvar :foo)) (send nil :meth))
1527
- "when *foo; meth"
1528
- ~~~
1529
-
1530
- #### Case-expression clause
1531
-
1532
- ##### Without else
1533
-
1534
- Format:
1535
-
1536
- ~~~
1537
- (case (lvar :foo) (when (str "bar") (lvar :bar)) nil)
1538
- "case foo; when "bar"; bar; end"
1539
- ~~~~ keyword ~~~ end
1540
- ~~~
1541
-
1542
- ##### With else
1543
-
1544
- Format:
1545
-
1546
- ~~~
1547
- (case (lvar :foo) (when (str "bar") (lvar :bar)) (lvar :baz))
1548
- "case foo; when "bar"; bar; else baz; end"
1549
- ~~~~ keyword ~~~~ else ~~~ end
1550
- ~~~
1551
-
1552
- #### Case-conditions clause
1553
-
1554
- ##### Without else
1555
-
1556
- Format:
1557
-
1558
- ~~~
1559
- (case nil (when (lvar :bar) (lvar :bar)) nil)
1560
- "case; when bar; bar; end"
1561
- ~~~~ keyword ~~~ end
1562
- ~~~
1563
-
1564
- ##### With else
1565
-
1566
- Format:
1567
-
1568
- ~~~
1569
- (case nil (when (lvar :bar) (lvar :bar)) (lvar :baz))
1570
- "case; when bar; bar; else baz; end"
1571
- ~~~~ keyword ~~~~ else ~~~ end
1572
-
1573
- (case nil (lvar :baz))
1574
- "case; else baz; end"
1575
- ~~~~ keyword
1576
- ~~~~ else
1577
- ~~~ end
1578
- ~~~
1579
-
1580
- ### Looping
1581
-
1582
- #### With precondition
1583
-
1584
- Format:
1585
-
1586
- ~~~
1587
- (while (lvar :condition) (send nil :foo))
1588
- "while condition do foo; end"
1589
- ~~~~~ keyword
1590
- ~~ begin
1591
- ~~~ end
1592
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1593
-
1594
- "while condition; foo; end"
1595
- ~~~~~ keyword
1596
- ~~~ end
1597
- ~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1598
-
1599
- "foo while condition"
1600
- ~~~~~ keyword
1601
- ~~~~~~~~~~~~~~~~~~~ expression
1602
-
1603
- (until (lvar :condition) (send nil :foo))
1604
- "until condition do foo; end"
1605
- ~~~~~ keyword
1606
- ~~ begin
1607
- ~~~ end
1608
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1609
-
1610
- (until (lvar :condition) (send nil :foo))
1611
- "until condition; foo; end"
1612
- ~~~~~ keyword
1613
- ~~~ end
1614
- ~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1615
-
1616
- "foo until condition"
1617
- ~~~~~ keyword
1618
- ~~~~~~~~~~~~~~~~~~~ expression
1619
- ~~~
1620
-
1621
- #### With postcondition
1622
-
1623
- Format:
1624
-
1625
- ~~~
1626
- (while-post (lvar :condition) (kwbegin (send nil :foo)))
1627
- "begin; foo; end while condition"
1628
- ~~~~~ begin (begin)
1629
- ~~~ end (begin)
1630
- ~~~~~ keyword (while-post)
1631
-
1632
- (until-post (lvar :condition) (kwbegin (send nil :foo)))
1633
- "begin; foo; end until condition"
1634
- ~~~~~ begin (begin)
1635
- ~~~ end (begin)
1636
- ~~~~~ keyword (until-post)
1637
- ~~~
1638
-
1639
- #### For-in
1640
-
1641
- Format:
1642
-
1643
- ~~~
1644
- (for (lvasgn :a) (lvar :array) (send nil :p (lvar :a)))
1645
- "for a in array do p a; end"
1646
- ~~~ keyword
1647
- ~~ in
1648
- ~~ begin
1649
- ~~~ end
1650
-
1651
- "for a in array; p a; end"
1652
- ~~~ keyword
1653
- ~~ in
1654
- ~~~ end
1655
-
1656
- (for
1657
- (mlhs (lvasgn :a) (lvasgn :b)) (lvar :array)
1658
- (send nil :p (lvar :a) (lvar :b)))
1659
- "for a, b in array; p a, b; end"
1660
- ~~~
1661
-
1662
- #### Break
1663
-
1664
- Format:
1665
-
1666
- ~~~
1667
- (break (int 1))
1668
- "break 1"
1669
- ~~~~~ keyword
1670
- ~~~~~~~ expression
1671
- ~~~
1672
-
1673
- #### Next
1674
-
1675
- Format:
1676
-
1677
- ~~~
1678
- (next (int 1))
1679
- "next 1"
1680
- ~~~~ keyword
1681
- ~~~~~~ expression
1682
- ~~~
1683
-
1684
- #### Redo
1685
-
1686
- Format:
1687
-
1688
- ~~~
1689
- (redo)
1690
- "redo"
1691
- ~~~~ keyword
1692
- ~~~~ expression
1693
- ~~~
1694
-
1695
- ### Return
1696
-
1697
- Format:
1698
-
1699
- ~~~
1700
- (return (lvar :foo))
1701
- "return(foo)"
1702
- ~~~~~~ keyword
1703
- ~~~~~~~~~~~ expression
1704
- ~~~
1705
-
1706
- ### Exception handling
1707
-
1708
- #### Rescue body
1709
-
1710
- Format:
1711
-
1712
- ~~~
1713
- (resbody (array (const nil :Exception) (const nil :A)) (lvasgn :bar) (int 1))
1714
- "rescue Exception, A => bar; 1"
1715
- ~~~~~~ keyword ~~ assoc
1716
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1717
-
1718
- "rescue Exception, A => bar then 1"
1719
- ~~~~~~ keyword ~~ assoc
1720
- ~~~~ begin
1721
-
1722
- (resbody (array (const nil :Exception)) (ivasgn :bar) (int 1))
1723
- "rescue Exception => @bar; 1"
1724
- ~~~~~~ keyword ~~ assoc
1725
-
1726
- (resbody nil (lvasgn :bar) (int 1))
1727
- "rescue => bar; 1"
1728
- ~~~~~~ keyword
1729
- ~~ assoc
1730
-
1731
- (resbody nil nil (int 1))
1732
- "rescue; 1"
1733
- ~~~~~~ keyword
1734
- ~~~
1735
-
1736
- #### Rescue statement
1737
-
1738
- ##### Without else
1739
-
1740
- Format:
1741
-
1742
- ~~~
1743
- (begin
1744
- (rescue (send nil :foo) (resbody ...) (resbody ...) nil))
1745
- "begin; foo; rescue Exception; rescue; end"
1746
- ~~~~~ begin ~~~ end
1747
- ~~~~~~~~~~~~~~~~~ expression (rescue.resbody/1)
1748
- ~~~~~~~ expression (rescue.resbody/2)
1749
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (rescue)
1750
- ~~~
1751
-
1752
- ##### With else
1753
-
1754
- Format:
1755
-
1756
- ~~~
1757
- (begin
1758
- (rescue (send nil :foo) (resbody ...) (resbody ...) (true)))
1759
- "begin; foo; rescue Exception; rescue; else true end"
1760
- ~~~~~ begin ~~~~ else (rescue)
1761
- ~~~ end
1762
- ~~~
1763
-
1764
- #### Ensure statement
1765
-
1766
- Format:
1767
-
1768
- ~~~
1769
- (begin
1770
- (ensure (send nil :foo) (send nil :bar))
1771
- "begin; foo; ensure; bar; end"
1772
- ~~~~~ begin ~~~~~~ keyword (ensure)
1773
- ~~~ end
1774
- ~~~
1775
-
1776
- #### Rescue with ensure
1777
-
1778
- Format:
1779
-
1780
- ~~~
1781
- (begin
1782
- (ensure
1783
- (rescue (send nil :foo) (resbody ...) (int 1))
1784
- (send nil :bar))
1785
- "begin; foo; rescue; nil; else; 1; ensure; bar; end"
1786
- ~~~~~ begin
1787
- ~~~~ else (ensure.rescue)
1788
- ~~~~~~~~~~~~~~~~~~~~~ expression (rescue)
1789
- ~~~~~~ keyword (ensure)
1790
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (ensure)
1791
- ~~~ end
1792
- ~~~
1793
-
1794
- #### Retry
1795
-
1796
- Format:
1797
-
1798
- ~~~
1799
- (retry)
1800
- "retry"
1801
- ~~~~~ keyword
1802
- ~~~~~ expression
1803
- ~~~
1804
-
1805
- ### BEGIN and END
1806
-
1807
- Format:
1808
-
1809
- ~~~
1810
- (preexe (send nil :puts (str "foo")))
1811
- "BEGIN { puts "foo" }"
1812
- ~~~~~ keyword
1813
- ^ begin ^ end
1814
- ~~~~~~~~~~~~~~~~~~~~ expression
1815
-
1816
- (postexe (send nil :puts (str "bar")))
1817
- "END { puts "bar" }"
1818
- ~~~ keyword
1819
- ^ begin ^ end
1820
- ~~~~~~~~~~~~~~~~~~ expression
1821
- ~~~
1822
-
1823
- ## Miscellanea
1824
-
1825
- ### Flip-flops
1826
-
1827
- Format:
1828
-
1829
- ~~~
1830
- (iflipflop (lvar :a) (lvar :b))
1831
- "if a..b; end"
1832
- ~~ operator
1833
- ~~~~ expression
1834
-
1835
- (eflipflop (lvar :a) (lvar :b))
1836
- "if a...b; end"
1837
- ~~~ operator
1838
- ~~~~~ expression
1839
- ~~~
1840
-
1841
- ### Implicit matches
1842
-
1843
- Format:
1844
-
1845
- ~~~
1846
- (match-current-line (regexp (str "a") (regopt)))
1847
- "if /a/; end"
1848
- ~~~ expression
1849
- ~~~
1850
-
1851
- ### Local variable injecting matches
1852
-
1853
- Format:
1854
-
1855
- ~~~
1856
- (match-with-lvasgn (regexp (str "(?<match>bar)") (regopt)) (lvar :baz))
1857
- "/(?<match>bar)/ =~ baz"
1858
- ~~ selector
1859
- ~~~~~~~~~~~~~~~~~~~~~~ expression
1860
- ~~~
1861
-
1862
- ## Special constants
1863
-
1864
- ### File
1865
-
1866
- Format:
1867
-
1868
- ~~~
1869
- (__FILE__)
1870
- "__FILE__"
1871
- ~~~~~~~~ expression
1872
- ~~~
1873
-
1874
- ### Line
1875
-
1876
- Format:
1877
-
1878
- ~~~
1879
- (__LINE__)
1880
- "__LINE__"
1881
- ~~~~~~~~ expression
1882
- ~~~
1883
-
1884
- ### Encoding
1885
-
1886
- Format:
1887
-
1888
- ~~~
1889
- (__ENCODING__)
1890
- "__ENCODING__"
1891
- ~~~~~~~~~~~~ expression
1892
- ~~~
1893
-
1894
- ## Pattern matching
1895
-
1896
- ### Using `in` modifier
1897
-
1898
- Format:
1899
-
1900
- ~~~
1901
- (in-match
1902
- (int 1)
1903
- (match-var :a))
1904
- "1 in a"
1905
- ~~ operator
1906
- ~~~~~~ expression
1907
- ~~~
1908
-
1909
- ### Case with pattern matching
1910
-
1911
- #### Without else
1912
-
1913
- Format:
1914
-
1915
- ~~~
1916
- (case-match
1917
- (str "str")
1918
- (in-pattern
1919
- (match-var :foo)
1920
- (lvar :bar)) nil)
1921
- "case "str"; in foo; bar; end"
1922
- ~~~~ keyword ~~~ end
1923
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1924
- ~~~
1925
-
1926
- #### With else
1927
-
1928
- Format:
1929
-
1930
- ~~~
1931
- (case-match,
1932
- (str "str")
1933
- (in-pattern
1934
- (match-var :foo)
1935
- (lvar :bar))
1936
- (lvar :baz))
1937
- "case "str"; in foo; bar; else; baz; end"
1938
- ~~~~ keyword ~~~~ else ~~~ end
1939
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1940
- ~~~
1941
-
1942
- #### With empty else
1943
-
1944
- Empty `else` differs from the missing (or _implicit_) `else` for pattern matching, since
1945
- the latter one raises a `NoMatchingPattern` exception. Thus, we need a way to distinguish this
1946
- two cases in the resulting AST.
1947
-
1948
- Format:
1949
-
1950
- ~~~
1951
- (case-match,
1952
- (str "str")
1953
- (in-pattern
1954
- (match-var :foo)
1955
- (lvar :bar))
1956
- (empty-else))
1957
- "case "str"; in foo; bar; else; end"
1958
- ~~~~ keyword ~~~~ else
1959
- ~~~ end
1960
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1961
- ~~~
1962
-
1963
- ### In clause
1964
-
1965
- Format:
1966
-
1967
- ~~~
1968
- (in-pattern
1969
- (match-var :foo)
1970
- (lvar :bar))
1971
- "in foo then bar"
1972
- ~~ keyword
1973
- ~~~~ begin
1974
- ~~~~~~~~~~~~~~~ expression
1975
- ~~~
1976
-
1977
- ### If guard
1978
-
1979
- This guard runs after matching, so it's not an `if` modifier.
1980
-
1981
- Format:
1982
-
1983
- ~~~
1984
- (in-pattern
1985
- (match-var :foo)
1986
- (if-guard
1987
- (lvar :bar)) nil)
1988
- "in foo if bar"
1989
- ~~ keyword
1990
- ~~~~~~ expression
1991
- ~~~
1992
-
1993
- ### Unless guard
1994
-
1995
- This guard runs after matching, so it's not an `unless` modifier.
1996
-
1997
- Format:
1998
-
1999
- ~~~
2000
- (in-pattern
2001
- (match-var :foo)
2002
- (unless-guard
2003
- (lvar :bar)) nil)
2004
- "in foo unless bar"
2005
- ~~~~~~ keyword
2006
- ~~~~~~~~~~ expression
2007
- ~~~
2008
-
2009
- ### Match variable
2010
-
2011
- Format:
2012
-
2013
- ~~~
2014
- (match-var :foo)
2015
- "in foo"
2016
- ~~~ name
2017
- ~~~ expression
2018
- ~~~
2019
-
2020
- ### Match rest
2021
-
2022
- #### With name
2023
-
2024
- Format:
2025
-
2026
- ~~~
2027
- (match-rest
2028
- (match-var :foo))
2029
- "in *foo"
2030
- ~ operator
2031
- ~~~~ expression
2032
- ~~~
2033
-
2034
- #### Without name
2035
-
2036
- Format:
2037
-
2038
- ~~~
2039
- (match-rest)
2040
- "in *"
2041
- ~ operator
2042
- ~ expression
2043
- ~~~
2044
-
2045
- ### Pin operator
2046
-
2047
- Format:
2048
-
2049
- ~~~
2050
- (pin
2051
- (lvar :foo))
2052
- "in ^foo"
2053
- ~ selector
2054
- ~~~~ expression
2055
- ~~~
2056
-
2057
- ### Match alternative
2058
-
2059
- Format:
2060
-
2061
- ~~~
2062
- (match-alt
2063
- (pin
2064
- (lvar :foo))
2065
- (int 1))
2066
- "in ^foo | 1"
2067
- ~ operator
2068
- ~~~~~~~~ expression
2069
- ~~~
2070
-
2071
- ### Match with alias
2072
-
2073
- Format:
2074
-
2075
- ~~~
2076
- (match-as
2077
- (int 1)
2078
- (match-var :foo))
2079
- "in 1 => foo"
2080
- ~~ operator
2081
- ~~~~~~~~ expression
2082
- ~~~
2083
-
2084
- ### Match using array pattern
2085
-
2086
- #### Explicit
2087
-
2088
- Format:
2089
-
2090
- ~~~
2091
- (array-pattern
2092
- (pin
2093
- (lvar :foo))
2094
- (match-var :bar))
2095
- "in [^foo, bar]"
2096
- ~ begin ~ end
2097
- ~~~~~~~~~~~ expression
2098
- ~~~
2099
-
2100
- #### Explicit with tail
2101
-
2102
- Adding a trailing comma in the end works as `, *`
2103
-
2104
- Format:
2105
-
2106
- ~~~
2107
- (array-pattern-with-tail
2108
- (pin
2109
- (lvar :foo))
2110
- (match-var :bar))
2111
- "in [^foo, bar,]"
2112
- ~ begin ~ end
2113
- ~~~~~~~~~~~~ expression
2114
- ~~~
2115
-
2116
- #### Implicit
2117
-
2118
- Format:
2119
-
2120
- ~~~
2121
- (array-pattern
2122
- (pin
2123
- (lvar :foo))
2124
- (match-var :bar))
2125
- "in ^foo, bar"
2126
- ~~~~~~~~~ expression
2127
- ~~~
2128
-
2129
- #### Implicit with tail
2130
-
2131
- Format:
2132
-
2133
- Adding a trailing comma in the end works as `, *`,
2134
- so a single item match with comma gets interpreted as an array.
2135
-
2136
- ~~~
2137
- (array-pattern-with-tail
2138
- (match-var :foo))
2139
- "in foo,"
2140
- ~~~~ expression
2141
- ~~~
2142
-
2143
- ### Matching using hash pattern
2144
-
2145
- #### Explicit
2146
-
2147
- Format:
2148
-
2149
- ~~~
2150
- (hash-pattern
2151
- (pair
2152
- (sym :a)
2153
- (int 10)))
2154
- "in { a: 10 }"
2155
- ~ begin ~ end
2156
- ~~~~~~~~~ expression
2157
- ~~~
2158
-
2159
- #### Implicit
2160
-
2161
- Format:
2162
-
2163
- ~~~
2164
- (hash-pattern
2165
- (pair
2166
- (sym :a)
2167
- (int 10)))
2168
- "in a: 10"
2169
- ~~~~~ expression
2170
- ~~~
2171
-
2172
- #### Assignment using hash pattern
2173
-
2174
- Format:
2175
-
2176
- ~~~
2177
- (hash-pattern
2178
- (match-var :a))
2179
- "in a:"
2180
- ~ name (match-var)
2181
- ~~ expression (match-var)
2182
- ~~~
2183
-
2184
- #### Nil hash pattern
2185
-
2186
- Format:
2187
- ~~~
2188
- (hash-pattern
2189
- (match-nil-pattern))
2190
- "in **nil"
2191
- ~~~~~ expression (match-nil-pattern)
2192
- ~~~ name (match-nil-pattern)
2193
- ~~~
2194
-
2195
- ### Matching using find pattern
2196
-
2197
- Format:
2198
-
2199
- ~~~
2200
- (find-pattern
2201
- (match-rest
2202
- (match-var :a))
2203
- (int 42)
2204
- (match-rest))
2205
- "in [*, 42, *]"
2206
- ~ begin
2207
- ~ end
2208
- ~~~~~~~~~~ expression
2209
- ~~~
2210
-
2211
- 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.
2212
-
2213
- ### Matching using const pattern
2214
-
2215
- #### With array pattern
2216
-
2217
- Format:
2218
-
2219
- ~~~
2220
- (const-pattern
2221
- (const nil :X)
2222
- (array-pattern
2223
- (pin
2224
- (lvar :foo))
2225
- (match-var :bar)))
2226
- "in X[^foo bar]"
2227
- ~ begin (const-pattern)
2228
- ~ end (const-pattern)
2229
- ~~~~~~~~~~~~ expression (const-pattern)
2230
- ~ name (const-pattern.const)
2231
- ~ expression (const-pattern.const)
2232
- ~~~
2233
-
2234
- #### With hash pattern
2235
-
2236
- Format:
2237
-
2238
- ~~~
2239
- (const-pattern
2240
- (const nil :X)
2241
- (hash-pattern
2242
- (match-var :foo)
2243
- (match-var :bar)))
2244
- "in X[foo:, bar:]"
2245
- ~ begin (const-pattern)
2246
- ~ end (const-pattern)
2247
- ~~~~~~~~~~~~~ expression (const-pattern)
2248
- ~ name (const-pattern.const)
2249
- ~ expression (const-pattern.const)
2250
- ~~~
2251
-
2252
- #### With array pattern without elements
2253
-
2254
- Format:
2255
-
2256
- ~~~
2257
- (const-pattern
2258
- (const nil :X)
2259
- (array-pattern))
2260
- "in X[]"
2261
- ~ begin (const-pattern)
2262
- ~ end (const-pattern)
2263
- ~~~ expression (const-pattern)
2264
- ~ name (const-pattern.const)
2265
- ~ expression (const-pattern.const)
2266
- ~~ expression (const-pattern.array_pattern)
2267
- ~~~
2268
-
2269
- #### With find pattern
2270
-
2271
- Format:
2272
-
2273
- ~~~
2274
- (const-pattern
2275
- (const nil :X)
2276
- (find-pattern
2277
- (match-rest)
2278
- (int 42)
2279
- (match-rest)))
2280
- "in X[*, 42, *]"
2281
- ~ begin
2282
- ~ end
2283
- ~~~~~~~~~~~ expression
2284
- ~~~