1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # AST nodes of the Nit language
16 # Was previously based on parser_abs.nit.
22 # Root of the AST class-hierarchy
24 # Location is set during AST building. Once built, location cannon be null.
25 # However, manual instantiated nodes may need more care.
26 var location
: Location is writable, noinit
28 # The location of the important part of the node (identifier or whatever)
29 fun hot_location
: Location do return location
31 # Display a message for the colored location of the node
32 fun debug
(message
: String)
34 sys
.stderr
.write
"{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
37 # Is `self` a token or a pure-structural production like `AQId`?
38 fun is_structural
: Bool do return false
40 # Write the subtree on stdout.
42 fun dump_tree
(display_structural
: nullable Bool)
44 var d
= new ASTDump(display_structural
or else true)
46 d
.write_to
(sys
.stdout
)
49 # Parent of the node in the AST
50 var parent
: nullable ANode = null
52 # The topmost ancestor of the element
53 # This just apply `parent` until the first one
59 if p
== null then return res
64 # The most specific common parent between `self` and `other`
65 # Return null if the two node are unrelated (distinct root)
66 fun common_parent
(other
: ANode): nullable ANode
68 # First, get the same depth
69 var s
: nullable ANode = self
70 var o
: nullable ANode = other
71 var d
= s
.depth
- o
.depth
80 assert o
.depth
== s
.depth
81 # Second, go up until same in found
89 # Number of nodes between `self` and the `root` of the AST
90 # ENSURE `self == self.root implies result == 0 `
91 # ENSURE `self != self.root implies result == self.parent.depth + 1`
98 if p
== null then return res
104 # Replace a child with an other node in the AST
105 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode) is abstract
107 # Detach a node from its parent
108 # Aborts if the node is not detachable. use `replace_with` instead
109 # REQUIRE: parent != null
110 # REQUIRE: is_detachable
111 # ENDURE: parent == null
114 assert parent
!= null
115 parent
.replace_child
(self, null)
119 # Replace itself with an other node in the AST
120 # REQUIRE: parent != null
121 # ENSURE: node.parent == old(parent)
122 # ENSURE: parent == null
123 fun replace_with
(node
: ANode)
125 assert parent
!= null
126 parent
.replace_child
(self, node
)
130 # Visit all nodes in order.
131 # Thus, call `v.enter_visit(e)` for each child `e`
132 fun visit_all
(v
: Visitor) is abstract
134 # Do a deep search and return an array of tokens that match a given text
135 fun collect_tokens_by_text
(text
: String): Array[Token]
137 var v
= new CollectTokensByTextVisitor(text
)
142 # Do a deep search and return an array of node that are annotated
143 # The attached node can be retrieved by two invocations of parent
144 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
146 var v
= new CollectAnnotationsByNameVisitor(name
)
152 private class CollectTokensByTextVisitor
155 var result
= new Array[Token]
156 redef fun visit
(node
)
159 if node
isa Token and node
.text
== text
then result
.add
(node
)
163 private class CollectAnnotationsByNameVisitor
166 var result
= new Array[AAnnotation]
167 redef fun visit
(node
)
170 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)
174 # A helper class to handle (print) Nit AST as an OrderedTree
177 super OrderedTree[ANode]
179 # Reference to the last parent in the Ordered Tree
180 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
181 private var last_parent
: nullable ANode = null
183 # Display tokens and structural production?
185 # Should tokens (and structural production like AQId) be displayed?
186 var display_structural
: Bool
190 if not display_structural
and n
.is_structural
then return
201 return "{n.class_name} \"{n.text.escape_to_c}\
" @{n.location}"
203 return "{n.class_name} @{n.location}"
208 # A sequence of nodes
209 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
210 class ANodes[E
: ANode]
212 private var parent
: ANode
213 private var items
= new Array[E
]
214 redef fun iterator
do return items
.iterator
215 redef fun reverse_iterator
do return items
.reverse_iterator
216 redef fun length
do return items
.length
217 redef fun is_empty
do return items
.is_empty
236 var res
= items
.shift
248 redef fun []=(index
, e
)
250 hook_remove
(self[index
])
254 redef fun remove_at
(index
)
256 hook_remove
(items
[index
])
257 items
.remove_at
(index
)
259 private fun hook_add
(e
: E
)
261 #assert e.parent == null
264 private fun hook_remove
(e
: E
)
266 assert e
.parent
== parent
270 # Used in parent constructor to fill elements
271 private fun unsafe_add_all
(nodes
: Collection[Object])
273 var parent
= self.parent
281 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
283 var parent
= self.parent
284 for i
in [0..length
[ do
285 if self[i
] == old_child
then
286 if new_child
!= null then
287 assert new_child
isa E
289 new_child
.parent
= parent
299 private fun visit_all
(v
: Visitor)
301 for n
in self do v
.enter_visit
(n
)
305 # Ancestor of all tokens
306 # A token is a node that has a `text` but no children.
310 # The raw content on the token
311 fun text
: String is abstract
313 # The raw content on the token
314 fun text
=(text
: String) is abstract
316 # The previous token in the Lexer.
317 # May have disappeared in the AST
318 var prev_token
: nullable Token = null
320 # The next token in the Lexer.
321 # May have disappeared in the AST
322 var next_token
: nullable Token = null
324 # Is `self` a token discarded from the AST?
326 # Loose tokens are not present in the AST.
327 # It means they were identified by the lexer but were discarded by the parser.
328 # It also means that they are not visited or manipulated by AST-related functions.
330 # Each loose token is attached to the non-loose token that precedes or follows it.
331 # The rules are the following:
333 # * tokens that follow a non-loose token on a same line are attached to it.
335 # * other tokens, thus that precede a non-loose token on the same line or the next one,
336 # are attached to this one. See `prev_looses`.
338 # Loose tokens are mostly end of lines (`TEol`) and comments (`TComment`).
339 # Whitespace are ignored by the lexer, so they are not even considered as loose tokens.
340 # See `blank_before` to get the whitespace that separate tokens.
343 redef fun is_structural
do return true
345 # Loose tokens that precede `self`.
347 # These tokens start the line or belong to a line with only loose tokens.
348 var prev_looses
= new Array[Token] is lazy
350 # Loose tokens that follow `self`
352 # These tokens are on the same line than `self`.
353 var next_looses
= new Array[Token] is lazy
355 # The verbatim blank text between `prev_token` and `self`
356 fun blank_before
: String
358 if prev_token
== null then return ""
359 var from
= prev_token
.location
.pend
+1
360 var to
= location
.pstart
361 return location
.file
.string
.substring
(from
,to-from
)
364 redef fun to_s
: String do
368 redef fun visit_all
(v
: Visitor) do end
369 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
372 redef class SourceFile
373 # The first token parser by the lexer
374 # May have disappeared in the final AST
375 var first_token
: nullable Token = null
377 # The first token parser by the lexer
378 # May have disappeared in the final AST
379 var last_token
: nullable Token = null
382 # Ancestor of all productions
383 # A production is a node without text but that usually has children.
387 # All the annotations attached directly to the node
388 var n_annotations
: nullable AAnnotations = null is writable
390 # Return all its annotations of a given name in the order of their declaration
391 # Retun an empty array if no such an annotation.
392 fun get_annotations
(name
: String): Array[AAnnotation]
394 var res
= new Array[AAnnotation]
395 var nas
= n_annotations
396 if nas
!= null then for na
in nas
.n_items
do
397 if na
.name
!= name
then continue
400 if self isa AClassdef then for na
in n_propdefs
do
401 if na
isa AAnnotPropdef then
402 if na
.name
!= name
then continue
410 redef fun replace_with
(n
: ANode)
414 if not isset n
._location
and isset _location
then n
._location
= _location
418 # Abstract standard visitor on the AST
419 abstract class Visitor
420 # What the visitor do when a node is visited
421 # Concrete visitors should implement this method.
423 protected fun visit
(e
: ANode) is abstract
425 # Ask the visitor to visit a given node.
426 # Usually automatically called by visit_all* methods.
427 # This method should not be redefined
428 fun enter_visit
(e
: nullable ANode)
430 if e
== null then return
431 var old
= _current_node
437 # The current visited node
438 var current_node
: nullable ANode = null is writable
441 # Token of end of line (basically `\n`)
450 # Token of a line of comments
451 # Starts with the `#` and contains the final end-of-line (if any)
456 # A token associated with a keyword
457 abstract class TokenKeyword
461 return "keyword '{text}'"
465 # The deprecated keyword `package`.
470 # The keyword `module`
475 # The keyword `import`
480 # The keyword `class`
485 # The keyword `abstract`
490 # The keyword `interface`
495 # The keywords `enum` ane `universal`
520 # The keyword `redef`
535 # The keyword `catch`
545 # The keyword `extern`
550 # The keyword `public`
555 # The keyword `protected`
560 # The keyword `private`
565 # The keyword `intrude`
585 # The keyword `while`
615 # The keyword `implies`
625 # The keyword `return`
630 # The keyword `continue`
635 # The keyword `break`
640 # The keyword `abort`
645 # The keyword `assert`
665 # The keyword `super`
680 # The keyword `false`
695 # The keyword `nullable`
700 # The keyword `isset`
705 # The keyword `label`
715 # The keyword `yield`
720 # The special keyword `__DEBUG__`
765 # A token associated with an operator (and other lookalike symbols)
766 abstract class TokenOperator
770 return "operator '{text}'"
954 # A class (or formal type) identifier. They start with an uppercase.
959 do return "type identifier '{text}'"
963 # A standard identifier (variable, method...). They start with a lowercase.
968 do return "identifier '{text}'"
972 # An attribute identifier. They start with an underscore.
977 do return "attribute '{text}'"
981 # A token of a literal value (string, integer, etc).
982 abstract class TokenLiteral
986 do return "literal value '{text}'"
995 # A literal floating point number
1000 # A literal character
1010 # The starting part of a super string (between `"` and `{`)
1015 # The middle part of a super string (between `}` and `{`)
1020 # The final part of a super string (between `}` and `"`)
1025 # A malformed string
1030 do return "malformed string {text}"
1039 do return "malformed character {text}"
1043 # A extern code block
1044 class TExternCodeSegment
1053 return "end of file"
1057 # A mark of an error
1061 # A lexical error (unexpected character)
1065 # A syntactic error (unexpected token)
1070 # The main node of a Nit source-file
1074 # The declaration part of the module
1075 var n_moduledecl
: nullable AModuledecl = null is writable
1077 # List of importation clauses
1078 var n_imports
= new ANodes[AImport](self)
1080 # List of extern blocks
1081 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1083 # List of class definition (including top-level methods and the main)
1084 var n_classdefs
= new ANodes[AClassdef](self)
1087 # Abstract class for definition of entities
1088 abstract class ADefinition
1091 var n_doc
: nullable ADoc = null is writable
1093 # The `redef` keyword
1094 var n_kwredef
: nullable TKwredef = null is writable
1096 # The declared visibility
1097 var n_visibility
: nullable AVisibility = null is writable
1100 # The declaration of the module with the documentation, name, and annotations
1104 # The `module` keyword
1105 var n_kwmodule
: TKwmodule is writable, noinit
1107 # The declared module name
1108 var n_name
: AModuleName is writable, noinit
1111 # A import clause of a module
1112 abstract class AImport
1115 # The declared visibility
1116 var n_visibility
: AVisibility is writable, noinit
1118 # The `import` keyword
1119 var n_kwimport
: TKwimport is writable, noinit
1122 # A standard import clause. eg `import x`
1125 # The imported module name
1126 var n_name
: AModuleName is writable, noinit
1129 # The special import clause of the kernel module. eg `import end`
1132 # The `end` keyword, that indicate the root module
1133 var n_kwend
: TKwend is writable, noinit
1136 # A visibility modifier
1138 # The public visibility is an empty production (no keyword).
1140 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1141 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1142 abstract class AVisibility
1146 # An implicit or explicit public visibility modifier
1147 class APublicVisibility
1149 # The `public` keyword, if any
1150 var n_kwpublic
: nullable TKwpublic = null is writable
1152 # An explicit private visibility modifier
1153 class APrivateVisibility
1155 # The `private` keyword
1156 var n_kwprivate
: TKwprivate is writable, noinit
1158 # An explicit protected visibility modifier
1159 class AProtectedVisibility
1161 # The `protected` keyword
1162 var n_kwprotected
: TKwprotected is writable, noinit
1164 # An explicit intrude visibility modifier
1165 class AIntrudeVisibility
1167 # The `intrude` keyword
1168 var n_kwintrude
: TKwintrude is writable, noinit
1171 # A class definition
1172 # While most definition are `AStdClassdef`
1173 # There is tow special case of class definition
1174 abstract class AClassdef
1176 # All the declared properties (including the main method)
1177 var n_propdefs
= new ANodes[APropdef](self)
1180 # A standard class definition with a name, superclasses and properties
1185 # The class kind (interface, abstract class, etc.)
1186 var n_classkind
: AClasskind is writable, noinit
1188 # The name of the class
1189 var n_qid
: nullable AQclassid = null is writable
1192 var n_obra
: nullable TObra = null is writable
1194 # The list of formal parameter types
1195 var n_formaldefs
= new ANodes[AFormaldef](self)
1198 var n_cbra
: nullable TCbra = null is writable
1200 # The extern block code
1201 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1204 var n_kwend
: TKwend is writable, noinit
1206 fun n_superclasses
: Array[ASuperPropdef] do
1207 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1210 redef fun hot_location
do return n_qid
.location
1213 # The implicit class definition of the implicit main method
1218 # The implicit class definition of the top-level methods
1223 # The modifier for the kind of class (abstract, interface, etc.)
1224 abstract class AClasskind
1228 # A default, or concrete class modifier (just `class`)
1229 class AConcreteClasskind
1232 # The `class` keyword.
1233 var n_kwclass
: TKwclass is writable, noinit
1236 # An abstract class modifier (`abstract class`)
1237 class AAbstractClasskind
1240 # The `abstract` keyword.
1241 var n_kwabstract
: TKwabstract is writable, noinit
1243 # The `class` keyword.
1244 var n_kwclass
: TKwclass is writable, noinit
1247 # An interface class modifier (`interface`)
1248 class AInterfaceClasskind
1251 # The `interface` keyword.
1252 var n_kwinterface
: TKwinterface is writable, noinit
1255 # An enum/universal class modifier (`enum class`)
1256 class AEnumClasskind
1259 # The `enum` keyword.
1260 var n_kwenum
: TKwenum is writable, noinit
1263 # An extern class modifier (`extern class`)
1264 class AExternClasskind
1267 # The `extern` keyword.
1268 var n_kwextern
: TKwextern is writable, noinit
1270 # The `class` keyword.
1271 var n_kwclass
: nullable TKwclass = null is writable
1274 # The definition of a formal generic parameter type. eg `X: Y`
1278 # The name of the parameter type
1279 var n_id
: TClassid is writable, noinit
1281 # The bound of the parameter type
1282 var n_type
: nullable AType = null is writable
1285 # The definition of a property
1286 abstract class APropdef
1290 # A definition of an attribute
1291 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1296 var n_kwvar
: TKwvar is writable, noinit
1298 # The identifier for a new-style attribute
1299 var n_id2
: TId is writable, noinit
1301 # The declared type of the attribute
1302 var n_type
: nullable AType = null is writable
1305 var n_assign
: nullable TAssign = null is writable
1307 # The initial value, if any (set with `=`)
1308 var n_expr
: nullable AExpr = null is writable
1311 var n_kwdo
: nullable TKwdo = null is writable
1313 # The initial value, if any (set with `do return`)
1314 var n_block
: nullable AExpr = null is writable
1317 var n_kwend
: nullable TKwend = null is writable
1319 redef fun hot_location
1321 return n_id2
.location
1325 # A definition of all kind of method (including constructors)
1329 # The `fun` keyword, if any
1330 var n_kwmeth
: nullable TKwmeth = null is writable
1332 # The `init` keyword, if any
1333 var n_kwinit
: nullable TKwinit = null is writable
1335 # The `new` keyword, if any
1336 var n_kwnew
: nullable TKwnew = null is writable
1338 # The name of the method, if any
1339 var n_methid
: nullable AMethid = null is writable
1341 # The signature of the method, if any
1342 var n_signature
: nullable ASignature = null is writable
1345 var n_kwdo
: nullable TKwdo = null is writable
1347 # The body (in Nit) of the method, if any
1348 var n_block
: nullable AExpr = null is writable
1351 var n_kwend
: nullable TKwend = null is writable
1353 # The list of declared callbacks (for extern methods)
1354 var n_extern_calls
: nullable AExternCalls = null is writable
1356 # The body (in extern code) of the method, if any
1357 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1359 redef fun hot_location
1361 if n_methid
!= null then
1362 return n_methid
.location
1363 else if n_kwinit
!= null then
1364 return n_kwinit
.location
1365 else if n_kwnew
!= null then
1366 return n_kwnew
.location
1373 # The implicit main method
1374 class AMainMethPropdef
1383 # A super-class. eg `super X`
1388 var n_kwsuper
: TKwsuper is writable, noinit
1390 # The super-class (indicated as a type)
1391 var n_type
: AType is writable, noinit
1395 # Declaration of callbacks for extern methods
1399 # The `import` keyword
1400 var n_kwimport
: TKwimport is writable, noinit
1402 # The list of declared callbacks
1403 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1406 # A single callback declaration
1407 abstract class AExternCall
1411 # A single callback declaration on a method
1412 abstract class APropExternCall
1416 # A single callback declaration on a method on the current receiver
1417 class ALocalPropExternCall
1418 super APropExternCall
1420 # The name of the called-back method
1421 var n_methid
: AMethid is writable, noinit
1424 # A single callback declaration on a method on an explicit receiver type.
1425 class AFullPropExternCall
1426 super APropExternCall
1428 # The type of the receiver of the called-back method
1429 var n_type
: AType is writable, noinit
1432 var n_dot
: nullable TDot = null is writable
1434 # The name of the called-back method
1435 var n_methid
: AMethid is writable, noinit
1438 # A single callback declaration on a method on a constructor
1439 class AInitPropExternCall
1440 super APropExternCall
1442 # The allocated type
1443 var n_type
: AType is writable, noinit
1446 # A single callback declaration on a `super` call
1447 class ASuperExternCall
1450 # The `super` keyword
1451 var n_kwsuper
: TKwsuper is writable, noinit
1454 # A single callback declaration on a cast
1455 abstract class ACastExternCall
1459 # A single callback declaration on a cast to a given type
1460 class ACastAsExternCall
1461 super ACastExternCall
1463 # The origin type of the cast
1464 var n_from_type
: AType is writable, noinit
1467 var n_dot
: nullable TDot = null is writable
1470 var n_kwas
: TKwas is writable, noinit
1472 # The destination of the cast
1473 var n_to_type
: AType is writable, noinit
1476 # A single callback declaration on a cast to a nullable type
1477 class AAsNullableExternCall
1478 super ACastExternCall
1480 # The origin type to cast as nullable
1481 var n_type
: AType is writable, noinit
1484 var n_kwas
: TKwas is writable, noinit
1486 # The `nullable` keyword
1487 var n_kwnullable
: TKwnullable is writable, noinit
1490 # A single callback declaration on a cast to a non-nullable type
1491 class AAsNotNullableExternCall
1492 super ACastExternCall
1494 # The destination type on a cast to not nullable
1495 var n_type
: AType is writable, noinit
1498 var n_kwas
: TKwas is writable, noinit
1501 var n_kwnot
: TKwnot is writable, noinit
1503 # The `nullable` keyword
1504 var n_kwnullable
: TKwnullable is writable, noinit
1507 # A definition of a virtual type
1511 # The `type` keyword
1512 var n_kwtype
: TKwtype is writable, noinit
1514 # The name of the virtual type
1515 var n_qid
: AQclassid is writable, noinit
1517 # The bound of the virtual type
1518 var n_type
: AType is writable, noinit
1521 # The identifier of a method in a method declaration.
1522 # There is a specific class because of operator and setters.
1523 abstract class AMethid
1527 # A method name with a simple identifier
1531 # The simple identifier
1532 var n_id
: TId is writable, noinit
1535 # A method name for an operator
1536 class AOperatorMethid
1539 # The associated operator symbol
1540 var n_op
: Token is writable, noinit
1544 super AOperatorMethid
1549 super AOperatorMethid
1554 super AOperatorMethid
1557 # A method name `**`
1558 class AStarstarMethid
1559 super AOperatorMethid
1564 super AOperatorMethid
1568 class APercentMethid
1569 super AOperatorMethid
1574 super AOperatorMethid
1579 super AOperatorMethid
1584 super AOperatorMethid
1589 super AOperatorMethid
1592 # A method name `==`
1594 super AOperatorMethid
1597 # A method name `!=`
1599 super AOperatorMethid
1602 # A method name `<=`
1604 super AOperatorMethid
1607 # A method name `>=`
1609 super AOperatorMethid
1614 super AOperatorMethid
1619 super AOperatorMethid
1622 # A method name `<<`
1624 super AOperatorMethid
1627 # A method name `>>`
1629 super AOperatorMethid
1632 # A method name `<=>`
1633 class AStarshipMethid
1634 super AOperatorMethid
1637 # A method name `[]`
1642 var n_obra
: TObra is writable, noinit
1645 var n_cbra
: TCbra is writable, noinit
1648 # A setter method name with a simple identifier (with a `=`)
1652 # The base identifier
1653 var n_id
: TId is writable, noinit
1656 var n_assign
: TAssign is writable, noinit
1659 # A method name `[]=`
1660 class ABraassignMethid
1664 var n_obra
: TObra is writable, noinit
1667 var n_cbra
: TCbra is writable, noinit
1670 var n_assign
: TAssign is writable, noinit
1673 # A potentially qualified simple identifier `foo::bar::baz`
1676 # The qualifier, if any
1677 var n_qualified
: nullable AQualified = null is writable
1679 # The final identifier
1680 var n_id
: TId is writable, noinit
1682 redef fun is_structural
do return true
1685 # A potentially qualified class identifier `foo::bar::Baz`
1688 # The qualifier, if any
1689 var n_qualified
: nullable AQualified = null is writable
1691 # The final identifier
1692 var n_id
: TClassid is writable, noinit
1694 redef fun is_structural
do return true
1697 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1702 var n_opar
: nullable TOpar = null is writable
1704 # The list of parameters
1705 var n_params
= new ANodes[AParam](self)
1708 var n_cpar
: nullable TCpar = null is writable
1711 var n_type
: nullable AType = null is writable
1714 # A parameter definition in a signature. eg `x:X`
1718 # The name of the parameter
1719 var n_id
: TId is writable, noinit
1721 # The type of the parameter, if any
1722 var n_type
: nullable AType = null is writable
1724 # The `...` symbol to indicate varargs
1725 var n_dotdotdot
: nullable TDotdotdot = null is writable
1728 # A static type. eg `nullable X[Y]`
1731 # The `nullable` keyword
1732 var n_kwnullable
: nullable TKwnullable = null is writable
1734 # The name of the class or of the formal type
1735 var n_qid
: AQclassid is writable, noinit
1737 # The opening bracket
1738 var n_obra
: nullable TObra = null is writable
1740 # Type arguments for a generic type
1741 var n_types
= new ANodes[AType](self)
1743 # The closing bracket
1744 var n_cbra
: nullable TCbra = null is writable
1747 # A label at the end of a block or in a break/continue statement. eg `label x`
1751 # The `label` keyword
1752 var n_kwlabel
: TKwlabel is writable, noinit
1754 # The name of the label, if any
1755 var n_id
: nullable TId is writable, noinit
1758 # Expression and statements
1759 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1760 abstract class AExpr
1764 # A sequence of `AExpr` (usually statements)
1765 # The last `AExpr` gives the value of the whole block
1769 # The list of statements in the bloc.
1770 # The last element is often considered as an expression that give the value of the whole block.
1771 var n_expr
= new ANodes[AExpr](self)
1774 var n_kwend
: nullable TKwend = null is writable
1777 # A declaration of a local variable. eg `var x: X = y`
1782 var n_kwvar
: nullable TKwvar = null is writable
1784 # The name of the local variable
1785 var n_id
: TId is writable, noinit
1787 # The declaration type of the local variable
1788 var n_type
: nullable AType = null is writable
1790 # The `=` symbol (for the initial value)
1791 var n_assign
: nullable TAssign = null is writable
1793 # The initial value, if any
1794 var n_expr
: nullable AExpr = null is writable
1797 # A `return` statement. eg `return x`
1801 # The `return` keyword
1802 var n_kwreturn
: nullable TKwreturn = null is writable
1805 # A `yield` statement. eg `yield x`
1809 # The `yield` keyword
1810 var n_kwyield
: nullable TKwyield = null is writable
1812 # The return value, if any
1813 var n_expr
: nullable AExpr = null is writable
1816 # Something that has a label.
1817 abstract class ALabelable
1820 # The associated label declatation
1821 var n_label
: nullable ALabel = null is writable
1824 # A `break` or a `continue`
1825 abstract class AEscapeExpr
1829 # The return value, if nay (unused currently)
1830 var n_expr
: nullable AExpr = null is writable
1833 # A `break` statement.
1837 # The `break` keyword
1838 var n_kwbreak
: TKwbreak is writable, noinit
1841 # An `abort` statement
1845 # The `abort` keyword
1846 var n_kwabort
: TKwabort is writable, noinit
1849 # A `continue` statement
1853 # The `continue` keyword.
1854 var n_kwcontinue
: nullable TKwcontinue = null is writable
1863 var n_kwdo
: TKwdo is writable, noinit
1865 # The list of statements of the `do`.
1866 var n_block
: nullable AExpr = null is writable
1868 # The `catch` keyword
1869 var n_kwcatch
: nullable TKwcatch = null is writable
1871 # The do catch block
1872 var n_catch
: nullable AExpr = null is writable
1880 var n_kwif
: TKwif is writable, noinit
1882 # The expression used as the condition of the `if`
1883 var n_expr
: AExpr is writable, noinit
1885 # The `then` keyword
1886 var n_kwthen
: TKwthen is writable, noinit
1888 # The body of the `then` part
1889 var n_then
: nullable AExpr = null is writable
1891 # The `else` keyword
1892 var n_kwelse
: nullable TKwelse = null is writable
1894 # The body of the `else` part
1895 var n_else
: nullable AExpr = null is writable
1898 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1903 var n_kwif
: TKwif is writable, noinit
1905 # The expression used as the condition of the `if`
1906 var n_expr
: AExpr is writable, noinit
1908 # The `then` keyword
1909 var n_kwthen
: TKwthen is writable, noinit
1911 # The expression in the `then` part
1912 var n_then
: AExpr is writable, noinit
1914 # The `else` keyword
1915 var n_kwelse
: TKwelse is writable, noinit
1917 # The expression in the `else` part
1918 var n_else
: AExpr is writable, noinit
1921 # A `while` statement
1926 # The `while` keyword
1927 var n_kwwhile
: TKwwhile is writable, noinit
1929 # The expression used as the condition of the `while`
1930 var n_expr
: AExpr is writable, noinit
1933 var n_kwdo
: TKwdo is writable, noinit
1935 # The body of the loop
1936 var n_block
: nullable AExpr = null is writable
1939 # A `loop` statement
1944 # The `loop` keyword
1945 var n_kwloop
: TKwloop is writable, noinit
1947 # The body of the loop
1948 var n_block
: nullable AExpr = null is writable
1957 var n_kwfor
: TKwfor is writable, noinit
1959 # The list of groups to iterate
1960 var n_groups
= new ANodes[AForGroup](self)
1963 var n_kwdo
: TKwdo is writable, noinit
1965 # The body of the loop
1966 var n_block
: nullable AExpr = null is writable
1969 # A collection iterated by a for, its automatic variables and its implicit iterator.
1971 # Standard `for` iterate on a single collection.
1972 # Multiple `for` can iterate on more than one collection at once.
1976 # The list of name of the automatic variables
1977 var n_ids
= new ANodes[TId](self)
1980 var n_kwin
: TKwin is writable, noinit
1982 # The expression used as the collection to iterate on
1983 var n_expr
: AExpr is writable, noinit
1986 # A `with` statement
1991 # The `with` keyword
1992 var n_kwwith
: TKwwith is writable, noinit
1994 # The expression used to get the value to control
1995 var n_expr
: AExpr is writable, noinit
1998 var n_kwdo
: TKwdo is writable, noinit
2000 # The body of the loop
2001 var n_block
: nullable AExpr = null is writable
2004 # An `assert` statement
2008 # The `assert` keyword
2009 var n_kwassert
: TKwassert is writable, noinit
2011 # The name of the assert, if any
2012 var n_id
: nullable TId = null is writable
2014 # The expression used as the condition of the `assert`
2015 var n_expr
: AExpr is writable, noinit
2017 # The `else` keyword
2018 var n_kwelse
: nullable TKwelse = null is writable
2020 # The body to execute when the assert fails
2021 var n_else
: nullable AExpr = null is writable
2024 # Whatever is a simple assignment. eg `= something`
2025 abstract class AAssignFormExpr
2029 var n_assign
: TAssign is writable, noinit
2031 # The right-value to assign.
2032 var n_value
: AExpr is writable, noinit
2035 # Whatever is a combined assignment. eg `+= something`
2036 abstract class AReassignFormExpr
2039 # The combined operator (eg. `+=`)
2040 var n_assign_op
: AAssignOp is writable, noinit
2042 # The right-value to apply on the combined operator.
2043 var n_value
: AExpr is writable, noinit
2046 # A `once` expression. eg `once x`
2050 # The `once` keyword
2051 var n_kwonce
: TKwonce is writable, noinit
2053 # The expression to evaluate only one time
2054 var n_expr
: AExpr is writable, noinit
2057 # A polymorphic invocation of a method
2058 # The form of the invocation (name, arguments, etc.) are specific
2059 abstract class ASendExpr
2061 # The receiver of the method invocation
2062 var n_expr
: AExpr is writable, noinit
2065 # A binary operation on a method
2066 abstract class ABinopExpr
2070 var n_op
: Token is writable, noinit
2072 # The second operand of the operation
2073 # Note: the receiver (`n_expr`) is the first operand
2074 var n_expr2
: AExpr is writable, noinit
2076 # The name of the operator (eg '+')
2077 fun operator
: String is abstract
2080 # Something that is boolean expression
2081 abstract class ABoolExpr
2085 # Something that is binary boolean expression
2086 abstract class ABinBoolExpr
2089 # The first boolean operand
2090 var n_expr
: AExpr is writable, noinit
2093 var n_op
: Token is writable, noinit
2095 # The second boolean operand
2096 var n_expr2
: AExpr is writable, noinit
2104 # A `and` expression
2109 # A `or else` expression
2113 # The `else` keyword
2114 var n_kwelse
: TKwelse is writable, noinit
2117 # A `implies` expression
2122 # A `not` expression
2127 var n_kwnot
: TKwnot is writable, noinit
2129 # The boolean operand of the `not`
2130 var n_expr
: AExpr is writable, noinit
2133 # A `==` or a `!=` expression
2135 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2143 redef fun operator
do return "=="
2149 redef fun operator
do return "!="
2155 redef fun operator
do return "<"
2161 redef fun operator
do return "<="
2167 redef fun operator
do return "<<"
2173 redef fun operator
do return ">"
2179 redef fun operator
do return ">="
2185 redef fun operator
do return ">>"
2188 # A type-ckeck expression. eg `x isa T`
2192 # The expression to check
2193 var n_expr
: AExpr is writable, noinit
2196 var n_kwisa
: TKwisa is writable, noinit
2198 # The destination type to check to
2199 var n_type
: AType is writable, noinit
2205 redef fun operator
do return "+"
2211 redef fun operator
do return "-"
2214 # A `<=>` expression
2217 redef fun operator
do return "<=>"
2223 redef fun operator
do return "*"
2229 redef fun operator
do return "**"
2235 redef fun operator
do return "/"
2241 redef fun operator
do return "%"
2247 redef fun operator
do return "|"
2253 redef fun operator
do return "^"
2259 redef fun operator
do return "&"
2262 # A unary operation on a method
2263 abstract class AUnaryopExpr
2267 var n_op
: Token is writable, noinit
2269 # The name of the operator (eg '+')
2270 fun operator
: String is abstract
2273 # A unary minus expression. eg `-x`
2276 redef fun operator
do return "-"
2279 # A unary plus expression. eg `+x`
2282 redef fun operator
do return "+"
2285 # A unary `~` expression
2288 redef fun operator
do return "~"
2291 # An explicit instantiation. eg `new T`
2296 var n_kwnew
: TKwnew is writable, noinit
2298 # The `type` keyword
2299 var n_type
: AType is writable, noinit
2301 # The name of the named-constructor, if any
2302 var n_qid
: nullable AQid = null is writable
2304 # The arguments of the `new`
2305 var n_args
: AExprs is writable, noinit
2308 # Whatever is a old-style attribute access
2309 abstract class AAttrFormExpr
2312 # The receiver of the attribute
2313 var n_expr
: AExpr is writable, noinit
2315 # The name of the attribute
2316 var n_id
: TAttrid is writable, noinit
2320 # The read of an attribute. eg `x._a`
2325 # The assignment of an attribute. eg `x._a=y`
2326 class AAttrAssignExpr
2328 super AAssignFormExpr
2331 # Whatever looks-like a call with a standard method and any number of arguments.
2332 abstract class ACallFormExpr
2335 # The name of the method
2336 var n_qid
: AQid is writable, noinit
2338 # The arguments of the call
2339 var n_args
: AExprs is writable, noinit
2342 # A complex setter call (standard or brackets)
2343 abstract class ASendReassignFormExpr
2345 super AReassignFormExpr
2348 # A complex attribute assignment. eg `x._a+=y`
2349 class AAttrReassignExpr
2351 super AReassignFormExpr
2354 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2355 # 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`.
2356 # Semantic analysis have to transform them to instance of `AVarExpr`.
2361 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2362 # 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`.
2363 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2364 class ACallAssignExpr
2366 super AAssignFormExpr
2369 # 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.
2370 # 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`.
2371 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2372 class ACallReassignExpr
2374 super ASendReassignFormExpr
2377 # A call to `super`. OR a call of a super-constructor
2381 # The qualifier part before the super (currenlty unused)
2382 var n_qualified
: nullable AQualified = null is writable
2384 # The `super` keyword
2385 var n_kwsuper
: TKwsuper is writable, noinit
2387 # The arguments of the super
2388 var n_args
: AExprs is writable, noinit
2391 # A call to the `init` constructor.
2392 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2396 # The `init` keyword
2397 var n_kwinit
: TKwinit is writable, noinit
2399 # The arguments of the init
2400 var n_args
: AExprs is writable, noinit
2403 # Whatever looks-like a call of the brackets `[]` operator.
2404 abstract class ABraFormExpr
2407 # The arguments inside the brackets
2408 var n_args
: AExprs is writable, noinit
2411 # A call of the brackets operator. eg `x[y,z]`
2416 # A setter call of the bracket operator. eg `x[y,z]=t`
2417 class ABraAssignExpr
2419 super AAssignFormExpr
2422 # Whatever is an access to a local variable
2423 abstract class AVarFormExpr
2426 # The name of the attribute
2427 var n_id
: TId is writable, noinit
2430 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2431 class ABraReassignExpr
2433 super ASendReassignFormExpr
2436 # A local variable read access.
2437 # The parser cannot instantiate them, see `ACallExpr`.
2442 # A local variable simple assignment access
2443 # The parser cannot instantiate them, see `ACallAssignExpr`.
2444 class AVarAssignExpr
2446 super AAssignFormExpr
2449 # A local variable complex assignment access
2450 # The parser cannot instantiate them, see `ACallReassignExpr`.
2451 class AVarReassignExpr
2453 super AReassignFormExpr
2456 # A literal range, open or closed
2457 abstract class ARangeExpr
2460 # The left (lower) element of the range
2461 var n_expr
: AExpr is writable, noinit
2464 var n_dotdot
: TDotdot is writable, noinit
2466 # The right (upper) element of the range
2467 var n_expr2
: AExpr is writable, noinit
2470 # A closed literal range. eg `[x..y]`
2474 # The opening bracket `[`
2475 var n_obra
: TObra is writable, noinit
2477 # The closing bracket `]`
2478 var n_cbra
: TCbra is writable, noinit
2481 # An open literal range. eg `[x..y[`
2485 # The opening bracket `[`
2486 var n_obra
: TObra is writable, noinit
2488 # The closing bracket `[` (because open range)
2489 var n_cbra
: TObra is writable, noinit
2492 # A literal array. eg. `[x,y,z]`
2496 # The opening bracket `[`
2497 var n_obra
: TObra is writable, noinit
2499 # The elements of the array
2500 var n_exprs
= new ANodes[AExpr](self)
2502 # The type of the element of the array (if any)
2503 var n_type
: nullable AType = null is writable
2505 # The closing bracket `]`
2506 var n_cbra
: TCbra is writable, noinit
2513 # The `self` keyword
2514 var n_kwself
: nullable TKwself = null is writable
2517 # When there is no explicit receiver, `self` is implicit
2518 class AImplicitSelfExpr
2522 # A `true` boolean literal constant
2526 # The `true` keyword
2527 var n_kwtrue
: TKwtrue is writable, noinit
2530 # A `false` boolean literal constant
2534 # The `false` keyword
2535 var n_kwfalse
: TKwfalse is writable, noinit
2538 # A `null` literal constant
2542 # The `null` keyword
2543 var n_kwnull
: TKwnull is writable, noinit
2546 # An integer literal
2551 var n_integer
: TInteger is writable, noinit
2559 var n_float
: TFloat is writable, noinit
2562 # A character literal
2566 # The character token
2567 var n_char
: TChar is writable, noinit
2571 abstract class AStringFormExpr
2575 var n_string
: Token is writable, noinit
2578 # A simple string. eg. `"abc"`
2580 super AStringFormExpr
2583 # The start of a superstring. eg `"abc{`
2584 class AStartStringExpr
2585 super AStringFormExpr
2588 # The middle of a superstring. eg `}abc{`
2589 class AMidStringExpr
2590 super AStringFormExpr
2593 # The end of a superstrng. eg `}abc"`
2594 class AEndStringExpr
2595 super AStringFormExpr
2598 # A superstring literal. eg `"a{x}b{y}c"`
2599 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2600 class ASuperstringExpr
2603 # The list of the expressions of the superstring
2604 var n_exprs
= new ANodes[AExpr](self)
2607 # A simple parenthesis. eg `(x)`
2611 # The opening parenthesis
2612 var n_opar
: TOpar is writable, noinit
2614 # The inner expression
2615 var n_expr
: AExpr is writable, noinit
2617 # The closing parenthesis
2618 var n_cpar
: TCpar is writable, noinit
2621 # A cast, against a type or `not null`
2625 # The expression to cast
2626 var n_expr
: AExpr is writable, noinit
2629 var n_kwas
: TKwas is writable, noinit
2631 # The opening parenthesis
2632 var n_opar
: nullable TOpar = null is writable
2634 # The closing parenthesis
2635 var n_cpar
: nullable TCpar = null is writable
2638 # A type cast. eg `x.as(T)`
2642 # The target type to cast to
2643 var n_type
: AType is writable, noinit
2646 # A as-not-null cast. eg `x.as(not null)`
2647 class AAsNotnullExpr
2651 var n_kwnot
: TKwnot is writable, noinit
2653 # The `null` keyword
2654 var n_kwnull
: TKwnull is writable, noinit
2657 # A is-set check of old-style attributes. eg `isset x._a`
2658 class AIssetAttrExpr
2661 # The `isset` keyword
2662 var n_kwisset
: TKwisset is writable, noinit
2665 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2669 # The passed expression
2670 var n_expr
: AExpr is writable, noinit
2673 var n_dotdotdot
: TDotdotdot is writable, noinit
2676 # An named notation used to pass an expression by name in a parameter
2680 # The name of the argument
2681 var n_id
: TId is writable, noinit
2684 var n_assign
: TAssign is writable, noinit
2686 # The passed expression
2687 var n_expr
: AExpr is writable, noinit
2690 # A list of expression separated with commas (arguments for instance)
2694 # The list of expressions
2695 var n_exprs
= new ANodes[AExpr](self)
2698 # A special expression that encapsulates a static type
2699 # Can only be found in special construction like arguments of annotations.
2703 # The encapsulated type
2704 var n_type
: AType is writable, noinit
2707 # A special expression that encapsulates a method identifier
2708 # Can only be found in special construction like arguments of annotations.
2713 var n_expr
: AExpr is writable, noinit
2715 # The encapsulated method identifier
2716 var n_id
: AMethid is writable, noinit
2719 # A special expression that encapsulate an annotation
2720 # Can only be found in special construction like arguments of annotations.
2722 # The encapsulated annotations are in `n_annotations`
2727 # A special expression to debug types
2728 class ADebugTypeExpr
2731 # The `debug` keyword
2732 var n_kwdebug
: TKwdebug is writable, noinit
2734 # The `type` keyword
2735 var n_kwtype
: TKwtype is writable, noinit
2737 # The expression to check
2738 var n_expr
: AExpr is writable, noinit
2741 var n_type
: AType is writable, noinit
2744 # A list of expression separated with commas (arguments for instance)
2745 abstract class AExprs
2748 # The list of expressions
2749 var n_exprs
= new ANodes[AExpr](self)
2752 # A simple list of expressions
2757 # A list of expressions enclosed in parentheses
2761 # The opening parenthesis
2762 var n_opar
: TOpar is writable, noinit
2764 # The closing parenthesis
2765 var n_cpar
: TCpar is writable, noinit
2768 # A list of expressions enclosed in brackets
2772 # The opening bracket
2773 var n_obra
: TObra is writable, noinit
2775 # The closing bracket
2776 var n_cbra
: TCbra is writable, noinit
2779 # A complex assignment operator. (`+=` and `-=`)
2780 abstract class AAssignOp
2783 # The combined assignment operator
2784 var n_op
: Token is writable, noinit
2786 # The name of the operator without the `=` (eg '+')
2787 fun operator
: String is abstract
2790 # A `+=` assignment operation
2794 redef fun operator
do return "+"
2797 # A `-=` assignment operation
2798 class AMinusAssignOp
2801 redef fun operator
do return "-"
2804 # A `*=` assignment operation
2808 redef fun operator
do return "*"
2811 # A `/=` assignment operation
2812 class ASlashAssignOp
2815 redef fun operator
do return "/"
2818 # A `%=` assignment operation
2819 class APercentAssignOp
2822 redef fun operator
do return "%"
2825 # A `**=` assignment operation
2826 class AStarstarAssignOp
2829 redef fun operator
do return "**"
2832 # A `|=` assignment operation
2836 redef fun operator
do return "|"
2839 # A `^=` assignment operation
2840 class ACaretAssignOp
2843 redef fun operator
do return "^"
2846 # A `&=` assignment operation
2850 redef fun operator
do return "&"
2853 # A `<<=` assignment operation
2857 redef fun operator
do return "<<"
2860 # A `>>=` assignment operation
2864 redef fun operator
do return ">>"
2867 # A possibly fully-qualified module identifier
2871 # The starting quad (`::`)
2872 var n_quad
: nullable TQuad = null is writable
2874 # The list of quad-separated package/group identifiers
2875 var n_path
= new ANodes[TId](self)
2877 # The final module identifier
2878 var n_id
: TId is writable, noinit
2881 # A language declaration for an extern block
2886 var n_kwin
: TKwin is writable, noinit
2889 var n_string
: TString is writable, noinit
2892 # An full extern block
2893 class AExternCodeBlock
2896 # The language declration
2897 var n_in_language
: nullable AInLanguage = null is writable
2899 # The block of extern code
2900 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2903 # A possible full method qualifier.
2907 # The starting quad (`::`)
2908 var n_quad
: nullable TQuad = null is writable
2910 # The list of quad-separated package/group/module identifiers
2911 var n_id
= new ANodes[TId](self)
2913 # A class identifier
2914 var n_classid
: nullable TClassid = null is writable
2917 # A documentation of a definition
2918 # It contains the block of comments just above the declaration
2922 # A list of lines of comment
2923 var n_comment
= new ANodes[TComment](self)
2926 # A group of annotation on a node
2928 # This same class is used for the 3 kind of annotations:
2930 # * *is* annotations. eg `module foo is bar`.
2931 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2932 # * *class* annotations, defined in classes.
2936 # The `is` keyword, for *is* annotations
2937 var n_kwis
: nullable TKwis = null is writable
2939 # The `@` symbol, for *at* annotations
2940 var n_at
: nullable TAt = null is writable
2942 # The opening parenthesis in *at* annotations
2943 var n_opar
: nullable TOpar = null is writable
2945 # The list of annotations
2946 var n_items
= new ANodes[AAnnotation](self)
2948 # The closing parenthesis in *at* annotations
2949 var n_cpar
: nullable TCpar = null is writable
2951 # The `end` keyword, for *is* annotations
2952 var n_kwend
: nullable TKwend = null is writable
2955 # A single annotation
2959 # The name of the annotation
2960 var n_atid
: AAtid is writable, noinit
2962 # The opening parenthesis of the arguments
2963 var n_opar
: nullable TOpar = null is writable
2965 # The list of arguments
2966 var n_args
= new ANodes[AExpr](self)
2968 # The closing parenthesis
2969 var n_cpar
: nullable TCpar = null is writable
2971 # The name of the annotation
2974 return n_atid
.n_id
.text
2978 # An annotation name
2979 abstract class AAtid
2982 # The identifier of the annotation.
2983 # Can be a TId of a keyword
2984 var n_id
: Token is writable, noinit
2987 # An annotation name based on an identifier
2992 # An annotation name based on the keyword `extern`
2997 # An annotation name based on the keyword `import`
3002 # An annotation name based on the keyword `abstract`
3003 class AKwabstractAtid
3007 # The root of the AST
3012 var n_base
: nullable AModule is writable
3014 # The end of file (or error) token
3015 var n_eof
: EOF is writable