-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
macros.cr
1896 lines (1535 loc) · 46 KB
/
macros.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
# The `Macros` module is a fictitious module used to document macros
# and macro methods.
#
# You can invoke a **fixed subset** of methods on AST nodes at compile-time. These methods
# are documented on the classes in this module. Additionally, methods of the
# `Macros` module are top-level methods that you can invoke, like `puts` and `run`.
module Crystal::Macros
# Compares two [semantic versions](http://semver.org/).
#
# Returns `-1`, `0` or `1` depending on whether *v1* is lower than *v2*,
# equal to *v2* or greater than *v2*.
#
# ```
# {{ compare_versions("1.10.0", "1.2.0") }} # => 1
# ```
def compare_versions(v1 : StringLiteral, v2 : StringLiteral) : NumberLiteral
end
# Outputs the current macro's buffer to the standard output. Useful for debugging
# a macro to see what's being generated.
#
# By default, the output is tried to be formatted using Crystal's
# formatter, but you can disable this by passing `false` to this method.
def debug(format = true) : Nop
end
# Gets the value of an environment variable at compile-time, or `nil` if it doesn't exist.
def env(name) : StringLiteral | NilLiteral
end
# Returns whether a [compile-time flag](https://crystal-lang.org/docs/syntax_and_semantics/compile_time_flags.html) is set.
#
# ```
# {{ flag?(:x86_64) }} # true or false
# ```
def flag?(name) : BoolLiteral
end
# Prints AST nodes at compile-time. Useful for debugging macros.
def puts(*expressions) : Nop
end
# Same as `puts`.
def p(*expressions) : Nop
end
# Same as `puts`.
def pp(*expressions) : Nop
end
# Prints macro expressions together with their values at compile-time. Useful for debugging macros.
def p!(*expressions) : Nop
end
# Same as `p!`
def pp!(*expressions) : Nop
end
# Executes a system command and returns the output as a `MacroId`.
# Gives a compile-time error if the command failed to execute.
def `(command) : MacroId
end
# :ditto:
def system(command) : MacroId
end
# Gives a compile-time error with the given *message*.
def raise(message) : NoReturn
end
# Reads a file and returns a `StringLiteral` with its contents.
#
# Gives a compile-time error if the file doesn't exist or if
# reading the file fails.
#
# To read a file relative to where the macro is defined, use:
#
# ```
# read_file("#{__DIR__}/some_file.txt")
# ```
#
# NOTE: Relative paths are resolved to the current working directory.
def read_file(filename) : StringLiteral
end
# Same as `read_file`, except that `nil` is returned on any I/O failure
# instead of issuing a compile-time failure.
def read_file?(filename) : StringLiteral | NilLiteral
end
# Compiles and execute a Crystal program and returns its output
# as a `MacroId`.
#
# The file denoted by *filename* must be a valid Crystal program.
# This macro invocation passes *args* to the program as regular
# program arguments. The program must output a valid Crystal expression.
# This output is the result of this macro invocation, as a `MacroId`.
#
# The `run` macro is useful when the subset of available macro methods
# are not enough for your purposes and you need something more powerful.
# With `run` you can read files at compile time, connect to the internet
# or to a database.
#
# A simple example:
#
# ```
# # read.cr
# puts File.read(ARGV[0])
# ```
#
# ```
# # main.cr
# macro read_file_at_compile_time(filename)
# {{ run("./read", filename).stringify }}
# end
#
# puts read_file_at_compile_time("some_file.txt")
# ```
#
# The above generates a program that will have the contents of `some_file.txt`.
# The file, however, is read at compile time and will not be needed at runtime.
#
# NOTE: the compiler is allowed to cache the executable generated for
# *filename* and only recompile it if any of the files it depends on changes
# (their modified time). This is why it's **strongly discouraged** to use a program
# for `run` that changes in subsequent compilations (for example, if it executes
# shell commands at compile time, or other macro run programs). It's also strongly
# discouraged to have a macro run program take a lot of time, because this will
# slow down compilation times. Reading files is OK, opening an HTTP connection
# at compile-time will most likely result in very slow compilations.
def run(filename, *args) : MacroId
end
# Skips the rest of the file from which it is executed.
# Typical usage is to skip files that have platform specific code,
# without having to surround the most relevant code in `{% if flag?(...) %} ... {% end %}` macro blocks.
#
# Example:
#
# ```
# # sth_for_osx.cr
# {% skip_file unless flag?(:darwin) %}
#
# # Class FooForMac will only be defined if we're compiling on OS X
# class FooForMac
# end
# ```
def skip_file : Nop
end
# This is the base class of all AST nodes. This methods are
# available to all AST nodes.
abstract class ASTNode
# Returns this node as a `MacroId`. Useful when you need an identifier
# out of a `StringLiteral`, `SymbolLiteral`, `Var` or `Call`.
#
# ```
# macro define_method(name, content)
# def {{name.id}}
# {{content}}
# end
# end
#
# define_method :foo, 1
# define_method "bar", 2
# define_method baz, 3
#
# puts foo # => prints 1
# puts bar # => prints 2
# puts baz # => prints 3
# ```
def id : MacroId
end
# Returns a `StringLiteral` that contains this node's textual representation.
# Note that invoking stringify on a string literal will return a `StringLiteral`
# that contains a string literal.
#
# ```
# macro test
# {{ "foo".stringify }}
# end
#
# puts test # prints "foo" (including the double quotes)
# ```
def stringify : StringLiteral
end
# Returns a `SymbolLiteral` that contains this node's textual representation.
#
# ```
# {{ "foo".id.symbolize }} # => :foo
# ```
def symbolize : SymbolLiteral
end
# Returns a `StringLiteral` that contains this node's name.
#
# ```
# macro test
# {{ "foo".class_name }}
# end
#
# puts test # => prints StringLiteral
# ```
def class_name : StringLiteral
end
# Returns the filename where this node is located.
# Might return `nil` if the location is not known.
def filename : StringLiteral | NilLiteral
end
# Returns the line number where this node begins.
# Might return `nil` if the location is not known.
#
# The first line number in a file is 1.
def line_number : StringLiteral | NilLiteral
end
# Returns the column number where this node begins.
# Might return `nil` if the location is not known.
#
# The first column number in a line is `1`.
def column_number : StringLiteral | NilLiteral
end
# Returns the line number where this node ends.
# Might return `nil` if the location is not known.
#
# The first line number in a file is `1`.
def end_line_number : StringLiteral | NilLiteral
end
# Returns the column number where this node ends.
# Might return `nil` if the location is not known.
#
# The first column number in a line is `1`.
def end_column_number : StringLiteral | NilLiteral
end
# Returns `true` if this node's textual representation is the same as
# the *other* node.
def ==(other : ASTNode) : BoolLiteral
end
# Returns `true` if this node's textual representation is not the same as
# the *other* node.
def !=(other : ASTNode) : BoolLiteral
end
# Gives a compile-time error with the given *message*. This will
# highlight this node in the error message.
def raise(message) : NoReturn
end
end
# The empty node. Similar to a `NilLiteral` but its textual representation
# is the empty string. This corresponds, for example, to the missing `else` branch of
# an `if` without an `else`.
class Nop < ASTNode
end
# The nil literal.
class NilLiteral < ASTNode
end
# A bool literal.
class BoolLiteral < ASTNode
end
# Any number literal.
class NumberLiteral < ASTNode
# Compares this node's value to another node's value.
def <(other : NumberLiteral) : BoolLiteral
end
# :ditto:
def <=(other : NumberLiteral) : BoolLiteral
end
# :ditto:
def >(other : NumberLiteral) : BoolLiteral
end
# :ditto:
def >=(other : NumberLiteral) : BoolLiteral
end
# :ditto:
def <=>(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#+`
def +(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#-`
def -(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#*`
def *(other : NumberLiteral) : NumberLiteral
end
# MathInterpreter only works with Integer and Number#/ : Float
#
# # Same as `Number#/`
# def /(other : NumberLiteral) : NumberLiteral
# end
# Same as `Number#//`
def //(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#%`
def %(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#&`
def &(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#|`
def |(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#^`
def ^(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#**`
def **(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#<<`
def <<(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#>>`
def >>(other : NumberLiteral) : NumberLiteral
end
# Same as `Number#+`
def + : NumberLiteral
end
# Same as `Number#-`
def - : NumberLiteral
end
# Same as `Number#~`
def ~ : NumberLiteral
end
# The type of the literal: `:i32`, `:u16`, `:f32`, `:f64`, etc.
def kind : SymbolLiteral
end
end
# A character literal.
class CharLiteral < ASTNode
# Returns a `MacroId` for this character's contents.
def id : MacroId
end
end
# A string literal.
class StringLiteral < ASTNode
# Returns a `MacroId` for this string's contents.
def id : MacroId
end
# Similar to `String#[]`.
def [](range : RangeLiteral) : StringLiteral
end
# Similar to `String#=~`.
def =~(range : RegexLiteral) : BoolLiteral
end
# Similar to `String#>`
def >(other : StringLiteral | MacroId) : BoolLiteral
end
# Similar to `String#<`
def <(other : StringLiteral | MacroId) : BoolLiteral
end
# Similar to `String#+`.
def +(other : StringLiteral | CharLiteral) : StringLiteral
end
# Similar to `String#camelcase`.
def camelcase(*, lower : BoolLiteral = false) : StringLiteral
end
# Similar to `String#capitalize`.
def capitalize : StringLiteral
end
# Similar to `String#chars`.
def chars : ArrayLiteral(CharLiteral)
end
# Similar to `String#chomp`.
def chomp : StringLiteral
end
# Similar to `String#count`.
def count(other : CharLiteral) : NumberLiteral
end
# Similar to `String#downcase`.
def downcase : StringLiteral
end
# Similar to `String#empty?`.
def empty? : BoolLiteral
end
# Similar to `String#ends_with?`.
def ends_with?(other : StringLiteral | CharLiteral) : BoolLiteral
end
# Similar to `String#gsub`.
def gsub(regex : RegexLiteral, replacement : StringLiteral) : StringLiteral
end
# Similar to `String#includes?`.
def includes?(search : StringLiteral | CharLiteral) : BoolLiteral
end
# Similar to `String#size`.
def size : NumberLiteral
end
# Similar to `String#lines`.
def lines : ArrayLiteral(StringLiteral)
end
# Similar to `String#split`.
def split : ArrayLiteral(StringLiteral)
end
# Similar to `String#split`.
def split(node : ASTNode) : ArrayLiteral(StringLiteral)
end
# Similar to `String#starts_with?`.
def starts_with?(other : StringLiteral | CharLiteral) : BoolLiteral
end
# Similar to `String#strip`.
def strip : StringLiteral
end
# Similar to `String#to_i`.
def to_i(base = 10)
end
# Similar to `String#tr`.
def tr(from : StringLiteral, to : StringLiteral) : StringLiteral
end
# Similar to `String#underscore`.
def underscore : StringLiteral
end
# Similar to `String#upcase`.
def upcase : StringLiteral
end
end
# An interpolated string like `"Hello, #{name}!"`.
class StringInterpolation < ASTNode
# Returns a list of expressions that comprise the interpolated string.
#
# These alternate between `StringLiteral` for the plaintext parts and
# `ASTNode`s of any type for the interpolated expressions.
def expressions : ArrayLiteral(ASTNode)
end
end
# A symbol literal.
class SymbolLiteral < ASTNode
# Returns a `MacroId` for this symbol's contents.
def id : MacroId
end
# Similar to `String#[]`.
def [](range : RangeLiteral) : SymbolLiteral
end
# Similar to `String#=~`.
def =~(range : RegexLiteral) : BoolLiteral
end
# Similar to `String#+`.
def +(other : StringLiteral | CharLiteral) : SymbolLiteral
end
# Similar to `String#capitalize`.
def capitalize : SymbolLiteral
end
# Similar to `String#chars`.
def chars : ArrayLiteral(CharLiteral)
end
# Similar to `String#chomp`.
def chomp : SymbolLiteral
end
# Similar to `String#downcase`.
def downcase : SymbolLiteral
end
# Similar to `String#empty?`.
def empty? : BoolLiteral
end
# Similar to `String#ends_with?`.
def ends_with?(other : StringLiteral | CharLiteral) : BoolLiteral
end
# Similar to `String#gsub`.
def gsub(regex : RegexLiteral, replacement : StringLiteral) : SymbolLiteral
end
# Similar to `String#includes?`.
def includes?(search : StringLiteral | CharLiteral) : BoolLiteral
end
# Similar to `String#size`.
def size : NumberLiteral
end
# Similar to `String#lines`.
def lines : ArrayLiteral(StringLiteral)
end
# Similar to `String#split`.
def split : ArrayLiteral(StringLiteral)
end
# Similar to `String#split`.
def split(node : ASTNode) : ArrayLiteral(StringLiteral)
end
# Similar to `String#starts_with?`.
def starts_with?(other : StringLiteral | CharLiteral) : BoolLiteral
end
# Similar to `String#strip`.
def strip : SymbolLiteral
end
# Similar to `String#tr`.
def tr(from : StringLiteral, to : StringLiteral) : SymbolLiteral
end
# Similar to `String#upcase`.
def upcase : SymbolLiteral
end
end
# An array literal.
class ArrayLiteral < ASTNode
# Similar to `Enumerable#any?`
def any?(&block) : BoolLiteral
end
# Similar to `Enumerable#all?`
def all?(&block) : BoolLiteral
end
# Returns a `MacroId` with all of this array's elements joined
# by commas.
#
# If *trailing_string* is given, it will be appended to
# the result unless this array is empty. This lets you
# splat an array and optionally write a trailing comma
# if needed.
def splat(trailing_string : StringLiteral = nil) : MacroId
end
# Similar to `Array#clear`
def clear : ArrayLiteral
end
# Similar to `Array#empty?`
def empty? : BoolLiteral
end
# Similar to `Enumerable#find`
def find(&block) : ASTNode | NilLiteral
end
# Similar to `Array#first`, but returns a `NilLiteral` if the array is empty.
def first : ASTNode | NilLiteral
end
# Similar to `Enumerable#includes?(obj)`.
def includes?(node : ASTNode) : BoolLiteral
end
# Similar to `Enumerable#join`
def join(separator) : StringLiteral
end
# Similar to `Array#last`, but returns a `NilLiteral` if the array is empty.
def last : ASTNode | NilLiteral
end
# Similar to `Array#size`
def size : NumberLiteral
end
# Similar to `Enumerable#map`
def map(&block) : ArrayLiteral
end
# Similar to `Enumerable#map_with_index`
def map_with_index(&block) : ArrayLiteral
end
# Similar to `Enumerable#select`
def select(&block) : ArrayLiteral
end
# Similar to `Enumerable#reject`
def reject(&block) : ArrayLiteral
end
# Similar to `Enumerable#reduce`
def reduce(&block) : ASTNode
end
# Similar to `Enumerable#reduce`
def reduce(memo : ASTNode, &block) : ASTNode
end
# Similar to `Array#shuffle`
def shuffle : ArrayLiteral
end
# Similar to `Array#sort`
def sort : ArrayLiteral
end
# Similar to `Array#sort_by`
def sort_by(&block) : ArrayLiteral
end
# Similar to `Array#uniq`
def uniq : ArrayLiteral
end
# Similar to `Array#[]`, but returns `NilLiteral` on out of bounds.
def [](index : NumberLiteral) : ASTNode
end
# Similar to `Array#[]=`.
def []=(index : NumberLiteral, value : ASTNode)
end
# Similar to `Array#unshift`.
def unshift(value : ASTNode) : ArrayLiteral
end
# Similar to `Array#push`.
def push(value : ASTNode) : ArrayLiteral
end
# Similar to `Array#<<`.
def <<(value : ASTNode) : ArrayLiteral
end
# Similar to `Array#+`.
def +(other : ArrayLiteral) : ArrayLiteral
end
# Returns the type specified at the end of the array literal, if any.
#
# This refers to the part after brackets in `[] of String`.
def of : ASTNode | Nop
end
# Returns the type that receives the items of the array.
#
# This refers to the part before brackets in `MyArray{1, 2, 3}`
def type : Path | Nop
end
end
# A hash literal.
class HashLiteral < ASTNode
# Similar to `Hash#clear`
def clear : HashLiteral
end
# Similar to `Hash#empty?`
def empty? : BoolLiteral
end
# Similar to `Hash#keys`
def keys : ArrayLiteral
end
# Similar to `Hash#size`
def size : NumberLiteral
end
# Similar to `Hash#to_a`
def to_a : ArrayLiteral(TupleLiteral)
end
# Similar to `Hash#values`
def values : ArrayLiteral
end
# Similar to `Hash#map`
def map : ArrayLiteral
end
# Similar to `Hash#[]`
def [](key : ASTNode) : ASTNode
end
# Similar to `Hash#[]=`
def []=(key : ASTNode) : ASTNode
end
# Returns the type specified at the end of the Hash literal, if any.
#
# This refers to the key type after brackets in `{} of String => Int32`.
def of_key : ASTNode | Nop
end
# Returns the type specified at the end of the Hash literal, if any.
#
# This refers to the value type after brackets in `{} of String => Int32`.
def of_value : ASTNode | Nop
end
# Returns the type that receives the items of the array.
#
# This refers to the part before brackets in `MyHash{'a' => 1, 'b' => 2}`
def type : Path | Nop
end
# Returns a `MacroId` with all of this hash elements joined
# by commas.
#
# If *trailing_string* is given, it will be appended to
# the result unless this hash is empty. This lets you
# splat a hash and optionally write a trailing comma
# if needed.
def double_splat(trailing_string : StringLiteral = nil) : MacroId
end
end
# A named tuple literal.
class NamedTupleLiteral < ASTNode
# Similar to `NamedTuple#empty?`
def empty? : BoolLiteral
end
# Similar to `NamedTuple#keys`
def keys : ArrayLiteral
end
# Similar to `NamedTuple#size`
def size : NumberLiteral
end
# Similar to `NamedTuple#to_a`
def to_a : ArrayLiteral(TupleLiteral)
end
# Similar to `NamedTuple#values`
def values : ArrayLiteral
end
# Similar to `NamedTuple#map`
def map : ArrayLiteral
end
# Similar to `HashLiteral#double_splat`
def double_splat(trailing_string : StringLiteral = nil) : MacroId
end
# Similar to `NamedTuple#[]` but returns `NilLiteral` if *key* is undefined.
def [](key : SymbolLiteral | StringLiteral | MacroId) : ASTNode
end
# Adds or replaces a key.
def []=(key : SymbolLiteral | StringLiteral | MacroId) : ASTNode
end
end
# A range literal.
class RangeLiteral < ASTNode
# Similar to `Range#begin`
def begin : ASTNode
end
# Similar to `Range#end`
def end : ASTNode
end
# Similar to `Range#excludes_end?`
def excludes_end? : ASTNode
end
# Similar to `Enumerable#map` for a `Range`.
# Only works on ranges of `NumberLiteral`s considered as integers.
def map : ArrayLiteral
end
# Similar to `Enumerable#to_a` for a `Range`.
# Only works on ranges of `NumberLiteral`s considered as integers.
def to_a : ArrayLiteral
end
end
# A regular expression literal.
class RegexLiteral < ASTNode
# Similar to `Regex#source`.
def source : StringLiteral | StringInterpolation
end
# Similar to `Regex#options`,
# but returns an array of symbols such as `[:i, :m, :x]`
def options : ArrayLiteral(SymbolLiteral)
end
end
# A tuple literal.
#
# Its macro methods are nearly the same as `ArrayLiteral`.
class TupleLiteral < ASTNode
end
# A fictitious node representing a variable or instance
# variable, together with type information.
class MetaVar < ASTNode
# Returns the name of this variable.
def name : MacroId
end
# Returns the type of this variable, if known, or `nil`.
def type : TypeNode | NilLiteral
end
# Returns the default value of this variable.
# Note that if the variable doesn't have a default value,
# or the default value is `nil`, a `NilLiteral` will be
# returned. To distinguish between these cases, use
# `has_default_value?`.
def default_value : ASTNode
end
# Returns whether this variable has a default value
# (which can in turn be `nil`).
def has_default_value? : BoolLiteral
end
# Returns the last `Annotation` with the given `type`
# attached to this variable or `NilLiteral` if there are none.
def annotation(type : TypeNode) : Annotation | NilLiteral
end
# Returns an array of annotations with the given `type`
# attached to this variable, or an empty `ArrayLiteral` if there are none.
def annotations(type : TypeNode) : ArrayLiteral(Annotation)
end
end
# An annotation on top of a type or variable.
class Annotation < ASTNode
# Returns the value of a positional argument,
# or NilLiteral if out of bounds.
def [](index : NumberLiteral) : ASTNode
end
# Returns the value of a named argument,
# or NilLiteral if the named argument isn't
# used on `self`.
def [](name : SymbolLiteral | StringLiteral | MacroId) : ASTNode
end
# Returns a `TupleLiteral` representing the positional arguments on `self`.
def args : TupleLiteral
end
# Returns a `NamedTupleLiteral` representing the named arguments on `self`.
def named_args : NamedTupleLiteral
end
end
# A local variable or block argument.
class Var < ASTNode
# Returns this var's name as a `MacroId`.
def id : MacroId
end
end
# A code block.
class Block < ASTNode
# Returns the block's body, if any.
def body : ASTNode
end
# Returns the blocks arguments.
def args : ArrayLiteral(MacroId)
end
# Returns the index of the argument with a *splat, if any.
def splat_index : NumberLiteral | NilLiteral
end
end
# A group of expressions.
class Expressions < ASTNode
# Returns the list of expressions in this node
def expressions : ArrayLiteral(ASTNode)
end
end
# A method call.
class Call < ASTNode
# Returns this call's name as a `MacroId`.
def id : MacroId
end
# Returns the method name of this call.
def name : MacroId
end
# Returns this call's receiver, if any.
def receiver : ASTNode | Nop
end
# Returns this call's arguments.
def args : ArrayLiteral
end
# Returns this call's named arguments.
def named_args : ArrayLiteral(NamedArgument)
end
# Returns this call's block, if any.
def block : Block | Nop
end
# Returns this call's block argument, if any
def block_arg : ASTNode | Nop
end
end
# A call's named argument.
class NamedArgument < ASTNode
# Returns this named argument name.
def name : MacroId
end
# Returns this named argument value.
def value : ASTNode
end
end
# An if expression.
class If < ASTNode
# Returns this if's condition.
def cond : ASTNode
end
# Returns this if's `then` clause's body.
def then : ASTNode
end
# Returns this if's `else` clause's body.
def else : ASTNode
end
end
# class Unless < ASTNode
# end
# Assign expression.
class Assign < ASTNode
# Returns the target assigned to.
def target : ASTNode
end
# Returns the value that is being assigned.
def value : ASTNode