1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # AST nodes of the Nit language
16 # Was previously based on parser_abs.nit.
22 # Root of the AST class-hierarchy
24 # Location is set during AST building. Once built, location cannon be null.
25 # However, manual instantiated nodes may need more care.
26 var location
: Location is writable, noinit
28 # The location of the important part of the node (identifier or whatever)
29 fun hot_location
: Location do return location
31 # Display a message for the colored location of the node
32 fun debug
(message
: String)
34 sys
.stderr
.write
"{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n"
37 # Is `self` a token or a pure-structural production like `AQId`?
38 fun is_structural
: Bool do return false
40 # Write the subtree on stdout.
46 d
.write_to
(sys
.stdout
)
49 # Parent of the node in the AST
50 var parent
: nullable ANode = null
52 # The topmost ancestor of the element
53 # This just apply `parent` until the first one
59 if p
== null then return res
64 # The most specific common parent between `self` and `other`
65 # Return null if the two node are unrelated (distinct root)
66 fun common_parent
(other
: ANode): nullable ANode
68 # First, get the same depth
69 var s
: nullable ANode = self
70 var o
: nullable ANode = other
71 var d
= s
.depth
- o
.depth
80 assert o
.depth
== s
.depth
81 # Second, go up until same in found
89 # Number of nodes between `self` and the `root` of the AST
90 # ENSURE `self == self.root implies result == 0 `
91 # ENSURE `self != self.root implies result == self.parent.depth + 1`
98 if p
== null then return res
104 # Replace a child with an other node in the AST
105 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode) is abstract
107 # Detach a node from its parent
108 # Aborts if the node is not detachable. use `replace_with` instead
109 # REQUIRE: parent != null
110 # REQUIRE: is_detachable
111 # ENDURE: parent == null
114 assert parent
!= null
115 parent
.replace_child
(self, null)
119 # Replace itself with an other node in the AST
120 # REQUIRE: parent != null
121 # ENSURE: node.parent == old(parent)
122 # ENSURE: parent == null
123 fun replace_with
(node
: ANode)
125 assert parent
!= null
126 parent
.replace_child
(self, node
)
130 # Visit all nodes in order.
131 # Thus, call `v.enter_visit(e)` for each child `e`
132 fun visit_all
(v
: Visitor) is abstract
134 # Do a deep search and return an array of tokens that match a given text
135 fun collect_tokens_by_text
(text
: String): Array[Token]
137 var v
= new CollectTokensByTextVisitor(text
)
142 # Do a deep search and return an array of node that are annotated
143 # The attached node can be retrieved by two invocations of parent
144 fun collect_annotations_by_name
(name
: String): Array[AAnnotation]
146 var v
= new CollectAnnotationsByNameVisitor(name
)
152 private class CollectTokensByTextVisitor
155 var result
= new Array[Token]
156 redef fun visit
(node
)
159 if node
isa Token and node
.text
== text
then result
.add
(node
)
163 private class CollectAnnotationsByNameVisitor
166 var result
= new Array[AAnnotation]
167 redef fun visit
(node
)
170 if node
isa AAnnotation and node
.n_atid
.n_id
.text
== name
then result
.add
(node
)
174 # A helper class to handle (print) Nit AST as an OrderedTree
177 super OrderedTree[ANode]
179 # Reference to the last parent in the Ordered Tree
180 # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent`
181 private var last_parent
: nullable ANode = null
195 return "{n.class_name} \"{n.text.escape_to_c}\
" @{n.location}"
197 return "{n.class_name} @{n.location}"
202 # A sequence of nodes
203 # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed
204 class ANodes[E
: ANode]
206 private var parent
: ANode
207 private var items
= new Array[E
]
208 redef fun iterator
do return items
.iterator
209 redef fun reverse_iterator
do return items
.reverse_iterator
210 redef fun length
do return items
.length
211 redef fun is_empty
do return items
.is_empty
230 var res
= items
.shift
242 redef fun []=(index
, e
)
244 hook_remove
(self[index
])
248 redef fun remove_at
(index
)
250 hook_remove
(items
[index
])
251 items
.remove_at
(index
)
253 private fun hook_add
(e
: E
)
255 #assert e.parent == null
258 private fun hook_remove
(e
: E
)
260 assert e
.parent
== parent
264 # Used in parent constructor to fill elements
265 private fun unsafe_add_all
(nodes
: Collection[Object])
267 var parent
= self.parent
275 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
277 var parent
= self.parent
278 for i
in [0..length
[ do
279 if self[i
] == old_child
then
280 if new_child
!= null then
281 assert new_child
isa E
283 new_child
.parent
= parent
293 private fun visit_all
(v
: Visitor)
295 for n
in self do v
.enter_visit
(n
)
299 # Ancestor of all tokens
300 # A token is a node that has a `text` but no children.
304 # The raw content on the token
305 fun text
: String is abstract
307 # The raw content on the token
308 fun text
=(text
: String) is abstract
310 # The previous token in the Lexer.
311 # May have disappeared in the AST
312 var prev_token
: nullable Token = null
314 # The next token in the Lexer.
315 # May have disappeared in the AST
316 var next_token
: nullable Token = null
318 # Is `self` a token discarded from the AST?
320 # Loose tokens are not present in the AST.
321 # It means they were identified by the lexer but were discarded by the parser.
322 # It also means that they are not visited or manipulated by AST-related functions.
324 # Each loose token is attached to the non-loose token that precedes or follows it.
325 # The rules are the following:
327 # * tokens that follow a non-loose token on a same line are attached to it.
329 # * other tokens, thus that precede a non-loose token on the same line or the next one,
330 # are attached to this one. See `prev_looses`.
332 # Loose tokens are mostly end of lines (`TEol`) and comments (`TComment`).
333 # Whitespace are ignored by the lexer, so they are not even considered as loose tokens.
334 # See `blank_before` to get the whitespace that separate tokens.
337 redef fun is_structural
do return true
339 # Loose tokens that precede `self`.
341 # These tokens start the line or belong to a line with only loose tokens.
342 var prev_looses
= new Array[Token] is lazy
344 # Loose tokens that follow `self`
346 # These tokens are on the same line than `self`.
347 var next_looses
= new Array[Token] is lazy
349 # The verbatim blank text between `prev_token` and `self`
350 fun blank_before
: String
352 if prev_token
== null then return ""
353 var from
= prev_token
.location
.pend
+1
354 var to
= location
.pstart
355 return location
.file
.string
.substring
(from
,to-from
)
358 redef fun to_s
: String do
362 redef fun visit_all
(v
: Visitor) do end
363 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
366 redef class SourceFile
367 # The first token parser by the lexer
368 # May have disappeared in the final AST
369 var first_token
: nullable Token = null
371 # The first token parser by the lexer
372 # May have disappeared in the final AST
373 var last_token
: nullable Token = null
376 # Ancestor of all productions
377 # A production is a node without text but that usually has children.
381 # All the annotations attached directly to the node
382 var n_annotations
: nullable AAnnotations = null is writable
384 # Return all its annotations of a given name in the order of their declaration
385 # Retun an empty array if no such an annotation.
386 fun get_annotations
(name
: String): Array[AAnnotation]
388 var res
= new Array[AAnnotation]
389 var nas
= n_annotations
390 if nas
!= null then for na
in nas
.n_items
do
391 if na
.name
!= name
then continue
394 if self isa AClassdef then for na
in n_propdefs
do
395 if na
isa AAnnotPropdef then
396 if na
.name
!= name
then continue
404 redef fun replace_with
(n
: ANode)
408 if not isset n
._location
and isset _location
then n
._location
= _location
412 # Abstract standard visitor on the AST
413 abstract class Visitor
414 # What the visitor do when a node is visited
415 # Concrete visitors should implement this method.
417 protected fun visit
(e
: ANode) is abstract
419 # Ask the visitor to visit a given node.
420 # Usually automatically called by visit_all* methods.
421 # This method should not be redefined
422 fun enter_visit
(e
: nullable ANode)
424 if e
== null then return
425 var old
= _current_node
431 # The current visited node
432 var current_node
: nullable ANode = null is writable
435 # Token of end of line (basically `\n`)
444 # Token of a line of comments
445 # Starts with the `#` and contains the final end-of-line (if any)
450 # A token associated with a keyword
451 abstract class TokenKeyword
455 return "keyword '{text}'"
459 # The deprecated keyword `package`.
464 # The keyword `module`
469 # The keyword `import`
474 # The keyword `class`
479 # The keyword `abstract`
484 # The keyword `interface`
489 # The keywords `enum` ane `universal`
514 # The keyword `redef`
529 # The keyword `catch`
539 # The keyword `extern`
544 # The keyword `public`
549 # The keyword `protected`
554 # The keyword `private`
559 # The keyword `intrude`
579 # The keyword `while`
609 # The keyword `implies`
619 # The keyword `return`
624 # The keyword `continue`
629 # The keyword `break`
634 # The keyword `abort`
639 # The keyword `assert`
659 # The keyword `super`
674 # The keyword `false`
689 # The keyword `nullable`
694 # The keyword `isset`
699 # The keyword `label`
709 # The keyword `yield`
714 # The special keyword `__DEBUG__`
759 # A token associated with an operator (and other lookalike symbols)
760 abstract class TokenOperator
764 return "operator '{text}'"
948 # A class (or formal type) identifier. They start with an uppercase.
953 do return "type identifier '{text}'"
957 # A standard identifier (variable, method...). They start with a lowercase.
962 do return "identifier '{text}'"
966 # An attribute identifier. They start with an underscore.
971 do return "attribute '{text}'"
975 # A token of a literal value (string, integer, etc).
976 abstract class TokenLiteral
980 do return "literal value '{text}'"
989 # A literal floating point number
994 # A literal character
1004 # The starting part of a super string (between `"` and `{`)
1009 # The middle part of a super string (between `}` and `{`)
1014 # The final part of a super string (between `}` and `"`)
1019 # A malformed string
1024 do return "malformed string {text}"
1033 do return "malformed character {text}"
1037 # A extern code block
1038 class TExternCodeSegment
1047 return "end of file"
1051 # A mark of an error
1055 # A lexical error (unexpected character)
1059 # A syntactic error (unexpected token)
1064 # The main node of a Nit source-file
1068 # The declaration part of the module
1069 var n_moduledecl
: nullable AModuledecl = null is writable
1071 # List of importation clauses
1072 var n_imports
= new ANodes[AImport](self)
1074 # List of extern blocks
1075 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1077 # List of class definition (including top-level methods and the main)
1078 var n_classdefs
= new ANodes[AClassdef](self)
1081 # Abstract class for definition of entities
1082 abstract class ADefinition
1085 var n_doc
: nullable ADoc = null is writable
1087 # The `redef` keyword
1088 var n_kwredef
: nullable TKwredef = null is writable
1090 # The declared visibility
1091 var n_visibility
: nullable AVisibility = null is writable
1094 # The declaration of the module with the documentation, name, and annotations
1098 # The `module` keyword
1099 var n_kwmodule
: TKwmodule is writable, noinit
1101 # The declared module name
1102 var n_name
: AModuleName is writable, noinit
1105 # A import clause of a module
1106 abstract class AImport
1109 # The declared visibility
1110 var n_visibility
: AVisibility is writable, noinit
1112 # The `import` keyword
1113 var n_kwimport
: TKwimport is writable, noinit
1116 # A standard import clause. eg `import x`
1119 # The imported module name
1120 var n_name
: AModuleName is writable, noinit
1123 # The special import clause of the kernel module. eg `import end`
1126 # The `end` keyword, that indicate the root module
1127 var n_kwend
: TKwend is writable, noinit
1130 # A visibility modifier
1132 # The public visibility is an empty production (no keyword).
1134 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1135 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1136 abstract class AVisibility
1140 # An implicit or explicit public visibility modifier
1141 class APublicVisibility
1143 # The `public` keyword, if any
1144 var n_kwpublic
: nullable TKwpublic = null is writable
1146 # An explicit private visibility modifier
1147 class APrivateVisibility
1149 # The `private` keyword
1150 var n_kwprivate
: TKwprivate is writable, noinit
1152 # An explicit protected visibility modifier
1153 class AProtectedVisibility
1155 # The `protected` keyword
1156 var n_kwprotected
: TKwprotected is writable, noinit
1158 # An explicit intrude visibility modifier
1159 class AIntrudeVisibility
1161 # The `intrude` keyword
1162 var n_kwintrude
: TKwintrude is writable, noinit
1165 # A class definition
1166 # While most definition are `AStdClassdef`
1167 # There is tow special case of class definition
1168 abstract class AClassdef
1170 # All the declared properties (including the main method)
1171 var n_propdefs
= new ANodes[APropdef](self)
1174 # A standard class definition with a name, superclasses and properties
1179 # The class kind (interface, abstract class, etc.)
1180 var n_classkind
: AClasskind is writable, noinit
1182 # The name of the class
1183 var n_qid
: nullable AQclassid = null is writable
1186 var n_obra
: nullable TObra = null is writable
1188 # The list of formal parameter types
1189 var n_formaldefs
= new ANodes[AFormaldef](self)
1192 var n_cbra
: nullable TCbra = null is writable
1194 # The extern block code
1195 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1198 var n_kwend
: TKwend is writable, noinit
1200 fun n_superclasses
: Array[ASuperPropdef] do
1201 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1204 redef fun hot_location
do return n_qid
.location
1207 # The implicit class definition of the implicit main method
1212 # The implicit class definition of the top-level methods
1217 # The modifier for the kind of class (abstract, interface, etc.)
1218 abstract class AClasskind
1222 # A default, or concrete class modifier (just `class`)
1223 class AConcreteClasskind
1226 # The `class` keyword.
1227 var n_kwclass
: TKwclass is writable, noinit
1230 # An abstract class modifier (`abstract class`)
1231 class AAbstractClasskind
1234 # The `abstract` keyword.
1235 var n_kwabstract
: TKwabstract is writable, noinit
1237 # The `class` keyword.
1238 var n_kwclass
: TKwclass is writable, noinit
1241 # An interface class modifier (`interface`)
1242 class AInterfaceClasskind
1245 # The `interface` keyword.
1246 var n_kwinterface
: TKwinterface is writable, noinit
1249 # An enum/universal class modifier (`enum class`)
1250 class AEnumClasskind
1253 # The `enum` keyword.
1254 var n_kwenum
: TKwenum is writable, noinit
1257 # An extern class modifier (`extern class`)
1258 class AExternClasskind
1261 # The `extern` keyword.
1262 var n_kwextern
: TKwextern is writable, noinit
1264 # The `class` keyword.
1265 var n_kwclass
: nullable TKwclass = null is writable
1268 # The definition of a formal generic parameter type. eg `X: Y`
1272 # The name of the parameter type
1273 var n_id
: TClassid is writable, noinit
1275 # The bound of the parameter type
1276 var n_type
: nullable AType = null is writable
1279 # The definition of a property
1280 abstract class APropdef
1284 # A definition of an attribute
1285 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1290 var n_kwvar
: TKwvar is writable, noinit
1292 # The identifier for a new-style attribute
1293 var n_id2
: TId is writable, noinit
1295 # The declared type of the attribute
1296 var n_type
: nullable AType = null is writable
1299 var n_assign
: nullable TAssign = null is writable
1301 # The initial value, if any (set with `=`)
1302 var n_expr
: nullable AExpr = null is writable
1305 var n_kwdo
: nullable TKwdo = null is writable
1307 # The initial value, if any (set with `do return`)
1308 var n_block
: nullable AExpr = null is writable
1311 var n_kwend
: nullable TKwend = null is writable
1313 redef fun hot_location
1315 return n_id2
.location
1319 # A definition of all kind of method (including constructors)
1323 # The `fun` keyword, if any
1324 var n_kwmeth
: nullable TKwmeth = null is writable
1326 # The `init` keyword, if any
1327 var n_kwinit
: nullable TKwinit = null is writable
1329 # The `new` keyword, if any
1330 var n_kwnew
: nullable TKwnew = null is writable
1332 # The name of the method, if any
1333 var n_methid
: nullable AMethid = null is writable
1335 # The signature of the method, if any
1336 var n_signature
: nullable ASignature = null is writable
1339 var n_kwdo
: nullable TKwdo = null is writable
1341 # The body (in Nit) of the method, if any
1342 var n_block
: nullable AExpr = null is writable
1345 var n_kwend
: nullable TKwend = null is writable
1347 # The list of declared callbacks (for extern methods)
1348 var n_extern_calls
: nullable AExternCalls = null is writable
1350 # The body (in extern code) of the method, if any
1351 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1353 redef fun hot_location
1355 if n_methid
!= null then
1356 return n_methid
.location
1357 else if n_kwinit
!= null then
1358 return n_kwinit
.location
1359 else if n_kwnew
!= null then
1360 return n_kwnew
.location
1367 # The implicit main method
1368 class AMainMethPropdef
1377 # A super-class. eg `super X`
1382 var n_kwsuper
: TKwsuper is writable, noinit
1384 # The super-class (indicated as a type)
1385 var n_type
: AType is writable, noinit
1389 # Declaration of callbacks for extern methods
1393 # The `import` keyword
1394 var n_kwimport
: TKwimport is writable, noinit
1396 # The list of declared callbacks
1397 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1400 # A single callback declaration
1401 abstract class AExternCall
1405 # A single callback declaration on a method
1406 abstract class APropExternCall
1410 # A single callback declaration on a method on the current receiver
1411 class ALocalPropExternCall
1412 super APropExternCall
1414 # The name of the called-back method
1415 var n_methid
: AMethid is writable, noinit
1418 # A single callback declaration on a method on an explicit receiver type.
1419 class AFullPropExternCall
1420 super APropExternCall
1422 # The type of the receiver of the called-back method
1423 var n_type
: AType is writable, noinit
1426 var n_dot
: nullable TDot = null is writable
1428 # The name of the called-back method
1429 var n_methid
: AMethid is writable, noinit
1432 # A single callback declaration on a method on a constructor
1433 class AInitPropExternCall
1434 super APropExternCall
1436 # The allocated type
1437 var n_type
: AType is writable, noinit
1440 # A single callback declaration on a `super` call
1441 class ASuperExternCall
1444 # The `super` keyword
1445 var n_kwsuper
: TKwsuper is writable, noinit
1448 # A single callback declaration on a cast
1449 abstract class ACastExternCall
1453 # A single callback declaration on a cast to a given type
1454 class ACastAsExternCall
1455 super ACastExternCall
1457 # The origin type of the cast
1458 var n_from_type
: AType is writable, noinit
1461 var n_dot
: nullable TDot = null is writable
1464 var n_kwas
: TKwas is writable, noinit
1466 # The destination of the cast
1467 var n_to_type
: AType is writable, noinit
1470 # A single callback declaration on a cast to a nullable type
1471 class AAsNullableExternCall
1472 super ACastExternCall
1474 # The origin type to cast as nullable
1475 var n_type
: AType is writable, noinit
1478 var n_kwas
: TKwas is writable, noinit
1480 # The `nullable` keyword
1481 var n_kwnullable
: TKwnullable is writable, noinit
1484 # A single callback declaration on a cast to a non-nullable type
1485 class AAsNotNullableExternCall
1486 super ACastExternCall
1488 # The destination type on a cast to not nullable
1489 var n_type
: AType is writable, noinit
1492 var n_kwas
: TKwas is writable, noinit
1495 var n_kwnot
: TKwnot is writable, noinit
1497 # The `nullable` keyword
1498 var n_kwnullable
: TKwnullable is writable, noinit
1501 # A definition of a virtual type
1505 # The `type` keyword
1506 var n_kwtype
: TKwtype is writable, noinit
1508 # The name of the virtual type
1509 var n_qid
: AQclassid is writable, noinit
1511 # The bound of the virtual type
1512 var n_type
: AType is writable, noinit
1515 # The identifier of a method in a method declaration.
1516 # There is a specific class because of operator and setters.
1517 abstract class AMethid
1521 # A method name with a simple identifier
1525 # The simple identifier
1526 var n_id
: TId is writable, noinit
1529 # A method name for an operator
1530 class AOperatorMethid
1533 # The associated operator symbol
1534 var n_op
: Token is writable, noinit
1538 super AOperatorMethid
1543 super AOperatorMethid
1548 super AOperatorMethid
1551 # A method name `**`
1552 class AStarstarMethid
1553 super AOperatorMethid
1558 super AOperatorMethid
1562 class APercentMethid
1563 super AOperatorMethid
1568 super AOperatorMethid
1573 super AOperatorMethid
1578 super AOperatorMethid
1583 super AOperatorMethid
1586 # A method name `==`
1588 super AOperatorMethid
1591 # A method name `!=`
1593 super AOperatorMethid
1596 # A method name `<=`
1598 super AOperatorMethid
1601 # A method name `>=`
1603 super AOperatorMethid
1608 super AOperatorMethid
1613 super AOperatorMethid
1616 # A method name `<<`
1618 super AOperatorMethid
1621 # A method name `>>`
1623 super AOperatorMethid
1626 # A method name `<=>`
1627 class AStarshipMethid
1628 super AOperatorMethid
1631 # A method name `[]`
1636 var n_obra
: TObra is writable, noinit
1639 var n_cbra
: TCbra is writable, noinit
1642 # A setter method name with a simple identifier (with a `=`)
1646 # The base identifier
1647 var n_id
: TId is writable, noinit
1650 var n_assign
: TAssign is writable, noinit
1653 # A method name `[]=`
1654 class ABraassignMethid
1658 var n_obra
: TObra is writable, noinit
1661 var n_cbra
: TCbra is writable, noinit
1664 var n_assign
: TAssign is writable, noinit
1667 # A potentially qualified simple identifier `foo::bar::baz`
1670 # The qualifier, if any
1671 var n_qualified
: nullable AQualified = null is writable
1673 # The final identifier
1674 var n_id
: TId is writable, noinit
1676 redef fun is_structural
do return true
1679 # A potentially qualified class identifier `foo::bar::Baz`
1682 # The qualifier, if any
1683 var n_qualified
: nullable AQualified = null is writable
1685 # The final identifier
1686 var n_id
: TClassid is writable, noinit
1688 redef fun is_structural
do return true
1691 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1696 var n_opar
: nullable TOpar = null is writable
1698 # The list of parameters
1699 var n_params
= new ANodes[AParam](self)
1702 var n_cpar
: nullable TCpar = null is writable
1705 var n_type
: nullable AType = null is writable
1708 # A parameter definition in a signature. eg `x:X`
1712 # The name of the parameter
1713 var n_id
: TId is writable, noinit
1715 # The type of the parameter, if any
1716 var n_type
: nullable AType = null is writable
1718 # The `...` symbol to indicate varargs
1719 var n_dotdotdot
: nullable TDotdotdot = null is writable
1722 # A static type. eg `nullable X[Y]`
1725 # The `nullable` keyword
1726 var n_kwnullable
: nullable TKwnullable = null is writable
1728 # The name of the class or of the formal type
1729 var n_qid
: AQclassid is writable, noinit
1731 # The opening bracket
1732 var n_obra
: nullable TObra = null is writable
1734 # Type arguments for a generic type
1735 var n_types
= new ANodes[AType](self)
1737 # The closing bracket
1738 var n_cbra
: nullable TCbra = null is writable
1741 # A label at the end of a block or in a break/continue statement. eg `label x`
1745 # The `label` keyword
1746 var n_kwlabel
: TKwlabel is writable, noinit
1748 # The name of the label, if any
1749 var n_id
: nullable TId is writable, noinit
1752 # Expression and statements
1753 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1754 abstract class AExpr
1758 # A sequence of `AExpr` (usually statements)
1759 # The last `AExpr` gives the value of the whole block
1763 # The list of statements in the bloc.
1764 # The last element is often considered as an expression that give the value of the whole block.
1765 var n_expr
= new ANodes[AExpr](self)
1768 var n_kwend
: nullable TKwend = null is writable
1771 # A declaration of a local variable. eg `var x: X = y`
1776 var n_kwvar
: nullable TKwvar = null is writable
1778 # The name of the local variable
1779 var n_id
: TId is writable, noinit
1781 # The declaration type of the local variable
1782 var n_type
: nullable AType = null is writable
1784 # The `=` symbol (for the initial value)
1785 var n_assign
: nullable TAssign = null is writable
1787 # The initial value, if any
1788 var n_expr
: nullable AExpr = null is writable
1791 # A `return` statement. eg `return x`
1795 # The `return` keyword
1796 var n_kwreturn
: nullable TKwreturn = null is writable
1799 # A `yield` statement. eg `yield x`
1803 # The `yield` keyword
1804 var n_kwyield
: nullable TKwyield = null is writable
1806 # The return value, if any
1807 var n_expr
: nullable AExpr = null is writable
1810 # Something that has a label.
1811 abstract class ALabelable
1814 # The associated label declatation
1815 var n_label
: nullable ALabel = null is writable
1818 # A `break` or a `continue`
1819 abstract class AEscapeExpr
1823 # The return value, if nay (unused currently)
1824 var n_expr
: nullable AExpr = null is writable
1827 # A `break` statement.
1831 # The `break` keyword
1832 var n_kwbreak
: TKwbreak is writable, noinit
1835 # An `abort` statement
1839 # The `abort` keyword
1840 var n_kwabort
: TKwabort is writable, noinit
1843 # A `continue` statement
1847 # The `continue` keyword.
1848 var n_kwcontinue
: nullable TKwcontinue = null is writable
1857 var n_kwdo
: TKwdo is writable, noinit
1859 # The list of statements of the `do`.
1860 var n_block
: nullable AExpr = null is writable
1862 # The `catch` keyword
1863 var n_kwcatch
: nullable TKwcatch = null is writable
1865 # The do catch block
1866 var n_catch
: nullable AExpr = null is writable
1874 var n_kwif
: TKwif is writable, noinit
1876 # The expression used as the condition of the `if`
1877 var n_expr
: AExpr is writable, noinit
1879 # The `then` keyword
1880 var n_kwthen
: TKwthen is writable, noinit
1882 # The body of the `then` part
1883 var n_then
: nullable AExpr = null is writable
1885 # The `else` keyword
1886 var n_kwelse
: nullable TKwelse = null is writable
1888 # The body of the `else` part
1889 var n_else
: nullable AExpr = null is writable
1892 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1897 var n_kwif
: TKwif is writable, noinit
1899 # The expression used as the condition of the `if`
1900 var n_expr
: AExpr is writable, noinit
1902 # The `then` keyword
1903 var n_kwthen
: TKwthen is writable, noinit
1905 # The expression in the `then` part
1906 var n_then
: AExpr is writable, noinit
1908 # The `else` keyword
1909 var n_kwelse
: TKwelse is writable, noinit
1911 # The expression in the `else` part
1912 var n_else
: AExpr is writable, noinit
1915 # A `while` statement
1920 # The `while` keyword
1921 var n_kwwhile
: TKwwhile is writable, noinit
1923 # The expression used as the condition of the `while`
1924 var n_expr
: AExpr is writable, noinit
1927 var n_kwdo
: TKwdo is writable, noinit
1929 # The body of the loop
1930 var n_block
: nullable AExpr = null is writable
1933 # A `loop` statement
1938 # The `loop` keyword
1939 var n_kwloop
: TKwloop is writable, noinit
1941 # The body of the loop
1942 var n_block
: nullable AExpr = null is writable
1951 var n_kwfor
: TKwfor is writable, noinit
1953 # The list of groups to iterate
1954 var n_groups
= new ANodes[AForGroup](self)
1957 var n_kwdo
: TKwdo is writable, noinit
1959 # The body of the loop
1960 var n_block
: nullable AExpr = null is writable
1963 # A collection iterated by a for, its automatic variables and its implicit iterator.
1965 # Standard `for` iterate on a single collection.
1966 # Multiple `for` can iterate on more than one collection at once.
1970 # The list of name of the automatic variables
1971 var n_ids
= new ANodes[TId](self)
1974 var n_kwin
: TKwin is writable, noinit
1976 # The expression used as the collection to iterate on
1977 var n_expr
: AExpr is writable, noinit
1980 # A `with` statement
1985 # The `with` keyword
1986 var n_kwwith
: TKwwith is writable, noinit
1988 # The expression used to get the value to control
1989 var n_expr
: AExpr is writable, noinit
1992 var n_kwdo
: TKwdo is writable, noinit
1994 # The body of the loop
1995 var n_block
: nullable AExpr = null is writable
1998 # An `assert` statement
2002 # The `assert` keyword
2003 var n_kwassert
: TKwassert is writable, noinit
2005 # The name of the assert, if any
2006 var n_id
: nullable TId = null is writable
2008 # The expression used as the condition of the `assert`
2009 var n_expr
: AExpr is writable, noinit
2011 # The `else` keyword
2012 var n_kwelse
: nullable TKwelse = null is writable
2014 # The body to execute when the assert fails
2015 var n_else
: nullable AExpr = null is writable
2018 # Whatever is a simple assignment. eg `= something`
2019 abstract class AAssignFormExpr
2023 var n_assign
: TAssign is writable, noinit
2025 # The right-value to assign.
2026 var n_value
: AExpr is writable, noinit
2029 # Whatever is a combined assignment. eg `+= something`
2030 abstract class AReassignFormExpr
2033 # The combined operator (eg. `+=`)
2034 var n_assign_op
: AAssignOp is writable, noinit
2036 # The right-value to apply on the combined operator.
2037 var n_value
: AExpr is writable, noinit
2040 # A `once` expression. eg `once x`
2044 # The `once` keyword
2045 var n_kwonce
: TKwonce is writable, noinit
2047 # The expression to evaluate only one time
2048 var n_expr
: AExpr is writable, noinit
2051 # A polymorphic invocation of a method
2052 # The form of the invocation (name, arguments, etc.) are specific
2053 abstract class ASendExpr
2055 # The receiver of the method invocation
2056 var n_expr
: AExpr is writable, noinit
2059 # A binary operation on a method
2060 abstract class ABinopExpr
2064 var n_op
: Token is writable, noinit
2066 # The second operand of the operation
2067 # Note: the receiver (`n_expr`) is the first operand
2068 var n_expr2
: AExpr is writable, noinit
2070 # The name of the operator (eg '+')
2071 fun operator
: String is abstract
2074 # Something that is boolean expression
2075 abstract class ABoolExpr
2079 # Something that is binary boolean expression
2080 abstract class ABinBoolExpr
2083 # The first boolean operand
2084 var n_expr
: AExpr is writable, noinit
2087 var n_op
: Token is writable, noinit
2089 # The second boolean operand
2090 var n_expr2
: AExpr is writable, noinit
2098 # A `and` expression
2103 # A `or else` expression
2107 # The `else` keyword
2108 var n_kwelse
: TKwelse is writable, noinit
2111 # A `implies` expression
2116 # A `not` expression
2121 var n_kwnot
: TKwnot is writable, noinit
2123 # The boolean operand of the `not`
2124 var n_expr
: AExpr is writable, noinit
2127 # A `==` or a `!=` expression
2129 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2137 redef fun operator
do return "=="
2143 redef fun operator
do return "!="
2149 redef fun operator
do return "<"
2155 redef fun operator
do return "<="
2161 redef fun operator
do return "<<"
2167 redef fun operator
do return ">"
2173 redef fun operator
do return ">="
2179 redef fun operator
do return ">>"
2182 # A type-ckeck expression. eg `x isa T`
2186 # The expression to check
2187 var n_expr
: AExpr is writable, noinit
2190 var n_kwisa
: TKwisa is writable, noinit
2192 # The destination type to check to
2193 var n_type
: AType is writable, noinit
2199 redef fun operator
do return "+"
2205 redef fun operator
do return "-"
2208 # A `<=>` expression
2211 redef fun operator
do return "<=>"
2217 redef fun operator
do return "*"
2223 redef fun operator
do return "**"
2229 redef fun operator
do return "/"
2235 redef fun operator
do return "%"
2241 redef fun operator
do return "|"
2247 redef fun operator
do return "^"
2253 redef fun operator
do return "&"
2256 # A unary operation on a method
2257 abstract class AUnaryopExpr
2261 var n_op
: Token is writable, noinit
2263 # The name of the operator (eg '+')
2264 fun operator
: String is abstract
2267 # A unary minus expression. eg `-x`
2270 redef fun operator
do return "-"
2273 # A unary plus expression. eg `+x`
2276 redef fun operator
do return "+"
2279 # A unary `~` expression
2282 redef fun operator
do return "~"
2285 # An explicit instantiation. eg `new T`
2290 var n_kwnew
: TKwnew is writable, noinit
2292 # The `type` keyword
2293 var n_type
: AType is writable, noinit
2295 # The name of the named-constructor, if any
2296 var n_qid
: nullable AQid = null is writable
2298 # The arguments of the `new`
2299 var n_args
: AExprs is writable, noinit
2302 # Whatever is a old-style attribute access
2303 abstract class AAttrFormExpr
2306 # The receiver of the attribute
2307 var n_expr
: AExpr is writable, noinit
2309 # The name of the attribute
2310 var n_id
: TAttrid is writable, noinit
2314 # The read of an attribute. eg `x._a`
2319 # The assignment of an attribute. eg `x._a=y`
2320 class AAttrAssignExpr
2322 super AAssignFormExpr
2325 # Whatever looks-like a call with a standard method and any number of arguments.
2326 abstract class ACallFormExpr
2329 # The name of the method
2330 var n_qid
: AQid is writable, noinit
2332 # The arguments of the call
2333 var n_args
: AExprs is writable, noinit
2336 # A complex setter call (standard or brackets)
2337 abstract class ASendReassignFormExpr
2339 super AReassignFormExpr
2342 # A complex attribute assignment. eg `x._a+=y`
2343 class AAttrReassignExpr
2345 super AReassignFormExpr
2348 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2349 # 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`.
2350 # Semantic analysis have to transform them to instance of `AVarExpr`.
2355 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2356 # 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`.
2357 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2358 class ACallAssignExpr
2360 super AAssignFormExpr
2363 # 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.
2364 # 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`.
2365 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2366 class ACallReassignExpr
2368 super ASendReassignFormExpr
2371 # A call to `super`. OR a call of a super-constructor
2375 # The qualifier part before the super (currenlty unused)
2376 var n_qualified
: nullable AQualified = null is writable
2378 # The `super` keyword
2379 var n_kwsuper
: TKwsuper is writable, noinit
2381 # The arguments of the super
2382 var n_args
: AExprs is writable, noinit
2385 # A call to the `init` constructor.
2386 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2390 # The `init` keyword
2391 var n_kwinit
: TKwinit is writable, noinit
2393 # The arguments of the init
2394 var n_args
: AExprs is writable, noinit
2397 # Whatever looks-like a call of the brackets `[]` operator.
2398 abstract class ABraFormExpr
2401 # The arguments inside the brackets
2402 var n_args
: AExprs is writable, noinit
2405 # A call of the brackets operator. eg `x[y,z]`
2410 # A setter call of the bracket operator. eg `x[y,z]=t`
2411 class ABraAssignExpr
2413 super AAssignFormExpr
2416 # Whatever is an access to a local variable
2417 abstract class AVarFormExpr
2420 # The name of the attribute
2421 var n_id
: TId is writable, noinit
2424 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2425 class ABraReassignExpr
2427 super ASendReassignFormExpr
2430 # A local variable read access.
2431 # The parser cannot instantiate them, see `ACallExpr`.
2436 # A local variable simple assignment access
2437 # The parser cannot instantiate them, see `ACallAssignExpr`.
2438 class AVarAssignExpr
2440 super AAssignFormExpr
2443 # A local variable complex assignment access
2444 # The parser cannot instantiate them, see `ACallReassignExpr`.
2445 class AVarReassignExpr
2447 super AReassignFormExpr
2450 # A literal range, open or closed
2451 abstract class ARangeExpr
2454 # The left (lower) element of the range
2455 var n_expr
: AExpr is writable, noinit
2458 var n_dotdot
: TDotdot is writable, noinit
2460 # The right (upper) element of the range
2461 var n_expr2
: AExpr is writable, noinit
2464 # A closed literal range. eg `[x..y]`
2468 # The opening bracket `[`
2469 var n_obra
: TObra is writable, noinit
2471 # The closing bracket `]`
2472 var n_cbra
: TCbra is writable, noinit
2475 # An open literal range. eg `[x..y[`
2479 # The opening bracket `[`
2480 var n_obra
: TObra is writable, noinit
2482 # The closing bracket `[` (because open range)
2483 var n_cbra
: TObra is writable, noinit
2486 # A literal array. eg. `[x,y,z]`
2490 # The opening bracket `[`
2491 var n_obra
: TObra is writable, noinit
2493 # The elements of the array
2494 var n_exprs
= new ANodes[AExpr](self)
2496 # The type of the element of the array (if any)
2497 var n_type
: nullable AType = null is writable
2499 # The closing bracket `]`
2500 var n_cbra
: TCbra is writable, noinit
2507 # The `self` keyword
2508 var n_kwself
: nullable TKwself = null is writable
2511 # When there is no explicit receiver, `self` is implicit
2512 class AImplicitSelfExpr
2516 # A `true` boolean literal constant
2520 # The `true` keyword
2521 var n_kwtrue
: TKwtrue is writable, noinit
2524 # A `false` boolean literal constant
2528 # The `false` keyword
2529 var n_kwfalse
: TKwfalse is writable, noinit
2532 # A `null` literal constant
2536 # The `null` keyword
2537 var n_kwnull
: TKwnull is writable, noinit
2540 # An integer literal
2545 var n_integer
: TInteger is writable, noinit
2553 var n_float
: TFloat is writable, noinit
2556 # A character literal
2560 # The character token
2561 var n_char
: TChar is writable, noinit
2565 abstract class AStringFormExpr
2569 var n_string
: Token is writable, noinit
2572 # A simple string. eg. `"abc"`
2574 super AStringFormExpr
2577 # The start of a superstring. eg `"abc{`
2578 class AStartStringExpr
2579 super AStringFormExpr
2582 # The middle of a superstring. eg `}abc{`
2583 class AMidStringExpr
2584 super AStringFormExpr
2587 # The end of a superstrng. eg `}abc"`
2588 class AEndStringExpr
2589 super AStringFormExpr
2592 # A superstring literal. eg `"a{x}b{y}c"`
2593 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2594 class ASuperstringExpr
2597 # The list of the expressions of the superstring
2598 var n_exprs
= new ANodes[AExpr](self)
2601 # A simple parenthesis. eg `(x)`
2605 # The opening parenthesis
2606 var n_opar
: TOpar is writable, noinit
2608 # The inner expression
2609 var n_expr
: AExpr is writable, noinit
2611 # The closing parenthesis
2612 var n_cpar
: TCpar is writable, noinit
2615 # A cast, against a type or `not null`
2619 # The expression to cast
2620 var n_expr
: AExpr is writable, noinit
2623 var n_kwas
: TKwas is writable, noinit
2625 # The opening parenthesis
2626 var n_opar
: nullable TOpar = null is writable
2628 # The closing parenthesis
2629 var n_cpar
: nullable TCpar = null is writable
2632 # A type cast. eg `x.as(T)`
2636 # The target type to cast to
2637 var n_type
: AType is writable, noinit
2640 # A as-not-null cast. eg `x.as(not null)`
2641 class AAsNotnullExpr
2645 var n_kwnot
: TKwnot is writable, noinit
2647 # The `null` keyword
2648 var n_kwnull
: TKwnull is writable, noinit
2651 # A is-set check of old-style attributes. eg `isset x._a`
2652 class AIssetAttrExpr
2655 # The `isset` keyword
2656 var n_kwisset
: TKwisset is writable, noinit
2659 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2663 # The passed expression
2664 var n_expr
: AExpr is writable, noinit
2667 var n_dotdotdot
: TDotdotdot is writable, noinit
2670 # An named notation used to pass an expression by name in a parameter
2674 # The name of the argument
2675 var n_id
: TId is writable, noinit
2678 var n_assign
: TAssign is writable, noinit
2680 # The passed expression
2681 var n_expr
: AExpr is writable, noinit
2684 # A list of expression separated with commas (arguments for instance)
2688 # The list of expressions
2689 var n_exprs
= new ANodes[AExpr](self)
2692 # A special expression that encapsulates a static type
2693 # Can only be found in special construction like arguments of annotations.
2697 # The encapsulated type
2698 var n_type
: AType is writable, noinit
2701 # A special expression that encapsulates a method identifier
2702 # Can only be found in special construction like arguments of annotations.
2707 var n_expr
: AExpr is writable, noinit
2709 # The encapsulated method identifier
2710 var n_id
: AMethid is writable, noinit
2713 # A special expression that encapsulate an annotation
2714 # Can only be found in special construction like arguments of annotations.
2716 # The encapsulated annotations are in `n_annotations`
2721 # A special expression to debug types
2722 class ADebugTypeExpr
2725 # The `debug` keyword
2726 var n_kwdebug
: TKwdebug is writable, noinit
2728 # The `type` keyword
2729 var n_kwtype
: TKwtype is writable, noinit
2731 # The expression to check
2732 var n_expr
: AExpr is writable, noinit
2735 var n_type
: AType is writable, noinit
2738 # A list of expression separated with commas (arguments for instance)
2739 abstract class AExprs
2742 # The list of expressions
2743 var n_exprs
= new ANodes[AExpr](self)
2746 # A simple list of expressions
2751 # A list of expressions enclosed in parentheses
2755 # The opening parenthesis
2756 var n_opar
: TOpar is writable, noinit
2758 # The closing parenthesis
2759 var n_cpar
: TCpar is writable, noinit
2762 # A list of expressions enclosed in brackets
2766 # The opening bracket
2767 var n_obra
: TObra is writable, noinit
2769 # The closing bracket
2770 var n_cbra
: TCbra is writable, noinit
2773 # A complex assignment operator. (`+=` and `-=`)
2774 abstract class AAssignOp
2777 # The combined assignment operator
2778 var n_op
: Token is writable, noinit
2780 # The name of the operator without the `=` (eg '+')
2781 fun operator
: String is abstract
2784 # A `+=` assignment operation
2788 redef fun operator
do return "+"
2791 # A `-=` assignment operation
2792 class AMinusAssignOp
2795 redef fun operator
do return "-"
2798 # A `*=` assignment operation
2802 redef fun operator
do return "*"
2805 # A `/=` assignment operation
2806 class ASlashAssignOp
2809 redef fun operator
do return "/"
2812 # A `%=` assignment operation
2813 class APercentAssignOp
2816 redef fun operator
do return "%"
2819 # A `**=` assignment operation
2820 class AStarstarAssignOp
2823 redef fun operator
do return "**"
2826 # A `|=` assignment operation
2830 redef fun operator
do return "|"
2833 # A `^=` assignment operation
2834 class ACaretAssignOp
2837 redef fun operator
do return "^"
2840 # A `&=` assignment operation
2844 redef fun operator
do return "&"
2847 # A `<<=` assignment operation
2851 redef fun operator
do return "<<"
2854 # A `>>=` assignment operation
2858 redef fun operator
do return ">>"
2861 # A possibly fully-qualified module identifier
2865 # The starting quad (`::`)
2866 var n_quad
: nullable TQuad = null is writable
2868 # The list of quad-separated package/group identifiers
2869 var n_path
= new ANodes[TId](self)
2871 # The final module identifier
2872 var n_id
: TId is writable, noinit
2875 # A language declaration for an extern block
2880 var n_kwin
: TKwin is writable, noinit
2883 var n_string
: TString is writable, noinit
2886 # An full extern block
2887 class AExternCodeBlock
2890 # The language declration
2891 var n_in_language
: nullable AInLanguage = null is writable
2893 # The block of extern code
2894 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2897 # A possible full method qualifier.
2901 # The starting quad (`::`)
2902 var n_quad
: nullable TQuad = null is writable
2904 # The list of quad-separated package/group/module identifiers
2905 var n_id
= new ANodes[TId](self)
2907 # A class identifier
2908 var n_classid
: nullable TClassid = null is writable
2911 # A documentation of a definition
2912 # It contains the block of comments just above the declaration
2916 # A list of lines of comment
2917 var n_comment
= new ANodes[TComment](self)
2920 # A group of annotation on a node
2922 # This same class is used for the 3 kind of annotations:
2924 # * *is* annotations. eg `module foo is bar`.
2925 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2926 # * *class* annotations, defined in classes.
2930 # The `is` keyword, for *is* annotations
2931 var n_kwis
: nullable TKwis = null is writable
2933 # The `@` symbol, for *at* annotations
2934 var n_at
: nullable TAt = null is writable
2936 # The opening parenthesis in *at* annotations
2937 var n_opar
: nullable TOpar = null is writable
2939 # The list of annotations
2940 var n_items
= new ANodes[AAnnotation](self)
2942 # The closing parenthesis in *at* annotations
2943 var n_cpar
: nullable TCpar = null is writable
2945 # The `end` keyword, for *is* annotations
2946 var n_kwend
: nullable TKwend = null is writable
2949 # A single annotation
2953 # The name of the annotation
2954 var n_atid
: AAtid is writable, noinit
2956 # The opening parenthesis of the arguments
2957 var n_opar
: nullable TOpar = null is writable
2959 # The list of arguments
2960 var n_args
= new ANodes[AExpr](self)
2962 # The closing parenthesis
2963 var n_cpar
: nullable TCpar = null is writable
2965 # The name of the annotation
2968 return n_atid
.n_id
.text
2972 # An annotation name
2973 abstract class AAtid
2976 # The identifier of the annotation.
2977 # Can be a TId of a keyword
2978 var n_id
: Token is writable, noinit
2981 # An annotation name based on an identifier
2986 # An annotation name based on the keyword `extern`
2991 # An annotation name based on the keyword `import`
2996 # An annotation name based on the keyword `abstract`
2997 class AKwabstractAtid
3001 # The root of the AST
3006 var n_base
: nullable AModule is writable
3008 # The end of file (or error) token
3009 var n_eof
: EOF is writable