First NIT release and new clean mercurial repository
[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 attr _parent: PNode 
39
40         # Remove a child from the AST
41         meth 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         meth replace_child(old_child: PNode, new_child: PNode) is abstract
48
49         # Replace itself with an other node in the AST
50         meth 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         meth 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         meth visit_all_reverse(v: Visitor) is abstract
64
65         # Give a human readable location of the node.
66         meth locate: String is abstract
67
68         # Debug method: output a message prefixed with the location.
69         meth printl(str: String)
70         do
71                 print("{locate}: {str}\n")
72         end
73 end
74
75 redef class Token
76         redef meth visit_all(v: Visitor) do end
77         redef meth visit_all_reverse(v: Visitor) do end
78         redef meth replace_child(old_child: PNode, new_child: PNode) do end
79
80         redef meth locate: String
81         do
82                 return "{filename}:{line},{pos}"
83         end
84 end
85
86 redef class Prod
87         # The first token of the production node
88         readable writable attr _first_token: Token 
89
90         # The last token of the production node
91         readable writable attr _last_token: Token 
92
93         redef meth locate: String
94         do
95                 if first_token == null then
96                         return "????"
97                 end
98                 var lastpos = last_token.pos + last_token.text.length - 1
99                 if first_token.line == last_token.line then
100                         return "{first_token.locate}--{lastpos}"
101                 else
102                         return "{first_token.locate}--{last_token.line}:{lastpos}"
103                 end
104         end
105
106         redef meth replace_with(n: PNode)
107         do
108                 super
109                 assert n isa Prod
110                 n.first_token = first_token
111                 n.last_token = last_token
112         end
113 end
114
115 # Abstract standard visitor
116 class Visitor
117         # Ask the visitor to visit a given node.
118         # Usually automatically called by visit_all* methods.
119         # Concrete visitors should redefine this method.
120         meth visit(e: PNode) is abstract
121 end
122
123 $ end template