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}'"
968 # A literal decimal integer
973 # A literal hexadecimal integer
978 # A literal binary integer
983 # A literal octal integer
988 # A literal decimal byte
993 # A literal hexadecimal byte
998 # A literal binary byte
1003 # A literal octal byte
1008 # A literal floating point number
1013 # A literal character
1023 # The starting part of a super string (between `"` and `{`)
1028 # The middle part of a super string (between `}` and `{`)
1033 # The final part of a super string (between `}` and `"`)
1038 # A malformed string
1043 do return "malformed string {text}"
1052 do return "malformed character {text}"
1056 # A extern code block
1057 class TExternCodeSegment
1066 return "end of file"
1070 # A mark of an error
1074 # A lexical error (unexpected character)
1078 # A syntactic error (unexpected token)
1083 # The main node of a Nit source-file
1087 # The declaration part of the module
1088 var n_moduledecl
: nullable AModuledecl = null is writable
1090 # List of importation clauses
1091 var n_imports
= new ANodes[AImport](self)
1093 # List of extern blocks
1094 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1096 # List of class definition (including top-level methods and the main)
1097 var n_classdefs
= new ANodes[AClassdef](self)
1100 # Abstract class for definition of entities
1101 abstract class ADefinition
1104 var n_doc
: nullable ADoc = null is writable
1106 # The `redef` keyword
1107 var n_kwredef
: nullable TKwredef = null is writable
1109 # The declared visibility
1110 var n_visibility
: nullable AVisibility = null is writable
1113 # The declaration of the module with the documentation, name, and annotations
1117 # The `module` keyword
1118 var n_kwmodule
: TKwmodule is writable, noinit
1120 # The declared module name
1121 var n_name
: AModuleName is writable, noinit
1124 # A import clause of a module
1125 abstract class AImport
1128 # The declared visibility
1129 var n_visibility
: AVisibility is writable, noinit
1131 # The `import` keyword
1132 var n_kwimport
: TKwimport is writable, noinit
1135 # A standard import clause. eg `import x`
1138 # The imported module name
1139 var n_name
: AModuleName is writable, noinit
1142 # The special import clause of the kernel module. eg `import end`
1145 # The `end` keyword, that indicate the root module
1146 var n_kwend
: TKwend is writable, noinit
1149 # A visibility modifier
1151 # The public visibility is an empty production (no keyword).
1153 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1154 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1155 abstract class AVisibility
1159 # An implicit or explicit public visibility modifier
1160 class APublicVisibility
1162 # The `public` keyword, if any
1163 var n_kwpublic
: nullable TKwpublic is writable
1165 # An explicit private visibility modifier
1166 class APrivateVisibility
1168 # The `private` keyword
1169 var n_kwprivate
: TKwprivate is writable, noinit
1171 # An explicit protected visibility modifier
1172 class AProtectedVisibility
1174 # The `protected` keyword
1175 var n_kwprotected
: TKwprotected is writable, noinit
1177 # An explicit intrude visibility modifier
1178 class AIntrudeVisibility
1180 # The `intrude` keyword
1181 var n_kwintrude
: TKwintrude is writable, noinit
1184 # A class definition
1185 # While most definition are `AStdClassdef`
1186 # There is tow special case of class definition
1187 abstract class AClassdef
1189 # All the declared properties (including the main method)
1190 var n_propdefs
= new ANodes[APropdef](self)
1193 # A standard class definition with a name, superclasses and properties
1198 # The class kind (interface, abstract class, etc.)
1199 var n_classkind
: AClasskind is writable, noinit
1201 # The name of the class
1202 var n_id
: nullable TClassid = null is writable
1205 var n_obra
: nullable TObra = null is writable
1207 # The list of formal parameter types
1208 var n_formaldefs
= new ANodes[AFormaldef](self)
1211 var n_cbra
: nullable TCbra = null is writable
1213 # The extern block code
1214 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1217 var n_kwend
: TKwend is writable, noinit
1219 fun n_superclasses
: Array[ASuperPropdef] do
1220 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1223 redef fun hot_location
do return n_id
.location
1226 # The implicit class definition of the implicit main method
1231 # The implicit class definition of the top-level methods
1236 # The modifier for the kind of class (abstract, interface, etc.)
1237 abstract class AClasskind
1241 # A default, or concrete class modifier (just `class`)
1242 class AConcreteClasskind
1245 # The `class` keyword.
1246 var n_kwclass
: TKwclass is writable, noinit
1249 # An abstract class modifier (`abstract class`)
1250 class AAbstractClasskind
1253 # The `abstract` keyword.
1254 var n_kwabstract
: TKwabstract is writable, noinit
1256 # The `class` keyword.
1257 var n_kwclass
: TKwclass is writable, noinit
1260 # An interface class modifier (`interface`)
1261 class AInterfaceClasskind
1264 # The `interface` keyword.
1265 var n_kwinterface
: TKwinterface is writable, noinit
1268 # An enum/universal class modifier (`enum class`)
1269 class AEnumClasskind
1272 # The `enum` keyword.
1273 var n_kwenum
: TKwenum is writable, noinit
1276 # An extern class modifier (`extern class`)
1277 class AExternClasskind
1280 # The `extern` keyword.
1281 var n_kwextern
: TKwextern is writable, noinit
1283 # The `class` keyword.
1284 var n_kwclass
: nullable TKwclass = null is writable
1287 # The definition of a formal generic parameter type. eg `X: Y`
1291 # The name of the parameter type
1292 var n_id
: TClassid is writable, noinit
1294 # The bound of the parameter type
1295 var n_type
: nullable AType = null is writable
1298 # The definition of a property
1299 abstract class APropdef
1303 # A definition of an attribute
1304 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1309 var n_kwvar
: TKwvar is writable, noinit
1311 # The identifier for a new-style attribute
1312 var n_id2
: TId is writable, noinit
1314 # The declared type of the attribute
1315 var n_type
: nullable AType = null is writable
1318 var n_assign
: nullable TAssign = null is writable
1320 # The initial value, if any (set with `=`)
1321 var n_expr
: nullable AExpr = null is writable
1324 var n_kwdo
: nullable TKwdo = null is writable
1326 # The initial value, if any (set with `do return`)
1327 var n_block
: nullable AExpr = null is writable
1330 var n_kwend
: nullable TKwend = null is writable
1332 redef fun hot_location
1334 return n_id2
.location
1338 # A definition of all kind of method (including constructors)
1342 # The `fun` keyword, if any
1343 var n_kwmeth
: nullable TKwmeth = null is writable
1345 # The `init` keyword, if any
1346 var n_kwinit
: nullable TKwinit = null is writable
1348 # The `new` keyword, if any
1349 var n_kwnew
: nullable TKwnew = null is writable
1351 # The name of the method, if any
1352 var n_methid
: nullable AMethid = null is writable
1354 # The signature of the method, if any
1355 var n_signature
: nullable ASignature = null is writable
1358 var n_kwdo
: nullable TKwdo = null is writable
1360 # The body (in Nit) of the method, if any
1361 var n_block
: nullable AExpr = null is writable
1364 var n_kwend
: nullable TKwend = null is writable
1366 # The list of declared callbacks (for extern methods)
1367 var n_extern_calls
: nullable AExternCalls = null is writable
1369 # The body (in extern code) of the method, if any
1370 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1372 redef fun hot_location
1374 if n_methid
!= null then
1375 return n_methid
.location
1376 else if n_kwinit
!= null then
1377 return n_kwinit
.location
1378 else if n_kwnew
!= null then
1379 return n_kwnew
.location
1386 # The implicit main method
1387 class AMainMethPropdef
1396 # A super-class. eg `super X`
1401 var n_kwsuper
: TKwsuper is writable, noinit
1403 # The super-class (indicated as a type)
1404 var n_type
: AType is writable, noinit
1408 # Declaration of callbacks for extern methods
1412 # The `import` keyword
1413 var n_kwimport
: TKwimport is writable, noinit
1415 # The list of declared callbacks
1416 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1419 # A single callback declaration
1420 abstract class AExternCall
1424 # A single callback declaration on a method
1425 abstract class APropExternCall
1429 # A single callback declaration on a method on the current receiver
1430 class ALocalPropExternCall
1431 super APropExternCall
1433 # The name of the called-back method
1434 var n_methid
: AMethid is writable, noinit
1437 # A single callback declaration on a method on an explicit receiver type.
1438 class AFullPropExternCall
1439 super APropExternCall
1441 # The type of the receiver of the called-back method
1442 var n_type
: AType is writable, noinit
1445 var n_dot
: nullable TDot = null is writable
1447 # The name of the called-back method
1448 var n_methid
: AMethid is writable, noinit
1451 # A single callback declaration on a method on a constructor
1452 class AInitPropExternCall
1453 super APropExternCall
1455 # The allocated type
1456 var n_type
: AType is writable, noinit
1459 # A single callback declaration on a `super` call
1460 class ASuperExternCall
1463 # The `super` keyword
1464 var n_kwsuper
: TKwsuper is writable, noinit
1467 # A single callback declaration on a cast
1468 abstract class ACastExternCall
1472 # A single callback declaration on a cast to a given type
1473 class ACastAsExternCall
1474 super ACastExternCall
1476 # The origin type of the cast
1477 var n_from_type
: AType is writable, noinit
1480 var n_dot
: nullable TDot = null is writable
1483 var n_kwas
: TKwas is writable, noinit
1485 # The destination of the cast
1486 var n_to_type
: AType is writable, noinit
1489 # A single callback declaration on a cast to a nullable type
1490 class AAsNullableExternCall
1491 super ACastExternCall
1493 # The origin type to cast as nullable
1494 var n_type
: AType is writable, noinit
1497 var n_kwas
: TKwas is writable, noinit
1499 # The `nullable` keyword
1500 var n_kwnullable
: TKwnullable is writable, noinit
1503 # A single callback declaration on a cast to a non-nullable type
1504 class AAsNotNullableExternCall
1505 super ACastExternCall
1507 # The destination type on a cast to not nullable
1508 var n_type
: AType is writable, noinit
1511 var n_kwas
: TKwas is writable, noinit
1514 var n_kwnot
: TKwnot is writable, noinit
1516 # The `nullable` keyword
1517 var n_kwnullable
: TKwnullable is writable, noinit
1520 # A definition of a virtual type
1524 # The `type` keyword
1525 var n_kwtype
: TKwtype is writable, noinit
1527 # The name of the virtual type
1528 var n_id
: TClassid is writable, noinit
1530 # The bound of the virtual type
1531 var n_type
: AType is writable, noinit
1534 # The identifier of a method in a method declaration.
1535 # There is a specific class because of operator and setters.
1536 abstract class AMethid
1540 # A method name with a simple identifier
1544 # The simple identifier
1545 var n_id
: TId is writable, noinit
1548 # A method name for an operator
1549 class AOperatorMethid
1552 # The associated operator symbol
1553 var n_op
: Token is writable, noinit
1557 super AOperatorMethid
1562 super AOperatorMethid
1567 super AOperatorMethid
1570 # A method name `**`
1571 class AStarstarMethid
1572 super AOperatorMethid
1577 super AOperatorMethid
1581 class APercentMethid
1582 super AOperatorMethid
1587 super AOperatorMethid
1592 super AOperatorMethid
1597 super AOperatorMethid
1602 super AOperatorMethid
1605 # A method name `==`
1607 super AOperatorMethid
1610 # A method name `!=`
1612 super AOperatorMethid
1615 # A method name `<=`
1617 super AOperatorMethid
1620 # A method name `>=`
1622 super AOperatorMethid
1627 super AOperatorMethid
1632 super AOperatorMethid
1635 # A method name `<<`
1637 super AOperatorMethid
1640 # A method name `>>`
1642 super AOperatorMethid
1645 # A method name `<=>`
1646 class AStarshipMethid
1647 super AOperatorMethid
1650 # A method name `[]`
1655 var n_obra
: TObra is writable, noinit
1658 var n_cbra
: TCbra is writable, noinit
1661 # A setter method name with a simple identifier (with a `=`)
1665 # The base identifier
1666 var n_id
: TId is writable, noinit
1669 var n_assign
: TAssign is writable, noinit
1672 # A method name `[]=`
1673 class ABraassignMethid
1677 var n_obra
: TObra is writable, noinit
1680 var n_cbra
: TCbra is writable, noinit
1683 var n_assign
: TAssign is writable, noinit
1686 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1691 var n_opar
: nullable TOpar = null is writable
1693 # The list of parameters
1694 var n_params
= new ANodes[AParam](self)
1697 var n_cpar
: nullable TCpar = null is writable
1700 var n_type
: nullable AType = null is writable
1703 # A parameter definition in a signature. eg `x:X`
1707 # The name of the parameter
1708 var n_id
: TId is writable, noinit
1710 # The type of the parameter, if any
1711 var n_type
: nullable AType = null is writable
1713 # The `...` symbol to indicate varargs
1714 var n_dotdotdot
: nullable TDotdotdot = null is writable
1717 # A static type. eg `nullable X[Y]`
1720 # The `nullable` keyword
1721 var n_kwnullable
: nullable TKwnullable = null is writable
1723 # The name of the class or of the formal type
1724 var n_id
: TClassid is writable, noinit
1726 # The opening bracket
1727 var n_obra
: nullable TObra = null is writable
1729 # Type arguments for a generic type
1730 var n_types
= new ANodes[AType](self)
1732 # The closing bracket
1733 var n_cbra
: nullable TCbra = null is writable
1736 # A label at the end of a block or in a break/continue statement. eg `label x`
1740 # The `label` keyword
1741 var n_kwlabel
: TKwlabel is writable, noinit
1743 # The name of the label, if any
1744 var n_id
: nullable TId is writable, noinit
1747 # Expression and statements
1748 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1749 abstract class AExpr
1753 # A sequence of `AExpr` (usually statements)
1754 # The last `AExpr` gives the value of the whole block
1758 # The list of statements in the bloc.
1759 # The last element is often considered as an expression that give the value of the whole block.
1760 var n_expr
= new ANodes[AExpr](self)
1763 var n_kwend
: nullable TKwend = null is writable
1766 # A declaration of a local variable. eg `var x: X = y`
1771 var n_kwvar
: nullable TKwvar = null is writable
1773 # The name of the local variable
1774 var n_id
: TId is writable, noinit
1776 # The declaration type of the local variable
1777 var n_type
: nullable AType = null is writable
1779 # The `=` symbol (for the initial value)
1780 var n_assign
: nullable TAssign = null is writable
1782 # The initial value, if any
1783 var n_expr
: nullable AExpr = null is writable
1786 # A `return` statement. eg `return x`
1790 # The `return` keyword
1791 var n_kwreturn
: nullable TKwreturn = null is writable
1793 # The return value, if any
1794 var n_expr
: nullable AExpr = null is writable
1797 # Something that has a label.
1798 abstract class ALabelable
1801 # The associated label declatation
1802 var n_label
: nullable ALabel = null is writable
1805 # A `break` or a `continue`
1806 abstract class AEscapeExpr
1810 # The return value, if nay (unused currently)
1811 var n_expr
: nullable AExpr = null is writable
1814 # A `break` statement.
1818 # The `break` keyword
1819 var n_kwbreak
: TKwbreak is writable, noinit
1822 # An `abort` statement
1826 # The `abort` keyword
1827 var n_kwabort
: TKwabort is writable, noinit
1830 # A `continue` statement
1834 # The `continue` keyword.
1835 var n_kwcontinue
: nullable TKwcontinue = null is writable
1844 var n_kwdo
: TKwdo is writable, noinit
1846 # The list of statements of the `do`.
1847 var n_block
: nullable AExpr = null is writable
1855 var n_kwif
: TKwif is writable, noinit
1857 # The expression used as the condition of the `if`
1858 var n_expr
: AExpr is writable, noinit
1860 # The `then` keyword
1861 var n_kwthen
: TKwthen is writable, noinit
1863 # The body of the `then` part
1864 var n_then
: nullable AExpr = null is writable
1866 # The `else` keyword
1867 var n_kwelse
: nullable TKwelse = null is writable
1869 # The body of the `else` part
1870 var n_else
: nullable AExpr = null is writable
1873 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1878 var n_kwif
: TKwif is writable, noinit
1880 # The expression used as the condition of the `if`
1881 var n_expr
: AExpr is writable, noinit
1883 # The `then` keyword
1884 var n_kwthen
: TKwthen is writable, noinit
1886 # The expression in the `then` part
1887 var n_then
: AExpr is writable, noinit
1889 # The `else` keyword
1890 var n_kwelse
: TKwelse is writable, noinit
1892 # The expression in the `else` part
1893 var n_else
: AExpr is writable, noinit
1896 # A `while` statement
1901 # The `while` keyword
1902 var n_kwwhile
: TKwwhile is writable, noinit
1904 # The expression used as the condition of the `while`
1905 var n_expr
: AExpr is writable, noinit
1908 var n_kwdo
: TKwdo is writable, noinit
1910 # The body of the loop
1911 var n_block
: nullable AExpr = null is writable
1914 # A `loop` statement
1919 # The `loop` keyword
1920 var n_kwloop
: TKwloop is writable, noinit
1922 # The body of the loop
1923 var n_block
: nullable AExpr = null is writable
1932 var n_kwfor
: TKwfor is writable, noinit
1934 # The list of name of the automatic variables
1935 var n_ids
= new ANodes[TId](self)
1938 var n_kwin
: TKwin is writable, noinit
1940 # The expression used as the collection to iterate on
1941 var n_expr
: AExpr is writable, noinit
1944 var n_kwdo
: TKwdo is writable, noinit
1946 # The body of the loop
1947 var n_block
: nullable AExpr = null is writable
1950 # A `with` statement
1955 # The `with` keyword
1956 var n_kwwith
: TKwwith is writable, noinit
1958 # The expression used to get the value to control
1959 var n_expr
: AExpr is writable, noinit
1962 var n_kwdo
: TKwdo is writable, noinit
1964 # The body of the loop
1965 var n_block
: nullable AExpr = null is writable
1968 # An `assert` statement
1972 # The `assert` keyword
1973 var n_kwassert
: TKwassert is writable, noinit
1975 # The name of the assert, if any
1976 var n_id
: nullable TId = null is writable
1978 # The expression used as the condition of the `assert`
1979 var n_expr
: AExpr is writable, noinit
1981 # The `else` keyword
1982 var n_kwelse
: nullable TKwelse = null is writable
1984 # The body to execute when the assert fails
1985 var n_else
: nullable AExpr = null is writable
1988 # Whatever is a simple assignment. eg `= something`
1989 abstract class AAssignFormExpr
1993 var n_assign
: TAssign is writable, noinit
1995 # The right-value to assign.
1996 var n_value
: AExpr is writable, noinit
1999 # Whatever is a combined assignment. eg `+= something`
2000 abstract class AReassignFormExpr
2003 # The combined operator (eg. `+=`)
2004 var n_assign_op
: AAssignOp is writable, noinit
2006 # The right-value to apply on the combined operator.
2007 var n_value
: AExpr is writable, noinit
2010 # A `once` expression. eg `once x`
2014 # The `once` keyword
2015 var n_kwonce
: TKwonce is writable, noinit
2017 # The expression to evaluate only one time
2018 var n_expr
: AExpr is writable, noinit
2021 # A polymorphic invocation of a method
2022 # The form of the invocation (name, arguments, etc.) are specific
2023 abstract class ASendExpr
2025 # The receiver of the method invocation
2026 var n_expr
: AExpr is writable, noinit
2029 # A binary operation on a method
2030 abstract class ABinopExpr
2034 var n_op
: Token is writable, noinit
2036 # The second operand of the operation
2037 # Note: the receiver (`n_expr`) is the first operand
2038 var n_expr2
: AExpr is writable, noinit
2040 # The name of the operator (eg '+')
2041 fun operator
: String is abstract
2044 # Something that is boolean expression
2045 abstract class ABoolExpr
2049 # Something that is binary boolean expression
2050 abstract class ABinBoolExpr
2053 # The first boolean operand
2054 var n_expr
: AExpr is writable, noinit
2057 var n_op
: Token is writable, noinit
2059 # The second boolean operand
2060 var n_expr2
: AExpr is writable, noinit
2068 # A `and` expression
2073 # A `or else` expression
2077 # The `else` keyword
2078 var n_kwelse
: TKwelse is writable, noinit
2081 # A `implies` expression
2086 # A `not` expression
2091 var n_kwnot
: TKwnot is writable, noinit
2093 # The boolean operand of the `not`
2094 var n_expr
: AExpr is writable, noinit
2097 # A `==` or a `!=` expression
2099 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2107 redef fun operator
do return "=="
2113 redef fun operator
do return "!="
2119 redef fun operator
do return "<"
2125 redef fun operator
do return "<="
2131 redef fun operator
do return "<<"
2137 redef fun operator
do return ">"
2143 redef fun operator
do return ">="
2149 redef fun operator
do return ">>"
2152 # A type-ckeck expression. eg `x isa T`
2156 # The expression to check
2157 var n_expr
: AExpr is writable, noinit
2160 var n_kwisa
: TKwisa is writable, noinit
2162 # The destination type to check to
2163 var n_type
: AType is writable, noinit
2169 redef fun operator
do return "+"
2175 redef fun operator
do return "-"
2178 # A `<=>` expression
2181 redef fun operator
do return "<=>"
2187 redef fun operator
do return "*"
2193 redef fun operator
do return "**"
2199 redef fun operator
do return "/"
2205 redef fun operator
do return "%"
2211 redef fun operator
do return "|"
2217 redef fun operator
do return "^"
2223 redef fun operator
do return "&"
2226 # A unary operation on a method
2231 var n_op
: Token is writable, noinit
2233 # The name of the operator (eg '+')
2234 fun operator
: String is abstract
2237 # A unary minus expression. eg `-x`
2240 redef fun operator
do return "-"
2243 # A unary plus expression. eg `+x`
2246 redef fun operator
do return "+"
2249 # A unary `~` expression
2252 redef fun operator
do return "~"
2255 # An explicit instantiation. eg `new T`
2260 var n_kwnew
: TKwnew is writable, noinit
2262 # The `type` keyword
2263 var n_type
: AType is writable, noinit
2265 # The name of the named-constructor, if any
2266 var n_id
: nullable TId = null is writable
2268 # The arguments of the `new`
2269 var n_args
: AExprs is writable, noinit
2272 # Whatever is a old-style attribute access
2273 abstract class AAttrFormExpr
2276 # The receiver of the attribute
2277 var n_expr
: AExpr is writable, noinit
2279 # The name of the attribute
2280 var n_id
: TAttrid is writable, noinit
2284 # The read of an attribute. eg `x._a`
2289 # The assignment of an attribute. eg `x._a=y`
2290 class AAttrAssignExpr
2292 super AAssignFormExpr
2295 # Whatever looks-like a call with a standard method and any number of arguments.
2296 abstract class ACallFormExpr
2299 # The name of the method
2300 var n_id
: TId is writable, noinit
2302 # The arguments of the call
2303 var n_args
: AExprs is writable, noinit
2306 # A complex setter call (standard or brackets)
2307 abstract class ASendReassignFormExpr
2309 super AReassignFormExpr
2312 # A complex attribute assignment. eg `x._a+=y`
2313 class AAttrReassignExpr
2315 super AReassignFormExpr
2318 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2319 # 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`.
2320 # Semantic analysis have to transform them to instance of `AVarExpr`.
2325 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2326 # 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`.
2327 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2328 class ACallAssignExpr
2330 super AAssignFormExpr
2333 # 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.
2334 # 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`.
2335 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2336 class ACallReassignExpr
2338 super ASendReassignFormExpr
2341 # A call to `super`. OR a call of a super-constructor
2345 # The qualifier part before the super (currenlty unused)
2346 var n_qualified
: nullable AQualified = null is writable
2348 # The `super` keyword
2349 var n_kwsuper
: TKwsuper is writable, noinit
2351 # The arguments of the super
2352 var n_args
: AExprs is writable, noinit
2355 # A call to the `init` constructor.
2356 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2360 # The `init` keyword
2361 var n_kwinit
: TKwinit is writable, noinit
2363 # The arguments of the init
2364 var n_args
: AExprs is writable, noinit
2367 # Whatever looks-like a call of the brackets `[]` operator.
2368 abstract class ABraFormExpr
2371 # The arguments inside the brackets
2372 var n_args
: AExprs is writable, noinit
2375 # A call of the brackets operator. eg `x[y,z]`
2380 # A setter call of the bracket operator. eg `x[y,z]=t`
2381 class ABraAssignExpr
2383 super AAssignFormExpr
2386 # Whatever is an access to a local variable
2387 abstract class AVarFormExpr
2390 # The name of the attribute
2391 var n_id
: TId is writable, noinit
2394 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2395 class ABraReassignExpr
2397 super ASendReassignFormExpr
2400 # A local variable read access.
2401 # The parser cannot instantiate them, see `ACallExpr`.
2406 # A local variable simple assignment access
2407 # The parser cannot instantiate them, see `ACallAssignExpr`.
2408 class AVarAssignExpr
2410 super AAssignFormExpr
2413 # A local variable complex assignment access
2414 # The parser cannot instantiate them, see `ACallReassignExpr`.
2415 class AVarReassignExpr
2417 super AReassignFormExpr
2420 # A literal range, open or closed
2421 abstract class ARangeExpr
2424 # The left (lower) element of the range
2425 var n_expr
: AExpr is writable, noinit
2428 var n_dotdot
: TDotdot is writable, noinit
2430 # The right (upper) element of the range
2431 var n_expr2
: AExpr is writable, noinit
2434 # A closed literal range. eg `[x..y]`
2438 # The opening bracket `[`
2439 var n_obra
: TObra is writable, noinit
2441 # The closing bracket `]`
2442 var n_cbra
: TCbra is writable, noinit
2445 # An open literal range. eg `[x..y[`
2449 # The opening bracket `[`
2450 var n_obra
: TObra is writable, noinit
2452 # The closing bracket `[` (because open range)
2453 var n_cbra
: TObra is writable, noinit
2456 # A literal array. eg. `[x,y,z]`
2460 # The opening bracket `[`
2461 var n_obra
: TObra is writable, noinit
2463 # The elements of the array
2464 var n_exprs
= new ANodes[AExpr](self)
2466 # The type of the element of the array (if any)
2467 var n_type
: nullable AType = null is writable
2469 # The closing bracket `]`
2470 var n_cbra
: TCbra is writable, noinit
2477 # The `self` keyword
2478 var n_kwself
: nullable TKwself = null is writable
2481 # When there is no explicit receiver, `self` is implicit
2482 class AImplicitSelfExpr
2486 # A `true` boolean literal constant
2490 # The `true` keyword
2491 var n_kwtrue
: TKwtrue is writable, noinit
2494 # A `false` boolean literal constant
2498 # The `false` keyword
2499 var n_kwfalse
: TKwfalse is writable, noinit
2502 # A `null` literal constant
2506 # The `null` keyword
2507 var n_kwnull
: TKwnull is writable, noinit
2510 # An integer literal
2515 # An integer literal in decimal format
2520 var n_number
: TNumber is writable, noinit
2523 # An integer literal in hexadecimal format
2527 # The hexadecimal token
2528 var n_hex_number
: THexNumber is writable, noinit
2531 # An integer literal in binary format
2536 var n_bin_number
: TBinNumber is writable, noinit
2539 # An integer literal in octal format
2544 var n_oct_number
: TOctNumber is writable, noinit
2552 # An byte literal in decimal format
2557 var n_bytenum
: TBytenum is writable, noinit
2560 # An byte literal in hexadecimal format
2564 # The hexadecimal token
2565 var n_hex_bytenum
: THexBytenum is writable, noinit
2568 # An byte literal in binary format
2573 var n_bin_bytenum
: TBinBytenum is writable, noinit
2576 # An byte literal in octal format
2581 var n_oct_bytenum
: TOctBytenum is writable, noinit
2589 var n_float
: TFloat is writable, noinit
2592 # A character literal
2596 # The character token
2597 var n_char
: TChar is writable, noinit
2601 abstract class AStringFormExpr
2605 var n_string
: Token is writable, noinit
2608 # A simple string. eg. `"abc"`
2610 super AStringFormExpr
2613 # The start of a superstring. eg `"abc{`
2614 class AStartStringExpr
2615 super AStringFormExpr
2618 # The middle of a superstring. eg `}abc{`
2619 class AMidStringExpr
2620 super AStringFormExpr
2623 # The end of a superstrng. eg `}abc"`
2624 class AEndStringExpr
2625 super AStringFormExpr
2628 # A superstring literal. eg `"a{x}b{y}c"`
2629 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2630 class ASuperstringExpr
2633 # The list of the expressions of the superstring
2634 var n_exprs
= new ANodes[AExpr](self)
2637 # A simple parenthesis. eg `(x)`
2641 # The opening parenthesis
2642 var n_opar
: TOpar is writable, noinit
2644 # The inner expression
2645 var n_expr
: AExpr is writable, noinit
2647 # The closing parenthesis
2648 var n_cpar
: TCpar is writable, noinit
2651 # A cast, against a type or `not null`
2655 # The expression to cast
2656 var n_expr
: AExpr is writable, noinit
2659 var n_kwas
: TKwas is writable, noinit
2661 # The opening parenthesis
2662 var n_opar
: nullable TOpar = null is writable
2664 # The closing parenthesis
2665 var n_cpar
: nullable TCpar = null is writable
2668 # A type cast. eg `x.as(T)`
2672 # The target type to cast to
2673 var n_type
: AType is writable, noinit
2676 # A as-not-null cast. eg `x.as(not null)`
2677 class AAsNotnullExpr
2681 var n_kwnot
: TKwnot is writable, noinit
2683 # The `null` keyword
2684 var n_kwnull
: TKwnull is writable, noinit
2687 # A is-set check of old-style attributes. eg `isset x._a`
2688 class AIssetAttrExpr
2691 # The `isset` keyword
2692 var n_kwisset
: TKwisset is writable, noinit
2695 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2699 # The passed expression
2700 var n_expr
: AExpr is writable, noinit
2703 var n_dotdotdot
: TDotdotdot is writable, noinit
2706 # An named notation used to pass an expression by name in a parameter
2710 # The name of the argument
2711 var n_id
: TId is writable, noinit
2714 var n_assign
: TAssign is writable, noinit
2716 # The passed expression
2717 var n_expr
: AExpr is writable, noinit
2720 # A list of expression separated with commas (arguments for instance)
2724 # The list of expressions
2725 var n_exprs
= new ANodes[AExpr](self)
2728 # A special expression that encapsulates a static type
2729 # Can only be found in special construction like arguments of annotations.
2733 # The encapsulated type
2734 var n_type
: AType is writable, noinit
2737 # A special expression that encapsulates a method identifier
2738 # Can only be found in special construction like arguments of annotations.
2743 var n_expr
: AExpr is writable, noinit
2745 # The encapsulated method identifier
2746 var n_id
: AMethid is writable, noinit
2749 # A special expression that encapsulate an annotation
2750 # Can only be found in special construction like arguments of annotations.
2752 # The encapsulated annotations are in `n_annotations`
2757 # A special expression to debug types
2758 class ADebugTypeExpr
2761 # The `debug` keyword
2762 var n_kwdebug
: TKwdebug is writable, noinit
2764 # The `type` keyword
2765 var n_kwtype
: TKwtype is writable, noinit
2767 # The expression to check
2768 var n_expr
: AExpr is writable, noinit
2771 var n_type
: AType is writable, noinit
2774 # A list of expression separated with commas (arguments for instance)
2775 abstract class AExprs
2778 # The list of expressions
2779 var n_exprs
= new ANodes[AExpr](self)
2782 # A simple list of expressions
2787 # A list of expressions enclosed in parentheses
2791 # The opening parenthesis
2792 var n_opar
: TOpar is writable, noinit
2794 # The closing parenthesis
2795 var n_cpar
: TCpar is writable, noinit
2798 # A list of expressions enclosed in brackets
2802 # The opening bracket
2803 var n_obra
: TObra is writable, noinit
2805 # The closing bracket
2806 var n_cbra
: TCbra is writable, noinit
2809 # A complex assignment operator. (`+=` and `-=`)
2810 abstract class AAssignOp
2813 # The combined assignment operator
2814 var n_op
: Token is writable, noinit
2816 # The name of the operator without the `=` (eg '+')
2817 fun operator
: String is abstract
2820 # A `+=` assignment operation
2824 redef fun operator
do return "+"
2827 # A `-=` assignment operation
2828 class AMinusAssignOp
2831 redef fun operator
do return "-"
2834 # A `*=` assignment operation
2838 redef fun operator
do return "*"
2841 # A `/=` assignment operation
2842 class ASlashAssignOp
2845 redef fun operator
do return "/"
2848 # A `%=` assignment operation
2849 class APercentAssignOp
2852 redef fun operator
do return "%"
2855 # A `**=` assignment operation
2856 class AStarstarAssignOp
2859 redef fun operator
do return "**"
2862 # A `|=` assignment operation
2866 redef fun operator
do return "|"
2869 # A `^=` assignment operation
2870 class ACaretAssignOp
2873 redef fun operator
do return "^"
2876 # A `&=` assignment operation
2880 redef fun operator
do return "&"
2883 # A `<<=` assignment operation
2887 redef fun operator
do return "<<"
2890 # A `>>=` assignment operation
2894 redef fun operator
do return ">>"
2897 # A possibly fully-qualified module identifier
2901 # The starting quad (`::`)
2902 var n_quad
: nullable TQuad = null is writable
2904 # The list of quad-separated project/group identifiers
2905 var n_path
= new ANodes[TId](self)
2907 # The final module identifier
2908 var n_id
: TId is writable, noinit
2911 # A language declaration for an extern block
2916 var n_kwin
: TKwin is writable, noinit
2919 var n_string
: TString is writable, noinit
2922 # An full extern block
2923 class AExternCodeBlock
2926 # The language declration
2927 var n_in_language
: nullable AInLanguage = null is writable
2929 # The block of extern code
2930 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2933 # A possible full method qualifier.
2937 # The starting quad (`::`)
2938 var n_quad
: nullable TQuad = null is writable
2940 # The list of quad-separated project/group/module identifiers
2941 var n_id
= new ANodes[TId](self)
2943 # A class identifier
2944 var n_classid
: nullable TClassid = null is writable
2947 # A documentation of a definition
2948 # It contains the block of comments just above the declaration
2952 # A list of lines of comment
2953 var n_comment
= new ANodes[TComment](self)
2956 # A group of annotation on a node
2958 # This same class is used for the 3 kind of annotations:
2960 # * *is* annotations. eg `module foo is bar`.
2961 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2962 # * *class* annotations, defined in classes.
2966 # The `is` keyword, for *is* annotations
2967 var n_kwis
: nullable TKwis = null is writable
2969 # The `@` symbol, for *at* annotations
2970 var n_at
: nullable TAt = null is writable
2972 # The opening parenthesis in *at* annotations
2973 var n_opar
: nullable TOpar = null is writable
2975 # The list of annotations
2976 var n_items
= new ANodes[AAnnotation](self)
2978 # The closing parenthesis in *at* annotations
2979 var n_cpar
: nullable TCpar = null is writable
2981 # The `end` keyword, for *is* annotations
2982 var n_kwend
: nullable TKwend = null is writable
2985 # A single annotation
2989 # The name of the annotation
2990 var n_atid
: AAtid is writable, noinit
2992 # The opening parenthesis of the arguments
2993 var n_opar
: nullable TOpar = null is writable
2995 # The list of arguments
2996 var n_args
= new ANodes[AExpr](self)
2998 # The closing parenthesis
2999 var n_cpar
: nullable TCpar = null is writable
3001 # The name of the annotation
3004 return n_atid
.n_id
.text
3008 # An annotation name
3009 abstract class AAtid
3012 # The identifier of the annotation.
3013 # Can be a TId of a keyword
3014 var n_id
: Token is writable, noinit
3017 # An annotation name based on an identifier
3022 # An annotation name based on the keyword `extern`
3027 # An annotation name based on the keyword `import`
3032 # An annotation name based on the keyword `abstract`
3033 class AKwabstractAtid
3037 # The root of the AST
3042 var n_base
: nullable AModule is writable
3044 # The end of file (or error) token
3045 var n_eof
: EOF is writable