parser: new class AOperatorMethid to factorize operator methods
[nit.git] / src / parser / parser_nodes.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # AST nodes of the Nit language
16 # Was previously based on parser_abs.nit.
17 module parser_nodes
18
19 import location
20 import ordered_tree
21
22 # Root of the AST class-hierarchy
23 abstract class ANode
24 # Location is set during AST building. Once built, location cannon be null.
25 # However, manual instantiated nodes may need more care.
26 var location: Location is writable, noinit
27
28 # The location of the important part of the node (identifier or whatever)
29 fun hot_location: Location do return location
30
31 # Display a message for the colored location of the node
32 fun debug(message: String)
33 do
34 sys.stderr.write "{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
35 end
36
37 # Write the subtree on stdout.
38 # See `ASTDump`
39 fun dump_tree
40 do
41 var d = new ASTDump
42 d.enter_visit(self)
43 d.write_to(sys.stdout)
44 end
45
46 # Parent of the node in the AST
47 var parent: nullable ANode = null
48
49 # The topmost ancestor of the element
50 # This just apply `parent` until the first one
51 fun root: ANode
52 do
53 var res = self
54 loop
55 var p = res.parent
56 if p == null then return res
57 res = p
58 end
59 end
60
61 # The most specific common parent between `self` and `other`
62 # Return null if the two node are unrelated (distinct root)
63 fun common_parent(other: ANode): nullable ANode
64 do
65 # First, get the same depth
66 var s: nullable ANode = self
67 var o: nullable ANode = other
68 var d = s.depth - o.depth
69 while d > 0 do
70 s = s.parent
71 d -= 1
72 end
73 while d < 0 do
74 o = o.parent
75 d += 1
76 end
77 assert o.depth == s.depth
78 # Second, go up until same in found
79 while s != o do
80 s = s.parent
81 o = o.parent
82 end
83 return s
84 end
85
86 # Number of nodes between `self` and the `root` of the AST
87 # ENSURE `self == self.root implies result == 0 `
88 # ENSURE `self != self.root implies result == self.parent.depth + 1`
89 fun depth: Int
90 do
91 var n = self
92 var res = 0
93 loop
94 var p = n.parent
95 if p == null then return res
96 n = p
97 res += 1
98 end
99 end
100
101 # Replace a child with an other node in the AST
102 private fun replace_child(old_child: ANode, new_child: nullable ANode) is abstract
103
104 # Detach a node from its parent
105 # Aborts if the node is not detachable. use `replace_with` instead
106 # REQUIRE: parent != null
107 # REQUIRE: is_detachable
108 # ENDURE: parent == null
109 fun detach
110 do
111 assert parent != null
112 parent.replace_child(self, null)
113 parent = null
114 end
115
116 # Replace itself with an other node in the AST
117 # REQUIRE: parent != null
118 # ENSURE: node.parent == old(parent)
119 # ENSURE: parent == null
120 fun replace_with(node: ANode)
121 do
122 assert parent != null
123 parent.replace_child(self, node)
124 parent = null
125 end
126
127 # Visit all nodes in order.
128 # Thus, call `v.enter_visit(e)` for each child `e`
129 fun visit_all(v: Visitor) is abstract
130
131 # Do a deep search and return an array of tokens that match a given text
132 fun collect_tokens_by_text(text: String): Array[Token]
133 do
134 var v = new CollectTokensByTextVisitor(text)
135 v.enter_visit(self)
136 return v.result
137 end
138
139 # Do a deep search and return an array of node that are annotated
140 # The attached node can be retrieved by two invocations of parent
141 fun collect_annotations_by_name(name: String): Array[AAnnotation]
142 do
143 var v = new CollectAnnotationsByNameVisitor(name)
144 v.enter_visit(self)
145 return v.result
146 end
147 end
148
149 private class CollectTokensByTextVisitor
150 super Visitor
151 var text: String
152 var result = new Array[Token]
153 redef fun visit(node)
154 do
155 node.visit_all(self)
156 if node isa Token and node.text == text then result.add(node)
157 end
158 end
159
160 private class CollectAnnotationsByNameVisitor
161 super Visitor
162 var name: String
163 var result = new Array[AAnnotation]
164 redef fun visit(node)
165 do
166 node.visit_all(self)
167 if node isa AAnnotation and node.n_atid.n_id.text == name then result.add(node)
168 end
169 end
170
171 # A helper class to handle (print) Nit AST as an OrderedTree
172 class ASTDump
173 super Visitor
174 super OrderedTree[ANode]
175
176 # Reference to the last parent in the Ordered Tree
177 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
178 private var last_parent: nullable ANode = null
179
180 redef fun visit(n)
181 do
182 var p = last_parent
183 add(p, n)
184 last_parent = n
185 n.visit_all(self)
186 last_parent = p
187 end
188
189 redef fun display(n)
190 do
191 if n isa Token then
192 return "{n.class_name} \"{n.text.escape_to_c}\" @{n.location}"
193 else
194 return "{n.class_name} @{n.location}"
195 end
196 end
197 end
198
199 # A sequence of nodes
200 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
201 class ANodes[E: ANode]
202 super Sequence[E]
203 private var parent: ANode
204 private var items = new Array[E]
205 redef fun iterator do return items.iterator
206 redef fun length do return items.length
207 redef fun is_empty do return items.is_empty
208 redef fun push(e)
209 do
210 hook_add(e)
211 items.push(e)
212 end
213 redef fun pop
214 do
215 var res = items.pop
216 hook_remove(res)
217 return res
218 end
219 redef fun unshift(e)
220 do
221 hook_add(e)
222 items.unshift(e)
223 end
224 redef fun shift
225 do
226 var res = items.shift
227 hook_remove(res)
228 return res
229 end
230 redef fun has(e)
231 do
232 return items.has(e)
233 end
234 redef fun [](index)
235 do
236 return items[index]
237 end
238 redef fun []=(index, e)
239 do
240 hook_remove(self[index])
241 hook_add(e)
242 items[index]=e
243 end
244 redef fun remove_at(index)
245 do
246 hook_remove(items[index])
247 items.remove_at(index)
248 end
249 private fun hook_add(e: E)
250 do
251 #assert e.parent == null
252 e.parent = parent
253 end
254 private fun hook_remove(e: E)
255 do
256 assert e.parent == parent
257 e.parent = null
258 end
259
260 # Used in parent constructor to fill elements
261 private fun unsafe_add_all(nodes: Collection[Object])
262 do
263 var parent = self.parent
264 for n in nodes do
265 assert n isa E
266 add n
267 n.parent = parent
268 end
269 end
270
271 private fun replace_child(old_child: ANode, new_child: nullable ANode): Bool
272 do
273 var parent = self.parent
274 for i in [0..length[ do
275 if self[i] == old_child then
276 if new_child != null then
277 assert new_child isa E
278 self[i] = new_child
279 new_child.parent = parent
280 else
281 self.remove_at(i)
282 end
283 return true
284 end
285 end
286 return false
287 end
288
289 private fun visit_all(v: Visitor)
290 do
291 for n in self do v.enter_visit(n)
292 end
293 end
294
295 # Ancestor of all tokens
296 # A token is a node that has a `text` but no children.
297 abstract class Token
298 super ANode
299
300 # The raw content on the token
301 fun text: String is abstract
302
303 # The raw content on the token
304 fun text=(text: String) is abstract
305
306 # The previous token in the Lexer.
307 # May have disappeared in the AST
308 var prev_token: nullable Token = null
309
310 # The next token in the Lexer.
311 # May have disappeared in the AST
312 var next_token: nullable Token = null
313
314 # The verbatim blank text between `prev_token` and `self`
315 fun blank_before: String
316 do
317 if prev_token == null then return ""
318 var from = prev_token.location.pend+1
319 var to = location.pstart
320 return location.file.string.substring(from,to-from)
321 end
322
323 redef fun to_s: String do
324 return "'{text}'"
325 end
326
327 redef fun visit_all(v: Visitor) do end
328 redef fun replace_child(old_child: ANode, new_child: nullable ANode) do end
329 end
330
331 redef class SourceFile
332 # The first token parser by the lexer
333 # May have disappeared in the final AST
334 var first_token: nullable Token = null
335
336 # The first token parser by the lexer
337 # May have disappeared in the final AST
338 var last_token: nullable Token = null
339 end
340
341 # Ancestor of all productions
342 # A production is a node without text but that usually has children.
343 abstract class Prod
344 super ANode
345
346 # All the annotations attached directly to the node
347 var n_annotations: nullable AAnnotations = null is writable
348
349 # Return all its annotations of a given name in the order of their declaration
350 # Retun an empty array if no such an annotation.
351 fun get_annotations(name: String): Array[AAnnotation]
352 do
353 var res = new Array[AAnnotation]
354 var nas = n_annotations
355 if nas != null then for na in nas.n_items do
356 if na.name != name then continue
357 res.add(na)
358 end
359 if self isa AClassdef then for na in n_propdefs do
360 if na isa AAnnotPropdef then
361 if na.name != name then continue
362 res.add na
363 end
364 end
365
366 return res
367 end
368
369 redef fun replace_with(n: ANode)
370 do
371 super
372 assert n isa Prod
373 if not isset n._location and isset _location then n._location = _location
374 end
375 end
376
377 # Abstract standard visitor on the AST
378 abstract class Visitor
379 # What the visitor do when a node is visited
380 # Concrete visitors should implement this method.
381 # @toimplement
382 protected fun visit(e: ANode) is abstract
383
384 # Ask the visitor to visit a given node.
385 # Usually automatically called by visit_all* methods.
386 # This method should not be redefined
387 fun enter_visit(e: nullable ANode)
388 do
389 if e == null then return
390 var old = _current_node
391 _current_node = e
392 visit(e)
393 _current_node = old
394 end
395
396 # The current visited node
397 var current_node: nullable ANode = null is writable
398 end
399
400 # Token of end of line (basically `\n`)
401 class TEol
402 super Token
403 redef fun to_s
404 do
405 return "end of line"
406 end
407 end
408
409 # Token of a line of comments
410 # Starts with the `#` and contains the final end-of-line (if any)
411 class TComment
412 super Token
413 end
414
415 # A token associated with a keyword
416 abstract class TokenKeyword
417 super Token
418 redef fun to_s
419 do
420 return "keyword '{text}'"
421 end
422 end
423
424 # The deprecated keyword `package`.
425 class TKwpackage
426 super TokenKeyword
427 end
428
429 # The keyword `module`
430 class TKwmodule
431 super TokenKeyword
432 end
433
434 # The keyword `import`
435 class TKwimport
436 super TokenKeyword
437 end
438
439 # The keyword `class`
440 class TKwclass
441 super TokenKeyword
442 end
443
444 # The keyword `abstract`
445 class TKwabstract
446 super TokenKeyword
447 end
448
449 # The keyword `interface`
450 class TKwinterface
451 super TokenKeyword
452 end
453
454 # The keywords `enum` ane `universal`
455 class TKwenum
456 super TokenKeyword
457 end
458
459 # The keyword `end`
460 class TKwend
461 super TokenKeyword
462 end
463
464 # The keyword `fun`
465 class TKwmeth
466 super TokenKeyword
467 end
468
469 # The keyword `type`
470 class TKwtype
471 super TokenKeyword
472 end
473
474 # The keyword `init`
475 class TKwinit
476 super TokenKeyword
477 end
478
479 # The keyword `redef`
480 class TKwredef
481 super TokenKeyword
482 end
483
484 # The keyword `is`
485 class TKwis
486 super TokenKeyword
487 end
488
489 # The keyword `do`
490 class TKwdo
491 super TokenKeyword
492 end
493
494 # The keyword `var`
495 class TKwvar
496 super TokenKeyword
497 end
498
499 # The keyword `extern`
500 class TKwextern
501 super TokenKeyword
502 end
503
504 # The keyword `public`
505 class TKwpublic
506 super TokenKeyword
507 end
508
509 # The keyword `protected`
510 class TKwprotected
511 super TokenKeyword
512 end
513
514 # The keyword `private`
515 class TKwprivate
516 super TokenKeyword
517 end
518
519 # The keyword `intrude`
520 class TKwintrude
521 super TokenKeyword
522 end
523
524 # The keyword `if`
525 class TKwif
526 super TokenKeyword
527 end
528
529 # The keyword `then`
530 class TKwthen
531 super TokenKeyword
532 end
533
534 # The keyword `else`
535 class TKwelse
536 super TokenKeyword
537 end
538
539 # The keyword `while`
540 class TKwwhile
541 super TokenKeyword
542 end
543
544 # The keyword `loop`
545 class TKwloop
546 super TokenKeyword
547 end
548
549 # The keyword `for`
550 class TKwfor
551 super TokenKeyword
552 end
553
554 # The keyword `in`
555 class TKwin
556 super TokenKeyword
557 end
558
559 # The keyword `and`
560 class TKwand
561 super TokenKeyword
562 end
563
564 # The keyword `or`
565 class TKwor
566 super TokenKeyword
567 end
568
569 # The keyword `implies`
570 class TKwimplies
571 super TokenKeyword
572 end
573
574 # The keyword `not`
575 class TKwnot
576 super TokenKeyword
577 end
578
579 # The keyword `return`
580 class TKwreturn
581 super TokenKeyword
582 end
583
584 # The keyword `continue`
585 class TKwcontinue
586 super TokenKeyword
587 end
588
589 # The keyword `break`
590 class TKwbreak
591 super TokenKeyword
592 end
593
594 # The keyword `abort`
595 class TKwabort
596 super TokenKeyword
597 end
598
599 # The keyword `assert`
600 class TKwassert
601 super TokenKeyword
602 end
603
604 # The keyword `new`
605 class TKwnew
606 super TokenKeyword
607 end
608
609 # The keyword `isa`
610 class TKwisa
611 super TokenKeyword
612 end
613
614 # The keyword `once`
615 class TKwonce
616 super TokenKeyword
617 end
618
619 # The keyword `super`
620 class TKwsuper
621 super TokenKeyword
622 end
623
624 # The keyword `self`
625 class TKwself
626 super TokenKeyword
627 end
628
629 # The keyword `true`
630 class TKwtrue
631 super TokenKeyword
632 end
633
634 # The keyword `false`
635 class TKwfalse
636 super TokenKeyword
637 end
638
639 # The keyword `null`
640 class TKwnull
641 super TokenKeyword
642 end
643
644 # The keyword `as`
645 class TKwas
646 super TokenKeyword
647 end
648
649 # The keyword `nullable`
650 class TKwnullable
651 super TokenKeyword
652 end
653
654 # The keyword `isset`
655 class TKwisset
656 super TokenKeyword
657 end
658
659 # The keyword `label`
660 class TKwlabel
661 super TokenKeyword
662 end
663
664 # The keyword `with`
665 class TKwwith
666 super TokenKeyword
667 end
668
669 # The special keyword `__DEBUG__`
670 class TKwdebug
671 super Token
672 end
673
674 # The symbol `(`
675 class TOpar
676 super Token
677 end
678
679 # The symbol `)`
680 class TCpar
681 super Token
682 end
683
684 # The symbol `[`
685 class TObra
686 super Token
687 end
688
689 # The symbol `]`
690 class TCbra
691 super Token
692 end
693
694 # The symbol `,`
695 class TComma
696 super Token
697 end
698
699 # The symbol `:`
700 class TColumn
701 super Token
702 end
703
704 # The symbol `::`
705 class TQuad
706 super Token
707 end
708
709 # The symbol `=`
710 class TAssign
711 super Token
712 end
713
714 # A token associated with an operator (and other lookalike symbols)
715 abstract class TokenOperator
716 super Token
717 redef fun to_s
718 do
719 return "operator '{text}'"
720 end
721 end
722
723 # The operator `+=`
724 class TPluseq
725 super TokenOperator
726 end
727
728 # The operator `-=`
729 class TMinuseq
730 super TokenOperator
731 end
732
733 # The operator `*=`
734 class TStareq
735 super TokenOperator
736 end
737
738 # The operator `/=`
739 class TSlasheq
740 super TokenOperator
741 end
742
743 # The operator `%=`
744 class TPercenteq
745 super TokenOperator
746 end
747
748 # The operator `**=`
749 class TStarstareq
750 super TokenOperator
751 end
752
753 # The operator `<<=`
754 class TLleq
755 super TokenOperator
756 end
757
758 # The operator `>>=`
759 class TGgeq
760 super TokenOperator
761 end
762
763 # The symbol `...`
764 class TDotdotdot
765 super Token
766 end
767
768 # The symbol `..`
769 class TDotdot
770 super Token
771 end
772
773 # The symbol `.`
774 class TDot
775 super Token
776 end
777
778 # The operator `+`
779 class TPlus
780 super TokenOperator
781 end
782
783 # The operator `-`
784 class TMinus
785 super TokenOperator
786 end
787
788 # The operator `*`
789 class TStar
790 super TokenOperator
791 end
792
793 # The operator `**`
794 class TStarstar
795 super TokenOperator
796 end
797
798 # The operator `/`
799 class TSlash
800 super TokenOperator
801 end
802
803 # The operator `+%
804 class TPercent
805 super TokenOperator
806 end
807
808 # The operator `==`
809 class TEq
810 super TokenOperator
811 end
812
813 # The operator `!=`
814 class TNe
815 super TokenOperator
816 end
817
818 # The operator `<`
819 class TLt
820 super TokenOperator
821 end
822
823 # The operator `<=`
824 class TLe
825 super TokenOperator
826 end
827
828 # The operator `<<`
829 class TLl
830 super TokenOperator
831 end
832
833 # The operator `>`
834 class TGt
835 super TokenOperator
836 end
837
838 # The operator `>=`
839 class TGe
840 super TokenOperator
841 end
842
843 # The operator `>>`
844 class TGg
845 super TokenOperator
846 end
847
848 # The operator `<=>`
849 class TStarship
850 super TokenOperator
851 end
852
853 # The operator `!`
854 class TBang
855 super TokenOperator
856 end
857
858 # The symbol `@`
859 class TAt
860 super Token
861 end
862
863 # A class (or formal type) identifier. They start with an uppercase.
864 class TClassid
865 super Token
866 redef fun to_s
867 do
868 do return "type identifier '{text}'"
869 end
870 end
871
872 # A standard identifier (variable, method...). They start with a lowercase.
873 class TId
874 super Token
875 redef fun to_s
876 do
877 do return "identifier '{text}'"
878 end
879 end
880
881 # An attribute identifier. They start with an underscore.
882 class TAttrid
883 super Token
884 redef fun to_s
885 do
886 do return "attribute '{text}'"
887 end
888 end
889
890 # A token of a literal value (string, integer, etc).
891 abstract class TokenLiteral
892 super Token
893 redef fun to_s
894 do
895 do return "literal value '{text}'"
896 end
897 end
898
899 # A literal decimal integer
900 class TNumber
901 super TokenLiteral
902 end
903
904 # A literal hexadecimal integer
905 class THexNumber
906 super TokenLiteral
907 end
908
909 # A literal floating point number
910 class TFloat
911 super TokenLiteral
912 end
913
914 # A literal character
915 class TChar
916 super TokenLiteral
917 end
918
919 # A literal string
920 class TString
921 super TokenLiteral
922 end
923
924 # The starting part of a super string (between `"` and `{`)
925 class TStartString
926 super TokenLiteral
927 end
928
929 # The middle part of a super string (between `}` and `{`)
930 class TMidString
931 super TokenLiteral
932 end
933
934 # The final part of a super string (between `}` and `"`)
935 class TEndString
936 super TokenLiteral
937 end
938
939 # A malformed string
940 class TBadString
941 super Token
942 redef fun to_s
943 do
944 do return "malformed string {text}"
945 end
946 end
947
948 # A malformed char
949 class TBadChar
950 super Token
951 redef fun to_s
952 do
953 do return "malformed character {text}"
954 end
955 end
956
957 # A extern code block
958 class TExternCodeSegment
959 super Token
960 end
961
962 # A end of file
963 class EOF
964 super Token
965 redef fun to_s
966 do
967 return "end of file"
968 end
969 end
970
971 # A mark of an error
972 class AError
973 super EOF
974 end
975 # A lexical error (unexpected character)
976 class ALexerError
977 super AError
978 end
979 # A syntactic error (unexpected token)
980 class AParserError
981 super AError
982 end
983
984 # The main node of a Nit source-file
985 class AModule
986 super Prod
987
988 # The declaration part of the module
989 var n_moduledecl: nullable AModuledecl = null is writable
990
991 # List of importation clauses
992 var n_imports = new ANodes[AImport](self)
993
994 # List of extern blocks
995 var n_extern_code_blocks = new ANodes[AExternCodeBlock](self)
996
997 # List of class definition (including top-level methods and the main)
998 var n_classdefs = new ANodes[AClassdef](self)
999 end
1000
1001 # Abstract class for definition of entities
1002 abstract class ADefinition
1003 super Prod
1004 # The documentation
1005 var n_doc: nullable ADoc = null is writable
1006
1007 # The `redef` keyword
1008 var n_kwredef: nullable TKwredef = null is writable
1009
1010 # The declared visibility
1011 var n_visibility: nullable AVisibility = null is writable
1012 end
1013
1014 # The declaration of the module with the documentation, name, and annotations
1015 class AModuledecl
1016 super ADefinition
1017
1018 # The `module` keyword
1019 var n_kwmodule: TKwmodule is writable, noinit
1020
1021 # The declared module name
1022 var n_name: AModuleName is writable, noinit
1023 end
1024
1025 # A import clause of a module
1026 abstract class AImport
1027 super Prod
1028
1029 # The declared visibility
1030 var n_visibility: AVisibility is writable, noinit
1031
1032 # The `import` keyword
1033 var n_kwimport: TKwimport is writable, noinit
1034 end
1035
1036 # A standard import clause. eg `import x`
1037 class AStdImport
1038 super AImport
1039 # The imported module name
1040 var n_name: AModuleName is writable, noinit
1041 end
1042
1043 # The special import clause of the kernel module. eg `import end`
1044 class ANoImport
1045 super AImport
1046 # The `end` keyword, that indicate the root module
1047 var n_kwend: TKwend is writable, noinit
1048 end
1049
1050 # A visibility modifier
1051 #
1052 # The public visibility is an empty production (no keyword).
1053 #
1054 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1055 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1056 abstract class AVisibility
1057 super Prod
1058 end
1059
1060 # An implicit or explicit public visibility modifier
1061 class APublicVisibility
1062 super AVisibility
1063 # The `public` keyword, if any
1064 var n_kwpublic: nullable TKwpublic is writable
1065 end
1066 # An explicit private visibility modifier
1067 class APrivateVisibility
1068 super AVisibility
1069 # The `private` keyword
1070 var n_kwprivate: TKwprivate is writable, noinit
1071 end
1072 # An explicit protected visibility modifier
1073 class AProtectedVisibility
1074 super AVisibility
1075 # The `protected` keyword
1076 var n_kwprotected: TKwprotected is writable, noinit
1077 end
1078 # An explicit intrude visibility modifier
1079 class AIntrudeVisibility
1080 super AVisibility
1081 # The `intrude` keyword
1082 var n_kwintrude: TKwintrude is writable, noinit
1083 end
1084
1085 # A class definition
1086 # While most definition are `AStdClassdef`
1087 # There is tow special case of class definition
1088 abstract class AClassdef
1089 super Prod
1090 # All the declared properties (including the main method)
1091 var n_propdefs = new ANodes[APropdef](self)
1092 end
1093
1094 # A standard class definition with a name, superclasses and properties
1095 class AStdClassdef
1096 super AClassdef
1097 super ADefinition
1098
1099 # The class kind (interface, abstract class, etc.)
1100 var n_classkind: AClasskind is writable, noinit
1101
1102 # The name of the class
1103 var n_id: nullable TClassid = null is writable
1104
1105 # The list of formal parameter types
1106 var n_formaldefs = new ANodes[AFormaldef](self)
1107
1108 # The extern block code
1109 var n_extern_code_block: nullable AExternCodeBlock = null is writable
1110
1111 # The `end` keyword
1112 var n_kwend: TKwend is writable, noinit
1113
1114 fun n_superclasses: Array[ASuperPropdef] do
1115 return [for d in n_propdefs do if d isa ASuperPropdef then d]
1116 end
1117
1118 redef fun hot_location do return n_id.location
1119 end
1120
1121 # The implicit class definition of the implicit main method
1122 class ATopClassdef
1123 super AClassdef
1124 end
1125
1126 # The implicit class definition of the top-level methods
1127 class AMainClassdef
1128 super AClassdef
1129 end
1130
1131 # The modifier for the kind of class (abstract, interface, etc.)
1132 abstract class AClasskind
1133 super Prod
1134 end
1135
1136 # A default, or concrete class modifier (just `class`)
1137 class AConcreteClasskind
1138 super AClasskind
1139
1140 # The `class` keyword.
1141 var n_kwclass: TKwclass is writable, noinit
1142 end
1143
1144 # An abstract class modifier (`abstract class`)
1145 class AAbstractClasskind
1146 super AClasskind
1147
1148 # The `abstract` keyword.
1149 var n_kwabstract: TKwabstract is writable, noinit
1150
1151 # The `class` keyword.
1152 var n_kwclass: TKwclass is writable, noinit
1153 end
1154
1155 # An interface class modifier (`interface`)
1156 class AInterfaceClasskind
1157 super AClasskind
1158
1159 # The `interface` keyword.
1160 var n_kwinterface: TKwinterface is writable, noinit
1161 end
1162
1163 # An enum/universal class modifier (`enum class`)
1164 class AEnumClasskind
1165 super AClasskind
1166
1167 # The `enum` keyword.
1168 var n_kwenum: TKwenum is writable, noinit
1169 end
1170
1171 # An extern class modifier (`extern class`)
1172 class AExternClasskind
1173 super AClasskind
1174
1175 # The `extern` keyword.
1176 var n_kwextern: TKwextern is writable, noinit
1177
1178 # The `class` keyword.
1179 var n_kwclass: nullable TKwclass = null is writable
1180 end
1181
1182 # The definition of a formal generic parameter type. eg `X: Y`
1183 class AFormaldef
1184 super Prod
1185
1186 # The name of the parameter type
1187 var n_id: TClassid is writable, noinit
1188
1189 # The bound of the parameter type
1190 var n_type: nullable AType = null is writable
1191 end
1192
1193 # The definition of a property
1194 abstract class APropdef
1195 super ADefinition
1196 end
1197
1198 # A definition of an attribute
1199 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1200 class AAttrPropdef
1201 super APropdef
1202
1203 # The identifier for a old-style attribute (null if new-style)
1204 var n_kwvar: TKwvar is writable, noinit
1205
1206 # The identifier for a new-style attribute (null if old-style)
1207 var n_id2: TId is writable, noinit
1208
1209 # The declared type of the attribute
1210 var n_type: nullable AType = null is writable
1211
1212 # The initial value, if any (set with `=`)
1213 var n_expr: nullable AExpr = null is writable
1214
1215 # The initial value, if any (set with `do return`)
1216 var n_block: nullable AExpr = null is writable
1217
1218 redef fun hot_location
1219 do
1220 return n_id2.location
1221 end
1222 end
1223
1224 # A definition of all kind of method (including constructors)
1225 class AMethPropdef
1226 super APropdef
1227
1228 # The `fun` keyword, if any
1229 var n_kwmeth: nullable TKwmeth = null is writable
1230
1231 # The `init` keyword, if any
1232 var n_kwinit: nullable TKwinit = null is writable
1233
1234 # The `new` keyword, if any
1235 var n_kwnew: nullable TKwnew = null is writable
1236
1237 # The name of the method, if any
1238 var n_methid: nullable AMethid = null is writable
1239
1240 # The signature of the method, if any
1241 var n_signature: nullable ASignature = null is writable
1242
1243 # The body (in Nit) of the method, if any
1244 var n_block: nullable AExpr = null is writable
1245
1246 # The list of declared callbacks (for extern methods)
1247 var n_extern_calls: nullable AExternCalls = null is writable
1248
1249 # The body (in extern code) of the method, if any
1250 var n_extern_code_block: nullable AExternCodeBlock = null is writable
1251
1252 redef fun hot_location
1253 do
1254 if n_methid != null then
1255 return n_methid.location
1256 else if n_kwinit != null then
1257 return n_kwinit.location
1258 else if n_kwnew != null then
1259 return n_kwnew.location
1260 else
1261 return location
1262 end
1263 end
1264 end
1265
1266 # The implicit main method
1267 class AMainMethPropdef
1268 super AMethPropdef
1269 end
1270
1271 class AAnnotPropdef
1272 super APropdef
1273 super AAnnotation
1274 end
1275
1276 # A super-class. eg `super X`
1277 class ASuperPropdef
1278 super APropdef
1279
1280 # The super keyword
1281 var n_kwsuper: TKwsuper is writable, noinit
1282
1283 # The super-class (indicated as a type)
1284 var n_type: AType is writable, noinit
1285 end
1286
1287
1288 # Declaration of callbacks for extern methods
1289 class AExternCalls
1290 super Prod
1291
1292 # The `import` keyword
1293 var n_kwimport: TKwimport is writable, noinit
1294
1295 # The list of declared callbacks
1296 var n_extern_calls: ANodes[AExternCall] = new ANodes[AExternCall](self)
1297 end
1298
1299 # A single callback declaration
1300 abstract class AExternCall
1301 super Prod
1302 end
1303
1304 # A single callback declaration on a method
1305 abstract class APropExternCall
1306 super AExternCall
1307 end
1308
1309 # A single callback declaration on a method on the current receiver
1310 class ALocalPropExternCall
1311 super APropExternCall
1312
1313 # The name of the called-back method
1314 var n_methid: AMethid is writable, noinit
1315 end
1316
1317 # A single callback declaration on a method on an explicit receiver type.
1318 class AFullPropExternCall
1319 super APropExternCall
1320
1321 # The type of the receiver of the called-back method
1322 var n_type: AType is writable, noinit
1323
1324 # The dot `.`
1325 var n_dot: nullable TDot = null is writable
1326
1327 # The name of the called-back method
1328 var n_methid: AMethid is writable, noinit
1329 end
1330
1331 # A single callback declaration on a method on a constructor
1332 class AInitPropExternCall
1333 super APropExternCall
1334
1335 # The allocated type
1336 var n_type: AType is writable, noinit
1337 end
1338
1339 # A single callback declaration on a `super` call
1340 class ASuperExternCall
1341 super AExternCall
1342
1343 # The `super` keyword
1344 var n_kwsuper: TKwsuper is writable, noinit
1345 end
1346
1347 # A single callback declaration on a cast
1348 abstract class ACastExternCall
1349 super AExternCall
1350 end
1351
1352 # A single callback declaration on a cast to a given type
1353 class ACastAsExternCall
1354 super ACastExternCall
1355
1356 # The origin type of the cast
1357 var n_from_type: AType is writable, noinit
1358
1359 # The dot (`.`)
1360 var n_dot: nullable TDot = null is writable
1361
1362 # The `as` keyword
1363 var n_kwas: TKwas is writable, noinit
1364
1365 # The destination of the cast
1366 var n_to_type: AType is writable, noinit
1367 end
1368
1369 # A single callback declaration on a cast to a nullable type
1370 class AAsNullableExternCall
1371 super ACastExternCall
1372
1373 # The origin type to cast as nullable
1374 var n_type: AType is writable, noinit
1375
1376 # The `as` keyword
1377 var n_kwas: TKwas is writable, noinit
1378
1379 # The `nullable` keyword
1380 var n_kwnullable: TKwnullable is writable, noinit
1381 end
1382
1383 # A single callback declaration on a cast to a non-nullable type
1384 class AAsNotNullableExternCall
1385 super ACastExternCall
1386
1387 # The destination type on a cast to not nullable
1388 var n_type: AType is writable, noinit
1389
1390 # The `as` keyword.
1391 var n_kwas: TKwas is writable, noinit
1392
1393 # The `not` keyword
1394 var n_kwnot: TKwnot is writable, noinit
1395
1396 # The `nullable` keyword
1397 var n_kwnullable: TKwnullable is writable, noinit
1398 end
1399
1400 # A definition of a virtual type
1401 class ATypePropdef
1402 super APropdef
1403
1404 # The `type` keyword
1405 var n_kwtype: TKwtype is writable, noinit
1406
1407 # The name of the virtual type
1408 var n_id: TClassid is writable, noinit
1409
1410 # The bound of the virtual type
1411 var n_type: AType is writable, noinit
1412 end
1413
1414 # The identifier of a method in a method declaration.
1415 # There is a specific class because of operator and setters.
1416 abstract class AMethid
1417 super Prod
1418 end
1419
1420 # A method name with a simple identifier
1421 class AIdMethid
1422 super AMethid
1423
1424 # The simple identifier
1425 var n_id: TId is writable, noinit
1426 end
1427
1428 # A method name for an operator
1429 class AOperatorMethid
1430 super AMethid
1431
1432 # The associated operator symbol
1433 var n_op: Token is writable, noinit
1434 end
1435 # A method name `+`
1436 class APlusMethid
1437 super AOperatorMethid
1438 end
1439
1440 # A method name `-`
1441 class AMinusMethid
1442 super AOperatorMethid
1443 end
1444
1445 # A method name `*`
1446 class AStarMethid
1447 super AOperatorMethid
1448 end
1449
1450 # A method name `**`
1451 class AStarstarMethid
1452 super AOperatorMethid
1453 end
1454
1455 # A method name `/`
1456 class ASlashMethid
1457 super AOperatorMethid
1458 end
1459
1460 # A method name `%`
1461 class APercentMethid
1462 super AOperatorMethid
1463
1464 end
1465
1466 # A method name `==`
1467 class AEqMethid
1468 super AOperatorMethid
1469 end
1470
1471 # A method name `!=`
1472 class ANeMethid
1473 super AOperatorMethid
1474 end
1475
1476 # A method name `<=`
1477 class ALeMethid
1478 super AOperatorMethid
1479 end
1480
1481 # A method name `>=`
1482 class AGeMethid
1483 super AOperatorMethid
1484 end
1485
1486 # A method name `<`
1487 class ALtMethid
1488 super AOperatorMethid
1489 end
1490
1491 # A method name `>`
1492 class AGtMethid
1493 super AOperatorMethid
1494 end
1495
1496 # A method name `<<`
1497 class ALlMethid
1498 super AOperatorMethid
1499 end
1500
1501 # A method name `>>`
1502 class AGgMethid
1503 super AOperatorMethid
1504 end
1505
1506 # A method name `<=>`
1507 class AStarshipMethid
1508 super AOperatorMethid
1509 end
1510
1511 # A method name `[]`
1512 class ABraMethid
1513 super AMethid
1514
1515 # The `[` symbol
1516 var n_obra: TObra is writable, noinit
1517
1518 # The `]` symbol
1519 var n_cbra: TCbra is writable, noinit
1520 end
1521
1522 # A setter method name with a simple identifier (with a `=`)
1523 class AAssignMethid
1524 super AMethid
1525
1526 # The base identifier
1527 var n_id: TId is writable, noinit
1528
1529 # The `=` symbol
1530 var n_assign: TAssign is writable, noinit
1531 end
1532
1533 # A method name `[]=`
1534 class ABraassignMethid
1535 super AMethid
1536
1537 # The `[` symbol
1538 var n_obra: TObra is writable, noinit
1539
1540 # The `]` symbol
1541 var n_cbra: TCbra is writable, noinit
1542
1543 # The `=` symbol
1544 var n_assign: TAssign is writable, noinit
1545 end
1546
1547 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1548 class ASignature
1549 super Prod
1550
1551 # The `(` symbol
1552 var n_opar: nullable TOpar = null is writable
1553
1554 # The list of parameters
1555 var n_params = new ANodes[AParam](self)
1556
1557 # The `)` symbol
1558 var n_cpar: nullable TCpar = null is writable
1559
1560 # The return type
1561 var n_type: nullable AType = null is writable
1562 end
1563
1564 # A parameter definition in a signature. eg `x:X`
1565 class AParam
1566 super Prod
1567
1568 # The name of the parameter
1569 var n_id: TId is writable, noinit
1570
1571 # The type of the parameter, if any
1572 var n_type: nullable AType = null is writable
1573
1574 # The `...` symbol to indicate varargs
1575 var n_dotdotdot: nullable TDotdotdot = null is writable
1576 end
1577
1578 # A static type. eg `nullable X[Y]`
1579 class AType
1580 super Prod
1581 # The `nullable` keyword
1582 var n_kwnullable: nullable TKwnullable = null is writable
1583
1584 # The name of the class or of the formal type
1585 var n_id: TClassid is writable, noinit
1586
1587 # Type arguments for a generic type
1588 var n_types = new ANodes[AType](self)
1589 end
1590
1591 # A label at the end of a block or in a break/continue statement. eg `label x`
1592 class ALabel
1593 super Prod
1594
1595 # The `label` keyword
1596 var n_kwlabel: TKwlabel is writable, noinit
1597
1598 # The name of the label, if any
1599 var n_id: nullable TId is writable, noinit
1600 end
1601
1602 # Expression and statements
1603 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1604 abstract class AExpr
1605 super Prod
1606 end
1607
1608 # A sequence of `AExpr` (usually statements)
1609 # The last `AExpr` gives the value of the whole block
1610 class ABlockExpr
1611 super AExpr
1612
1613 # The list of statements in the bloc.
1614 # The last element is often considered as an expression that give the value of the whole block.
1615 var n_expr = new ANodes[AExpr](self)
1616
1617 # The `end` keyword
1618 var n_kwend: nullable TKwend = null is writable
1619 end
1620
1621 # A declaration of a local variable. eg `var x: X = y`
1622 class AVardeclExpr
1623 super AExpr
1624
1625 # The `var` keyword
1626 var n_kwvar: nullable TKwvar = null is writable
1627
1628 # The name of the local variable
1629 var n_id: TId is writable, noinit
1630
1631 # The declaration type of the local variable
1632 var n_type: nullable AType = null is writable
1633
1634 # The `=` symbol (for the initial value)
1635 var n_assign: nullable TAssign = null is writable
1636
1637 # The initial value, if any
1638 var n_expr: nullable AExpr = null is writable
1639 end
1640
1641 # A `return` statement. eg `return x`
1642 class AReturnExpr
1643 super AExpr
1644
1645 # The `return` keyword
1646 var n_kwreturn: nullable TKwreturn = null is writable
1647
1648 # The return value, if any
1649 var n_expr: nullable AExpr = null is writable
1650 end
1651
1652 # Something that has a label.
1653 abstract class ALabelable
1654 super Prod
1655
1656 # The associated label declatation
1657 var n_label: nullable ALabel = null is writable
1658 end
1659
1660 # A `break` or a `continue`
1661 abstract class AEscapeExpr
1662 super AExpr
1663 super ALabelable
1664
1665 # The return value, if nay (unused currently)
1666 var n_expr: nullable AExpr = null is writable
1667 end
1668
1669 # A `break` statement.
1670 class ABreakExpr
1671 super AEscapeExpr
1672
1673 # The `break` keyword
1674 var n_kwbreak: TKwbreak is writable, noinit
1675 end
1676
1677 # An `abort` statement
1678 class AAbortExpr
1679 super AExpr
1680
1681 # The `abort` keyword
1682 var n_kwabort: TKwabort is writable, noinit
1683 end
1684
1685 # A `continue` statement
1686 class AContinueExpr
1687 super AEscapeExpr
1688
1689 # The `continue` keyword.
1690 var n_kwcontinue: nullable TKwcontinue = null is writable
1691 end
1692
1693 # A `do` statement
1694 class ADoExpr
1695 super AExpr
1696 super ALabelable
1697
1698 # The `do` keyword
1699 var n_kwdo: TKwdo is writable, noinit
1700
1701 # The list of statements of the `do`.
1702 var n_block: nullable AExpr = null is writable
1703 end
1704
1705 # A `if` statement
1706 class AIfExpr
1707 super AExpr
1708
1709 # The `if` keyword
1710 var n_kwif: TKwif is writable, noinit
1711
1712 # The expression used as the condition of the `if`
1713 var n_expr: AExpr is writable, noinit
1714
1715 # The body of the `then` part
1716 var n_then: nullable AExpr = null is writable
1717
1718 # The body of the `else` part
1719 var n_else: nullable AExpr = null is writable
1720 end
1721
1722 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1723 class AIfexprExpr
1724 super AExpr
1725
1726 # The `if` keyword
1727 var n_kwif: TKwif is writable, noinit
1728
1729 # The expression used as the condition of the `if`
1730 var n_expr: AExpr is writable, noinit
1731
1732 # The `then` keyword
1733 var n_kwthen: TKwthen is writable, noinit
1734
1735 # The expression in the `then` part
1736 var n_then: AExpr is writable, noinit
1737
1738 # The `else` keyword
1739 var n_kwelse: TKwelse is writable, noinit
1740
1741 # The expression in the `else` part
1742 var n_else: AExpr is writable, noinit
1743 end
1744
1745 # A `while` statement
1746 class AWhileExpr
1747 super AExpr
1748 super ALabelable
1749
1750 # The `while` keyword
1751 var n_kwwhile: TKwwhile is writable, noinit
1752
1753 # The expression used as the condition of the `while`
1754 var n_expr: AExpr is writable, noinit
1755
1756 # The `do` keyword
1757 var n_kwdo: TKwdo is writable, noinit
1758
1759 # The body of the loop
1760 var n_block: nullable AExpr = null is writable
1761 end
1762
1763 # A `loop` statement
1764 class ALoopExpr
1765 super AExpr
1766 super ALabelable
1767
1768 # The `loop` keyword
1769 var n_kwloop: TKwloop is writable, noinit
1770
1771 # The body of the loop
1772 var n_block: nullable AExpr = null is writable
1773 end
1774
1775 # A `for` statement
1776 class AForExpr
1777 super AExpr
1778 super ALabelable
1779
1780 # The `for` keyword
1781 var n_kwfor: TKwfor is writable, noinit
1782
1783 # The list of name of the automatic variables
1784 var n_ids = new ANodes[TId](self)
1785
1786 # The expression used as the collection to iterate on
1787 var n_expr: AExpr is writable, noinit
1788
1789 # The `do` keyword
1790 var n_kwdo: TKwdo is writable, noinit
1791
1792 # The body of the loop
1793 var n_block: nullable AExpr = null is writable
1794 end
1795
1796 # A `with` statement
1797 class AWithExpr
1798 super AExpr
1799 super ALabelable
1800
1801 # The `with` keyword
1802 var n_kwwith: TKwwith is writable, noinit
1803
1804 # The expression used to get the value to control
1805 var n_expr: AExpr is writable, noinit
1806
1807 # The `do` keyword
1808 var n_kwdo: TKwdo is writable, noinit
1809
1810 # The body of the loop
1811 var n_block: nullable AExpr = null is writable
1812 end
1813
1814 # An `assert` statement
1815 class AAssertExpr
1816 super AExpr
1817
1818 # The `assert` keyword
1819 var n_kwassert: TKwassert is writable, noinit
1820
1821 # The name of the assert, if any
1822 var n_id: nullable TId = null is writable
1823
1824 # The expression used as the condition of the `assert`
1825 var n_expr: AExpr is writable, noinit
1826
1827 # The body to execute when the assert fails
1828 var n_else: nullable AExpr = null is writable
1829 end
1830
1831 # Whatever is a simple assignment. eg `= something`
1832 abstract class AAssignFormExpr
1833 super AExpr
1834
1835 # The `=` symbol
1836 var n_assign: TAssign is writable, noinit
1837
1838 # The right-value to assign.
1839 var n_value: AExpr is writable, noinit
1840 end
1841
1842 # Whatever is a combined assignment. eg `+= something`
1843 abstract class AReassignFormExpr
1844 super AExpr
1845
1846 # The combined operator (eg. `+=`)
1847 var n_assign_op: AAssignOp is writable, noinit
1848
1849 # The right-value to apply on the combined operator.
1850 var n_value: AExpr is writable, noinit
1851 end
1852
1853 # A `once` expression. eg `once x`
1854 class AOnceExpr
1855 super AExpr
1856
1857 # The `once` keyword
1858 var n_kwonce: TKwonce is writable, noinit
1859
1860 # The expression to evaluate only one time
1861 var n_expr: AExpr is writable, noinit
1862 end
1863
1864 # A polymorphic invocation of a method
1865 # The form of the invocation (name, arguments, etc.) are specific
1866 abstract class ASendExpr
1867 super AExpr
1868 # The receiver of the method invocation
1869 var n_expr: AExpr is writable, noinit
1870 end
1871
1872 # A binary operation on a method
1873 abstract class ABinopExpr
1874 super ASendExpr
1875
1876 # The operator
1877 var n_op: Token is writable, noinit
1878
1879 # The second operand of the operation
1880 # Note: the receiver (`n_expr`) is the first operand
1881 var n_expr2: AExpr is writable, noinit
1882
1883 # The name of the operator (eg '+')
1884 fun operator: String is abstract
1885 end
1886
1887 # Something that is boolean expression
1888 abstract class ABoolExpr
1889 super AExpr
1890 end
1891
1892 # Something that is binary boolean expression
1893 abstract class ABinBoolExpr
1894 super ABoolExpr
1895
1896 # The first boolean operand
1897 var n_expr: AExpr is writable, noinit
1898
1899 # The operator
1900 var n_op: Token is writable, noinit
1901
1902 # The second boolean operand
1903 var n_expr2: AExpr is writable, noinit
1904 end
1905
1906 # A `or` expression
1907 class AOrExpr
1908 super ABinBoolExpr
1909 end
1910
1911 # A `and` expression
1912 class AAndExpr
1913 super ABinBoolExpr
1914 end
1915
1916 # A `or else` expression
1917 class AOrElseExpr
1918 super ABinBoolExpr
1919
1920 # The `else` keyword
1921 var n_kwelse: TKwelse is writable, noinit
1922 end
1923
1924 # A `implies` expression
1925 class AImpliesExpr
1926 super ABinBoolExpr
1927 end
1928
1929 # A `not` expression
1930 class ANotExpr
1931 super ABoolExpr
1932
1933 # The `not` keyword
1934 var n_kwnot: TKwnot is writable, noinit
1935
1936 # The boolean operand of the `not`
1937 var n_expr: AExpr is writable, noinit
1938 end
1939
1940 # A `==` expression
1941 class AEqExpr
1942 super ABinopExpr
1943 redef fun operator do return "=="
1944 end
1945
1946 # A `!=` expression
1947 class ANeExpr
1948 super ABinopExpr
1949 redef fun operator do return "!="
1950 end
1951
1952 # A `<` expression
1953 class ALtExpr
1954 super ABinopExpr
1955 redef fun operator do return "<"
1956 end
1957
1958 # A `<=` expression
1959 class ALeExpr
1960 super ABinopExpr
1961 redef fun operator do return "<="
1962 end
1963
1964 # A `<<` expression
1965 class ALlExpr
1966 super ABinopExpr
1967 redef fun operator do return "<<"
1968 end
1969
1970 # A `>` expression
1971 class AGtExpr
1972 super ABinopExpr
1973 redef fun operator do return ">"
1974 end
1975
1976 # A `>=` expression
1977 class AGeExpr
1978 super ABinopExpr
1979 redef fun operator do return ">="
1980 end
1981
1982 # A `>>` expression
1983 class AGgExpr
1984 super ABinopExpr
1985 redef fun operator do return ">>"
1986 end
1987
1988 # A type-ckeck expression. eg `x isa T`
1989 class AIsaExpr
1990 super ABoolExpr
1991
1992 # The expression to check
1993 var n_expr: AExpr is writable, noinit
1994
1995 # The `isa` keyword
1996 var n_kwisa: TKwisa is writable, noinit
1997
1998 # The destination type to check to
1999 var n_type: AType is writable, noinit
2000 end
2001
2002 # A `+` expression
2003 class APlusExpr
2004 super ABinopExpr
2005 redef fun operator do return "+"
2006 end
2007
2008 # A `-` expression
2009 class AMinusExpr
2010 super ABinopExpr
2011 redef fun operator do return "-"
2012 end
2013
2014 # A `<=>` expression
2015 class AStarshipExpr
2016 super ABinopExpr
2017 redef fun operator do return "<=>"
2018 end
2019
2020 # A `*` expression
2021 class AStarExpr
2022 super ABinopExpr
2023 redef fun operator do return "*"
2024 end
2025
2026 # A `**` expression
2027 class AStarstarExpr
2028 super ABinopExpr
2029 redef fun operator do return "**"
2030 end
2031
2032 # A `/` expression
2033 class ASlashExpr
2034 super ABinopExpr
2035 redef fun operator do return "/"
2036 end
2037
2038 # A `%` expression
2039 class APercentExpr
2040 super ABinopExpr
2041 redef fun operator do return "%"
2042 end
2043
2044 # A unary minus expression. eg `-x`
2045 class AUminusExpr
2046 super ASendExpr
2047
2048 # The `-` symbol
2049 var n_minus: TMinus is writable, noinit
2050 end
2051
2052 # A unary plus expression. eg `+x`
2053 class AUplusExpr
2054 super ASendExpr
2055
2056 # The `+` symbol
2057 var n_plus: TPlus is writable, noinit
2058 end
2059
2060 # An explicit instantiation. eg `new T`
2061 class ANewExpr
2062 super AExpr
2063
2064 # The `new` keyword
2065 var n_kwnew: TKwnew is writable, noinit
2066
2067 # The `type` keyword
2068 var n_type: AType is writable, noinit
2069
2070 # The name of the named-constructor, if any
2071 var n_id: nullable TId = null is writable
2072
2073 # The arguments of the `new`
2074 var n_args: AExprs is writable, noinit
2075 end
2076
2077 # Whatever is a old-style attribute access
2078 abstract class AAttrFormExpr
2079 super AExpr
2080
2081 # The receiver of the attribute
2082 var n_expr: AExpr is writable, noinit
2083
2084 # The name of the attribute
2085 var n_id: TAttrid is writable, noinit
2086
2087 end
2088
2089 # The read of an attribute. eg `x._a`
2090 class AAttrExpr
2091 super AAttrFormExpr
2092 end
2093
2094 # The assignment of an attribute. eg `x._a=y`
2095 class AAttrAssignExpr
2096 super AAttrFormExpr
2097 super AAssignFormExpr
2098 end
2099
2100 # Whatever looks-like a call with a standard method and any number of arguments.
2101 abstract class ACallFormExpr
2102 super ASendExpr
2103
2104 # The name of the method
2105 var n_id: TId is writable, noinit
2106
2107 # The arguments of the call
2108 var n_args: AExprs is writable, noinit
2109 end
2110
2111 # A complex setter call (standard or brackets)
2112 abstract class ASendReassignFormExpr
2113 super ASendExpr
2114 super AReassignFormExpr
2115 end
2116
2117 # A complex attribute assignment. eg `x._a+=y`
2118 class AAttrReassignExpr
2119 super AAttrFormExpr
2120 super AReassignFormExpr
2121 end
2122
2123 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2124 # Note: because the parser cannot distinguish a variable read with a method call with an implicit receiver and no arguments, it always returns a `ACallExpr`.
2125 # Semantic analysis have to transform them to instance of `AVarExpr`.
2126 class ACallExpr
2127 super ACallFormExpr
2128 end
2129
2130 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2131 # Note: because the parser cannot distinguish a variable write with a setter call with an implicit receiver and no arguments, it always returns a `ACallAssignExpr`.
2132 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2133 class ACallAssignExpr
2134 super ACallFormExpr
2135 super AAssignFormExpr
2136 end
2137
2138 # A complex setter call with a standard method-name and any number of arguments. eg `x.m(y)+=z`. OR just a simple complex assignment.
2139 # Note: because the parser cannot distinguish a variable write with a complex setter call with an implicit receiver and no arguments, it always returns a `ACallReassignExpr`.
2140 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2141 class ACallReassignExpr
2142 super ACallFormExpr
2143 super ASendReassignFormExpr
2144 end
2145
2146 # A call to `super`. OR a call of a super-constructor
2147 class ASuperExpr
2148 super AExpr
2149
2150 # The qualifier part before the super (currenlty unused)
2151 var n_qualified: nullable AQualified = null is writable
2152
2153 # The `super` keyword
2154 var n_kwsuper: TKwsuper is writable, noinit
2155
2156 # The arguments of the super
2157 var n_args: AExprs is writable, noinit
2158 end
2159
2160 # A call to the `init` constructor.
2161 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2162 class AInitExpr
2163 super ASendExpr
2164
2165 # The `init` keyword
2166 var n_kwinit: TKwinit is writable, noinit
2167
2168 # The arguments of the init
2169 var n_args: AExprs is writable, noinit
2170 end
2171
2172 # Whatever looks-like a call of the brackets `[]` operator.
2173 abstract class ABraFormExpr
2174 super ASendExpr
2175
2176 # The arguments inside the brackets
2177 var n_args: AExprs is writable, noinit
2178 end
2179
2180 # A call of the brackets operator. eg `x[y,z]`
2181 class ABraExpr
2182 super ABraFormExpr
2183 end
2184
2185 # A setter call of the bracket operator. eg `x[y,z]=t`
2186 class ABraAssignExpr
2187 super ABraFormExpr
2188 super AAssignFormExpr
2189 end
2190
2191 # Whatever is an access to a local variable
2192 abstract class AVarFormExpr
2193 super AExpr
2194
2195 # The name of the attribute
2196 var n_id: TId is writable, noinit
2197 end
2198
2199 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2200 class ABraReassignExpr
2201 super ABraFormExpr
2202 super ASendReassignFormExpr
2203 end
2204
2205 # A local variable read access.
2206 # The parser cannot instantiate them, see `ACallExpr`.
2207 class AVarExpr
2208 super AVarFormExpr
2209 end
2210
2211 # A local variable simple assignment access
2212 # The parser cannot instantiate them, see `ACallAssignExpr`.
2213 class AVarAssignExpr
2214 super AVarFormExpr
2215 super AAssignFormExpr
2216 end
2217
2218 # A local variable complex assignment access
2219 # The parser cannot instantiate them, see `ACallReassignExpr`.
2220 class AVarReassignExpr
2221 super AVarFormExpr
2222 super AReassignFormExpr
2223 end
2224
2225 # A literal range, open or closed
2226 abstract class ARangeExpr
2227 super AExpr
2228
2229 # The left (lower) element of the range
2230 var n_expr: AExpr is writable, noinit
2231
2232 # The right (uppr) element of the range
2233 var n_expr2: AExpr is writable, noinit
2234 end
2235
2236 # A closed literal range. eg `[x..y]`
2237 class ACrangeExpr
2238 super ARangeExpr
2239
2240 # The opening bracket `[`
2241 var n_obra: TObra is writable, noinit
2242
2243 # The closing bracket `]`
2244 var n_cbra: TCbra is writable, noinit
2245 end
2246
2247 # An open literal range. eg `[x..y[`
2248 class AOrangeExpr
2249 super ARangeExpr
2250
2251 # The opening bracket `[`
2252 var n_obra: TObra is writable, noinit
2253
2254 # The closing bracket `[` (because open range)
2255 var n_cbra: TObra is writable, noinit
2256 end
2257
2258 # A literal array. eg. `[x,y,z]`
2259 class AArrayExpr
2260 super AExpr
2261
2262 # The opening bracket `[`
2263 var n_obra: TObra is writable, noinit
2264
2265 # The elements of the array
2266 var n_exprs = new ANodes[AExpr](self)
2267
2268 # The type of the element of the array (if any)
2269 var n_type: nullable AType = null is writable
2270
2271 # The closing bracket `]`
2272 var n_cbra: TCbra is writable, noinit
2273 end
2274
2275 # A read of `self`
2276 class ASelfExpr
2277 super AExpr
2278
2279 # The `self` keyword
2280 var n_kwself: nullable TKwself = null is writable
2281 end
2282
2283 # When there is no explicit receiver, `self` is implicit
2284 class AImplicitSelfExpr
2285 super ASelfExpr
2286 end
2287
2288 # A `true` boolean literal constant
2289 class ATrueExpr
2290 super ABoolExpr
2291
2292 # The `true` keyword
2293 var n_kwtrue: TKwtrue is writable, noinit
2294 end
2295
2296 # A `false` boolean literal constant
2297 class AFalseExpr
2298 super ABoolExpr
2299
2300 # The `false` keyword
2301 var n_kwfalse: TKwfalse is writable, noinit
2302 end
2303
2304 # A `null` literal constant
2305 class ANullExpr
2306 super AExpr
2307
2308 # The `null` keyword
2309 var n_kwnull: TKwnull is writable, noinit
2310 end
2311
2312 # An integer literal
2313 class AIntExpr
2314 super AExpr
2315 end
2316
2317 # An integer literal in decimal format
2318 class ADecIntExpr
2319 super AIntExpr
2320
2321 # The decimal token
2322 var n_number: TNumber is writable, noinit
2323 end
2324
2325 # An integer literal in hexadecimal format
2326 class AHexIntExpr
2327 super AIntExpr
2328
2329 # The hexadecimal token
2330 var n_hex_number: THexNumber is writable, noinit
2331 end
2332
2333 # A float literal
2334 class AFloatExpr
2335 super AExpr
2336
2337 # The float token
2338 var n_float: TFloat is writable, noinit
2339 end
2340
2341 # A character literal
2342 class ACharExpr
2343 super AExpr
2344
2345 # The character token
2346 var n_char: TChar is writable, noinit
2347 end
2348
2349 # A string literal
2350 abstract class AStringFormExpr
2351 super AExpr
2352
2353 # The string token
2354 var n_string: Token is writable, noinit
2355 end
2356
2357 # A simple string. eg. `"abc"`
2358 class AStringExpr
2359 super AStringFormExpr
2360 end
2361
2362 # The start of a superstring. eg `"abc{`
2363 class AStartStringExpr
2364 super AStringFormExpr
2365 end
2366
2367 # The middle of a superstring. eg `}abc{`
2368 class AMidStringExpr
2369 super AStringFormExpr
2370 end
2371
2372 # The end of a superstrng. eg `}abc"`
2373 class AEndStringExpr
2374 super AStringFormExpr
2375 end
2376
2377 # A superstring literal. eg `"a{x}b{y}c"`
2378 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2379 class ASuperstringExpr
2380 super AExpr
2381
2382 # The list of the expressions of the superstring
2383 var n_exprs = new ANodes[AExpr](self)
2384 end
2385
2386 # A simple parenthesis. eg `(x)`
2387 class AParExpr
2388 super AExpr
2389
2390 # The opening parenthesis
2391 var n_opar: TOpar is writable, noinit
2392
2393 # The inner expression
2394 var n_expr: AExpr is writable, noinit
2395
2396 # The closing parenthesis
2397 var n_cpar: TCpar is writable, noinit
2398 end
2399
2400 # A cast, against a type or `not null`
2401 class AAsCastForm
2402 super AExpr
2403
2404 # The expression to cast
2405 var n_expr: AExpr is writable, noinit
2406
2407 # The `as` keyword
2408 var n_kwas: TKwas is writable, noinit
2409
2410 # The opening parenthesis
2411 var n_opar: nullable TOpar = null is writable
2412
2413 # The closing parenthesis
2414 var n_cpar: nullable TCpar = null is writable
2415 end
2416
2417 # A type cast. eg `x.as(T)`
2418 class AAsCastExpr
2419 super AAsCastForm
2420
2421 # The target type to cast to
2422 var n_type: AType is writable, noinit
2423 end
2424
2425 # A as-not-null cast. eg `x.as(not null)`
2426 class AAsNotnullExpr
2427 super AAsCastForm
2428
2429 # The `not` keyword
2430 var n_kwnot: TKwnot is writable, noinit
2431
2432 # The `null` keyword
2433 var n_kwnull: TKwnull is writable, noinit
2434 end
2435
2436 # A is-set check of old-style attributes. eg `isset x._a`
2437 class AIssetAttrExpr
2438 super AAttrFormExpr
2439
2440 # The `isset` keyword
2441 var n_kwisset: TKwisset is writable, noinit
2442 end
2443
2444 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2445 class AVarargExpr
2446 super AExpr
2447
2448 # The passed expression
2449 var n_expr: AExpr is writable, noinit
2450
2451 # The `...` symbol
2452 var n_dotdotdot: TDotdotdot is writable, noinit
2453 end
2454
2455 # A list of expression separated with commas (arguments for instance)
2456 class AManyExpr
2457 super AExpr
2458
2459 # The list of expressions
2460 var n_exprs = new ANodes[AExpr](self)
2461 end
2462
2463 # A special expression that encapsulates a static type
2464 # Can only be found in special construction like arguments of annotations.
2465 class ATypeExpr
2466 super AExpr
2467
2468 # The encapsulated type
2469 var n_type: AType is writable, noinit
2470 end
2471
2472 # A special expression that encapsulates a method identifier
2473 # Can only be found in special construction like arguments of annotations.
2474 class AMethidExpr
2475 super AExpr
2476
2477 # The receiver
2478 var n_expr: AExpr is writable, noinit
2479
2480 # The encapsulated method identifier
2481 var n_id: AMethid is writable, noinit
2482 end
2483
2484 # A special expression that encapsulate an annotation
2485 # Can only be found in special construction like arguments of annotations.
2486 #
2487 # The encapsulated annotations are in `n_annotations`
2488 class AAtExpr
2489 super AExpr
2490 end
2491
2492 # A special expression to debug types
2493 class ADebugTypeExpr
2494 super AExpr
2495
2496 # The `debug` keyword
2497 var n_kwdebug: TKwdebug is writable, noinit
2498
2499 # The `type` keyword
2500 var n_kwtype: TKwtype is writable, noinit
2501
2502 # The expression to check
2503 var n_expr: AExpr is writable, noinit
2504
2505 # The type to check
2506 var n_type: AType is writable, noinit
2507 end
2508
2509 # A list of expression separated with commas (arguments for instance)
2510 abstract class AExprs
2511 super Prod
2512
2513 # The list of expressions
2514 var n_exprs = new ANodes[AExpr](self)
2515 end
2516
2517 # A simple list of expressions
2518 class AListExprs
2519 super AExprs
2520 end
2521
2522 # A list of expressions enclosed in parentheses
2523 class AParExprs
2524 super AExprs
2525
2526 # The opening parenthesis
2527 var n_opar: TOpar is writable, noinit
2528
2529 # The closing parenthesis
2530 var n_cpar: TCpar is writable, noinit
2531 end
2532
2533 # A list of expressions enclosed in brackets
2534 class ABraExprs
2535 super AExprs
2536
2537 # The opening bracket
2538 var n_obra: TObra is writable, noinit
2539
2540 # The closing bracket
2541 var n_cbra: TCbra is writable, noinit
2542 end
2543
2544 # A complex assignment operator. (`+=` and `-=`)
2545 abstract class AAssignOp
2546 super Prod
2547
2548 # The combined assignment operator
2549 var n_op: Token is writable, noinit
2550
2551 # The name of the operator without the `=` (eg '+')
2552 fun operator: String is abstract
2553 end
2554
2555 # A `+=` assignment operation
2556 class APlusAssignOp
2557 super AAssignOp
2558
2559 redef fun operator do return "+"
2560 end
2561
2562 # A `-=` assignment operation
2563 class AMinusAssignOp
2564 super AAssignOp
2565
2566 redef fun operator do return "-"
2567 end
2568
2569 # A `*=` assignment operation
2570 class AStarAssignOp
2571 super AAssignOp
2572
2573 redef fun operator do return "*"
2574 end
2575
2576 # A `/=` assignment operation
2577 class ASlashAssignOp
2578 super AAssignOp
2579
2580 redef fun operator do return "/"
2581 end
2582
2583 # A `%=` assignment operation
2584 class APercentAssignOp
2585 super AAssignOp
2586
2587 redef fun operator do return "%"
2588 end
2589
2590 # A `**=` assignment operation
2591 class AStarstarAssignOp
2592 super AAssignOp
2593
2594 redef fun operator do return "**"
2595 end
2596
2597 # A `<<=` assignment operation
2598 class ALlAssignOp
2599 super AAssignOp
2600
2601 redef fun operator do return "<<"
2602 end
2603
2604 # A `>>=` assignment operation
2605 class AGgAssignOp
2606 super AAssignOp
2607
2608 redef fun operator do return ">>"
2609 end
2610
2611 # A possibly fully-qualified module identifier
2612 class AModuleName
2613 super Prod
2614
2615 # The starting quad (`::`)
2616 var n_quad: nullable TQuad = null is writable
2617
2618 # The list of quad-separated project/group identifiers
2619 var n_path = new ANodes[TId](self)
2620
2621 # The final module identifier
2622 var n_id: TId is writable, noinit
2623 end
2624
2625 # A language declaration for an extern block
2626 class AInLanguage
2627 super Prod
2628
2629 # The `in` keyword
2630 var n_kwin: TKwin is writable, noinit
2631
2632 # The language name
2633 var n_string: TString is writable, noinit
2634 end
2635
2636 # An full extern block
2637 class AExternCodeBlock
2638 super Prod
2639
2640 # The language declration
2641 var n_in_language: nullable AInLanguage = null is writable
2642
2643 # The block of extern code
2644 var n_extern_code_segment: TExternCodeSegment is writable, noinit
2645 end
2646
2647 # A possible full method qualifier.
2648 class AQualified
2649 super Prod
2650
2651 # The starting quad (`::`)
2652 var n_quad: nullable TQuad = null is writable
2653
2654 # The list of quad-separated project/group/module identifiers
2655 var n_id = new ANodes[TId](self)
2656
2657 # A class identifier
2658 var n_classid: nullable TClassid = null is writable
2659 end
2660
2661 # A documentation of a definition
2662 # It contains the block of comments just above the declaration
2663 class ADoc
2664 super Prod
2665
2666 # A list of lines of comment
2667 var n_comment = new ANodes[TComment](self)
2668 end
2669
2670 # A group of annotation on a node
2671 #
2672 # This same class is used for the 3 kind of annotations:
2673 #
2674 # * *is* annotations. eg `module foo is bar`.
2675 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2676 # * *class* annotations, defined in classes.
2677 class AAnnotations
2678 super Prod
2679
2680 # The `@` symbol, for *at* annotations
2681 var n_at: nullable TAt = null is writable
2682
2683 # The opening parenthesis in *at* annotations
2684 var n_opar: nullable TOpar = null is writable
2685
2686 # The list of annotations
2687 var n_items = new ANodes[AAnnotation](self)
2688
2689 # The closing parenthesis in *at* annotations
2690 var n_cpar: nullable TCpar = null is writable
2691 end
2692
2693 # A single annotation
2694 class AAnnotation
2695 super ADefinition
2696
2697 # The name of the annotation
2698 var n_atid: AAtid is writable, noinit
2699
2700 # The opening parenthesis of the arguments
2701 var n_opar: nullable TOpar = null is writable
2702
2703 # The list of arguments
2704 var n_args = new ANodes[AExpr](self)
2705
2706 # The closing parenthesis
2707 var n_cpar: nullable TCpar = null is writable
2708
2709 # The name of the annotation
2710 fun name: String
2711 do
2712 return n_atid.n_id.text
2713 end
2714 end
2715
2716 # An annotation name
2717 abstract class AAtid
2718 super Prod
2719
2720 # The identifier of the annotation.
2721 # Can be a TId of a keyword
2722 var n_id: Token is writable, noinit
2723 end
2724
2725 # An annotation name based on an identifier
2726 class AIdAtid
2727 super AAtid
2728 end
2729
2730 # An annotation name based on the keyword `extern`
2731 class AKwexternAtid
2732 super AAtid
2733 end
2734
2735 # An annotation name based on the keyword `import`
2736 class AKwimportAtid
2737 super AAtid
2738 end
2739
2740 # An annotation name based on the keyword `abstract`
2741 class AKwabstractAtid
2742 super AAtid
2743 end
2744
2745 # The root of the AST
2746 class Start
2747 super Prod
2748
2749 # The main module
2750 var n_base: nullable AModule is writable
2751
2752 # The end of file (or error) token
2753 var n_eof: EOF is writable
2754 end