-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathparser_spec.cr
1939 lines (1585 loc) · 132 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 "../../support/syntax"
private def regex(string, options = Regex::Options::None)
RegexLiteral.new(StringLiteral.new(string), options)
end
private def it_parses(string, expected_node, file = __FILE__, line = __LINE__)
it "parses #{string.dump}", file, line do
parser = Parser.new(string)
parser.filename = "/foo/bar/baz.cr"
node = parser.parse
# If it's an Array, map it all to ASTNode (the array might be of a
# union that's not exactly ASTNode). Not having to write `[...] of ASTNode`
# simplifies testing a bit.
local_expected_node = expected_node
if local_expected_node.is_a?(Array)
local_expected_node = local_expected_node.map(&.as(ASTNode))
end
node.should eq(Expressions.from(local_expected_node))
end
end
private def assert_end_location(source, line_number = 1, column_number = source.size, file = __FILE__, line = __LINE__)
it "gets corrects end location for #{source.inspect}", file, line do
parser = Parser.new("#{source}; 1")
node = parser.parse.as(Expressions).expressions[0]
end_loc = node.end_location.not_nil!
end_loc.line_number.should eq(line_number)
end_loc.column_number.should eq(column_number)
end
end
module Crystal
describe "Parser" do
it_parses "nil", NilLiteral.new
it_parses "true", true.bool
it_parses "false", false.bool
it_parses "1", 1.int32
it_parses "+1", 1.int32
it_parses "-1", -1.int32
it_parses "1_i64", 1.int64
it_parses "+1_i64", 1.int64
it_parses "-1_i64", -1.int64
it_parses "1_u128", 1.uint128
it_parses "1_i128", 1.int128
it_parses "1.0", 1.0.float64
it_parses "+1.0", 1.0.float64
it_parses "-1.0", -1.0.float64
it_parses "1.0_f32", "1.0".float32
it_parses "+1.0_f32", "+1.0".float32
it_parses "-1.0_f32", "-1.0".float32
it_parses "2.3_f32", 2.3.float32
it_parses "'a'", CharLiteral.new('a')
it_parses %("foo"), "foo".string
it_parses %(""), "".string
it_parses %("hello \\\n world"), "hello world".string
it_parses %(%Q{hello \\n world}), "hello \n world".string
it_parses %(%q{hello \\n world}), "hello \\n world".string
it_parses %(%q{hello \#{foo} world}), "hello \#{foo} world".string
[":foo", ":foo!", ":foo?", ":\"foo\"", ":かたな", ":+", ":-", ":*", ":/", ":==", ":<", ":<=", ":>",
":>=", ":!", ":!=", ":=~", ":!~", ":&", ":|", ":^", ":~", ":**", ":>>", ":<<", ":%", ":[]", ":[]?",
":[]=", ":<=>", ":==="].each do |symbol|
value = symbol[1, symbol.size - 1]
value = value[1, value.size - 2] if value.starts_with?('"')
it_parses symbol, value.symbol
end
it_parses ":foo", "foo".symbol
it_parses ":[]=", "[]=".symbol
it_parses ":[]?", "[]?".symbol
it_parses %(:"\\\\foo"), "\\foo".symbol
it_parses %(:"\\\"foo"), "\"foo".symbol
it_parses %(:"\\\"foo\\\""), "\"foo\"".symbol
it_parses %(:"\\a\\b\\n\\r\\t\\v\\f\\e"), "\a\b\n\r\t\v\f\e".symbol
it_parses %(:"\\u{61}"), "a".symbol
it_parses "[1, 2]", ([1.int32, 2.int32] of ASTNode).array
it_parses "[\n1, 2]", ([1.int32, 2.int32] of ASTNode).array
it_parses "[1,\n 2,]", ([1.int32, 2.int32] of ASTNode).array
it_parses "1 + 2", Call.new(1.int32, "+", 2.int32)
it_parses "1 +\n2", Call.new(1.int32, "+", 2.int32)
it_parses "1 +2", Call.new(1.int32, "+", 2.int32)
it_parses "1 -2", Call.new(1.int32, "-", 2.int32)
it_parses "1 +2.0", Call.new(1.int32, "+", 2.float64)
it_parses "1 -2.0", Call.new(1.int32, "-", 2.float64)
it_parses "1 +2_i64", Call.new(1.int32, "+", 2.int64)
it_parses "1 -2_i64", Call.new(1.int32, "-", 2.int64)
it_parses "1\n+2", [1.int32, 2.int32] of ASTNode
it_parses "1;+2", [1.int32, 2.int32] of ASTNode
it_parses "1 - 2", Call.new(1.int32, "-", 2.int32)
it_parses "1 -\n2", Call.new(1.int32, "-", 2.int32)
it_parses "1\n-2", [1.int32, -2.int32] of ASTNode
it_parses "1;-2", [1.int32, -2.int32] of ASTNode
it_parses "1 * 2", Call.new(1.int32, "*", 2.int32)
it_parses "1 * -2", Call.new(1.int32, "*", -2.int32)
it_parses "2 * 3 + 4 * 5", Call.new(Call.new(2.int32, "*", 3.int32), "+", Call.new(4.int32, "*", 5.int32))
it_parses "1 / 2", Call.new(1.int32, "/", 2.int32)
it_parses "1 / -2", Call.new(1.int32, "/", -2.int32)
it_parses "2 / 3 + 4 / 5", Call.new(Call.new(2.int32, "/", 3.int32), "+", Call.new(4.int32, "/", 5.int32))
it_parses "2 * (3 + 4)", Call.new(2.int32, "*", Expressions.new([Call.new(3.int32, "+", 4.int32)] of ASTNode))
it_parses "1/2", Call.new(1.int32, "/", [2.int32] of ASTNode)
it_parses "1 + /foo/", Call.new(1.int32, "+", regex("foo"))
it_parses "1+0", Call.new(1.int32, "+", 0.int32)
it_parses "a = 1; a /b", [Assign.new("a".var, 1.int32), Call.new("a".var, "/", "b".call)]
it_parses "a = 1; a/b", [Assign.new("a".var, 1.int32), Call.new("a".var, "/", "b".call)]
it_parses "a = 1; (a)/b", [Assign.new("a".var, 1.int32), Call.new(Expressions.new(["a".var] of ASTNode), "/", "b".call)]
it_parses "_ = 1", Assign.new(Underscore.new, 1.int32)
it_parses "@foo/2", Call.new("@foo".instance_var, "/", 2.int32)
it_parses "@@foo/2", Call.new("@@foo".class_var, "/", 2.int32)
it_parses "1+2*3", Call.new(1.int32, "+", Call.new(2.int32, "*", 3.int32))
it_parses "foo[] /2", Call.new(Call.new("foo".call, "[]"), "/", 2.int32)
it_parses "foo[1] /2", Call.new(Call.new("foo".call, "[]", 1.int32), "/", 2.int32)
it_parses "[1] /2", Call.new(([1.int32] of ASTNode).array, "/", 2.int32)
it_parses "!1", Not.new(1.int32)
it_parses "- 1", Call.new(1.int32, "-")
it_parses "+ 1", Call.new(1.int32, "+")
it_parses "~ 1", Call.new(1.int32, "~")
it_parses "1.~", Call.new(1.int32, "~")
it_parses "1.!", Not.new(1.int32)
it_parses "1 && 2", And.new(1.int32, 2.int32)
it_parses "1 || 2", Or.new(1.int32, 2.int32)
it_parses "&- 1", Call.new(1.int32, "&-")
it_parses "&+ 1", Call.new(1.int32, "&+")
it_parses "1 <=> 2", Call.new(1.int32, "<=>", 2.int32)
it_parses "1 !~ 2", Call.new(1.int32, "!~", 2.int32)
it_parses "a = 1", Assign.new("a".var, 1.int32)
it_parses "a = b = 2", Assign.new("a".var, Assign.new("b".var, 2.int32))
it_parses "a, b = 1, 2", MultiAssign.new(["a".var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "a, b = 1", MultiAssign.new(["a".var, "b".var] of ASTNode, [1.int32] of ASTNode)
it_parses "_, _ = 1, 2", MultiAssign.new([Underscore.new, Underscore.new] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "a[0], a[1] = 1, 2", MultiAssign.new([Call.new("a".call, "[]", 0.int32), Call.new("a".call, "[]", 1.int32)] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "a.foo, a.bar = 1, 2", MultiAssign.new([Call.new("a".call, "foo"), Call.new("a".call, "bar")] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "x = 0; a, b = x += 1", [Assign.new("x".var, 0.int32), MultiAssign.new(["a".var, "b".var] of ASTNode, [OpAssign.new("x".var, "+", 1.int32)] of ASTNode)] of ASTNode
it_parses "a, b = 1, 2 if 3", If.new(3.int32, MultiAssign.new(["a".var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode))
it_parses "@a, b = 1, 2", MultiAssign.new(["@a".instance_var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "@@a, b = 1, 2", MultiAssign.new(["@@a".class_var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode)
assert_syntax_error "b? = 1", "unexpected token: ="
assert_syntax_error "b! = 1", "unexpected token: ="
assert_syntax_error "a, B = 1, 2", "can't assign to constant in multiple assignment"
assert_syntax_error "1 == 2, a = 4"
assert_syntax_error "x : String, a = 4"
assert_syntax_error "b, 1 == 2, a = 4"
assert_syntax_error "a = 1, 2, 3", "Multiple assignment count mismatch"
assert_syntax_error "a = 1, b = 2", "Multiple assignment count mismatch"
it_parses "def foo\n1\nend", Def.new("foo", body: 1.int32)
it_parses "def downto(n)\n1\nend", Def.new("downto", ["n".arg], 1.int32)
it_parses "def foo ; 1 ; end", Def.new("foo", body: 1.int32)
it_parses "def foo; end", Def.new("foo")
it_parses "def foo(var); end", Def.new("foo", ["var".arg])
it_parses "def foo(\nvar); end", Def.new("foo", ["var".arg])
it_parses "def foo(\nvar\n); end", Def.new("foo", ["var".arg])
it_parses "def foo(var1, var2); end", Def.new("foo", ["var1".arg, "var2".arg])
it_parses "def foo; 1; 2; end", Def.new("foo", body: [1.int32, 2.int32] of ASTNode)
it_parses "def foo=(value); end", Def.new("foo=", ["value".arg])
it_parses "def foo(n); foo(n -1); end", Def.new("foo", ["n".arg], "foo".call(Call.new("n".var, "-", 1.int32)))
it_parses "def type(type); end", Def.new("type", ["type".arg])
# #4815
assert_syntax_error "def foo!=; end", "unexpected token: !="
assert_syntax_error "def foo?=(x); end", "unexpected token: ?"
# #5856
assert_syntax_error "def foo=(a,b); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(a = 1, b = 2); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(*args); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(**kwargs); end", "setter method 'foo=' cannot have more than one argument"
assert_syntax_error "def foo=(&block); end", "setter method 'foo=' cannot have a block"
assert_syntax_error "def []=(&block); end", "setter method '[]=' cannot have a block"
assert_syntax_error "f.[]= do |a| end", "setter method '[]=' cannot be called with a block"
assert_syntax_error "f.[]= { |bar| }", "setter method '[]=' cannot be called with a block"
# #5895, #6042, #5997
%w(
begin nil true false yield with abstract
def macro require case select if unless include
extend class struct module enum while until return
next break lib fun alias pointerof sizeof
instance_sizeof offsetof typeof private protected asm out
end
).each do |kw|
assert_syntax_error "def foo(#{kw}); end", "cannot use '#{kw}' as an argument name", 1, 9
assert_syntax_error "def foo(foo #{kw}); end", "cannot use '#{kw}' as an argument name", 1, 13
it_parses "def foo(#{kw} foo); end", Def.new("foo", [Arg.new("foo", external_name: kw.to_s)])
it_parses "def foo(@#{kw}); end", Def.new("foo", [Arg.new("__arg0", external_name: kw.to_s)], [Assign.new("@#{kw}".instance_var, "__arg0".var)] of ASTNode)
it_parses "def foo(@@#{kw}); end", Def.new("foo", [Arg.new("__arg0", external_name: kw.to_s)], [Assign.new("@@#{kw}".class_var, "__arg0".var)] of ASTNode)
end
it_parses "def self.foo\n1\nend", Def.new("foo", body: 1.int32, receiver: "self".var)
it_parses "def self.foo()\n1\nend", Def.new("foo", body: 1.int32, receiver: "self".var)
it_parses "def self.foo=\n1\nend", Def.new("foo=", body: 1.int32, receiver: "self".var)
it_parses "def self.foo=()\n1\nend", Def.new("foo=", body: 1.int32, receiver: "self".var)
it_parses "def Foo.foo\n1\nend", Def.new("foo", body: 1.int32, receiver: "Foo".path)
it_parses "def Foo::Bar.foo\n1\nend", Def.new("foo", body: 1.int32, receiver: ["Foo", "Bar"].path)
it_parses "def foo; a; end", Def.new("foo", body: "a".call)
it_parses "def foo(a); a; end", Def.new("foo", ["a".arg], "a".var)
it_parses "def foo; a = 1; a; end", Def.new("foo", body: [Assign.new("a".var, 1.int32), "a".var] of ASTNode)
it_parses "def foo; a = 1; a {}; end", Def.new("foo", body: [Assign.new("a".var, 1.int32), Call.new(nil, "a", block: Block.new)] of ASTNode)
it_parses "def foo; a = 1; x { a }; end", Def.new("foo", body: [Assign.new("a".var, 1.int32), Call.new(nil, "x", block: Block.new(body: ["a".var] of ASTNode))] of ASTNode)
it_parses "def foo; x { |a| a }; end", Def.new("foo", body: [Call.new(nil, "x", block: Block.new(["a".var], ["a".var] of ASTNode))] of ASTNode)
it_parses "def foo; x { |_| 1 }; end", Def.new("foo", body: [Call.new(nil, "x", block: Block.new(["_".var], [1.int32] of ASTNode))] of ASTNode)
it_parses "def foo; x { |a, *b| b }; end", Def.new("foo", body: [Call.new(nil, "x", block: Block.new(["a".var, "b".var], ["b".var] of ASTNode, splat_index: 1))] of ASTNode)
assert_syntax_error "x { |*a, *b| }", "splat block argument already specified"
it_parses "def foo(var = 1); end", Def.new("foo", [Arg.new("var", 1.int32)])
it_parses "def foo(var : Int); end", Def.new("foo", [Arg.new("var", restriction: "Int".path)])
it_parses "def foo(var : self); end", Def.new("foo", [Arg.new("var", restriction: Self.new)])
it_parses "def foo(var : self?); end", Def.new("foo", [Arg.new("var", restriction: Crystal::Union.new([Self.new, Path.global("Nil")] of ASTNode))])
it_parses "def foo(var : self.class); end", Def.new("foo", [Arg.new("var", restriction: Metaclass.new(Self.new))])
it_parses "def foo(var : self*); end", Def.new("foo", [Arg.new("var", restriction: Self.new.pointer_of)])
it_parses "def foo(var : Int | Double); end", Def.new("foo", [Arg.new("var", restriction: Crystal::Union.new(["Int".path, "Double".path] of ASTNode))])
it_parses "def foo(var : Int?); end", Def.new("foo", [Arg.new("var", restriction: Crystal::Union.new(["Int".path, "Nil".path(true)] of ASTNode))])
it_parses "def foo(var : Int*); end", Def.new("foo", [Arg.new("var", restriction: "Int".path.pointer_of)])
it_parses "def foo(var : Int**); end", Def.new("foo", [Arg.new("var", restriction: "Int".path.pointer_of.pointer_of)])
it_parses "def foo(var : Int -> Double); end", Def.new("foo", [Arg.new("var", restriction: ProcNotation.new(["Int".path] of ASTNode, "Double".path))])
it_parses "def foo(var : Int, Float -> Double); end", Def.new("foo", [Arg.new("var", restriction: ProcNotation.new(["Int".path, "Float".path] of ASTNode, "Double".path))])
it_parses "def foo(var : (Int, Float -> Double)); end", Def.new("foo", [Arg.new("var", restriction: ProcNotation.new(["Int".path, "Float".path] of ASTNode, "Double".path))])
it_parses "def foo(var : (Int, Float) -> Double); end", Def.new("foo", [Arg.new("var", restriction: ProcNotation.new(["Int".path, "Float".path] of ASTNode, "Double".path))])
it_parses "def foo(var : Char[256]); end", Def.new("foo", [Arg.new("var", restriction: "Char".static_array_of(256))])
it_parses "def foo(var : Char[N]); end", Def.new("foo", [Arg.new("var", restriction: "Char".static_array_of("N".path))])
it_parses "def foo(var : Int32 = 1); end", Def.new("foo", [Arg.new("var", 1.int32, "Int32".path)])
it_parses "def foo(var : Int32 -> = 1); end", Def.new("foo", [Arg.new("var", 1.int32, ProcNotation.new(["Int32".path] of ASTNode))])
it_parses "def foo; yield; end", Def.new("foo", body: Yield.new, yields: 0)
it_parses "def foo; yield 1; end", Def.new("foo", body: Yield.new([1.int32] of ASTNode), yields: 1)
it_parses "def foo; yield 1; yield; end", Def.new("foo", body: [Yield.new([1.int32] of ASTNode), Yield.new] of ASTNode, yields: 1)
it_parses "def foo(a, b = a); end", Def.new("foo", [Arg.new("a"), Arg.new("b", "a".var)])
it_parses "def foo(&block); end", Def.new("foo", block_arg: Arg.new("block"), yields: 0)
it_parses "def foo(&); end", Def.new("foo", block_arg: Arg.new(""), yields: 0)
it_parses "def foo(&\n); end", Def.new("foo", block_arg: Arg.new(""), yields: 0)
it_parses "def foo(a, &block); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block"), yields: 0)
it_parses "def foo(a, &block : Int -> Double); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path] of ASTNode, "Double".path)), yields: 1)
it_parses "def foo(a, & : Int -> Double); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("", restriction: ProcNotation.new(["Int".path] of ASTNode, "Double".path)), yields: 1)
it_parses "def foo(a, &block : Int, Float -> Double); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path, "Float".path] of ASTNode, "Double".path)), yields: 2)
it_parses "def foo(a, &block : Int, self -> Double); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path, Self.new] of ASTNode, "Double".path)), yields: 2)
it_parses "def foo(a, &block : -> Double); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new(nil, "Double".path)), yields: 0)
it_parses "def foo(a, &block : Int -> ); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path] of ASTNode)), yields: 1)
it_parses "def foo(a, &block : self -> self); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new([Self.new] of ASTNode, Self.new)), yields: 1)
it_parses "def foo(a, &block : Foo); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: Path.new("Foo")), yields: 0)
it_parses "def foo; with a yield; end", Def.new("foo", body: Yield.new(scope: "a".call), yields: 1)
it_parses "def foo; with a yield 1; end", Def.new("foo", body: Yield.new([1.int32] of ASTNode, "a".call), yields: 1)
it_parses "def foo; a = 1; with a yield a; end", Def.new("foo", body: [Assign.new("a".var, 1.int32), Yield.new(["a".var] of ASTNode, "a".var)] of ASTNode, yields: 1)
it_parses "def foo(@var); end", Def.new("foo", [Arg.new("var")], [Assign.new("@var".instance_var, "var".var)] of ASTNode)
it_parses "def foo(@var); 1; end", Def.new("foo", [Arg.new("var")], [Assign.new("@var".instance_var, "var".var), 1.int32] of ASTNode)
it_parses "def foo(@var = 1); 1; end", Def.new("foo", [Arg.new("var", 1.int32)], [Assign.new("@var".instance_var, "var".var), 1.int32] of ASTNode)
it_parses "def foo(@@var); end", Def.new("foo", [Arg.new("var")], [Assign.new("@@var".class_var, "var".var)] of ASTNode)
it_parses "def foo(@@var); 1; end", Def.new("foo", [Arg.new("var")], [Assign.new("@@var".class_var, "var".var), 1.int32] of ASTNode)
it_parses "def foo(@@var = 1); 1; end", Def.new("foo", [Arg.new("var", 1.int32)], [Assign.new("@@var".class_var, "var".var), 1.int32] of ASTNode)
it_parses "def foo(&@block); end", Def.new("foo", body: Assign.new("@block".instance_var, "block".var), block_arg: Arg.new("block"), yields: 0)
it_parses "def foo(\n&block\n); end", Def.new("foo", block_arg: Arg.new("block"), yields: 0)
it_parses "def foo(&block :\n Int ->); end", Def.new("foo", block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path] of ASTNode)), yields: 1)
it_parses "def foo(&block : Int ->\n); end", Def.new("foo", block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path] of ASTNode)), yields: 1)
it_parses "def foo(a, &block : *Int -> ); end", Def.new("foo", [Arg.new("a")], block_arg: Arg.new("block", restriction: ProcNotation.new(["Int".path.splat] of ASTNode)), yields: 1)
it_parses "def foo(x, *args, y = 2); 1; end", Def.new("foo", args: ["x".arg, "args".arg, Arg.new("y", default_value: 2.int32)], body: 1.int32, splat_index: 1)
it_parses "def foo(x, *args, y = 2, w, z = 3); 1; end", Def.new("foo", args: ["x".arg, "args".arg, Arg.new("y", default_value: 2.int32), "w".arg, Arg.new("z", default_value: 3.int32)], body: 1.int32, splat_index: 1)
it_parses "def foo(x, *, y); 1; end", Def.new("foo", args: ["x".arg, "".arg, "y".arg], body: 1.int32, splat_index: 1)
assert_syntax_error "def foo(x, *); 1; end", "named arguments must follow bare *"
assert_syntax_error "def foo(var = 1 : Int32); end", "the syntax for an argument with a default value V and type T is `arg : T = V`"
assert_syntax_error "def foo(var = x : Int); end", "the syntax for an argument with a default value V and type T is `arg : T = V`"
it_parses "def foo(**args)\n1\nend", Def.new("foo", body: 1.int32, double_splat: "args".arg)
it_parses "def foo(x, **args)\n1\nend", Def.new("foo", body: 1.int32, args: ["x".arg], double_splat: "args".arg)
it_parses "def foo(x, **args, &block)\n1\nend", Def.new("foo", body: 1.int32, args: ["x".arg], double_splat: "args".arg, block_arg: "block".arg, yields: 0)
it_parses "def foo(**args)\nargs\nend", Def.new("foo", body: "args".var, double_splat: "args".arg)
it_parses "def foo(x = 1, **args)\n1\nend", Def.new("foo", body: 1.int32, args: [Arg.new("x", default_value: 1.int32)], double_splat: "args".arg)
it_parses "def foo(**args : Foo)\n1\nend", Def.new("foo", body: 1.int32, double_splat: Arg.new("args", restriction: "Foo".path))
it_parses "def foo(**args : **Foo)\n1\nend", Def.new("foo", body: 1.int32, double_splat: Arg.new("args", restriction: DoubleSplat.new("Foo".path)))
assert_syntax_error "def foo(**args, **args2); end", "only block argument is allowed after double splat"
assert_syntax_error "def foo(**args, x); end", "only block argument is allowed after double splat"
assert_syntax_error "def foo(**args, *x); end", "only block argument is allowed after double splat"
it_parses "def foo(x y); y; end", Def.new("foo", args: [Arg.new("y", external_name: "x")], body: "y".var)
it_parses "def foo(x @var); end", Def.new("foo", [Arg.new("var", external_name: "x")], [Assign.new("@var".instance_var, "var".var)] of ASTNode)
it_parses "def foo(x @@var); end", Def.new("foo", [Arg.new("var", external_name: "x")], [Assign.new("@@var".class_var, "var".var)] of ASTNode)
assert_syntax_error "def foo(_ y); y; end"
it_parses %(def foo("bar qux" y); y; end), Def.new("foo", args: [Arg.new("y", external_name: "bar qux")], body: "y".var)
assert_syntax_error "def foo(x x); 1; end", "when specified, external name must be different than internal name"
assert_syntax_error "def foo(x @x); 1; end", "when specified, external name must be different than internal name"
assert_syntax_error "def foo(x @@x); 1; end", "when specified, external name must be different than internal name"
assert_syntax_error "def foo(*a foo); end"
assert_syntax_error "def foo(**a foo); end"
assert_syntax_error "def foo(&a foo); end"
it_parses "macro foo(**args)\n1\nend", Macro.new("foo", body: MacroLiteral.new("1\n"), double_splat: "args".arg)
assert_syntax_error "macro foo(x, *); 1; end", "named arguments must follow bare *"
assert_syntax_error "macro foo(**x, **y)", "only block argument is allowed after double splat"
assert_syntax_error "macro foo(**x, y)", "only block argument is allowed after double splat"
it_parses "abstract def foo", Def.new("foo", abstract: true)
it_parses "abstract def foo; 1", [Def.new("foo", abstract: true), 1.int32]
it_parses "abstract def foo\n1", [Def.new("foo", abstract: true), 1.int32]
it_parses "abstract def foo(x)", Def.new("foo", ["x".arg], abstract: true)
assert_syntax_error "def foo var; end", "parentheses are mandatory for def arguments"
assert_syntax_error "def foo var\n end", "parentheses are mandatory for def arguments"
assert_syntax_error "def foo &block ; end", "parentheses are mandatory for def arguments"
assert_syntax_error "def foo &block : Int -> Double ; end", "parentheses are mandatory for def arguments"
assert_syntax_error "def foo @var, █ end", "parentheses are mandatory for def arguments"
assert_syntax_error "def foo @@var, █ end", "parentheses are mandatory for def arguments"
assert_syntax_error "def foo *y; 1; end", "parentheses are mandatory for def arguments"
it_parses "def foo(x : U) forall U; end", Def.new("foo", args: [Arg.new("x", restriction: "U".path)], free_vars: %w(U))
it_parses "def foo(x : U) forall T, U; end", Def.new("foo", args: [Arg.new("x", restriction: "U".path)], free_vars: %w(T U))
it_parses "def foo(x : U) : Int32 forall T, U; end", Def.new("foo", args: [Arg.new("x", restriction: "U".path)], return_type: "Int32".path, free_vars: %w(T U))
assert_syntax_error "def foo(x : U) forall; end"
assert_syntax_error "def foo(x : U) forall U,; end"
it_parses "foo", "foo".call
it_parses "foo()", "foo".call
it_parses "foo(1)", "foo".call(1.int32)
it_parses "foo 1", "foo".call(1.int32)
it_parses "foo 1\n", "foo".call(1.int32)
it_parses "foo 1;", "foo".call(1.int32)
it_parses "foo 1, 2", "foo".call(1.int32, 2.int32)
it_parses "foo (1 + 2), 3", "foo".call(Expressions.new([Call.new(1.int32, "+", 2.int32)] of ASTNode), 3.int32)
it_parses "foo(1 + 2)", "foo".call(Call.new(1.int32, "+", 2.int32))
it_parses "foo -1.0, -2.0", "foo".call(-1.float64, -2.float64)
it_parses "foo(\n1)", "foo".call(1.int32)
it_parses "::foo", Call.new(nil, "foo", [] of ASTNode, nil, nil, nil, true)
it_parses "foo + 1", Call.new("foo".call, "+", 1.int32)
it_parses "foo +1", Call.new(nil, "foo", 1.int32)
it_parses "foo +1.0", Call.new(nil, "foo", 1.float64)
it_parses "foo +1_i64", Call.new(nil, "foo", 1.int64)
it_parses "foo = 1; foo +1", [Assign.new("foo".var, 1.int32), Call.new("foo".var, "+", 1.int32)]
it_parses "foo = 1; foo(+1)", [Assign.new("foo".var, 1.int32), Call.new(nil, "foo", 1.int32)]
it_parses "foo = 1; foo -1", [Assign.new("foo".var, 1.int32), Call.new("foo".var, "-", 1.int32)]
it_parses "foo = 1; foo(-1)", [Assign.new("foo".var, 1.int32), Call.new(nil, "foo", -1.int32)]
it_parses "foo(&block)", Call.new(nil, "foo", block_arg: "block".call)
it_parses "foo &block", Call.new(nil, "foo", block_arg: "block".call)
it_parses "a.foo &block", Call.new("a".call, "foo", block_arg: "block".call)
it_parses "a.foo(&block)", Call.new("a".call, "foo", block_arg: "block".call)
it_parses "foo(&.block)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "block")))
it_parses "foo &.block", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "block")))
it_parses "foo &./(1)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "/", 1.int32)))
it_parses "foo &.%(1)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "%", 1.int32)))
it_parses "foo &.block(1)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "block", 1.int32)))
it_parses "foo &.+(2)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "+", 2.int32)))
it_parses "foo &.bar.baz", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Call.new(Var.new("__arg0"), "bar"), "baz")))
it_parses "foo(&.bar.baz)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Call.new(Var.new("__arg0"), "bar"), "baz")))
it_parses "foo &.block[0]", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Call.new(Var.new("__arg0"), "block"), "[]", 0.int32)))
it_parses "foo &.block=(0)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "block=", 0.int32)))
it_parses "foo &.block = 0", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "block=", 0.int32)))
it_parses "foo &.block[0] = 1", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Call.new(Var.new("__arg0"), "block"), "[]=", 0.int32, 1.int32)))
it_parses "foo &.[0]", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "[]", 0.int32)))
it_parses "foo &.[0] = 1", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "[]=", 0.int32, 1.int32)))
it_parses "foo(&.is_a?(T))", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], IsA.new(Var.new("__arg0"), "T".path)))
it_parses "foo(&.!)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Not.new(Var.new("__arg0"))))
it_parses "foo(&.responds_to?(:foo))", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], RespondsTo.new(Var.new("__arg0"), "foo")))
it_parses "foo &.each {\n}", Call.new(nil, "foo", block: Block.new(["__arg0".var], Call.new("__arg0".var, "each", block: Block.new)))
it_parses "foo &.each do\nend", Call.new(nil, "foo", block: Block.new(["__arg0".var], Call.new("__arg0".var, "each", block: Block.new)))
it_parses "foo &.@bar", Call.new(nil, "foo", block: Block.new(["__arg0".var], ReadInstanceVar.new("__arg0".var, "@bar")))
it_parses "foo(&.as(T))", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Cast.new(Var.new("__arg0"), "T".path)))
it_parses "foo(&.as(T).bar)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Cast.new(Var.new("__arg0"), "T".path), "bar")))
it_parses "foo &.as(T)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Cast.new(Var.new("__arg0"), "T".path)))
it_parses "foo &.as(T).bar", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Cast.new(Var.new("__arg0"), "T".path), "bar")))
it_parses "foo(&.as?(T))", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], NilableCast.new(Var.new("__arg0"), "T".path)))
it_parses "foo(&.as?(T).bar)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(NilableCast.new(Var.new("__arg0"), "T".path), "bar")))
it_parses "foo &.as?(T)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], NilableCast.new(Var.new("__arg0"), "T".path)))
it_parses "foo &.as?(T).bar", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(NilableCast.new(Var.new("__arg0"), "T".path), "bar")))
it_parses "foo(\n &.block\n)", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Call.new(Var.new("__arg0"), "block")))
it_parses "foo.[0]", Call.new("foo".call, "[]", 0.int32)
it_parses "foo.[0] = 1", Call.new("foo".call, "[]=", [0.int32, 1.int32] of ASTNode)
it_parses "foo(a: 1, b: 2)", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "foo(1, a: 1, b: 2)", Call.new(nil, "foo", [1.int32] of ASTNode, named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "foo a: 1, b: 2", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "foo 1, a: 1, b: 2", Call.new(nil, "foo", [1.int32] of ASTNode, named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "foo 1, a: 1, b: 2\n1", [Call.new(nil, "foo", [1.int32] of ASTNode, named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)]), 1.int32]
it_parses "foo(a: 1\n)", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)])
it_parses "foo(\na: 1,\n)", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)])
it_parses %(foo("foo bar": 1, "baz": 2)), Call.new(nil, "foo", named_args: [NamedArgument.new("foo bar", 1.int32), NamedArgument.new("baz", 2.int32)])
it_parses %(foo "foo bar": 1, "baz": 2), Call.new(nil, "foo", named_args: [NamedArgument.new("foo bar", 1.int32), NamedArgument.new("baz", 2.int32)])
it_parses "x.foo(a: 1, b: 2)", Call.new("x".call, "foo", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "x.foo a: 1, b: 2 ", Call.new("x".call, "foo", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "x[a: 1, b: 2]", Call.new("x".call, "[]", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "x[a: 1, b: 2,]", Call.new("x".call, "[]", named_args: [NamedArgument.new("a", 1.int32), NamedArgument.new("b", 2.int32)])
it_parses "x[{1}]", Call.new("x".call, "[]", TupleLiteral.new([1.int32] of ASTNode))
it_parses "x[+ 1]", Call.new("x".call, "[]", Call.new(1.int32, "+"))
it_parses "foo(a: 1, &block)", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)], block_arg: "block".call)
it_parses "foo a: 1, &block", Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)], block_arg: "block".call)
it_parses "foo a: b(1) do\nend", Call.new(nil, "foo", named_args: [NamedArgument.new("a", Call.new(nil, "b", 1.int32))], block: Block.new)
it_parses "Foo.bar x.y do\nend", Call.new("Foo".path, "bar", args: [Call.new("x".call, "y")] of ASTNode, block: Block.new)
it_parses "x = 1; foo x do\nend", [Assign.new("x".var, 1.int32), Call.new(nil, "foo", ["x".var] of ASTNode, Block.new)]
it_parses "x = 1; foo x { }", [Assign.new("x".var, 1.int32), Call.new(nil, "foo", [Call.new(nil, "x", block: Block.new)] of ASTNode)]
it_parses "x = 1; foo x {\n}", [Assign.new("x".var, 1.int32), Call.new(nil, "foo", [Call.new(nil, "x", block: Block.new)] of ASTNode)]
it_parses "foo x do\nend", Call.new(nil, "foo", ["x".call] of ASTNode, Block.new)
it_parses "foo x, y do\nend", Call.new(nil, "foo", ["x".call, "y".call] of ASTNode, Block.new)
it_parses "1.x; foo do\nend", [Call.new(1.int32, "x"), Call.new(nil, "foo", block: Block.new)] of ASTNode
it_parses "x = 1; foo.bar x do\nend", [Assign.new("x".var, 1.int32), Call.new("foo".call, "bar", ["x".var] of ASTNode, Block.new)]
it_parses "foo do\n//\nend", Call.new(nil, "foo", [] of ASTNode, Block.new(body: regex("")))
it_parses "foo x do\n//\nend", Call.new(nil, "foo", ["x".call] of ASTNode, Block.new(body: regex("")))
it_parses "foo(x) do\n//\nend", Call.new(nil, "foo", ["x".call] of ASTNode, Block.new(body: regex("")))
it_parses "foo !false", Call.new(nil, "foo", [Not.new(false.bool)] of ASTNode)
it_parses "!a && b", And.new(Not.new("a".call), "b".call)
it_parses "foo.bar.baz", Call.new(Call.new("foo".call, "bar"), "baz")
it_parses "f.x Foo.new", Call.new("f".call, "x", [Call.new("Foo".path, "new")] of ASTNode)
it_parses "f.x = Foo.new", Call.new("f".call, "x=", [Call.new("Foo".path, "new")] of ASTNode)
it_parses "f.x = - 1", Call.new("f".call, "x=", [Call.new(1.int32, "-")] of ASTNode)
["+", "-", "*", "/", "//", "%", "|", "&", "^", "**", "<<", ">>", "&+", "&-", "&*"].each do |op|
it_parses "f.x #{op}= 2", OpAssign.new(Call.new("f".call, "x"), op, 2.int32)
end
["/", "<", "<=", "==", "!=", "=~", "!~", ">", ">=", "+", "-", "*", "/", "~", "%", "&", "|", "^", "**", "==="].each do |op|
it_parses "def #{op}; end;", Def.new(op)
end
it_parses "def %(); end;", Def.new("%")
it_parses "def /(); end;", Def.new("/")
["<<", "<", "<=", "==", ">>", ">", ">=", "+", "-", "*", "/", "//", "%", "|", "&", "^", "**", "===", "=~", "!~", "&+", "&-", "&*", "&**"].each do |op|
it_parses "1 #{op} 2", Call.new(1.int32, op, 2.int32)
it_parses "n #{op} 2", Call.new("n".call, op, 2.int32)
it_parses "foo(n #{op} 2)", Call.new(nil, "foo", Call.new("n".call, op, 2.int32))
it_parses "foo(0, n #{op} 2)", Call.new(nil, "foo", 0.int32, Call.new("n".call, op, 2.int32))
it_parses "foo(a: n #{op} 2)", Call.new(nil, "foo", [] of ASTNode, named_args: [NamedArgument.new("a", Call.new("n".call, op, 2.int32))])
it_parses "foo(z: 0, a: n #{op} 2)", Call.new(nil, "foo", [] of ASTNode, named_args: [NamedArgument.new("z", 0.int32), NamedArgument.new("a", Call.new("n".call, op, 2.int32))])
it_parses "def #{op}(); end", Def.new(op)
it_parses "macro #{op};end", Macro.new(op, [] of Arg, Expressions.new)
it_parses "foo = 1; ->foo.#{op}(Int32)", [Assign.new("foo".var, 1.int32), ProcPointer.new("foo".var, op, ["Int32".path] of ASTNode)]
it_parses "->Foo.#{op}(Int32)", ProcPointer.new("Foo".path, op, ["Int32".path] of ASTNode)
end
["[]", "[]="].each do |op|
it_parses "foo = 1; ->foo.#{op}(Int32)", [Assign.new("foo".var, 1.int32), ProcPointer.new("foo".var, op, ["Int32".path] of ASTNode)]
it_parses "->Foo.#{op}(Int32)", ProcPointer.new("Foo".path, op, ["Int32".path] of ASTNode)
end
["bar", "+", "-", "*", "/", "<", "<=", "==", ">", ">=", "%", "|", "&", "^", "**", "===", "=~", "!~"].each do |name|
it_parses "foo.#{name}", Call.new("foo".call, name)
it_parses "foo.#{name} 1, 2", Call.new("foo".call, name, 1.int32, 2.int32)
it_parses "foo.#{name}(1, 2)", Call.new("foo".call, name, 1.int32, 2.int32)
end
["+", "-", "*", "/", "//", "%", "|", "&", "^", "**", "<<", ">>", "&+", "&-", "&*"].each do |op|
it_parses "a = 1; a #{op}= 1", [Assign.new("a".var, 1.int32), OpAssign.new("a".var, op, 1.int32)]
it_parses "a = 1; a #{op}=\n1", [Assign.new("a".var, 1.int32), OpAssign.new("a".var, op, 1.int32)]
it_parses "a.b #{op}=\n1", OpAssign.new(Call.new("a".call, "b"), op, 1.int32)
end
it_parses "a = 1; a &&= 1", [Assign.new("a".var, 1.int32), OpAssign.new("a".var, "&&", 1.int32)]
it_parses "a = 1; a ||= 1", [Assign.new("a".var, 1.int32), OpAssign.new("a".var, "||", 1.int32)]
it_parses "a = 1; a[2] &&= 3", [Assign.new("a".var, 1.int32), OpAssign.new(Call.new("a".var, "[]", 2.int32), "&&", 3.int32)]
it_parses "a = 1; a[2] ||= 3", [Assign.new("a".var, 1.int32), OpAssign.new(Call.new("a".var, "[]", 2.int32), "||", 3.int32)]
it_parses "if foo; 1; end", If.new("foo".call, 1.int32)
it_parses "if foo\n1\nend", If.new("foo".call, 1.int32)
it_parses "if foo; 1; else; 2; end", If.new("foo".call, 1.int32, 2.int32)
it_parses "if foo\n1\nelse\n2\nend", If.new("foo".call, 1.int32, 2.int32)
it_parses "if foo; 1; elsif bar; 2; else 3; end", If.new("foo".call, 1.int32, If.new("bar".call, 2.int32, 3.int32))
it_parses "include Foo", Include.new("Foo".path)
it_parses "include Foo\nif true; end", [Include.new("Foo".path), If.new(true.bool)]
it_parses "extend Foo", Extend.new("Foo".path)
it_parses "extend Foo\nif true; end", [Extend.new("Foo".path), If.new(true.bool)]
it_parses "extend self", Extend.new(Self.new)
it_parses "unless foo; 1; end", Unless.new("foo".call, 1.int32)
it_parses "unless foo; 1; else; 2; end", Unless.new("foo".call, 1.int32, 2.int32)
it_parses "class Foo; end", ClassDef.new("Foo".path)
it_parses "class Foo\nend", ClassDef.new("Foo".path)
it_parses "class Foo\ndef foo; end; end", ClassDef.new("Foo".path, [Def.new("foo")] of ASTNode)
it_parses "class Foo < Bar; end", ClassDef.new("Foo".path, superclass: "Bar".path)
it_parses "class Foo(T); end", ClassDef.new("Foo".path, type_vars: ["T"])
it_parses "class Foo(T1); end", ClassDef.new("Foo".path, type_vars: ["T1"])
it_parses "class Foo(Type); end", ClassDef.new("Foo".path, type_vars: ["Type"])
it_parses "abstract class Foo; end", ClassDef.new("Foo".path, abstract: true)
it_parses "abstract struct Foo; end", ClassDef.new("Foo".path, abstract: true, struct: true)
it_parses "class Foo < self; end", ClassDef.new("Foo".path, superclass: Self.new)
it_parses "module Foo(*T); end", ModuleDef.new("Foo".path, type_vars: ["T"], splat_index: 0)
it_parses "class Foo(*T); end", ClassDef.new("Foo".path, type_vars: ["T"], splat_index: 0)
it_parses "class Foo(T, *U); end", ClassDef.new("Foo".path, type_vars: ["T", "U"], splat_index: 1)
assert_syntax_error "class Foo(*T, *U); end", "splat type argument already specified"
it_parses "x : Foo(A, *B, C)", TypeDeclaration.new("x".var, Generic.new("Foo".path, ["A".path, "B".path.splat, "C".path] of ASTNode))
it_parses "x : *T -> R", TypeDeclaration.new("x".var, ProcNotation.new(["T".path.splat] of ASTNode, "R".path))
it_parses "def foo(x : *T -> R); end", Def.new("foo", args: [Arg.new("x", restriction: ProcNotation.new(["T".path.splat] of ASTNode, "R".path))])
it_parses "struct Foo; end", ClassDef.new("Foo".path, struct: true)
it_parses "Foo(T)", Generic.new("Foo".path, ["T".path] of ASTNode)
it_parses "Foo(T | U)", Generic.new("Foo".path, [Crystal::Union.new(["T".path, "U".path] of ASTNode)] of ASTNode)
it_parses "Foo(Bar(T | U))", Generic.new("Foo".path, [Generic.new("Bar".path, [Crystal::Union.new(["T".path, "U".path] of ASTNode)] of ASTNode)] of ASTNode)
it_parses "Foo(T?)", Generic.new("Foo".path, [Crystal::Union.new(["T".path, Path.global("Nil")] of ASTNode)] of ASTNode)
it_parses "Foo(1)", Generic.new("Foo".path, [1.int32] of ASTNode)
it_parses "Foo(T, 1)", Generic.new("Foo".path, ["T".path, 1.int32] of ASTNode)
it_parses "Foo(T, U, 1)", Generic.new("Foo".path, ["T".path, "U".path, 1.int32] of ASTNode)
it_parses "Foo(T, 1, U)", Generic.new("Foo".path, ["T".path, 1.int32, "U".path] of ASTNode)
it_parses "Foo(typeof(1))", Generic.new("Foo".path, [TypeOf.new([1.int32] of ASTNode)] of ASTNode)
it_parses "Foo(typeof(1), typeof(2))", Generic.new("Foo".path, [TypeOf.new([1.int32] of ASTNode), TypeOf.new([2.int32] of ASTNode)] of ASTNode)
it_parses "Foo({X, Y})", Generic.new("Foo".path, [Generic.new(Path.global("Tuple"), ["X".path, "Y".path] of ASTNode)] of ASTNode)
it_parses "Foo({X, Y,})", Generic.new("Foo".path, [Generic.new(Path.global("Tuple"), ["X".path, "Y".path] of ASTNode)] of ASTNode)
it_parses "Foo({->})", Generic.new("Foo".path, [Generic.new(Path.global("Tuple"), [ProcNotation.new] of ASTNode)] of ASTNode)
it_parses "Foo({String, ->})", Generic.new("Foo".path, [Generic.new(Path.global("Tuple"), ["String".path, ProcNotation.new] of ASTNode)] of ASTNode)
it_parses "Foo({String, ->, ->})", Generic.new("Foo".path, [Generic.new(Path.global("Tuple"), ["String".path, ProcNotation.new, ProcNotation.new] of ASTNode)] of ASTNode)
it_parses "[] of {String, ->}", ArrayLiteral.new([] of ASTNode, Generic.new(Path.global("Tuple"), ["String".path, ProcNotation.new] of ASTNode))
it_parses "x([] of Foo, Bar.new)", Call.new(nil, "x", ArrayLiteral.new([] of ASTNode, "Foo".path), Call.new("Bar".path, "new"))
it_parses "Foo(x: U)", Generic.new("Foo".path, [] of ASTNode, named_args: [NamedArgument.new("x", "U".path)])
it_parses "Foo(x: U, y: V)", Generic.new("Foo".path, [] of ASTNode, named_args: [NamedArgument.new("x", "U".path), NamedArgument.new("y", "V".path)])
assert_syntax_error "Foo(T, x: U)"
assert_syntax_error "Foo(x: T y: U)"
it_parses %(Foo("foo bar": U)), Generic.new("Foo".path, [] of ASTNode, named_args: [NamedArgument.new("foo bar", "U".path)])
it_parses %(Foo("foo": U, "bar": V)), Generic.new("Foo".path, [] of ASTNode, named_args: [NamedArgument.new("foo", "U".path), NamedArgument.new("bar", "V".path)])
it_parses "Foo({x: X})", Generic.new("Foo".path, [Generic.new(Path.global("NamedTuple"), [] of ASTNode, named_args: [NamedArgument.new("x", "X".path)])] of ASTNode)
it_parses "Foo({x: X, y: Y})", Generic.new("Foo".path, [Generic.new(Path.global("NamedTuple"), [] of ASTNode, named_args: [NamedArgument.new("x", "X".path), NamedArgument.new("y", "Y".path)])] of ASTNode)
it_parses "Foo(T, {x: X})", Generic.new("Foo".path, ["T".path, Generic.new(Path.global("NamedTuple"), [] of ASTNode, named_args: [NamedArgument.new("x", "X".path)])] of ASTNode)
assert_syntax_error "Foo({x: X, x: Y})", "duplicated key: x"
it_parses %(Foo({"foo bar": X})), Generic.new("Foo".path, [Generic.new(Path.global("NamedTuple"), [] of ASTNode, named_args: [NamedArgument.new("foo bar", "X".path)])] of ASTNode)
it_parses %(Foo({"foo": X, "bar": Y})), Generic.new("Foo".path, [Generic.new(Path.global("NamedTuple"), [] of ASTNode, named_args: [NamedArgument.new("foo", "X".path), NamedArgument.new("bar", "Y".path)])] of ASTNode)
it_parses %(Foo{"x" => "y"}), HashLiteral.new([HashLiteral::Entry.new("x".string, "y".string)], name: "Foo".path)
it_parses %(::Foo{"x" => "y"}), HashLiteral.new([HashLiteral::Entry.new("x".string, "y".string)], name: Path.global("Foo"))
it_parses "Foo(*T)", Generic.new("Foo".path, ["T".path.splat] of ASTNode)
it_parses "Foo(X, sizeof(Int32))", Generic.new("Foo".path, ["X".path, SizeOf.new("Int32".path)] of ASTNode)
it_parses "Foo(X, instance_sizeof(Int32))", Generic.new("Foo".path, ["X".path, InstanceSizeOf.new("Int32".path)] of ASTNode)
it_parses "Foo(X, offsetof(Foo, @a))", Generic.new("Foo".path, ["X".path, OffsetOf.new("Foo".path, "@a".instance_var)] of ASTNode)
it_parses "Foo(\nT\n)", Generic.new("Foo".path, ["T".path] of ASTNode)
it_parses "Foo(\nT,\nU,\n)", Generic.new("Foo".path, ["T".path, "U".path] of ASTNode)
it_parses "Foo(\nx:\nT,\ny:\nU,\n)", Generic.new("Foo".path, [] of ASTNode, named_args: [NamedArgument.new("x", "T".path), NamedArgument.new("y", "U".path)])
it_parses "module Foo; end", ModuleDef.new("Foo".path)
it_parses "module Foo\ndef foo; end; end", ModuleDef.new("Foo".path, [Def.new("foo")] of ASTNode)
it_parses "module Foo(T); end", ModuleDef.new("Foo".path, type_vars: ["T"])
it_parses "while true; end;", While.new(true.bool)
it_parses "while true; 1; end;", While.new(true.bool, 1.int32)
it_parses "until true; end;", Until.new(true.bool)
it_parses "until true; 1; end;", Until.new(true.bool, 1.int32)
it_parses "foo do; 1; end", Call.new(nil, "foo", block: Block.new(body: 1.int32))
it_parses "foo do |a|; 1; end", Call.new(nil, "foo", block: Block.new(["a".var], 1.int32))
it_parses "foo { 1 }", Call.new(nil, "foo", block: Block.new(body: 1.int32))
it_parses "foo { |a| 1 }", Call.new(nil, "foo", block: Block.new(["a".var], 1.int32))
it_parses "foo { |a, b| 1 }", Call.new(nil, "foo", block: Block.new(["a".var, "b".var], 1.int32))
it_parses "foo { |a, b, | 1 }", Call.new(nil, "foo", block: Block.new(["a".var, "b".var], 1.int32))
it_parses "1.foo do; 1; end", Call.new(1.int32, "foo", block: Block.new(body: 1.int32))
it_parses "a b() {}", Call.new(nil, "a", Call.new(nil, "b", block: Block.new))
it_parses "foo { |a, (b, c), (d, e)| a; b; c; d; e }", Call.new(nil, "foo",
block: Block.new(["a".var, "__arg0".var, "__arg1".var],
Expressions.new([
Assign.new("b".var, Call.new("__arg0".var, "[]", 0.int32)),
Assign.new("c".var, Call.new("__arg0".var, "[]", 1.int32)),
Assign.new("d".var, Call.new("__arg1".var, "[]", 0.int32)),
Assign.new("e".var, Call.new("__arg1".var, "[]", 1.int32)),
"a".var, "b".var, "c".var, "d".var, "e".var,
] of ASTNode)))
it_parses "foo { |(_, c)| c }", Call.new(nil, "foo",
block: Block.new(["__arg0".var],
Expressions.new([
Assign.new("c".var, Call.new("__arg0".var, "[]", 1.int32)),
"c".var,
] of ASTNode)))
it_parses "foo { |(_, c, )| c }", Call.new(nil, "foo",
block: Block.new(["__arg0".var],
Expressions.new([
Assign.new("c".var, Call.new("__arg0".var, "[]", 1.int32)),
"c".var,
] of ASTNode)))
assert_syntax_error "foo { |a b| }", "expecting ',' or '|', not b"
assert_syntax_error "foo { |(a b)| }", "expecting ',' or ')', not b"
it_parses "1 ? 2 : 3", If.new(1.int32, 2.int32, 3.int32)
it_parses "1 ? a : b", If.new(1.int32, "a".call, "b".call)
it_parses "1 ? a : b ? c : 3", If.new(1.int32, "a".call, If.new("b".call, "c".call, 3.int32))
it_parses "a ? 1 : b ? 2 : c ? 3 : 0", If.new("a".call, 1.int32, If.new("b".call, 2.int32, If.new("c".call, 3.int32, 0.int32)))
it_parses "a ? 1
: b", If.new("a".call, 1.int32, "b".call)
it_parses "a ? 1 :
b ? 2 :
c ? 3
: 0", If.new("a".call, 1.int32, If.new("b".call, 2.int32, If.new("c".call, 3.int32, 0.int32)))
it_parses "a ? 1
: b ? 2
: c ? 3
: 0", If.new("a".call, 1.int32, If.new("b".call, 2.int32, If.new("c".call, 3.int32, 0.int32)))
it_parses "a ?
b ? b1 : b2
: c ? 3
: 0", If.new("a".call, If.new("b".call, "b1".call, "b2".call), If.new("c".call, 3.int32, 0.int32))
it_parses "1 if 3", If.new(3.int32, 1.int32)
it_parses "1 unless 3", Unless.new(3.int32, 1.int32)
it_parses "r = 1; r.x += 2", [Assign.new("r".var, 1.int32), OpAssign.new(Call.new("r".var, "x"), "+", 2.int32)] of ASTNode
it_parses "foo if 3", If.new(3.int32, "foo".call)
it_parses "foo unless 3", Unless.new(3.int32, "foo".call)
it_parses "a = 1; a += 10 if a += 20", [Assign.new("a".var, 1.int32), If.new(OpAssign.new("a".var, "+", 20.int32), OpAssign.new("a".var, "+", 10.int32))]
it_parses "puts a if true", If.new(true.bool, Call.new(nil, "puts", "a".call))
it_parses "puts ::foo", Call.new(nil, "puts", Call.new(nil, "foo", global: true))
it_parses "puts __FILE__", Call.new(nil, "puts", "/foo/bar/baz.cr".string)
it_parses "puts __DIR__", Call.new(nil, "puts", "/foo/bar".string)
it_parses "puts __LINE__", Call.new(nil, "puts", 1.int32)
it_parses "puts _", Call.new(nil, "puts", Underscore.new)
it_parses "x = 2; foo do bar x end", [Assign.new("x".var, 2.int32), Call.new(nil, "foo", block: Block.new(body: Call.new(nil, "bar", "x".var)))] of ASTNode
{ {"break", Break}, {"return", Return}, {"next", Next} }.each do |(keyword, klass)|
it_parses "#{keyword}", klass.new
it_parses "#{keyword};", klass.new
it_parses "#{keyword} 1", klass.new(1.int32)
it_parses "#{keyword} 1, 2", klass.new(TupleLiteral.new([1.int32, 2.int32] of ASTNode))
it_parses "#{keyword} {1, 2}", klass.new(TupleLiteral.new([1.int32, 2.int32] of ASTNode))
it_parses "#{keyword} {1 => 2}", klass.new(HashLiteral.new([HashLiteral::Entry.new(1.int32, 2.int32)]))
it_parses "#{keyword} 1 if true", If.new(true.bool, klass.new(1.int32))
it_parses "#{keyword} if true", If.new(true.bool, klass.new)
assert_syntax_error "a = #{keyword}", "void value expression"
assert_syntax_error "a = 1; a += #{keyword}", "void value expression"
assert_syntax_error "yield #{keyword}", "void value expression"
assert_syntax_error "foo(#{keyword})", "void value expression"
assert_syntax_error "foo[#{keyword}]", "void value expression"
assert_syntax_error "foo[1] = #{keyword}", "void value expression"
assert_syntax_error "if #{keyword}; end", "void value expression"
assert_syntax_error "unless #{keyword}; end", "void value expression"
assert_syntax_error "while #{keyword}; end", "void value expression"
assert_syntax_error "until #{keyword}; end", "void value expression"
assert_syntax_error "1 if #{keyword}", "void value expression"
assert_syntax_error "1 unless #{keyword}", "void value expression"
assert_syntax_error "#{keyword}.foo", "void value expression"
assert_syntax_error "#{keyword}.as(Int32)", "void value expression"
assert_syntax_error "#{keyword}[]", "void value expression"
assert_syntax_error "#{keyword}[0]", "void value expression"
assert_syntax_error "#{keyword}[0]= 1", "void value expression"
assert_syntax_error "#{keyword} .. 1", "void value expression"
assert_syntax_error "#{keyword} ... 1", "void value expression"
assert_syntax_error "1 .. #{keyword}", "void value expression"
assert_syntax_error "1 ... #{keyword}", "void value expression"
assert_syntax_error "#{keyword} ? 1 : 2", "void value expression"
assert_syntax_error "+#{keyword}", "void value expression"
["<<", "<", "<=", "==", ">>", ">", ">=", "+", "-", "*", "/", "//", "%", "|",
"&", "^", "**", "===", "&+", "&-", "&*", "&**"].each do |op|
assert_syntax_error "#{keyword} #{op} 1", "void value expression"
end
assert_syntax_error "case #{keyword}; when 1; end; end", "void value expression"
assert_syntax_error "case 1; when #{keyword}; end; end", "void value expression"
end
assert_syntax_error "break when true"
it_parses "yield", Yield.new
it_parses "yield;", Yield.new
it_parses "yield 1", Yield.new([1.int32] of ASTNode)
it_parses "yield 1 if true", If.new(true.bool, Yield.new([1.int32] of ASTNode))
it_parses "yield if true", If.new(true.bool, Yield.new)
it_parses "Int", "Int".path
it_parses "Int[]", Call.new("Int".path, "[]")
it_parses "def []; end", Def.new("[]")
it_parses "def []?; end", Def.new("[]?")
it_parses "def []=(value); end", Def.new("[]=", ["value".arg])
it_parses "def self.[]; end", Def.new("[]", receiver: "self".var)
it_parses "def self.[]?; end", Def.new("[]?", receiver: "self".var)
it_parses "Int[8]", Call.new("Int".path, "[]", 8.int32)
it_parses "Int[8, 4]", Call.new("Int".path, "[]", 8.int32, 4.int32)
it_parses "Int[8, 4,]", Call.new("Int".path, "[]", 8.int32, 4.int32)
it_parses "Int[8]?", Call.new("Int".path, "[]?", 8.int32)
it_parses "x[0] ? 1 : 0", If.new(Call.new("x".call, "[]", 0.int32), 1.int32, 0.int32)
it_parses "def [](x); end", Def.new("[]", ["x".arg])
it_parses "foo[0] = 1", Call.new("foo".call, "[]=", 0.int32, 1.int32)
it_parses "foo[0] = 1 if 2", If.new(2.int32, Call.new("foo".call, "[]=", 0.int32, 1.int32))
it_parses "begin; 1; end;", Expressions.new([1.int32] of ASTNode)
it_parses "begin; 1; 2; 3; end;", Expressions.new([1.int32, 2.int32, 3.int32] of ASTNode)
it_parses "self", "self".var
it_parses "@foo", "@foo".instance_var
it_parses "@foo = 1", Assign.new("@foo".instance_var, 1.int32)
it_parses "-@foo", Call.new("@foo".instance_var, "-")
it_parses "var.@foo", ReadInstanceVar.new("var".call, "@foo")
it_parses "var.@foo.@bar", ReadInstanceVar.new(ReadInstanceVar.new("var".call, "@foo"), "@bar")
it_parses "@@foo", "@@foo".class_var
it_parses "@@foo = 1", Assign.new("@@foo".class_var, 1.int32)
it_parses "-@@foo", Call.new("@@foo".class_var, "-")
it_parses "call @foo.bar", Call.new(nil, "call", Call.new("@foo".instance_var, "bar"))
it_parses "call \"foo\"", Call.new(nil, "call", "foo".string)
it_parses "def foo; end; if false; 1; else; 2; end", [Def.new("foo", [] of Arg), If.new(false.bool, 1.int32, 2.int32)]
it_parses "A.new(\"x\", B.new(\"y\"))", Call.new("A".path, "new", "x".string, Call.new("B".path, "new", "y".string))
it_parses "foo [1]", Call.new(nil, "foo", ([1.int32] of ASTNode).array)
it_parses "foo.bar [1]", Call.new("foo".call, "bar", ([1.int32] of ASTNode).array)
it_parses "class Foo; end\nwhile true; end", [ClassDef.new("Foo".path), While.new(true.bool)]
it_parses "while true; end\nif true; end", [While.new(true.bool), If.new(true.bool)]
it_parses "(1)\nif true; end", [Expressions.new([1.int32] of ASTNode), If.new(true.bool)]
it_parses "begin\n1\nend\nif true; end", [Expressions.new([1.int32] of ASTNode), If.new(true.bool)]
it_parses "Foo::Bar", ["Foo", "Bar"].path
it_parses "lib LibC\nend", LibDef.new("LibC")
it_parses "lib LibC\nfun getchar\nend", LibDef.new("LibC", [FunDef.new("getchar")] of ASTNode)
it_parses "lib LibC\nfun getchar(...)\nend", LibDef.new("LibC", [FunDef.new("getchar", varargs: true)] of ASTNode)
it_parses "lib LibC\nfun getchar : Int\nend", LibDef.new("LibC", [FunDef.new("getchar", return_type: "Int".path)] of ASTNode)
it_parses "lib LibC\nfun getchar : (->)?\nend", LibDef.new("LibC", [FunDef.new("getchar", return_type: Crystal::Union.new([ProcNotation.new, "Nil".path(true)] of ASTNode))] of ASTNode)
it_parses "lib LibC\nfun getchar(Int, Float)\nend", LibDef.new("LibC", [FunDef.new("getchar", [Arg.new("", restriction: "Int".path), Arg.new("", restriction: "Float".path)])] of ASTNode)
it_parses "lib LibC\nfun getchar(a : Int, b : Float)\nend", LibDef.new("LibC", [FunDef.new("getchar", [Arg.new("a", restriction: "Int".path), Arg.new("b", restriction: "Float".path)])] of ASTNode)
it_parses "lib LibC\nfun getchar(a : Int)\nend", LibDef.new("LibC", [FunDef.new("getchar", [Arg.new("a", restriction: "Int".path)])] of ASTNode)
it_parses "lib LibC\nfun getchar(a : Int, b : Float) : Int\nend", LibDef.new("LibC", [FunDef.new("getchar", [Arg.new("a", restriction: "Int".path), Arg.new("b", restriction: "Float".path)], "Int".path)] of ASTNode)
it_parses "lib LibC; fun getchar(a : Int, b : Float) : Int; end", LibDef.new("LibC", [FunDef.new("getchar", [Arg.new("a", restriction: "Int".path), Arg.new("b", restriction: "Float".path)], "Int".path)] of ASTNode)
it_parses "lib LibC; fun foo(a : Int*); end", LibDef.new("LibC", [FunDef.new("foo", [Arg.new("a", restriction: "Int".path.pointer_of)])] of ASTNode)
it_parses "lib LibC; fun foo(a : Int**); end", LibDef.new("LibC", [FunDef.new("foo", [Arg.new("a", restriction: "Int".path.pointer_of.pointer_of)])] of ASTNode)
it_parses "lib LibC; fun foo : Int*; end", LibDef.new("LibC", [FunDef.new("foo", return_type: "Int".path.pointer_of)] of ASTNode)
it_parses "lib LibC; fun foo : Int**; end", LibDef.new("LibC", [FunDef.new("foo", return_type: "Int".path.pointer_of.pointer_of)] of ASTNode)
it_parses "lib LibC; fun foo(a : ::B, ::C -> ::D); end", LibDef.new("LibC", [FunDef.new("foo", [Arg.new("a", restriction: ProcNotation.new([Path.global("B"), Path.global("C")] of ASTNode, Path.global("D")))])] of ASTNode)
it_parses "lib LibC; type A = B; end", LibDef.new("LibC", [TypeDef.new("A", "B".path)] of ASTNode)
it_parses "lib LibC; type A = B*; end", LibDef.new("LibC", [TypeDef.new("A", "B".path.pointer_of)] of ASTNode)
it_parses "lib LibC; type A = B**; end", LibDef.new("LibC", [TypeDef.new("A", "B".path.pointer_of.pointer_of)] of ASTNode)
it_parses "lib LibC; type A = B.class; end", LibDef.new("LibC", [TypeDef.new("A", Metaclass.new("B".path))] of ASTNode)
it_parses "lib LibC; struct Foo; end end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo")] of ASTNode)
it_parses "lib LibC; struct Foo; x : Int; y : Float; end end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", [TypeDeclaration.new("x".var, "Int".path), TypeDeclaration.new("y".var, "Float".path)] of ASTNode)] of ASTNode)
it_parses "lib LibC; struct Foo; x : Int*; end end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", Expressions.from(TypeDeclaration.new("x".var, "Int".path.pointer_of)))] of ASTNode)
it_parses "lib LibC; struct Foo; x : Int**; end end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", Expressions.from(TypeDeclaration.new("x".var, "Int".path.pointer_of.pointer_of)))] of ASTNode)
it_parses "lib LibC; struct Foo; x, y, z : Int; end end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", [TypeDeclaration.new("x".var, "Int".path), TypeDeclaration.new("y".var, "Int".path), TypeDeclaration.new("z".var, "Int".path)] of ASTNode)] of ASTNode)
it_parses "lib LibC; union Foo; end end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", union: true)] of ASTNode)
it_parses "lib LibC; enum Foo; A\nB; C\nD = 1; end end", LibDef.new("LibC", [EnumDef.new("Foo".path, [Arg.new("A"), Arg.new("B"), Arg.new("C"), Arg.new("D", 1.int32)] of ASTNode)] of ASTNode)
it_parses "lib LibC; enum Foo; A = 1; B; end end", LibDef.new("LibC", [EnumDef.new("Foo".path, [Arg.new("A", 1.int32), Arg.new("B")] of ASTNode)] of ASTNode)
it_parses "lib LibC; Foo = 1; end", LibDef.new("LibC", [Assign.new("Foo".path, 1.int32)] of ASTNode)
it_parses "lib LibC\nfun getch = GetChar\nend", LibDef.new("LibC", [FunDef.new("getch", real_name: "GetChar")] of ASTNode)
it_parses %(lib LibC\nfun getch = "get.char"\nend), LibDef.new("LibC", [FunDef.new("getch", real_name: "get.char")] of ASTNode)
it_parses %(lib LibC\nfun getch = "get.char" : Int32\nend), LibDef.new("LibC", [FunDef.new("getch", return_type: "Int32".path, real_name: "get.char")] of ASTNode)
it_parses %(lib LibC\nfun getch = "get.char"(x : Int32)\nend), LibDef.new("LibC", [FunDef.new("getch", [Arg.new("x", restriction: "Int32".path)], real_name: "get.char")] of ASTNode)
it_parses "lib LibC\n$errno : Int32\n$errno2 : Int32\nend", LibDef.new("LibC", [ExternalVar.new("errno", "Int32".path), ExternalVar.new("errno2", "Int32".path)] of ASTNode)
it_parses "lib LibC\n$errno : B, C -> D\nend", LibDef.new("LibC", [ExternalVar.new("errno", ProcNotation.new(["B".path, "C".path] of ASTNode, "D".path))] of ASTNode)
it_parses "lib LibC\n$errno = Foo : Int32\nend", LibDef.new("LibC", [ExternalVar.new("errno", "Int32".path, "Foo")] of ASTNode)
it_parses "lib LibC\nalias Foo = Bar\nend", LibDef.new("LibC", [Alias.new("Foo".path, "Bar".path)] of ASTNode)
it_parses "lib LibC; struct Foo; include Bar; end; end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", Include.new("Bar".path))] of ASTNode)
it_parses "lib LibC\nfun SomeFun\nend", LibDef.new("LibC", [FunDef.new("SomeFun")] of ASTNode)
it_parses "fun foo(x : Int32) : Int64\nx\nend", FunDef.new("foo", [Arg.new("x", restriction: "Int32".path)], "Int64".path, body: "x".var)
assert_syntax_error "fun Foo : Int64\nend"
it_parses "lib LibC; {{ 1 }}; end", LibDef.new("LibC", body: [MacroExpression.new(1.int32)] of ASTNode)
it_parses "lib LibC; {% if 1 %}2{% end %}; end", LibDef.new("LibC", body: [MacroIf.new(1.int32, MacroLiteral.new("2"))] of ASTNode)
it_parses "lib LibC; struct Foo; {{ 1 }}; end; end", LibDef.new("LibC", body: CStructOrUnionDef.new("Foo", Expressions.from([MacroExpression.new(1.int32)] of ASTNode)))
it_parses "lib LibC; struct Foo; {% if 1 %}2{% end %}; end; end", LibDef.new("LibC", body: CStructOrUnionDef.new("Foo", Expressions.from([MacroIf.new(1.int32, MacroLiteral.new("2"))] of ASTNode)))
it_parses "1 .. 2", RangeLiteral.new(1.int32, 2.int32, false)
it_parses "1 ... 2", RangeLiteral.new(1.int32, 2.int32, true)
it_parses "(1 .. )", Expressions.new([RangeLiteral.new(1.int32, Nop.new, false)] of ASTNode)
it_parses "(1 ... )", Expressions.new([RangeLiteral.new(1.int32, Nop.new, true)] of ASTNode)
it_parses "foo(1.., 2)", Call.new(nil, "foo", [RangeLiteral.new(1.int32, Nop.new, false), 2.int32] of ASTNode)
it_parses "1..;", RangeLiteral.new(1.int32, Nop.new, false)
it_parses "1..\n2..", Expressions.new([RangeLiteral.new(1.int32, Nop.new, false), RangeLiteral.new(2.int32, Nop.new, false)] of ASTNode)
it_parses "{1.. => 2};", HashLiteral.new([HashLiteral::Entry.new(RangeLiteral.new(1.int32, Nop.new, false), 2.int32)])
it_parses "..2", RangeLiteral.new(Nop.new, 2.int32, false)
it_parses "...2", RangeLiteral.new(Nop.new, 2.int32, true)
it_parses "foo..2", RangeLiteral.new("foo".call, 2.int32, false)
it_parses "foo ..2", RangeLiteral.new("foo".call, 2.int32, false)
it_parses "foo(..2)", Call.new(nil, "foo", RangeLiteral.new(Nop.new, 2.int32, false))
it_parses "x[..2]", Call.new("x".call, "[]", RangeLiteral.new(Nop.new, 2.int32, false))
it_parses "x[1, ..2]", Call.new("x".call, "[]", [1.int32, RangeLiteral.new(Nop.new, 2.int32, false)] of ASTNode)
it_parses "{..2}", TupleLiteral.new([RangeLiteral.new(Nop.new, 2.int32, false)] of ASTNode)
it_parses "[..2]", ArrayLiteral.new([RangeLiteral.new(Nop.new, 2.int32, false)] of ASTNode)
it_parses "A = 1", Assign.new("A".path, 1.int32)
it_parses "puts %w(one two)", Call.new(nil, "puts", (["one".string, "two".string] of ASTNode).array_of(Path.global("String")))
it_parses "puts %w{one two}", Call.new(nil, "puts", (["one".string, "two".string] of ASTNode).array_of(Path.global("String")))
it_parses "puts %i(one two)", Call.new(nil, "puts", (["one".symbol, "two".symbol] of ASTNode).array_of(Path.global("Symbol")))
it_parses "puts {{1}}", Call.new(nil, "puts", MacroExpression.new(1.int32))
it_parses "puts {{\n1\n}}", Call.new(nil, "puts", MacroExpression.new(1.int32))
it_parses "puts {{*1}}", Call.new(nil, "puts", MacroExpression.new(1.int32.splat))
it_parses "puts {{**1}}", Call.new(nil, "puts", MacroExpression.new(DoubleSplat.new(1.int32)))
it_parses "{{a = 1 if 2}}", MacroExpression.new(If.new(2.int32, Assign.new("a".var, 1.int32)))
it_parses "{% a = 1 %}", MacroExpression.new(Assign.new("a".var, 1.int32), output: false)
it_parses "{%\na = 1\n%}", MacroExpression.new(Assign.new("a".var, 1.int32), output: false)
it_parses "{% a = 1 if 2 %}", MacroExpression.new(If.new(2.int32, Assign.new("a".var, 1.int32)), output: false)
it_parses "{% if 1; 2; end %}", MacroExpression.new(If.new(1.int32, 2.int32), output: false)
it_parses "{%\nif 1; 2; end\n%}", MacroExpression.new(If.new(1.int32, 2.int32), output: false)
it_parses "{% unless 1; 2; end %}", MacroExpression.new(If.new(1.int32, Nop.new, 2.int32), output: false)
it_parses "{%\n1\n2\n3\n%}", MacroExpression.new(Expressions.new([1.int32, 2.int32, 3.int32] of ASTNode), output: false)
it_parses "{{ 1 // 2 }}", MacroExpression.new(Expressions.from([Call.new(1.int32, "//", 2.int32)] of ASTNode))
it_parses "{{ //.options }}", MacroExpression.new(Expressions.from([Call.new(RegexLiteral.new(StringLiteral.new("")), "options")] of ASTNode))
it_parses "[] of Int", ([] of ASTNode).array_of("Int".path)
it_parses "[1, 2] of Int", ([1.int32, 2.int32] of ASTNode).array_of("Int".path)
it_parses "::A::B", Path.global(["A", "B"])
assert_syntax_error "$foo", "$global_variables are not supported, use @@class_variables instead"
it_parses "macro foo;end", Macro.new("foo", [] of Arg, Expressions.new)
it_parses "macro [];end", Macro.new("[]", [] of Arg, Expressions.new)
it_parses %(macro foo; 1 + 2; end), Macro.new("foo", [] of Arg, Expressions.from([" 1 + 2; ".macro_literal] of ASTNode))
it_parses %(macro foo(x); 1 + 2; end), Macro.new("foo", ([Arg.new("x")]), Expressions.from([" 1 + 2; ".macro_literal] of ASTNode))
it_parses %(macro foo(x)\n 1 + 2; end), Macro.new("foo", ([Arg.new("x")]), Expressions.from([" 1 + 2; ".macro_literal] of ASTNode))
it_parses "macro foo; 1 + 2 {{foo}} 3 + 4; end", Macro.new("foo", [] of Arg, Expressions.from([" 1 + 2 ".macro_literal, MacroExpression.new("foo".var), " 3 + 4; ".macro_literal] of ASTNode))
it_parses "macro foo; 1 + 2 {{ foo }} 3 + 4; end", Macro.new("foo", [] of Arg, Expressions.from([" 1 + 2 ".macro_literal, MacroExpression.new("foo".var), " 3 + 4; ".macro_literal] of ASTNode))
it_parses "macro foo;bar{% for x in y %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroFor.new(["x".var], "y".var, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% for x, y in z %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroFor.new(["x".var, "y".var], "z".var, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% if x %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new("x".var, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% if x %}body{% else %}body2{%end%}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new("x".var, "body".macro_literal, "body2".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% if x %}body{% elsif y %}body2{%end%}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new("x".var, "body".macro_literal, MacroIf.new("y".var, "body2".macro_literal)), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% if x %}body{% elsif y %}body2{% else %}body3{%end%}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new("x".var, "body".macro_literal, MacroIf.new("y".var, "body2".macro_literal, "body3".macro_literal)), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% unless x %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new("x".var, Nop.new, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% for x in y %}\\ \n body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroFor.new(["x".var], "y".var, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo;bar{% for x in y %}\\ \n body{% end %}\\ baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroFor.new(["x".var], "y".var, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro foo; 1 + 2 {{foo}}\\ 3 + 4; end", Macro.new("foo", [] of Arg, Expressions.from([" 1 + 2 ".macro_literal, MacroExpression.new("foo".var), "3 + 4; ".macro_literal] of ASTNode))
it_parses "macro foo(\na = 0\n)\nend", Macro.new("foo", [Arg.new("a", default_value: 0.int32)], Expressions.new)
it_parses "macro foo;{% verbatim do %}1{% foo %}2{% end %};end", Macro.new("foo", [] of Arg, Expressions.from([MacroVerbatim.new(Expressions.from(["1".macro_literal, MacroExpression.new("foo".var, false), "2".macro_literal] of ASTNode)), ";".macro_literal] of ASTNode))
it_parses "macro foo\n{%\nif 1\n2\nelse\n3\nend\n%}end", Macro.new("foo", body: MacroExpression.new(If.new(1.int32, 2.int32, 3.int32), output: false))
assert_syntax_error "macro foo; {% foo = 1 }; end"
assert_syntax_error "macro def foo : String; 1; end"
it_parses "def foo;{{@type}};end", Def.new("foo", body: Expressions.from([MacroExpression.new("@type".instance_var)] of ASTNode), macro_def: true)
it_parses "macro foo;bar{% begin %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new(true.bool, "body".macro_literal), "baz;".macro_literal] of ASTNode))
it_parses "macro x\n%{}\nend", Macro.new("x", body: MacroLiteral.new("%{}\n"))
it_parses "def foo : Int32\n1\nend", Def.new("foo", body: 1.int32, return_type: "Int32".path)
it_parses "def foo(x) : Int32\n1\nend", Def.new("foo", args: ["x".arg], body: 1.int32, return_type: "Int32".path)
it_parses "abstract def foo : Int32", Def.new("foo", return_type: "Int32".path, abstract: true)
it_parses "abstract def foo(x) : Int32", Def.new("foo", args: ["x".arg], return_type: "Int32".path, abstract: true)
it_parses "{% for x in y %}body{% end %}", MacroFor.new(["x".var], "y".var, "body".macro_literal)
it_parses "{% if x %}body{% end %}", MacroIf.new("x".var, "body".macro_literal)
it_parses "{% begin %}{% if true %}if true{% end %}\n{% if true %}end{% end %}{% end %}", MacroIf.new(true.bool, [MacroIf.new(true.bool, "if true".macro_literal), "\n".macro_literal, MacroIf.new(true.bool, "end".macro_literal)] of ASTNode)
it_parses "{{ foo }}", MacroExpression.new("foo".var)
it_parses "macro foo;%var;end", Macro.new("foo", [] of Arg, Expressions.from([MacroVar.new("var"), MacroLiteral.new(";")] of ASTNode))
it_parses "macro foo;%var{1, x} = hello;end", Macro.new("foo", [] of Arg, Expressions.from([MacroVar.new("var", [1.int32, "x".var] of ASTNode), MacroLiteral.new(" = hello;")] of ASTNode))
["if", "unless"].each do |keyword|
it_parses "macro foo;%var #{keyword} true;end", Macro.new("foo", [] of Arg, Expressions.from([MacroVar.new("var"), " #{keyword} true;".macro_literal] of ASTNode))
it_parses "macro foo;var #{keyword} true;end", Macro.new("foo", [] of Arg, "var #{keyword} true;".macro_literal)
it_parses "macro foo;#{keyword} %var;true;end;end", Macro.new("foo", [] of Arg, Expressions.from(["#{keyword} ".macro_literal, MacroVar.new("var"), ";true;".macro_literal, "end;".macro_literal] of ASTNode))
it_parses "macro foo;#{keyword} var;true;end;end", Macro.new("foo", [] of Arg, Expressions.from(["#{keyword} var;true;".macro_literal, "end;".macro_literal] of ASTNode))
end
it_parses "a = 1; pointerof(a)", [Assign.new("a".var, 1.int32), PointerOf.new("a".var)]
it_parses "pointerof(@a)", PointerOf.new("@a".instance_var)
it_parses "a = 1; pointerof(a)", [Assign.new("a".var, 1.int32), PointerOf.new("a".var)]
it_parses "pointerof(@a)", PointerOf.new("@a".instance_var)
it_parses "sizeof(X)", SizeOf.new("X".path)
it_parses "instance_sizeof(X)", InstanceSizeOf.new("X".path)
it_parses "offsetof(X, @a)", OffsetOf.new("X".path, "@a".instance_var)
it_parses "foo.is_a?(Const)", IsA.new("foo".call, "Const".path)
it_parses "foo.is_a?(Foo | Bar)", IsA.new("foo".call, Crystal::Union.new(["Foo".path, "Bar".path] of ASTNode))
it_parses "foo.is_a? Const", IsA.new("foo".call, "Const".path)
it_parses "foo.responds_to?(:foo)", RespondsTo.new("foo".call, "foo")
it_parses "foo.responds_to? :foo", RespondsTo.new("foo".call, "foo")
it_parses "if foo.responds_to? :foo\nx = 1\nend", If.new(RespondsTo.new("foo".call, "foo"), Assign.new("x".var, 1.int32))
it_parses "is_a?(Const)", IsA.new("self".var, "Const".path)
it_parses "responds_to?(:foo)", RespondsTo.new("self".var, "foo")
it_parses "nil?", IsA.new("self".var, Path.global("Nil"), nil_check: true)
it_parses "nil?( )", IsA.new("self".var, Path.global("Nil"), nil_check: true)
it_parses "foo.nil?", IsA.new("foo".call, Path.global("Nil"), nil_check: true)
it_parses "foo.nil?( )", IsA.new("foo".call, Path.global("Nil"), nil_check: true)
it_parses "foo &.nil?", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], IsA.new(Var.new("__arg0"), Path.global("Nil"), nil_check: true)))
it_parses "foo &.baz.qux do\nend", Call.new(nil, "foo",
block: Block.new(["__arg0".var],
Call.new(Call.new("__arg0".var, "baz"), "qux", block: Block.new)
)
)
it_parses "foo.!", Not.new("foo".call)
it_parses "foo.!.!", Not.new(Not.new("foo".call))
it_parses "foo.!( )", Not.new("foo".call)
it_parses "foo &.!", Call.new(nil, "foo", block: Block.new([Var.new("__arg0")], Not.new(Var.new("__arg0"))))
# multiline pseudo methods (#8318)
it_parses "sizeof(\n Int32\n)", SizeOf.new(Path.new("Int32"))
it_parses "instance_sizeof(\n Int32\n)", InstanceSizeOf.new(Path.new("Int32"))
it_parses "typeof(\n 1\n)", TypeOf.new([1.int32] of ASTNode)
it_parses "offsetof(\n Foo,\n @foo\n)", OffsetOf.new(Path.new("Foo"), InstanceVar.new("@foo"))
it_parses "pointerof(\n foo\n)", PointerOf.new("foo".call)
it_parses "1.as(\n Int32\n)", Cast.new(1.int32, Path.new("Int32"))
it_parses "1.as?(\n Int32\n)", NilableCast.new(1.int32, Path.new("Int32"))
it_parses "1.is_a?(\n Int32\n)", IsA.new(1.int32, Path.new("Int32"))
it_parses "1.responds_to?(\n :foo\n)", RespondsTo.new(1.int32, "foo")
it_parses "1.nil?(\n)", IsA.new(1.int32, Path.global("Nil"), nil_check: true)
it_parses "1.!(\n)", Not.new(1.int32)
it_parses "/foo/", regex("foo")
it_parses "/foo/i", regex("foo", Regex::Options::IGNORE_CASE)
it_parses "/foo/m", regex("foo", Regex::Options::MULTILINE)
it_parses "/foo/x", regex("foo", Regex::Options::EXTENDED)
it_parses "/foo/imximx", regex("foo", Regex::Options::IGNORE_CASE | Regex::Options::MULTILINE | Regex::Options::EXTENDED)
it_parses "/fo\\so/", regex("fo\\so")
it_parses "/fo\#{1}o/", RegexLiteral.new(StringInterpolation.new(["fo".string, 1.int32, "o".string] of ASTNode))
it_parses "/(fo\#{\"bar\"}\#{1}o)/", RegexLiteral.new(StringInterpolation.new(["(fo".string, "bar".string, 1.int32, "o)".string] of ASTNode))
it_parses "%r(foo(bar))", regex("foo(bar)")
it_parses "/ /", regex(" ")
it_parses "/=/", regex("=")
it_parses "/ hi /", regex(" hi ")
it_parses "self / number", Call.new("self".var, "/", "number".call)
it_parses "a == / /", Call.new("a".call, "==", regex(" "))
it_parses "/ /", regex(" ")
it_parses "/ /; / /", [regex(" "), regex(" ")] of ASTNode
it_parses "/ /\n/ /", [regex(" "), regex(" ")] of ASTNode
it_parses "a = / /", Assign.new("a".var, regex(" "))
it_parses "(/ /)", Expressions.new([regex(" ")] of ASTNode)
it_parses "a = /=/", Assign.new("a".var, regex("="))
it_parses "a; if / /; / /; elsif / /; / /; end", ["a".call, If.new(regex(" "), regex(" "), If.new(regex(" "), regex(" ")))]
it_parses "a; if / /\n/ /\nelsif / /\n/ /\nend", ["a".call, If.new(regex(" "), regex(" "), If.new(regex(" "), regex(" ")))]
it_parses "a; while / /; / /; end", ["a".call, While.new(regex(" "), regex(" "))]
it_parses "a\nwhile / /\n/ /\nend", ["a".call, While.new(regex(" "), regex(" "))]
it_parses "[/ /, / /]", ArrayLiteral.new([regex(" "), regex(" ")] of ASTNode)
it_parses "{/ / => / /, / / => / /}", HashLiteral.new([HashLiteral::Entry.new(regex(" "), regex(" ")), HashLiteral::Entry.new(regex(" "), regex(" "))])
it_parses "{/ /, / /}", TupleLiteral.new([regex(" "), regex(" ")] of ASTNode)
it_parses "begin; / /; end", Expressions.new([regex(" ")] of ASTNode)
it_parses "begin\n/ /\nend", Expressions.new([regex(" ")] of ASTNode)
it_parses "/\\//", regex("/")
it_parses "%r(/)", regex("/")
it_parses "a()/3", Call.new("a".call, "/", 3.int32)
it_parses "a() /3", Call.new("a".call, "/", 3.int32)
it_parses "a.b() /3", Call.new(Call.new("a".call, "b"), "/", 3.int32)
it_parses "def foo(x = / /); end", Def.new("foo", [Arg.new("x", regex(" "))])
it_parses "begin 1 end / 2", Call.new(Expressions.new([1.int32] of ASTNode), "/", 2.int32)