1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # AST nodes of the Nit language
16 # Was previously based on parser_abs.nit.
22 # Root of the AST class-hierarchy
24 # Location is set during AST building. Once built, location cannon be null.
25 # However, manual instantiated nodes may need more care.
26 var location
: Location is writable, noinit
28 # The location of the important part of the node (identifier or whatever)
29 fun hot_location
: Location do return location
31 # Display a message for the colored location of the node
32 fun debug
(message
: String)
34 sys
.stderr
.write
"{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
37 # Is `self` a token or a pure-structural production like `AQId`?
38 fun is_structural
: Bool do return false
40 # Write the subtree on stdout.
42 fun dump_tree
(display_structural
: nullable Bool)
44 var d
= new ASTDump(display_structural
or else true)
46 d
.write_to
(sys
.stdout
)
49 # Information to display on a node
51 # Refine this method to add additional information on each node type.
52 fun dump_info
(v
: ASTDump): String do return ""
54 # Parent of the node in the AST
55 var parent
: nullable ANode = null
57 # The topmost ancestor of the element
58 # This just apply `parent` until the first one
64 if p
== null then return res
69 # The most specific common parent between `self` and `other`
70 # Return null if the two node are unrelated (distinct root)
71 fun common_parent
(other
: ANode): nullable ANode
73 # First, get the same depth
74 var s
: nullable ANode = self
75 var o
: nullable ANode = other
76 var d
= s
.depth
- o
.depth
85 assert o
.depth
== s
.depth
86 # Second, go up until same in found
94 # Number of nodes between `self` and the `root` of the AST
95 # ENSURE `self == self.root implies result == 0 `
96 # ENSURE `self != self.root implies result == self.parent.depth + 1`
103 if p
== null then return res
109 # Replace a child with an other node in the AST
110 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode) is abstract
112 # Detach a node from its parent
113 # Aborts if the node is not detachable. use `replace_with` instead
114 # REQUIRE: parent != null
115 # REQUIRE: is_detachable
116 # ENDURE: parent == null
119 assert parent
!= null
120 parent
.replace_child
(self, null)
124 # Replace itself with an other node in the AST
125 # REQUIRE: parent != null
126 # ENSURE: node.parent == old(parent)
127 # ENSURE: parent == null
128 fun replace_with
(node
: ANode)
130 assert parent
!= null
131 parent
.replace_child
(self, node
)
135 # Visit all nodes in order.
136 # Thus, call `v.enter_visit(e)` for each child `e`
137 fun visit_all
(v
: Visitor) is abstract
139 # Do a deep search and return an array of tokens that match a given text
140 fun collect_tokens_by_text
(text
: String): Array[Token]
142 var v
= new CollectTokensByTextVisitor(text
)
147 # Do a deep search and return an array of node that are annotated
148 # The attached node can be retrieved by two invocations of parent
149 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
151 var v
= new CollectAnnotationsByNameVisitor(name
)
157 private class CollectTokensByTextVisitor
160 var result
= new Array[Token]
161 redef fun visit
(node
)
164 if node
isa Token and node
.text
== text
then result
.add
(node
)
168 private class CollectAnnotationsByNameVisitor
171 var result
= new Array[AAnnotation]
172 redef fun visit
(node
)
175 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)
179 # A helper class to handle (print) Nit AST as an OrderedTree
182 super OrderedTree[ANode]
184 # Reference to the last parent in the Ordered Tree
185 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
186 private var last_parent
: nullable ANode = null
188 # Display tokens and structural production?
190 # Should tokens (and structural production like AQId) be displayed?
191 var display_structural
: Bool
195 if not display_structural
and n
.is_structural
then return
205 return "{n.class_name} {n.dump_info(self)} @{n.location}"
209 # A sequence of nodes
210 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
211 class ANodes[E
: ANode]
213 private var parent
: ANode
214 private var items
= new Array[E
]
215 redef fun iterator
do return items
.iterator
216 redef fun reverse_iterator
do return items
.reverse_iterator
217 redef fun length
do return items
.length
218 redef fun is_empty
do return items
.is_empty
237 var res
= items
.shift
249 redef fun []=(index
, e
)
251 hook_remove
(self[index
])
255 redef fun remove_at
(index
)
257 hook_remove
(items
[index
])
258 items
.remove_at
(index
)
260 private fun hook_add
(e
: E
)
262 #assert e.parent == null
265 private fun hook_remove
(e
: E
)
267 assert e
.parent
== parent
271 # Used in parent constructor to fill elements
272 private fun unsafe_add_all
(nodes
: Collection[Object])
274 var parent
= self.parent
282 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
284 var parent
= self.parent
285 for i
in [0..length
[ do
286 if self[i
] == old_child
then
287 if new_child
!= null then
288 assert new_child
isa E
290 new_child
.parent
= parent
300 private fun visit_all
(v
: Visitor)
302 for n
in self do v
.enter_visit
(n
)
306 # Ancestor of all tokens
307 # A token is a node that has a `text` but no children.
311 # The raw content on the token
312 fun text
: String is abstract
314 # The raw content on the token
315 fun text
=(text
: String) is abstract
317 # The previous token in the Lexer.
318 # May have disappeared in the AST
319 var prev_token
: nullable Token = null
321 # The next token in the Lexer.
322 # May have disappeared in the AST
323 var next_token
: nullable Token = null
325 # Is `self` a token discarded from the AST?
327 # Loose tokens are not present in the AST.
328 # It means they were identified by the lexer but were discarded by the parser.
329 # It also means that they are not visited or manipulated by AST-related functions.
331 # Each loose token is attached to the non-loose token that precedes or follows it.
332 # The rules are the following:
334 # * tokens that follow a non-loose token on a same line are attached to it.
336 # * other tokens, thus that precede a non-loose token on the same line or the next one,
337 # are attached to this one. See `prev_looses`.
339 # Loose tokens are mostly end of lines (`TEol`) and comments (`TComment`).
340 # Whitespace are ignored by the lexer, so they are not even considered as loose tokens.
341 # See `blank_before` to get the whitespace that separate tokens.
344 redef fun is_structural
do return true
346 redef fun dump_info
(v
) do return " {text.escape_to_c}"
348 # Loose tokens that precede `self`.
350 # These tokens start the line or belong to a line with only loose tokens.
351 var prev_looses
= new Array[Token] is lazy
353 # Loose tokens that follow `self`
355 # These tokens are on the same line than `self`.
356 var next_looses
= new Array[Token] is lazy
358 # The verbatim blank text between `prev_token` and `self`
359 fun blank_before
: String
361 if prev_token
== null then return ""
362 var from
= prev_token
.location
.pend
+1
363 var to
= location
.pstart
364 return location
.file
.string
.substring
(from
,to-from
)
367 redef fun to_s
: String do
371 redef fun visit_all
(v
: Visitor) do end
372 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
375 redef class SourceFile
376 # The first token parser by the lexer
377 # May have disappeared in the final AST
378 var first_token
: nullable Token = null
380 # The first token parser by the lexer
381 # May have disappeared in the final AST
382 var last_token
: nullable Token = null
385 # Ancestor of all productions
386 # A production is a node without text but that usually has children.
390 # All the annotations attached directly to the node
391 var n_annotations
: nullable AAnnotations = null is writable
393 # Return all its annotations of a given name in the order of their declaration
394 # Retun an empty array if no such an annotation.
395 fun get_annotations
(name
: String): Array[AAnnotation]
397 var res
= new Array[AAnnotation]
398 var nas
= n_annotations
399 if nas
!= null then for na
in nas
.n_items
do
400 if na
.name
!= name
then continue
403 if self isa AClassdef then for na
in n_propdefs
do
404 if na
isa AAnnotPropdef then
405 if na
.name
!= name
then continue
413 redef fun replace_with
(n
: ANode)
417 if not isset n
._location
and isset _location
then n
._location
= _location
421 # Abstract standard visitor on the AST
422 abstract class Visitor
423 # What the visitor do when a node is visited
424 # Concrete visitors should implement this method.
426 protected fun visit
(e
: ANode) is abstract
428 # Ask the visitor to visit a given node.
429 # Usually automatically called by visit_all* methods.
430 # This method should not be redefined
431 fun enter_visit
(e
: nullable ANode)
433 if e
== null then return
434 var old
= _current_node
440 # The current visited node
441 var current_node
: nullable ANode = null is writable
444 # Token of end of line (basically `\n`)
453 # Token of a line of comments
454 # Starts with the `#` and contains the final end-of-line (if any)
459 # A token associated with a keyword
460 abstract class TokenKeyword
464 return "keyword '{text}'"
468 # The deprecated keyword `package`.
473 # The keyword `module`
478 # The keyword `import`
483 # The keyword `class`
488 # The keyword `abstract`
493 # The keyword `interface`
498 # The keywords `enum` ane `universal`
523 # The keyword `redef`
538 # The keyword `catch`
548 # The keyword `extern`
553 # The keyword `public`
558 # The keyword `protected`
563 # The keyword `private`
568 # The keyword `intrude`
588 # The keyword `while`
618 # The keyword `implies`
628 # The keyword `return`
633 # The keyword `continue`
638 # The keyword `break`
643 # The keyword `abort`
648 # The keyword `assert`
668 # The keyword `super`
683 # The keyword `false`
698 # The keyword `nullable`
703 # The keyword `isset`
708 # The keyword `label`
718 # The keyword `yield`
723 # The special keyword `__DEBUG__`
768 # A token associated with an operator (and other lookalike symbols)
769 abstract class TokenOperator
773 return "operator '{text}'"
957 # A class (or formal type) identifier. They start with an uppercase.
962 do return "type identifier '{text}'"
966 # A standard identifier (variable, method...). They start with a lowercase.
971 do return "identifier '{text}'"
975 # An attribute identifier. They start with an underscore.
980 do return "attribute '{text}'"
984 # A token of a literal value (string, integer, etc).
985 abstract class TokenLiteral
989 do return "literal value '{text}'"
998 # A literal floating point number
1003 # A literal character
1013 # The starting part of a super string (between `"` and `{`)
1018 # The middle part of a super string (between `}` and `{`)
1023 # The final part of a super string (between `}` and `"`)
1028 # A malformed string
1033 do return "malformed string {text}"
1042 do return "malformed character {text}"
1046 # A extern code block
1047 class TExternCodeSegment
1056 return "end of file"
1060 # A mark of an error
1064 # A lexical error (unexpected character)
1068 # A syntactic error (unexpected token)
1073 # The main node of a Nit source-file
1077 # The declaration part of the module
1078 var n_moduledecl
: nullable AModuledecl = null is writable
1080 # List of importation clauses
1081 var n_imports
= new ANodes[AImport](self)
1083 # List of extern blocks
1084 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1086 # List of class definition (including top-level methods and the main)
1087 var n_classdefs
= new ANodes[AClassdef](self)
1090 # Abstract class for definition of entities
1091 abstract class ADefinition
1094 var n_doc
: nullable ADoc = null is writable
1096 # The `redef` keyword
1097 var n_kwredef
: nullable TKwredef = null is writable
1099 # The declared visibility
1100 var n_visibility
: nullable AVisibility = null is writable
1103 # The declaration of the module with the documentation, name, and annotations
1107 # The `module` keyword
1108 var n_kwmodule
: TKwmodule is writable, noinit
1110 # The declared module name
1111 var n_name
: AModuleName is writable, noinit
1114 # A import clause of a module
1115 abstract class AImport
1118 # The declared visibility
1119 var n_visibility
: AVisibility is writable, noinit
1121 # The `import` keyword
1122 var n_kwimport
: TKwimport is writable, noinit
1125 # A standard import clause. eg `import x`
1128 # The imported module name
1129 var n_name
: AModuleName is writable, noinit
1132 # The special import clause of the kernel module. eg `import end`
1135 # The `end` keyword, that indicate the root module
1136 var n_kwend
: TKwend is writable, noinit
1139 # A visibility modifier
1141 # The public visibility is an empty production (no keyword).
1143 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1144 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1145 abstract class AVisibility
1149 # An implicit or explicit public visibility modifier
1150 class APublicVisibility
1152 # The `public` keyword, if any
1153 var n_kwpublic
: nullable TKwpublic = null is writable
1155 # An explicit private visibility modifier
1156 class APrivateVisibility
1158 # The `private` keyword
1159 var n_kwprivate
: TKwprivate is writable, noinit
1161 # An explicit protected visibility modifier
1162 class AProtectedVisibility
1164 # The `protected` keyword
1165 var n_kwprotected
: TKwprotected is writable, noinit
1167 # An explicit intrude visibility modifier
1168 class AIntrudeVisibility
1170 # The `intrude` keyword
1171 var n_kwintrude
: TKwintrude is writable, noinit
1174 # A class definition
1175 # While most definition are `AStdClassdef`
1176 # There is tow special case of class definition
1177 abstract class AClassdef
1179 # All the declared properties (including the main method)
1180 var n_propdefs
= new ANodes[APropdef](self)
1183 # A standard class definition with a name, superclasses and properties
1188 # The class kind (interface, abstract class, etc.)
1189 var n_classkind
: AClasskind is writable, noinit
1191 # The name of the class
1192 var n_qid
: nullable AQclassid = null is writable
1195 var n_obra
: nullable TObra = null is writable
1197 # The list of formal parameter types
1198 var n_formaldefs
= new ANodes[AFormaldef](self)
1201 var n_cbra
: nullable TCbra = null is writable
1203 # The extern block code
1204 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1207 var n_kwend
: TKwend is writable, noinit
1209 fun n_superclasses
: Array[ASuperPropdef] do
1210 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1213 redef fun hot_location
do return n_qid
.location
1216 # The implicit class definition of the implicit main method
1221 # The implicit class definition of the top-level methods
1226 # The modifier for the kind of class (abstract, interface, etc.)
1227 abstract class AClasskind
1231 # A default, or concrete class modifier (just `class`)
1232 class AConcreteClasskind
1235 # The `class` keyword.
1236 var n_kwclass
: TKwclass is writable, noinit
1239 # An abstract class modifier (`abstract class`)
1240 class AAbstractClasskind
1243 # The `abstract` keyword.
1244 var n_kwabstract
: TKwabstract is writable, noinit
1246 # The `class` keyword.
1247 var n_kwclass
: TKwclass is writable, noinit
1250 # An interface class modifier (`interface`)
1251 class AInterfaceClasskind
1254 # The `interface` keyword.
1255 var n_kwinterface
: TKwinterface is writable, noinit
1258 # An enum/universal class modifier (`enum class`)
1259 class AEnumClasskind
1262 # The `enum` keyword.
1263 var n_kwenum
: TKwenum is writable, noinit
1266 # An extern class modifier (`extern class`)
1267 class AExternClasskind
1270 # The `extern` keyword.
1271 var n_kwextern
: TKwextern is writable, noinit
1273 # The `class` keyword.
1274 var n_kwclass
: nullable TKwclass = null is writable
1277 # The definition of a formal generic parameter type. eg `X: Y`
1281 # The name of the parameter type
1282 var n_id
: TClassid is writable, noinit
1284 # The bound of the parameter type
1285 var n_type
: nullable AType = null is writable
1288 # The definition of a property
1289 abstract class APropdef
1293 # A definition of an attribute
1294 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1299 var n_kwvar
: TKwvar is writable, noinit
1301 # The identifier for a new-style attribute
1302 var n_id2
: TId is writable, noinit
1304 # The declared type of the attribute
1305 var n_type
: nullable AType = null is writable
1308 var n_assign
: nullable TAssign = null is writable
1310 # The initial value, if any (set with `=`)
1311 var n_expr
: nullable AExpr = null is writable
1314 var n_kwdo
: nullable TKwdo = null is writable
1316 # The initial value, if any (set with `do return`)
1317 var n_block
: nullable AExpr = null is writable
1320 var n_kwend
: nullable TKwend = null is writable
1322 redef fun hot_location
1324 return n_id2
.location
1328 # A definition of all kind of method (including constructors)
1332 # The `fun` keyword, if any
1333 var n_kwmeth
: nullable TKwmeth = null is writable
1335 # The `init` keyword, if any
1336 var n_kwinit
: nullable TKwinit = null is writable
1338 # The `new` keyword, if any
1339 var n_kwnew
: nullable TKwnew = null is writable
1341 # The name of the method, if any
1342 var n_methid
: nullable AMethid = null is writable
1344 # The signature of the method, if any
1345 var n_signature
: nullable ASignature = null is writable
1348 var n_kwdo
: nullable TKwdo = null is writable
1350 # The body (in Nit) of the method, if any
1351 var n_block
: nullable AExpr = null is writable
1354 var n_kwend
: nullable TKwend = null is writable
1356 # The list of declared callbacks (for extern methods)
1357 var n_extern_calls
: nullable AExternCalls = null is writable
1359 # The body (in extern code) of the method, if any
1360 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1362 redef fun hot_location
1364 if n_methid
!= null then
1365 return n_methid
.location
1366 else if n_kwinit
!= null then
1367 return n_kwinit
.location
1368 else if n_kwnew
!= null then
1369 return n_kwnew
.location
1376 # The implicit main method
1377 class AMainMethPropdef
1386 # A super-class. eg `super X`
1391 var n_kwsuper
: TKwsuper is writable, noinit
1393 # The super-class (indicated as a type)
1394 var n_type
: AType is writable, noinit
1398 # Declaration of callbacks for extern methods
1402 # The `import` keyword
1403 var n_kwimport
: TKwimport is writable, noinit
1405 # The list of declared callbacks
1406 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1409 # A single callback declaration
1410 abstract class AExternCall
1414 # A single callback declaration on a method
1415 abstract class APropExternCall
1419 # A single callback declaration on a method on the current receiver
1420 class ALocalPropExternCall
1421 super APropExternCall
1423 # The name of the called-back method
1424 var n_methid
: AMethid is writable, noinit
1427 # A single callback declaration on a method on an explicit receiver type.
1428 class AFullPropExternCall
1429 super APropExternCall
1431 # The type of the receiver of the called-back method
1432 var n_type
: AType is writable, noinit
1435 var n_dot
: nullable TDot = null is writable
1437 # The name of the called-back method
1438 var n_methid
: AMethid is writable, noinit
1441 # A single callback declaration on a method on a constructor
1442 class AInitPropExternCall
1443 super APropExternCall
1445 # The allocated type
1446 var n_type
: AType is writable, noinit
1449 # A single callback declaration on a `super` call
1450 class ASuperExternCall
1453 # The `super` keyword
1454 var n_kwsuper
: TKwsuper is writable, noinit
1457 # A single callback declaration on a cast
1458 abstract class ACastExternCall
1462 # A single callback declaration on a cast to a given type
1463 class ACastAsExternCall
1464 super ACastExternCall
1466 # The origin type of the cast
1467 var n_from_type
: AType is writable, noinit
1470 var n_dot
: nullable TDot = null is writable
1473 var n_kwas
: TKwas is writable, noinit
1475 # The destination of the cast
1476 var n_to_type
: AType is writable, noinit
1479 # A single callback declaration on a cast to a nullable type
1480 class AAsNullableExternCall
1481 super ACastExternCall
1483 # The origin type to cast as nullable
1484 var n_type
: AType is writable, noinit
1487 var n_kwas
: TKwas is writable, noinit
1489 # The `nullable` keyword
1490 var n_kwnullable
: TKwnullable is writable, noinit
1493 # A single callback declaration on a cast to a non-nullable type
1494 class AAsNotNullableExternCall
1495 super ACastExternCall
1497 # The destination type on a cast to not nullable
1498 var n_type
: AType is writable, noinit
1501 var n_kwas
: TKwas is writable, noinit
1504 var n_kwnot
: TKwnot is writable, noinit
1506 # The `nullable` keyword
1507 var n_kwnullable
: TKwnullable is writable, noinit
1510 # A definition of a virtual type
1514 # The `type` keyword
1515 var n_kwtype
: TKwtype is writable, noinit
1517 # The name of the virtual type
1518 var n_qid
: AQclassid is writable, noinit
1520 # The bound of the virtual type
1521 var n_type
: AType is writable, noinit
1524 # The identifier of a method in a method declaration.
1525 # There is a specific class because of operator and setters.
1526 abstract class AMethid
1530 # A method name with a simple identifier
1534 # The simple identifier
1535 var n_id
: TId is writable, noinit
1538 # A method name for an operator
1539 class AOperatorMethid
1542 # The associated operator symbol
1543 var n_op
: Token is writable, noinit
1547 super AOperatorMethid
1552 super AOperatorMethid
1557 super AOperatorMethid
1560 # A method name `**`
1561 class AStarstarMethid
1562 super AOperatorMethid
1567 super AOperatorMethid
1571 class APercentMethid
1572 super AOperatorMethid
1577 super AOperatorMethid
1582 super AOperatorMethid
1587 super AOperatorMethid
1592 super AOperatorMethid
1595 # A method name `==`
1597 super AOperatorMethid
1600 # A method name `!=`
1602 super AOperatorMethid
1605 # A method name `<=`
1607 super AOperatorMethid
1610 # A method name `>=`
1612 super AOperatorMethid
1617 super AOperatorMethid
1622 super AOperatorMethid
1625 # A method name `<<`
1627 super AOperatorMethid
1630 # A method name `>>`
1632 super AOperatorMethid
1635 # A method name `<=>`
1636 class AStarshipMethid
1637 super AOperatorMethid
1640 # A method name `[]`
1645 var n_obra
: TObra is writable, noinit
1648 var n_cbra
: TCbra is writable, noinit
1651 # A setter method name with a simple identifier (with a `=`)
1655 # The base identifier
1656 var n_id
: TId is writable, noinit
1659 var n_assign
: TAssign is writable, noinit
1662 # A method name `[]=`
1663 class ABraassignMethid
1667 var n_obra
: TObra is writable, noinit
1670 var n_cbra
: TCbra is writable, noinit
1673 var n_assign
: TAssign is writable, noinit
1676 # A potentially qualified simple identifier `foo::bar::baz`
1679 # The qualifier, if any
1680 var n_qualified
: nullable AQualified = null is writable
1682 # The final identifier
1683 var n_id
: TId is writable, noinit
1685 redef fun is_structural
do return true
1688 # A potentially qualified class identifier `foo::bar::Baz`
1691 # The qualifier, if any
1692 var n_qualified
: nullable AQualified = null is writable
1694 # The final identifier
1695 var n_id
: TClassid is writable, noinit
1697 redef fun is_structural
do return true
1700 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1705 var n_opar
: nullable TOpar = null is writable
1707 # The list of parameters
1708 var n_params
= new ANodes[AParam](self)
1711 var n_cpar
: nullable TCpar = null is writable
1714 var n_type
: nullable AType = null is writable
1717 # A parameter definition in a signature. eg `x:X`
1721 # The name of the parameter
1722 var n_id
: TId is writable, noinit
1724 # The type of the parameter, if any
1725 var n_type
: nullable AType = null is writable
1727 # The `...` symbol to indicate varargs
1728 var n_dotdotdot
: nullable TDotdotdot = null is writable
1731 # A static type. eg `nullable X[Y]`
1734 # The `nullable` keyword
1735 var n_kwnullable
: nullable TKwnullable = null is writable
1737 # The name of the class or of the formal type
1738 var n_qid
: AQclassid is writable, noinit
1740 # The opening bracket
1741 var n_obra
: nullable TObra = null is writable
1743 # Type arguments for a generic type
1744 var n_types
= new ANodes[AType](self)
1746 # The closing bracket
1747 var n_cbra
: nullable TCbra = null is writable
1750 # A label at the end of a block or in a break/continue statement. eg `label x`
1754 # The `label` keyword
1755 var n_kwlabel
: TKwlabel is writable, noinit
1757 # The name of the label, if any
1758 var n_id
: nullable TId is writable, noinit
1761 # Expression and statements
1762 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1763 abstract class AExpr
1767 # A sequence of `AExpr` (usually statements)
1768 # The last `AExpr` gives the value of the whole block
1772 # The list of statements in the bloc.
1773 # The last element is often considered as an expression that give the value of the whole block.
1774 var n_expr
= new ANodes[AExpr](self)
1777 var n_kwend
: nullable TKwend = null is writable
1780 # A declaration of a local variable. eg `var x: X = y`
1785 var n_kwvar
: nullable TKwvar = null is writable
1787 # The name of the local variable
1788 var n_id
: TId is writable, noinit
1790 # The declaration type of the local variable
1791 var n_type
: nullable AType = null is writable
1793 # The `=` symbol (for the initial value)
1794 var n_assign
: nullable TAssign = null is writable
1796 # The initial value, if any
1797 var n_expr
: nullable AExpr = null is writable
1800 # A `return` statement. eg `return x`
1804 # The `return` keyword
1805 var n_kwreturn
: nullable TKwreturn = null is writable
1808 # A `yield` statement. eg `yield x`
1812 # The `yield` keyword
1813 var n_kwyield
: nullable TKwyield = null is writable
1815 # The return value, if any
1816 var n_expr
: nullable AExpr = null is writable
1819 # Something that has a label.
1820 abstract class ALabelable
1823 # The associated label declatation
1824 var n_label
: nullable ALabel = null is writable
1827 # A `break` or a `continue`
1828 abstract class AEscapeExpr
1832 # The return value, if nay (unused currently)
1833 var n_expr
: nullable AExpr = null is writable
1836 # A `break` statement.
1840 # The `break` keyword
1841 var n_kwbreak
: TKwbreak is writable, noinit
1844 # An `abort` statement
1848 # The `abort` keyword
1849 var n_kwabort
: TKwabort is writable, noinit
1852 # A `continue` statement
1856 # The `continue` keyword.
1857 var n_kwcontinue
: nullable TKwcontinue = null is writable
1866 var n_kwdo
: TKwdo is writable, noinit
1868 # The list of statements of the `do`.
1869 var n_block
: nullable AExpr = null is writable
1871 # The `catch` keyword
1872 var n_kwcatch
: nullable TKwcatch = null is writable
1874 # The do catch block
1875 var n_catch
: nullable AExpr = null is writable
1883 var n_kwif
: TKwif is writable, noinit
1885 # The expression used as the condition of the `if`
1886 var n_expr
: AExpr is writable, noinit
1888 # The `then` keyword
1889 var n_kwthen
: TKwthen is writable, noinit
1891 # The body of the `then` part
1892 var n_then
: nullable AExpr = null is writable
1894 # The `else` keyword
1895 var n_kwelse
: nullable TKwelse = null is writable
1897 # The body of the `else` part
1898 var n_else
: nullable AExpr = null is writable
1901 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1906 var n_kwif
: TKwif is writable, noinit
1908 # The expression used as the condition of the `if`
1909 var n_expr
: AExpr is writable, noinit
1911 # The `then` keyword
1912 var n_kwthen
: TKwthen is writable, noinit
1914 # The expression in the `then` part
1915 var n_then
: AExpr is writable, noinit
1917 # The `else` keyword
1918 var n_kwelse
: TKwelse is writable, noinit
1920 # The expression in the `else` part
1921 var n_else
: AExpr is writable, noinit
1924 # A `while` statement
1929 # The `while` keyword
1930 var n_kwwhile
: TKwwhile is writable, noinit
1932 # The expression used as the condition of the `while`
1933 var n_expr
: AExpr is writable, noinit
1936 var n_kwdo
: TKwdo is writable, noinit
1938 # The body of the loop
1939 var n_block
: nullable AExpr = null is writable
1942 # A `loop` statement
1947 # The `loop` keyword
1948 var n_kwloop
: TKwloop is writable, noinit
1950 # The body of the loop
1951 var n_block
: nullable AExpr = null is writable
1960 var n_kwfor
: TKwfor is writable, noinit
1962 # The list of groups to iterate
1963 var n_groups
= new ANodes[AForGroup](self)
1966 var n_kwdo
: TKwdo is writable, noinit
1968 # The body of the loop
1969 var n_block
: nullable AExpr = null is writable
1972 # A collection iterated by a for, its automatic variables and its implicit iterator.
1974 # Standard `for` iterate on a single collection.
1975 # Multiple `for` can iterate on more than one collection at once.
1979 # The list of name of the automatic variables
1980 var n_ids
= new ANodes[TId](self)
1983 var n_kwin
: TKwin is writable, noinit
1985 # The expression used as the collection to iterate on
1986 var n_expr
: AExpr is writable, noinit
1989 # A `with` statement
1994 # The `with` keyword
1995 var n_kwwith
: TKwwith is writable, noinit
1997 # The expression used to get the value to control
1998 var n_expr
: AExpr is writable, noinit
2001 var n_kwdo
: TKwdo is writable, noinit
2003 # The body of the loop
2004 var n_block
: nullable AExpr = null is writable
2007 # An `assert` statement
2011 # The `assert` keyword
2012 var n_kwassert
: TKwassert is writable, noinit
2014 # The name of the assert, if any
2015 var n_id
: nullable TId = null is writable
2017 # The expression used as the condition of the `assert`
2018 var n_expr
: AExpr is writable, noinit
2020 # The `else` keyword
2021 var n_kwelse
: nullable TKwelse = null is writable
2023 # The body to execute when the assert fails
2024 var n_else
: nullable AExpr = null is writable
2027 # Whatever is a simple assignment. eg `= something`
2028 abstract class AAssignFormExpr
2032 var n_assign
: TAssign is writable, noinit
2034 # The right-value to assign.
2035 var n_value
: AExpr is writable, noinit
2038 # Whatever is a combined assignment. eg `+= something`
2039 abstract class AReassignFormExpr
2042 # The combined operator (eg. `+=`)
2043 var n_assign_op
: AAssignOp is writable, noinit
2045 # The right-value to apply on the combined operator.
2046 var n_value
: AExpr is writable, noinit
2049 # A `once` expression. eg `once x`
2053 # The `once` keyword
2054 var n_kwonce
: TKwonce is writable, noinit
2056 # The expression to evaluate only one time
2057 var n_expr
: AExpr is writable, noinit
2060 # A polymorphic invocation of a method
2061 # The form of the invocation (name, arguments, etc.) are specific
2062 abstract class ASendExpr
2064 # The receiver of the method invocation
2065 var n_expr
: AExpr is writable, noinit
2068 # A binary operation on a method
2069 abstract class ABinopExpr
2073 var n_op
: Token is writable, noinit
2075 # The second operand of the operation
2076 # Note: the receiver (`n_expr`) is the first operand
2077 var n_expr2
: AExpr is writable, noinit
2079 # The name of the operator (eg '+')
2080 fun operator
: String is abstract
2083 # Something that is boolean expression
2084 abstract class ABoolExpr
2088 # Something that is binary boolean expression
2089 abstract class ABinBoolExpr
2092 # The first boolean operand
2093 var n_expr
: AExpr is writable, noinit
2096 var n_op
: Token is writable, noinit
2098 # The second boolean operand
2099 var n_expr2
: AExpr is writable, noinit
2107 # A `and` expression
2112 # A `or else` expression
2116 # The `else` keyword
2117 var n_kwelse
: TKwelse is writable, noinit
2120 # A `implies` expression
2125 # A `not` expression
2130 var n_kwnot
: TKwnot is writable, noinit
2132 # The boolean operand of the `not`
2133 var n_expr
: AExpr is writable, noinit
2136 # A `==` or a `!=` expression
2138 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2146 redef fun operator
do return "=="
2152 redef fun operator
do return "!="
2158 redef fun operator
do return "<"
2164 redef fun operator
do return "<="
2170 redef fun operator
do return "<<"
2176 redef fun operator
do return ">"
2182 redef fun operator
do return ">="
2188 redef fun operator
do return ">>"
2191 # A type-ckeck expression. eg `x isa T`
2195 # The expression to check
2196 var n_expr
: AExpr is writable, noinit
2199 var n_kwisa
: TKwisa is writable, noinit
2201 # The destination type to check to
2202 var n_type
: AType is writable, noinit
2208 redef fun operator
do return "+"
2214 redef fun operator
do return "-"
2217 # A `<=>` expression
2220 redef fun operator
do return "<=>"
2226 redef fun operator
do return "*"
2232 redef fun operator
do return "**"
2238 redef fun operator
do return "/"
2244 redef fun operator
do return "%"
2250 redef fun operator
do return "|"
2256 redef fun operator
do return "^"
2262 redef fun operator
do return "&"
2265 # A unary operation on a method
2266 abstract class AUnaryopExpr
2270 var n_op
: Token is writable, noinit
2272 # The name of the operator (eg '+')
2273 fun operator
: String is abstract
2276 # A unary minus expression. eg `-x`
2279 redef fun operator
do return "-"
2282 # A unary plus expression. eg `+x`
2285 redef fun operator
do return "+"
2288 # A unary `~` expression
2291 redef fun operator
do return "~"
2294 # An explicit instantiation. eg `new T`
2299 var n_kwnew
: TKwnew is writable, noinit
2301 # The `type` keyword
2302 var n_type
: AType is writable, noinit
2304 # The name of the named-constructor, if any
2305 var n_qid
: nullable AQid = null is writable
2307 # The arguments of the `new`
2308 var n_args
: AExprs is writable, noinit
2311 # Whatever is a old-style attribute access
2312 abstract class AAttrFormExpr
2315 # The receiver of the attribute
2316 var n_expr
: AExpr is writable, noinit
2318 # The name of the attribute
2319 var n_id
: TAttrid is writable, noinit
2323 # The read of an attribute. eg `x._a`
2328 # The assignment of an attribute. eg `x._a=y`
2329 class AAttrAssignExpr
2331 super AAssignFormExpr
2334 # Whatever looks-like a call with a standard method and any number of arguments.
2335 abstract class ACallFormExpr
2338 # The name of the method
2339 var n_qid
: AQid is writable, noinit
2341 # The arguments of the call
2342 var n_args
: AExprs is writable, noinit
2345 # A complex setter call (standard or brackets)
2346 abstract class ASendReassignFormExpr
2348 super AReassignFormExpr
2351 # A complex attribute assignment. eg `x._a+=y`
2352 class AAttrReassignExpr
2354 super AReassignFormExpr
2357 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2358 # 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`.
2359 # Semantic analysis have to transform them to instance of `AVarExpr`.
2364 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2365 # 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`.
2366 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2367 class ACallAssignExpr
2369 super AAssignFormExpr
2372 # 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.
2373 # Note: because the parser cannot distinguish a variable write with a complex setter call with an implicit receiver and no arguments, it always returns a `ACallReassignExpr`.
2374 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2375 class ACallReassignExpr
2377 super ASendReassignFormExpr
2380 # A call to `super`. OR a call of a super-constructor
2384 # The qualifier part before the super (currenlty unused)
2385 var n_qualified
: nullable AQualified = null is writable
2387 # The `super` keyword
2388 var n_kwsuper
: TKwsuper is writable, noinit
2390 # The arguments of the super
2391 var n_args
: AExprs is writable, noinit
2394 # A call to the `init` constructor.
2395 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2399 # The `init` keyword
2400 var n_kwinit
: TKwinit is writable, noinit
2402 # The arguments of the init
2403 var n_args
: AExprs is writable, noinit
2406 # Whatever looks-like a call of the brackets `[]` operator.
2407 abstract class ABraFormExpr
2410 # The arguments inside the brackets
2411 var n_args
: AExprs is writable, noinit
2414 # A call of the brackets operator. eg `x[y,z]`
2419 # A setter call of the bracket operator. eg `x[y,z]=t`
2420 class ABraAssignExpr
2422 super AAssignFormExpr
2425 # Whatever is an access to a local variable
2426 abstract class AVarFormExpr
2429 # The name of the attribute
2430 var n_id
: TId is writable, noinit
2433 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2434 class ABraReassignExpr
2436 super ASendReassignFormExpr
2439 # A local variable read access.
2440 # The parser cannot instantiate them, see `ACallExpr`.
2445 # A local variable simple assignment access
2446 # The parser cannot instantiate them, see `ACallAssignExpr`.
2447 class AVarAssignExpr
2449 super AAssignFormExpr
2452 # A local variable complex assignment access
2453 # The parser cannot instantiate them, see `ACallReassignExpr`.
2454 class AVarReassignExpr
2456 super AReassignFormExpr
2459 # A literal range, open or closed
2460 abstract class ARangeExpr
2463 # The left (lower) element of the range
2464 var n_expr
: AExpr is writable, noinit
2467 var n_dotdot
: TDotdot is writable, noinit
2469 # The right (upper) element of the range
2470 var n_expr2
: AExpr is writable, noinit
2473 # A closed literal range. eg `[x..y]`
2477 # The opening bracket `[`
2478 var n_obra
: TObra is writable, noinit
2480 # The closing bracket `]`
2481 var n_cbra
: TCbra is writable, noinit
2484 # An open literal range. eg `[x..y[`
2488 # The opening bracket `[`
2489 var n_obra
: TObra is writable, noinit
2491 # The closing bracket `[` (because open range)
2492 var n_cbra
: TObra is writable, noinit
2495 # A literal array. eg. `[x,y,z]`
2499 # The opening bracket `[`
2500 var n_obra
: TObra is writable, noinit
2502 # The elements of the array
2503 var n_exprs
= new ANodes[AExpr](self)
2505 # The type of the element of the array (if any)
2506 var n_type
: nullable AType = null is writable
2508 # The closing bracket `]`
2509 var n_cbra
: TCbra is writable, noinit
2516 # The `self` keyword
2517 var n_kwself
: nullable TKwself = null is writable
2520 # When there is no explicit receiver, `self` is implicit
2521 class AImplicitSelfExpr
2525 # A `true` boolean literal constant
2529 # The `true` keyword
2530 var n_kwtrue
: TKwtrue is writable, noinit
2533 # A `false` boolean literal constant
2537 # The `false` keyword
2538 var n_kwfalse
: TKwfalse is writable, noinit
2541 # A `null` literal constant
2545 # The `null` keyword
2546 var n_kwnull
: TKwnull is writable, noinit
2549 # An integer literal
2554 var n_integer
: TInteger is writable, noinit
2562 var n_float
: TFloat is writable, noinit
2565 # A character literal
2569 # The character token
2570 var n_char
: TChar is writable, noinit
2574 abstract class AStringFormExpr
2578 var n_string
: Token is writable, noinit
2581 # A simple string. eg. `"abc"`
2583 super AStringFormExpr
2586 # The start of a superstring. eg `"abc{`
2587 class AStartStringExpr
2588 super AStringFormExpr
2591 # The middle of a superstring. eg `}abc{`
2592 class AMidStringExpr
2593 super AStringFormExpr
2596 # The end of a superstrng. eg `}abc"`
2597 class AEndStringExpr
2598 super AStringFormExpr
2601 # A superstring literal. eg `"a{x}b{y}c"`
2602 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2603 class ASuperstringExpr
2606 # The list of the expressions of the superstring
2607 var n_exprs
= new ANodes[AExpr](self)
2610 # A simple parenthesis. eg `(x)`
2614 # The opening parenthesis
2615 var n_opar
: TOpar is writable, noinit
2617 # The inner expression
2618 var n_expr
: AExpr is writable, noinit
2620 # The closing parenthesis
2621 var n_cpar
: TCpar is writable, noinit
2624 # A cast, against a type or `not null`
2628 # The expression to cast
2629 var n_expr
: AExpr is writable, noinit
2632 var n_kwas
: TKwas is writable, noinit
2634 # The opening parenthesis
2635 var n_opar
: nullable TOpar = null is writable
2637 # The closing parenthesis
2638 var n_cpar
: nullable TCpar = null is writable
2641 # A type cast. eg `x.as(T)`
2645 # The target type to cast to
2646 var n_type
: AType is writable, noinit
2649 # A as-not-null cast. eg `x.as(not null)`
2650 class AAsNotnullExpr
2654 var n_kwnot
: TKwnot is writable, noinit
2656 # The `null` keyword
2657 var n_kwnull
: TKwnull is writable, noinit
2660 # A is-set check of old-style attributes. eg `isset x._a`
2661 class AIssetAttrExpr
2664 # The `isset` keyword
2665 var n_kwisset
: TKwisset is writable, noinit
2668 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2672 # The passed expression
2673 var n_expr
: AExpr is writable, noinit
2676 var n_dotdotdot
: TDotdotdot is writable, noinit
2679 # An named notation used to pass an expression by name in a parameter
2683 # The name of the argument
2684 var n_id
: TId is writable, noinit
2687 var n_assign
: TAssign is writable, noinit
2689 # The passed expression
2690 var n_expr
: AExpr is writable, noinit
2693 # A list of expression separated with commas (arguments for instance)
2697 # The list of expressions
2698 var n_exprs
= new ANodes[AExpr](self)
2701 # A special expression that encapsulates a static type
2702 # Can only be found in special construction like arguments of annotations.
2706 # The encapsulated type
2707 var n_type
: AType is writable, noinit
2710 # A special expression that encapsulates a method identifier
2711 # Can only be found in special construction like arguments of annotations.
2716 var n_expr
: AExpr is writable, noinit
2718 # The encapsulated method identifier
2719 var n_id
: AMethid is writable, noinit
2722 # A special expression that encapsulate an annotation
2723 # Can only be found in special construction like arguments of annotations.
2725 # The encapsulated annotations are in `n_annotations`
2730 # A special expression to debug types
2731 class ADebugTypeExpr
2734 # The `debug` keyword
2735 var n_kwdebug
: TKwdebug is writable, noinit
2737 # The `type` keyword
2738 var n_kwtype
: TKwtype is writable, noinit
2740 # The expression to check
2741 var n_expr
: AExpr is writable, noinit
2744 var n_type
: AType is writable, noinit
2747 # A list of expression separated with commas (arguments for instance)
2748 abstract class AExprs
2751 # The list of expressions
2752 var n_exprs
= new ANodes[AExpr](self)
2755 # A simple list of expressions
2760 # A list of expressions enclosed in parentheses
2764 # The opening parenthesis
2765 var n_opar
: TOpar is writable, noinit
2767 # The closing parenthesis
2768 var n_cpar
: TCpar is writable, noinit
2771 # A list of expressions enclosed in brackets
2775 # The opening bracket
2776 var n_obra
: TObra is writable, noinit
2778 # The closing bracket
2779 var n_cbra
: TCbra is writable, noinit
2782 # A complex assignment operator. (`+=` and `-=`)
2783 abstract class AAssignOp
2786 # The combined assignment operator
2787 var n_op
: Token is writable, noinit
2789 # The name of the operator without the `=` (eg '+')
2790 fun operator
: String is abstract
2793 # A `+=` assignment operation
2797 redef fun operator
do return "+"
2800 # A `-=` assignment operation
2801 class AMinusAssignOp
2804 redef fun operator
do return "-"
2807 # A `*=` assignment operation
2811 redef fun operator
do return "*"
2814 # A `/=` assignment operation
2815 class ASlashAssignOp
2818 redef fun operator
do return "/"
2821 # A `%=` assignment operation
2822 class APercentAssignOp
2825 redef fun operator
do return "%"
2828 # A `**=` assignment operation
2829 class AStarstarAssignOp
2832 redef fun operator
do return "**"
2835 # A `|=` assignment operation
2839 redef fun operator
do return "|"
2842 # A `^=` assignment operation
2843 class ACaretAssignOp
2846 redef fun operator
do return "^"
2849 # A `&=` assignment operation
2853 redef fun operator
do return "&"
2856 # A `<<=` assignment operation
2860 redef fun operator
do return "<<"
2863 # A `>>=` assignment operation
2867 redef fun operator
do return ">>"
2870 # A possibly fully-qualified module identifier
2874 # The starting quad (`::`)
2875 var n_quad
: nullable TQuad = null is writable
2877 # The list of quad-separated package/group identifiers
2878 var n_path
= new ANodes[TId](self)
2880 # The final module identifier
2881 var n_id
: TId is writable, noinit
2884 # A language declaration for an extern block
2889 var n_kwin
: TKwin is writable, noinit
2892 var n_string
: TString is writable, noinit
2895 # An full extern block
2896 class AExternCodeBlock
2899 # The language declration
2900 var n_in_language
: nullable AInLanguage = null is writable
2902 # The block of extern code
2903 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2906 # A possible full method qualifier.
2910 # The starting quad (`::`)
2911 var n_quad
: nullable TQuad = null is writable
2913 # The list of quad-separated package/group/module identifiers
2914 var n_id
= new ANodes[TId](self)
2916 # A class identifier
2917 var n_classid
: nullable TClassid = null is writable
2920 # A documentation of a definition
2921 # It contains the block of comments just above the declaration
2925 # A list of lines of comment
2926 var n_comment
= new ANodes[TComment](self)
2929 # A group of annotation on a node
2931 # This same class is used for the 3 kind of annotations:
2933 # * *is* annotations. eg `module foo is bar`.
2934 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2935 # * *class* annotations, defined in classes.
2939 # The `is` keyword, for *is* annotations
2940 var n_kwis
: nullable TKwis = null is writable
2942 # The `@` symbol, for *at* annotations
2943 var n_at
: nullable TAt = null is writable
2945 # The opening parenthesis in *at* annotations
2946 var n_opar
: nullable TOpar = null is writable
2948 # The list of annotations
2949 var n_items
= new ANodes[AAnnotation](self)
2951 # The closing parenthesis in *at* annotations
2952 var n_cpar
: nullable TCpar = null is writable
2954 # The `end` keyword, for *is* annotations
2955 var n_kwend
: nullable TKwend = null is writable
2958 # A single annotation
2962 # The name of the annotation
2963 var n_atid
: AAtid is writable, noinit
2965 # The opening parenthesis of the arguments
2966 var n_opar
: nullable TOpar = null is writable
2968 # The list of arguments
2969 var n_args
= new ANodes[AExpr](self)
2971 # The closing parenthesis
2972 var n_cpar
: nullable TCpar = null is writable
2974 # The name of the annotation
2977 return n_atid
.n_id
.text
2981 # An annotation name
2982 abstract class AAtid
2985 # The identifier of the annotation.
2986 # Can be a TId of a keyword
2987 var n_id
: Token is writable, noinit
2990 # An annotation name based on an identifier
2995 # An annotation name based on the keyword `extern`
3000 # An annotation name based on the keyword `import`
3005 # An annotation name based on the keyword `abstract`
3006 class AKwabstractAtid
3010 # The root of the AST
3015 var n_base
: nullable AModule is writable
3017 # The end of file (or error) token
3018 var n_eof
: EOF is writable