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