tools: add 'Location' class to use in AST and errors
[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 end
34 $ end template
35
36 $ template make_nodes()
37 redef class PNode
38         # Parent of the node in the AST
39         readable writable var _parent: nullable PNode
40
41         # Remove a child from the AST
42         fun remove_child(child: PNode)
43         do
44                 replace_child(child, null)
45         end
46
47         # Replace a child with an other node in the AST
48         fun replace_child(old_child: PNode, new_child: nullable PNode) is abstract
49
50         # Replace itself with an other node in the AST
51         fun replace_with(node: PNode)
52         do
53                 if (_parent != null) then
54                         _parent.replace_child(self, node)
55                 end
56         end
57
58         # Visit all nodes in order.
59         # Thus, call "v.visit(e)" for each node e
60         fun visit_all(v: Visitor) is abstract
61
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
65
66         # Give a human readable location of the node.
67         fun locate: String
68         do
69                 if location == null then
70                         return "????"
71                 end
72                 return location.to_s
73         end
74
75
76         # Return only the line number of the node
77         fun line_number: Int is abstract
78
79         # Debug method: output a message prefixed with the location.
80         fun printl(str: String)
81         do
82                 print("{locate}: {str}\n")
83         end
84 end
85
86 redef class Token
87         redef fun visit_all(v: Visitor) do end
88         redef fun visit_all_reverse(v: Visitor) do end
89         redef fun replace_child(old_child: PNode, new_child: nullable PNode) do end
90
91         redef fun line_number do return line
92 end
93
94 redef class Prod
95         # The first token of the production node
96         readable writable var _first_token: nullable Token
97
98         # The last token of the production node
99         readable writable var _last_token: nullable Token
100
101         redef fun replace_with(n: PNode)
102         do
103                 super
104                 assert n isa Prod
105                 n.first_token = first_token
106                 n.last_token = last_token
107                 n._location = location
108         end
109
110         redef fun line_number
111         do
112                 if first_token != null then
113                         return first_token.line
114                 else
115                         return 0
116                 end
117         end
118 end
119
120 # Abstract standard visitor
121 class Visitor
122         # Ask the visitor to visit a given node.
123         # Usually automatically called by visit_all* methods.
124         # Concrete visitors should redefine this method.
125         fun visit(e: nullable PNode) is abstract
126 end
127
128 $ end template