/
ast.cr
2268 lines (1780 loc) · 48 KB
/
ast.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
module Crystal
# Base class for nodes in the grammar.
abstract class ASTNode
# The location where this node starts, or `nil`
# if the location is not known.
property location : Location?
# The location where this node ends, or `nil`
# if the location is not known.
property end_location : Location?
# Updates this node's location and returns `self`
def at(@location : Location?)
self
end
# Sets this node's location and end location to those
# of `node`, and returns `self`
def at(node : ASTNode)
@location = node.location
@end_location = node.end_location
self
end
# Updates this node's end location and returns `self`
def at_end(@end_location : Location?)
self
end
# Sets this node's end location to those of `node` and
# returns self
def at_end(node : ASTNode)
@end_location = node.end_location
self
end
# Returns a deep copy of this node. Copied nodes retain
# the location and end location of the original nodes.
def clone
clone = clone_without_location
clone.location = location
clone.end_location = end_location
clone.doc = doc
clone
end
# Returns the doc comment attached to this node. Not every node
# supports having doc comments, so by default this returns `nil`.
def doc
end
# Attaches a doc comment to this node. Not every node supports
# having doc comments, so by default this does nothing and some
# subclasses implement this.
def doc=(doc)
end
def name_location
nil
end
def name_size
0
end
def visibility=(visibility : Visibility)
end
def visibility
Visibility::Public
end
def nop?
self.is_a?(Nop)
end
def true_literal?
self.is_a?(BoolLiteral) && self.value
end
def false_literal?
self.is_a?(BoolLiteral) && !self.value
end
def class_desc : String
{{@type.name.split("::").last.id.stringify}}
end
def pretty_print(pp)
pp.text to_s
end
# It yields itself for any node, but `Expressions` yields first node
# if it holds only a node.
def single_expression
single_expression? || self
end
# It yields `nil` always.
# (It is overriden by `Expressions` to implement `#single_expression`.)
def single_expression?
nil
end
end
class Nop < ASTNode
def clone_without_location
Nop.new
end
def_equals_and_hash
end
# A container for one or many expressions.
class Expressions < ASTNode
property expressions : Array(ASTNode)
property keyword : Symbol?
def self.from(obj : Nil)
Nop.new
end
def self.from(obj : Array)
case obj.size
when 0
Nop.new
when 1
obj.first
else
new obj
end
end
def self.from(obj : ASTNode)
obj
end
def initialize(@expressions = [] of ASTNode)
end
def empty?
@expressions.empty?
end
def [](i)
@expressions[i]
end
def last
@expressions.last
end
def location
@location || @expressions.first?.try &.location
end
def end_location
@end_location || @expressions.last?.try &.end_location
end
# It yields first node if this holds only one node, or yields `nil`.
def single_expression?
return @expressions.first.single_expression if @expressions.size == 1
nil
end
def accept_children(visitor)
@expressions.each &.accept visitor
end
def clone_without_location
Expressions.new(@expressions.clone).tap &.keyword = keyword
end
def_equals_and_hash expressions
end
# The nil literal.
#
# 'nil'
#
class NilLiteral < ASTNode
def clone_without_location
NilLiteral.new
end
def_equals_and_hash
end
# A bool literal.
#
# 'true' | 'false'
#
class BoolLiteral < ASTNode
property value : Bool
def initialize(@value)
end
def clone_without_location
BoolLiteral.new(@value)
end
def_equals_and_hash value
end
# Any number literal.
# kind stores a symbol indicating which type is it: i32, u16, f32, f64, etc.
class NumberLiteral < ASTNode
property value : String
property kind : Symbol
def initialize(@value : String, @kind = :i32)
end
def self.new(value : Number)
new(value.to_s, kind_from_number(value))
end
def has_sign?
@value[0] == '+' || @value[0] == '-'
end
def integer_value
case kind
when :i8 then value.to_i8
when :i16 then value.to_i16
when :i32 then value.to_i32
when :i64 then value.to_i64
when :u8 then value.to_u8
when :u16 then value.to_u16
when :u32 then value.to_u32
when :u64 then value.to_u64
else
raise "Bug: called 'integer_value' for non-integer literal"
end
end
def clone_without_location
NumberLiteral.new(@value, @kind)
end
def_equals value.to_f64, kind
def_hash value, kind
def self.kind_from_number(number : Number)
case number
when Int8 then :i8
when Int16 then :i16
when Int32 then :i32
when Int64 then :i64
when Int128 then :i128
when UInt8 then :u8
when UInt16 then :u16
when UInt32 then :u32
when UInt64 then :u64
when UInt128 then :u128
when Float32 then :f32
when Float64 then :f64
else raise "Unsupported Number type for NumberLiteral: #{number.class}"
end
end
end
# A char literal.
#
# "'" \w "'"
#
class CharLiteral < ASTNode
property value : Char
def initialize(@value : Char)
end
def clone_without_location
CharLiteral.new(@value)
end
def_equals_and_hash value
end
class StringLiteral < ASTNode
property value : String
def initialize(@value : String)
end
def clone_without_location
StringLiteral.new(@value)
end
def_equals_and_hash value
end
class StringInterpolation < ASTNode
property expressions : Array(ASTNode)
# Removed indentation size.
# This property is only available when this is created from heredoc.
property heredoc_indent : Int32
def initialize(@expressions : Array(ASTNode), @heredoc_indent = 0)
end
def accept_children(visitor)
@expressions.each &.accept visitor
end
def clone_without_location
StringInterpolation.new(@expressions.clone)
end
def_equals_and_hash expressions
end
class SymbolLiteral < ASTNode
property value : String
def initialize(@value : String)
end
def clone_without_location
SymbolLiteral.new(@value)
end
def_equals_and_hash value
end
# An array literal.
#
# '[' [ expression [ ',' expression ]* ] ']'
#
class ArrayLiteral < ASTNode
property elements : Array(ASTNode)
property of : ASTNode?
property name : ASTNode?
def initialize(@elements = [] of ASTNode, @of = nil, @name = nil)
end
def self.map(values, of = nil)
new(values.map { |value| (yield value).as(ASTNode) }, of: of)
end
def self.map_with_index(values)
new(values.map_with_index { |value, idx| (yield value, idx).as(ASTNode) }, of: nil)
end
def accept_children(visitor)
@name.try &.accept visitor
elements.each &.accept visitor
@of.try &.accept visitor
end
def clone_without_location
ArrayLiteral.new(@elements.clone, @of.clone, @name.clone)
end
def_equals_and_hash @elements, @of, @name
end
class HashLiteral < ASTNode
property entries : Array(Entry)
property of : Entry?
property name : ASTNode?
def initialize(@entries = [] of Entry, @of = nil, @name = nil)
end
def accept_children(visitor)
@name.try &.accept visitor
@entries.each do |entry|
entry.key.accept visitor
entry.value.accept visitor
end
if of = @of
of.key.accept visitor
of.value.accept visitor
end
end
def clone_without_location
HashLiteral.new(@entries.clone, @of.clone, @name.clone)
end
def_equals_and_hash @entries, @of, @name
record Entry, key : ASTNode, value : ASTNode
end
class NamedTupleLiteral < ASTNode
property entries : Array(Entry)
def initialize(@entries = [] of Entry)
end
def accept_children(visitor)
@entries.each do |entry|
entry.value.accept visitor
end
end
def clone_without_location
NamedTupleLiteral.new(@entries.clone)
end
def_equals_and_hash @entries
record Entry, key : String, value : ASTNode
end
class RangeLiteral < ASTNode
property from : ASTNode
property to : ASTNode
property? exclusive : Bool
def initialize(@from, @to, @exclusive)
end
def accept_children(visitor)
@from.accept visitor
@to.accept visitor
end
def clone_without_location
RangeLiteral.new(@from.clone, @to.clone, @exclusive.clone)
end
def_equals_and_hash @from, @to, @exclusive
end
class RegexLiteral < ASTNode
property value : ASTNode
property options : Regex::Options
def initialize(@value, @options = Regex::Options::None)
end
def accept_children(visitor)
@value.accept visitor
end
def clone_without_location
RegexLiteral.new(@value.clone, @options)
end
def_equals_and_hash @value, @options
end
class TupleLiteral < ASTNode
property elements : Array(ASTNode)
def initialize(@elements)
end
def self.map(values)
new(values.map { |value| (yield value).as(ASTNode) })
end
def self.map_with_index(values)
new(values.map_with_index { |value, idx| (yield value, idx).as(ASTNode) })
end
def accept_children(visitor)
elements.each &.accept visitor
end
def clone_without_location
TupleLiteral.new(elements.clone)
end
def_equals_and_hash elements
end
module SpecialVar
def special_var?
@name.starts_with? '$'
end
end
# A local variable or block argument.
class Var < ASTNode
include SpecialVar
property name : String
def initialize(@name : String)
end
def name_size
name.size
end
def clone_without_location
Var.new(@name)
end
def_equals name
def_hash name
end
# A code block.
#
# 'do' [ '|' arg [ ',' arg ]* '|' ]
# body
# 'end'
# |
# '{' [ '|' arg [ ',' arg ]* '|' ] body '}'
#
class Block < ASTNode
property args : Array(Var)
property body : ASTNode
property call : Call?
property splat_index : Int32?
def initialize(@args = [] of Var, body = nil, @splat_index = nil)
@body = Expressions.from body
end
def accept_children(visitor)
@args.each &.accept visitor
@body.accept visitor
end
def clone_without_location
Block.new(@args.clone, @body.clone, @splat_index)
end
def_equals_and_hash args, body, splat_index
end
# A method call.
#
# [ obj '.' ] name '(' ')' [ block ]
# |
# [ obj '.' ] name '(' arg [ ',' arg ]* ')' [ block ]
# |
# [ obj '.' ] name arg [ ',' arg ]* [ block ]
# |
# arg name arg
#
# The last syntax is for infix operators, and name will be
# the symbol of that operator instead of a string.
#
class Call < ASTNode
property obj : ASTNode?
property name : String
property args : Array(ASTNode)
property block : Block?
property block_arg : ASTNode?
property named_args : Array(NamedArgument)?
property name_location : Location?
@name_size = -1
property doc : String?
property visibility = Visibility::Public
property? global : Bool
property? expansion = false
property? has_parentheses = false
def initialize(@obj, @name, @args = [] of ASTNode, @block = nil, @block_arg = nil, @named_args = nil, @global : Bool = false)
if block = @block
block.call = self
end
end
def self.new(obj, name, arg : ASTNode)
new obj, name, [arg] of ASTNode
end
def self.new(obj, name, arg1 : ASTNode, arg2 : ASTNode)
new obj, name, [arg1, arg2] of ASTNode
end
def self.global(name, arg : ASTNode)
new nil, name, [arg] of ASTNode, global: true
end
def self.global(name, arg1 : ASTNode, arg2 : ASTNode)
new nil, name, [arg1, arg2] of ASTNode, global: true
end
def name_size
if @name_size == -1
@name_size = name.to_s.ends_with?('=') || name.to_s.ends_with?('@') ? name.size - 1 : name.size
end
@name_size
end
setter name_size
def accept_children(visitor)
@obj.try &.accept visitor
@args.each &.accept visitor
@named_args.try &.each &.accept visitor
@block_arg.try &.accept visitor
@block.try &.accept visitor
end
def clone_without_location
clone = Call.new(@obj.clone, @name, @args.clone, @block.clone, @block_arg.clone, @named_args.clone, @global)
clone.name_location = name_location
clone.has_parentheses = has_parentheses?
clone.name_size = name_size
clone.expansion = expansion?
clone
end
def name_end_location
loc = @name_location
return unless loc
Location.new(loc.filename, loc.line_number, loc.column_number + name_size)
end
def_equals_and_hash obj, name, args, block, block_arg, named_args, global?
end
class NamedArgument < ASTNode
property name : String
property value : ASTNode
def initialize(@name : String, @value : ASTNode)
end
def accept_children(visitor)
@value.accept visitor
end
def clone_without_location
NamedArgument.new(name, value.clone)
end
def_equals_and_hash name, value
end
# An if expression.
#
# 'if' cond
# then
# [
# 'else'
# else
# ]
# 'end'
#
# An if elsif end is parsed as an If whose
# else is another If.
class If < ASTNode
property cond : ASTNode
property then : ASTNode
property else : ASTNode
property? ternary : Bool
def initialize(@cond, a_then = nil, a_else = nil, @ternary = false)
@then = Expressions.from a_then
@else = Expressions.from a_else
end
def accept_children(visitor)
@cond.accept visitor
@then.accept visitor
@else.accept visitor
end
def clone_without_location
If.new(@cond.clone, @then.clone, @else.clone, @ternary)
end
def_equals_and_hash @cond, @then, @else
end
class Unless < ASTNode
property cond : ASTNode
property then : ASTNode
property else : ASTNode
def initialize(@cond, a_then = nil, a_else = nil)
@then = Expressions.from a_then
@else = Expressions.from a_else
end
def accept_children(visitor)
@cond.accept visitor
@then.accept visitor
@else.accept visitor
end
def clone_without_location
Unless.new(@cond.clone, @then.clone, @else.clone)
end
def_equals_and_hash @cond, @then, @else
end
# Assign expression.
#
# target '=' value
#
class Assign < ASTNode
property target : ASTNode
property value : ASTNode
property doc : String?
def initialize(@target, @value)
end
def visibility=(visibility)
target.visibility = visibility
end
def accept_children(visitor)
@target.accept visitor
@value.accept visitor
end
def end_location
@end_location || value.end_location
end
def clone_without_location
Assign.new(@target.clone, @value.clone)
end
def_equals_and_hash @target, @value
end
# Operator assign expression.
#
# target op'=' value
#
# For example if `op` is `+` then the above is:
#
# target '+=' value
class OpAssign < ASTNode
property target : ASTNode
property op : String
property value : ASTNode
property name_location : Location?
def initialize(@target, @op, @value)
end
def accept_children(visitor)
@target.accept visitor
@value.accept visitor
end
def end_location
@end_location || value.end_location
end
def clone_without_location
OpAssign.new(@target.clone, @op, @value.clone)
end
def_equals_and_hash @target, @op, @value
end
# Assign expression.
#
# target [',' target]+ '=' value [',' value]*
#
class MultiAssign < ASTNode
property targets : Array(ASTNode)
property values : Array(ASTNode)
def initialize(@targets, @values)
end
def accept_children(visitor)
@targets.each &.accept visitor
@values.each &.accept visitor
end
def end_location
@end_location || @values.last.end_location
end
def ==(other : self)
other.targets == targets && other.values == values
end
def clone_without_location
MultiAssign.new(@targets.clone, @values.clone)
end
def_hash @targets, @values
end
# An instance variable.
class InstanceVar < ASTNode
property name : String
def initialize(@name)
end
def name_size
name.size
end
def clone_without_location
InstanceVar.new(@name)
end
def_equals_and_hash name
end
class ReadInstanceVar < ASTNode
property obj : ASTNode
property name : String
def initialize(@obj, @name)
end
def accept_children(visitor)
@obj.accept visitor
end
def clone_without_location
ReadInstanceVar.new(@obj.clone, @name)
end
def_equals_and_hash @obj, @name
end
class ClassVar < ASTNode
property name : String
def initialize(@name)
end
def clone_without_location
ClassVar.new(@name)
end
def_equals_and_hash name
end
# A global variable.
class Global < ASTNode
property name : String
def initialize(@name)
end
def name_size
name.size
end
def clone_without_location
Global.new(@name)
end
def_equals_and_hash name
end
abstract class BinaryOp < ASTNode
property left : ASTNode
property right : ASTNode
def initialize(@left, @right)
end
def accept_children(visitor)
@left.accept visitor
@right.accept visitor
end
def end_location
@end_location || @right.end_location
end
def_equals_and_hash left, right
end
# Expressions and.
#
# expression '&&' expression
#
class And < BinaryOp
def clone_without_location
And.new(@left.clone, @right.clone)
end
end
# Expressions or.
#
# expression '||' expression
#
class Or < BinaryOp
def clone_without_location
Or.new(@left.clone, @right.clone)
end
end
# A def argument.
class Arg < ASTNode
include SpecialVar
# The internal name
property name : String
property external_name : String
property default_value : ASTNode?
property restriction : ASTNode?
property doc : String?
def initialize(@name : String, @default_value : ASTNode? = nil, @restriction : ASTNode? = nil, external_name : String? = nil)
@external_name = external_name || @name
end
def accept_children(visitor)
@default_value.try &.accept visitor
@restriction.try &.accept visitor
end
def name_size
name.size
end
def clone_without_location
Arg.new @name, @default_value.clone, @restriction.clone, @external_name.clone
end
def_equals_and_hash name, default_value, restriction, external_name
end
# The Proc notation in the type grammar:
#
# input1, input2, ..., inputN -> output
class ProcNotation < ASTNode
property inputs : Array(ASTNode)?
property output : ASTNode?
def initialize(@inputs = nil, @output = nil)
end
def accept_children(visitor)
@inputs.try &.each &.accept visitor
@output.try &.accept visitor
end
def clone_without_location
ProcNotation.new(@inputs.clone, @output.clone)
end
def_equals_and_hash inputs, output
end
# A method definition.
#
# 'def' [ receiver '.' ] name
# body
# 'end'
# |
# 'def' [ receiver '.' ] name '(' [ arg [ ',' arg ]* ] ')'
# body
# 'end'
#
class Def < ASTNode
property free_vars : Array(String)?
property receiver : ASTNode?
property name : String
property args : Array(Arg)
property double_splat : Arg?
property body : ASTNode
property block_arg : Arg?
property return_type : ASTNode?
property yields : Int32?
property name_location : Location?
property splat_index : Int32?
property doc : String?
property visibility = Visibility::Public
property? macro_def : Bool
property? calls_super = false
property? calls_initialize = false
property? calls_previous_def = false
property? uses_block_arg = false
property? assigns_special_var = false
property? abstract : Bool
def initialize(@name, @args = [] of Arg, body = nil, @receiver = nil, @block_arg = nil, @return_type = nil, @macro_def = false, @yields = nil, @abstract = false, @splat_index = nil, @double_splat = nil, @free_vars = nil)
@body = Expressions.from body
end
def accept_children(visitor)
@receiver.try &.accept visitor
@args.each &.accept visitor
@double_splat.try &.accept visitor
@block_arg.try &.accept visitor
@return_type.try &.accept visitor
@body.accept visitor
end
def name_size
name.size
end
def clone_without_location