+
+ # The location of the important part of the node (identifier or whatever)
+ fun hot_location: Location do return location
+
+ # Display a message for the colored location of the node
+ fun debug(message: String)
+ do
+ print "{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}"
+ end
+
+ # Parent of the node in the AST
+ var parent: nullable ANode = null
+
+ # Protect form invalid instantiation of nodes
+ private init do end
+
+ # Replace a child with an other node in the AST
+ private fun replace_child(old_child: ANode, new_child: nullable ANode) is abstract
+
+ # Detach a node from its parent
+ # Aborts if the node is not detashable. use `replace_with` instead
+ # REQUIRE: parent != null
+ # REQUIRE: is_detachable
+ # ENDURE: parent == null
+ fun detach
+ do
+ assert parent != null
+ parent.replace_child(self, null)
+ parent = null
+ end
+
+ # Replace itself with an other node in the AST
+ # REQUIRE: parent != null
+ # ENSURE: node.parent == old(parent)
+ # ENSURE: parent == null
+ fun replace_with(node: ANode)
+ do
+ assert parent != null
+ parent.replace_child(self, node)
+ parent = null
+ end
+
+ # Visit all nodes in order.
+ # Thus, call `v.enter_visit(e)` for each child `e`
+ fun visit_all(v: Visitor) is abstract
+end
+
+# A sequence of nodes
+# There is a specifc class (instead of a using Array) to track the parent/child relation when nodes are added or removed
+class ANodes[E: ANode]
+ super Sequence[E]
+ private var parent: ANode
+ private var items = new Array[E]
+ redef fun iterator do return items.iterator
+ redef fun length do return items.length
+ redef fun is_empty do return items.is_empty
+ redef fun push(e)
+ do
+ hook_add(e)
+ items.push(e)
+ end
+ redef fun pop
+ do
+ var res = items.pop
+ hook_remove(res)
+ return res
+ end
+ redef fun unshift(e)
+ do
+ hook_add(e)
+ items.unshift(e)
+ end
+ redef fun shift
+ do
+ var res = items.shift
+ hook_remove(res)
+ return res
+ end
+ redef fun has(e)
+ do
+ return items.has(e)
+ end
+ redef fun [](index)
+ do
+ return items[index]
+ end
+ redef fun []=(index, e)
+ do
+ hook_remove(self[index])
+ hook_add(e)
+ items[index]=e
+ end
+ redef fun remove_at(index)
+ do
+ hook_remove(items[index])
+ items.remove_at(index)
+ end
+ private fun hook_add(e: E)
+ do
+ #assert e.parent == null
+ e.parent = parent
+ end
+ private fun hook_remove(e: E)
+ do
+ assert e.parent == parent
+ e.parent = null
+ end