1 //
2 // Copyright (c) 2006, Brian Frank and Andy Frank
3 // Licensed under the Academic Free License version 3.0
4 //
5 // History:
6 // 15 Sep 05 Brian Frank Creation
7 // 26 Aug 06 Brian Frank Ported from Java to Fan
8 //
9
10 **
11 ** Vistor is used to walk the abstract syntax tree and visit key nodes.
12 ** The walk for each node type entails:
13 ** 1. enter
14 ** 2. children
15 ** 3. exit
16 ** 4. visit
17 **
18 mixin Visitor
19 {
20
21 //////////////////////////////////////////////////////////////////////////
22 // Visit
23 //////////////////////////////////////////////////////////////////////////
24
25 **
26 ** Peform a walk of the abstract syntax tree down
27 ** to the specified depth.
28 **
29 Void walk(TypeDef[] typeDefs, VisitDepth depth)
30 {
31 typeDefs.each |TypeDef def| { def.walk(this, depth) }
32 }
33
34 //////////////////////////////////////////////////////////////////////////
35 // TypeDef Callbacks
36 //////////////////////////////////////////////////////////////////////////
37
38 **
39 ** Callback when entering a type definition.
40 **
41 virtual Void enterTypeDef(TypeDef def) {}
42
43 **
44 ** Callback when exiting a type definition.
45 **
46 virtual Void exitTypeDef(TypeDef def) {}
47
48 **
49 ** Callback when visiting a type definition.
50 **
51 virtual Void visitTypeDef(TypeDef def) {}
52
53 //////////////////////////////////////////////////////////////////////////
54 // FieldDef Callbacks
55 //////////////////////////////////////////////////////////////////////////
56
57 **
58 ** Callback when entering a field definition.
59 **
60 virtual Void enterFieldDef(FieldDef def) {}
61
62 **
63 ** Callback when exiting a field definition.
64 **
65 virtual Void exitFieldDef(FieldDef def) {}
66
67 **
68 ** Callback when visiting a field definition.
69 **
70 virtual Void visitFieldDef(FieldDef def) {}
71
72 //////////////////////////////////////////////////////////////////////////
73 // MethodDef Callbacks
74 //////////////////////////////////////////////////////////////////////////
75
76 **
77 ** Callback when entering a method.
78 **
79 virtual Void enterMethodDef(MethodDef def) {}
80
81 **
82 ** Callback when exiting a method.
83 **
84 virtual Void exitMethodDef(MethodDef def) {}
85
86 **
87 ** Callback when visiting a method.
88 **
89 virtual Void visitMethodDef(MethodDef def) {}
90
91 //////////////////////////////////////////////////////////////////////////
92 // Block Callbacks
93 //////////////////////////////////////////////////////////////////////////
94
95 **
96 ** Callback when entering a block.
97 **
98 virtual Void enterBlock(Block block) {}
99
100 **
101 ** Callback when exiting a block.
102 **
103 virtual Void exitBlock(Block block) {}
104
105 **
106 ** Callback when visiting a block.
107 **
108 virtual Void visitBlock(Block block) {}
109
110 //////////////////////////////////////////////////////////////////////////
111 // Stmt Callbacks
112 //////////////////////////////////////////////////////////////////////////
113
114 **
115 ** Callback when entering a stmt.
116 **
117 virtual Void enterStmt(Stmt stmt) {}
118
119 **
120 ** Callback when exiting a stmt.
121 **
122 virtual Void exitStmt(Stmt stmt) {}
123
124 **
125 ** Callback when visiting a stmt.
126 **
127 virtual Void visitStmt(Stmt stmt) {}
128
129 **
130 ** Callback when entering a finally block
131 **
132 virtual Void enterFinally(TryStmt stmt) {}
133
134 **
135 ** Callback when exiting a finally block
136 **
137 virtual Void exitFinally(TryStmt stmt) {}
138
139 //////////////////////////////////////////////////////////////////////////
140 // Expr Callbacks
141 //////////////////////////////////////////////////////////////////////////
142
143 **
144 ** Call to visit an expression. Return expr or a new
145 ** expression if doing a replacement for the expression in
146 ** the abstract syntax tree.
147 **
148 virtual Expr visitExpr(Expr expr) { return expr }
149 }
150
151 **************************************************************************
152 ** VisitDepth
153 **************************************************************************
154
155 **
156 ** VisitDepth enumerates how deep to traverse the AST
157 **
158 internal enum VisitDepth { typeDef, slotDef, stmt, expr }
159
160 **************************************************************************
161 ** ExprVisitor
162 **************************************************************************
163
164 **
165 ** ExprVisitor implements a Visitor which visits
166 ** each expr using a closure.
167 **
168 internal class ExprVisitor : Visitor
169 {
170 new make(|Expr expr->Expr| func)
171 {
172 this.func = func
173 }
174
175 override Expr visitExpr(Expr expr)
176 {
177 return (Expr)func.call1(expr)
178 }
179
180 |Expr expr->Expr| func
181 }
182
183 **************************************************************************
184 ** TreeWriter
185 **************************************************************************
186
187 /*
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
2 // Copyright (c) 2006, Brian Frank and Andy Frank
3 // Licensed under the Academic Free License version 3.0
4 //
5 // History:
6 // 15 Sep 05 Brian Frank Creation
7 // 26 Aug 06 Brian Frank Ported from Java to Fan
8 //
9
10 **
11 ** Vistor is used to walk the abstract syntax tree and visit key nodes.
12 ** The walk for each node type entails:
13 ** 1. enter
14 ** 2. children
15 ** 3. exit
16 ** 4. visit
17 **
18 mixin Visitor
19 {
20
21 //////////////////////////////////////////////////////////////////////////
22 // Visit
23 //////////////////////////////////////////////////////////////////////////
24
25 **
26 ** Peform a walk of the abstract syntax tree down
27 ** to the specified depth.
28 **
29 Void walk(TypeDef[] typeDefs, VisitDepth depth)
30 {
31 typeDefs.each |TypeDef def| { def.walk(this, depth) }
32 }
33
34 //////////////////////////////////////////////////////////////////////////
35 // TypeDef Callbacks
36 //////////////////////////////////////////////////////////////////////////
37
38 **
39 ** Callback when entering a type definition.
40 **
41 virtual Void enterTypeDef(TypeDef def) {}
42
43 **
44 ** Callback when exiting a type definition.
45 **
46 virtual Void exitTypeDef(TypeDef def) {}
47
48 **
49 ** Callback when visiting a type definition.
50 **
51 virtual Void visitTypeDef(TypeDef def) {}
52
53 //////////////////////////////////////////////////////////////////////////
54 // FieldDef Callbacks
55 //////////////////////////////////////////////////////////////////////////
56
57 **
58 ** Callback when entering a field definition.
59 **
60 virtual Void enterFieldDef(FieldDef def) {}
61
62 **
63 ** Callback when exiting a field definition.
64 **
65 virtual Void exitFieldDef(FieldDef def) {}
66
67 **
68 ** Callback when visiting a field definition.
69 **
70 virtual Void visitFieldDef(FieldDef def) {}
71
72 //////////////////////////////////////////////////////////////////////////
73 // MethodDef Callbacks
74 //////////////////////////////////////////////////////////////////////////
75
76 **
77 ** Callback when entering a method.
78 **
79 virtual Void enterMethodDef(MethodDef def) {}
80
81 **
82 ** Callback when exiting a method.
83 **
84 virtual Void exitMethodDef(MethodDef def) {}
85
86 **
87 ** Callback when visiting a method.
88 **
89 virtual Void visitMethodDef(MethodDef def) {}
90
91 //////////////////////////////////////////////////////////////////////////
92 // Block Callbacks
93 //////////////////////////////////////////////////////////////////////////
94
95 **
96 ** Callback when entering a block.
97 **
98 virtual Void enterBlock(Block block) {}
99
100 **
101 ** Callback when exiting a block.
102 **
103 virtual Void exitBlock(Block block) {}
104
105 **
106 ** Callback when visiting a block.
107 **
108 virtual Void visitBlock(Block block) {}
109
110 //////////////////////////////////////////////////////////////////////////
111 // Stmt Callbacks
112 //////////////////////////////////////////////////////////////////////////
113
114 **
115 ** Callback when entering a stmt.
116 **
117 virtual Void enterStmt(Stmt stmt) {}
118
119 **
120 ** Callback when exiting a stmt.
121 **
122 virtual Void exitStmt(Stmt stmt) {}
123
124 **
125 ** Callback when visiting a stmt.
126 **
127 virtual Void visitStmt(Stmt stmt) {}
128
129 **
130 ** Callback when entering a finally block
131 **
132 virtual Void enterFinally(TryStmt stmt) {}
133
134 **
135 ** Callback when exiting a finally block
136 **
137 virtual Void exitFinally(TryStmt stmt) {}
138
139 //////////////////////////////////////////////////////////////////////////
140 // Expr Callbacks
141 //////////////////////////////////////////////////////////////////////////
142
143 **
144 ** Call to visit an expression. Return expr or a new
145 ** expression if doing a replacement for the expression in
146 ** the abstract syntax tree.
147 **
148 virtual Expr visitExpr(Expr expr) { return expr }
149 }
150
151 **************************************************************************
152 ** VisitDepth
153 **************************************************************************
154
155 **
156 ** VisitDepth enumerates how deep to traverse the AST
157 **
158 internal enum VisitDepth { typeDef, slotDef, stmt, expr }
159
160 **************************************************************************
161 ** ExprVisitor
162 **************************************************************************
163
164 **
165 ** ExprVisitor implements a Visitor which visits
166 ** each expr using a closure.
167 **
168 internal class ExprVisitor : Visitor
169 {
170 new make(|Expr expr->Expr| func)
171 {
172 this.func = func
173 }
174
175 override Expr visitExpr(Expr expr)
176 {
177 return (Expr)func.call1(expr)
178 }
179
180 |Expr expr->Expr| func
181 }
182
183 **************************************************************************
184 ** TreeWriter
185 **************************************************************************
186
187 /*
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210