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