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 reverse_iterator
do return items
.reverse_iterator
207 redef fun length
do return items
.length
208 redef fun is_empty
do return items
.is_empty
227 var res
= items
.shift
239 redef fun []=(index
, e
)
241 hook_remove
(self[index
])
245 redef fun remove_at
(index
)
247 hook_remove
(items
[index
])
248 items
.remove_at
(index
)
250 private fun hook_add
(e
: E
)
252 #assert e.parent == null
255 private fun hook_remove
(e
: E
)
257 assert e
.parent
== parent
261 # Used in parent constructor to fill elements
262 private fun unsafe_add_all
(nodes
: Collection[Object])
264 var parent
= self.parent
272 private fun replace_child
(old_child
: ANode, new_child
: nullable ANode): Bool
274 var parent
= self.parent
275 for i
in [0..length
[ do
276 if self[i
] == old_child
then
277 if new_child
!= null then
278 assert new_child
isa E
280 new_child
.parent
= parent
290 private fun visit_all
(v
: Visitor)
292 for n
in self do v
.enter_visit
(n
)
296 # Ancestor of all tokens
297 # A token is a node that has a `text` but no children.
301 # The raw content on the token
302 fun text
: String is abstract
304 # The raw content on the token
305 fun text
=(text
: String) is abstract
307 # The previous token in the Lexer.
308 # May have disappeared in the AST
309 var prev_token
: nullable Token = null
311 # The next token in the Lexer.
312 # May have disappeared in the AST
313 var next_token
: nullable Token = null
315 # Is `self` a token discarded from the AST?
317 # Loose tokens are not present in the AST.
318 # It means they were identified by the lexer but were discarded by the parser.
319 # It also means that they are not visited or manipulated by AST-related functions.
321 # Each loose token is attached to the non-loose token that precedes or follows it.
322 # The rules are the following:
324 # * tokens that follow a non-loose token on a same line are attached to it.
326 # * other tokens, thus that precede a non-loose token on the same line or the next one,
327 # are attached to this one. See `prev_looses`.
329 # Loose tokens are mostly end of lines (`TEol`) and comments (`TComment`).
330 # Whitespace are ignored by the lexer, so they are not even considered as loose tokens.
331 # See `blank_before` to get the whitespace that separate tokens.
334 # Loose tokens that precede `self`.
336 # These tokens start the line or belong to a line with only loose tokens.
337 var prev_looses
= new Array[Token] is lazy
339 # Loose tokens that follow `self`
341 # These tokens are on the same line than `self`.
342 var next_looses
= new Array[Token] is lazy
344 # The verbatim blank text between `prev_token` and `self`
345 fun blank_before
: String
347 if prev_token
== null then return ""
348 var from
= prev_token
.location
.pend
+1
349 var to
= location
.pstart
350 return location
.file
.string
.substring
(from
,to-from
)
353 redef fun to_s
: String do
357 redef fun visit_all
(v
: Visitor) do end
358 redef fun replace_child
(old_child
: ANode, new_child
: nullable ANode) do end
361 redef class SourceFile
362 # The first token parser by the lexer
363 # May have disappeared in the final AST
364 var first_token
: nullable Token = null
366 # The first token parser by the lexer
367 # May have disappeared in the final AST
368 var last_token
: nullable Token = null
371 # Ancestor of all productions
372 # A production is a node without text but that usually has children.
376 # All the annotations attached directly to the node
377 var n_annotations
: nullable AAnnotations = null is writable
379 # Return all its annotations of a given name in the order of their declaration
380 # Retun an empty array if no such an annotation.
381 fun get_annotations
(name
: String): Array[AAnnotation]
383 var res
= new Array[AAnnotation]
384 var nas
= n_annotations
385 if nas
!= null then for na
in nas
.n_items
do
386 if na
.name
!= name
then continue
389 if self isa AClassdef then for na
in n_propdefs
do
390 if na
isa AAnnotPropdef then
391 if na
.name
!= name
then continue
399 redef fun replace_with
(n
: ANode)
403 if not isset n
._location
and isset _location
then n
._location
= _location
407 # Abstract standard visitor on the AST
408 abstract class Visitor
409 # What the visitor do when a node is visited
410 # Concrete visitors should implement this method.
412 protected fun visit
(e
: ANode) is abstract
414 # Ask the visitor to visit a given node.
415 # Usually automatically called by visit_all* methods.
416 # This method should not be redefined
417 fun enter_visit
(e
: nullable ANode)
419 if e
== null then return
420 var old
= _current_node
426 # The current visited node
427 var current_node
: nullable ANode = null is writable
430 # Token of end of line (basically `\n`)
439 # Token of a line of comments
440 # Starts with the `#` and contains the final end-of-line (if any)
445 # A token associated with a keyword
446 abstract class TokenKeyword
450 return "keyword '{text}'"
454 # The deprecated keyword `package`.
459 # The keyword `module`
464 # The keyword `import`
469 # The keyword `class`
474 # The keyword `abstract`
479 # The keyword `interface`
484 # The keywords `enum` ane `universal`
509 # The keyword `redef`
524 # The keyword `catch`
534 # The keyword `extern`
539 # The keyword `public`
544 # The keyword `protected`
549 # The keyword `private`
554 # The keyword `intrude`
574 # The keyword `while`
604 # The keyword `implies`
614 # The keyword `return`
619 # The keyword `continue`
624 # The keyword `break`
629 # The keyword `abort`
634 # The keyword `assert`
654 # The keyword `super`
669 # The keyword `false`
684 # The keyword `nullable`
689 # The keyword `isset`
694 # The keyword `label`
704 # The keyword `yield`
709 # The special keyword `__DEBUG__`
754 # A token associated with an operator (and other lookalike symbols)
755 abstract class TokenOperator
759 return "operator '{text}'"
943 # A class (or formal type) identifier. They start with an uppercase.
948 do return "type identifier '{text}'"
952 # A standard identifier (variable, method...). They start with a lowercase.
957 do return "identifier '{text}'"
961 # An attribute identifier. They start with an underscore.
966 do return "attribute '{text}'"
970 # A token of a literal value (string, integer, etc).
971 abstract class TokenLiteral
975 do return "literal value '{text}'"
984 # A literal floating point number
989 # A literal character
999 # The starting part of a super string (between `"` and `{`)
1004 # The middle part of a super string (between `}` and `{`)
1009 # The final part of a super string (between `}` and `"`)
1014 # A malformed string
1019 do return "malformed string {text}"
1028 do return "malformed character {text}"
1032 # A extern code block
1033 class TExternCodeSegment
1042 return "end of file"
1046 # A mark of an error
1050 # A lexical error (unexpected character)
1054 # A syntactic error (unexpected token)
1059 # The main node of a Nit source-file
1063 # The declaration part of the module
1064 var n_moduledecl
: nullable AModuledecl = null is writable
1066 # List of importation clauses
1067 var n_imports
= new ANodes[AImport](self)
1069 # List of extern blocks
1070 var n_extern_code_blocks
= new ANodes[AExternCodeBlock](self)
1072 # List of class definition (including top-level methods and the main)
1073 var n_classdefs
= new ANodes[AClassdef](self)
1076 # Abstract class for definition of entities
1077 abstract class ADefinition
1080 var n_doc
: nullable ADoc = null is writable
1082 # The `redef` keyword
1083 var n_kwredef
: nullable TKwredef = null is writable
1085 # The declared visibility
1086 var n_visibility
: nullable AVisibility = null is writable
1089 # The declaration of the module with the documentation, name, and annotations
1093 # The `module` keyword
1094 var n_kwmodule
: TKwmodule is writable, noinit
1096 # The declared module name
1097 var n_name
: AModuleName is writable, noinit
1100 # A import clause of a module
1101 abstract class AImport
1104 # The declared visibility
1105 var n_visibility
: AVisibility is writable, noinit
1107 # The `import` keyword
1108 var n_kwimport
: TKwimport is writable, noinit
1111 # A standard import clause. eg `import x`
1114 # The imported module name
1115 var n_name
: AModuleName is writable, noinit
1118 # The special import clause of the kernel module. eg `import end`
1121 # The `end` keyword, that indicate the root module
1122 var n_kwend
: TKwend is writable, noinit
1125 # A visibility modifier
1127 # The public visibility is an empty production (no keyword).
1129 # Note: even if some visibilities are only valid on some placse (for instance, no `protected` class or no `intrude` method)
1130 # the parser has no such a restriction, therefore the semantic phases has to check that the visibilities make sense.
1131 abstract class AVisibility
1135 # An implicit or explicit public visibility modifier
1136 class APublicVisibility
1138 # The `public` keyword, if any
1139 var n_kwpublic
: nullable TKwpublic = null is writable
1141 # An explicit private visibility modifier
1142 class APrivateVisibility
1144 # The `private` keyword
1145 var n_kwprivate
: TKwprivate is writable, noinit
1147 # An explicit protected visibility modifier
1148 class AProtectedVisibility
1150 # The `protected` keyword
1151 var n_kwprotected
: TKwprotected is writable, noinit
1153 # An explicit intrude visibility modifier
1154 class AIntrudeVisibility
1156 # The `intrude` keyword
1157 var n_kwintrude
: TKwintrude is writable, noinit
1160 # A class definition
1161 # While most definition are `AStdClassdef`
1162 # There is tow special case of class definition
1163 abstract class AClassdef
1165 # All the declared properties (including the main method)
1166 var n_propdefs
= new ANodes[APropdef](self)
1169 # A standard class definition with a name, superclasses and properties
1174 # The class kind (interface, abstract class, etc.)
1175 var n_classkind
: AClasskind is writable, noinit
1177 # The name of the class
1178 var n_qid
: nullable AQclassid = null is writable
1181 var n_obra
: nullable TObra = null is writable
1183 # The list of formal parameter types
1184 var n_formaldefs
= new ANodes[AFormaldef](self)
1187 var n_cbra
: nullable TCbra = null is writable
1189 # The extern block code
1190 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1193 var n_kwend
: TKwend is writable, noinit
1195 fun n_superclasses
: Array[ASuperPropdef] do
1196 return [for d
in n_propdefs
do if d
isa ASuperPropdef then d
]
1199 redef fun hot_location
do return n_qid
.location
1202 # The implicit class definition of the implicit main method
1207 # The implicit class definition of the top-level methods
1212 # The modifier for the kind of class (abstract, interface, etc.)
1213 abstract class AClasskind
1217 # A default, or concrete class modifier (just `class`)
1218 class AConcreteClasskind
1221 # The `class` keyword.
1222 var n_kwclass
: TKwclass is writable, noinit
1225 # An abstract class modifier (`abstract class`)
1226 class AAbstractClasskind
1229 # The `abstract` keyword.
1230 var n_kwabstract
: TKwabstract is writable, noinit
1232 # The `class` keyword.
1233 var n_kwclass
: TKwclass is writable, noinit
1236 # An interface class modifier (`interface`)
1237 class AInterfaceClasskind
1240 # The `interface` keyword.
1241 var n_kwinterface
: TKwinterface is writable, noinit
1244 # An enum/universal class modifier (`enum class`)
1245 class AEnumClasskind
1248 # The `enum` keyword.
1249 var n_kwenum
: TKwenum is writable, noinit
1252 # An extern class modifier (`extern class`)
1253 class AExternClasskind
1256 # The `extern` keyword.
1257 var n_kwextern
: TKwextern is writable, noinit
1259 # The `class` keyword.
1260 var n_kwclass
: nullable TKwclass = null is writable
1263 # The definition of a formal generic parameter type. eg `X: Y`
1267 # The name of the parameter type
1268 var n_id
: TClassid is writable, noinit
1270 # The bound of the parameter type
1271 var n_type
: nullable AType = null is writable
1274 # The definition of a property
1275 abstract class APropdef
1279 # A definition of an attribute
1280 # For historical reason, old-syle and new-style attributes use the same `ANode` sub-class
1285 var n_kwvar
: TKwvar is writable, noinit
1287 # The identifier for a new-style attribute
1288 var n_id2
: TId is writable, noinit
1290 # The declared type of the attribute
1291 var n_type
: nullable AType = null is writable
1294 var n_assign
: nullable TAssign = null is writable
1296 # The initial value, if any (set with `=`)
1297 var n_expr
: nullable AExpr = null is writable
1300 var n_kwdo
: nullable TKwdo = null is writable
1302 # The initial value, if any (set with `do return`)
1303 var n_block
: nullable AExpr = null is writable
1306 var n_kwend
: nullable TKwend = null is writable
1308 redef fun hot_location
1310 return n_id2
.location
1314 # A definition of all kind of method (including constructors)
1318 # The `fun` keyword, if any
1319 var n_kwmeth
: nullable TKwmeth = null is writable
1321 # The `init` keyword, if any
1322 var n_kwinit
: nullable TKwinit = null is writable
1324 # The `new` keyword, if any
1325 var n_kwnew
: nullable TKwnew = null is writable
1327 # The name of the method, if any
1328 var n_methid
: nullable AMethid = null is writable
1330 # The signature of the method, if any
1331 var n_signature
: nullable ASignature = null is writable
1334 var n_kwdo
: nullable TKwdo = null is writable
1336 # The body (in Nit) of the method, if any
1337 var n_block
: nullable AExpr = null is writable
1340 var n_kwend
: nullable TKwend = null is writable
1342 # The list of declared callbacks (for extern methods)
1343 var n_extern_calls
: nullable AExternCalls = null is writable
1345 # The body (in extern code) of the method, if any
1346 var n_extern_code_block
: nullable AExternCodeBlock = null is writable
1348 redef fun hot_location
1350 if n_methid
!= null then
1351 return n_methid
.location
1352 else if n_kwinit
!= null then
1353 return n_kwinit
.location
1354 else if n_kwnew
!= null then
1355 return n_kwnew
.location
1362 # The implicit main method
1363 class AMainMethPropdef
1372 # A super-class. eg `super X`
1377 var n_kwsuper
: TKwsuper is writable, noinit
1379 # The super-class (indicated as a type)
1380 var n_type
: AType is writable, noinit
1384 # Declaration of callbacks for extern methods
1388 # The `import` keyword
1389 var n_kwimport
: TKwimport is writable, noinit
1391 # The list of declared callbacks
1392 var n_extern_calls
: ANodes[AExternCall] = new ANodes[AExternCall](self)
1395 # A single callback declaration
1396 abstract class AExternCall
1400 # A single callback declaration on a method
1401 abstract class APropExternCall
1405 # A single callback declaration on a method on the current receiver
1406 class ALocalPropExternCall
1407 super APropExternCall
1409 # The name of the called-back method
1410 var n_methid
: AMethid is writable, noinit
1413 # A single callback declaration on a method on an explicit receiver type.
1414 class AFullPropExternCall
1415 super APropExternCall
1417 # The type of the receiver of the called-back method
1418 var n_type
: AType is writable, noinit
1421 var n_dot
: nullable TDot = null is writable
1423 # The name of the called-back method
1424 var n_methid
: AMethid is writable, noinit
1427 # A single callback declaration on a method on a constructor
1428 class AInitPropExternCall
1429 super APropExternCall
1431 # The allocated type
1432 var n_type
: AType is writable, noinit
1435 # A single callback declaration on a `super` call
1436 class ASuperExternCall
1439 # The `super` keyword
1440 var n_kwsuper
: TKwsuper is writable, noinit
1443 # A single callback declaration on a cast
1444 abstract class ACastExternCall
1448 # A single callback declaration on a cast to a given type
1449 class ACastAsExternCall
1450 super ACastExternCall
1452 # The origin type of the cast
1453 var n_from_type
: AType is writable, noinit
1456 var n_dot
: nullable TDot = null is writable
1459 var n_kwas
: TKwas is writable, noinit
1461 # The destination of the cast
1462 var n_to_type
: AType is writable, noinit
1465 # A single callback declaration on a cast to a nullable type
1466 class AAsNullableExternCall
1467 super ACastExternCall
1469 # The origin type to cast as nullable
1470 var n_type
: AType is writable, noinit
1473 var n_kwas
: TKwas is writable, noinit
1475 # The `nullable` keyword
1476 var n_kwnullable
: TKwnullable is writable, noinit
1479 # A single callback declaration on a cast to a non-nullable type
1480 class AAsNotNullableExternCall
1481 super ACastExternCall
1483 # The destination type on a cast to not nullable
1484 var n_type
: AType is writable, noinit
1487 var n_kwas
: TKwas is writable, noinit
1490 var n_kwnot
: TKwnot is writable, noinit
1492 # The `nullable` keyword
1493 var n_kwnullable
: TKwnullable is writable, noinit
1496 # A definition of a virtual type
1500 # The `type` keyword
1501 var n_kwtype
: TKwtype is writable, noinit
1503 # The name of the virtual type
1504 var n_qid
: AQclassid is writable, noinit
1506 # The bound of the virtual type
1507 var n_type
: AType is writable, noinit
1510 # The identifier of a method in a method declaration.
1511 # There is a specific class because of operator and setters.
1512 abstract class AMethid
1516 # A method name with a simple identifier
1520 # The simple identifier
1521 var n_id
: TId is writable, noinit
1524 # A method name for an operator
1525 class AOperatorMethid
1528 # The associated operator symbol
1529 var n_op
: Token is writable, noinit
1533 super AOperatorMethid
1538 super AOperatorMethid
1543 super AOperatorMethid
1546 # A method name `**`
1547 class AStarstarMethid
1548 super AOperatorMethid
1553 super AOperatorMethid
1557 class APercentMethid
1558 super AOperatorMethid
1563 super AOperatorMethid
1568 super AOperatorMethid
1573 super AOperatorMethid
1578 super AOperatorMethid
1581 # A method name `==`
1583 super AOperatorMethid
1586 # A method name `!=`
1588 super AOperatorMethid
1591 # A method name `<=`
1593 super AOperatorMethid
1596 # A method name `>=`
1598 super AOperatorMethid
1603 super AOperatorMethid
1608 super AOperatorMethid
1611 # A method name `<<`
1613 super AOperatorMethid
1616 # A method name `>>`
1618 super AOperatorMethid
1621 # A method name `<=>`
1622 class AStarshipMethid
1623 super AOperatorMethid
1626 # A method name `[]`
1631 var n_obra
: TObra is writable, noinit
1634 var n_cbra
: TCbra is writable, noinit
1637 # A setter method name with a simple identifier (with a `=`)
1641 # The base identifier
1642 var n_id
: TId is writable, noinit
1645 var n_assign
: TAssign is writable, noinit
1648 # A method name `[]=`
1649 class ABraassignMethid
1653 var n_obra
: TObra is writable, noinit
1656 var n_cbra
: TCbra is writable, noinit
1659 var n_assign
: TAssign is writable, noinit
1662 # A potentially qualified simple identifier `foo::bar::baz`
1665 # The qualifier, if any
1666 var n_qualified
: nullable AQualified = null is writable
1668 # The final identifier
1669 var n_id
: TId is writable, noinit
1672 # A potentially qualified class identifier `foo::bar::Baz`
1675 # The qualifier, if any
1676 var n_qualified
: nullable AQualified = null is writable
1678 # The final identifier
1679 var n_id
: TClassid is writable, noinit
1682 # A signature in a method definition. eg `(x,y:X,z:Z):T`
1687 var n_opar
: nullable TOpar = null is writable
1689 # The list of parameters
1690 var n_params
= new ANodes[AParam](self)
1693 var n_cpar
: nullable TCpar = null is writable
1696 var n_type
: nullable AType = null is writable
1699 # A parameter definition in a signature. eg `x:X`
1703 # The name of the parameter
1704 var n_id
: TId is writable, noinit
1706 # The type of the parameter, if any
1707 var n_type
: nullable AType = null is writable
1709 # The `...` symbol to indicate varargs
1710 var n_dotdotdot
: nullable TDotdotdot = null is writable
1713 # A static type. eg `nullable X[Y]`
1716 # The `nullable` keyword
1717 var n_kwnullable
: nullable TKwnullable = null is writable
1719 # The name of the class or of the formal type
1720 var n_qid
: AQclassid is writable, noinit
1722 # The opening bracket
1723 var n_obra
: nullable TObra = null is writable
1725 # Type arguments for a generic type
1726 var n_types
= new ANodes[AType](self)
1728 # The closing bracket
1729 var n_cbra
: nullable TCbra = null is writable
1732 # A label at the end of a block or in a break/continue statement. eg `label x`
1736 # The `label` keyword
1737 var n_kwlabel
: TKwlabel is writable, noinit
1739 # The name of the label, if any
1740 var n_id
: nullable TId is writable, noinit
1743 # Expression and statements
1744 # From a AST point of view there is no distinction between statement and expressions (even if the parser has to distinguish them)
1745 abstract class AExpr
1749 # A sequence of `AExpr` (usually statements)
1750 # The last `AExpr` gives the value of the whole block
1754 # The list of statements in the bloc.
1755 # The last element is often considered as an expression that give the value of the whole block.
1756 var n_expr
= new ANodes[AExpr](self)
1759 var n_kwend
: nullable TKwend = null is writable
1762 # A declaration of a local variable. eg `var x: X = y`
1767 var n_kwvar
: nullable TKwvar = null is writable
1769 # The name of the local variable
1770 var n_id
: TId is writable, noinit
1772 # The declaration type of the local variable
1773 var n_type
: nullable AType = null is writable
1775 # The `=` symbol (for the initial value)
1776 var n_assign
: nullable TAssign = null is writable
1778 # The initial value, if any
1779 var n_expr
: nullable AExpr = null is writable
1782 # A `return` statement. eg `return x`
1786 # The `return` keyword
1787 var n_kwreturn
: nullable TKwreturn = null is writable
1790 # A `yield` statement. eg `yield x`
1794 # The `yield` keyword
1795 var n_kwyield
: nullable TKwyield = null is writable
1797 # The return value, if any
1798 var n_expr
: nullable AExpr = null is writable
1801 # Something that has a label.
1802 abstract class ALabelable
1805 # The associated label declatation
1806 var n_label
: nullable ALabel = null is writable
1809 # A `break` or a `continue`
1810 abstract class AEscapeExpr
1814 # The return value, if nay (unused currently)
1815 var n_expr
: nullable AExpr = null is writable
1818 # A `break` statement.
1822 # The `break` keyword
1823 var n_kwbreak
: TKwbreak is writable, noinit
1826 # An `abort` statement
1830 # The `abort` keyword
1831 var n_kwabort
: TKwabort is writable, noinit
1834 # A `continue` statement
1838 # The `continue` keyword.
1839 var n_kwcontinue
: nullable TKwcontinue = null is writable
1848 var n_kwdo
: TKwdo is writable, noinit
1850 # The list of statements of the `do`.
1851 var n_block
: nullable AExpr = null is writable
1853 # The `catch` keyword
1854 var n_kwcatch
: nullable TKwcatch = null is writable
1856 # The do catch block
1857 var n_catch
: nullable AExpr = null is writable
1865 var n_kwif
: TKwif is writable, noinit
1867 # The expression used as the condition of the `if`
1868 var n_expr
: AExpr is writable, noinit
1870 # The `then` keyword
1871 var n_kwthen
: TKwthen is writable, noinit
1873 # The body of the `then` part
1874 var n_then
: nullable AExpr = null is writable
1876 # The `else` keyword
1877 var n_kwelse
: nullable TKwelse = null is writable
1879 # The body of the `else` part
1880 var n_else
: nullable AExpr = null is writable
1883 # A `if` expression (ternary conditional). eg. `if true then 1 else 0`
1888 var n_kwif
: TKwif is writable, noinit
1890 # The expression used as the condition of the `if`
1891 var n_expr
: AExpr is writable, noinit
1893 # The `then` keyword
1894 var n_kwthen
: TKwthen is writable, noinit
1896 # The expression in the `then` part
1897 var n_then
: AExpr is writable, noinit
1899 # The `else` keyword
1900 var n_kwelse
: TKwelse is writable, noinit
1902 # The expression in the `else` part
1903 var n_else
: AExpr is writable, noinit
1906 # A `while` statement
1911 # The `while` keyword
1912 var n_kwwhile
: TKwwhile is writable, noinit
1914 # The expression used as the condition of the `while`
1915 var n_expr
: AExpr is writable, noinit
1918 var n_kwdo
: TKwdo is writable, noinit
1920 # The body of the loop
1921 var n_block
: nullable AExpr = null is writable
1924 # A `loop` statement
1929 # The `loop` keyword
1930 var n_kwloop
: TKwloop is writable, noinit
1932 # The body of the loop
1933 var n_block
: nullable AExpr = null is writable
1942 var n_kwfor
: TKwfor is writable, noinit
1944 # The list of groups to iterate
1945 var n_groups
= new ANodes[AForGroup](self)
1948 var n_kwdo
: TKwdo is writable, noinit
1950 # The body of the loop
1951 var n_block
: nullable AExpr = null is writable
1954 # A collection iterated by a for, its automatic variables and its implicit iterator.
1956 # Standard `for` iterate on a single collection.
1957 # Multiple `for` can iterate on more than one collection at once.
1961 # The list of name of the automatic variables
1962 var n_ids
= new ANodes[TId](self)
1965 var n_kwin
: TKwin is writable, noinit
1967 # The expression used as the collection to iterate on
1968 var n_expr
: AExpr is writable, noinit
1971 # A `with` statement
1976 # The `with` keyword
1977 var n_kwwith
: TKwwith is writable, noinit
1979 # The expression used to get the value to control
1980 var n_expr
: AExpr is writable, noinit
1983 var n_kwdo
: TKwdo is writable, noinit
1985 # The body of the loop
1986 var n_block
: nullable AExpr = null is writable
1989 # An `assert` statement
1993 # The `assert` keyword
1994 var n_kwassert
: TKwassert is writable, noinit
1996 # The name of the assert, if any
1997 var n_id
: nullable TId = null is writable
1999 # The expression used as the condition of the `assert`
2000 var n_expr
: AExpr is writable, noinit
2002 # The `else` keyword
2003 var n_kwelse
: nullable TKwelse = null is writable
2005 # The body to execute when the assert fails
2006 var n_else
: nullable AExpr = null is writable
2009 # Whatever is a simple assignment. eg `= something`
2010 abstract class AAssignFormExpr
2014 var n_assign
: TAssign is writable, noinit
2016 # The right-value to assign.
2017 var n_value
: AExpr is writable, noinit
2020 # Whatever is a combined assignment. eg `+= something`
2021 abstract class AReassignFormExpr
2024 # The combined operator (eg. `+=`)
2025 var n_assign_op
: AAssignOp is writable, noinit
2027 # The right-value to apply on the combined operator.
2028 var n_value
: AExpr is writable, noinit
2031 # A `once` expression. eg `once x`
2035 # The `once` keyword
2036 var n_kwonce
: TKwonce is writable, noinit
2038 # The expression to evaluate only one time
2039 var n_expr
: AExpr is writable, noinit
2042 # A polymorphic invocation of a method
2043 # The form of the invocation (name, arguments, etc.) are specific
2044 abstract class ASendExpr
2046 # The receiver of the method invocation
2047 var n_expr
: AExpr is writable, noinit
2050 # A binary operation on a method
2051 abstract class ABinopExpr
2055 var n_op
: Token is writable, noinit
2057 # The second operand of the operation
2058 # Note: the receiver (`n_expr`) is the first operand
2059 var n_expr2
: AExpr is writable, noinit
2061 # The name of the operator (eg '+')
2062 fun operator
: String is abstract
2065 # Something that is boolean expression
2066 abstract class ABoolExpr
2070 # Something that is binary boolean expression
2071 abstract class ABinBoolExpr
2074 # The first boolean operand
2075 var n_expr
: AExpr is writable, noinit
2078 var n_op
: Token is writable, noinit
2080 # The second boolean operand
2081 var n_expr2
: AExpr is writable, noinit
2089 # A `and` expression
2094 # A `or else` expression
2098 # The `else` keyword
2099 var n_kwelse
: TKwelse is writable, noinit
2102 # A `implies` expression
2107 # A `not` expression
2112 var n_kwnot
: TKwnot is writable, noinit
2114 # The boolean operand of the `not`
2115 var n_expr
: AExpr is writable, noinit
2118 # A `==` or a `!=` expression
2120 # Both have a similar effect on adaptive typing, so this class factorizes the common behavior.
2128 redef fun operator
do return "=="
2134 redef fun operator
do return "!="
2140 redef fun operator
do return "<"
2146 redef fun operator
do return "<="
2152 redef fun operator
do return "<<"
2158 redef fun operator
do return ">"
2164 redef fun operator
do return ">="
2170 redef fun operator
do return ">>"
2173 # A type-ckeck expression. eg `x isa T`
2177 # The expression to check
2178 var n_expr
: AExpr is writable, noinit
2181 var n_kwisa
: TKwisa is writable, noinit
2183 # The destination type to check to
2184 var n_type
: AType is writable, noinit
2190 redef fun operator
do return "+"
2196 redef fun operator
do return "-"
2199 # A `<=>` expression
2202 redef fun operator
do return "<=>"
2208 redef fun operator
do return "*"
2214 redef fun operator
do return "**"
2220 redef fun operator
do return "/"
2226 redef fun operator
do return "%"
2232 redef fun operator
do return "|"
2238 redef fun operator
do return "^"
2244 redef fun operator
do return "&"
2247 # A unary operation on a method
2248 abstract class AUnaryopExpr
2252 var n_op
: Token is writable, noinit
2254 # The name of the operator (eg '+')
2255 fun operator
: String is abstract
2258 # A unary minus expression. eg `-x`
2261 redef fun operator
do return "-"
2264 # A unary plus expression. eg `+x`
2267 redef fun operator
do return "+"
2270 # A unary `~` expression
2273 redef fun operator
do return "~"
2276 # An explicit instantiation. eg `new T`
2281 var n_kwnew
: TKwnew is writable, noinit
2283 # The `type` keyword
2284 var n_type
: AType is writable, noinit
2286 # The name of the named-constructor, if any
2287 var n_qid
: nullable AQid = null is writable
2289 # The arguments of the `new`
2290 var n_args
: AExprs is writable, noinit
2293 # Whatever is a old-style attribute access
2294 abstract class AAttrFormExpr
2297 # The receiver of the attribute
2298 var n_expr
: AExpr is writable, noinit
2300 # The name of the attribute
2301 var n_id
: TAttrid is writable, noinit
2305 # The read of an attribute. eg `x._a`
2310 # The assignment of an attribute. eg `x._a=y`
2311 class AAttrAssignExpr
2313 super AAssignFormExpr
2316 # Whatever looks-like a call with a standard method and any number of arguments.
2317 abstract class ACallFormExpr
2320 # The name of the method
2321 var n_qid
: AQid is writable, noinit
2323 # The arguments of the call
2324 var n_args
: AExprs is writable, noinit
2327 # A complex setter call (standard or brackets)
2328 abstract class ASendReassignFormExpr
2330 super AReassignFormExpr
2333 # A complex attribute assignment. eg `x._a+=y`
2334 class AAttrReassignExpr
2336 super AReassignFormExpr
2339 # A call with a standard method-name and any number of arguments. eg `x.m(y)`. OR just a simple id
2340 # 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`.
2341 # Semantic analysis have to transform them to instance of `AVarExpr`.
2346 # A setter call with a standard method-name and any number of arguments. eg `x.m(y)=z`. OR just a simple assignment.
2347 # 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`.
2348 # Semantic analysis have to transform them to instance of `AVarAssignExpr`.
2349 class ACallAssignExpr
2351 super AAssignFormExpr
2354 # 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.
2355 # 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`.
2356 # Semantic analysis have to transform them to instance of `AVarReassignExpr`.
2357 class ACallReassignExpr
2359 super ASendReassignFormExpr
2362 # A call to `super`. OR a call of a super-constructor
2366 # The qualifier part before the super (currenlty unused)
2367 var n_qualified
: nullable AQualified = null is writable
2369 # The `super` keyword
2370 var n_kwsuper
: TKwsuper is writable, noinit
2372 # The arguments of the super
2373 var n_args
: AExprs is writable, noinit
2376 # A call to the `init` constructor.
2377 # Note: because `init` is a keyword and not a `TId`, the explicit call to init cannot be a `ACallFormExpr`.
2381 # The `init` keyword
2382 var n_kwinit
: TKwinit is writable, noinit
2384 # The arguments of the init
2385 var n_args
: AExprs is writable, noinit
2388 # Whatever looks-like a call of the brackets `[]` operator.
2389 abstract class ABraFormExpr
2392 # The arguments inside the brackets
2393 var n_args
: AExprs is writable, noinit
2396 # A call of the brackets operator. eg `x[y,z]`
2401 # A setter call of the bracket operator. eg `x[y,z]=t`
2402 class ABraAssignExpr
2404 super AAssignFormExpr
2407 # Whatever is an access to a local variable
2408 abstract class AVarFormExpr
2411 # The name of the attribute
2412 var n_id
: TId is writable, noinit
2415 # A complex setter call of the bracket operator. eg `x[y,z]+=t`
2416 class ABraReassignExpr
2418 super ASendReassignFormExpr
2421 # A local variable read access.
2422 # The parser cannot instantiate them, see `ACallExpr`.
2427 # A local variable simple assignment access
2428 # The parser cannot instantiate them, see `ACallAssignExpr`.
2429 class AVarAssignExpr
2431 super AAssignFormExpr
2434 # A local variable complex assignment access
2435 # The parser cannot instantiate them, see `ACallReassignExpr`.
2436 class AVarReassignExpr
2438 super AReassignFormExpr
2441 # A literal range, open or closed
2442 abstract class ARangeExpr
2445 # The left (lower) element of the range
2446 var n_expr
: AExpr is writable, noinit
2449 var n_dotdot
: TDotdot is writable, noinit
2451 # The right (upper) element of the range
2452 var n_expr2
: AExpr is writable, noinit
2455 # A closed literal range. eg `[x..y]`
2459 # The opening bracket `[`
2460 var n_obra
: TObra is writable, noinit
2462 # The closing bracket `]`
2463 var n_cbra
: TCbra is writable, noinit
2466 # An open literal range. eg `[x..y[`
2470 # The opening bracket `[`
2471 var n_obra
: TObra is writable, noinit
2473 # The closing bracket `[` (because open range)
2474 var n_cbra
: TObra is writable, noinit
2477 # A literal array. eg. `[x,y,z]`
2481 # The opening bracket `[`
2482 var n_obra
: TObra is writable, noinit
2484 # The elements of the array
2485 var n_exprs
= new ANodes[AExpr](self)
2487 # The type of the element of the array (if any)
2488 var n_type
: nullable AType = null is writable
2490 # The closing bracket `]`
2491 var n_cbra
: TCbra is writable, noinit
2498 # The `self` keyword
2499 var n_kwself
: nullable TKwself = null is writable
2502 # When there is no explicit receiver, `self` is implicit
2503 class AImplicitSelfExpr
2507 # A `true` boolean literal constant
2511 # The `true` keyword
2512 var n_kwtrue
: TKwtrue is writable, noinit
2515 # A `false` boolean literal constant
2519 # The `false` keyword
2520 var n_kwfalse
: TKwfalse is writable, noinit
2523 # A `null` literal constant
2527 # The `null` keyword
2528 var n_kwnull
: TKwnull is writable, noinit
2531 # An integer literal
2536 var n_integer
: TInteger is writable, noinit
2544 var n_float
: TFloat is writable, noinit
2547 # A character literal
2551 # The character token
2552 var n_char
: TChar is writable, noinit
2556 abstract class AStringFormExpr
2560 var n_string
: Token is writable, noinit
2563 # A simple string. eg. `"abc"`
2565 super AStringFormExpr
2568 # The start of a superstring. eg `"abc{`
2569 class AStartStringExpr
2570 super AStringFormExpr
2573 # The middle of a superstring. eg `}abc{`
2574 class AMidStringExpr
2575 super AStringFormExpr
2578 # The end of a superstrng. eg `}abc"`
2579 class AEndStringExpr
2580 super AStringFormExpr
2583 # A superstring literal. eg `"a{x}b{y}c"`
2584 # Each part is modeled a sequence of expression. eg. `["a{, x, }b{, y, }c"]`
2585 class ASuperstringExpr
2588 # The list of the expressions of the superstring
2589 var n_exprs
= new ANodes[AExpr](self)
2592 # A simple parenthesis. eg `(x)`
2596 # The opening parenthesis
2597 var n_opar
: TOpar is writable, noinit
2599 # The inner expression
2600 var n_expr
: AExpr is writable, noinit
2602 # The closing parenthesis
2603 var n_cpar
: TCpar is writable, noinit
2606 # A cast, against a type or `not null`
2610 # The expression to cast
2611 var n_expr
: AExpr is writable, noinit
2614 var n_kwas
: TKwas is writable, noinit
2616 # The opening parenthesis
2617 var n_opar
: nullable TOpar = null is writable
2619 # The closing parenthesis
2620 var n_cpar
: nullable TCpar = null is writable
2623 # A type cast. eg `x.as(T)`
2627 # The target type to cast to
2628 var n_type
: AType is writable, noinit
2631 # A as-not-null cast. eg `x.as(not null)`
2632 class AAsNotnullExpr
2636 var n_kwnot
: TKwnot is writable, noinit
2638 # The `null` keyword
2639 var n_kwnull
: TKwnull is writable, noinit
2642 # A is-set check of old-style attributes. eg `isset x._a`
2643 class AIssetAttrExpr
2646 # The `isset` keyword
2647 var n_kwisset
: TKwisset is writable, noinit
2650 # An ellipsis notation used to pass an expression as it, in a vararg parameter
2654 # The passed expression
2655 var n_expr
: AExpr is writable, noinit
2658 var n_dotdotdot
: TDotdotdot is writable, noinit
2661 # An named notation used to pass an expression by name in a parameter
2665 # The name of the argument
2666 var n_id
: TId is writable, noinit
2669 var n_assign
: TAssign is writable, noinit
2671 # The passed expression
2672 var n_expr
: AExpr is writable, noinit
2675 # A list of expression separated with commas (arguments for instance)
2679 # The list of expressions
2680 var n_exprs
= new ANodes[AExpr](self)
2683 # A special expression that encapsulates a static type
2684 # Can only be found in special construction like arguments of annotations.
2688 # The encapsulated type
2689 var n_type
: AType is writable, noinit
2692 # A special expression that encapsulates a method identifier
2693 # Can only be found in special construction like arguments of annotations.
2698 var n_expr
: AExpr is writable, noinit
2700 # The encapsulated method identifier
2701 var n_id
: AMethid is writable, noinit
2704 # A special expression that encapsulate an annotation
2705 # Can only be found in special construction like arguments of annotations.
2707 # The encapsulated annotations are in `n_annotations`
2712 # A special expression to debug types
2713 class ADebugTypeExpr
2716 # The `debug` keyword
2717 var n_kwdebug
: TKwdebug is writable, noinit
2719 # The `type` keyword
2720 var n_kwtype
: TKwtype is writable, noinit
2722 # The expression to check
2723 var n_expr
: AExpr is writable, noinit
2726 var n_type
: AType is writable, noinit
2729 # A list of expression separated with commas (arguments for instance)
2730 abstract class AExprs
2733 # The list of expressions
2734 var n_exprs
= new ANodes[AExpr](self)
2737 # A simple list of expressions
2742 # A list of expressions enclosed in parentheses
2746 # The opening parenthesis
2747 var n_opar
: TOpar is writable, noinit
2749 # The closing parenthesis
2750 var n_cpar
: TCpar is writable, noinit
2753 # A list of expressions enclosed in brackets
2757 # The opening bracket
2758 var n_obra
: TObra is writable, noinit
2760 # The closing bracket
2761 var n_cbra
: TCbra is writable, noinit
2764 # A complex assignment operator. (`+=` and `-=`)
2765 abstract class AAssignOp
2768 # The combined assignment operator
2769 var n_op
: Token is writable, noinit
2771 # The name of the operator without the `=` (eg '+')
2772 fun operator
: String is abstract
2775 # A `+=` assignment operation
2779 redef fun operator
do return "+"
2782 # A `-=` assignment operation
2783 class AMinusAssignOp
2786 redef fun operator
do return "-"
2789 # A `*=` assignment operation
2793 redef fun operator
do return "*"
2796 # A `/=` assignment operation
2797 class ASlashAssignOp
2800 redef fun operator
do return "/"
2803 # A `%=` assignment operation
2804 class APercentAssignOp
2807 redef fun operator
do return "%"
2810 # A `**=` assignment operation
2811 class AStarstarAssignOp
2814 redef fun operator
do return "**"
2817 # A `|=` assignment operation
2821 redef fun operator
do return "|"
2824 # A `^=` assignment operation
2825 class ACaretAssignOp
2828 redef fun operator
do return "^"
2831 # A `&=` assignment operation
2835 redef fun operator
do return "&"
2838 # A `<<=` assignment operation
2842 redef fun operator
do return "<<"
2845 # A `>>=` assignment operation
2849 redef fun operator
do return ">>"
2852 # A possibly fully-qualified module identifier
2856 # The starting quad (`::`)
2857 var n_quad
: nullable TQuad = null is writable
2859 # The list of quad-separated package/group identifiers
2860 var n_path
= new ANodes[TId](self)
2862 # The final module identifier
2863 var n_id
: TId is writable, noinit
2866 # A language declaration for an extern block
2871 var n_kwin
: TKwin is writable, noinit
2874 var n_string
: TString is writable, noinit
2877 # An full extern block
2878 class AExternCodeBlock
2881 # The language declration
2882 var n_in_language
: nullable AInLanguage = null is writable
2884 # The block of extern code
2885 var n_extern_code_segment
: TExternCodeSegment is writable, noinit
2888 # A possible full method qualifier.
2892 # The starting quad (`::`)
2893 var n_quad
: nullable TQuad = null is writable
2895 # The list of quad-separated package/group/module identifiers
2896 var n_id
= new ANodes[TId](self)
2898 # A class identifier
2899 var n_classid
: nullable TClassid = null is writable
2902 # A documentation of a definition
2903 # It contains the block of comments just above the declaration
2907 # A list of lines of comment
2908 var n_comment
= new ANodes[TComment](self)
2911 # A group of annotation on a node
2913 # This same class is used for the 3 kind of annotations:
2915 # * *is* annotations. eg `module foo is bar`.
2916 # * *at* annotations. eg `foo@bar` or `foo@(bar,baz)`.
2917 # * *class* annotations, defined in classes.
2921 # The `is` keyword, for *is* annotations
2922 var n_kwis
: nullable TKwis = null is writable
2924 # The `@` symbol, for *at* annotations
2925 var n_at
: nullable TAt = null is writable
2927 # The opening parenthesis in *at* annotations
2928 var n_opar
: nullable TOpar = null is writable
2930 # The list of annotations
2931 var n_items
= new ANodes[AAnnotation](self)
2933 # The closing parenthesis in *at* annotations
2934 var n_cpar
: nullable TCpar = null is writable
2936 # The `end` keyword, for *is* annotations
2937 var n_kwend
: nullable TKwend = null is writable
2940 # A single annotation
2944 # The name of the annotation
2945 var n_atid
: AAtid is writable, noinit
2947 # The opening parenthesis of the arguments
2948 var n_opar
: nullable TOpar = null is writable
2950 # The list of arguments
2951 var n_args
= new ANodes[AExpr](self)
2953 # The closing parenthesis
2954 var n_cpar
: nullable TCpar = null is writable
2956 # The name of the annotation
2959 return n_atid
.n_id
.text
2963 # An annotation name
2964 abstract class AAtid
2967 # The identifier of the annotation.
2968 # Can be a TId of a keyword
2969 var n_id
: Token is writable, noinit
2972 # An annotation name based on an identifier
2977 # An annotation name based on the keyword `extern`
2982 # An annotation name based on the keyword `import`
2987 # An annotation name based on the keyword `abstract`
2988 class AKwabstractAtid
2992 # The root of the AST
2997 var n_base
: nullable AModule is writable
2999 # The end of file (or error) token
3000 var n_eof
: EOF is writable