1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # AST nodes of the Nit language
16 # Was previously based on parser_abs.nit.
22 # Root of the AST class-hierarchy
24 # Location is set during AST building. Once built, location cannon be null.
25 # However, manual instantiated nodes may need more care.
26 var location
: Location is writable, noinit
28 # The location of the important part of the node (identifier or whatever)
29 fun hot_location
: Location do return location
31 # Display a message for the colored location of the node
32 fun debug
(message
: String)
34 sys
.stderr
.write
"{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
37 # Write the subtree on stdout.
43 d
.write_to
(sys
.stdout
)
46 # Parent of the node in the AST
47 var parent
: nullable ANode = null
49 # The topmost ancestor of the element
50 # This just apply `parent` until the first one
56 if p
== null then return res
61 # The most specific common parent between `self` and `other`
62 # Return null if the two node are unrelated (distinct root)
63 fun common_parent
(other
: ANode): nullable ANode
65 # First, get the same depth
66 var s
: nullable ANode = self
67 var o
: nullable ANode = other
68 var d
= s
.depth
- o
.depth
77 assert o
.depth
== s
.depth
78 # Second, go up until same in found
86 # Number of nodes between `self` and the `root` of the AST
87 # ENSURE `self == self.root implies result == 0 `
88 # ENSURE `self != self.root implies result == self.parent.depth + 1`
95 if p
== null then return res
101 # Replace a child with an other node in the AST
102 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode) is abstract
104 # Detach a node from its parent
105 # Aborts if the node is not detachable. use `replace_with` instead
106 # REQUIRE: parent != null
107 # REQUIRE: is_detachable
108 # ENDURE: parent == null
111 assert parent
!= null
112 parent
.replace_child
(self, null)
116 # Replace itself with an other node in the AST
117 # REQUIRE: parent != null
118 # ENSURE: node.parent == old(parent)
119 # ENSURE: parent == null
120 fun replace_with
(node
: ANode)
122 assert parent
!= null
123 parent
.replace_child
(self, node
)
127 # Visit all nodes in order.
128 # Thus, call `v.enter_visit(e)` for each child `e`
129 fun visit_all
(v
: Visitor) is abstract
131 # Do a deep search and return an array of tokens that match a given text
132 fun collect_tokens_by_text
(text
: String): Array[Token]
134 var v
= new CollectTokensByTextVisitor(text
)
139 # Do a deep search and return an array of node that are annotated
140 # The attached node can be retrieved by two invocations of parent
141 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
143 var v
= new CollectAnnotationsByNameVisitor(name
)
149 private class CollectTokensByTextVisitor
152 var result
= new Array[Token]
153 redef fun visit
(node
)
156 if node
isa Token and node
.text
== text
then result
.add
(node
)
160 private class CollectAnnotationsByNameVisitor
163 var result
= new Array[AAnnotation]
164 redef fun visit
(node
)
167 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)
171 # A helper class to handle (print) Nit AST as an OrderedTree
174 super OrderedTree[ANode]
176 # Reference to the last parent in the Ordered Tree
177 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
178 private var last_parent
: nullable ANode = null
192 return "{n.class_name} \"{n.text.escape_to_c}\
" @{n.location}"
194 return "{n.class_name} @{n.location}"
199 # A sequence of nodes
200 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
201 class ANodes[E
: ANode]
203 private var parent
: ANode
204 private var items
= new Array[E
]
205 redef fun iterator
do return items
.iterator
206 redef fun length
do return items
.length
207 redef fun is_empty
do return items
.is_empty
226 var res
= items
.shift
238 redef fun []=(index
, e
)
240 hook_remove
(self[index
])
244 redef fun remove_at
(index
)
246 hook_remove
(items
[index
])
247 items
.remove_at
(index
)
249 private fun hook_add
(e
: E
)
251 #assert e.parent == null
254 private fun hook_remove
(e
: E
)
256 assert e
.parent
== parent
260 # Used in parent constructor to fill elements
261 private fun unsafe_add_all
(nodes
: Collection[Object])
263 var parent
= self.parent
271 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
273 var parent
= self.parent
274 for i
in [0..length
[ do
275 if self[i
] == old_child
then
276 if new_child
!= null then
277 assert new_child
isa E
279 new_child
.parent
= parent
289 private fun visit_all
(v
: Visitor)
291 for n
in self do v
.enter_visit
(n
)
295 # Ancestor of all tokens
296 # A token is a node that has a `text` but no children.
300 # The raw content on the token
301 fun text
: String is abstract
303 # The raw content on the token
304 fun text
=(text
: String) is abstract
306 # The previous token in the Lexer.
307 # May have disappeared in the AST
308 var prev_token
: nullable Token = null
310 # The next token in the Lexer.
311 # May have disappeared in the AST
312 var next_token
: nullable Token = null
314 # The verbatim blank text between `prev_token` and `self`
315 fun blank_before
: String
317 if prev_token
== null then return ""
318 var from
= prev_token
.location
.pend
+1
319 var to
= location
.pstart
320 return location
.file
.string
.substring
(from
,to-from
)
323 redef fun to_s
: String do
327 redef fun visit_all
(v
: Visitor) do end
328 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
331 redef class SourceFile
332 # The first token parser by the lexer
333 # May have disappeared in the final AST
334 var first_token
: nullable Token = null
336 # The first token parser by the lexer
337 # May have disappeared in the final AST
338 var last_token
: nullable Token = null
341 # Ancestor of all productions
342 # A production is a node without text but that usually has children.
346 # All the annotations attached directly to the node
347 var n_annotations
: nullable AAnnotations = null is writable
349 # Return all its annotations of a given name in the order of their declaration
350 # Retun an empty array if no such an annotation.
351 fun get_annotations
(name
: String): Array[AAnnotation]
353 var res
= new Array[AAnnotation]
354 var nas
= n_annotations
355 if nas
!= null then for na
in nas
.n_items
do
356 if na
.name
!= name
then continue
359 if self isa AClassdef then for na
in n_propdefs
do
360 if na
isa AAnnotPropdef then
361 if na
.name
!= name
then continue
369 redef fun replace_with
(n
: ANode)
373 if not isset n
._location
and isset _location
then n
._location
= _location
377 # Abstract standard visitor on the AST
378 abstract class Visitor
379 # What the visitor do when a node is visited
380 # Concrete visitors should implement this method.
382 protected fun visit
(e
: ANode) is abstract
384 # Ask the visitor to visit a given node.
385 # Usually automatically called by visit_all* methods.
386 # This method should not be redefined
387 fun enter_visit
(e
: nullable ANode)
389 if e
== null then return
390 var old
= _current_node
396 # The current visited node
397 var current_node
: nullable ANode = null is writable
400 # Token of end of line (basically `\n`)
409 # Token of a line of comments
410 # Starts with the `#` and contains the final end-of-line (if any)
415 # A token associated with a keyword
416 abstract class TokenKeyword
420 return "keyword '{text}'"
424 # The deprecated keyword `package`.
429 # The keyword `module`
434 # The keyword `import`
439 # The keyword `class`
444 # The keyword `abstract`
449 # The keyword `interface`
454 # The keywords `enum` ane `universal`
479 # The keyword `redef`
499 # The keyword `extern`
504 # The keyword `public`
509 # The keyword `protected`
514 # The keyword `private`
519 # The keyword `intrude`
539 # The keyword `while`
569 # The keyword `implies`
579 # The keyword `return`
584 # The keyword `continue`
589 # The keyword `break`
594 # The keyword `abort`
599 # The keyword `assert`
619 # The keyword `super`
634 # The keyword `false`
649 # The keyword `nullable`
654 # The keyword `isset`
659 # The keyword `label`
669 # The special keyword `__DEBUG__`
714 # A token associated with an operator (and other lookalike symbols)
715 abstract class TokenOperator
719 return "operator '{text}'"
898 # A class (or formal type) identifier. They start with an uppercase.
903 do return "type identifier '{text}'"
907 # A standard identifier (variable, method...). They start with a lowercase.
912 do return "identifier '{text}'"
916 # An attribute identifier. They start with an underscore.
921 do return "attribute '{text}'"
925 # A token of a literal value (string, integer, etc).
926 abstract class TokenLiteral
930 do return "literal value '{text}'"
934 # A literal decimal integer
939 # A literal hexadecimal integer
944 # A literal floating point number
949 # A literal character
959 # The starting part of a super string (between `"` and `{`)
964 # The middle part of a super string (between `}` and `{`)
969 # The final part of a super string (between `}` and `"`)
979 do return "malformed string {text}"
988 do return "malformed character {text}"
992 # A extern code block
993 class TExternCodeSegment
1002 return "end of file"
1006 # A mark of an error
1010 # A lexical error (unexpected character)
1014 # A syntactic error (unexpected token)
1019 # The main node of a Nit source-file
1023 # The declaration part of the module
1024 var n_moduledecl
: nullable AModuledecl = null is writable
1026 # List of importation clauses
1027 var n_imports
= new ANodes[AImport](self)
1029 # List of extern blocks
1030 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1032 # List of class definition (including top-level methods and the main)
1033 var n_classdefs
= new ANodes[AClassdef](self)
1036 # Abstract class for definition of entities
1037 abstract class ADefinition
1040 var n_doc
: nullable ADoc = null is writable
1042 # The `redef` keyword
1043 var n_kwredef
: nullable TKwredef = null is writable
1045 # The declared visibility
1046 var n_visibility
: nullable AVisibility = null is writable
1049 # The declaration of the module with the documentation, name, and annotations
1053 # The `module` keyword
1054 var n_kwmodule
: TKwmodule is writable, noinit
1056 # The declared module name
1057 var n_name
: AModuleName is writable, noinit
1060 # A import clause of a module
1061 abstract class AImport
1064 # The declared visibility
1065 var n_visibility
: AVisibility is writable, noinit
1067 # The `import` keyword
1068 var n_kwimport
: TKwimport is writable, noinit
1071 # A standard import clause. eg `import x`
1074 # The imported module name
1075 var n_name
: AModuleName is writable, noinit
1078 # The special import clause of the kernel module. eg `import end`
1081 # The `end` keyword, that indicate the root module
1082 var n_kwend
: TKwend is writable, noinit
1085 # A visibility modifier
1087 # The public visibility is an empty production (no keyword).
1089 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1090 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1091 abstract class AVisibility
1095 # An implicit or explicit public visibility modifier
1096 class APublicVisibility
1098 # The `public` keyword, if any
1099 var n_kwpublic
: nullable TKwpublic is writable
1101 # An explicit private visibility modifier
1102 class APrivateVisibility
1104 # The `private` keyword
1105 var n_kwprivate
: TKwprivate is writable, noinit
1107 # An explicit protected visibility modifier
1108 class AProtectedVisibility
1110 # The `protected` keyword
1111 var n_kwprotected
: TKwprotected is writable, noinit
1113 # An explicit intrude visibility modifier
1114 class AIntrudeVisibility
1116 # The `intrude` keyword
1117 var n_kwintrude
: TKwintrude is writable, noinit
1120 # A class definition
1121 # While most definition are `AStdClassdef`
1122 # There is tow special case of class definition
1123 abstract class AClassdef
1125 # All the declared properties (including the main method)
1126 var n_propdefs
= new ANodes[APropdef](self)
1129 # A standard class definition with a name, superclasses and properties
1134 # The class kind (interface, abstract class, etc.)
1135 var n_classkind
: AClasskind is writable, noinit
1137 # The name of the class
1138 var n_id
: nullable TClassid = null is writable
1140 # The list of formal parameter types
1141 var n_formaldefs
= new ANodes[AFormaldef](self)
1143 # The extern block code
1144 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1147 var n_kwend
: TKwend is writable, noinit
1149 fun n_superclasses
: Array[ASuperPropdef] do
1150 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1153 redef fun hot_location
do return n_id
.location
1156 # The implicit class definition of the implicit main method
1161 # The implicit class definition of the top-level methods
1166 # The modifier for the kind of class (abstract, interface, etc.)
1167 abstract class AClasskind
1171 # A default, or concrete class modifier (just `class`)
1172 class AConcreteClasskind
1175 # The `class` keyword.
1176 var n_kwclass
: TKwclass is writable, noinit
1179 # An abstract class modifier (`abstract class`)
1180 class AAbstractClasskind
1183 # The `abstract` keyword.
1184 var n_kwabstract
: TKwabstract is writable, noinit
1186 # The `class` keyword.
1187 var n_kwclass
: TKwclass is writable, noinit
1190 # An interface class modifier (`interface`)
1191 class AInterfaceClasskind
1194 # The `interface` keyword.
1195 var n_kwinterface
: TKwinterface is writable, noinit
1198 # An enum/universal class modifier (`enum class`)
1199 class AEnumClasskind
1202 # The `enum` keyword.
1203 var n_kwenum
: TKwenum is writable, noinit
1206 # An extern class modifier (`extern class`)
1207 class AExternClasskind
1210 # The `extern` keyword.
1211 var n_kwextern
: TKwextern is writable, noinit
1213 # The `class` keyword.
1214 var n_kwclass
: nullable TKwclass = null is writable
1217 # The definition of a formal generic parameter type. eg `X: Y`
1221 # The name of the parameter type
1222 var n_id
: TClassid is writable, noinit
1224 # The bound of the parameter type
1225 var n_type
: nullable AType = null is writable
1228 # The definition of a property
1229 abstract class APropdef
1233 # A definition of an attribute
1234 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1238 # The identifier for a old-style attribute (null if new-style)
1239 var n_kwvar
: TKwvar is writable, noinit
1241 # The identifier for a new-style attribute (null if old-style)
1242 var n_id2
: TId is writable, noinit
1244 # The declared type of the attribute
1245 var n_type
: nullable AType = null is writable
1247 # The initial value, if any (set with `=`)
1248 var n_expr
: nullable AExpr = null is writable
1250 # The initial value, if any (set with `do return`)
1251 var n_block
: nullable AExpr = null is writable
1253 redef fun hot_location
1255 return n_id2
.location
1259 # A definition of all kind of method (including constructors)
1263 # The `fun` keyword, if any
1264 var n_kwmeth
: nullable TKwmeth = null is writable
1266 # The `init` keyword, if any
1267 var n_kwinit
: nullable TKwinit = null is writable
1269 # The `new` keyword, if any
1270 var n_kwnew
: nullable TKwnew = null is writable
1272 # The name of the method, if any
1273 var n_methid
: nullable AMethid = null is writable
1275 # The signature of the method, if any
1276 var n_signature
: nullable ASignature = null is writable
1278 # The body (in Nit) of the method, if any
1279 var n_block
: nullable AExpr = null is writable
1281 # The list of declared callbacks (for extern methods)
1282 var n_extern_calls
: nullable AExternCalls = null is writable
1284 # The body (in extern code) of the method, if any
1285 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1287 redef fun hot_location
1289 if n_methid
!= null then
1290 return n_methid
.location
1291 else if n_kwinit
!= null then
1292 return n_kwinit
.location
1293 else if n_kwnew
!= null then
1294 return n_kwnew
.location
1301 # The implicit main method
1302 class AMainMethPropdef
1311 # A super-class. eg `super X`
1316 var n_kwsuper
: TKwsuper is writable, noinit
1318 # The super-class (indicated as a type)
1319 var n_type
: AType is writable, noinit
1323 # Declaration of callbacks for extern methods
1327 # The `import` keyword
1328 var n_kwimport
: TKwimport is writable, noinit
1330 # The list of declared callbacks
1331 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1334 # A single callback declaration
1335 abstract class AExternCall
1339 # A single callback declaration on a method
1340 abstract class APropExternCall
1344 # A single callback declaration on a method on the current receiver
1345 class ALocalPropExternCall
1346 super APropExternCall
1348 # The name of the called-back method
1349 var n_methid
: AMethid is writable, noinit
1352 # A single callback declaration on a method on an explicit receiver type.
1353 class AFullPropExternCall
1354 super APropExternCall
1356 # The type of the receiver of the called-back method
1357 var n_type
: AType is writable, noinit
1360 var n_dot
: nullable TDot = null is writable
1362 # The name of the called-back method
1363 var n_methid
: AMethid is writable, noinit
1366 # A single callback declaration on a method on a constructor
1367 class AInitPropExternCall
1368 super APropExternCall
1370 # The allocated type
1371 var n_type
: AType is writable, noinit
1374 # A single callback declaration on a `super` call
1375 class ASuperExternCall
1378 # The `super` keyword
1379 var n_kwsuper
: TKwsuper is writable, noinit
1382 # A single callback declaration on a cast
1383 abstract class ACastExternCall
1387 # A single callback declaration on a cast to a given type
1388 class ACastAsExternCall
1389 super ACastExternCall
1391 # The origin type of the cast
1392 var n_from_type
: AType is writable, noinit
1395 var n_dot
: nullable TDot = null is writable
1398 var n_kwas
: TKwas is writable, noinit
1400 # The destination of the cast
1401 var n_to_type
: AType is writable, noinit
1404 # A single callback declaration on a cast to a nullable type
1405 class AAsNullableExternCall
1406 super ACastExternCall
1408 # The origin type to cast as nullable
1409 var n_type
: AType is writable, noinit
1412 var n_kwas
: TKwas is writable, noinit
1414 # The `nullable` keyword
1415 var n_kwnullable
: TKwnullable is writable, noinit
1418 # A single callback declaration on a cast to a non-nullable type
1419 class AAsNotNullableExternCall
1420 super ACastExternCall
1422 # The destination type on a cast to not nullable
1423 var n_type
: AType is writable, noinit
1426 var n_kwas
: TKwas is writable, noinit
1429 var n_kwnot
: TKwnot is writable, noinit
1431 # The `nullable` keyword
1432 var n_kwnullable
: TKwnullable is writable, noinit
1435 # A definition of a virtual type
1439 # The `type` keyword
1440 var n_kwtype
: TKwtype is writable, noinit
1442 # The name of the virtual type
1443 var n_id
: TClassid is writable, noinit
1445 # The bound of the virtual type
1446 var n_type
: AType is writable, noinit
1449 # The identifier of a method in a method declaration.
1450 # There is a specific class because of operator and setters.
1451 abstract class AMethid
1455 # A method name with a simple identifier
1459 # The simple identifier
1460 var n_id
: TId is writable, noinit
1463 # A method name for an operator
1464 class AOperatorMethid
1467 # The associated operator symbol
1468 var n_op
: Token is writable, noinit
1472 super AOperatorMethid
1477 super AOperatorMethid
1482 super AOperatorMethid
1485 # A method name `**`
1486 class AStarstarMethid
1487 super AOperatorMethid
1492 super AOperatorMethid
1496 class APercentMethid
1497 super AOperatorMethid
1502 super AOperatorMethid
1507 super AOperatorMethid
1512 super AOperatorMethid
1517 super AOperatorMethid
1520 # A method name `==`
1522 super AOperatorMethid
1525 # A method name `!=`
1527 super AOperatorMethid
1530 # A method name `<=`
1532 super AOperatorMethid
1535 # A method name `>=`
1537 super AOperatorMethid
1542 super AOperatorMethid
1547 super AOperatorMethid
1550 # A method name `<<`
1552 super AOperatorMethid
1555 # A method name `>>`
1557 super AOperatorMethid
1560 # A method name `<=>`
1561 class AStarshipMethid
1562 super AOperatorMethid
1565 # A method name `[]`
1570 var n_obra
: TObra is writable, noinit
1573 var n_cbra
: TCbra is writable, noinit
1576 # A setter method name with a simple identifier (with a `=`)
1580 # The base identifier
1581 var n_id
: TId is writable, noinit
1584 var n_assign
: TAssign is writable, noinit
1587 # A method name `[]=`
1588 class ABraassignMethid
1592 var n_obra
: TObra is writable, noinit
1595 var n_cbra
: TCbra is writable, noinit
1598 var n_assign
: TAssign is writable, noinit
1601 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1606 var n_opar
: nullable TOpar = null is writable
1608 # The list of parameters
1609 var n_params
= new ANodes[AParam](self)
1612 var n_cpar
: nullable TCpar = null is writable
1615 var n_type
: nullable AType = null is writable
1618 # A parameter definition in a signature. eg `x:X`
1622 # The name of the parameter
1623 var n_id
: TId is writable, noinit
1625 # The type of the parameter, if any
1626 var n_type
: nullable AType = null is writable
1628 # The `...` symbol to indicate varargs
1629 var n_dotdotdot
: nullable TDotdotdot = null is writable
1632 # A static type. eg `nullable X[Y]`
1635 # The `nullable` keyword
1636 var n_kwnullable
: nullable TKwnullable = null is writable
1638 # The name of the class or of the formal type
1639 var n_id
: TClassid is writable, noinit
1641 # Type arguments for a generic type
1642 var n_types
= new ANodes[AType](self)
1645 # A label at the end of a block or in a break/continue statement. eg `label x`
1649 # The `label` keyword
1650 var n_kwlabel
: TKwlabel is writable, noinit
1652 # The name of the label, if any
1653 var n_id
: nullable TId is writable, noinit
1656 # Expression and statements
1657 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1658 abstract class AExpr
1662 # A sequence of `AExpr` (usually statements)
1663 # The last `AExpr` gives the value of the whole block
1667 # The list of statements in the bloc.
1668 # The last element is often considered as an expression that give the value of the whole block.
1669 var n_expr
= new ANodes[AExpr](self)
1672 var n_kwend
: nullable TKwend = null is writable
1675 # A declaration of a local variable. eg `var x: X = y`
1680 var n_kwvar
: nullable TKwvar = null is writable
1682 # The name of the local variable
1683 var n_id
: TId is writable, noinit
1685 # The declaration type of the local variable
1686 var n_type
: nullable AType = null is writable
1688 # The `=` symbol (for the initial value)
1689 var n_assign
: nullable TAssign = null is writable
1691 # The initial value, if any
1692 var n_expr
: nullable AExpr = null is writable
1695 # A `return` statement. eg `return x`
1699 # The `return` keyword
1700 var n_kwreturn
: nullable TKwreturn = null is writable
1702 # The return value, if any
1703 var n_expr
: nullable AExpr = null is writable
1706 # Something that has a label.
1707 abstract class ALabelable
1710 # The associated label declatation
1711 var n_label
: nullable ALabel = null is writable
1714 # A `break` or a `continue`
1715 abstract class AEscapeExpr
1719 # The return value, if nay (unused currently)
1720 var n_expr
: nullable AExpr = null is writable
1723 # A `break` statement.
1727 # The `break` keyword
1728 var n_kwbreak
: TKwbreak is writable, noinit
1731 # An `abort` statement
1735 # The `abort` keyword
1736 var n_kwabort
: TKwabort is writable, noinit
1739 # A `continue` statement
1743 # The `continue` keyword.
1744 var n_kwcontinue
: nullable TKwcontinue = null is writable
1753 var n_kwdo
: TKwdo is writable, noinit
1755 # The list of statements of the `do`.
1756 var n_block
: nullable AExpr = null is writable
1764 var n_kwif
: TKwif is writable, noinit
1766 # The expression used as the condition of the `if`
1767 var n_expr
: AExpr is writable, noinit
1769 # The body of the `then` part
1770 var n_then
: nullable AExpr = null is writable
1772 # The body of the `else` part
1773 var n_else
: nullable AExpr = null is writable
1776 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1781 var n_kwif
: TKwif is writable, noinit
1783 # The expression used as the condition of the `if`
1784 var n_expr
: AExpr is writable, noinit
1786 # The `then` keyword
1787 var n_kwthen
: TKwthen is writable, noinit
1789 # The expression in the `then` part
1790 var n_then
: AExpr is writable, noinit
1792 # The `else` keyword
1793 var n_kwelse
: TKwelse is writable, noinit
1795 # The expression in the `else` part
1796 var n_else
: AExpr is writable, noinit
1799 # A `while` statement
1804 # The `while` keyword
1805 var n_kwwhile
: TKwwhile is writable, noinit
1807 # The expression used as the condition of the `while`
1808 var n_expr
: AExpr is writable, noinit
1811 var n_kwdo
: TKwdo is writable, noinit
1813 # The body of the loop
1814 var n_block
: nullable AExpr = null is writable
1817 # A `loop` statement
1822 # The `loop` keyword
1823 var n_kwloop
: TKwloop is writable, noinit
1825 # The body of the loop
1826 var n_block
: nullable AExpr = null is writable
1835 var n_kwfor
: TKwfor is writable, noinit
1837 # The list of name of the automatic variables
1838 var n_ids
= new ANodes[TId](self)
1840 # The expression used as the collection to iterate on
1841 var n_expr
: AExpr is writable, noinit
1844 var n_kwdo
: TKwdo is writable, noinit
1846 # The body of the loop
1847 var n_block
: nullable AExpr = null is writable
1850 # A `with` statement
1855 # The `with` keyword
1856 var n_kwwith
: TKwwith is writable, noinit
1858 # The expression used to get the value to control
1859 var n_expr
: AExpr is writable, noinit
1862 var n_kwdo
: TKwdo is writable, noinit
1864 # The body of the loop
1865 var n_block
: nullable AExpr = null is writable
1868 # An `assert` statement
1872 # The `assert` keyword
1873 var n_kwassert
: TKwassert is writable, noinit
1875 # The name of the assert, if any
1876 var n_id
: nullable TId = null is writable
1878 # The expression used as the condition of the `assert`
1879 var n_expr
: AExpr is writable, noinit
1881 # The body to execute when the assert fails
1882 var n_else
: nullable AExpr = null is writable
1885 # Whatever is a simple assignment. eg `= something`
1886 abstract class AAssignFormExpr
1890 var n_assign
: TAssign is writable, noinit
1892 # The right-value to assign.
1893 var n_value
: AExpr is writable, noinit
1896 # Whatever is a combined assignment. eg `+= something`
1897 abstract class AReassignFormExpr
1900 # The combined operator (eg. `+=`)
1901 var n_assign_op
: AAssignOp is writable, noinit
1903 # The right-value to apply on the combined operator.
1904 var n_value
: AExpr is writable, noinit
1907 # A `once` expression. eg `once x`
1911 # The `once` keyword
1912 var n_kwonce
: TKwonce is writable, noinit
1914 # The expression to evaluate only one time
1915 var n_expr
: AExpr is writable, noinit
1918 # A polymorphic invocation of a method
1919 # The form of the invocation (name, arguments, etc.) are specific
1920 abstract class ASendExpr
1922 # The receiver of the method invocation
1923 var n_expr
: AExpr is writable, noinit
1926 # A binary operation on a method
1927 abstract class ABinopExpr
1931 var n_op
: Token is writable, noinit
1933 # The second operand of the operation
1934 # Note: the receiver (`n_expr`) is the first operand
1935 var n_expr2
: AExpr is writable, noinit
1937 # The name of the operator (eg '+')
1938 fun operator
: String is abstract
1941 # Something that is boolean expression
1942 abstract class ABoolExpr
1946 # Something that is binary boolean expression
1947 abstract class ABinBoolExpr
1950 # The first boolean operand
1951 var n_expr
: AExpr is writable, noinit
1954 var n_op
: Token is writable, noinit
1956 # The second boolean operand
1957 var n_expr2
: AExpr is writable, noinit
1965 # A `and` expression
1970 # A `or else` expression
1974 # The `else` keyword
1975 var n_kwelse
: TKwelse is writable, noinit
1978 # A `implies` expression
1983 # A `not` expression
1988 var n_kwnot
: TKwnot is writable, noinit
1990 # The boolean operand of the `not`
1991 var n_expr
: AExpr is writable, noinit
1997 redef fun operator
do return "=="
2003 redef fun operator
do return "!="
2009 redef fun operator
do return "<"
2015 redef fun operator
do return "<="
2021 redef fun operator
do return "<<"
2027 redef fun operator
do return ">"
2033 redef fun operator
do return ">="
2039 redef fun operator
do return ">>"
2042 # A type-ckeck expression. eg `x isa T`
2046 # The expression to check
2047 var n_expr
: AExpr is writable, noinit
2050 var n_kwisa
: TKwisa is writable, noinit
2052 # The destination type to check to
2053 var n_type
: AType is writable, noinit
2059 redef fun operator
do return "+"
2065 redef fun operator
do return "-"
2068 # A `<=>` expression
2071 redef fun operator
do return "<=>"
2077 redef fun operator
do return "*"
2083 redef fun operator
do return "**"
2089 redef fun operator
do return "/"
2095 redef fun operator
do return "%"
2101 redef fun operator
do return "|"
2107 redef fun operator
do return "^"
2113 redef fun operator
do return "&"
2116 # A unary operation on a method
2121 var n_op
: Token is writable, noinit
2123 # The name of the operator (eg '+')
2124 fun operator
: String is abstract
2127 # A unary minus expression. eg `-x`
2130 redef fun operator
do return "-"
2133 # A unary plus expression. eg `+x`
2136 redef fun operator
do return "+"
2139 # A unary `~` expression
2142 redef fun operator
do return "~"
2145 # An explicit instantiation. eg `new T`
2150 var n_kwnew
: TKwnew is writable, noinit
2152 # The `type` keyword
2153 var n_type
: AType is writable, noinit
2155 # The name of the named-constructor, if any
2156 var n_id
: nullable TId = null is writable
2158 # The arguments of the `new`
2159 var n_args
: AExprs is writable, noinit
2162 # Whatever is a old-style attribute access
2163 abstract class AAttrFormExpr
2166 # The receiver of the attribute
2167 var n_expr
: AExpr is writable, noinit
2169 # The name of the attribute
2170 var n_id
: TAttrid is writable, noinit
2174 # The read of an attribute. eg `x._a`
2179 # The assignment of an attribute. eg `x._a=y`
2180 class AAttrAssignExpr
2182 super AAssignFormExpr
2185 # Whatever looks-like a call with a standard method and any number of arguments.
2186 abstract class ACallFormExpr
2189 # The name of the method
2190 var n_id
: TId is writable, noinit
2192 # The arguments of the call
2193 var n_args
: AExprs is writable, noinit
2196 # A complex setter call (standard or brackets)
2197 abstract class ASendReassignFormExpr
2199 super AReassignFormExpr
2202 # A complex attribute assignment. eg `x._a+=y`
2203 class AAttrReassignExpr
2205 super AReassignFormExpr
2208 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2209 # 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`.
2210 # Semantic analysis have to transform them to instance of `AVarExpr`.
2215 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2216 # 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`.
2217 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2218 class ACallAssignExpr
2220 super AAssignFormExpr
2223 # 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.
2224 # 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`.
2225 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2226 class ACallReassignExpr
2228 super ASendReassignFormExpr
2231 # A call to `super`. OR a call of a super-constructor
2235 # The qualifier part before the super (currenlty unused)
2236 var n_qualified
: nullable AQualified = null is writable
2238 # The `super` keyword
2239 var n_kwsuper
: TKwsuper is writable, noinit
2241 # The arguments of the super
2242 var n_args
: AExprs is writable, noinit
2245 # A call to the `init` constructor.
2246 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2250 # The `init` keyword
2251 var n_kwinit
: TKwinit is writable, noinit
2253 # The arguments of the init
2254 var n_args
: AExprs is writable, noinit
2257 # Whatever looks-like a call of the brackets `[]` operator.
2258 abstract class ABraFormExpr
2261 # The arguments inside the brackets
2262 var n_args
: AExprs is writable, noinit
2265 # A call of the brackets operator. eg `x[y,z]`
2270 # A setter call of the bracket operator. eg `x[y,z]=t`
2271 class ABraAssignExpr
2273 super AAssignFormExpr
2276 # Whatever is an access to a local variable
2277 abstract class AVarFormExpr
2280 # The name of the attribute
2281 var n_id
: TId is writable, noinit
2284 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2285 class ABraReassignExpr
2287 super ASendReassignFormExpr
2290 # A local variable read access.
2291 # The parser cannot instantiate them, see `ACallExpr`.
2296 # A local variable simple assignment access
2297 # The parser cannot instantiate them, see `ACallAssignExpr`.
2298 class AVarAssignExpr
2300 super AAssignFormExpr
2303 # A local variable complex assignment access
2304 # The parser cannot instantiate them, see `ACallReassignExpr`.
2305 class AVarReassignExpr
2307 super AReassignFormExpr
2310 # A literal range, open or closed
2311 abstract class ARangeExpr
2314 # The left (lower) element of the range
2315 var n_expr
: AExpr is writable, noinit
2317 # The right (uppr) element of the range
2318 var n_expr2
: AExpr is writable, noinit
2321 # A closed literal range. eg `[x..y]`
2325 # The opening bracket `[`
2326 var n_obra
: TObra is writable, noinit
2328 # The closing bracket `]`
2329 var n_cbra
: TCbra is writable, noinit
2332 # An open literal range. eg `[x..y[`
2336 # The opening bracket `[`
2337 var n_obra
: TObra is writable, noinit
2339 # The closing bracket `[` (because open range)
2340 var n_cbra
: TObra is writable, noinit
2343 # A literal array. eg. `[x,y,z]`
2347 # The opening bracket `[`
2348 var n_obra
: TObra is writable, noinit
2350 # The elements of the array
2351 var n_exprs
= new ANodes[AExpr](self)
2353 # The type of the element of the array (if any)
2354 var n_type
: nullable AType = null is writable
2356 # The closing bracket `]`
2357 var n_cbra
: TCbra is writable, noinit
2364 # The `self` keyword
2365 var n_kwself
: nullable TKwself = null is writable
2368 # When there is no explicit receiver, `self` is implicit
2369 class AImplicitSelfExpr
2373 # A `true` boolean literal constant
2377 # The `true` keyword
2378 var n_kwtrue
: TKwtrue is writable, noinit
2381 # A `false` boolean literal constant
2385 # The `false` keyword
2386 var n_kwfalse
: TKwfalse is writable, noinit
2389 # A `null` literal constant
2393 # The `null` keyword
2394 var n_kwnull
: TKwnull is writable, noinit
2397 # An integer literal
2402 # An integer literal in decimal format
2407 var n_number
: TNumber is writable, noinit
2410 # An integer literal in hexadecimal format
2414 # The hexadecimal token
2415 var n_hex_number
: THexNumber is writable, noinit
2423 var n_float
: TFloat is writable, noinit
2426 # A character literal
2430 # The character token
2431 var n_char
: TChar is writable, noinit
2435 abstract class AStringFormExpr
2439 var n_string
: Token is writable, noinit
2442 # A simple string. eg. `"abc"`
2444 super AStringFormExpr
2447 # The start of a superstring. eg `"abc{`
2448 class AStartStringExpr
2449 super AStringFormExpr
2452 # The middle of a superstring. eg `}abc{`
2453 class AMidStringExpr
2454 super AStringFormExpr
2457 # The end of a superstrng. eg `}abc"`
2458 class AEndStringExpr
2459 super AStringFormExpr
2462 # A superstring literal. eg `"a{x}b{y}c"`
2463 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2464 class ASuperstringExpr
2467 # The list of the expressions of the superstring
2468 var n_exprs
= new ANodes[AExpr](self)
2471 # A simple parenthesis. eg `(x)`
2475 # The opening parenthesis
2476 var n_opar
: TOpar is writable, noinit
2478 # The inner expression
2479 var n_expr
: AExpr is writable, noinit
2481 # The closing parenthesis
2482 var n_cpar
: TCpar is writable, noinit
2485 # A cast, against a type or `not null`
2489 # The expression to cast
2490 var n_expr
: AExpr is writable, noinit
2493 var n_kwas
: TKwas is writable, noinit
2495 # The opening parenthesis
2496 var n_opar
: nullable TOpar = null is writable
2498 # The closing parenthesis
2499 var n_cpar
: nullable TCpar = null is writable
2502 # A type cast. eg `x.as(T)`
2506 # The target type to cast to
2507 var n_type
: AType is writable, noinit
2510 # A as-not-null cast. eg `x.as(not null)`
2511 class AAsNotnullExpr
2515 var n_kwnot
: TKwnot is writable, noinit
2517 # The `null` keyword
2518 var n_kwnull
: TKwnull is writable, noinit
2521 # A is-set check of old-style attributes. eg `isset x._a`
2522 class AIssetAttrExpr
2525 # The `isset` keyword
2526 var n_kwisset
: TKwisset is writable, noinit
2529 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2533 # The passed expression
2534 var n_expr
: AExpr is writable, noinit
2537 var n_dotdotdot
: TDotdotdot is writable, noinit
2540 # A list of expression separated with commas (arguments for instance)
2544 # The list of expressions
2545 var n_exprs
= new ANodes[AExpr](self)
2548 # A special expression that encapsulates a static type
2549 # Can only be found in special construction like arguments of annotations.
2553 # The encapsulated type
2554 var n_type
: AType is writable, noinit
2557 # A special expression that encapsulates a method identifier
2558 # Can only be found in special construction like arguments of annotations.
2563 var n_expr
: AExpr is writable, noinit
2565 # The encapsulated method identifier
2566 var n_id
: AMethid is writable, noinit
2569 # A special expression that encapsulate an annotation
2570 # Can only be found in special construction like arguments of annotations.
2572 # The encapsulated annotations are in `n_annotations`
2577 # A special expression to debug types
2578 class ADebugTypeExpr
2581 # The `debug` keyword
2582 var n_kwdebug
: TKwdebug is writable, noinit
2584 # The `type` keyword
2585 var n_kwtype
: TKwtype is writable, noinit
2587 # The expression to check
2588 var n_expr
: AExpr is writable, noinit
2591 var n_type
: AType is writable, noinit
2594 # A list of expression separated with commas (arguments for instance)
2595 abstract class AExprs
2598 # The list of expressions
2599 var n_exprs
= new ANodes[AExpr](self)
2602 # A simple list of expressions
2607 # A list of expressions enclosed in parentheses
2611 # The opening parenthesis
2612 var n_opar
: TOpar is writable, noinit
2614 # The closing parenthesis
2615 var n_cpar
: TCpar is writable, noinit
2618 # A list of expressions enclosed in brackets
2622 # The opening bracket
2623 var n_obra
: TObra is writable, noinit
2625 # The closing bracket
2626 var n_cbra
: TCbra is writable, noinit
2629 # A complex assignment operator. (`+=` and `-=`)
2630 abstract class AAssignOp
2633 # The combined assignment operator
2634 var n_op
: Token is writable, noinit
2636 # The name of the operator without the `=` (eg '+')
2637 fun operator
: String is abstract
2640 # A `+=` assignment operation
2644 redef fun operator
do return "+"
2647 # A `-=` assignment operation
2648 class AMinusAssignOp
2651 redef fun operator
do return "-"
2654 # A `*=` assignment operation
2658 redef fun operator
do return "*"
2661 # A `/=` assignment operation
2662 class ASlashAssignOp
2665 redef fun operator
do return "/"
2668 # A `%=` assignment operation
2669 class APercentAssignOp
2672 redef fun operator
do return "%"
2675 # A `**=` assignment operation
2676 class AStarstarAssignOp
2679 redef fun operator
do return "**"
2682 # A `|=` assignment operation
2686 redef fun operator
do return "|"
2689 # A `^=` assignment operation
2690 class ACaretAssignOp
2693 redef fun operator
do return "^"
2696 # A `&=` assignment operation
2700 redef fun operator
do return "&"
2703 # A `<<=` assignment operation
2707 redef fun operator
do return "<<"
2710 # A `>>=` assignment operation
2714 redef fun operator
do return ">>"
2717 # A possibly fully-qualified module identifier
2721 # The starting quad (`::`)
2722 var n_quad
: nullable TQuad = null is writable
2724 # The list of quad-separated project/group identifiers
2725 var n_path
= new ANodes[TId](self)
2727 # The final module identifier
2728 var n_id
: TId is writable, noinit
2731 # A language declaration for an extern block
2736 var n_kwin
: TKwin is writable, noinit
2739 var n_string
: TString is writable, noinit
2742 # An full extern block
2743 class AExternCodeBlock
2746 # The language declration
2747 var n_in_language
: nullable AInLanguage = null is writable
2749 # The block of extern code
2750 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2753 # A possible full method qualifier.
2757 # The starting quad (`::`)
2758 var n_quad
: nullable TQuad = null is writable
2760 # The list of quad-separated project/group/module identifiers
2761 var n_id
= new ANodes[TId](self)
2763 # A class identifier
2764 var n_classid
: nullable TClassid = null is writable
2767 # A documentation of a definition
2768 # It contains the block of comments just above the declaration
2772 # A list of lines of comment
2773 var n_comment
= new ANodes[TComment](self)
2776 # A group of annotation on a node
2778 # This same class is used for the 3 kind of annotations:
2780 # * *is* annotations. eg `module foo is bar`.
2781 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2782 # * *class* annotations, defined in classes.
2786 # The `@` symbol, for *at* annotations
2787 var n_at
: nullable TAt = null is writable
2789 # The opening parenthesis in *at* annotations
2790 var n_opar
: nullable TOpar = null is writable
2792 # The list of annotations
2793 var n_items
= new ANodes[AAnnotation](self)
2795 # The closing parenthesis in *at* annotations
2796 var n_cpar
: nullable TCpar = null is writable
2799 # A single annotation
2803 # The name of the annotation
2804 var n_atid
: AAtid is writable, noinit
2806 # The opening parenthesis of the arguments
2807 var n_opar
: nullable TOpar = null is writable
2809 # The list of arguments
2810 var n_args
= new ANodes[AExpr](self)
2812 # The closing parenthesis
2813 var n_cpar
: nullable TCpar = null is writable
2815 # The name of the annotation
2818 return n_atid
.n_id
.text
2822 # An annotation name
2823 abstract class AAtid
2826 # The identifier of the annotation.
2827 # Can be a TId of a keyword
2828 var n_id
: Token is writable, noinit
2831 # An annotation name based on an identifier
2836 # An annotation name based on the keyword `extern`
2841 # An annotation name based on the keyword `import`
2846 # An annotation name based on the keyword `abstract`
2847 class AKwabstractAtid
2851 # The root of the AST
2856 var n_base
: nullable AModule is writable
2858 # The end of file (or error) token
2859 var n_eof
: EOF is writable