parser 2.7.1.1 → 2.7.2.0

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