1 $ // This file is part of NIT ( http://www.nitlanguage.org ).
3 $ // Copyright 2008 Jean Privat <jean@pryen.org>
4 $ // Based on algorithms developped for ( http://www.sablecc.org/ ).
6 $ // Licensed under the Apache License, Version 2.0 (the "License");
7 $ // you may not use this file except in compliance with the License.
8 $ // You may obtain a copy of the License at
10 $ // http://www.apache.org/licenses/LICENSE-2.0
12 $ // Unless required by applicable law or agreed to in writing, software
13 $ // distributed under the License is distributed on an "AS IS" BASIS,
14 $ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 $ // See the License for the specific language governing permissions and
16 $ // limitations under the License.
18 $ template make_abs_nodes()
19 # Root of the AST hierarchy
21 readable var _location: nullable Location
24 # Ancestor of all tokens
29 # Ancestor of all productions
32 fun location=(loc: nullable Location) do _location = loc
36 $ template make_nodes()
38 # Parent of the node in the AST
39 readable writable var _parent: nullable PNode
41 # Remove a child from the AST
42 fun remove_child(child: PNode)
44 replace_child(child, null)
47 # Replace a child with an other node in the AST
48 fun replace_child(old_child: PNode, new_child: nullable PNode) is abstract
50 # Replace itself with an other node in the AST
51 fun replace_with(node: PNode)
53 if (_parent != null) then
54 _parent.replace_child(self, node)
58 # Visit all nodes in order.
59 # Thus, call "v.visit(e)" for each node e
60 fun visit_all(v: Visitor) is abstract
62 # Visit all nodes in reverse order.
63 # Thus, call "v.visit(e)" for each node e starting from the last child
64 fun visit_all_reverse(v: Visitor) is abstract
66 # Debug method: output a message prefixed with the location.
67 fun printl(str: String)
69 if location == null then
72 print("{location}: {str}\n")
78 redef fun visit_all(v: Visitor) do end
79 redef fun visit_all_reverse(v: Visitor) do end
80 redef fun replace_child(old_child: PNode, new_child: nullable PNode) do end
84 # The first token of the production node
85 readable writable var _first_token: nullable Token
87 # The last token of the production node
88 readable writable var _last_token: nullable Token
90 redef fun replace_with(n: PNode)
94 n.first_token = first_token
95 n.last_token = last_token
100 # Abstract standard visitor
102 # What the visitor do when a node is visited
103 # Concrete visitors should redefine this method.
104 protected fun visit(e: nullable PNode) is abstract
106 # Ask the visitor to visit a given node.
107 # Usually automatically called by visit_all* methods.
108 # This methos should not be redefined
109 fun enter_visit(e: nullable PNode)
111 var old = _current_node
117 # The current visited node
118 readable var _current_node: nullable PNode = null