parser 2.7.1.3 → 3.0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parser.rb +1 -1
  3. data/lib/parser/all.rb +1 -1
  4. data/lib/parser/ast/processor.rb +5 -7
  5. data/lib/parser/base.rb +7 -5
  6. data/lib/parser/builders/default.rb +225 -29
  7. data/lib/parser/context.rb +5 -0
  8. data/lib/parser/current.rb +11 -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 +8 -7
  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/rubymotion.rb +6086 -0
  28. data/lib/parser/runner.rb +4 -4
  29. data/lib/parser/source/buffer.rb +50 -27
  30. data/lib/parser/source/comment.rb +1 -1
  31. data/lib/parser/source/comment/associator.rb +1 -1
  32. data/lib/parser/source/map/{endless_definition.rb → method_definition.rb} +5 -3
  33. data/lib/parser/source/range.rb +3 -3
  34. data/lib/parser/source/tree_rewriter.rb +94 -1
  35. data/lib/parser/source/tree_rewriter/action.rb +39 -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 +2 -18
  40. metadata +13 -102
  41. data/.gitignore +0 -34
  42. data/.travis.yml +0 -40
  43. data/.yardopts +0 -21
  44. data/CHANGELOG.md +0 -1101
  45. data/CONTRIBUTING.md +0 -17
  46. data/Gemfile +0 -10
  47. data/README.md +0 -308
  48. data/Rakefile +0 -167
  49. data/ci/run_rubocop_specs +0 -14
  50. data/doc/AST_FORMAT.md +0 -2229
  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 -2543
  56. data/lib/parser/macruby.y +0 -2198
  57. data/lib/parser/ruby18.y +0 -1934
  58. data/lib/parser/ruby19.y +0 -2175
  59. data/lib/parser/ruby20.y +0 -2353
  60. data/lib/parser/ruby21.y +0 -2357
  61. data/lib/parser/ruby22.y +0 -2364
  62. data/lib/parser/ruby23.y +0 -2370
  63. data/lib/parser/ruby24.y +0 -2408
  64. data/lib/parser/ruby25.y +0 -2405
  65. data/lib/parser/ruby26.y +0 -2413
  66. data/lib/parser/ruby27.y +0 -2941
  67. data/lib/parser/ruby28.y +0 -3016
  68. data/lib/parser/rubymotion.y +0 -2182
  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 -79
  73. data/test/parse_helper.rb +0 -313
  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 -9596
  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 -263
  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,2229 +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 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:
1174
-
1175
- ~~~
1176
- (def :foo
1177
- (forward-args) nil)
1178
- "def foo(...); end"
1179
- ~ end
1180
- ~ begin
1181
- ~~~~~ expression
1182
- ~~~
1183
-
1184
- ### Method call taking arguments of the currently forwarding method
1185
-
1186
- Format:
1187
-
1188
- ~~~
1189
- (send nil :foo
1190
- (forwarded-args))
1191
- "foo(...)"
1192
- ~~~ expression
1193
- ~~~
1194
-
1195
- ## Send
1196
-
1197
- ### To self
1198
-
1199
- Format:
1200
-
1201
- ~~~
1202
- (send nil :foo (lvar :bar))
1203
- "foo(bar)"
1204
- ~~~ selector
1205
- ^ begin
1206
- ^ end
1207
- ~~~~~~~~ expression
1208
- ~~~
1209
-
1210
- ### To receiver
1211
-
1212
- Format:
1213
-
1214
- ~~~
1215
- (send (lvar :foo) :bar (int 1))
1216
- "foo.bar(1)"
1217
- ^ dot
1218
- ~~~ selector
1219
- ^ begin
1220
- ^ end
1221
- ~~~~~~~~~~ expression
1222
-
1223
- (send (lvar :foo) :+ (int 1))
1224
- "foo + 1"
1225
- ^ selector
1226
- ~~~~~~~ expression
1227
-
1228
- (send (lvar :foo) :-@)
1229
- "-foo"
1230
- ^ selector
1231
- ~~~~ expression
1232
-
1233
- (send (lvar :foo) :a= (int 1))
1234
- "foo.a = 1"
1235
- ~ selector
1236
- ^ operator
1237
- ~~~~~~~~~ expression
1238
- ~~~
1239
-
1240
- ### To superclass
1241
-
1242
- Format of super with arguments:
1243
-
1244
- ~~~
1245
- (super (lvar :a))
1246
- "super a"
1247
- ~~~~~ keyword
1248
- ~~~~~~~ expression
1249
-
1250
- (super)
1251
- "super()"
1252
- ^ begin
1253
- ^ end
1254
- ~~~~~ keyword
1255
- ~~~~~~~ expression
1256
- ~~~
1257
-
1258
- Format of super without arguments (**z**ero-arity):
1259
-
1260
- ~~~
1261
- (zsuper)
1262
- "super"
1263
- ~~~~~ keyword
1264
- ~~~~~ expression
1265
- ~~~
1266
-
1267
- ### To block argument
1268
-
1269
- Format:
1270
-
1271
- ~~~
1272
- (yield (lvar :foo))
1273
- "yield(foo)"
1274
- ~~~~~ keyword
1275
- ^ begin
1276
- ^ end
1277
- ~~~~~~~~~~ expression
1278
- ~~~
1279
-
1280
- ### Indexing
1281
-
1282
- Format:
1283
-
1284
- ~~~
1285
- (index (lvar :foo) (int 1))
1286
- "foo[1]"
1287
- ^ begin
1288
- ^ end
1289
- ~~~~~~ expression
1290
-
1291
- (indexasgn (lvar :bar) (int 1) (int 2) (lvar :baz))
1292
- "bar[1, 2] = baz"
1293
- ^ begin
1294
- ^ end
1295
- ^ operator
1296
- ~~~~~~~~~~~~~~~ expression
1297
-
1298
- ~~~
1299
-
1300
- ### Passing a literal block
1301
-
1302
- ~~~
1303
- (block (send nil :foo) (args (arg :bar)) (begin ...))
1304
- "foo do |bar|; end"
1305
- ~~ begin
1306
- ~~~ end
1307
- ~~~~~~~~~~~~~ expression
1308
- ~~~
1309
-
1310
- ### Passing expression as block
1311
-
1312
- Used when passing expression as block `foo(&bar)`
1313
-
1314
- ~~~
1315
- (send nil :foo (int 1) (block-pass (lvar :foo)))
1316
- "foo(1, &foo)"
1317
- ^ operator
1318
- ~~~~ expression
1319
- ~~~
1320
-
1321
- ### "Stabby lambda"
1322
-
1323
- ~~~
1324
- (block (lambda) (args) nil)
1325
- "-> {}"
1326
- ~~ lambda.expression
1327
- ~~~
1328
-
1329
- ### "Safe navigation operator"
1330
-
1331
- ~~~
1332
- (csend (send nil :foo) :bar)
1333
- "foo&.bar"
1334
- ~~ dot
1335
- ~~~
1336
-
1337
- ### Objective-C variadic send
1338
-
1339
- MacRuby allows to pass a variadic amount of arguments via the last
1340
- keyword "argument". Semantically, these, together with the pair value
1341
- of the last pair in the hash implicitly passed as the last argument,
1342
- form an array, which replaces the pair value. Despite that, the node
1343
- is called `objc-varargs` to distinguish it from a literal array passed
1344
- as a value.
1345
-
1346
- ~~~
1347
- (send nil :foo (int 1) (hash (pair (sym :bar) (objc-varargs (int 1) (int 2) (nil)))))
1348
- "foo(1, bar: 2, 3, nil)"
1349
- ~~~~~~~~~ expression (array)
1350
- ~~~
1351
-
1352
- ## Control flow
1353
-
1354
- ### Logical operators
1355
-
1356
- #### Binary (and or && ||)
1357
-
1358
- Format:
1359
-
1360
- ~~~
1361
- (and (lvar :foo) (lvar :bar))
1362
- "foo and bar"
1363
- ~~~ operator
1364
- ~~~~~~~~~~~ expression
1365
- ~~~
1366
-
1367
- ~~~
1368
- (or (lvar :foo) (lvar :bar))
1369
- "foo or bar"
1370
- ~~ operator
1371
- ~~~~~~~~~~ expression
1372
- ~~~
1373
-
1374
- #### Unary (! not) (1.8)
1375
-
1376
- Format:
1377
-
1378
- ~~~
1379
- (not (lvar :foo))
1380
- "!foo"
1381
- ^ operator
1382
- "not foo"
1383
- ~~~ operator
1384
- ~~~
1385
-
1386
- ### Branching
1387
-
1388
- #### Without else
1389
-
1390
- Format:
1391
-
1392
- ~~~
1393
- (if (lvar :cond) (lvar :iftrue) nil)
1394
- "if cond then iftrue; end"
1395
- ~~ keyword
1396
- ~~~~ begin
1397
- ~~~ end
1398
- ~~~~~~~~~~~~~~~~~~~~~~~~ expression
1399
-
1400
- "if cond; iftrue; end"
1401
- ~~ keyword
1402
- ~~~ end
1403
- ~~~~~~~~~~~~~~~~~~~~ expression
1404
-
1405
- "iftrue if cond"
1406
- ~~ keyword
1407
- ~~~~~~~~~~~~~~ expression
1408
-
1409
- (if (lvar :cond) nil (lvar :iftrue))
1410
- "unless cond then iftrue; end"
1411
- ~~~~~~ keyword
1412
- ~~~~ begin
1413
- ~~~ end
1414
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1415
-
1416
- "unless cond; iftrue; end"
1417
- ~~~~~~ keyword
1418
- ~~~ end
1419
- ~~~~~~~~~~~~~~~~~~~~~~~~ expression
1420
-
1421
- "iftrue unless cond"
1422
- ~~~~~~ keyword
1423
- ~~~~~~~~~~~~~~~~~~ expression
1424
- ~~~
1425
-
1426
- #### With else
1427
-
1428
- Format:
1429
-
1430
- ~~~
1431
- (if (lvar :cond) (lvar :iftrue) (lvar :iffalse))
1432
- "if cond then iftrue; else; iffalse; end"
1433
- ~~ keyword
1434
- ~~~~ begin
1435
- ~~~~ else
1436
- ~~~ end
1437
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1438
-
1439
- "if cond; iftrue; else; iffalse; end"
1440
- ~~ keyword
1441
- ~~~~ else
1442
- ~~~ end
1443
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1444
-
1445
- (if (lvar :cond) (lvar :iffalse) (lvar :iftrue))
1446
- "unless cond then iftrue; else; iffalse; end"
1447
- ~~~~~~ keyword
1448
- ~~~~ begin
1449
- ~~~~ else
1450
- ~~~ end
1451
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1452
-
1453
- "unless cond; iftrue; else; iffalse; end"
1454
- ~~~~~~ keyword
1455
- ~~~~ else
1456
- ~~~ end
1457
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1458
- ~~~
1459
-
1460
- #### With elsif
1461
-
1462
- Format:
1463
-
1464
- ~~~
1465
- (if (lvar :cond1) (int 1) (if (lvar :cond2 (int 2) (int 3))))
1466
- "if cond1; 1; elsif cond2; 2; else 3; end"
1467
- ~~ keyword (left)
1468
- ~~~~~ else (left)
1469
- ~~~ end (left)
1470
- ~~~~~ keyword (right)
1471
- ~~~~ else (right)
1472
- ~~~ end (right)
1473
- ~~~
1474
-
1475
- #### Ternary
1476
-
1477
- Format:
1478
-
1479
- ~~~
1480
- (if (lvar :cond) (lvar :iftrue) (lvar :iffalse))
1481
- "cond ? iftrue : iffalse"
1482
- ^ question
1483
- ^ colon
1484
- ~~~~~~~~~~~~~~~~~~~~~~~ expression
1485
- ~~~
1486
-
1487
- ### Case matching
1488
-
1489
- #### When clause
1490
-
1491
- Format:
1492
-
1493
- ~~~
1494
- (when (regexp "foo" (regopt)) (begin (lvar :bar)))
1495
- "when /foo/ then bar"
1496
- ~~~~ keyword
1497
- ~~~~ begin
1498
- ~~~~~~~~~~~~~~~~~~~ expression
1499
-
1500
- (when (int 1) (int 2) (send nil :meth))
1501
- "when 1, 2; meth"
1502
-
1503
- (when (int 1) (splat (lvar :foo)) (send nil :meth))
1504
- "when 1, *foo; meth"
1505
-
1506
- (when (splat (lvar :foo)) (send nil :meth))
1507
- "when *foo; meth"
1508
- ~~~
1509
-
1510
- #### Case-expression clause
1511
-
1512
- ##### Without else
1513
-
1514
- Format:
1515
-
1516
- ~~~
1517
- (case (lvar :foo) (when (str "bar") (lvar :bar)) nil)
1518
- "case foo; when "bar"; bar; end"
1519
- ~~~~ keyword ~~~ end
1520
- ~~~
1521
-
1522
- ##### With else
1523
-
1524
- Format:
1525
-
1526
- ~~~
1527
- (case (lvar :foo) (when (str "bar") (lvar :bar)) (lvar :baz))
1528
- "case foo; when "bar"; bar; else baz; end"
1529
- ~~~~ keyword ~~~~ else ~~~ end
1530
- ~~~
1531
-
1532
- #### Case-conditions clause
1533
-
1534
- ##### Without else
1535
-
1536
- Format:
1537
-
1538
- ~~~
1539
- (case nil (when (lvar :bar) (lvar :bar)) nil)
1540
- "case; when bar; bar; end"
1541
- ~~~~ keyword ~~~ end
1542
- ~~~
1543
-
1544
- ##### With else
1545
-
1546
- Format:
1547
-
1548
- ~~~
1549
- (case nil (when (lvar :bar) (lvar :bar)) (lvar :baz))
1550
- "case; when bar; bar; else baz; end"
1551
- ~~~~ keyword ~~~~ else ~~~ end
1552
-
1553
- (case nil (lvar :baz))
1554
- "case; else baz; end"
1555
- ~~~~ keyword
1556
- ~~~~ else
1557
- ~~~ end
1558
- ~~~
1559
-
1560
- ### Looping
1561
-
1562
- #### With precondition
1563
-
1564
- Format:
1565
-
1566
- ~~~
1567
- (while (lvar :condition) (send nil :foo))
1568
- "while condition do foo; end"
1569
- ~~~~~ keyword
1570
- ~~ begin
1571
- ~~~ end
1572
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1573
-
1574
- "while condition; foo; end"
1575
- ~~~~~ keyword
1576
- ~~~ end
1577
- ~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1578
-
1579
- "foo while condition"
1580
- ~~~~~ keyword
1581
- ~~~~~~~~~~~~~~~~~~~ expression
1582
-
1583
- (until (lvar :condition) (send nil :foo))
1584
- "until condition do foo; end"
1585
- ~~~~~ keyword
1586
- ~~ begin
1587
- ~~~ end
1588
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1589
-
1590
- (until (lvar :condition) (send nil :foo))
1591
- "until condition; foo; end"
1592
- ~~~~~ keyword
1593
- ~~~ end
1594
- ~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1595
-
1596
- "foo until condition"
1597
- ~~~~~ keyword
1598
- ~~~~~~~~~~~~~~~~~~~ expression
1599
- ~~~
1600
-
1601
- #### With postcondition
1602
-
1603
- Format:
1604
-
1605
- ~~~
1606
- (while-post (lvar :condition) (kwbegin (send nil :foo)))
1607
- "begin; foo; end while condition"
1608
- ~~~~~ begin (begin)
1609
- ~~~ end (begin)
1610
- ~~~~~ keyword (while-post)
1611
-
1612
- (until-post (lvar :condition) (kwbegin (send nil :foo)))
1613
- "begin; foo; end until condition"
1614
- ~~~~~ begin (begin)
1615
- ~~~ end (begin)
1616
- ~~~~~ keyword (until-post)
1617
- ~~~
1618
-
1619
- #### For-in
1620
-
1621
- Format:
1622
-
1623
- ~~~
1624
- (for (lvasgn :a) (lvar :array) (send nil :p (lvar :a)))
1625
- "for a in array do p a; end"
1626
- ~~~ keyword
1627
- ~~ in
1628
- ~~ begin
1629
- ~~~ end
1630
-
1631
- "for a in array; p a; end"
1632
- ~~~ keyword
1633
- ~~ in
1634
- ~~~ end
1635
-
1636
- (for
1637
- (mlhs (lvasgn :a) (lvasgn :b)) (lvar :array)
1638
- (send nil :p (lvar :a) (lvar :b)))
1639
- "for a, b in array; p a, b; end"
1640
- ~~~
1641
-
1642
- #### Break
1643
-
1644
- Format:
1645
-
1646
- ~~~
1647
- (break (int 1))
1648
- "break 1"
1649
- ~~~~~ keyword
1650
- ~~~~~~~ expression
1651
- ~~~
1652
-
1653
- #### Next
1654
-
1655
- Format:
1656
-
1657
- ~~~
1658
- (next (int 1))
1659
- "next 1"
1660
- ~~~~ keyword
1661
- ~~~~~~ expression
1662
- ~~~
1663
-
1664
- #### Redo
1665
-
1666
- Format:
1667
-
1668
- ~~~
1669
- (redo)
1670
- "redo"
1671
- ~~~~ keyword
1672
- ~~~~ expression
1673
- ~~~
1674
-
1675
- ### Return
1676
-
1677
- Format:
1678
-
1679
- ~~~
1680
- (return (lvar :foo))
1681
- "return(foo)"
1682
- ~~~~~~ keyword
1683
- ~~~~~~~~~~~ expression
1684
- ~~~
1685
-
1686
- ### Exception handling
1687
-
1688
- #### Rescue body
1689
-
1690
- Format:
1691
-
1692
- ~~~
1693
- (resbody (array (const nil :Exception) (const nil :A)) (lvasgn :bar) (int 1))
1694
- "rescue Exception, A => bar; 1"
1695
- ~~~~~~ keyword ~~ assoc
1696
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1697
-
1698
- "rescue Exception, A => bar then 1"
1699
- ~~~~~~ keyword ~~ assoc
1700
- ~~~~ begin
1701
-
1702
- (resbody (array (const nil :Exception)) (ivasgn :bar) (int 1))
1703
- "rescue Exception => @bar; 1"
1704
- ~~~~~~ keyword ~~ assoc
1705
-
1706
- (resbody nil (lvasgn :bar) (int 1))
1707
- "rescue => bar; 1"
1708
- ~~~~~~ keyword
1709
- ~~ assoc
1710
-
1711
- (resbody nil nil (int 1))
1712
- "rescue; 1"
1713
- ~~~~~~ keyword
1714
- ~~~
1715
-
1716
- #### Rescue statement
1717
-
1718
- ##### Without else
1719
-
1720
- Format:
1721
-
1722
- ~~~
1723
- (begin
1724
- (rescue (send nil :foo) (resbody ...) (resbody ...) nil))
1725
- "begin; foo; rescue Exception; rescue; end"
1726
- ~~~~~ begin ~~~ end
1727
- ~~~~~~~~~~~~~~~~~ expression (rescue.resbody/1)
1728
- ~~~~~~~ expression (rescue.resbody/2)
1729
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (rescue)
1730
- ~~~
1731
-
1732
- ##### With else
1733
-
1734
- Format:
1735
-
1736
- ~~~
1737
- (begin
1738
- (rescue (send nil :foo) (resbody ...) (resbody ...) (true)))
1739
- "begin; foo; rescue Exception; rescue; else true end"
1740
- ~~~~~ begin ~~~~ else (rescue)
1741
- ~~~ end
1742
- ~~~
1743
-
1744
- #### Ensure statement
1745
-
1746
- Format:
1747
-
1748
- ~~~
1749
- (begin
1750
- (ensure (send nil :foo) (send nil :bar))
1751
- "begin; foo; ensure; bar; end"
1752
- ~~~~~ begin ~~~~~~ keyword (ensure)
1753
- ~~~ end
1754
- ~~~
1755
-
1756
- #### Rescue with ensure
1757
-
1758
- Format:
1759
-
1760
- ~~~
1761
- (begin
1762
- (ensure
1763
- (rescue (send nil :foo) (resbody ...) (int 1))
1764
- (send nil :bar))
1765
- "begin; foo; rescue; nil; else; 1; ensure; bar; end"
1766
- ~~~~~ begin
1767
- ~~~~ else (ensure.rescue)
1768
- ~~~~~~~~~~~~~~~~~~~~~ expression (rescue)
1769
- ~~~~~~ keyword (ensure)
1770
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (ensure)
1771
- ~~~ end
1772
- ~~~
1773
-
1774
- #### Retry
1775
-
1776
- Format:
1777
-
1778
- ~~~
1779
- (retry)
1780
- "retry"
1781
- ~~~~~ keyword
1782
- ~~~~~ expression
1783
- ~~~
1784
-
1785
- ### BEGIN and END
1786
-
1787
- Format:
1788
-
1789
- ~~~
1790
- (preexe (send nil :puts (str "foo")))
1791
- "BEGIN { puts "foo" }"
1792
- ~~~~~ keyword
1793
- ^ begin ^ end
1794
- ~~~~~~~~~~~~~~~~~~~~ expression
1795
-
1796
- (postexe (send nil :puts (str "bar")))
1797
- "END { puts "bar" }"
1798
- ~~~ keyword
1799
- ^ begin ^ end
1800
- ~~~~~~~~~~~~~~~~~~ expression
1801
- ~~~
1802
-
1803
- ## Miscellanea
1804
-
1805
- ### Flip-flops
1806
-
1807
- Format:
1808
-
1809
- ~~~
1810
- (iflipflop (lvar :a) (lvar :b))
1811
- "if a..b; end"
1812
- ~~ operator
1813
- ~~~~ expression
1814
-
1815
- (eflipflop (lvar :a) (lvar :b))
1816
- "if a...b; end"
1817
- ~~~ operator
1818
- ~~~~~ expression
1819
- ~~~
1820
-
1821
- ### Implicit matches
1822
-
1823
- Format:
1824
-
1825
- ~~~
1826
- (match-current-line (regexp (str "a") (regopt)))
1827
- "if /a/; end"
1828
- ~~~ expression
1829
- ~~~
1830
-
1831
- ### Local variable injecting matches
1832
-
1833
- Format:
1834
-
1835
- ~~~
1836
- (match-with-lvasgn (regexp (str "(?<match>bar)") (regopt)) (lvar :baz))
1837
- "/(?<match>bar)/ =~ baz"
1838
- ~~ selector
1839
- ~~~~~~~~~~~~~~~~~~~~~~ expression
1840
- ~~~
1841
-
1842
- ## Special constants
1843
-
1844
- ### File
1845
-
1846
- Format:
1847
-
1848
- ~~~
1849
- (__FILE__)
1850
- "__FILE__"
1851
- ~~~~~~~~ expression
1852
- ~~~
1853
-
1854
- ### Line
1855
-
1856
- Format:
1857
-
1858
- ~~~
1859
- (__LINE__)
1860
- "__LINE__"
1861
- ~~~~~~~~ expression
1862
- ~~~
1863
-
1864
- ### Encoding
1865
-
1866
- Format:
1867
-
1868
- ~~~
1869
- (__ENCODING__)
1870
- "__ENCODING__"
1871
- ~~~~~~~~~~~~ expression
1872
- ~~~
1873
-
1874
- ## Pattern matching
1875
-
1876
- ### Using `in` modifier
1877
-
1878
- Format:
1879
-
1880
- ~~~
1881
- (in-match
1882
- (int 1)
1883
- (match-var :a))
1884
- "1 in a"
1885
- ~~ operator
1886
- ~~~~~~ expression
1887
- ~~~
1888
-
1889
- ### Case with pattern matching
1890
-
1891
- #### Without else
1892
-
1893
- Format:
1894
-
1895
- ~~~
1896
- (case-match
1897
- (str "str")
1898
- (in-pattern
1899
- (match-var :foo)
1900
- (lvar :bar)) nil)
1901
- "case "str"; in foo; bar; end"
1902
- ~~~~ keyword ~~~ end
1903
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1904
- ~~~
1905
-
1906
- #### With else
1907
-
1908
- Format:
1909
-
1910
- ~~~
1911
- (case-match,
1912
- (str "str")
1913
- (in-pattern
1914
- (match-var :foo)
1915
- (lvar :bar))
1916
- (lvar :baz))
1917
- "case "str"; in foo; bar; else; baz; end"
1918
- ~~~~ keyword ~~~~ else ~~~ end
1919
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1920
- ~~~
1921
-
1922
- #### With empty else
1923
-
1924
- Empty `else` differs from the missing (or _implicit_) `else` for pattern matching, since
1925
- the latter one raises a `NoMatchingPattern` exception. Thus, we need a way to distinguish this
1926
- two cases in the resulting AST.
1927
-
1928
- Format:
1929
-
1930
- ~~~
1931
- (case-match,
1932
- (str "str")
1933
- (in-pattern
1934
- (match-var :foo)
1935
- (lvar :bar))
1936
- (empty-else))
1937
- "case "str"; in foo; bar; else; end"
1938
- ~~~~ keyword ~~~~ else
1939
- ~~~ end
1940
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1941
- ~~~
1942
-
1943
- ### In clause
1944
-
1945
- Format:
1946
-
1947
- ~~~
1948
- (in-pattern
1949
- (match-var :foo)
1950
- (lvar :bar))
1951
- "in foo then bar"
1952
- ~~ keyword
1953
- ~~~~ begin
1954
- ~~~~~~~~~~~~~~~ expression
1955
- ~~~
1956
-
1957
- ### If guard
1958
-
1959
- This guard runs after matching, so it's not an `if` modifier.
1960
-
1961
- Format:
1962
-
1963
- ~~~
1964
- (in-pattern
1965
- (match-var :foo)
1966
- (if-guard
1967
- (lvar :bar)) nil)
1968
- "in foo if bar"
1969
- ~~ keyword
1970
- ~~~~~~ expression
1971
- ~~~
1972
-
1973
- ### Unless guard
1974
-
1975
- This guard runs after matching, so it's not an `unless` modifier.
1976
-
1977
- Format:
1978
-
1979
- ~~~
1980
- (in-pattern
1981
- (match-var :foo)
1982
- (unless-guard
1983
- (lvar :bar)) nil)
1984
- "in foo unless bar"
1985
- ~~~~~~ keyword
1986
- ~~~~~~~~~~ expression
1987
- ~~~
1988
-
1989
- ### Match variable
1990
-
1991
- Format:
1992
-
1993
- ~~~
1994
- (match-var :foo)
1995
- "in foo"
1996
- ~~~ name
1997
- ~~~ expression
1998
- ~~~
1999
-
2000
- ### Match rest
2001
-
2002
- #### With name
2003
-
2004
- Format:
2005
-
2006
- ~~~
2007
- (match-rest
2008
- (match-var :foo))
2009
- "in *foo"
2010
- ~ operator
2011
- ~~~~ expression
2012
- ~~~
2013
-
2014
- #### Without name
2015
-
2016
- Format:
2017
-
2018
- ~~~
2019
- (match-rest)
2020
- "in *"
2021
- ~ operator
2022
- ~ expression
2023
- ~~~
2024
-
2025
- ### Pin operator
2026
-
2027
- Format:
2028
-
2029
- ~~~
2030
- (pin
2031
- (lvar :foo))
2032
- "in ^foo"
2033
- ~ selector
2034
- ~~~~ expression
2035
- ~~~
2036
-
2037
- ### Match alternative
2038
-
2039
- Format:
2040
-
2041
- ~~~
2042
- (match-alt
2043
- (pin
2044
- (lvar :foo))
2045
- (int 1))
2046
- "in ^foo | 1"
2047
- ~ operator
2048
- ~~~~~~~~ expression
2049
- ~~~
2050
-
2051
- ### Match with alias
2052
-
2053
- Format:
2054
-
2055
- ~~~
2056
- (match-as
2057
- (int 1)
2058
- (match-var :foo))
2059
- "in 1 => foo"
2060
- ~~ operator
2061
- ~~~~~~~~ expression
2062
- ~~~
2063
-
2064
- ### Match using array pattern
2065
-
2066
- #### Explicit
2067
-
2068
- Format:
2069
-
2070
- ~~~
2071
- (array-pattern
2072
- (pin
2073
- (lvar :foo))
2074
- (match-var :bar))
2075
- "in [^foo, bar]"
2076
- ~ begin ~ end
2077
- ~~~~~~~~~~~ expression
2078
- ~~~
2079
-
2080
- #### Explicit with tail
2081
-
2082
- Adding a trailing comma in the end works as `, *`
2083
-
2084
- Format:
2085
-
2086
- ~~~
2087
- (array-pattern-with-tail
2088
- (pin
2089
- (lvar :foo))
2090
- (match-var :bar))
2091
- "in [^foo, bar,]"
2092
- ~ begin ~ end
2093
- ~~~~~~~~~~~~ expression
2094
- ~~~
2095
-
2096
- #### Implicit
2097
-
2098
- Format:
2099
-
2100
- ~~~
2101
- (array-pattern
2102
- (pin
2103
- (lvar :foo))
2104
- (match-var :bar))
2105
- "in ^foo, bar"
2106
- ~~~~~~~~~ expression
2107
- ~~~
2108
-
2109
- #### Implicit with tail
2110
-
2111
- Format:
2112
-
2113
- Adding a trailing comma in the end works as `, *`,
2114
- so a single item match with comma gets interpreted as an array.
2115
-
2116
- ~~~
2117
- (array-pattern-with-tail
2118
- (match-var :foo))
2119
- "in foo,"
2120
- ~~~~ expression
2121
- ~~~
2122
-
2123
- ### Matching using hash pattern
2124
-
2125
- #### Explicit
2126
-
2127
- Format:
2128
-
2129
- ~~~
2130
- (hash-pattern
2131
- (pair
2132
- (sym :a)
2133
- (int 10)))
2134
- "in { a: 10 }"
2135
- ~ begin ~ end
2136
- ~~~~~~~~~ expression
2137
- ~~~
2138
-
2139
- #### Implicit
2140
-
2141
- Format:
2142
-
2143
- ~~~
2144
- (hash-pattern
2145
- (pair
2146
- (sym :a)
2147
- (int 10)))
2148
- "in a: 10"
2149
- ~~~~~ expression
2150
- ~~~
2151
-
2152
- #### Assignment using hash pattern
2153
-
2154
- Format:
2155
-
2156
- ~~~
2157
- (hash-pattern
2158
- (match-var :a))
2159
- "in a:"
2160
- ~ name (match-var)
2161
- ~~ expression (match-var)
2162
- ~~~
2163
-
2164
- #### Nil hash pattern
2165
-
2166
- Format:
2167
- ~~~
2168
- (hash-pattern
2169
- (match-nil-pattern))
2170
- "in **nil"
2171
- ~~~~~ expression (match-nil-pattern)
2172
- ~~~ name (match-nil-pattern)
2173
- ~~~
2174
-
2175
- ### Matching using const pattern
2176
-
2177
- #### With array pattern
2178
-
2179
- Format:
2180
-
2181
- ~~~
2182
- (const-pattern
2183
- (const nil :X)
2184
- (array-pattern
2185
- (pin
2186
- (lvar :foo))
2187
- (match-var :bar)))
2188
- "in X[^foo bar]"
2189
- ~ begin (const-pattern)
2190
- ~ end (const-pattern)
2191
- ~~~~~~~~~~~~ expression (const-pattern)
2192
- ~ name (const-pattern.const)
2193
- ~ expression (const-pattern.const)
2194
- ~~~
2195
-
2196
- #### With hash pattern
2197
-
2198
- Format:
2199
-
2200
- ~~~
2201
- (const-pattern
2202
- (const nil :X)
2203
- (hash-pattern
2204
- (match-var :foo)
2205
- (match-var :bar)))
2206
- "in X[foo:, bar:]"
2207
- ~ begin (const-pattern)
2208
- ~ end (const-pattern)
2209
- ~~~~~~~~~~~~~ expression (const-pattern)
2210
- ~ name (const-pattern.const)
2211
- ~ expression (const-pattern.const)
2212
- ~~~
2213
-
2214
- #### With array pattern without elements
2215
-
2216
- Format:
2217
-
2218
- ~~~
2219
- (const-pattern
2220
- (const nil :X)
2221
- (array-pattern))
2222
- "in X[]"
2223
- ~ begin (const-pattern)
2224
- ~ end (const-pattern)
2225
- ~~~ expression (const-pattern)
2226
- ~ name (const-pattern.const)
2227
- ~ expression (const-pattern.const)
2228
- ~~ expression (const-pattern.array_pattern)
2229
- ~~~