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