60a1ec94d58ae9a8b91a4163acf2808ee63d70c8
[nit.git] / src / parser / xss / nodes.xss
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2  *
3  * Copyright 2008 Jean Privat <jean@pryen.org>
4  * Based on algorithms developped for ( http://www.sablecc.org/ ).
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 $ template make_abs_nodes()
20 # Root of the AST hierarchy
21 abstract class PNode
22         readable var _location: nullable Location
23 end
24
25 # Ancestor of all tokens
26 abstract class Token
27 special PNode
28 end
29
30 # Ancestor of all productions
31 abstract class Prod
32 special PNode
33         fun location=(loc: nullable Location) do _location = loc
34 end
35 $ end template
36
37 $ template make_nodes()
38 redef class PNode
39         # Parent of the node in the AST
40         readable writable var _parent: nullable PNode
41
42         # Remove a child from the AST
43         fun remove_child(child: PNode)
44         do
45                 replace_child(child, null)
46         end
47
48         # Replace a child with an other node in the AST
49         fun replace_child(old_child: PNode, new_child: nullable PNode) is abstract
50
51         # Replace itself with an other node in the AST
52         fun replace_with(node: PNode)
53         do
54                 if (_parent != null) then
55                         _parent.replace_child(self, node)
56                 end
57         end
58
59         # Visit all nodes in order.
60         # Thus, call "v.visit(e)" for each node e
61         fun visit_all(v: Visitor) is abstract
62
63         # Visit all nodes in reverse order.
64         # Thus, call "v.visit(e)" for each node e starting from the last child
65         fun visit_all_reverse(v: Visitor) is abstract
66
67         # Give a human readable location of the node.
68         fun locate: String
69         do
70                 if location == null then
71                         return "????"
72                 end
73                 return location.to_s
74         end
75
76         # Debug method: output a message prefixed with the location.
77         fun printl(str: String)
78         do
79                 print("{locate}: {str}\n")
80         end
81 end
82
83 redef class Token
84         redef fun visit_all(v: Visitor) do end
85         redef fun visit_all_reverse(v: Visitor) do end
86         redef fun replace_child(old_child: PNode, new_child: nullable PNode) do end
87 end
88
89 redef class Prod
90         # The first token of the production node
91         readable writable var _first_token: nullable Token
92
93         # The last token of the production node
94         readable writable var _last_token: nullable Token
95
96         redef fun replace_with(n: PNode)
97         do
98                 super
99                 assert n isa Prod
100                 n.first_token = first_token
101                 n.last_token = last_token
102                 n.location = location
103         end
104 end
105
106 # Abstract standard visitor
107 class Visitor
108         # What the visitor do when a node is visited
109         # Concrete visitors should redefine this method.
110         protected fun visit(e: nullable PNode) is abstract
111
112         # Ask the visitor to visit a given node.
113         # Usually automatically called by visit_all* methods.
114         # This methos should not be redefined
115         fun enter_visit(e: nullable PNode)
116         do
117                 var old = _current_node
118                 _current_node = e
119                 visit(e)
120                 _current_node = old
121         end
122
123         # The current visited node
124         readable var _current_node: nullable PNode = null
125 end
126
127 $ end template