-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathparser_spec.cr
3178 lines (2909 loc) · 119 KB
/
parser_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "../spec_helper"
require "../support/nodes.cr"
# Check that parsing the given source succeeds. If given, additionally check
# that the result of parsing the source matches the given nodes.
private def it_parses(source, *expected, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
it %Q(parses `#{source}`), file, line, end_line do
result = parse_program(source)
unless expected.empty?
result.should eq(Expressions.new(*expected))
end
end
end
# Expect the given source to raise an error when parsed. If `message` is given,
# the raised error will be expected to contain at least that content.
private def it_does_not_parse(source, message=nil, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
it %Q(does not parse `#{source}`), file, line, end_line do
exception = expect_raises(ParseError) do
result = parse_program(source)
end
if message
(exception.message || "").downcase.should match(message)
end
end
end
private def test_calls_with_receiver(receiver_source, receiver_node)
it_parses %Q(#{receiver_source}call), Call.new(receiver_node, "call")
it_parses %Q(#{receiver_source}call?), Call.new(receiver_node, "call?")
it_parses %Q(#{receiver_source}call!), Call.new(receiver_node, "call!")
it_parses %Q(#{receiver_source}call()), Call.new(receiver_node, "call")
it_parses %Q(#{receiver_source}call?()), Call.new(receiver_node, "call?")
it_parses %Q(#{receiver_source}call!()), Call.new(receiver_node, "call!")
it_parses %Q(#{receiver_source}call(1)), Call.new(receiver_node, "call", [l(1)])
it_parses %Q(#{receiver_source}call(1, 2 + 3)), Call.new(receiver_node, "call", [l(1), Call.new(l(2), "+", [l(3)], infix: true)])
it_parses %Q(#{receiver_source}call (1)), Call.new(receiver_node, "call", [l(1)])
it_parses %Q(
#{receiver_source}call(
1,
2
)
), Call.new(receiver_node, "call", [l(1), l(2)])
it_parses %Q(
#{receiver_source}call(
)
), Call.new(receiver_node, "call")
# Calls with parameters _must_ wrap them in parentheses.
it_does_not_parse %Q(#{receiver_source}call a, b)
# Blocks can be given to a Call as either brace blocks (`{}`) or `do...end` constructs.
it_parses %Q(#{receiver_source}call{ }), Call.new(receiver_node, "call", block: Block.new)
it_parses %Q(#{receiver_source}call { }), Call.new(receiver_node, "call", block: Block.new)
it_parses %Q(
#{receiver_source}call do
end
), Call.new(receiver_node, "call", block: Block.new)
it_parses %Q(
#{receiver_source}call do
end
), Call.new(receiver_node, "call", block: Block.new)
# The `do...end` syntax can also have a delimiter after the `do` and parameters.
it_parses %Q(#{receiver_source}call do; end), Call.new(receiver_node, "call", block: Block.new)
it_parses %Q(#{receiver_source}call? do; end), Call.new(receiver_node, "call?", block: Block.new)
it_parses %Q(#{receiver_source}call! do; end), Call.new(receiver_node, "call!", block: Block.new)
it_parses %Q(#{receiver_source}call do; end), Call.new(receiver_node, "call", block: Block.new)
it_parses %Q(#{receiver_source}call do |a|; end), Call.new(receiver_node, "call", block: Block.new([p("a")]))
# Brace blocks accept arguments after the opening brace.
it_parses %Q(#{receiver_source}call{ |a,b| }), Call.new(receiver_node, "call", block: Block.new([p("a"), p("b")]))
it_parses %Q(#{receiver_source}call?{ |a,b| }), Call.new(receiver_node, "call?", block: Block.new([p("a"), p("b")]))
it_parses %Q(#{receiver_source}call!{ |a,b| }), Call.new(receiver_node, "call!", block: Block.new([p("a"), p("b")]))
# Block parameters are exactly like normal Def parameters, with the same syntax support.
it_parses %Q(#{receiver_source}call{ | | }), Call.new(receiver_node, "call", block: Block.new())
it_parses %Q(#{receiver_source}call{ |a,*b| }), Call.new(receiver_node, "call", block: Block.new([p("a"), p("b", splat: true)]))
it_parses %Q(#{receiver_source}call{ |1,nil=:thing| }), Call.new(receiver_node, "call", block: Block.new([p(nil, l(1)), p("thing", l(nil))]))
it_parses %Q(#{receiver_source}call{ |a : Integer, b : Nil| }), Call.new(receiver_node, "call", block: Block.new([p("a", restriction: c("Integer")), p("b", restriction: c("Nil"))]))
it_parses %Q(#{receiver_source}call{ |1 =: a : Integer| }), Call.new(receiver_node, "call", block: Block.new([p("a", l(1), restriction: c("Integer"))]))
it_parses %Q(#{receiver_source}call{ |<other>| }), Call.new(receiver_node, "call", block: Block.new([p(nil, i(Call.new(nil, "other")))]))
it_parses %Q(#{receiver_source}call{ |<a.b>| }), Call.new(receiver_node, "call", block: Block.new([p(nil, i(Call.new(Call.new(nil, "a"), "b")))]))
it_parses %Q(#{receiver_source}call{ |<a[0]>| }), Call.new(receiver_node, "call", block: Block.new([p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])))]))
it_parses %Q(#{receiver_source}call{ |*a,b| }), Call.new(receiver_node, "call", block: Block.new([p("a", splat: true), p("b")]))
it_parses %Q(#{receiver_source}call{ |a,*b,c| }), Call.new(receiver_node, "call", block: Block.new([p("a"), p("b", splat: true), p("c")]))
it_parses %Q(#{receiver_source}call{ |a,&block| }), Call.new(receiver_node, "call", block: Block.new([p("a")], block_param: p("block", block: true)))
it_parses %Q(#{receiver_source}call{ |a,&b| }), Call.new(receiver_node, "call", block: Block.new([p("a")], block_param: p("b", block: true)))
it_parses %Q(#{receiver_source}call{ |a,
&b| }), Call.new(receiver_node, "call", block: Block.new([p("a")], block_param: p("b", block: true)))
it_does_not_parse %Q(#{receiver_source}call{ |&b,a| }), /block parameter/
it_does_not_parse %Q(#{receiver_source}call{ |*a,*b| }), /multiple splat/
# `do...end` blocks accept arguments
it_parses %Q(
#{receiver_source}call do | |
end
), Call.new(receiver_node, "call", block: Block.new())
it_parses %Q(
#{receiver_source}call do |a,*b|
end
), Call.new(receiver_node, "call", block: Block.new([p("a"), p("b", splat: true)]))
it_parses %Q(
#{receiver_source}call do |*a,b|
end
), Call.new(receiver_node, "call", block: Block.new([p("a", splat: true), p("b")]))
it_parses %Q(
#{receiver_source}call do |a,*b,c|
end
), Call.new(receiver_node, "call", block: Block.new([p("a"), p("b", splat: true), p("c")]))
it_parses %Q(
#{receiver_source}call do |a,&block|
end
), Call.new(receiver_node, "call", block: Block.new([p("a")], block_param: p("block", block: true)))
it_parses %Q(
#{receiver_source}call do |a,&b|
end
), Call.new(receiver_node, "call", block: Block.new([p("a")], block_param: p("b", block: true)))
it_parses %Q(
#{receiver_source}call do |a,
&b|
end
), Call.new(receiver_node, "call", block: Block.new([p("a")], block_param: p("b", block: true)))
it_does_not_parse %Q(
#{receiver_source}call do |&b,a|
end
), /block parameter/
it_does_not_parse %Q(
#{receiver_source}call do |*a,*b|
end
), /multiple splat/
it_does_not_parse %Q(
#{receiver_source}call{
|arg|
}
)
it_does_not_parse %Q(
#{receiver_source}call do
|arg|
end
)
# Calls with arguments _and_ blocks provide the block after the closing parenthesis.
it_parses %Q(#{receiver_source}call(1, 2){ }), Call.new(receiver_node, "call", [l(1), l(2)], block: Block.new)
it_parses %Q(
#{receiver_source}call(1, 2) do
end
), Call.new(receiver_node, "call", [l(1), l(2)], block: Block.new)
# Calls with blocks that are within other calls can also accept blocks.
it_parses %Q(call(#{receiver_source}inner(1){ })), Call.new(nil, "call", [Call.new(receiver_node, "inner", [l(1)], block: Block.new).as(Node)])
it_parses %Q(
call(#{receiver_source}inner(1) do
end)
), Call.new(nil, "call", [Call.new(receiver_node, "inner", [l(1)], block: Block.new).as(Node)])
it_parses %Q(call(1, #{receiver_source}inner(1){ }, 2)), Call.new(nil, "call", [l(1), Call.new(receiver_node, "inner", [l(1)], block: Block.new), l(2)])
it_parses %Q(
call(1, #{receiver_source}inner(1) do
end, 2)
), Call.new(nil, "call", [l(1), Call.new(receiver_node, "inner", [l(1)], block: Block.new), l(2)])
# Blocks are exactly like normal defs, they can contain any valid Expressions node as a body.
it_parses %Q(#{receiver_source}call{ a = 1; a }), Call.new(receiver_node, "call", block: Block.new(body: e(SimpleAssign.new(v("a"), l(1)), v("a"))))
it_parses %Q(#{receiver_source}call{
a = 1
a
}
), Call.new(receiver_node, "call", block: Block.new(body: e(SimpleAssign.new(v("a"), l(1)), v("a"))))
it_parses %Q(#{receiver_source}call do
a = 1
a
end
), Call.new(receiver_node, "call", block: Block.new(body: e(SimpleAssign.new(v("a"), l(1)), v("a"))))
end
describe "Parser" do
# Empty program
# An empty program should not contain any nodes under the root Expressions.
it_parses %q()
# Literals
it_parses %q(nil), l(nil)
it_parses %q(true), l(true)
it_parses %q(false), l(false)
it_parses %q(1), l(1)
it_parses %q(1_000), l(1000)
it_parses %q(1234567890), l(1234567890)
it_parses %q(1.0), l(1.0)
it_parses %q(123.456), l(123.456)
it_parses %q(1_234.567_89), l(1234.56789)
it_parses %q("hello"), l("hello")
it_parses %q("hello\nworld"), l("hello\nworld")
it_parses %q(""), l("")
it_parses %q(" \t "), l(" \t ")
it_parses %q(:name), l(:name)
it_parses %q(:"hello world"), l(:"hello world")
# Identifiers not previously defined as locals are considered Calls.
it_parses %q(what), Call.new(nil, "what")
it_parses %q(long_identifier), Call.new(nil, "long_identifier")
it_parses %q(ident_with_1234), Call.new(nil, "ident_with_1234")
it_parses %q(_), u("_")
it_parses %q(_named), u("_named")
it_parses %q(_named_longer), u("_named_longer")
it_parses %q(_1234), u("_1234")
it_parses %q(Thing), c("Thing")
it_parses %q(A), c("A")
it_parses %q(ANOTHER), c("ANOTHER")
it_parses %q(UNDER_SCORES), c("UNDER_SCORES")
it_parses %q([]), ListLiteral.new
it_parses %q([call]), l([Call.new(nil, "call")])
it_parses %q([1, 2, 3]), l([1, 2, 3])
it_parses %q([1 , 2, 3]), l([1, 2, 3])
it_parses %q([ 1, 3 ]), l([1, 3])
it_parses %q(
[
100,
2.42,
"hello"
]
), l([100, 2.42, "hello"])
it_parses %q([1, *a, 3]), l([1, Splat.new(Call.new(nil, "a")), 3])
it_parses %q([*a, *b]), l([Splat.new(Call.new(nil, "a")), Splat.new(Call.new(nil, "b"))])
it_parses %q({}), MapLiteral.new
it_parses %q({a: 1, b: 2}), l({ :a => 1, :b => 2 })
it_parses %q({ a: call }), l({ :a => Call.new(nil, "call") })
it_parses %q(
{
something: "hello",
other: 5.4
}
), l({ :something => "hello", :other => 5.4 })
it_parses %q(__FILE__), MagicConst.new(:"__FILE__")
it_parses %q(__LINE__), MagicConst.new(:"__LINE__")
it_parses %q(__DIR__), MagicConst.new(:"__DIR__")
# Value interpolations
# Any literal value is valid in an interpolation.
it_parses %q(<nil>), i(nil)
it_parses %q(<true>), i(true)
it_parses %q(<false>), i(false)
it_parses %q(<1>), i(1)
it_parses %q(<1.5>), i(1.5)
it_parses %q(<"hi">), i("hi")
it_parses %q(<:hello>), i(:hello)
it_parses %q(<:"hi there">), i(:"hi there")
it_parses %q(<[1, 2]>), i([1, 2])
it_parses %q(<[a, *b]>), i(l([Call.new(nil, "a"), Splat.new(Call.new(nil, "b"))]))
it_parses %q(<{a: 1}>), i({:a => 1})
# Interpolations are valid as receivers for Calls
test_calls_with_receiver("<a>.", i(Call.new(nil, "a")))
# Calls, Vars, Consts, Underscores are also valid.
it_parses %q(<a>), i(Call.new(nil, "a"))
it_parses %q(<a?>), i(Call.new(nil, "a?"))
it_parses %q(<a!>), i(Call.new(nil, "a!"))
it_parses %q(<a(1, 2)>), i(Call.new(nil, "a", [l(1), l(2)]))
it_parses %q(<a.b(1)>), i(Call.new(Call.new(nil, "a"), "b", [l(1)]))
it_parses %q(<a.b.c>), i(Call.new(Call.new(Call.new(nil, "a"), "b"), "c"))
it_parses %q(<a{ }>), i(Call.new(nil, "a", block: Block.new))
it_parses %q(<a do; end>), i(Call.new(nil, "a", block: Block.new))
it_parses %q(<Thing>), i(c("Thing"))
it_parses %q(<Thing.Other>), i(Call.new(c("Thing"), "Other"))
it_parses %q(<A.B.C>), i(Call.new(Call.new(c("A"), "B"), "C"))
it_parses %q(<_>), i(u("_"))
it_parses %q(<a[0]>), i(Call.new(Call.new(nil, "a"), "[]", [l(0)]))
it_parses %q(<a.b[0]>), i(Call.new(Call.new(Call.new(nil, "a"), "b"), "[]", [l(0)]))
it_parses %q(<[1, 2][0]>), i(Call.new(l([1, 2]), "[]", [l(0)]))
it_parses %q(<{a: 1}[:a]>), i(Call.new(l({ :a => 1 }), "[]", [l(:a)]))
it_parses %q(<a(1, 2)[0]>), i(Call.new(Call.new(nil, "a", [l(1), l(2)]), "[]", [l(0)]))
# Complex expressions must be wrapped in parentheses.
it_parses %q(<(a)>), i(Call.new(nil, "a"))
it_parses %q(<(1 + 2)>), i(Call.new(l(1), "+", [l(2)], infix: true))
it_does_not_parse %q(<1 + 2>)
it_does_not_parse %q(<a + b>)
it_does_not_parse %q(< a + b >)
# Spacing within the braces is not important
it_parses %q(< a >), i(Call.new(nil, "a"))
it_parses %q(< a[0] >), i(Call.new(Call.new(nil, "a"), "[]", [l(0)]))
# Interpolations can span multiple lines if necessary.
it_parses %q(<
a
>), i(Call.new(nil, "a"))
it_parses %q(<
(1 + 2)
>), i(Call.new(l(1), "+", [l(2)], infix: true))
# Interpolations can also be used as Map keys.
it_parses %q(
{
<1>: "int",
<nil>: :nil
}
), l({ i(1) => "int", i(nil) => :nil })
# Interpolations can be used as a replacement for any primary expression.
it_parses %q([1, <2>, 3]), l([1, i(2), 3])
it_parses %q([1, <a.b>, 3]), l([1, i(Call.new(Call.new(nil, "a"), "b")), 3])
it_parses %q(<a[0]> + 4), Call.new(i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), "+", [l(4)], infix: true)
# String Interpolations
# Strings with interpolations are lexed as single String tokens. The parser
# splits the string into components around `<(...)>` constructs, then parses
# the contents of those constructs and joins them together to form a list of
# String components consisting of StringLiterals and arbitrary Nodes.
# Empty interpolations and string pieces should be removed, and the
# surrounding string pieces should be merged. If the string is otherwise
# empty, a blank string literal is used. If the remainder is a single
# StringLiteral, it is not wrapped in an InterpolatedStringLiteral.
it_parses %q("<()>"), l("")
it_parses %q("hello<()>"), l("hello")
it_parses %q("<()>, world"), l(", world")
it_parses %q("hello<()>, world"), istr(l("hello"), l(", world"))
it_parses %q("<()>hello<()>, world<()>!"), istr(l("hello"), l(", world"), l("!"))
# Simple expressions
it_parses %q("<(a)>"), istr(Call.new(nil, "a"))
it_parses %q("<(nil)>"), istr(l(nil))
it_parses %q("<(true)>"), istr(l(true))
it_parses %q("<(false)>"), istr(l(false))
it_parses %q("<(1)>"), istr(l(1))
it_parses %q("<(1.0)>"), istr(l(1.0))
it_parses %q("<("hi")>"), istr(l("hi"))
it_parses %q("<("")>"), istr(l(""))
it_parses %q("<(:hi)>"), istr(l(:hi))
it_parses %q("<([])>"), istr(ListLiteral.new)
it_parses %q("<({})>"), istr(MapLiteral.new)
# Unterminated string literals and unclosed interpolations are caught and
# handled by the lexer. For example, the code `"<("` will raise a SyntaxError
# before being lexed.
# Spacing within the interpolation is not important
it_parses %q("<( {} )>"), istr(MapLiteral.new)
it_parses %q("<(
{} )>"), istr(MapLiteral.new)
it_parses %q("<(
)>"), l("")
# Arbitrary newlines are also allowed, and are not included in the resulting
# string contents.
it_parses %q("hello<(
""
)>, world"), istr(l("hello"), l(""), l(", world"))
# Local variables are preserved inside the interpolation
it_parses %q(a = 1; "<(a)>"), SimpleAssign.new(v("a"), l(1)), istr(v("a"))
# Complex expressions
it_parses %q("2 is <(1 + 1)>"), istr(l("2 is "), Call.new(l(1), "+", [l(1)], infix: true))
# Nested interpolations
it_parses %q("<( "<(b)>" )>"), istr(istr(Call.new(nil, "b")))
# Maps, brace blocks, and calls with arguments in interpolations are all
# potentially ambiguous.
it_parses %q("<(a.b{ |e| e*2 })>"), istr(Call.new(Call.new(nil, "a"), "b", block: Block.new([p("e")], Call.new(v("e"), "*", [l(2)], infix: true))))
it_parses %q("<(a.b{ |e| "<(e)>" })>"), istr(Call.new(Call.new(nil, "a"), "b", block: Block.new([p("e")], istr(v("e")))))
it_parses %q("<({a: "<(2)>"})>"), istr(l({:a => istr(l(2))}))
it_parses %q("<(a.join(","))>"), istr(Call.new(Call.new(nil, "a"), "join", [l(",")]))
# Multiple interpolations
it_parses %q("hello, <(first_name)> <(last_name)>"), istr(l("hello, "), Call.new(nil, "first_name"), l(" "), Call.new(nil, "last_name"))
it_parses %q("<(first_name)><(last_name)>"), istr(Call.new(nil, "first_name"), Call.new(nil, "last_name"))
it_parses %q("hello, <(first_name)>, or <(other)>"), istr(l("hello, "), Call.new(nil, "first_name"), l(", or "), Call.new(nil, "other"))
# Infix expressions
it_parses %q(1 || 2), Or.new(l(1), l(2))
it_parses %q(1 || 2 || 3), Or.new(l(1), Or.new(l(2), l(3)))
it_parses %q(1 && 2), And.new(l(1), l(2))
it_parses %q(1 && 2 && 3), And.new(l(1), And.new(l(2), l(3)))
it_parses %q(1 == 2), Call.new(l(1), "==", [l(2)], infix: true)
it_parses %q(1 != 2), Call.new(l(1), "!=", [l(2)], infix: true)
it_parses %q(1 < 2), Call.new(l(1), "<", [l(2)], infix: true)
it_parses %q(1 <= 2), Call.new(l(1), "<=", [l(2)], infix: true)
it_parses %q(1 >= 2), Call.new(l(1), ">=", [l(2)], infix: true)
it_parses %q(1 > 2), Call.new(l(1), ">", [l(2)], infix: true)
it_parses %q(1 + 2), Call.new(l(1), "+", [l(2)], infix: true)
it_parses %q(1 - 2), Call.new(l(1), "-", [l(2)], infix: true)
it_parses %q(1 * 2), Call.new(l(1), "*", [l(2)], infix: true)
it_parses %q(1 / 2), Call.new(l(1), "/", [l(2)], infix: true)
it_parses %q(1 % 2), Call.new(l(1), "%", [l(2)], infix: true)
it_parses %q("hello" * 2), Call.new(l("hello"), "*", [l(2)], infix: true)
it_parses %q([1] - [2]), Call.new(l([1]), "-", [l([2])], infix: true)
# Precedence
it_parses %q(1 && 2 || 3), Or.new(And.new(l(1), l(2)), l(3))
it_parses %q(1 || 2 && 3), Or.new(l(1), And.new(l(2), l(3)))
it_parses %q(1 == 2 && 3), And.new(Call.new(l(1), "==", [l(2)], infix: true).as(Node), l(3))
it_parses %q(1 && 2 == 3), And.new(l(1), Call.new(l(2), "==", [l(3)], infix: true))
it_parses %q(1 < 2 == 3), Call.new(Call.new(l(1), "<", [l(2)], infix: true).as(Node), "==", [l(3)], infix: true)
it_parses %q(1 == 2 < 3), Call.new(l(1), "==", [Call.new(l(2), "<", [l(3)], infix: true).as(Node)], infix: true)
it_parses %q(1 + 2 < 3), Call.new(Call.new(l(1), "+", [l(2)], infix: true).as(Node), "<", [l(3)], infix: true)
it_parses %q(1 < 2 + 3), Call.new(l(1), "<", [Call.new(l(2), "+", [l(3)], infix: true).as(Node)], infix: true)
it_parses %q(1 * 2 + 3), Call.new(Call.new(l(1), "*", [l(2)], infix: true).as(Node), "+", [l(3)], infix: true)
it_parses %q(1 + 2 * 3), Call.new(l(1), "+", [Call.new(l(2), "*", [l(3)], infix: true).as(Node)], infix: true)
# Left-associativity for arithmetic expressions
it_parses %q(1 - 1 - 1), Call.new(Call.new(l(1), "-", [l(1)], infix: true), "-", [l(1)], infix: true)
it_parses %q(1 + 1 - 1), Call.new(Call.new(l(1), "+", [l(1)], infix: true), "-", [l(1)], infix: true)
it_parses %q(1 - 1 + 1), Call.new(Call.new(l(1), "-", [l(1)], infix: true), "+", [l(1)], infix: true)
it_parses %q(1 / 1 / 1), Call.new(Call.new(l(1), "/", [l(1)], infix: true), "/", [l(1)], infix: true)
it_parses %q(1 * 1 / 1), Call.new(Call.new(l(1), "*", [l(1)], infix: true), "/", [l(1)], infix: true)
it_parses %q(1 / 1 * 1), Call.new(Call.new(l(1), "/", [l(1)], infix: true), "*", [l(1)], infix: true)
it_parses %q(1 / 1 % 1), Call.new(Call.new(l(1), "/", [l(1)], infix: true), "%", [l(1)], infix: true)
it_parses %q(1 % 1 / 1), Call.new(Call.new(l(1), "%", [l(1)], infix: true), "/", [l(1)], infix: true)
it_parses %q(1 % 1 % 1), Call.new(Call.new(l(1), "%", [l(1)], infix: true), "%", [l(1)], infix: true)
it_parses %q(1 * (2 || 3)), Call.new(l(1), "*", [Or.new(l(2), l(3)).as(Node)], infix: true)
# Ensure Calls can be used as operands to infix expressions
it_parses %q(call + other * last), Call.new(Call.new(nil, "call"), "+", [Call.new(Call.new(nil, "other"), "*", [Call.new(nil, "last").as(Node)], infix: true).as(Node)], infix: true)
# Unary expressions.
# Note: these examples represent valid _syntax_. They may appear semantically
# invalid, but should be accepted by the parser none-the-less.
{% for op in [[:!, Not], [:-, Negation], [:*, Splat]] %}
# Unary expressions are an operator followed by any valid postfix expression.
it_parses %q({{op[0].id}} nil), {{op[1]}}.new(l(nil))
it_parses %q({{op[0].id}}false), {{op[1]}}.new(l(false))
it_parses %q({{op[0].id}}"hello"), {{op[1]}}.new(l("hello"))
it_parses %q({{op[0].id}}[1, 2]), {{op[1]}}.new(l([1, 2]))
it_parses %q({{op[0].id}}{a: 2}), {{op[1]}}.new(l({ :a => 2 }))
it_parses %q({{op[0].id}}:hi), {{op[1]}}.new(l(:hi))
it_parses %q({{op[0].id}}<1.5>), {{op[1]}}.new(i(1.5))
it_parses %q({{op[0].id}}<other>), {{op[1]}}.new(i(Call.new(nil, "other")))
it_parses %q({{op[0].id}}a), {{op[1]}}.new(Call.new(nil, "a"))
it_parses %q({{op[0].id}}(1 + 2)), {{op[1]}}.new(Call.new(l(1), "+", [l(2)], infix: true))
it_parses %q({{op[0].id}}a.b), {{op[1]}}.new(Call.new(Call.new(nil, "a"), "b"))
it_parses %q({{op[0].id}}Thing.b), {{op[1]}}.new(Call.new(c("Thing"), "b"))
it_parses %q(
{{op[0].id}}(
1 + 2
)
), {{op[1]}}.new(Call.new(l(1), "+", [l(2)], infix: true))
# Unary operators can be chained any number of times.
it_parses %q({{op[0].id}}{{op[0].id}}a), {{op[1]}}.new({{op[1]}}.new(Call.new(nil, "a")))
it_parses %q({{op[0].id}}{{op[0].id}}{{op[0].id}}a), {{op[1]}}.new({{op[1]}}.new({{op[1]}}.new(Call.new(nil, "a"))))
# Unary operators are not valid without an argument.
it_does_not_parse %q({{op[0].id}})
# The operand must start on the same line as the operator.
it_does_not_parse %q(
{{op[0].id}}
a
)
# Unary operations are more precedent than binary operations
it_parses %q({{op[0].id}}1 + 2), Call.new({{op[1]}}.new(l(1)), "+", [l(2)], infix: true)
it_parses %q(1 + {{op[0].id}}2), Call.new(l(1), "+", [{{op[1]}}.new(l(2)).as(Node)], infix: true)
# Unary operations can be used anywherea primary expression is expected.
it_parses %q([1, {{op[0].id}}a]), l([1, {{op[1]}}.new(Call.new(nil, "a"))])
{% end %}
# Unary operators can also be mixed when chaining.
it_parses %q(!*-a), Not.new(Splat.new(Negation.new(Call.new(nil, "a"))))
it_parses %q(-*!100), Negation.new(Splat.new(Not.new(l(100))))
it_parses %q(-!*[1,2]), Negation.new(Not.new(Splat.new(l([1, 2]))))
# Unary operators have a higher precedence than any binary operation.
it_parses %q(-1 + -2), Call.new(Negation.new(l(1)), "+", [Negation.new(l(2)).as(Node)], infix: true)
it_parses %q(!1 || !2), Or.new(Not.new(l(1)), Not.new(l(2)).as(Node))
it_parses %q(-1 == -2), Call.new(Negation.new(l(1)), "==", [Negation.new(l(2)).as(Node)], infix: true)
it_parses %q( a = -1), SimpleAssign.new(v("a"), Negation.new(l(1)))
# Simple Assignments
it_parses %q(a = b), SimpleAssign.new(v("a"), Call.new(nil, "b"))
it_parses %q(a = b = c), SimpleAssign.new(v("a"), SimpleAssign.new(v("b"), Call.new(nil, "c")))
# Precedence with logical operations is odd.
# An assignment with a logical operation as an argument considers the logical as higher priority.
it_parses %q(a = 1 && 2), SimpleAssign.new(v("a"), And.new(l(1), l(2)))
it_parses %q(a = 1 || 2), SimpleAssign.new(v("a"), Or.new(l(1), l(2)))
# A logical operation with an assignment as an argument considers the assignment as higher priority.
it_parses %q(1 && b = 2), And.new(l(1), SimpleAssign.new(v("b"), l(2)))
it_parses %q(1 || b = 2), Or.new(l(1), SimpleAssign.new(v("b"), l(2)))
# Assignments take over the remainder of the expression when appearing in a logical operation.
it_parses %q(1 || b = 2 && 3), Or.new(l(1), SimpleAssign.new(v("b"), And.new(l(2), l(3))))
it_parses %q(1 || b = 2 + c = 3 || 4), Or.new(l(1), SimpleAssign.new(v("b"), Call.new(l(2), "+", [SimpleAssign.new(v("c"), Or.new(l(3), l(4))).as(Node)], infix: true)))
# Assignments within parentheses are contained by them.
it_parses %q(1 || (b = 2) && 3), Or.new(l(1), And.new(SimpleAssign.new(v("b"), l(2)), l(3)))
# Once a variable has been assigned, future references to it should be a Var, not a Call.
it_parses %q(
a
a = 2
a
), Call.new(nil, "a"), SimpleAssign.new(v("a"), l(2)), v("a")
# Consts and Underscores can also be the target of an assignment, and they
# should be declared in the current scope.
it_parses %q(THING = 4), SimpleAssign.new(c("THING"), l(4))
it_parses %q(_ = 2), SimpleAssign.new(u("_"), l(2))
# The left hand side may also be a Call expression as long as the Call has a receiver.
it_parses %q(a.b = 1), Call.new(Call.new(nil, "a"), "b=", [l(1)])
it_parses %q(a.b.c = 1), Call.new(Call.new(Call.new(nil, "a"), "b"), "c=", [l(1)])
it_parses %q(a[0] = 1), Call.new(Call.new(nil, "a"), "[]=", [l(0), l(1)])
it_parses %q(a.b = c.d = 1), Call.new(Call.new(nil, "a"), "b=", [Call.new(Call.new(nil, "c"), "d=", [l(1)]).as(Node)])
it_parses %q(a[0] = b[0] = 1), Call.new(Call.new(nil, "a"), "[]=", [l(0), Call.new(Call.new(nil, "b"), "[]=", [l(0), l(1)]).as(Node)])
# Assignments are not allowed to methods with modifiers
it_does_not_parse %q(a.b? = 1)
it_does_not_parse %q(a.b! = 1)
# Assigned Anonymous Functions called should be coerced to a Call
it_parses %q(
foo = fn
->() { }
end
foo()
), SimpleAssign.new(v("foo"), AnonymousFunction.new([Block.new])), Call.new(nil, "foo")
it_parses %q(
foo = fn
->() { }
end
bar = foo
bar()
), SimpleAssign.new(v("foo"), AnonymousFunction.new([Block.new])), SimpleAssign.new(v("bar"), v("foo")), Call.new(nil, "bar")
# Assignments can not be made to literal values.
it_does_not_parse %q(2 = 4), /cannot assign to literal value/i
it_does_not_parse %q(2.56 = 4), /cannot assign to literal value/i
it_does_not_parse %q("hi" = 4), /cannot assign to literal value/i
it_does_not_parse %q(nil = 4), /cannot assign to literal value/i
it_does_not_parse %q(false = true), /cannot assign to literal value/i
it_does_not_parse %q([1, 2, 3] = 4), /cannot assign to literal value/i
# Match Assignments
# Match assignments allow literal values on either side
it_parses %q(1 =: 1), MatchAssign.new(l(1), l(1))
it_parses %q(:hi =: "hi"), MatchAssign.new(l(:hi), l("hi"))
it_parses %q(true =: false), MatchAssign.new(l(true), l(false))
it_parses %q([1, 2] =: [1, 2]), MatchAssign.new(l([1, 2]), l([1, 2]))
it_parses %q({a: 2} =: {a: 2}), MatchAssign.new(l({:a => 2}), l({:a => 2}))
# Splats in list literals act as Splat collectors (as in Params).
it_parses %q([1, *_, 3] =: list), MatchAssign.new(l([1, Splat.new(u("_")), 3]), Call.new(nil, "list"))
it_parses %q([1, *a, 3] =: list), MatchAssign.new(l([1, Splat.new(v("a")), 3]), Call.new(nil, "list"))
# Only one Splat is allowed in a List pattern.
it_does_not_parse %q([*a, *b] =: [1, 2])
it_does_not_parse %q([1, *a, 2, *b] =: [1, 2])
# Multiple Splats can be used if they are in different List patterns.
it_parses %q([[*a, 2], [3, *d]] =: list), MatchAssign.new(l([[Splat.new(v("a")), 2], [3, Splat.new(v("d"))]]), Call.new(nil, "list"))
# Vars, Consts, and Underscores can also be used on either side.
it_parses %q(a =: 5), MatchAssign.new(v("a"), l(5))
it_parses %q(Thing =: 10), MatchAssign.new(c("Thing"), l(10))
it_parses %q(_ =: 15), MatchAssign.new(u("_"), l(15))
# Value Interpolations are also allowed on either side for complex patterns/values.
it_parses %q(<a> =: <b>), MatchAssign.new(i(Call.new(nil, "a")), i(Call.new(nil, "b")))
it_parses %q(<a.b> =: <c.d>), MatchAssign.new(i(Call.new(Call.new(nil, "a"), "b")), i(Call.new(Call.new(nil, "c"), "d")))
it_parses %q(<a[0]> =: <b[0]>), MatchAssign.new(i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), i(Call.new(Call.new(nil, "b"), "[]", [l(0)])))
# Bare multiple assignment is not allowed. Use a List pattern instead.
it_does_not_parse %q(a, b =: [1, 2])
# The value of a match assignment may appear on a new line.
it_parses %q(
a =:
4
), MatchAssign.new(v("a"), l(4))
# Patterns can be arbitrarily nested.
it_parses %q(
[1, {
a: a,
b: b
}, 4] =:
thing
), MatchAssign.new(l([1, { :a => v("a"), :b => v("b") }, 4]), Call.new(nil, "thing"))
# Matches can be chained with other matches, as well as simple assignments.
it_parses %q(
a = 3 =: b
), SimpleAssign.new(v("a"), MatchAssign.new(l(3), Call.new(nil, "b")))
it_parses %q(
3 =: a = b
), MatchAssign.new(l(3), SimpleAssign.new(v("a"), Call.new(nil, "b")))
it_parses %q(
3 =: a =: 3
), MatchAssign.new(l(3), MatchAssign.new(v("a"), l(3)))
# Operational Assignments
# Most binary operations can be concatenated with an assignment to form an
# Operational Assignment. These are a syntactic shorthand for and operation
# and assignment on the same variable, i.e., `a op= b` is equivalent to
# writing `a = a op b`.
{% for op in ["+=", "-=", "*=", "/=", "%=", "||=", "&&="] %}
# When the left-hand-side is an identifier, treat it as a Var.
it_parses %q(a {{op.id}} 1), OpAssign.new(v("a"), {{op}}, l(1))
it_parses %q(a {{op.id}} a {{op.id}} 1), OpAssign.new(v("a"), {{op}}, OpAssign.new(v("a"), {{op}}, l(1)))
it_parses %q(a {{op.id}} 1 + 2), OpAssign.new(v("a"), {{op}}, Call.new(l(1), "+", [l(2)], infix: true))
it_parses %q(a {{op.id}} Thing.member), OpAssign.new(v("a"), {{op}}, Call.new(c("Thing"), "member"))
# The left-hand-side can also be any simple Call
it_parses %q(a.b {{op.id}} 1), OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, l(1))
it_parses %q(a.b {{op.id}} a.b {{op.id}} 1), OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, l(1)))
it_parses %q(a.b {{op.id}} 1 + 2), OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, Call.new(l(1), "+", [l(2)], infix: true))
it_parses %q(a.b {{op.id}} Thing.member), OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, Call.new(c("Thing"), "member"))
it_parses %q(a[0] {{op.id}} 1), OpAssign.new(Call.new(Call.new(nil, "a"), "[]", [l(0)]), {{op}}, l(1))
it_parses %q(a[0] {{op.id}} a[0] {{op.id}} 1), OpAssign.new(Call.new(Call.new(nil, "a"), "[]", [l(0)]), {{op}}, OpAssign.new(Call.new(Call.new(nil, "a"), "[]", [l(0)]), {{op}}, l(1)))
it_parses %q(a[0] {{op.id}} 1 + 2), OpAssign.new(Call.new(Call.new(nil, "a"), "[]", [l(0)]), {{op}}, Call.new(l(1), "+", [l(2)], infix: true))
it_parses %q(a[0] {{op.id}} Thing.member), OpAssign.new(Call.new(Call.new(nil, "a"), "[]", [l(0)]), {{op}}, Call.new(c("Thing"), "member"))
# As an infix expression, the value can appear on a new line
it_parses %q(
a.b {{op.id}}
1 + 2
), OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, Call.new(l(1), "+", [l(2)], infix: true))
it_parses %q(
a.b {{op.id}} (1 +
2
)
), OpAssign.new(Call.new(Call.new(nil, "a"), "b"), {{op}}, Call.new(l(1), "+", [l(2)], infix: true))
# The left-hand-side must be an assignable value (i.e., not a literal)
it_does_not_parse %q(1 {{op.id}} 2)
it_does_not_parse %q(nil {{op.id}} 2)
it_does_not_parse %q([1, 2] {{op.id}} 2)
# No left-hand-side is also invalid
it_does_not_parse %q({{op.id}} 2)
{% end %}
# Element access
# The List notation `[...]` is used on any object to access specific
# elements within it.
it_parses %q(list[1]), Call.new(Call.new(nil, "list"), "[]", [l(1)])
it_parses %q(list[a]), Call.new(Call.new(nil, "list"), "[]", [Call.new(nil, "a").as(Node)])
it_parses %q(list[Thing]), Call.new(Call.new(nil, "list"), "[]", [c("Thing").as(Node)])
it_parses %q(list[1 + 2]), Call.new(Call.new(nil, "list"), "[]", [Call.new(l(1), "+", [l(2)], infix: true).as(Node)])
it_parses %q(list[a = 1]), Call.new(Call.new(nil, "list"), "[]", [SimpleAssign.new(v("a"), l(1)).as(Node)])
it_parses %q(list[a = 1]), Call.new(Call.new(nil, "list"), "[]", [SimpleAssign.new(v("a"), l(1)).as(Node)])
it_parses %q(list["hi"]), Call.new(Call.new(nil, "list"), "[]", [l("hi")])
it_parses %q(list[:hello]), Call.new(Call.new(nil, "list"), "[]", [l(:hello)])
# Accesses can accept any number of arguments, of any type.
it_parses %q(list[1, 2]), Call.new(Call.new(nil, "list"), "[]", [l(1), l(2)])
it_parses %q(list[nil, false]), Call.new(Call.new(nil, "list"), "[]", [l(nil), l(false)])
# The receiver can be any expression.
it_parses %q((1 + 2)[0]), Call.new(Call.new(l(1), "+", [l(2)], infix: true), "[]", [l(0)])
it_parses %q((a = 1)[0]), Call.new(SimpleAssign.new(v("a"), l(1)), "[]", [l(0)])
it_parses %q(false[0]), Call.new(l(false), "[]", [l(0)])
it_parses %q("hello"[0]), Call.new(l("hello"), "[]", [l(0)])
it_parses %q([1, 2][0]), Call.new(l([1, 2]), "[]", [l(0)])
it_parses %q({a: 1}[0]), Call.new(l({ :a => 1 }), "[]", [l(0)])
it_parses %q(a.b[0]), Call.new(Call.new(Call.new(nil, "a"), "b"), "[]", [l(0)])
it_parses %q(Thing.a[0]), Call.new(Call.new(c("Thing"), "a"), "[]", [l(0)])
it_parses %q(a(1, 2)[0]), Call.new(Call.new(nil, "a", [l(1), l(2)]), "[]", [l(0)])
it_parses %q(
map{ }[0]
), Call.new(Call.new(nil, "map", block: Block.new), "[]", [l(0)])
it_parses %q(
map do
end[0]
), Call.new(Call.new(nil, "map", block: Block.new), "[]", [l(0)])
# Accesses must start on the same line, but can span multiple after the opening brace.
it_parses %q(
list[
1,
2
]
), Call.new(Call.new(nil, "list"), "[]", [l(1), l(2)])
it_parses %q(
list[
1 + 2
]
), Call.new(Call.new(nil, "list"), "[]", [Call.new(l(1), "+", [l(2)], infix: true).as(Node)])
it_parses %q(
list
[1, 2]
), Call.new(nil, "list"), l([1, 2])
it_parses %q(
[1, 2]
[0]
), l([1, 2]), l([0])
# Accesses can also be chained together
it_parses %q(list[1][2]), Call.new(Call.new(Call.new(nil, "list"), "[]", [l(1)]), "[]", [l(2)])
# The List notation must have at least one argument to be valid
it_does_not_parse %q(list[])
# Expression delimiters
# Newlines can be used to delimit complete expressions
it_parses %q(
a = 1
a + 2
), SimpleAssign.new(v("a"), l(1)), Call.new(v("a"), "+", [l(2)], infix: true)
it_parses %q(
nil
[4, 5]
), l(nil), l([4, 5])
# Semicolons can also be used to place multiple expressions on a single line
it_parses %q(
a = 1; a + 2;
b = 2;
), SimpleAssign.new(v("a"), l(1)), Call.new(v("a"), "+", [l(2)], infix: true), SimpleAssign.new(v("b"), l(2))
# Without the semicolon, a syntax error should occur
it_does_not_parse %q(a = 1 b = 2)
# Expression with operators must include the operator on the first line, but
# the rest of the expression may flow to multiple lines.
it_parses %q(
a =
[
1,
2
]
), SimpleAssign.new(v("a"), l([1, 2]))
it_parses %q(
var1 +
var2
), Call.new(Call.new(nil, "var1"), "+", [Call.new(nil, "var2").as(Node)], infix: true)
it_does_not_parse %q(
var1
+ var2
)
# Method definitions
it_parses %q(
def foo
end
), Def.new("foo") # `@body` will be a Nop
# Semicolons can be used as delimiters to compact the definition.
it_parses %q(def foo; end), Def.new("foo")
# Any identifier is valid as a definition name
it_parses %q(def foo_; end), Def.new("foo_")
it_parses %q(def _foo; end), Def.new("_foo")
it_parses %q(def foo?; end), Def.new("foo?")
it_parses %q(def foo!; end), Def.new("foo!")
it_parses %q(def foo_!; end), Def.new("foo_!")
it_parses %q(def foo_?; end), Def.new("foo_?")
# Identifiers that already exist as local variables cannot be used as function names.
it_does_not_parse %q(foo = 1; def foo; end), /collides/
# Variables outside of the current scope do not count as collisions
it_parses %q(
foo = 1
defmodule Bar
def foo; end
end
)
# `=` can also be appended to any non-modified identifier.
it_parses %q(def foo=; end), Def.new("foo=")
it_parses %q(def foo_=(); end), Def.new("foo_=")
it_parses %q(def _foo=(); end), Def.new("_foo=")
# Multiple modifiers are not allowed
it_does_not_parse %q(def foo?=; end)
it_does_not_parse %q(def foo!=; end)
it_parses %q(
def foo(a, b)
end
)
it_parses %q(def foo(); end), Def.new("foo")
it_parses %q(def foo(a); end), Def.new("foo", [p("a")])
it_parses %q(def foo(a, b); end), Def.new("foo", [p("a"), p("b")])
it_parses %q(def foo(_, _second); end), Def.new("foo", [p("_"), p("_second")])
it_parses %q(
def foo
1 + 2
end
), Def.new("foo", body: e(Call.new(l(1), "+", [l(2)], infix: true)))
it_parses %q(
def foo
a = 1
a * 4
end
), Def.new("foo", body: e(SimpleAssign.new(v("a"), l(1)), Call.new(v("a"), "*", [l(4)], infix: true)))
# A Splat collector can appear anywhere in the param list
it_parses %q(def foo(*a); end), Def.new("foo", [p("a", splat: true)], splat_index: 0)
it_parses %q(def foo(*a, b); end), Def.new("foo", [p("a", splat: true), p("b")], splat_index: 0)
it_parses %q(def foo(a, *b); end), Def.new("foo", [p("a"), p("b", splat: true)], splat_index: 1)
it_parses %q(def foo(a, *b, c); end), Def.new("foo", [p("a"), p("b", splat: true), p("c")], splat_index: 1)
# Multiple splat collectors are not allowed in the param list
it_does_not_parse %q(def foo(*a, *b); end), /multiple splat parameters/
# A Block parameter must be the last parameter in the param list
it_parses %q(def foo(&block); end), Def.new("foo", block_param: p("block", block: true))
it_parses %q(def foo(a, &block); end), Def.new("foo", [p("a")], block_param: p("block", block: true))
it_parses %q(def foo(a, *b, &block); end), Def.new("foo", [p("a"), p("b", splat: true)], block_param: p("block", block: true), splat_index: 1)
# The block parameter may also be given any name
it_parses %q(def foo(a, &b); end), Def.new("foo", [p("a")], block_param: p("b", block: true))
it_parses %q(def foo(a, &_); end), Def.new("foo", [p("a")], block_param: p("_", block: true))
it_does_not_parse %q(def foo(&block, a); end), /block parameter/
it_does_not_parse %q(def foo(&block1, &block2); end), /block parameter/
it_does_not_parse %q(def foo(a, &block, c); end), /block parameter/
it_parses %q(
def foo; end
def foo; end
), Def.new("foo"), Def.new("foo")
# References to variables defined as parameters should be considered Vars,
# not Calls. This also applies to the block parameter.
it_parses %q(def foo(a); a; end), Def.new("foo", [p("a")], e(v("a")))
it_parses %q(def foo(&block); block; end), Def.new("foo", block_param: p("block", block: true), body: e(v("block")))
# The block can be forced into a Call with parentheses, like any other local variable.
it_parses %q(def foo(&block); block(); end), Def.new("foo", block_param: p("block", block: true), body: e(Call.new(nil, "block")))
# The Vars defined within the Def should be removed after the Def finishes.
it_parses %q(def foo(a); end; a), Def.new("foo", [p("a")]), Call.new(nil, "a")
# Defs allow patterns as parameters
it_parses %q(def foo(nil); end), Def.new("foo", [p(nil, l(nil))])
it_parses %q(def foo(1, 2); end), Def.new("foo", [p(nil, l(1)), p(nil, l(2))])
it_parses %q(def foo([1, a]); end), Def.new("foo", [p(nil, l([1, v("a")]))])
it_parses %q(def foo({a: 1, b: b}); end), Def.new("foo", [p(nil, l({ :a => 1, :b => v("b") }))])
# Patterns can also be followed by a name to capture the entire argument.
it_parses %q(def foo([1, a] =: b); end), Def.new("foo", [p("b", l([1, v("a")]))])
it_parses %q(def foo([1, _] =: _); end), Def.new("foo", [p("_", l([1, u("_")]))])
it_parses %q(def foo(<other> =: _); end), Def.new("foo", [p("_", i(Call.new(nil, "other")))])
it_parses %q(def foo(<a.b> =: _); end), Def.new("foo", [p("_", i(Call.new(Call.new(nil, "a"), "b")))])
it_parses %q(def foo(<a[0]> =: _); end), Def.new("foo", [p("_", i(Call.new(Call.new(nil, "a"), "[]", [l(0)])))])
# Splats within patterns are allowed.
it_parses %q(def foo([1, *_, 3]); end), Def.new("foo", [p(nil, l([1, Splat.new(u("_")), 3]))])
# Type restrictions can be appended to any parameter to restrict the parameter
# to an exact type. The type must be a constant.
# Simple names
it_parses %q(def foo(a : Integer); end), Def.new("foo", [p("a", restriction: c("Integer"))])
it_parses %q(def foo(a : Nil); end), Def.new("foo", [p("a", restriction: c("Nil"))])
it_parses %q(def foo(a : Thing); end), Def.new("foo", [p("a", restriction: c("Thing"))])
it_does_not_parse %q(def foo(a : 123); end)
it_does_not_parse %q(def foo(a : nil); end)
it_does_not_parse %q(def foo(a : [1, 2]); end)
it_does_not_parse %q(def foo(a : b); end)
it_does_not_parse %q(def foo(a : 1 + 2); end)
it_does_not_parse %q(def foo(a : <thing>); end)
it_does_not_parse %q(def foo(a : (A + B)); end)
# Simple patterns
it_parses %q(def foo(1 : Integer); end), Def.new("foo", [p(nil, l(1), restriction: c("Integer"))])
it_parses %q(def foo(1 : Nil); end), Def.new("foo", [p(nil, l(1), restriction: c("Nil"))])
it_parses %q(def foo(1 : Thing); end), Def.new("foo", [p(nil, l(1), restriction: c("Thing"))])
it_parses %q(def foo(nil : Integer); end), Def.new("foo", [p(nil, l(nil), restriction: c("Integer"))])
it_parses %q(def foo(nil : Nil); end), Def.new("foo", [p(nil, l(nil), restriction: c("Nil"))])
it_parses %q(def foo(nil : Thing); end), Def.new("foo", [p(nil, l(nil), restriction: c("Thing"))])
it_parses %q(def foo(<call> : Integer); end), Def.new("foo", [p(nil, i(Call.new(nil, "call")), restriction: c("Integer"))])
it_parses %q(def foo(<call> : Nil); end), Def.new("foo", [p(nil, i(Call.new(nil, "call")), restriction: c("Nil"))])
it_parses %q(def foo(<call> : Thing); end), Def.new("foo", [p(nil, i(Call.new(nil, "call")), restriction: c("Thing"))])
it_parses %q(def foo(<a.b> : Integer); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "b")), restriction: c("Integer"))])
it_parses %q(def foo(<a.b> : Nil); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "b")), restriction: c("Nil"))])
it_parses %q(def foo(<a.b> : Thing); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "b")), restriction: c("Thing"))])
it_parses %q(def foo(<a[0]> : Integer); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), restriction: c("Integer"))])
it_parses %q(def foo(<a[0]> : Nil); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), restriction: c("Nil"))])
it_parses %q(def foo(<a[0]> : Thing); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), restriction: c("Thing"))])
it_parses %q(def foo([1, 2] : Integer); end), Def.new("foo", [p(nil, l([1, 2]), restriction: c("Integer"))])
it_parses %q(def foo([1, 2] : Nil); end), Def.new("foo", [p(nil, l([1, 2]), restriction: c("Nil"))])
it_parses %q(def foo([1, 2] : Thing); end), Def.new("foo", [p(nil, l([1, 2]), restriction: c("Thing"))])
# Patterns and names
it_parses %q(def foo(1 =: a : Integer); end), Def.new("foo", [p("a", l(1), restriction: c("Integer"))])
it_parses %q(def foo(1 =: a : Nil); end), Def.new("foo", [p("a", l(1), restriction: c("Nil"))])
it_parses %q(def foo(1 =: a : Thing); end), Def.new("foo", [p("a", l(1), restriction: c("Thing"))])
it_parses %q(def foo(nil =: a : Integer); end), Def.new("foo", [p("a", l(nil), restriction: c("Integer"))])
it_parses %q(def foo(nil =: a : Nil); end), Def.new("foo", [p("a", l(nil), restriction: c("Nil"))])
it_parses %q(def foo(nil =: a : Thing); end), Def.new("foo", [p("a", l(nil), restriction: c("Thing"))])
it_parses %q(def foo(<call> =: a : Integer); end), Def.new("foo", [p("a", i(Call.new(nil, "call")), restriction: c("Integer"))])
it_parses %q(def foo(<call> =: a : Nil); end), Def.new("foo", [p("a", i(Call.new(nil, "call")), restriction: c("Nil"))])
it_parses %q(def foo(<call> =: a : Thing); end), Def.new("foo", [p("a", i(Call.new(nil, "call")), restriction: c("Thing"))])
it_parses %q(def foo(<a.b> : Integer); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "b")), restriction: c("Integer"))])
it_parses %q(def foo(<a.b> : Nil); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "b")), restriction: c("Nil"))])
it_parses %q(def foo(<a.b> : Thing); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "b")), restriction: c("Thing"))])
it_parses %q(def foo(<a[0]> : Integer); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), restriction: c("Integer"))])
it_parses %q(def foo(<a[0]> : Nil); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), restriction: c("Nil"))])
it_parses %q(def foo(<a[0]> : Thing); end), Def.new("foo", [p(nil, i(Call.new(Call.new(nil, "a"), "[]", [l(0)])), restriction: c("Thing"))])
it_parses %q(def foo([1, 2] =: a : Integer); end), Def.new("foo", [p("a", l([1, 2]), restriction: c("Integer"))])
it_parses %q(def foo([1, 2] =: a : Nil); end), Def.new("foo", [p("a", l([1, 2]), restriction: c("Nil"))])
it_parses %q(def foo([1, 2] =: a : Thing); end), Def.new("foo", [p("a", l([1, 2]), restriction: c("Thing"))])
# Only the top level parameters may have retrictions.
it_does_not_parse %q(def foo([1, a : List]); end)
it_does_not_parse %q(def foo([1, _ : List]); end)
it_does_not_parse %q(def foo([1, [a, b] : List]); end)
it_does_not_parse %q(def foo([1, a : List] =: c); end)
it_does_not_parse %q(def foo([1, _ : List] =: c); end)
it_does_not_parse %q(def foo([1, [a, b] : List] =: c); end)
# Block and Splat parameters may not have restrictions
it_does_not_parse %q(def foo(*a : List); end)
it_does_not_parse %q(def foo(&block : Block); end)
# All components of a parameter must appear inline with the previous component
it_does_not_parse %q(
def foo(a :
List)
)
it_does_not_parse %q(
def foo(a
: List)
)
it_does_not_parse %q(
def foo(<(1+2)> =:
a)
)
it_does_not_parse %q(
def foo(<(1+2)>
=: a)
)
# Individual components of a parameter _may_ span multiple lines, but should
# avoid it where possible.
it_parses %q(
def foo(<(1 +
2)> =: a : Integer); end
), Def.new("foo", [p("a", i(Call.new(l(1), "+", [l(2)], infix: true)), restriction: c("Integer"))])
# Parameters may each appear on their own line for clarity
it_parses %q(
def foo(
[1, _] =: list : List,
nil,
b : Integer
)
end
), Def.new("foo", [p("list", l([1, u("_")]), restriction: c("List")), p(nil, l(nil)), p("b", restriction: c("Integer"))])
# Some operators are also allowed as method names for overloading.
[
"+", "-", "*", "/", "%", "[]", "[]=",
"<", "<=", "!=", "==", ">=", ">"
].each do |op|
it_parses %Q(def #{op}; end), Def.new(op)
it_parses %Q(def #{op}(); end), Def.new(op)
it_parses %Q(def #{op}(other); end), Def.new(op, [p("other")])
it_parses %Q(def #{op}(a, b); end), Def.new(op, [p("a"), p("b")])
end
# Module definitions
it_parses %q(
defmodule Foo
end
), ModuleDef.new("Foo")
it_parses %q(defmodule Foo; end), ModuleDef.new("Foo")
# Modules must specify a Constant as their name
it_does_not_parse %q(defmodule foo; end)
it_does_not_parse %q(defmodule _nope; end)
it_parses %q(
defmodule Foo
def foo; end
end
), ModuleDef.new("Foo", e(Def.new("foo")))
# Modules allow immediate code evaluation on their scope.
it_parses %q(
defmodule Foo
1 + 2
a = 3
end
), ModuleDef.new("Foo", e(Call.new(l(1), "+", [l(2)], infix: true), SimpleAssign.new(v("a"), l(3))))
# Modules can also be nested
it_parses %q(
defmodule Foo
defmodule Bar
end
end
), ModuleDef.new("Foo", e(ModuleDef.new("Bar")))
# Type definitions