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