d335917be4cf0b9d4b529a54e6370a26ee2d6bee
[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
21 # Root of the AST class-hierarchy
22 abstract class ANode
23 # Location is set during AST building. Once built, location cannon be null.
24 # However, manual instantiated nodes may need more care.
25 var location: Location is writable, noinit
26
27 # The location of the important part of the node (identifier or whatever)
28 fun hot_location: Location do return location
29
30 # Display a message for the colored location of the node
31 fun debug(message: String)
32 do
33 sys.stderr.write "{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
34 end
35
36 # Parent of the node in the AST
37 var parent: nullable ANode = null
38
39 # The topmost ancestor of the element
40 # This just apply `parent` until the first one
41 fun root: ANode
42 do
43 var res = self
44 loop
45 var p = res.parent
46 if p == null then return res
47 res = p
48 end
49 end
50
51 # The most specific common parent between `self` and `other`
52 # Return null if the two node are unrelated (distinct root)
53 fun common_parent(other: ANode): nullable ANode
54 do
55 # First, get the same depth
56 var s: nullable ANode = self
57 var o: nullable ANode = other
58 var d = s.depth - o.depth
59 while d > 0 do
60 s = s.parent
61 d -= 1
62 end
63 while d < 0 do
64 o = o.parent
65 d += 1
66 end
67 assert o.depth == s.depth
68 # Second, go up until same in found
69 while s != o do
70 s = s.parent
71 o = o.parent
72 end
73 return s
74 end
75
76 # Number of nodes between `self` and the `root` of the AST
77 # ENSURE `self == self.root implies result == 0 `
78 # ENSURE `self != self.root implies result == self.parent.depth + 1`
79 fun depth: Int
80 do
81 var n = self
82 var res = 0
83 loop
84 var p = n.parent
85 if p == null then return res
86 n = p
87 res += 1
88 end
89 end
90
91 # Replace a child with an other node in the AST
92 private fun replace_child(old_child: ANode, new_child: nullable ANode) is abstract
93
94 # Detach a node from its parent
95 # Aborts if the node is not detashable. use `replace_with` instead
96 # REQUIRE: parent != null
97 # REQUIRE: is_detachable
98 # ENDURE: parent == null
99 fun detach
100 do
101 assert parent != null
102 parent.replace_child(self, null)
103 parent = null
104 end
105
106 # Replace itself with an other node in the AST
107 # REQUIRE: parent != null
108 # ENSURE: node.parent == old(parent)
109 # ENSURE: parent == null
110 fun replace_with(node: ANode)
111 do
112 assert parent != null
113 parent.replace_child(self, node)
114 parent = null
115 end
116
117 # Visit all nodes in order.
118 # Thus, call `v.enter_visit(e)` for each child `e`
119 fun visit_all(v: Visitor) is abstract
120 end
121
122 # A sequence of nodes
123 # There is a specifc class (instead of a using Array) to track the parent/child relation when nodes are added or removed
124 class ANodes[E: ANode]
125 super Sequence[E]
126 private var parent: ANode
127 private var items = new Array[E]
128 redef fun iterator do return items.iterator
129 redef fun length do return items.length
130 redef fun is_empty do return items.is_empty
131 redef fun push(e)
132 do
133 hook_add(e)
134 items.push(e)
135 end
136 redef fun pop
137 do
138 var res = items.pop
139 hook_remove(res)
140 return res
141 end
142 redef fun unshift(e)
143 do
144 hook_add(e)
145 items.unshift(e)
146 end
147 redef fun shift
148 do
149 var res = items.shift
150 hook_remove(res)
151 return res
152 end
153 redef fun has(e)
154 do
155 return items.has(e)
156 end
157 redef fun [](index)
158 do
159 return items[index]
160 end
161 redef fun []=(index, e)
162 do
163 hook_remove(self[index])
164 hook_add(e)
165 items[index]=e
166 end
167 redef fun remove_at(index)
168 do
169 hook_remove(items[index])
170 items.remove_at(index)
171 end
172 private fun hook_add(e: E)
173 do
174 #assert e.parent == null
175 e.parent = parent
176 end
177 private fun hook_remove(e: E)
178 do
179 assert e.parent == parent
180 e.parent = null
181 end
182
183 # Used in parent contructor to fill elements
184 private fun unsafe_add_all(nodes: Collection[Object])
185 do
186 var parent = self.parent
187 for n in nodes do
188 assert n isa E
189 add n
190 n.parent = parent
191 end
192 end
193
194 private fun replace_child(old_child: ANode, new_child: nullable ANode): Bool
195 do
196 var parent = self.parent
197 for i in [0..length[ do
198 if self[i] == old_child then
199 if new_child != null then
200 assert new_child isa E
201 self[i] = new_child
202 new_child.parent = parent
203 else
204 self.remove_at(i)
205 end
206 return true
207 end
208 end
209 return false
210 end
211
212 private fun visit_all(v: Visitor)
213 do
214 for n in self do v.enter_visit(n)
215 end
216 end
217
218 # Ancestor of all tokens
219 # A token is a node that has a `text` but no children.
220 abstract class Token
221 super ANode
222
223 # The raw content on the token
224 fun text: String is abstract
225 fun text=(text: String) is abstract
226
227 # The previous token in the Lexer.
228 # May have disapeared in the AST
229 var prev_token: nullable Token = null
230
231 # The next token in the Lexer.
232 # May have disapeared in the AST
233 var next_token: nullable Token = null
234
235 # The verbatim blank text between `prev_token` and `self`
236 fun blank_before: String
237 do
238 if prev_token == null then return ""
239 var from = prev_token.location.pend+1
240 var to = location.pstart
241 return location.file.string.substring(from,to-from)
242 end
243
244 redef fun to_s: String do
245 return "'{text}'"
246 end
247
248 redef fun visit_all(v: Visitor) do end
249 redef fun replace_child(old_child: ANode, new_child: nullable ANode) do end
250 end
251
252 redef class SourceFile
253 # The first token parser by the lexer
254 # May have disapeared in the final AST
255 var first_token: nullable Token = null
256
257 # The first token parser by the lexer
258 # May have disapeared in the final AST
259 var last_token: nullable Token = null
260 end
261
262 # Ancestor of all productions
263 # A production is a node without text but that usually has children.
264 abstract class Prod
265 super ANode
266
267 # All the annotations attached directly to the node
268 var n_annotations: nullable AAnnotations = null is writable
269
270 redef fun replace_with(n: ANode)
271 do
272 super
273 assert n isa Prod
274 if not isset n._location and isset _location then n._location = _location
275 end
276 end
277
278 # Abstract standard visitor on the AST
279 abstract class Visitor
280 # What the visitor do when a node is visited
281 # Concrete visitors should implement this method.
282 # @toimplement
283 protected fun visit(e: ANode) is abstract
284
285 # Ask the visitor to visit a given node.
286 # Usually automatically called by visit_all* methods.
287 # This method should not be redefined
288 fun enter_visit(e: nullable ANode)
289 do
290 if e == null then return
291 var old = _current_node
292 _current_node = e
293 visit(e)
294 _current_node = old
295 end
296
297 # The current visited node
298 var current_node: nullable ANode = null is writable
299 end
300
301 # Token of end of line (basically `\n`)
302 class TEol
303 super Token
304 redef fun to_s
305 do
306 return "end of line"
307 end
308 end
309
310 # Token of a line of comments
311 # Starts with the `#` and contains the final end-of-line (if any)
312 class TComment
313 super Token
314 end
315
316 # A token associated with a keyword
317 abstract class TokenKeyword
318 super Token
319 redef fun to_s
320 do
321 return "keyword '{text}'"
322 end
323 end
324 class TKwpackage
325 super TokenKeyword
326 end
327 class TKwmodule
328 super TokenKeyword
329 end
330 class TKwimport
331 super TokenKeyword
332 end
333 class TKwclass
334 super TokenKeyword
335 end
336 class TKwabstract
337 super TokenKeyword
338 end
339 class TKwinterface
340 super TokenKeyword
341 end
342 class TKwenum
343 super TokenKeyword
344 end
345 class TKwend
346 super TokenKeyword
347 end
348 class TKwmeth
349 super TokenKeyword
350 end
351 class TKwtype
352 super TokenKeyword
353 end
354 class TKwinit
355 super TokenKeyword
356 end
357 class TKwredef
358 super TokenKeyword
359 end
360 class TKwis
361 super TokenKeyword
362 end
363 class TKwdo
364 super TokenKeyword
365 end
366 class TKwvar
367 super TokenKeyword
368 end
369 class TKwextern
370 super TokenKeyword
371 end
372 class TKwpublic
373 super TokenKeyword
374 end
375 class TKwprotected
376 super TokenKeyword
377 end
378 class TKwprivate
379 super TokenKeyword
380 end
381 class TKwintrude
382 super TokenKeyword
383 end
384 class TKwif
385 super TokenKeyword
386 end
387 class TKwthen
388 super TokenKeyword
389 end
390 class TKwelse
391 super TokenKeyword
392 end
393 class TKwwhile
394 super TokenKeyword
395 end
396 class TKwloop
397 super TokenKeyword
398 end
399 class TKwfor
400 super TokenKeyword
401 end
402 class TKwin
403 super TokenKeyword
404 end
405 class TKwand
406 super TokenKeyword
407 end
408 class TKwor
409 super TokenKeyword
410 end
411 class TKwimplies
412 super TokenKeyword
413 end
414 class TKwnot
415 super TokenKeyword
416 end
417 class TKwreturn
418 super TokenKeyword
419 end
420 class TKwcontinue
421 super TokenKeyword
422 end
423 class TKwbreak
424 super TokenKeyword
425 end
426 class TKwabort
427 super TokenKeyword
428 end
429 class TKwassert
430 super TokenKeyword
431 end
432 class TKwnew
433 super TokenKeyword
434 end
435 class TKwisa
436 super TokenKeyword
437 end
438 class TKwonce
439 super TokenKeyword
440 end
441 class TKwsuper
442 super TokenKeyword
443 end
444 class TKwself
445 super TokenKeyword
446 end
447 class TKwtrue
448 super TokenKeyword
449 end
450 class TKwfalse
451 super TokenKeyword
452 end
453 class TKwnull
454 super TokenKeyword
455 end
456 class TKwas
457 super TokenKeyword
458 end
459 class TKwnullable
460 super TokenKeyword
461 end
462 class TKwisset
463 super TokenKeyword
464 end
465 class TKwlabel
466 super TokenKeyword
467 end
468 class TKwdebug
469 super Token
470 end
471 class TOpar
472 super Token
473 end
474 class TCpar
475 super Token
476 end
477 class TObra
478 super Token
479 end
480 class TCbra
481 super Token
482 end
483 class TComma
484 super Token
485 end
486 class TColumn
487 super Token
488 end
489 class TQuad
490 super Token
491 end
492 class TAssign
493 super Token
494 end
495
496 # A token associated with an operator (and other lookalike symbols)
497 abstract class TokenOperator
498 super Token
499 redef fun to_s
500 do
501 return "operator '{text}'"
502 end
503 end
504 class TPluseq
505 super TokenOperator
506 end
507 class TMinuseq
508 super TokenOperator
509 end
510 class TDotdotdot
511 super Token
512 end
513 class TDotdot
514 super Token
515 end
516 class TDot
517 super Token
518 end
519 class TPlus
520 super TokenOperator
521 end
522 class TMinus
523 super TokenOperator
524 end
525 class TStar
526 super TokenOperator
527 end
528 class TStarstar
529 super TokenOperator
530 end
531 class TSlash
532 super TokenOperator
533 end
534 class TPercent
535 super TokenOperator
536 end
537 class TEq
538 super TokenOperator
539 end
540 class TNe
541 super TokenOperator
542 end
543 class TLt
544 super TokenOperator
545 end
546 class TLe
547 super TokenOperator
548 end
549 class TLl
550 super TokenOperator
551 end
552 class TGt
553 super TokenOperator
554 end
555 class TGe
556 super TokenOperator
557 end
558 class TGg
559 super TokenOperator
560 end
561 class TStarship
562 super TokenOperator
563 end
564 class TBang
565 super TokenOperator
566 end
567 class TAt
568 super Token
569 end
570
571 # A class (or formal type) identifier. They start with an uppercase.
572 class TClassid
573 super Token
574 redef fun to_s
575 do
576 do return "type identifier '{text}'"
577 end
578 end
579
580 # A standard identifier (variable, method...). They start with a lowercase.
581 class TId
582 super Token
583 redef fun to_s
584 do
585 do return "identifier '{text}'"
586 end
587 end
588
589 # An attribute identifier. They start with an underscore.
590 class TAttrid
591 super Token
592 redef fun to_s
593 do
594 do return "attribute '{text}'"
595 end
596 end
597
598 # A token of a literal value (string, integer, etc).
599 abstract class TokenLiteral
600 super Token
601 redef fun to_s
602 do
603 do return "literal value '{text}'"
604 end
605 end
606 class TNumber
607 super TokenLiteral
608 end
609 class THexNumber
610 super TokenLiteral
611 end
612 class TFloat
613 super TokenLiteral
614 end
615 class TChar
616 super TokenLiteral
617 end
618 class TString
619 super TokenLiteral
620 end
621 class TStartString
622 super TokenLiteral
623 end
624 class TMidString
625 super TokenLiteral
626 end
627 class TEndString
628 super TokenLiteral
629 end
630
631 # A malformed string
632 class TBadString
633 super Token
634 redef fun to_s
635 do
636 do return "malformed string {text}"
637 end
638 end
639
640 # A malformed char
641 class TBadChar
642 super Token
643 redef fun to_s
644 do
645 do return "malformed character {text}"
646 end
647 end
648
649 class TExternCodeSegment
650 super Token
651 end
652
653 # A end of file
654 class EOF
655 super Token
656 redef fun to_s
657 do
658 return "end of file"
659 end
660 end
661
662 # A mark of an error
663 class AError
664 super EOF
665 end
666 class ALexerError
667 super AError
668 end
669 class AParserError
670 super AError
671 end
672
673 # The main node of a Nit source-file
674 class AModule
675 super Prod
676
677 var n_moduledecl: nullable AModuledecl = null is writable
678 var n_imports: ANodes[AImport] = new ANodes[AImport](self)
679 var n_extern_code_blocks: ANodes[AExternCodeBlock] = new ANodes[AExternCodeBlock](self)
680 var n_classdefs: ANodes[AClassdef] = new ANodes[AClassdef](self)
681 end
682
683 # The declaration of the module with the documentation, name, and annotations
684 class AModuledecl
685 super Prod
686 var n_doc: nullable ADoc = null is writable
687 var n_kwredef: nullable TKwredef = null is writable
688 var n_visibility: AVisibility is writable, noinit
689 var n_kwmodule: TKwmodule is writable, noinit
690 var n_name: AModuleName is writable, noinit
691 end
692
693 # A import clause of a module
694 abstract class AImport
695 super Prod
696 end
697
698 # A standard import clause. eg `import x`
699 class AStdImport
700 super AImport
701 var n_visibility: AVisibility is writable, noinit
702 var n_kwimport: TKwimport is writable, noinit
703 var n_name: AModuleName is writable, noinit
704 end
705
706 # The special import clause of the kernel module. eg `import end`
707 class ANoImport
708 super AImport
709 var n_visibility: AVisibility is writable, noinit
710 var n_kwimport: TKwimport is writable, noinit
711 var n_kwend: TKwend is writable, noinit
712 end
713
714 # A visibility modifier
715 #
716 # The public visibility is an empty production (no keyword).
717 #
718 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
719 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
720 abstract class AVisibility
721 super Prod
722 end
723 class APublicVisibility
724 super AVisibility
725 var n_kwpublic: nullable TKwpublic is writable
726 end
727 class APrivateVisibility
728 super AVisibility
729 var n_kwprivate: TKwprivate is writable, noinit
730 end
731 class AProtectedVisibility
732 super AVisibility
733 var n_kwprotected: TKwprotected is writable, noinit
734 end
735 class AIntrudeVisibility
736 super AVisibility
737 var n_kwintrude: TKwintrude is writable, noinit
738 end
739
740 # A class definition
741 # While most definition are `AStdClassdef`
742 # There is tow special case of class definition
743 abstract class AClassdef
744 super Prod
745 var n_propdefs: ANodes[APropdef] = new ANodes[APropdef](self)
746 end
747
748 # A standard class definition with a name, superclasses and properties
749 class AStdClassdef
750 super AClassdef
751 var n_doc: nullable ADoc = null is writable
752 var n_kwredef: nullable TKwredef = null is writable
753 var n_visibility: AVisibility is writable, noinit
754 var n_classkind: AClasskind is writable, noinit
755 var n_id: nullable TClassid = null is writable
756 var n_formaldefs: ANodes[AFormaldef] = new ANodes[AFormaldef](self)
757 var n_extern_code_block: nullable AExternCodeBlock = null is writable
758 var n_superclasses: ANodes[ASuperclass] = new ANodes[ASuperclass](self)
759 var n_kwend: TKwend is writable, noinit
760 redef fun hot_location do return n_id.location
761 end
762
763 # The implicit class definition of the implicit main method
764 class ATopClassdef
765 super AClassdef
766 end
767
768 # The implicit class definition of the top-level methods
769 class AMainClassdef
770 super AClassdef
771 end
772
773 # The modifier for the kind of class (abstract, interface, etc.)
774 abstract class AClasskind
775 super Prod
776 end
777 class AConcreteClasskind
778 super AClasskind
779 var n_kwclass: TKwclass is writable, noinit
780 end
781 class AAbstractClasskind
782 super AClasskind
783 var n_kwabstract: TKwabstract is writable, noinit
784 var n_kwclass: TKwclass is writable, noinit
785 end
786 class AInterfaceClasskind
787 super AClasskind
788 var n_kwinterface: TKwinterface is writable, noinit
789 end
790 class AEnumClasskind
791 super AClasskind
792 var n_kwenum: TKwenum is writable, noinit
793 end
794 class AExternClasskind
795 super AClasskind
796 var n_kwextern: TKwextern is writable, noinit
797 var n_kwclass: nullable TKwclass = null is writable
798 end
799
800 # The definition of a formal generic parameter type. eg `X: Y`
801 class AFormaldef
802 super Prod
803 var n_id: TClassid is writable, noinit
804 # The bound of the parameter type
805 var n_type: nullable AType = null is writable
806 end
807
808 # A super-class. eg `super X`
809 class ASuperclass
810 super Prod
811 var n_kwsuper: TKwsuper is writable, noinit
812 var n_type: AType is writable, noinit
813 end
814
815 # The definition of a property
816 abstract class APropdef
817 super Prod
818 var n_doc: nullable ADoc = null is writable
819 var n_kwredef: nullable TKwredef = null is writable
820 var n_visibility: nullable AVisibility = null is writable
821 end
822
823 # A definition of an attribute
824 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
825 class AAttrPropdef
826 super APropdef
827 var n_kwvar: TKwvar is writable, noinit
828
829 # The identifier for a new-style attribute (null if old-style)
830 var n_id2: TId is writable, noinit
831
832 var n_type: nullable AType = null is writable
833
834 # The initial value, if any
835 var n_expr: nullable AExpr = null is writable
836 redef fun hot_location
837 do
838 return n_id2.location
839 end
840 end
841
842 # A definition of all kind of method (including constructors)
843 class AMethPropdef
844 super APropdef
845 var n_kwmeth: nullable TKwmeth = null is writable
846 var n_kwinit: nullable TKwinit = null is writable
847 var n_kwnew: nullable TKwnew = null is writable
848 var n_methid: nullable AMethid = null is writable
849 var n_signature: nullable ASignature = null is writable
850 var n_block: nullable AExpr = null is writable
851 var n_extern_calls: nullable AExternCalls = null is writable
852 var n_extern_code_block: nullable AExternCodeBlock = null is writable
853 redef fun hot_location
854 do
855 if n_methid != null then
856 return n_methid.location
857 else if n_kwinit != null then
858 return n_kwinit.location
859 else if n_kwnew != null then
860 return n_kwnew.location
861 else
862 return location
863 end
864 end
865 end
866
867 # The implicit main method
868 class AMainMethPropdef
869 super AMethPropdef
870 end
871
872 # Declaration of callbacks for extern methods
873 class AExternCalls
874 super Prod
875 var n_kwimport: TKwimport is writable, noinit
876 var n_extern_calls: ANodes[AExternCall] = new ANodes[AExternCall](self)
877 end
878 abstract class AExternCall
879 super Prod
880 end
881 abstract class APropExternCall
882 super AExternCall
883 end
884 class ALocalPropExternCall
885 super APropExternCall
886 var n_methid: AMethid is writable, noinit
887 end
888 class AFullPropExternCall
889 super APropExternCall
890 var n_type: AType is writable, noinit
891 var n_dot: nullable TDot = null is writable
892 var n_methid: AMethid is writable, noinit
893 end
894 class AInitPropExternCall
895 super APropExternCall
896 var n_type: AType is writable, noinit
897 end
898 class ASuperExternCall
899 super AExternCall
900 var n_kwsuper: TKwsuper is writable, noinit
901 end
902 abstract class ACastExternCall
903 super AExternCall
904 end
905 class ACastAsExternCall
906 super ACastExternCall
907 var n_from_type: AType is writable, noinit
908 var n_dot: nullable TDot = null is writable
909 var n_kwas: TKwas is writable, noinit
910 var n_to_type: AType is writable, noinit
911 end
912 class AAsNullableExternCall
913 super ACastExternCall
914 var n_type: AType is writable, noinit
915 var n_kwas: TKwas is writable, noinit
916 var n_kwnullable: TKwnullable is writable, noinit
917 end
918 class AAsNotNullableExternCall
919 super ACastExternCall
920 var n_type: AType is writable, noinit
921 var n_kwas: TKwas is writable, noinit
922 var n_kwnot: TKwnot is writable, noinit
923 var n_kwnullable: TKwnullable is writable, noinit
924 end
925
926 # A definition of a virtual type
927 class ATypePropdef
928 super APropdef
929 var n_kwtype: TKwtype is writable, noinit
930 var n_id: TClassid is writable, noinit
931 var n_type: AType is writable, noinit
932 end
933
934 # The identifier of a method in a method declaration.
935 # There is a specific class because of operator and setters.
936 abstract class AMethid
937 super Prod
938 end
939 class AIdMethid
940 super AMethid
941 var n_id: TId is writable, noinit
942 end
943 class APlusMethid
944 super AMethid
945 var n_plus: TPlus is writable, noinit
946 end
947 class AMinusMethid
948 super AMethid
949 var n_minus: TMinus is writable, noinit
950 end
951 class AStarMethid
952 super AMethid
953 var n_star: TStar is writable, noinit
954 end
955 class AStarstarMethid
956 super AMethid
957 var n_starstar: TStarstar is writable, noinit
958 end
959 class ASlashMethid
960 super AMethid
961 var n_slash: TSlash is writable, noinit
962 end
963 class APercentMethid
964 super AMethid
965 var n_percent: TPercent is writable, noinit
966 end
967 class AEqMethid
968 super AMethid
969 var n_eq: TEq is writable, noinit
970 end
971 class ANeMethid
972 super AMethid
973 var n_ne: TNe is writable, noinit
974 end
975 class ALeMethid
976 super AMethid
977 var n_le: TLe is writable, noinit
978 end
979 class AGeMethid
980 super AMethid
981 var n_ge: TGe is writable, noinit
982 end
983 class ALtMethid
984 super AMethid
985 var n_lt: TLt is writable, noinit
986 end
987 class AGtMethid
988 super AMethid
989 var n_gt: TGt is writable, noinit
990 end
991 class ALlMethid
992 super AMethid
993 var n_ll: TLl is writable, noinit
994 end
995 class AGgMethid
996 super AMethid
997 var n_gg: TGg is writable, noinit
998 end
999 class ABraMethid
1000 super AMethid
1001 var n_obra: TObra is writable, noinit
1002 var n_cbra: TCbra is writable, noinit
1003 end
1004 class AStarshipMethid
1005 super AMethid
1006 var n_starship: TStarship is writable, noinit
1007 end
1008 class AAssignMethid
1009 super AMethid
1010 var n_id: TId is writable, noinit
1011 var n_assign: TAssign is writable, noinit
1012 end
1013 class ABraassignMethid
1014 super AMethid
1015 var n_obra: TObra is writable, noinit
1016 var n_cbra: TCbra is writable, noinit
1017 var n_assign: TAssign is writable, noinit
1018 end
1019
1020 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1021 class ASignature
1022 super Prod
1023 var n_opar: nullable TOpar = null is writable
1024 var n_params: ANodes[AParam] = new ANodes[AParam](self)
1025 var n_cpar: nullable TCpar = null is writable
1026 var n_type: nullable AType = null is writable
1027 end
1028
1029 # A parameter definition in a signature. eg `x:X`
1030 class AParam
1031 super Prod
1032 var n_id: TId is writable, noinit
1033 var n_type: nullable AType = null is writable
1034 var n_dotdotdot: nullable TDotdotdot = null is writable
1035 end
1036
1037 # A static type. eg `nullable X[Y]`
1038 class AType
1039 super Prod
1040 var n_kwnullable: nullable TKwnullable = null is writable
1041
1042 # The name of the class or of the formal type
1043 var n_id: TClassid is writable, noinit
1044
1045 # Type arguments for a generic type
1046 var n_types: ANodes[AType] = new ANodes[AType](self)
1047 end
1048
1049 # A label at the end of a block or in a break/continue statement. eg `label x`
1050 class ALabel
1051 super Prod
1052 var n_kwlabel: TKwlabel is writable, noinit
1053 var n_id: nullable TId is writable
1054 end
1055
1056 # Expression and statements
1057 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1058 abstract class AExpr
1059 super Prod
1060 end
1061
1062 # A sequence of AExpr (usually statements)
1063 # The last AExpr gives the value of the whole block
1064 class ABlockExpr
1065 super AExpr
1066 var n_expr: ANodes[AExpr] = new ANodes[AExpr](self)
1067 var n_kwend: nullable TKwend = null is writable
1068 end
1069
1070 # A declaration of a local variable. eg `var x: X = y`
1071 class AVardeclExpr
1072 super AExpr
1073 var n_kwvar: TKwvar is writable, noinit
1074 var n_id: TId is writable, noinit
1075 var n_type: nullable AType = null is writable
1076 var n_assign: nullable TAssign = null is writable
1077
1078 # The initial value, if any
1079 var n_expr: nullable AExpr = null is writable
1080 end
1081
1082 # A `return` statement. eg `return x`
1083 class AReturnExpr
1084 super AExpr
1085 var n_kwreturn: nullable TKwreturn = null is writable
1086 var n_expr: nullable AExpr = null is writable
1087 end
1088
1089 # Something that has a label.
1090 abstract class ALabelable
1091 super Prod
1092 var n_label: nullable ALabel = null is writable
1093 end
1094
1095 # A `break` statement.
1096 class ABreakExpr
1097 super AExpr
1098 super ALabelable
1099 var n_kwbreak: TKwbreak is writable, noinit
1100 var n_expr: nullable AExpr = null is writable
1101 end
1102
1103 # An `abort` statement
1104 class AAbortExpr
1105 super AExpr
1106 var n_kwabort: TKwabort is writable, noinit
1107 end
1108
1109 # A `continue` statement
1110 class AContinueExpr
1111 super AExpr
1112 super ALabelable
1113 var n_kwcontinue: nullable TKwcontinue = null is writable
1114 var n_expr: nullable AExpr = null is writable
1115 end
1116
1117 # A `do` statement
1118 class ADoExpr
1119 super AExpr
1120 super ALabelable
1121 var n_kwdo: TKwdo is writable, noinit
1122 var n_block: nullable AExpr = null is writable
1123 end
1124
1125 # A `if` statement
1126 class AIfExpr
1127 super AExpr
1128 var n_kwif: TKwif is writable, noinit
1129 var n_expr: AExpr is writable, noinit
1130 var n_then: nullable AExpr = null is writable
1131 var n_else: nullable AExpr = null is writable
1132 end
1133
1134 # A `if` expression
1135 class AIfexprExpr
1136 super AExpr
1137 var n_kwif: TKwif is writable, noinit
1138 var n_expr: AExpr is writable, noinit
1139 var n_kwthen: TKwthen is writable, noinit
1140 var n_then: AExpr is writable, noinit
1141 var n_kwelse: TKwelse is writable, noinit
1142 var n_else: AExpr is writable, noinit
1143 end
1144
1145 # A `while` statement
1146 class AWhileExpr
1147 super AExpr
1148 super ALabelable
1149 var n_kwwhile: TKwwhile is writable, noinit
1150 var n_expr: AExpr is writable, noinit
1151 var n_kwdo: TKwdo is writable, noinit
1152 var n_block: nullable AExpr = null is writable
1153 end
1154
1155 # A `loop` statement
1156 class ALoopExpr
1157 super AExpr
1158 super ALabelable
1159 var n_kwloop: TKwloop is writable, noinit
1160 var n_block: nullable AExpr = null is writable
1161 end
1162
1163 # A `for` statement
1164 class AForExpr
1165 super AExpr
1166 super ALabelable
1167 var n_kwfor: TKwfor is writable, noinit
1168 var n_ids: ANodes[TId] = new ANodes[TId](self)
1169 var n_expr: AExpr is writable, noinit
1170 var n_kwdo: TKwdo is writable, noinit
1171 var n_block: nullable AExpr = null is writable
1172 end
1173
1174 # An `assert` statement
1175 class AAssertExpr
1176 super AExpr
1177 var n_kwassert: TKwassert is writable, noinit
1178 var n_id: nullable TId = null is writable
1179 var n_expr: AExpr is writable, noinit
1180 var n_else: nullable AExpr = null is writable
1181 end
1182
1183 # Whatever is a simple assignment. eg `= something`
1184 abstract class AAssignFormExpr
1185 super AExpr
1186 var n_assign: TAssign is writable, noinit
1187 var n_value: AExpr is writable, noinit
1188 end
1189
1190 # Whatever is a combined assignment. eg `+= something`
1191 abstract class AReassignFormExpr
1192 super AExpr
1193 var n_assign_op: AAssignOp is writable, noinit
1194 var n_value: AExpr is writable, noinit
1195 end
1196
1197 # A `once` expression. eg `once x`
1198 class AOnceExpr
1199 super AProxyExpr
1200 var n_kwonce: TKwonce is writable, noinit
1201 end
1202
1203 # A polymorphic invocation of a method
1204 # The form of the invocation (name, arguments, etc) are specific
1205 abstract class ASendExpr
1206 super AExpr
1207 # The receiver of the method invocation
1208 var n_expr: AExpr is writable, noinit
1209 end
1210
1211 # A binary operation on a method
1212 abstract class ABinopExpr
1213 super ASendExpr
1214 # The second operand of the operation
1215 # Note: the receiver (`n_expr`) is the first operand
1216 var n_expr2: AExpr is writable, noinit
1217 end
1218
1219 # Something that is boolean expression
1220 abstract class ABoolExpr
1221 super AExpr
1222 end
1223
1224 # A `or` expression
1225 class AOrExpr
1226 super ABoolExpr
1227 var n_expr: AExpr is writable, noinit
1228 var n_expr2: AExpr is writable, noinit
1229 end
1230
1231 # A `and` expression
1232 class AAndExpr
1233 super ABoolExpr
1234 var n_expr: AExpr is writable, noinit
1235 var n_expr2: AExpr is writable, noinit
1236 end
1237
1238 # A `or else` expression
1239 class AOrElseExpr
1240 super ABoolExpr
1241 var n_expr: AExpr is writable, noinit
1242 var n_expr2: AExpr is writable, noinit
1243 end
1244
1245 # A `implies` expression
1246 class AImpliesExpr
1247 super ABoolExpr
1248 var n_expr: AExpr is writable, noinit
1249 var n_expr2: AExpr is writable, noinit
1250 end
1251
1252 # A `not` expression
1253 class ANotExpr
1254 super ABoolExpr
1255 var n_kwnot: TKwnot is writable, noinit
1256 var n_expr: AExpr is writable, noinit
1257 end
1258
1259 # A `==` expression
1260 class AEqExpr
1261 super ABinopExpr
1262 end
1263
1264 # A `!=` expression
1265 class ANeExpr
1266 super ABinopExpr
1267 end
1268
1269 # A `<` expression
1270 class ALtExpr
1271 super ABinopExpr
1272 end
1273
1274 # A `<=` expression
1275 class ALeExpr
1276 super ABinopExpr
1277 end
1278
1279 # A `<<` expression
1280 class ALlExpr
1281 super ABinopExpr
1282 end
1283
1284 # A `>` expression
1285 class AGtExpr
1286 super ABinopExpr
1287 end
1288
1289 # A `>=` expression
1290 class AGeExpr
1291 super ABinopExpr
1292 end
1293
1294 # A `>>` expression
1295 class AGgExpr
1296 super ABinopExpr
1297 end
1298
1299 # A type-ckeck expression. eg `x isa T`
1300 class AIsaExpr
1301 super ABoolExpr
1302 var n_expr: AExpr is writable, noinit
1303 var n_type: AType is writable, noinit
1304 end
1305
1306 # A `+` expression
1307 class APlusExpr
1308 super ABinopExpr
1309 end
1310
1311 # A `-` expression
1312 class AMinusExpr
1313 super ABinopExpr
1314 end
1315
1316 # A `<=>` expression
1317 class AStarshipExpr
1318 super ABinopExpr
1319 end
1320
1321 # A `*` expression
1322 class AStarExpr
1323 super ABinopExpr
1324 end
1325
1326 # A `**` expression
1327 class AStarstarExpr
1328 super ABinopExpr
1329 end
1330
1331 # A `/` expression
1332 class ASlashExpr
1333 super ABinopExpr
1334 end
1335
1336 # A `%` expression
1337 class APercentExpr
1338 super ABinopExpr
1339 end
1340
1341 # A unary minus expression. eg `-x`
1342 class AUminusExpr
1343 super ASendExpr
1344 var n_minus: TMinus is writable, noinit
1345 end
1346
1347 # An explicit instantiation. eg `new T`
1348 class ANewExpr
1349 super AExpr
1350 var n_kwnew: TKwnew is writable, noinit
1351 var n_type: AType is writable, noinit
1352
1353 # The name of the named-constructor, if any
1354 var n_id: nullable TId = null is writable
1355 var n_args: AExprs is writable, noinit
1356 end
1357
1358 # Whatever is a old-style attribute access
1359 abstract class AAttrFormExpr
1360 super AExpr
1361
1362 # The receiver of the attribute
1363 var n_expr: AExpr is writable, noinit
1364
1365 # The name of the attribute
1366 var n_id: TAttrid is writable, noinit
1367
1368 end
1369
1370 # The read of an attribute. eg `x._a`
1371 class AAttrExpr
1372 super AAttrFormExpr
1373 end
1374
1375 # The assignment of an attribute. eg `x._a=y`
1376 class AAttrAssignExpr
1377 super AAttrFormExpr
1378 super AAssignFormExpr
1379 end
1380
1381 # Whatever looks-like a call with a standard method and any number of arguments.
1382 abstract class ACallFormExpr
1383 super ASendExpr
1384
1385 # The name of the method
1386 var n_id: TId is writable, noinit
1387
1388 # The arguments of the call
1389 var n_args: AExprs is writable, noinit
1390 end
1391
1392 # A complex setter call (standard or brackets)
1393 abstract class ASendReassignFormExpr
1394 super ASendExpr
1395 super AReassignFormExpr
1396 end
1397
1398 # A complex attribute assignment. eg `x._a+=y`
1399 class AAttrReassignExpr
1400 super AAttrFormExpr
1401 super AReassignFormExpr
1402 end
1403
1404 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
1405 # 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`.
1406 # Semantic analysis have to transform them to instance of `AVarExpr`.
1407 class ACallExpr
1408 super ACallFormExpr
1409 end
1410
1411 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
1412 # 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`.
1413 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
1414 class ACallAssignExpr
1415 super ACallFormExpr
1416 super AAssignFormExpr
1417 end
1418
1419 # 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.
1420 # Note: because the parser cannot distinguish a variable write with a compex setter call with an implicit receiver and no arguments, it always returns a `ACallReassignExpr`.
1421 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
1422 class ACallReassignExpr
1423 super ACallFormExpr
1424 super ASendReassignFormExpr
1425 end
1426
1427 # A call to `super`. OR a call of a super-constructor
1428 class ASuperExpr
1429 super AExpr
1430 var n_qualified: nullable AQualified = null is writable
1431 var n_kwsuper: TKwsuper is writable, noinit
1432 var n_args: AExprs is writable, noinit
1433 end
1434
1435 # A call to the `init` constructor.
1436 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a ACallFormExpr.
1437 class AInitExpr
1438 super ASendExpr
1439 var n_kwinit: TKwinit is writable, noinit
1440 var n_args: AExprs is writable, noinit
1441 end
1442
1443 # Whatever looks-like a call of the brackets `[]` operator.
1444 abstract class ABraFormExpr
1445 super ASendExpr
1446 var n_args: AExprs is writable, noinit
1447 end
1448
1449 # A call of the brackets operator. eg `x[y,z]`
1450 class ABraExpr
1451 super ABraFormExpr
1452 end
1453
1454 # A setter call of the bracket operator. eg `x[y,z]=t`
1455 class ABraAssignExpr
1456 super ABraFormExpr
1457 super AAssignFormExpr
1458 end
1459
1460 # Whatever is an access to a local variable
1461 abstract class AVarFormExpr
1462 super AExpr
1463 var n_id: TId is writable, noinit
1464 end
1465
1466 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
1467 class ABraReassignExpr
1468 super ABraFormExpr
1469 super ASendReassignFormExpr
1470 end
1471
1472 # A local variable read access.
1473 # The parser cannot instantiate them, see `ACallExpr`.
1474 class AVarExpr
1475 super AVarFormExpr
1476 end
1477
1478 # A local variable simple assigment access
1479 # The parser cannot instantiate them, see `ACallAssignExpr`.
1480 class AVarAssignExpr
1481 super AVarFormExpr
1482 super AAssignFormExpr
1483 end
1484
1485 # A local variable complex assignment access
1486 # The parser cannot instantiate them, see `ACallReassignExpr`.
1487 class AVarReassignExpr
1488 super AVarFormExpr
1489 super AReassignFormExpr
1490 end
1491
1492 # A literal range, open or closed
1493 abstract class ARangeExpr
1494 super AExpr
1495 var n_expr: AExpr is writable, noinit
1496 var n_expr2: AExpr is writable, noinit
1497 end
1498
1499 # A closed literal range. eg `[x..y]`
1500 class ACrangeExpr
1501 super ARangeExpr
1502 var n_obra: TObra is writable, noinit
1503 var n_cbra: TCbra is writable, noinit
1504 end
1505
1506 # An open literal range. eg `[x..y[`
1507 class AOrangeExpr
1508 super ARangeExpr
1509 var n_obra: TObra is writable, noinit
1510 var n_cbra: TObra is writable, noinit
1511 end
1512
1513 # A literal array. eg. `[x,y,z]`
1514 class AArrayExpr
1515 super AExpr
1516 var n_exprs: AExprs is writable, noinit
1517 end
1518
1519 # A read of `self`
1520 class ASelfExpr
1521 super AExpr
1522 var n_kwself: nullable TKwself is writable
1523 end
1524
1525 # When there is no explicit receiver, `self` is implicit
1526 class AImplicitSelfExpr
1527 super ASelfExpr
1528 end
1529
1530 # A `true` boolean literal constant
1531 class ATrueExpr
1532 super ABoolExpr
1533 var n_kwtrue: TKwtrue is writable, noinit
1534 end
1535 # A `false` boolean literal constant
1536 class AFalseExpr
1537 super ABoolExpr
1538 var n_kwfalse: TKwfalse is writable, noinit
1539 end
1540 # A `null` literal constant
1541 class ANullExpr
1542 super AExpr
1543 var n_kwnull: TKwnull is writable, noinit
1544 end
1545 # An integer literal
1546 class AIntExpr
1547 super AExpr
1548 end
1549 # An integer literal in decimal format
1550 class ADecIntExpr
1551 super AIntExpr
1552 var n_number: TNumber is writable, noinit
1553 end
1554 # An integer literal in hexadecimal format
1555 class AHexIntExpr
1556 super AIntExpr
1557 var n_hex_number: THexNumber is writable, noinit
1558 end
1559 # A float literal
1560 class AFloatExpr
1561 super AExpr
1562 var n_float: TFloat is writable, noinit
1563 end
1564 # A character literal
1565 class ACharExpr
1566 super AExpr
1567 var n_char: TChar is writable, noinit
1568 end
1569 # A string literal
1570 abstract class AStringFormExpr
1571 super AExpr
1572 var n_string: Token is writable, noinit
1573 end
1574
1575 # A simple string. eg. `"abc"`
1576 class AStringExpr
1577 super AStringFormExpr
1578 end
1579
1580 # The start of a superstring. eg `"abc{`
1581 class AStartStringExpr
1582 super AStringFormExpr
1583 end
1584
1585 # The middle of a superstring. eg `}abc{`
1586 class AMidStringExpr
1587 super AStringFormExpr
1588 end
1589
1590 # The end of a superstrng. eg `}abc"`
1591 class AEndStringExpr
1592 super AStringFormExpr
1593 end
1594
1595 # A superstring literal. eg `"a{x}b{y}c"`
1596 # Each part is modelized a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
1597 class ASuperstringExpr
1598 super AExpr
1599 var n_exprs: ANodes[AExpr] = new ANodes[AExpr](self)
1600 end
1601
1602 # A simple parenthesis. eg `(x)`
1603 class AParExpr
1604 super AProxyExpr
1605 var n_opar: TOpar is writable, noinit
1606 var n_cpar: TCpar is writable, noinit
1607 end
1608
1609 # Whatevej just contains (and mimic) an other expression
1610 abstract class AProxyExpr
1611 super AExpr
1612 var n_expr: AExpr is writable, noinit
1613 end
1614
1615 # A type cast. eg `x.as(T)`
1616 class AAsCastExpr
1617 super AExpr
1618 var n_expr: AExpr is writable, noinit
1619 var n_kwas: TKwas is writable, noinit
1620 var n_opar: nullable TOpar = null is writable
1621 var n_type: AType is writable, noinit
1622 var n_cpar: nullable TCpar = null is writable
1623 end
1624
1625 # A as-not-null cast. eg `x.as(not null)`
1626 class AAsNotnullExpr
1627 super AExpr
1628 var n_expr: AExpr is writable, noinit
1629 var n_kwas: TKwas is writable, noinit
1630 var n_opar: nullable TOpar = null is writable
1631 var n_kwnot: TKwnot is writable, noinit
1632 var n_kwnull: TKwnull is writable, noinit
1633 var n_cpar: nullable TCpar = null is writable
1634 end
1635
1636 # A is-set check of old-style attributes. eg `isset x._a`
1637 class AIssetAttrExpr
1638 super AAttrFormExpr
1639 var n_kwisset: TKwisset is writable, noinit
1640 end
1641
1642 # A elyspis notation used to pass an expression as it in a vararg parameter
1643 class AVarargExpr
1644 super AExpr
1645 var n_expr: AExpr is writable, noinit
1646 var n_dotdotdot: TDotdotdot is writable, noinit
1647 end
1648
1649 # A list of expression separated with commas (arguments for instance)
1650 abstract class AExprs
1651 super Prod
1652 var n_exprs: ANodes[AExpr] = new ANodes[AExpr](self)
1653 end
1654
1655
1656 class ADebugTypeExpr
1657 super AExpr
1658 var n_kwdebug: TKwdebug is writable, noinit
1659 var n_kwtype: TKwtype is writable, noinit
1660 var n_expr: AExpr is writable, noinit
1661 var n_type: AType is writable, noinit
1662 end
1663
1664 # A simple list of expressions
1665 class AListExprs
1666 super AExprs
1667 end
1668
1669 # A list of expressions enclosed in parentheses
1670 class AParExprs
1671 super AExprs
1672 var n_opar: TOpar is writable, noinit
1673 var n_cpar: TCpar is writable, noinit
1674 end
1675
1676 # A list of expressions enclosed in brackets
1677 class ABraExprs
1678 super AExprs
1679 var n_obra: TObra is writable, noinit
1680 var n_cbra: TCbra is writable, noinit
1681 end
1682
1683 # A complex assignment operator. eg `+=`
1684 abstract class AAssignOp
1685 super Prod
1686 end
1687 class APlusAssignOp
1688 super AAssignOp
1689 var n_pluseq: TPluseq is writable, noinit
1690 end
1691 class AMinusAssignOp
1692 super AAssignOp
1693 var n_minuseq: TMinuseq is writable, noinit
1694 end
1695
1696 class AModuleName
1697 super Prod
1698 var n_quad: nullable TQuad = null is writable
1699 var n_path: ANodes[TId] = new ANodes[TId](self)
1700 var n_id: TId is writable, noinit
1701 end
1702 class AInLanguage
1703 super Prod
1704 var n_kwin: TKwin is writable, noinit
1705 var n_string: TString is writable, noinit
1706 end
1707 class AExternCodeBlock
1708 super Prod
1709 var n_in_language: nullable AInLanguage = null is writable
1710 var n_extern_code_segment: TExternCodeSegment is writable, noinit
1711 end
1712 class AQualified
1713 super Prod
1714 var n_quad: nullable TQuad = null is writable
1715 var n_id: ANodes[TId] = new ANodes[TId](self)
1716 var n_classid: nullable TClassid = null is writable
1717 end
1718
1719 # A documentation of a definition
1720 # It contains the block of comments just above the declaration
1721 class ADoc
1722 super Prod
1723 var n_comment: ANodes[TComment] = new ANodes[TComment](self)
1724 end
1725
1726 class AAnnotations
1727 super Prod
1728 var n_at: nullable TAt = null is writable
1729 var n_opar: nullable TOpar = null is writable
1730 var n_items: ANodes[AAnnotation] = new ANodes[AAnnotation](self)
1731 var n_cpar: nullable TCpar = null is writable
1732 end
1733 class AAnnotation
1734 super Prod
1735 var n_doc: nullable ADoc = null is writable
1736 var n_kwredef: nullable TKwredef = null is writable
1737 var n_visibility: nullable AVisibility is writable
1738 var n_atid: AAtid is writable, noinit
1739 var n_opar: nullable TOpar = null is writable
1740 var n_args: ANodes[AAtArg] = new ANodes[AAtArg](self)
1741 var n_cpar: nullable TCpar = null is writable
1742 end
1743 abstract class AAtArg
1744 super Prod
1745 end
1746 class ATypeAtArg
1747 super AAtArg
1748 var n_type: AType is writable, noinit
1749 end
1750 class AExprAtArg
1751 super AAtArg
1752 var n_expr: AExpr is writable, noinit
1753 end
1754 class AAtAtArg
1755 super AAtArg
1756 end
1757 abstract class AAtid
1758 super Prod
1759 var n_id: Token is writable, noinit
1760 end
1761 class AIdAtid
1762 super AAtid
1763 end
1764 class AKwexternAtid
1765 super AAtid
1766 end
1767 class AKwimportAtid
1768 super AAtid
1769 end
1770 class AKwabstractAtid
1771 super AAtid
1772 end
1773
1774 # The root of the AST
1775 class Start
1776 super Prod
1777 var n_base: nullable AModule is writable
1778 var n_eof: EOF is writable, noinit
1779 init(n_base: nullable AModule, n_eof: EOF)
1780 do
1781 self._n_base = n_base
1782 self._n_eof = n_eof
1783 end
1784 end