metamodel: rename 'universal' to 'enum'
[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 $ template make_abs_nodes()
19 # Root of the AST hierarchy
20 abstract class PNode
21         var _location: nullable Location
22
23         # Location is set during AST building. Once built, location cannon be null
24         # However, manual instanciated nodes may need mode care
25         fun location: Location do return _location.as(not null)
26 end
27
28 # Ancestor of all tokens
29 abstract class Token
30         super PNode
31 end
32
33 # Ancestor of all productions
34 abstract class Prod
35         super PNode
36         fun location=(loc: Location) do _location = loc
37 end
38 $ end template
39
40 $ template make_nodes()
41 redef class PNode
42         # Parent of the node in the AST
43         readable writable var _parent: nullable PNode
44
45         # Remove a child from the AST
46         fun remove_child(child: PNode)
47         do
48                 replace_child(child, null)
49         end
50
51         # Replace a child with an other node in the AST
52         fun replace_child(old_child: PNode, new_child: nullable PNode) is abstract
53
54         # Replace itself with an other node in the AST
55         fun replace_with(node: PNode)
56         do
57                 if (_parent != null) then
58                         _parent.replace_child(self, node)
59                 end
60         end
61
62         # Visit all nodes in order.
63         # Thus, call "v.visit(e)" for each node e
64         fun visit_all(v: Visitor) is abstract
65 end
66
67 redef class Token
68         redef fun visit_all(v: Visitor) do end
69         redef fun replace_child(old_child: PNode, new_child: nullable PNode) do end
70 end
71
72 redef class Prod
73         redef fun replace_with(n: PNode)
74         do
75                 super
76                 assert n isa Prod
77                 n.location = location
78         end
79 end
80
81 # Abstract standard visitor
82 class Visitor
83         # What the visitor do when a node is visited
84         # Concrete visitors should redefine this method.
85         protected fun visit(e: nullable PNode) is abstract
86
87         # Ask the visitor to visit a given node.
88         # Usually automatically called by visit_all* methods.
89         # This methos should not be redefined
90         fun enter_visit(e: nullable PNode)
91         do
92                 var old = _current_node
93                 _current_node = e
94                 visit(e)
95                 _current_node = old
96         end
97
98         # The current visited node
99         readable var _current_node: nullable PNode = null
100 end
101
102 $ end template