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.
21 private import console
23 # Root of the AST class-hierarchy
25 # Location is set during AST building. Once built, location cannon be null.
26 # However, manual instantiated nodes may need more care.
27 var location
: Location is writable, noinit
29 # The location of the important part of the node (identifier or whatever)
30 fun hot_location
: Location do return location
32 # Display a message for the colored location of the node
33 fun debug
(message
: String)
35 sys
.stderr
.write
"{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
38 # Is `self` a token or a pure-structural production like `AQId`?
39 fun is_structural
: Bool do return false
41 # Write the subtree on stdout.
43 fun dump_tree
(display_structural
: nullable Bool)
45 var d
= new ASTDump(display_structural
or else true)
47 d
.write_to
(sys
.stdout
)
50 # Information to display on a node
52 # Refine this method to add additional information on each node type.
53 fun dump_info
(v
: ASTDump): String do return ""
55 # Parent of the node in the AST
56 var parent
: nullable ANode = null
58 # The topmost ancestor of the element
59 # This just apply `parent` until the first one
65 if p
== null then return res
70 # The most specific common parent between `self` and `other`
71 # Return null if the two node are unrelated (distinct root)
72 fun common_parent
(other
: ANode): nullable ANode
74 # First, get the same depth
75 var s
: nullable ANode = self
76 var o
: nullable ANode = other
77 var d
= s
.depth
- o
.depth
86 assert o
.depth
== s
.depth
87 # Second, go up until same in found
95 # Number of nodes between `self` and the `root` of the AST
96 # ENSURE `self == self.root implies result == 0 `
97 # ENSURE `self != self.root implies result == self.parent.depth + 1`
104 if p
== null then return res
110 # Replace a child with an other node in the AST
111 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode) is abstract
113 # Detach a node from its parent
114 # Aborts if the node is not detachable. use `replace_with` instead
115 # REQUIRE: parent != null
116 # REQUIRE: is_detachable
117 # ENDURE: parent == null
120 assert parent
!= null
121 parent
.replace_child
(self, null)
125 # Replace itself with an other node in the AST
126 # REQUIRE: parent != null
127 # ENSURE: node.parent == old(parent)
128 # ENSURE: parent == null
129 fun replace_with
(node
: ANode)
131 assert parent
!= null
132 parent
.replace_child
(self, node
)
136 # Visit all nodes in order.
137 # Thus, call `v.enter_visit(e)` for each child `e`
138 fun visit_all
(v
: Visitor) is abstract
140 # Do a deep search and return an array of tokens that match a given text
141 fun collect_tokens_by_text
(text
: String): Array[Token]
143 var v
= new CollectTokensByTextVisitor(text
)
148 # Do a deep search and return an array of node that are annotated
149 # The attached node can be retrieved by two invocations of parent
150 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
152 var v
= new CollectAnnotationsByNameVisitor(name
)
158 private class CollectTokensByTextVisitor
161 var result
= new Array[Token]
162 redef fun visit
(node
)
165 if node
isa Token and node
.text
== text
then result
.add
(node
)
169 private class CollectAnnotationsByNameVisitor
172 var result
= new Array[AAnnotation]
173 redef fun visit
(node
)
176 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)
180 # A helper class to handle (print) Nit AST as an OrderedTree
183 super OrderedTree[ANode]
185 # Reference to the last parent in the Ordered Tree
186 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
187 private var last_parent
: nullable ANode = null
189 # Display tokens and structural production?
191 # Should tokens (and structural production like AQId) be displayed?
192 var display_structural
: Bool
196 if not display_structural
and n
.is_structural
then return
206 return "{n.class_name} {n.dump_info(self)} @{n.location}"
210 fun yellow
(s
: String): String do return s
.yellow
213 fun red
(s
: String): String do return s
.red
216 # A sequence of nodes
217 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
218 class ANodes[E
: ANode]
220 private var parent
: ANode
221 private var items
= new Array[E
]
222 redef fun iterator
do return items
.iterator
223 redef fun reverse_iterator
do return items
.reverse_iterator
224 redef fun length
do return items
.length
225 redef fun is_empty
do return items
.is_empty
244 var res
= items
.shift
256 redef fun []=(index
, e
)
258 hook_remove
(self[index
])
262 redef fun remove_at
(index
)
264 hook_remove
(items
[index
])
265 items
.remove_at
(index
)
267 private fun hook_add
(e
: E
)
269 #assert e.parent == null
272 private fun hook_remove
(e
: E
)
274 assert e
.parent
== parent
278 # Used in parent constructor to fill elements
279 private fun unsafe_add_all
(nodes
: Collection[Object])
281 var parent
= self.parent
289 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
291 var parent
= self.parent
292 for i
in [0..length
[ do
293 if self[i
] == old_child
then
294 if new_child
!= null then
295 assert new_child
isa E
297 new_child
.parent
= parent
307 private fun visit_all
(v
: Visitor)
309 for n
in self do v
.enter_visit
(n
)
313 # Ancestor of all tokens
314 # A token is a node that has a `text` but no children.
318 # The raw content on the token
319 fun text
: String is abstract
321 # The raw content on the token
322 fun text
=(text
: String) is abstract
324 # The previous token in the Lexer.
325 # May have disappeared in the AST
326 var prev_token
: nullable Token = null
328 # The next token in the Lexer.
329 # May have disappeared in the AST
330 var next_token
: nullable Token = null
332 # Is `self` a token discarded from the AST?
334 # Loose tokens are not present in the AST.
335 # It means they were identified by the lexer but were discarded by the parser.
336 # It also means that they are not visited or manipulated by AST-related functions.
338 # Each loose token is attached to the non-loose token that precedes or follows it.
339 # The rules are the following:
341 # * tokens that follow a non-loose token on a same line are attached to it.
343 # * other tokens, thus that precede a non-loose token on the same line or the next one,
344 # are attached to this one. See `prev_looses`.
346 # Loose tokens are mostly end of lines (`TEol`) and comments (`TComment`).
347 # Whitespace are ignored by the lexer, so they are not even considered as loose tokens.
348 # See `blank_before` to get the whitespace that separate tokens.
351 redef fun is_structural
do return true
353 redef fun dump_info
(v
) do return " {text.escape_to_c}"
355 # Loose tokens that precede `self`.
357 # These tokens start the line or belong to a line with only loose tokens.
358 var prev_looses
= new Array[Token] is lazy
360 # Loose tokens that follow `self`
362 # These tokens are on the same line than `self`.
363 var next_looses
= new Array[Token] is lazy
365 # The verbatim blank text between `prev_token` and `self`
366 fun blank_before
: String
368 if prev_token
== null then return ""
369 var from
= prev_token
.location
.pend
+1
370 var to
= location
.pstart
371 return location
.file
.string
.substring
(from
,to-from
)
374 redef fun to_s
: String do
378 redef fun visit_all
(v
: Visitor) do end
379 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
382 redef class SourceFile
383 # The first token parser by the lexer
384 # May have disappeared in the final AST
385 var first_token
: nullable Token = null
387 # The first token parser by the lexer
388 # May have disappeared in the final AST
389 var last_token
: nullable Token = null
392 # Ancestor of all productions
393 # A production is a node without text but that usually has children.
397 # All the annotations attached directly to the node
398 var n_annotations
: nullable AAnnotations = null is writable
400 # Return all its annotations of a given name in the order of their declaration
401 # Retun an empty array if no such an annotation.
402 fun get_annotations
(name
: String): Array[AAnnotation]
404 var res
= new Array[AAnnotation]
405 var nas
= n_annotations
406 if nas
!= null then for na
in nas
.n_items
do
407 if na
.name
!= name
then continue
410 if self isa AClassdef then for na
in n_propdefs
do
411 if na
isa AAnnotPropdef then
412 if na
.name
!= name
then continue
420 redef fun replace_with
(n
: ANode)
424 if not isset n
._location
and isset _location
then n
._location
= _location
428 # Abstract standard visitor on the AST
429 abstract class Visitor
430 # What the visitor do when a node is visited
431 # Concrete visitors should implement this method.
433 protected fun visit
(e
: ANode) is abstract
435 # Ask the visitor to visit a given node.
436 # Usually automatically called by visit_all* methods.
437 # This method should not be redefined
438 fun enter_visit
(e
: nullable ANode)
440 if e
== null then return
441 var old
= _current_node
447 # The current visited node
448 var current_node
: nullable ANode = null is writable
451 # Token of end of line (basically `\n`)
460 # Token of a line of comments
461 # Starts with the `#` and contains the final end-of-line (if any)
466 # A token associated with a keyword
467 abstract class TokenKeyword
471 return "keyword '{text}'"
475 # The deprecated keyword `package`.
480 # The keyword `module`
485 # The keyword `import`
490 # The keyword `class`
495 # The keyword `abstract`
500 # The keyword `interface`
505 # The keywords `enum` ane `universal`
530 # The keyword `redef`
545 # The keyword `catch`
555 # The keyword `extern`
560 # The keyword `public`
565 # The keyword `protected`
570 # The keyword `private`
575 # The keyword `intrude`
595 # The keyword `while`
625 # The keyword `implies`
635 # The keyword `return`
640 # The keyword `continue`
645 # The keyword `break`
650 # The keyword `abort`
655 # The keyword `assert`
675 # The keyword `super`
690 # The keyword `false`
705 # The keyword `nullable`
710 # The keyword `isset`
715 # The keyword `label`
725 # The keyword `yield`
730 # The special keyword `__DEBUG__`
775 # A token associated with an operator (and other lookalike symbols)
776 abstract class TokenOperator
780 return "operator '{text}'"
964 # A class (or formal type) identifier. They start with an uppercase.
969 do return "type identifier '{text}'"
973 # A standard identifier (variable, method...). They start with a lowercase.
978 do return "identifier '{text}'"
982 # An attribute identifier. They start with an underscore.
987 do return "attribute '{text}'"
991 # A token of a literal value (string, integer, etc).
992 abstract class TokenLiteral
996 do return "literal value '{text}'"
1005 # A literal floating point number
1010 # A literal character
1020 # The starting part of a super string (between `"` and `{`)
1025 # The middle part of a super string (between `}` and `{`)
1030 # The final part of a super string (between `}` and `"`)
1035 # A malformed string
1040 do return "malformed string {text}"
1049 do return "malformed character {text}"
1053 # A extern code block
1054 class TExternCodeSegment
1063 return "end of file"
1067 # A mark of an error
1071 # A lexical error (unexpected character)
1075 # A syntactic error (unexpected token)
1080 # The main node of a Nit source-file
1084 # The declaration part of the module
1085 var n_moduledecl
: nullable AModuledecl = null is writable
1087 # List of importation clauses
1088 var n_imports
= new ANodes[AImport](self)
1090 # List of extern blocks
1091 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1093 # List of class definition (including top-level methods and the main)
1094 var n_classdefs
= new ANodes[AClassdef](self)
1097 # Abstract class for definition of entities
1098 abstract class ADefinition
1101 var n_doc
: nullable ADoc = null is writable
1103 # The `redef` keyword
1104 var n_kwredef
: nullable TKwredef = null is writable
1106 # The declared visibility
1107 var n_visibility
: nullable AVisibility = null is writable
1110 # The declaration of the module with the documentation, name, and annotations
1114 # The `module` keyword
1115 var n_kwmodule
: TKwmodule is writable, noinit
1117 # The declared module name
1118 var n_name
: AModuleName is writable, noinit
1121 # A import clause of a module
1122 abstract class AImport
1125 # The declared visibility
1126 var n_visibility
: AVisibility is writable, noinit
1128 # The `import` keyword
1129 var n_kwimport
: TKwimport is writable, noinit
1132 # A standard import clause. eg `import x`
1135 # The imported module name
1136 var n_name
: AModuleName is writable, noinit
1139 # The special import clause of the kernel module. eg `import end`
1142 # The `end` keyword, that indicate the root module
1143 var n_kwend
: TKwend is writable, noinit
1146 # A visibility modifier
1148 # The public visibility is an empty production (no keyword).
1150 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1151 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1152 abstract class AVisibility
1156 # An implicit or explicit public visibility modifier
1157 class APublicVisibility
1159 # The `public` keyword, if any
1160 var n_kwpublic
: nullable TKwpublic = null is writable
1162 # An explicit private visibility modifier
1163 class APrivateVisibility
1165 # The `private` keyword
1166 var n_kwprivate
: TKwprivate is writable, noinit
1168 # An explicit protected visibility modifier
1169 class AProtectedVisibility
1171 # The `protected` keyword
1172 var n_kwprotected
: TKwprotected is writable, noinit
1174 # An explicit intrude visibility modifier
1175 class AIntrudeVisibility
1177 # The `intrude` keyword
1178 var n_kwintrude
: TKwintrude is writable, noinit
1181 # A class definition
1182 # While most definition are `AStdClassdef`
1183 # There is tow special case of class definition
1184 abstract class AClassdef
1186 # All the declared properties (including the main method)
1187 var n_propdefs
= new ANodes[APropdef](self)
1190 # A standard class definition with a name, superclasses and properties
1195 # The class kind (interface, abstract class, etc.)
1196 var n_classkind
: AClasskind is writable, noinit
1198 # The name of the class
1199 var n_qid
: nullable AQclassid = null is writable
1202 var n_obra
: nullable TObra = null is writable
1204 # The list of formal parameter types
1205 var n_formaldefs
= new ANodes[AFormaldef](self)
1208 var n_cbra
: nullable TCbra = null is writable
1210 # The extern block code
1211 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1214 var n_kwend
: TKwend is writable, noinit
1216 fun n_superclasses
: Array[ASuperPropdef] do
1217 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1220 redef fun hot_location
do return n_qid
.location
1223 # The implicit class definition of the implicit main method
1228 # The implicit class definition of the top-level methods
1233 # The modifier for the kind of class (abstract, interface, etc.)
1234 abstract class AClasskind
1238 # A default, or concrete class modifier (just `class`)
1239 class AConcreteClasskind
1242 # The `class` keyword.
1243 var n_kwclass
: TKwclass is writable, noinit
1246 # An abstract class modifier (`abstract class`)
1247 class AAbstractClasskind
1250 # The `abstract` keyword.
1251 var n_kwabstract
: TKwabstract is writable, noinit
1253 # The `class` keyword.
1254 var n_kwclass
: TKwclass is writable, noinit
1257 # An interface class modifier (`interface`)
1258 class AInterfaceClasskind
1261 # The `interface` keyword.
1262 var n_kwinterface
: TKwinterface is writable, noinit
1265 # An enum/universal class modifier (`enum class`)
1266 class AEnumClasskind
1269 # The `enum` keyword.
1270 var n_kwenum
: TKwenum is writable, noinit
1273 # An extern class modifier (`extern class`)
1274 class AExternClasskind
1277 # The `extern` keyword.
1278 var n_kwextern
: TKwextern is writable, noinit
1280 # The `class` keyword.
1281 var n_kwclass
: nullable TKwclass = null is writable
1284 # The definition of a formal generic parameter type. eg `X: Y`
1288 # The name of the parameter type
1289 var n_id
: TClassid is writable, noinit
1291 # The bound of the parameter type
1292 var n_type
: nullable AType = null is writable
1295 # The definition of a property
1296 abstract class APropdef
1300 # A definition of an attribute
1301 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1306 var n_kwvar
: TKwvar is writable, noinit
1308 # The identifier for a new-style attribute
1309 var n_id2
: TId is writable, noinit
1311 # The declared type of the attribute
1312 var n_type
: nullable AType = null is writable
1315 var n_assign
: nullable TAssign = null is writable
1317 # The initial value, if any (set with `=`)
1318 var n_expr
: nullable AExpr = null is writable
1321 var n_kwdo
: nullable TKwdo = null is writable
1323 # The initial value, if any (set with `do return`)
1324 var n_block
: nullable AExpr = null is writable
1327 var n_kwend
: nullable TKwend = null is writable
1329 redef fun hot_location
1331 return n_id2
.location
1335 # A definition of all kind of method (including constructors)
1339 # The `fun` keyword, if any
1340 var n_kwmeth
: nullable TKwmeth = null is writable
1342 # The `init` keyword, if any
1343 var n_kwinit
: nullable TKwinit = null is writable
1345 # The `new` keyword, if any
1346 var n_kwnew
: nullable TKwnew = null is writable
1348 # The name of the method, if any
1349 var n_methid
: nullable AMethid = null is writable
1351 # The signature of the method, if any
1352 var n_signature
: nullable ASignature = null is writable
1355 var n_kwdo
: nullable TKwdo = null is writable
1357 # The body (in Nit) of the method, if any
1358 var n_block
: nullable AExpr = null is writable
1361 var n_kwend
: nullable TKwend = null is writable
1363 # The list of declared callbacks (for extern methods)
1364 var n_extern_calls
: nullable AExternCalls = null is writable
1366 # The body (in extern code) of the method, if any
1367 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1369 redef fun hot_location
1371 if n_methid
!= null then
1372 return n_methid
.location
1373 else if n_kwinit
!= null then
1374 return n_kwinit
.location
1375 else if n_kwnew
!= null then
1376 return n_kwnew
.location
1383 # The implicit main method
1384 class AMainMethPropdef
1393 # A super-class. eg `super X`
1398 var n_kwsuper
: TKwsuper is writable, noinit
1400 # The super-class (indicated as a type)
1401 var n_type
: AType is writable, noinit
1405 # Declaration of callbacks for extern methods
1409 # The `import` keyword
1410 var n_kwimport
: TKwimport is writable, noinit
1412 # The list of declared callbacks
1413 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1416 # A single callback declaration
1417 abstract class AExternCall
1421 # A single callback declaration on a method
1422 abstract class APropExternCall
1426 # A single callback declaration on a method on the current receiver
1427 class ALocalPropExternCall
1428 super APropExternCall
1430 # The name of the called-back method
1431 var n_methid
: AMethid is writable, noinit
1434 # A single callback declaration on a method on an explicit receiver type.
1435 class AFullPropExternCall
1436 super APropExternCall
1438 # The type of the receiver of the called-back method
1439 var n_type
: AType is writable, noinit
1442 var n_dot
: nullable TDot = null is writable
1444 # The name of the called-back method
1445 var n_methid
: AMethid is writable, noinit
1448 # A single callback declaration on a method on a constructor
1449 class AInitPropExternCall
1450 super APropExternCall
1452 # The allocated type
1453 var n_type
: AType is writable, noinit
1456 # A single callback declaration on a `super` call
1457 class ASuperExternCall
1460 # The `super` keyword
1461 var n_kwsuper
: TKwsuper is writable, noinit
1464 # A single callback declaration on a cast
1465 abstract class ACastExternCall
1469 # A single callback declaration on a cast to a given type
1470 class ACastAsExternCall
1471 super ACastExternCall
1473 # The origin type of the cast
1474 var n_from_type
: AType is writable, noinit
1477 var n_dot
: nullable TDot = null is writable
1480 var n_kwas
: TKwas is writable, noinit
1482 # The destination of the cast
1483 var n_to_type
: AType is writable, noinit
1486 # A single callback declaration on a cast to a nullable type
1487 class AAsNullableExternCall
1488 super ACastExternCall
1490 # The origin type to cast as nullable
1491 var n_type
: AType is writable, noinit
1494 var n_kwas
: TKwas is writable, noinit
1496 # The `nullable` keyword
1497 var n_kwnullable
: TKwnullable is writable, noinit
1500 # A single callback declaration on a cast to a non-nullable type
1501 class AAsNotNullableExternCall
1502 super ACastExternCall
1504 # The destination type on a cast to not nullable
1505 var n_type
: AType is writable, noinit
1508 var n_kwas
: TKwas is writable, noinit
1511 var n_kwnot
: TKwnot is writable, noinit
1513 # The `nullable` keyword
1514 var n_kwnullable
: TKwnullable is writable, noinit
1517 # A definition of a virtual type
1521 # The `type` keyword
1522 var n_kwtype
: TKwtype is writable, noinit
1524 # The name of the virtual type
1525 var n_qid
: AQclassid is writable, noinit
1527 # The bound of the virtual type
1528 var n_type
: AType is writable, noinit
1531 # The identifier of a method in a method declaration.
1532 # There is a specific class because of operator and setters.
1533 abstract class AMethid
1537 # A method name with a simple identifier
1541 # The simple identifier
1542 var n_id
: TId is writable, noinit
1545 # A method name for an operator
1546 class AOperatorMethid
1549 # The associated operator symbol
1550 var n_op
: Token is writable, noinit
1554 super AOperatorMethid
1559 super AOperatorMethid
1564 super AOperatorMethid
1567 # A method name `**`
1568 class AStarstarMethid
1569 super AOperatorMethid
1574 super AOperatorMethid
1578 class APercentMethid
1579 super AOperatorMethid
1584 super AOperatorMethid
1589 super AOperatorMethid
1594 super AOperatorMethid
1599 super AOperatorMethid
1602 # A method name `==`
1604 super AOperatorMethid
1607 # A method name `!=`
1609 super AOperatorMethid
1612 # A method name `<=`
1614 super AOperatorMethid
1617 # A method name `>=`
1619 super AOperatorMethid
1624 super AOperatorMethid
1629 super AOperatorMethid
1632 # A method name `<<`
1634 super AOperatorMethid
1637 # A method name `>>`
1639 super AOperatorMethid
1642 # A method name `<=>`
1643 class AStarshipMethid
1644 super AOperatorMethid
1647 # A method name `[]`
1652 var n_obra
: TObra is writable, noinit
1655 var n_cbra
: TCbra is writable, noinit
1658 # A setter method name with a simple identifier (with a `=`)
1662 # The base identifier
1663 var n_id
: TId is writable, noinit
1666 var n_assign
: TAssign is writable, noinit
1669 # A method name `[]=`
1670 class ABraassignMethid
1674 var n_obra
: TObra is writable, noinit
1677 var n_cbra
: TCbra is writable, noinit
1680 var n_assign
: TAssign is writable, noinit
1683 # A potentially qualified simple identifier `foo::bar::baz`
1686 # The qualifier, if any
1687 var n_qualified
: nullable AQualified = null is writable
1689 # The final identifier
1690 var n_id
: TId is writable, noinit
1692 redef fun is_structural
do return true
1695 # A potentially qualified class identifier `foo::bar::Baz`
1698 # The qualifier, if any
1699 var n_qualified
: nullable AQualified = null is writable
1701 # The final identifier
1702 var n_id
: TClassid is writable, noinit
1704 redef fun is_structural
do return true
1707 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1712 var n_opar
: nullable TOpar = null is writable
1714 # The list of parameters
1715 var n_params
= new ANodes[AParam](self)
1718 var n_cpar
: nullable TCpar = null is writable
1721 var n_type
: nullable AType = null is writable
1724 # A parameter definition in a signature. eg `x:X`
1728 # The name of the parameter
1729 var n_id
: TId is writable, noinit
1731 # The type of the parameter, if any
1732 var n_type
: nullable AType = null is writable
1734 # The `...` symbol to indicate varargs
1735 var n_dotdotdot
: nullable TDotdotdot = null is writable
1738 # A static type. eg `nullable X[Y]`
1741 # The `nullable` keyword
1742 var n_kwnullable
: nullable TKwnullable = null is writable
1744 # The name of the class or of the formal type
1745 var n_qid
: AQclassid is writable, noinit
1747 # The opening bracket
1748 var n_obra
: nullable TObra = null is writable
1750 # Type arguments for a generic type
1751 var n_types
= new ANodes[AType](self)
1753 # The closing bracket
1754 var n_cbra
: nullable TCbra = null is writable
1757 # A label at the end of a block or in a break/continue statement. eg `label x`
1761 # The `label` keyword
1762 var n_kwlabel
: TKwlabel is writable, noinit
1764 # The name of the label, if any
1765 var n_id
: nullable TId is writable, noinit
1768 # Expression and statements
1769 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1770 abstract class AExpr
1774 # A sequence of `AExpr` (usually statements)
1775 # The last `AExpr` gives the value of the whole block
1779 # The list of statements in the bloc.
1780 # The last element is often considered as an expression that give the value of the whole block.
1781 var n_expr
= new ANodes[AExpr](self)
1784 var n_kwend
: nullable TKwend = null is writable
1787 # A declaration of a local variable. eg `var x: X = y`
1792 var n_kwvar
: nullable TKwvar = null is writable
1794 # The name of the local variable
1795 var n_id
: TId is writable, noinit
1797 # The declaration type of the local variable
1798 var n_type
: nullable AType = null is writable
1800 # The `=` symbol (for the initial value)
1801 var n_assign
: nullable TAssign = null is writable
1803 # The initial value, if any
1804 var n_expr
: nullable AExpr = null is writable
1807 # A `return` statement. eg `return x`
1811 # The `return` keyword
1812 var n_kwreturn
: nullable TKwreturn = null is writable
1815 # A `yield` statement. eg `yield x`
1819 # The `yield` keyword
1820 var n_kwyield
: nullable TKwyield = null is writable
1822 # The return value, if any
1823 var n_expr
: nullable AExpr = null is writable
1826 # Something that has a label.
1827 abstract class ALabelable
1830 # The associated label declatation
1831 var n_label
: nullable ALabel = null is writable
1834 # A `break` or a `continue`
1835 abstract class AEscapeExpr
1839 # The return value, if nay (unused currently)
1840 var n_expr
: nullable AExpr = null is writable
1843 # A `break` statement.
1847 # The `break` keyword
1848 var n_kwbreak
: TKwbreak is writable, noinit
1851 # An `abort` statement
1855 # The `abort` keyword
1856 var n_kwabort
: TKwabort is writable, noinit
1859 # A `continue` statement
1863 # The `continue` keyword.
1864 var n_kwcontinue
: nullable TKwcontinue = null is writable
1873 var n_kwdo
: TKwdo is writable, noinit
1875 # The list of statements of the `do`.
1876 var n_block
: nullable AExpr = null is writable
1878 # The `catch` keyword
1879 var n_kwcatch
: nullable TKwcatch = null is writable
1881 # The do catch block
1882 var n_catch
: nullable AExpr = null is writable
1890 var n_kwif
: TKwif is writable, noinit
1892 # The expression used as the condition of the `if`
1893 var n_expr
: AExpr is writable, noinit
1895 # The `then` keyword
1896 var n_kwthen
: TKwthen is writable, noinit
1898 # The body of the `then` part
1899 var n_then
: nullable AExpr = null is writable
1901 # The `else` keyword
1902 var n_kwelse
: nullable TKwelse = null is writable
1904 # The body of the `else` part
1905 var n_else
: nullable AExpr = null is writable
1908 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1913 var n_kwif
: TKwif is writable, noinit
1915 # The expression used as the condition of the `if`
1916 var n_expr
: AExpr is writable, noinit
1918 # The `then` keyword
1919 var n_kwthen
: TKwthen is writable, noinit
1921 # The expression in the `then` part
1922 var n_then
: AExpr is writable, noinit
1924 # The `else` keyword
1925 var n_kwelse
: TKwelse is writable, noinit
1927 # The expression in the `else` part
1928 var n_else
: AExpr is writable, noinit
1931 # A `while` statement
1936 # The `while` keyword
1937 var n_kwwhile
: TKwwhile is writable, noinit
1939 # The expression used as the condition of the `while`
1940 var n_expr
: AExpr is writable, noinit
1943 var n_kwdo
: TKwdo is writable, noinit
1945 # The body of the loop
1946 var n_block
: nullable AExpr = null is writable
1949 # A `loop` statement
1954 # The `loop` keyword
1955 var n_kwloop
: TKwloop is writable, noinit
1957 # The body of the loop
1958 var n_block
: nullable AExpr = null is writable
1967 var n_kwfor
: TKwfor is writable, noinit
1969 # The list of groups to iterate
1970 var n_groups
= new ANodes[AForGroup](self)
1973 var n_kwdo
: TKwdo is writable, noinit
1975 # The body of the loop
1976 var n_block
: nullable AExpr = null is writable
1979 # A collection iterated by a for, its automatic variables and its implicit iterator.
1981 # Standard `for` iterate on a single collection.
1982 # Multiple `for` can iterate on more than one collection at once.
1986 # The list of name of the automatic variables
1987 var n_ids
= new ANodes[TId](self)
1990 var n_kwin
: TKwin is writable, noinit
1992 # The expression used as the collection to iterate on
1993 var n_expr
: AExpr is writable, noinit
1996 # A `with` statement
2001 # The `with` keyword
2002 var n_kwwith
: TKwwith is writable, noinit
2004 # The expression used to get the value to control
2005 var n_expr
: AExpr is writable, noinit
2008 var n_kwdo
: TKwdo is writable, noinit
2010 # The body of the loop
2011 var n_block
: nullable AExpr = null is writable
2014 # An `assert` statement
2018 # The `assert` keyword
2019 var n_kwassert
: TKwassert is writable, noinit
2021 # The name of the assert, if any
2022 var n_id
: nullable TId = null is writable
2024 # The expression used as the condition of the `assert`
2025 var n_expr
: AExpr is writable, noinit
2027 # The `else` keyword
2028 var n_kwelse
: nullable TKwelse = null is writable
2030 # The body to execute when the assert fails
2031 var n_else
: nullable AExpr = null is writable
2034 # Whatever is a simple assignment. eg `= something`
2035 abstract class AAssignFormExpr
2039 var n_assign
: TAssign is writable, noinit
2041 # The right-value to assign.
2042 var n_value
: AExpr is writable, noinit
2045 # Whatever is a combined assignment. eg `+= something`
2046 abstract class AReassignFormExpr
2049 # The combined operator (eg. `+=`)
2050 var n_assign_op
: AAssignOp is writable, noinit
2052 # The right-value to apply on the combined operator.
2053 var n_value
: AExpr is writable, noinit
2056 # A `once` expression. eg `once x`
2060 # The `once` keyword
2061 var n_kwonce
: TKwonce is writable, noinit
2063 # The expression to evaluate only one time
2064 var n_expr
: AExpr is writable, noinit
2067 # A polymorphic invocation of a method
2068 # The form of the invocation (name, arguments, etc.) are specific
2069 abstract class ASendExpr
2071 # The receiver of the method invocation
2072 var n_expr
: AExpr is writable, noinit
2075 # A binary operation on a method
2076 abstract class ABinopExpr
2080 var n_op
: Token is writable, noinit
2082 # The second operand of the operation
2083 # Note: the receiver (`n_expr`) is the first operand
2084 var n_expr2
: AExpr is writable, noinit
2086 # The name of the operator (eg '+')
2087 fun operator
: String is abstract
2090 # Something that is boolean expression
2091 abstract class ABoolExpr
2095 # Something that is binary boolean expression
2096 abstract class ABinBoolExpr
2099 # The first boolean operand
2100 var n_expr
: AExpr is writable, noinit
2103 var n_op
: Token is writable, noinit
2105 # The second boolean operand
2106 var n_expr2
: AExpr is writable, noinit
2114 # A `and` expression
2119 # A `or else` expression
2123 # The `else` keyword
2124 var n_kwelse
: TKwelse is writable, noinit
2127 # A `implies` expression
2132 # A `not` expression
2137 var n_kwnot
: TKwnot is writable, noinit
2139 # The boolean operand of the `not`
2140 var n_expr
: AExpr is writable, noinit
2143 # A `==` or a `!=` expression
2145 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2153 redef fun operator
do return "=="
2159 redef fun operator
do return "!="
2165 redef fun operator
do return "<"
2171 redef fun operator
do return "<="
2177 redef fun operator
do return "<<"
2183 redef fun operator
do return ">"
2189 redef fun operator
do return ">="
2195 redef fun operator
do return ">>"
2198 # A type-ckeck expression. eg `x isa T`
2202 # The expression to check
2203 var n_expr
: AExpr is writable, noinit
2206 var n_kwisa
: TKwisa is writable, noinit
2208 # The destination type to check to
2209 var n_type
: AType is writable, noinit
2215 redef fun operator
do return "+"
2221 redef fun operator
do return "-"
2224 # A `<=>` expression
2227 redef fun operator
do return "<=>"
2233 redef fun operator
do return "*"
2239 redef fun operator
do return "**"
2245 redef fun operator
do return "/"
2251 redef fun operator
do return "%"
2257 redef fun operator
do return "|"
2263 redef fun operator
do return "^"
2269 redef fun operator
do return "&"
2272 # A unary operation on a method
2273 abstract class AUnaryopExpr
2277 var n_op
: Token is writable, noinit
2279 # The name of the operator (eg '+')
2280 fun operator
: String is abstract
2283 # A unary minus expression. eg `-x`
2286 redef fun operator
do return "-"
2289 # A unary plus expression. eg `+x`
2292 redef fun operator
do return "+"
2295 # A unary `~` expression
2298 redef fun operator
do return "~"
2301 # An explicit instantiation. eg `new T`
2306 var n_kwnew
: TKwnew is writable, noinit
2308 # The `type` keyword
2309 var n_type
: AType is writable, noinit
2311 # The name of the named-constructor, if any
2312 var n_qid
: nullable AQid = null is writable
2314 # The arguments of the `new`
2315 var n_args
: AExprs is writable, noinit
2318 # Whatever is a old-style attribute access
2319 abstract class AAttrFormExpr
2322 # The receiver of the attribute
2323 var n_expr
: AExpr is writable, noinit
2325 # The name of the attribute
2326 var n_id
: TAttrid is writable, noinit
2330 # The read of an attribute. eg `x._a`
2335 # The assignment of an attribute. eg `x._a=y`
2336 class AAttrAssignExpr
2338 super AAssignFormExpr
2341 # Whatever looks-like a call with a standard method and any number of arguments.
2342 abstract class ACallFormExpr
2345 # The name of the method
2346 var n_qid
: AQid is writable, noinit
2348 # The arguments of the call
2349 var n_args
: AExprs is writable, noinit
2352 # A complex setter call (standard or brackets)
2353 abstract class ASendReassignFormExpr
2355 super AReassignFormExpr
2358 # A complex attribute assignment. eg `x._a+=y`
2359 class AAttrReassignExpr
2361 super AReassignFormExpr
2364 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2365 # 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`.
2366 # Semantic analysis have to transform them to instance of `AVarExpr`.
2371 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2372 # 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`.
2373 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2374 class ACallAssignExpr
2376 super AAssignFormExpr
2379 # 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.
2380 # 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`.
2381 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2382 class ACallReassignExpr
2384 super ASendReassignFormExpr
2387 # A call to `super`. OR a call of a super-constructor
2391 # The qualifier part before the super (currenlty unused)
2392 var n_qualified
: nullable AQualified = null is writable
2394 # The `super` keyword
2395 var n_kwsuper
: TKwsuper is writable, noinit
2397 # The arguments of the super
2398 var n_args
: AExprs is writable, noinit
2401 # A call to the `init` constructor.
2402 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2406 # The `init` keyword
2407 var n_kwinit
: TKwinit is writable, noinit
2409 # The arguments of the init
2410 var n_args
: AExprs is writable, noinit
2413 # Whatever looks-like a call of the brackets `[]` operator.
2414 abstract class ABraFormExpr
2417 # The arguments inside the brackets
2418 var n_args
: AExprs is writable, noinit
2421 # A call of the brackets operator. eg `x[y,z]`
2426 # A setter call of the bracket operator. eg `x[y,z]=t`
2427 class ABraAssignExpr
2429 super AAssignFormExpr
2432 # Whatever is an access to a local variable
2433 abstract class AVarFormExpr
2436 # The name of the attribute
2437 var n_id
: TId is writable, noinit
2440 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2441 class ABraReassignExpr
2443 super ASendReassignFormExpr
2446 # A local variable read access.
2447 # The parser cannot instantiate them, see `ACallExpr`.
2452 # A local variable simple assignment access
2453 # The parser cannot instantiate them, see `ACallAssignExpr`.
2454 class AVarAssignExpr
2456 super AAssignFormExpr
2459 # A local variable complex assignment access
2460 # The parser cannot instantiate them, see `ACallReassignExpr`.
2461 class AVarReassignExpr
2463 super AReassignFormExpr
2466 # A literal range, open or closed
2467 abstract class ARangeExpr
2470 # The left (lower) element of the range
2471 var n_expr
: AExpr is writable, noinit
2474 var n_dotdot
: TDotdot is writable, noinit
2476 # The right (upper) element of the range
2477 var n_expr2
: AExpr is writable, noinit
2480 # A closed literal range. eg `[x..y]`
2484 # The opening bracket `[`
2485 var n_obra
: TObra is writable, noinit
2487 # The closing bracket `]`
2488 var n_cbra
: TCbra is writable, noinit
2491 # An open literal range. eg `[x..y[`
2495 # The opening bracket `[`
2496 var n_obra
: TObra is writable, noinit
2498 # The closing bracket `[` (because open range)
2499 var n_cbra
: TObra is writable, noinit
2502 # A literal array. eg. `[x,y,z]`
2506 # The opening bracket `[`
2507 var n_obra
: TObra is writable, noinit
2509 # The elements of the array
2510 var n_exprs
= new ANodes[AExpr](self)
2512 # The type of the element of the array (if any)
2513 var n_type
: nullable AType = null is writable
2515 # The closing bracket `]`
2516 var n_cbra
: TCbra is writable, noinit
2523 # The `self` keyword
2524 var n_kwself
: nullable TKwself = null is writable
2527 # When there is no explicit receiver, `self` is implicit
2528 class AImplicitSelfExpr
2532 # A `true` boolean literal constant
2536 # The `true` keyword
2537 var n_kwtrue
: TKwtrue is writable, noinit
2540 # A `false` boolean literal constant
2544 # The `false` keyword
2545 var n_kwfalse
: TKwfalse is writable, noinit
2548 # A `null` literal constant
2552 # The `null` keyword
2553 var n_kwnull
: TKwnull is writable, noinit
2556 # An integer literal
2561 var n_integer
: TInteger is writable, noinit
2569 var n_float
: TFloat is writable, noinit
2572 # A character literal
2576 # The character token
2577 var n_char
: TChar is writable, noinit
2581 abstract class AStringFormExpr
2585 var n_string
: Token is writable, noinit
2588 # A simple string. eg. `"abc"`
2590 super AStringFormExpr
2593 # The start of a superstring. eg `"abc{`
2594 class AStartStringExpr
2595 super AStringFormExpr
2598 # The middle of a superstring. eg `}abc{`
2599 class AMidStringExpr
2600 super AStringFormExpr
2603 # The end of a superstrng. eg `}abc"`
2604 class AEndStringExpr
2605 super AStringFormExpr
2608 # A superstring literal. eg `"a{x}b{y}c"`
2609 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2610 class ASuperstringExpr
2613 # The list of the expressions of the superstring
2614 var n_exprs
= new ANodes[AExpr](self)
2617 # A simple parenthesis. eg `(x)`
2621 # The opening parenthesis
2622 var n_opar
: TOpar is writable, noinit
2624 # The inner expression
2625 var n_expr
: AExpr is writable, noinit
2627 # The closing parenthesis
2628 var n_cpar
: TCpar is writable, noinit
2631 # A cast, against a type or `not null`
2635 # The expression to cast
2636 var n_expr
: AExpr is writable, noinit
2639 var n_kwas
: TKwas is writable, noinit
2641 # The opening parenthesis
2642 var n_opar
: nullable TOpar = null is writable
2644 # The closing parenthesis
2645 var n_cpar
: nullable TCpar = null is writable
2648 # A type cast. eg `x.as(T)`
2652 # The target type to cast to
2653 var n_type
: AType is writable, noinit
2656 # A as-not-null cast. eg `x.as(not null)`
2657 class AAsNotnullExpr
2661 var n_kwnot
: TKwnot is writable, noinit
2663 # The `null` keyword
2664 var n_kwnull
: TKwnull is writable, noinit
2667 # A is-set check of old-style attributes. eg `isset x._a`
2668 class AIssetAttrExpr
2671 # The `isset` keyword
2672 var n_kwisset
: TKwisset is writable, noinit
2675 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2679 # The passed expression
2680 var n_expr
: AExpr is writable, noinit
2683 var n_dotdotdot
: TDotdotdot is writable, noinit
2686 # An named notation used to pass an expression by name in a parameter
2690 # The name of the argument
2691 var n_id
: TId is writable, noinit
2694 var n_assign
: TAssign is writable, noinit
2696 # The passed expression
2697 var n_expr
: AExpr is writable, noinit
2700 # A list of expression separated with commas (arguments for instance)
2704 # The list of expressions
2705 var n_exprs
= new ANodes[AExpr](self)
2708 # A special expression that encapsulates a static type
2709 # Can only be found in special construction like arguments of annotations.
2713 # The encapsulated type
2714 var n_type
: AType is writable, noinit
2717 # A special expression that encapsulates a method identifier
2718 # Can only be found in special construction like arguments of annotations.
2723 var n_expr
: AExpr is writable, noinit
2725 # The encapsulated method identifier
2726 var n_id
: AMethid is writable, noinit
2729 # A special expression that encapsulate an annotation
2730 # Can only be found in special construction like arguments of annotations.
2732 # The encapsulated annotations are in `n_annotations`
2737 # A special expression to debug types
2738 class ADebugTypeExpr
2741 # The `debug` keyword
2742 var n_kwdebug
: TKwdebug is writable, noinit
2744 # The `type` keyword
2745 var n_kwtype
: TKwtype is writable, noinit
2747 # The expression to check
2748 var n_expr
: AExpr is writable, noinit
2751 var n_type
: AType is writable, noinit
2754 # A list of expression separated with commas (arguments for instance)
2755 abstract class AExprs
2758 # The list of expressions
2759 var n_exprs
= new ANodes[AExpr](self)
2762 # A simple list of expressions
2767 # A list of expressions enclosed in parentheses
2771 # The opening parenthesis
2772 var n_opar
: TOpar is writable, noinit
2774 # The closing parenthesis
2775 var n_cpar
: TCpar is writable, noinit
2778 # A list of expressions enclosed in brackets
2782 # The opening bracket
2783 var n_obra
: TObra is writable, noinit
2785 # The closing bracket
2786 var n_cbra
: TCbra is writable, noinit
2789 # A complex assignment operator. (`+=` and `-=`)
2790 abstract class AAssignOp
2793 # The combined assignment operator
2794 var n_op
: Token is writable, noinit
2796 # The name of the operator without the `=` (eg '+')
2797 fun operator
: String is abstract
2800 # A `+=` assignment operation
2804 redef fun operator
do return "+"
2807 # A `-=` assignment operation
2808 class AMinusAssignOp
2811 redef fun operator
do return "-"
2814 # A `*=` assignment operation
2818 redef fun operator
do return "*"
2821 # A `/=` assignment operation
2822 class ASlashAssignOp
2825 redef fun operator
do return "/"
2828 # A `%=` assignment operation
2829 class APercentAssignOp
2832 redef fun operator
do return "%"
2835 # A `**=` assignment operation
2836 class AStarstarAssignOp
2839 redef fun operator
do return "**"
2842 # A `|=` assignment operation
2846 redef fun operator
do return "|"
2849 # A `^=` assignment operation
2850 class ACaretAssignOp
2853 redef fun operator
do return "^"
2856 # A `&=` assignment operation
2860 redef fun operator
do return "&"
2863 # A `<<=` assignment operation
2867 redef fun operator
do return "<<"
2870 # A `>>=` assignment operation
2874 redef fun operator
do return ">>"
2877 # A possibly fully-qualified module identifier
2881 # The starting quad (`::`)
2882 var n_quad
: nullable TQuad = null is writable
2884 # The list of quad-separated package/group identifiers
2885 var n_path
= new ANodes[TId](self)
2887 # The final module identifier
2888 var n_id
: TId is writable, noinit
2891 # A language declaration for an extern block
2896 var n_kwin
: TKwin is writable, noinit
2899 var n_string
: TString is writable, noinit
2902 # An full extern block
2903 class AExternCodeBlock
2906 # The language declration
2907 var n_in_language
: nullable AInLanguage = null is writable
2909 # The block of extern code
2910 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2913 # A possible full method qualifier.
2917 # The starting quad (`::`)
2918 var n_quad
: nullable TQuad = null is writable
2920 # The list of quad-separated package/group/module identifiers
2921 var n_id
= new ANodes[TId](self)
2923 # A class identifier
2924 var n_classid
: nullable TClassid = null is writable
2927 # A documentation of a definition
2928 # It contains the block of comments just above the declaration
2932 # A list of lines of comment
2933 var n_comment
= new ANodes[TComment](self)
2936 # A group of annotation on a node
2938 # This same class is used for the 3 kind of annotations:
2940 # * *is* annotations. eg `module foo is bar`.
2941 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2942 # * *class* annotations, defined in classes.
2946 # The `is` keyword, for *is* annotations
2947 var n_kwis
: nullable TKwis = null is writable
2949 # The `@` symbol, for *at* annotations
2950 var n_at
: nullable TAt = null is writable
2952 # The opening parenthesis in *at* annotations
2953 var n_opar
: nullable TOpar = null is writable
2955 # The list of annotations
2956 var n_items
= new ANodes[AAnnotation](self)
2958 # The closing parenthesis in *at* annotations
2959 var n_cpar
: nullable TCpar = null is writable
2961 # The `end` keyword, for *is* annotations
2962 var n_kwend
: nullable TKwend = null is writable
2965 # A single annotation
2969 # The name of the annotation
2970 var n_atid
: AAtid is writable, noinit
2972 # The opening parenthesis of the arguments
2973 var n_opar
: nullable TOpar = null is writable
2975 # The list of arguments
2976 var n_args
= new ANodes[AExpr](self)
2978 # The closing parenthesis
2979 var n_cpar
: nullable TCpar = null is writable
2981 # The name of the annotation
2984 return n_atid
.n_id
.text
2988 # An annotation name
2989 abstract class AAtid
2992 # The identifier of the annotation.
2993 # Can be a TId of a keyword
2994 var n_id
: Token is writable, noinit
2997 # An annotation name based on an identifier
3002 # An annotation name based on the keyword `extern`
3007 # An annotation name based on the keyword `import`
3012 # An annotation name based on the keyword `abstract`
3013 class AKwabstractAtid
3017 # The root of the AST
3022 var n_base
: nullable AModule is writable
3024 # The end of file (or error) token
3025 var n_eof
: EOF is writable