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