KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > js > ast > JsVisitor


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.js.ast;
17
18 import com.google.gwt.dev.jjs.InternalCompilerException;
19 import com.google.gwt.dev.js.ast.JsVars.JsVar;
20
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * Implemented by nodes that will visit child nodes.
25  */

26 public class JsVisitor {
27
28   protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() {
29
30     public boolean canInsert() {
31       return false;
32     }
33
34     public boolean canRemove() {
35       return false;
36     }
37
38     public void insertAfter(JsNode node) {
39       throw new UnsupportedOperationException JavaDoc();
40     }
41
42     public void insertBefore(JsNode node) {
43       throw new UnsupportedOperationException JavaDoc();
44     }
45
46     public void removeMe() {
47       throw new UnsupportedOperationException JavaDoc();
48     }
49
50     public void replaceMe(JsNode node) {
51       throw new UnsupportedOperationException JavaDoc();
52     }
53
54   };
55
56   public final void accept(JsCollection collection) {
57     doAccept(collection);
58   }
59
60   public final JsExpression accept(JsExpression node) {
61     return (JsExpression) doAccept(node);
62   }
63
64   public final JsNode accept(JsNode node) {
65     return doAccept(node);
66   }
67
68   public final JsStatement accept(JsStatement node) {
69     return (JsStatement) doAccept(node);
70   }
71
72   public final void acceptWithInsertRemove(JsCollection collection) {
73     doAcceptWithInsertRemove(collection);
74   }
75
76   public boolean didChange() {
77     throw new UnsupportedOperationException JavaDoc();
78   }
79
80   public void endVisit(JsArrayAccess x, JsContext ctx) {
81   }
82
83   public void endVisit(JsArrayLiteral x, JsContext ctx) {
84   }
85
86   public void endVisit(JsBinaryOperation x, JsContext ctx) {
87   }
88
89   public void endVisit(JsBlock x, JsContext ctx) {
90   }
91
92   public void endVisit(JsBooleanLiteral x, JsContext ctx) {
93   }
94
95   public void endVisit(JsBreak x, JsContext ctx) {
96   }
97
98   public void endVisit(JsCase x, JsContext ctx) {
99   }
100
101   public void endVisit(JsCatch x, JsContext ctx) {
102   }
103
104   public void endVisit(JsConditional x, JsContext ctx) {
105   }
106
107   public void endVisit(JsContinue x, JsContext ctx) {
108   }
109
110   public void endVisit(JsDebugger x, JsContext ctx) {
111   }
112
113   public void endVisit(JsDecimalLiteral x, JsContext ctx) {
114   }
115
116   public void endVisit(JsDefault x, JsContext ctx) {
117   }
118
119   public void endVisit(JsDoWhile x, JsContext ctx) {
120   }
121
122   public void endVisit(JsEmpty x, JsContext ctx) {
123   }
124
125   public void endVisit(JsExprStmt x, JsContext ctx) {
126   }
127
128   public void endVisit(JsFor x, JsContext ctx) {
129   }
130
131   public void endVisit(JsForIn x, JsContext ctx) {
132   }
133
134   public void endVisit(JsFunction x, JsContext ctx) {
135   }
136
137   public void endVisit(JsIf x, JsContext ctx) {
138   }
139
140   public void endVisit(JsIntegralLiteral x, JsContext ctx) {
141   }
142
143   public void endVisit(JsInvocation x, JsContext ctx) {
144   }
145
146   public void endVisit(JsLabel x, JsContext ctx) {
147   }
148
149   public void endVisit(JsNameRef x, JsContext ctx) {
150   }
151
152   public void endVisit(JsNew x, JsContext ctx) {
153   }
154
155   public void endVisit(JsNullLiteral x, JsContext ctx) {
156   }
157
158   public void endVisit(JsObjectLiteral x, JsContext ctx) {
159   }
160
161   public void endVisit(JsParameter x, JsContext ctx) {
162   }
163
164   public void endVisit(JsPostfixOperation x, JsContext ctx) {
165   }
166
167   public void endVisit(JsPrefixOperation x, JsContext ctx) {
168   }
169
170   public void endVisit(JsProgram x, JsContext ctx) {
171   }
172
173   public void endVisit(JsPropertyInitializer x, JsContext ctx) {
174   }
175
176   public void endVisit(JsRegExp x, JsContext ctx) {
177   }
178
179   public void endVisit(JsReturn x, JsContext ctx) {
180   }
181
182   public void endVisit(JsStringLiteral x, JsContext ctx) {
183   }
184
185   public void endVisit(JsSwitch x, JsContext ctx) {
186   }
187
188   public void endVisit(JsThisRef x, JsContext ctx) {
189   }
190
191   public void endVisit(JsThrow x, JsContext ctx) {
192   }
193
194   public void endVisit(JsTry x, JsContext ctx) {
195   }
196
197   public void endVisit(JsVar x, JsContext ctx) {
198   }
199
200   public void endVisit(JsVars x, JsContext ctx) {
201   }
202
203   public void endVisit(JsWhile x, JsContext ctx) {
204   }
205
206   public boolean visit(JsArrayAccess x, JsContext ctx) {
207     return true;
208   }
209
210   public boolean visit(JsArrayLiteral x, JsContext ctx) {
211     return true;
212   }
213
214   public boolean visit(JsBinaryOperation x, JsContext ctx) {
215     return true;
216   }
217
218   public boolean visit(JsBlock x, JsContext ctx) {
219     return true;
220   }
221
222   public boolean visit(JsBooleanLiteral x, JsContext ctx) {
223     return true;
224   }
225
226   public boolean visit(JsBreak x, JsContext ctx) {
227     return true;
228   }
229
230   public boolean visit(JsCase x, JsContext ctx) {
231     return true;
232   }
233
234   public boolean visit(JsCatch x, JsContext ctx) {
235     return true;
236   }
237
238   public boolean visit(JsConditional x, JsContext ctx) {
239     return true;
240   }
241
242   public boolean visit(JsContinue x, JsContext ctx) {
243     return true;
244   }
245
246   public boolean visit(JsDebugger x, JsContext ctx) {
247     return true;
248   }
249
250   public boolean visit(JsDecimalLiteral x, JsContext ctx) {
251     return true;
252   }
253
254   public boolean visit(JsDefault x, JsContext ctx) {
255     return true;
256   }
257
258   public boolean visit(JsDoWhile x, JsContext ctx) {
259     return true;
260   }
261
262   public boolean visit(JsEmpty x, JsContext ctx) {
263     return true;
264   }
265
266   public boolean visit(JsExprStmt x, JsContext ctx) {
267     return true;
268   }
269
270   public boolean visit(JsFor x, JsContext ctx) {
271     return true;
272   }
273
274   public boolean visit(JsForIn x, JsContext ctx) {
275     return true;
276   }
277
278   public boolean visit(JsFunction x, JsContext ctx) {
279     return true;
280   }
281
282   public boolean visit(JsIf x, JsContext ctx) {
283     return true;
284   }
285
286   public boolean visit(JsIntegralLiteral x, JsContext ctx) {
287     return true;
288   }
289
290   public boolean visit(JsInvocation x, JsContext ctx) {
291     return true;
292   }
293
294   public boolean visit(JsLabel x, JsContext ctx) {
295     return true;
296   }
297
298   public boolean visit(JsNameRef x, JsContext ctx) {
299     return true;
300   }
301
302   public boolean visit(JsNew x, JsContext ctx) {
303     return true;
304   }
305
306   public boolean visit(JsNullLiteral x, JsContext ctx) {
307     return true;
308   }
309
310   public boolean visit(JsObjectLiteral x, JsContext ctx) {
311     return true;
312   }
313
314   public boolean visit(JsParameter x, JsContext ctx) {
315     return true;
316   }
317
318   public boolean visit(JsParameters x, JsContext ctx) {
319     return true;
320   }
321
322   public boolean visit(JsPostfixOperation x, JsContext ctx) {
323     return true;
324   }
325
326   public boolean visit(JsPrefixOperation x, JsContext ctx) {
327     return true;
328   }
329
330   public boolean visit(JsProgram x, JsContext ctx) {
331     return true;
332   }
333
334   public boolean visit(JsPropertyInitializer x, JsContext ctx) {
335     return true;
336   }
337
338   public boolean visit(JsRegExp x, JsContext ctx) {
339     return true;
340   }
341
342   public boolean visit(JsReturn x, JsContext ctx) {
343     return true;
344   }
345
346   public boolean visit(JsStringLiteral x, JsContext ctx) {
347     return true;
348   }
349
350   public boolean visit(JsSwitch x, JsContext ctx) {
351     return true;
352   }
353
354   public boolean visit(JsThisRef x, JsContext ctx) {
355     return true;
356   }
357
358   public boolean visit(JsThrow x, JsContext ctx) {
359     return true;
360   }
361
362   public boolean visit(JsTry x, JsContext ctx) {
363     return true;
364   }
365
366   public boolean visit(JsVar x, JsContext ctx) {
367     return true;
368   }
369
370   public boolean visit(JsVars x, JsContext ctx) {
371     return true;
372   }
373
374   public boolean visit(JsWhile x, JsContext ctx) {
375     return true;
376   }
377
378   protected void doAccept(JsCollection collection) {
379     for (Iterator JavaDoc it = collection.iterator(); it.hasNext();) {
380       doTraverse((JsNode) it.next(), UNMODIFIABLE_CONTEXT);
381     }
382   }
383
384   protected JsNode doAccept(JsNode node) {
385     doTraverse(node, UNMODIFIABLE_CONTEXT);
386     return node;
387   }
388
389   protected void doAcceptWithInsertRemove(JsCollection collection) {
390     for (Iterator JavaDoc it = collection.iterator(); it.hasNext();) {
391       doTraverse((JsNode) it.next(), UNMODIFIABLE_CONTEXT);
392     }
393   }
394
395   protected final void doTraverse(JsNode node, JsContext ctx) {
396     try {
397       node.traverse(this, ctx);
398     } catch (Throwable JavaDoc e) {
399       throw translateException(node, e);
400     }
401   }
402
403   private InternalCompilerException translateException(JsNode node, Throwable JavaDoc e) {
404     InternalCompilerException ice;
405     if (e instanceof InternalCompilerException) {
406       ice = (InternalCompilerException) e;
407     } else {
408       ice = new InternalCompilerException("Unexpected error during visit.", e);
409     }
410     ice.addNode(node);
411     return ice;
412   }
413 }
414
Popular Tags