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 # Write the subtree on stdout.
43 d
.write_to
(sys
.stdout
)
46 # Parent of the node in the AST
47 var parent
: nullable ANode = null
49 # The topmost ancestor of the element
50 # This just apply `parent` until the first one
56 if p
== null then return res
61 # The most specific common parent between `self` and `other`
62 # Return null if the two node are unrelated (distinct root)
63 fun common_parent
(other
: ANode): nullable ANode
65 # First, get the same depth
66 var s
: nullable ANode = self
67 var o
: nullable ANode = other
68 var d
= s
.depth
- o
.depth
77 assert o
.depth
== s
.depth
78 # Second, go up until same in found
86 # Number of nodes between `self` and the `root` of the AST
87 # ENSURE `self == self.root implies result == 0 `
88 # ENSURE `self != self.root implies result == self.parent.depth + 1`
95 if p
== null then return res
101 # Replace a child with an other node in the AST
102 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode) is abstract
104 # Detach a node from its parent
105 # Aborts if the node is not detachable. use `replace_with` instead
106 # REQUIRE: parent != null
107 # REQUIRE: is_detachable
108 # ENDURE: parent == null
111 assert parent
!= null
112 parent
.replace_child
(self, null)
116 # Replace itself with an other node in the AST
117 # REQUIRE: parent != null
118 # ENSURE: node.parent == old(parent)
119 # ENSURE: parent == null
120 fun replace_with
(node
: ANode)
122 assert parent
!= null
123 parent
.replace_child
(self, node
)
127 # Visit all nodes in order.
128 # Thus, call `v.enter_visit(e)` for each child `e`
129 fun visit_all
(v
: Visitor) is abstract
131 # Do a deep search and return an array of tokens that match a given text
132 fun collect_tokens_by_text
(text
: String): Array[Token]
134 var v
= new CollectTokensByTextVisitor(text
)
139 # Do a deep search and return an array of node that are annotated
140 # The attached node can be retrieved by two invocations of parent
141 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
143 var v
= new CollectAnnotationsByNameVisitor(name
)
149 private class CollectTokensByTextVisitor
152 var result
= new Array[Token]
153 redef fun visit
(node
)
156 if node
isa Token and node
.text
== text
then result
.add
(node
)
160 private class CollectAnnotationsByNameVisitor
163 var result
= new Array[AAnnotation]
164 redef fun visit
(node
)
167 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)
171 # A helper class to handle (print) Nit AST as an OrderedTree
174 super OrderedTree[ANode]
176 # Reference to the last parent in the Ordered Tree
177 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
178 private var last_parent
: nullable ANode = null
192 return "{n.class_name} \"{n.text.escape_to_c}\
" @{n.location}"
194 return "{n.class_name} @{n.location}"
199 # A sequence of nodes
200 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
201 class ANodes[E
: ANode]
203 private var parent
: ANode
204 private var items
= new Array[E
]
205 redef fun iterator
do return items
.iterator
206 redef fun length
do return items
.length
207 redef fun is_empty
do return items
.is_empty
226 var res
= items
.shift
238 redef fun []=(index
, e
)
240 hook_remove
(self[index
])
244 redef fun remove_at
(index
)
246 hook_remove
(items
[index
])
247 items
.remove_at
(index
)
249 private fun hook_add
(e
: E
)
251 #assert e.parent == null
254 private fun hook_remove
(e
: E
)
256 assert e
.parent
== parent
260 # Used in parent constructor to fill elements
261 private fun unsafe_add_all
(nodes
: Collection[Object])
263 var parent
= self.parent
271 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
273 var parent
= self.parent
274 for i
in [0..length
[ do
275 if self[i
] == old_child
then
276 if new_child
!= null then
277 assert new_child
isa E
279 new_child
.parent
= parent
289 private fun visit_all
(v
: Visitor)
291 for n
in self do v
.enter_visit
(n
)
295 # Ancestor of all tokens
296 # A token is a node that has a `text` but no children.
300 # The raw content on the token
301 fun text
: String is abstract
303 # The raw content on the token
304 fun text
=(text
: String) is abstract
306 # The previous token in the Lexer.
307 # May have disappeared in the AST
308 var prev_token
: nullable Token = null
310 # The next token in the Lexer.
311 # May have disappeared in the AST
312 var next_token
: nullable Token = null
314 # Is `self` a token discarded from the AST?
316 # Loose tokens are not present in the AST.
317 # It means they were identified by the lexer but were discarded by the parser.
318 # It also means that they are not visited or manipulated by AST-related functions.
320 # Each loose token is attached to the non-loose token that precedes or follows it.
321 # The rules are the following:
323 # * tokens that follow a non-loose token on a same line are attached to it.
325 # * other tokens, thus that precede a non-loose token on the same line or the next one,
326 # are attached to this one. See `prev_looses`.
328 # Loose tokens are mostly end of lines (`TEol`) and comments (`TComment`).
329 # Whitespace are ignored by the lexer, so they are not even considered as loose tokens.
330 # See `blank_before` to get the whitespace that separate tokens.
333 # Loose tokens that precede `self`.
335 # These tokens start the line or belong to a line with only loose tokens.
336 var prev_looses
= new Array[Token] is lazy
338 # Loose tokens that follow `self`
340 # These tokens are on the same line than `self`.
341 var next_looses
= new Array[Token] is lazy
343 # The verbatim blank text between `prev_token` and `self`
344 fun blank_before
: String
346 if prev_token
== null then return ""
347 var from
= prev_token
.location
.pend
+1
348 var to
= location
.pstart
349 return location
.file
.string
.substring
(from
,to-from
)
352 redef fun to_s
: String do
356 redef fun visit_all
(v
: Visitor) do end
357 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
360 redef class SourceFile
361 # The first token parser by the lexer
362 # May have disappeared in the final AST
363 var first_token
: nullable Token = null
365 # The first token parser by the lexer
366 # May have disappeared in the final AST
367 var last_token
: nullable Token = null
370 # Ancestor of all productions
371 # A production is a node without text but that usually has children.
375 # All the annotations attached directly to the node
376 var n_annotations
: nullable AAnnotations = null is writable
378 # Return all its annotations of a given name in the order of their declaration
379 # Retun an empty array if no such an annotation.
380 fun get_annotations
(name
: String): Array[AAnnotation]
382 var res
= new Array[AAnnotation]
383 var nas
= n_annotations
384 if nas
!= null then for na
in nas
.n_items
do
385 if na
.name
!= name
then continue
388 if self isa AClassdef then for na
in n_propdefs
do
389 if na
isa AAnnotPropdef then
390 if na
.name
!= name
then continue
398 redef fun replace_with
(n
: ANode)
402 if not isset n
._location
and isset _location
then n
._location
= _location
406 # Abstract standard visitor on the AST
407 abstract class Visitor
408 # What the visitor do when a node is visited
409 # Concrete visitors should implement this method.
411 protected fun visit
(e
: ANode) is abstract
413 # Ask the visitor to visit a given node.
414 # Usually automatically called by visit_all* methods.
415 # This method should not be redefined
416 fun enter_visit
(e
: nullable ANode)
418 if e
== null then return
419 var old
= _current_node
425 # The current visited node
426 var current_node
: nullable ANode = null is writable
429 # Token of end of line (basically `\n`)
438 # Token of a line of comments
439 # Starts with the `#` and contains the final end-of-line (if any)
444 # A token associated with a keyword
445 abstract class TokenKeyword
449 return "keyword '{text}'"
453 # The deprecated keyword `package`.
458 # The keyword `module`
463 # The keyword `import`
468 # The keyword `class`
473 # The keyword `abstract`
478 # The keyword `interface`
483 # The keywords `enum` ane `universal`
508 # The keyword `redef`
528 # The keyword `extern`
533 # The keyword `public`
538 # The keyword `protected`
543 # The keyword `private`
548 # The keyword `intrude`
568 # The keyword `while`
598 # The keyword `implies`
608 # The keyword `return`
613 # The keyword `continue`
618 # The keyword `break`
623 # The keyword `abort`
628 # The keyword `assert`
648 # The keyword `super`
663 # The keyword `false`
678 # The keyword `nullable`
683 # The keyword `isset`
688 # The keyword `label`
698 # The special keyword `__DEBUG__`
743 # A token associated with an operator (and other lookalike symbols)
744 abstract class TokenOperator
748 return "operator '{text}'"
932 # A class (or formal type) identifier. They start with an uppercase.
937 do return "type identifier '{text}'"
941 # A standard identifier (variable, method...). They start with a lowercase.
946 do return "identifier '{text}'"
950 # An attribute identifier. They start with an underscore.
955 do return "attribute '{text}'"
959 # A token of a literal value (string, integer, etc).
960 abstract class TokenLiteral
964 do return "literal value '{text}'"
973 # A literal floating point number
978 # A literal character
988 # The starting part of a super string (between `"` and `{`)
993 # The middle part of a super string (between `}` and `{`)
998 # The final part of a super string (between `}` and `"`)
1003 # A malformed string
1008 do return "malformed string {text}"
1017 do return "malformed character {text}"
1021 # A extern code block
1022 class TExternCodeSegment
1031 return "end of file"
1035 # A mark of an error
1039 # A lexical error (unexpected character)
1043 # A syntactic error (unexpected token)
1048 # The main node of a Nit source-file
1052 # The declaration part of the module
1053 var n_moduledecl
: nullable AModuledecl = null is writable
1055 # List of importation clauses
1056 var n_imports
= new ANodes[AImport](self)
1058 # List of extern blocks
1059 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1061 # List of class definition (including top-level methods and the main)
1062 var n_classdefs
= new ANodes[AClassdef](self)
1065 # Abstract class for definition of entities
1066 abstract class ADefinition
1069 var n_doc
: nullable ADoc = null is writable
1071 # The `redef` keyword
1072 var n_kwredef
: nullable TKwredef = null is writable
1074 # The declared visibility
1075 var n_visibility
: nullable AVisibility = null is writable
1078 # The declaration of the module with the documentation, name, and annotations
1082 # The `module` keyword
1083 var n_kwmodule
: TKwmodule is writable, noinit
1085 # The declared module name
1086 var n_name
: AModuleName is writable, noinit
1089 # A import clause of a module
1090 abstract class AImport
1093 # The declared visibility
1094 var n_visibility
: AVisibility is writable, noinit
1096 # The `import` keyword
1097 var n_kwimport
: TKwimport is writable, noinit
1100 # A standard import clause. eg `import x`
1103 # The imported module name
1104 var n_name
: AModuleName is writable, noinit
1107 # The special import clause of the kernel module. eg `import end`
1110 # The `end` keyword, that indicate the root module
1111 var n_kwend
: TKwend is writable, noinit
1114 # A visibility modifier
1116 # The public visibility is an empty production (no keyword).
1118 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1119 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1120 abstract class AVisibility
1124 # An implicit or explicit public visibility modifier
1125 class APublicVisibility
1127 # The `public` keyword, if any
1128 var n_kwpublic
: nullable TKwpublic is writable
1130 # An explicit private visibility modifier
1131 class APrivateVisibility
1133 # The `private` keyword
1134 var n_kwprivate
: TKwprivate is writable, noinit
1136 # An explicit protected visibility modifier
1137 class AProtectedVisibility
1139 # The `protected` keyword
1140 var n_kwprotected
: TKwprotected is writable, noinit
1142 # An explicit intrude visibility modifier
1143 class AIntrudeVisibility
1145 # The `intrude` keyword
1146 var n_kwintrude
: TKwintrude is writable, noinit
1149 # A class definition
1150 # While most definition are `AStdClassdef`
1151 # There is tow special case of class definition
1152 abstract class AClassdef
1154 # All the declared properties (including the main method)
1155 var n_propdefs
= new ANodes[APropdef](self)
1158 # A standard class definition with a name, superclasses and properties
1163 # The class kind (interface, abstract class, etc.)
1164 var n_classkind
: AClasskind is writable, noinit
1166 # The name of the class
1167 var n_id
: nullable TClassid = null is writable
1170 var n_obra
: nullable TObra = null is writable
1172 # The list of formal parameter types
1173 var n_formaldefs
= new ANodes[AFormaldef](self)
1176 var n_cbra
: nullable TCbra = null is writable
1178 # The extern block code
1179 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1182 var n_kwend
: TKwend is writable, noinit
1184 fun n_superclasses
: Array[ASuperPropdef] do
1185 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1188 redef fun hot_location
do return n_id
.location
1191 # The implicit class definition of the implicit main method
1196 # The implicit class definition of the top-level methods
1201 # The modifier for the kind of class (abstract, interface, etc.)
1202 abstract class AClasskind
1206 # A default, or concrete class modifier (just `class`)
1207 class AConcreteClasskind
1210 # The `class` keyword.
1211 var n_kwclass
: TKwclass is writable, noinit
1214 # An abstract class modifier (`abstract class`)
1215 class AAbstractClasskind
1218 # The `abstract` keyword.
1219 var n_kwabstract
: TKwabstract is writable, noinit
1221 # The `class` keyword.
1222 var n_kwclass
: TKwclass is writable, noinit
1225 # An interface class modifier (`interface`)
1226 class AInterfaceClasskind
1229 # The `interface` keyword.
1230 var n_kwinterface
: TKwinterface is writable, noinit
1233 # An enum/universal class modifier (`enum class`)
1234 class AEnumClasskind
1237 # The `enum` keyword.
1238 var n_kwenum
: TKwenum is writable, noinit
1241 # An extern class modifier (`extern class`)
1242 class AExternClasskind
1245 # The `extern` keyword.
1246 var n_kwextern
: TKwextern is writable, noinit
1248 # The `class` keyword.
1249 var n_kwclass
: nullable TKwclass = null is writable
1252 # The definition of a formal generic parameter type. eg `X: Y`
1256 # The name of the parameter type
1257 var n_id
: TClassid is writable, noinit
1259 # The bound of the parameter type
1260 var n_type
: nullable AType = null is writable
1263 # The definition of a property
1264 abstract class APropdef
1268 # A definition of an attribute
1269 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1274 var n_kwvar
: TKwvar is writable, noinit
1276 # The identifier for a new-style attribute
1277 var n_id2
: TId is writable, noinit
1279 # The declared type of the attribute
1280 var n_type
: nullable AType = null is writable
1283 var n_assign
: nullable TAssign = null is writable
1285 # The initial value, if any (set with `=`)
1286 var n_expr
: nullable AExpr = null is writable
1289 var n_kwdo
: nullable TKwdo = null is writable
1291 # The initial value, if any (set with `do return`)
1292 var n_block
: nullable AExpr = null is writable
1295 var n_kwend
: nullable TKwend = null is writable
1297 redef fun hot_location
1299 return n_id2
.location
1303 # A definition of all kind of method (including constructors)
1307 # The `fun` keyword, if any
1308 var n_kwmeth
: nullable TKwmeth = null is writable
1310 # The `init` keyword, if any
1311 var n_kwinit
: nullable TKwinit = null is writable
1313 # The `new` keyword, if any
1314 var n_kwnew
: nullable TKwnew = null is writable
1316 # The name of the method, if any
1317 var n_methid
: nullable AMethid = null is writable
1319 # The signature of the method, if any
1320 var n_signature
: nullable ASignature = null is writable
1323 var n_kwdo
: nullable TKwdo = null is writable
1325 # The body (in Nit) of the method, if any
1326 var n_block
: nullable AExpr = null is writable
1329 var n_kwend
: nullable TKwend = null is writable
1331 # The list of declared callbacks (for extern methods)
1332 var n_extern_calls
: nullable AExternCalls = null is writable
1334 # The body (in extern code) of the method, if any
1335 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1337 redef fun hot_location
1339 if n_methid
!= null then
1340 return n_methid
.location
1341 else if n_kwinit
!= null then
1342 return n_kwinit
.location
1343 else if n_kwnew
!= null then
1344 return n_kwnew
.location
1351 # The implicit main method
1352 class AMainMethPropdef
1361 # A super-class. eg `super X`
1366 var n_kwsuper
: TKwsuper is writable, noinit
1368 # The super-class (indicated as a type)
1369 var n_type
: AType is writable, noinit
1373 # Declaration of callbacks for extern methods
1377 # The `import` keyword
1378 var n_kwimport
: TKwimport is writable, noinit
1380 # The list of declared callbacks
1381 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1384 # A single callback declaration
1385 abstract class AExternCall
1389 # A single callback declaration on a method
1390 abstract class APropExternCall
1394 # A single callback declaration on a method on the current receiver
1395 class ALocalPropExternCall
1396 super APropExternCall
1398 # The name of the called-back method
1399 var n_methid
: AMethid is writable, noinit
1402 # A single callback declaration on a method on an explicit receiver type.
1403 class AFullPropExternCall
1404 super APropExternCall
1406 # The type of the receiver of the called-back method
1407 var n_type
: AType is writable, noinit
1410 var n_dot
: nullable TDot = null is writable
1412 # The name of the called-back method
1413 var n_methid
: AMethid is writable, noinit
1416 # A single callback declaration on a method on a constructor
1417 class AInitPropExternCall
1418 super APropExternCall
1420 # The allocated type
1421 var n_type
: AType is writable, noinit
1424 # A single callback declaration on a `super` call
1425 class ASuperExternCall
1428 # The `super` keyword
1429 var n_kwsuper
: TKwsuper is writable, noinit
1432 # A single callback declaration on a cast
1433 abstract class ACastExternCall
1437 # A single callback declaration on a cast to a given type
1438 class ACastAsExternCall
1439 super ACastExternCall
1441 # The origin type of the cast
1442 var n_from_type
: AType is writable, noinit
1445 var n_dot
: nullable TDot = null is writable
1448 var n_kwas
: TKwas is writable, noinit
1450 # The destination of the cast
1451 var n_to_type
: AType is writable, noinit
1454 # A single callback declaration on a cast to a nullable type
1455 class AAsNullableExternCall
1456 super ACastExternCall
1458 # The origin type to cast as nullable
1459 var n_type
: AType is writable, noinit
1462 var n_kwas
: TKwas is writable, noinit
1464 # The `nullable` keyword
1465 var n_kwnullable
: TKwnullable is writable, noinit
1468 # A single callback declaration on a cast to a non-nullable type
1469 class AAsNotNullableExternCall
1470 super ACastExternCall
1472 # The destination type on a cast to not nullable
1473 var n_type
: AType is writable, noinit
1476 var n_kwas
: TKwas is writable, noinit
1479 var n_kwnot
: TKwnot is writable, noinit
1481 # The `nullable` keyword
1482 var n_kwnullable
: TKwnullable is writable, noinit
1485 # A definition of a virtual type
1489 # The `type` keyword
1490 var n_kwtype
: TKwtype is writable, noinit
1492 # The name of the virtual type
1493 var n_id
: TClassid is writable, noinit
1495 # The bound of the virtual type
1496 var n_type
: AType is writable, noinit
1499 # The identifier of a method in a method declaration.
1500 # There is a specific class because of operator and setters.
1501 abstract class AMethid
1505 # A method name with a simple identifier
1509 # The simple identifier
1510 var n_id
: TId is writable, noinit
1513 # A method name for an operator
1514 class AOperatorMethid
1517 # The associated operator symbol
1518 var n_op
: Token is writable, noinit
1522 super AOperatorMethid
1527 super AOperatorMethid
1532 super AOperatorMethid
1535 # A method name `**`
1536 class AStarstarMethid
1537 super AOperatorMethid
1542 super AOperatorMethid
1546 class APercentMethid
1547 super AOperatorMethid
1552 super AOperatorMethid
1557 super AOperatorMethid
1562 super AOperatorMethid
1567 super AOperatorMethid
1570 # A method name `==`
1572 super AOperatorMethid
1575 # A method name `!=`
1577 super AOperatorMethid
1580 # A method name `<=`
1582 super AOperatorMethid
1585 # A method name `>=`
1587 super AOperatorMethid
1592 super AOperatorMethid
1597 super AOperatorMethid
1600 # A method name `<<`
1602 super AOperatorMethid
1605 # A method name `>>`
1607 super AOperatorMethid
1610 # A method name `<=>`
1611 class AStarshipMethid
1612 super AOperatorMethid
1615 # A method name `[]`
1620 var n_obra
: TObra is writable, noinit
1623 var n_cbra
: TCbra is writable, noinit
1626 # A setter method name with a simple identifier (with a `=`)
1630 # The base identifier
1631 var n_id
: TId is writable, noinit
1634 var n_assign
: TAssign is writable, noinit
1637 # A method name `[]=`
1638 class ABraassignMethid
1642 var n_obra
: TObra is writable, noinit
1645 var n_cbra
: TCbra is writable, noinit
1648 var n_assign
: TAssign is writable, noinit
1651 # A potentially qualified simple identifier `foo::bar::baz`
1654 # The qualifier, if any
1655 var n_qualified
: nullable AQualified = null is writable
1657 # The final identifier
1658 var n_id
: TId is writable, noinit
1661 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1666 var n_opar
: nullable TOpar = null is writable
1668 # The list of parameters
1669 var n_params
= new ANodes[AParam](self)
1672 var n_cpar
: nullable TCpar = null is writable
1675 var n_type
: nullable AType = null is writable
1678 # A parameter definition in a signature. eg `x:X`
1682 # The name of the parameter
1683 var n_id
: TId is writable, noinit
1685 # The type of the parameter, if any
1686 var n_type
: nullable AType = null is writable
1688 # The `...` symbol to indicate varargs
1689 var n_dotdotdot
: nullable TDotdotdot = null is writable
1692 # A static type. eg `nullable X[Y]`
1695 # The `nullable` keyword
1696 var n_kwnullable
: nullable TKwnullable = null is writable
1698 # The name of the class or of the formal type
1699 var n_id
: TClassid is writable, noinit
1701 # The opening bracket
1702 var n_obra
: nullable TObra = null is writable
1704 # Type arguments for a generic type
1705 var n_types
= new ANodes[AType](self)
1707 # The closing bracket
1708 var n_cbra
: nullable TCbra = null is writable
1711 # A label at the end of a block or in a break/continue statement. eg `label x`
1715 # The `label` keyword
1716 var n_kwlabel
: TKwlabel is writable, noinit
1718 # The name of the label, if any
1719 var n_id
: nullable TId is writable, noinit
1722 # Expression and statements
1723 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1724 abstract class AExpr
1728 # A sequence of `AExpr` (usually statements)
1729 # The last `AExpr` gives the value of the whole block
1733 # The list of statements in the bloc.
1734 # The last element is often considered as an expression that give the value of the whole block.
1735 var n_expr
= new ANodes[AExpr](self)
1738 var n_kwend
: nullable TKwend = null is writable
1741 # A declaration of a local variable. eg `var x: X = y`
1746 var n_kwvar
: nullable TKwvar = null is writable
1748 # The name of the local variable
1749 var n_id
: TId is writable, noinit
1751 # The declaration type of the local variable
1752 var n_type
: nullable AType = null is writable
1754 # The `=` symbol (for the initial value)
1755 var n_assign
: nullable TAssign = null is writable
1757 # The initial value, if any
1758 var n_expr
: nullable AExpr = null is writable
1761 # A `return` statement. eg `return x`
1765 # The `return` keyword
1766 var n_kwreturn
: nullable TKwreturn = null is writable
1768 # The return value, if any
1769 var n_expr
: nullable AExpr = null is writable
1772 # Something that has a label.
1773 abstract class ALabelable
1776 # The associated label declatation
1777 var n_label
: nullable ALabel = null is writable
1780 # A `break` or a `continue`
1781 abstract class AEscapeExpr
1785 # The return value, if nay (unused currently)
1786 var n_expr
: nullable AExpr = null is writable
1789 # A `break` statement.
1793 # The `break` keyword
1794 var n_kwbreak
: TKwbreak is writable, noinit
1797 # An `abort` statement
1801 # The `abort` keyword
1802 var n_kwabort
: TKwabort is writable, noinit
1805 # A `continue` statement
1809 # The `continue` keyword.
1810 var n_kwcontinue
: nullable TKwcontinue = null is writable
1819 var n_kwdo
: TKwdo is writable, noinit
1821 # The list of statements of the `do`.
1822 var n_block
: nullable AExpr = null is writable
1830 var n_kwif
: TKwif is writable, noinit
1832 # The expression used as the condition of the `if`
1833 var n_expr
: AExpr is writable, noinit
1835 # The `then` keyword
1836 var n_kwthen
: TKwthen is writable, noinit
1838 # The body of the `then` part
1839 var n_then
: nullable AExpr = null is writable
1841 # The `else` keyword
1842 var n_kwelse
: nullable TKwelse = null is writable
1844 # The body of the `else` part
1845 var n_else
: nullable AExpr = null is writable
1848 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1853 var n_kwif
: TKwif is writable, noinit
1855 # The expression used as the condition of the `if`
1856 var n_expr
: AExpr is writable, noinit
1858 # The `then` keyword
1859 var n_kwthen
: TKwthen is writable, noinit
1861 # The expression in the `then` part
1862 var n_then
: AExpr is writable, noinit
1864 # The `else` keyword
1865 var n_kwelse
: TKwelse is writable, noinit
1867 # The expression in the `else` part
1868 var n_else
: AExpr is writable, noinit
1871 # A `while` statement
1876 # The `while` keyword
1877 var n_kwwhile
: TKwwhile is writable, noinit
1879 # The expression used as the condition of the `while`
1880 var n_expr
: AExpr is writable, noinit
1883 var n_kwdo
: TKwdo is writable, noinit
1885 # The body of the loop
1886 var n_block
: nullable AExpr = null is writable
1889 # A `loop` statement
1894 # The `loop` keyword
1895 var n_kwloop
: TKwloop is writable, noinit
1897 # The body of the loop
1898 var n_block
: nullable AExpr = null is writable
1907 var n_kwfor
: TKwfor is writable, noinit
1909 # The list of name of the automatic variables
1910 var n_ids
= new ANodes[TId](self)
1913 var n_kwin
: TKwin is writable, noinit
1915 # The expression used as the collection to iterate on
1916 var n_expr
: AExpr is writable, noinit
1919 var n_kwdo
: TKwdo is writable, noinit
1921 # The body of the loop
1922 var n_block
: nullable AExpr = null is writable
1925 # A `with` statement
1930 # The `with` keyword
1931 var n_kwwith
: TKwwith is writable, noinit
1933 # The expression used to get the value to control
1934 var n_expr
: AExpr is writable, noinit
1937 var n_kwdo
: TKwdo is writable, noinit
1939 # The body of the loop
1940 var n_block
: nullable AExpr = null is writable
1943 # An `assert` statement
1947 # The `assert` keyword
1948 var n_kwassert
: TKwassert is writable, noinit
1950 # The name of the assert, if any
1951 var n_id
: nullable TId = null is writable
1953 # The expression used as the condition of the `assert`
1954 var n_expr
: AExpr is writable, noinit
1956 # The `else` keyword
1957 var n_kwelse
: nullable TKwelse = null is writable
1959 # The body to execute when the assert fails
1960 var n_else
: nullable AExpr = null is writable
1963 # Whatever is a simple assignment. eg `= something`
1964 abstract class AAssignFormExpr
1968 var n_assign
: TAssign is writable, noinit
1970 # The right-value to assign.
1971 var n_value
: AExpr is writable, noinit
1974 # Whatever is a combined assignment. eg `+= something`
1975 abstract class AReassignFormExpr
1978 # The combined operator (eg. `+=`)
1979 var n_assign_op
: AAssignOp is writable, noinit
1981 # The right-value to apply on the combined operator.
1982 var n_value
: AExpr is writable, noinit
1985 # A `once` expression. eg `once x`
1989 # The `once` keyword
1990 var n_kwonce
: TKwonce is writable, noinit
1992 # The expression to evaluate only one time
1993 var n_expr
: AExpr is writable, noinit
1996 # A polymorphic invocation of a method
1997 # The form of the invocation (name, arguments, etc.) are specific
1998 abstract class ASendExpr
2000 # The receiver of the method invocation
2001 var n_expr
: AExpr is writable, noinit
2004 # A binary operation on a method
2005 abstract class ABinopExpr
2009 var n_op
: Token is writable, noinit
2011 # The second operand of the operation
2012 # Note: the receiver (`n_expr`) is the first operand
2013 var n_expr2
: AExpr is writable, noinit
2015 # The name of the operator (eg '+')
2016 fun operator
: String is abstract
2019 # Something that is boolean expression
2020 abstract class ABoolExpr
2024 # Something that is binary boolean expression
2025 abstract class ABinBoolExpr
2028 # The first boolean operand
2029 var n_expr
: AExpr is writable, noinit
2032 var n_op
: Token is writable, noinit
2034 # The second boolean operand
2035 var n_expr2
: AExpr is writable, noinit
2043 # A `and` expression
2048 # A `or else` expression
2052 # The `else` keyword
2053 var n_kwelse
: TKwelse is writable, noinit
2056 # A `implies` expression
2061 # A `not` expression
2066 var n_kwnot
: TKwnot is writable, noinit
2068 # The boolean operand of the `not`
2069 var n_expr
: AExpr is writable, noinit
2072 # A `==` or a `!=` expression
2074 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2082 redef fun operator
do return "=="
2088 redef fun operator
do return "!="
2094 redef fun operator
do return "<"
2100 redef fun operator
do return "<="
2106 redef fun operator
do return "<<"
2112 redef fun operator
do return ">"
2118 redef fun operator
do return ">="
2124 redef fun operator
do return ">>"
2127 # A type-ckeck expression. eg `x isa T`
2131 # The expression to check
2132 var n_expr
: AExpr is writable, noinit
2135 var n_kwisa
: TKwisa is writable, noinit
2137 # The destination type to check to
2138 var n_type
: AType is writable, noinit
2144 redef fun operator
do return "+"
2150 redef fun operator
do return "-"
2153 # A `<=>` expression
2156 redef fun operator
do return "<=>"
2162 redef fun operator
do return "*"
2168 redef fun operator
do return "**"
2174 redef fun operator
do return "/"
2180 redef fun operator
do return "%"
2186 redef fun operator
do return "|"
2192 redef fun operator
do return "^"
2198 redef fun operator
do return "&"
2201 # A unary operation on a method
2206 var n_op
: Token is writable, noinit
2208 # The name of the operator (eg '+')
2209 fun operator
: String is abstract
2212 # A unary minus expression. eg `-x`
2215 redef fun operator
do return "-"
2218 # A unary plus expression. eg `+x`
2221 redef fun operator
do return "+"
2224 # A unary `~` expression
2227 redef fun operator
do return "~"
2230 # An explicit instantiation. eg `new T`
2235 var n_kwnew
: TKwnew is writable, noinit
2237 # The `type` keyword
2238 var n_type
: AType is writable, noinit
2240 # The name of the named-constructor, if any
2241 var n_qid
: nullable AQid = null is writable
2243 # The arguments of the `new`
2244 var n_args
: AExprs is writable, noinit
2247 # Whatever is a old-style attribute access
2248 abstract class AAttrFormExpr
2251 # The receiver of the attribute
2252 var n_expr
: AExpr is writable, noinit
2254 # The name of the attribute
2255 var n_id
: TAttrid is writable, noinit
2259 # The read of an attribute. eg `x._a`
2264 # The assignment of an attribute. eg `x._a=y`
2265 class AAttrAssignExpr
2267 super AAssignFormExpr
2270 # Whatever looks-like a call with a standard method and any number of arguments.
2271 abstract class ACallFormExpr
2274 # The name of the method
2275 var n_qid
: AQid is writable, noinit
2277 # The arguments of the call
2278 var n_args
: AExprs is writable, noinit
2281 # A complex setter call (standard or brackets)
2282 abstract class ASendReassignFormExpr
2284 super AReassignFormExpr
2287 # A complex attribute assignment. eg `x._a+=y`
2288 class AAttrReassignExpr
2290 super AReassignFormExpr
2293 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2294 # 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`.
2295 # Semantic analysis have to transform them to instance of `AVarExpr`.
2300 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2301 # 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`.
2302 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2303 class ACallAssignExpr
2305 super AAssignFormExpr
2308 # 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.
2309 # 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`.
2310 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2311 class ACallReassignExpr
2313 super ASendReassignFormExpr
2316 # A call to `super`. OR a call of a super-constructor
2320 # The qualifier part before the super (currenlty unused)
2321 var n_qualified
: nullable AQualified = null is writable
2323 # The `super` keyword
2324 var n_kwsuper
: TKwsuper is writable, noinit
2326 # The arguments of the super
2327 var n_args
: AExprs is writable, noinit
2330 # A call to the `init` constructor.
2331 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2335 # The `init` keyword
2336 var n_kwinit
: TKwinit is writable, noinit
2338 # The arguments of the init
2339 var n_args
: AExprs is writable, noinit
2342 # Whatever looks-like a call of the brackets `[]` operator.
2343 abstract class ABraFormExpr
2346 # The arguments inside the brackets
2347 var n_args
: AExprs is writable, noinit
2350 # A call of the brackets operator. eg `x[y,z]`
2355 # A setter call of the bracket operator. eg `x[y,z]=t`
2356 class ABraAssignExpr
2358 super AAssignFormExpr
2361 # Whatever is an access to a local variable
2362 abstract class AVarFormExpr
2365 # The name of the attribute
2366 var n_id
: TId is writable, noinit
2369 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2370 class ABraReassignExpr
2372 super ASendReassignFormExpr
2375 # A local variable read access.
2376 # The parser cannot instantiate them, see `ACallExpr`.
2381 # A local variable simple assignment access
2382 # The parser cannot instantiate them, see `ACallAssignExpr`.
2383 class AVarAssignExpr
2385 super AAssignFormExpr
2388 # A local variable complex assignment access
2389 # The parser cannot instantiate them, see `ACallReassignExpr`.
2390 class AVarReassignExpr
2392 super AReassignFormExpr
2395 # A literal range, open or closed
2396 abstract class ARangeExpr
2399 # The left (lower) element of the range
2400 var n_expr
: AExpr is writable, noinit
2403 var n_dotdot
: TDotdot is writable, noinit
2405 # The right (upper) element of the range
2406 var n_expr2
: AExpr is writable, noinit
2409 # A closed literal range. eg `[x..y]`
2413 # The opening bracket `[`
2414 var n_obra
: TObra is writable, noinit
2416 # The closing bracket `]`
2417 var n_cbra
: TCbra is writable, noinit
2420 # An open literal range. eg `[x..y[`
2424 # The opening bracket `[`
2425 var n_obra
: TObra is writable, noinit
2427 # The closing bracket `[` (because open range)
2428 var n_cbra
: TObra is writable, noinit
2431 # A literal array. eg. `[x,y,z]`
2435 # The opening bracket `[`
2436 var n_obra
: TObra is writable, noinit
2438 # The elements of the array
2439 var n_exprs
= new ANodes[AExpr](self)
2441 # The type of the element of the array (if any)
2442 var n_type
: nullable AType = null is writable
2444 # The closing bracket `]`
2445 var n_cbra
: TCbra is writable, noinit
2452 # The `self` keyword
2453 var n_kwself
: nullable TKwself = null is writable
2456 # When there is no explicit receiver, `self` is implicit
2457 class AImplicitSelfExpr
2461 # A `true` boolean literal constant
2465 # The `true` keyword
2466 var n_kwtrue
: TKwtrue is writable, noinit
2469 # A `false` boolean literal constant
2473 # The `false` keyword
2474 var n_kwfalse
: TKwfalse is writable, noinit
2477 # A `null` literal constant
2481 # The `null` keyword
2482 var n_kwnull
: TKwnull is writable, noinit
2485 # An integer literal
2490 var n_integer
: TInteger is writable, noinit
2498 var n_float
: TFloat is writable, noinit
2501 # A character literal
2505 # The character token
2506 var n_char
: TChar is writable, noinit
2510 abstract class AStringFormExpr
2514 var n_string
: Token is writable, noinit
2517 # A simple string. eg. `"abc"`
2519 super AStringFormExpr
2522 # The start of a superstring. eg `"abc{`
2523 class AStartStringExpr
2524 super AStringFormExpr
2527 # The middle of a superstring. eg `}abc{`
2528 class AMidStringExpr
2529 super AStringFormExpr
2532 # The end of a superstrng. eg `}abc"`
2533 class AEndStringExpr
2534 super AStringFormExpr
2537 # A superstring literal. eg `"a{x}b{y}c"`
2538 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2539 class ASuperstringExpr
2542 # The list of the expressions of the superstring
2543 var n_exprs
= new ANodes[AExpr](self)
2546 # A simple parenthesis. eg `(x)`
2550 # The opening parenthesis
2551 var n_opar
: TOpar is writable, noinit
2553 # The inner expression
2554 var n_expr
: AExpr is writable, noinit
2556 # The closing parenthesis
2557 var n_cpar
: TCpar is writable, noinit
2560 # A cast, against a type or `not null`
2564 # The expression to cast
2565 var n_expr
: AExpr is writable, noinit
2568 var n_kwas
: TKwas is writable, noinit
2570 # The opening parenthesis
2571 var n_opar
: nullable TOpar = null is writable
2573 # The closing parenthesis
2574 var n_cpar
: nullable TCpar = null is writable
2577 # A type cast. eg `x.as(T)`
2581 # The target type to cast to
2582 var n_type
: AType is writable, noinit
2585 # A as-not-null cast. eg `x.as(not null)`
2586 class AAsNotnullExpr
2590 var n_kwnot
: TKwnot is writable, noinit
2592 # The `null` keyword
2593 var n_kwnull
: TKwnull is writable, noinit
2596 # A is-set check of old-style attributes. eg `isset x._a`
2597 class AIssetAttrExpr
2600 # The `isset` keyword
2601 var n_kwisset
: TKwisset is writable, noinit
2604 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2608 # The passed expression
2609 var n_expr
: AExpr is writable, noinit
2612 var n_dotdotdot
: TDotdotdot is writable, noinit
2615 # An named notation used to pass an expression by name in a parameter
2619 # The name of the argument
2620 var n_id
: TId is writable, noinit
2623 var n_assign
: TAssign is writable, noinit
2625 # The passed expression
2626 var n_expr
: AExpr is writable, noinit
2629 # A list of expression separated with commas (arguments for instance)
2633 # The list of expressions
2634 var n_exprs
= new ANodes[AExpr](self)
2637 # A special expression that encapsulates a static type
2638 # Can only be found in special construction like arguments of annotations.
2642 # The encapsulated type
2643 var n_type
: AType is writable, noinit
2646 # A special expression that encapsulates a method identifier
2647 # Can only be found in special construction like arguments of annotations.
2652 var n_expr
: AExpr is writable, noinit
2654 # The encapsulated method identifier
2655 var n_id
: AMethid is writable, noinit
2658 # A special expression that encapsulate an annotation
2659 # Can only be found in special construction like arguments of annotations.
2661 # The encapsulated annotations are in `n_annotations`
2666 # A special expression to debug types
2667 class ADebugTypeExpr
2670 # The `debug` keyword
2671 var n_kwdebug
: TKwdebug is writable, noinit
2673 # The `type` keyword
2674 var n_kwtype
: TKwtype is writable, noinit
2676 # The expression to check
2677 var n_expr
: AExpr is writable, noinit
2680 var n_type
: AType is writable, noinit
2683 # A list of expression separated with commas (arguments for instance)
2684 abstract class AExprs
2687 # The list of expressions
2688 var n_exprs
= new ANodes[AExpr](self)
2691 # A simple list of expressions
2696 # A list of expressions enclosed in parentheses
2700 # The opening parenthesis
2701 var n_opar
: TOpar is writable, noinit
2703 # The closing parenthesis
2704 var n_cpar
: TCpar is writable, noinit
2707 # A list of expressions enclosed in brackets
2711 # The opening bracket
2712 var n_obra
: TObra is writable, noinit
2714 # The closing bracket
2715 var n_cbra
: TCbra is writable, noinit
2718 # A complex assignment operator. (`+=` and `-=`)
2719 abstract class AAssignOp
2722 # The combined assignment operator
2723 var n_op
: Token is writable, noinit
2725 # The name of the operator without the `=` (eg '+')
2726 fun operator
: String is abstract
2729 # A `+=` assignment operation
2733 redef fun operator
do return "+"
2736 # A `-=` assignment operation
2737 class AMinusAssignOp
2740 redef fun operator
do return "-"
2743 # A `*=` assignment operation
2747 redef fun operator
do return "*"
2750 # A `/=` assignment operation
2751 class ASlashAssignOp
2754 redef fun operator
do return "/"
2757 # A `%=` assignment operation
2758 class APercentAssignOp
2761 redef fun operator
do return "%"
2764 # A `**=` assignment operation
2765 class AStarstarAssignOp
2768 redef fun operator
do return "**"
2771 # A `|=` assignment operation
2775 redef fun operator
do return "|"
2778 # A `^=` assignment operation
2779 class ACaretAssignOp
2782 redef fun operator
do return "^"
2785 # A `&=` assignment operation
2789 redef fun operator
do return "&"
2792 # A `<<=` assignment operation
2796 redef fun operator
do return "<<"
2799 # A `>>=` assignment operation
2803 redef fun operator
do return ">>"
2806 # A possibly fully-qualified module identifier
2810 # The starting quad (`::`)
2811 var n_quad
: nullable TQuad = null is writable
2813 # The list of quad-separated project/group identifiers
2814 var n_path
= new ANodes[TId](self)
2816 # The final module identifier
2817 var n_id
: TId is writable, noinit
2820 # A language declaration for an extern block
2825 var n_kwin
: TKwin is writable, noinit
2828 var n_string
: TString is writable, noinit
2831 # An full extern block
2832 class AExternCodeBlock
2835 # The language declration
2836 var n_in_language
: nullable AInLanguage = null is writable
2838 # The block of extern code
2839 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2842 # A possible full method qualifier.
2846 # The starting quad (`::`)
2847 var n_quad
: nullable TQuad = null is writable
2849 # The list of quad-separated project/group/module identifiers
2850 var n_id
= new ANodes[TId](self)
2852 # A class identifier
2853 var n_classid
: nullable TClassid = null is writable
2856 # A documentation of a definition
2857 # It contains the block of comments just above the declaration
2861 # A list of lines of comment
2862 var n_comment
= new ANodes[TComment](self)
2865 # A group of annotation on a node
2867 # This same class is used for the 3 kind of annotations:
2869 # * *is* annotations. eg `module foo is bar`.
2870 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2871 # * *class* annotations, defined in classes.
2875 # The `is` keyword, for *is* annotations
2876 var n_kwis
: nullable TKwis = null is writable
2878 # The `@` symbol, for *at* annotations
2879 var n_at
: nullable TAt = null is writable
2881 # The opening parenthesis in *at* annotations
2882 var n_opar
: nullable TOpar = null is writable
2884 # The list of annotations
2885 var n_items
= new ANodes[AAnnotation](self)
2887 # The closing parenthesis in *at* annotations
2888 var n_cpar
: nullable TCpar = null is writable
2890 # The `end` keyword, for *is* annotations
2891 var n_kwend
: nullable TKwend = null is writable
2894 # A single annotation
2898 # The name of the annotation
2899 var n_atid
: AAtid is writable, noinit
2901 # The opening parenthesis of the arguments
2902 var n_opar
: nullable TOpar = null is writable
2904 # The list of arguments
2905 var n_args
= new ANodes[AExpr](self)
2907 # The closing parenthesis
2908 var n_cpar
: nullable TCpar = null is writable
2910 # The name of the annotation
2913 return n_atid
.n_id
.text
2917 # An annotation name
2918 abstract class AAtid
2921 # The identifier of the annotation.
2922 # Can be a TId of a keyword
2923 var n_id
: Token is writable, noinit
2926 # An annotation name based on an identifier
2931 # An annotation name based on the keyword `extern`
2936 # An annotation name based on the keyword `import`
2941 # An annotation name based on the keyword `abstract`
2942 class AKwabstractAtid
2946 # The root of the AST
2951 var n_base
: nullable AModule is writable
2953 # The end of file (or error) token
2954 var n_eof
: EOF is writable