001: /* ====================================================================
002: * Tea - Copyright (c) 1997-2000 Walt Disney Internet Group
003: * ====================================================================
004: * The Tea Software License, Version 1.1
005: *
006: * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Walt Disney Internet Group (http://opensource.go.com/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact opensource@dig.com.
031: *
032: * 5. Products derived from this software may not be called "Tea",
033: * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
034: * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
035: * written permission of the Walt Disney Internet Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
041: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
042: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
043: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
044: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
045: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
046: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
047: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
048: * ====================================================================
049: *
050: * For more information about Tea, please see http://opensource.go.com/.
051: */
052:
053: package com.go.tea.parsetree;
054:
055: /******************************************************************************
056: * A TreeWalker traverses a parse tree in its canonical order. By overriding
057: * a visit method, individual nodes can be captured and processed based on
058: * their type. Call super.visit inside the overriden visit method to ensure
059: * that the node's children are properly traversed.
060: *
061: * @author Brian S O'Neill
062: * @version
063: * <!--$$Revision:--> 18 <!-- $-->, <!--$$JustDate:--> 5/31/01 <!-- $-->
064: */
065: public abstract class TreeWalker implements NodeVisitor {
066: public Object visit(Template node) {
067: node.getName().accept(this );
068:
069: Variable[] params = node.getParams();
070: if (params != null) {
071: for (int i = 0; i < params.length; i++) {
072: params[i].accept(this );
073: }
074: }
075:
076: Statement stmt = node.getStatement();
077: if (stmt != null) {
078: stmt.accept(this );
079: }
080:
081: return null;
082: }
083:
084: public Object visit(Name node) {
085: return null;
086: }
087:
088: public Object visit(TypeName node) {
089: return null;
090: }
091:
092: public Object visit(Variable node) {
093: node.getTypeName().accept(this );
094: return null;
095: }
096:
097: public Object visit(ExpressionList node) {
098: Expression[] exprs = node.getExpressions();
099: for (int i = 0; i < exprs.length; i++) {
100: exprs[i].accept(this );
101: }
102:
103: return null;
104: }
105:
106: public Object visit(Statement node) {
107: return null;
108: }
109:
110: public Object visit(StatementList node) {
111: Statement[] stmts = node.getStatements();
112: if (stmts != null) {
113: for (int i = 0; i < stmts.length; i++) {
114: stmts[i].accept(this );
115: }
116: }
117:
118: return null;
119: }
120:
121: public Object visit(Block node) {
122: Statement init = node.getInitializer();
123: if (init != null) {
124: init.accept(this );
125: }
126:
127: visit((StatementList) node);
128:
129: Statement fin = node.getFinalizer();
130: if (fin != null) {
131: fin.accept(this );
132: }
133:
134: return null;
135: }
136:
137: public Object visit(AssignmentStatement node) {
138: node.getLValue().accept(this );
139: node.getRValue().accept(this );
140:
141: return null;
142: }
143:
144: public Object visit(BreakStatement node) {
145: return null;
146: }
147:
148: public Object visit(ForeachStatement node) {
149: node.getLoopVariable().accept(this );
150: node.getRange().accept(this );
151: Expression endRange = node.getEndRange();
152: if (endRange != null) {
153: endRange.accept(this );
154: }
155:
156: Statement init = node.getInitializer();
157: if (init != null) {
158: init.accept(this );
159: }
160:
161: Block body = node.getBody();
162: if (body != null) {
163: body.accept(this );
164: }
165:
166: return null;
167: }
168:
169: public Object visit(IfStatement node) {
170: node.getCondition().accept(this );
171:
172: Block block = node.getThenPart();
173: if (block != null) {
174: block.accept(this );
175: }
176:
177: block = node.getElsePart();
178: if (block != null) {
179: block.accept(this );
180: }
181:
182: return null;
183: }
184:
185: public Object visit(SubstitutionStatement node) {
186: return null;
187: }
188:
189: public Object visit(ExpressionStatement node) {
190: node.getExpression().accept(this );
191: return null;
192: }
193:
194: public Object visit(ReturnStatement node) {
195: Expression expr = node.getExpression();
196: if (expr != null) {
197: expr.accept(this );
198: }
199: return null;
200: }
201:
202: public Object visit(ExceptionGuardStatement node) {
203: Statement stmt = node.getGuarded();
204: if (stmt != null) {
205: stmt.accept(this );
206: }
207: stmt = node.getReplacement();
208: if (stmt != null) {
209: stmt.accept(this );
210: }
211: return null;
212: }
213:
214: public Object visit(Expression node) {
215: return null;
216: }
217:
218: public Object visit(ParenExpression node) {
219: node.getExpression().accept(this );
220: return null;
221: }
222:
223: public Object visit(NewArrayExpression node) {
224: node.getExpressionList().accept(this );
225: return null;
226: }
227:
228: public Object visit(FunctionCallExpression node) {
229: return visit((CallExpression) node);
230: }
231:
232: public Object visit(TemplateCallExpression node) {
233: return visit((CallExpression) node);
234: }
235:
236: private Object visit(CallExpression node) {
237: node.getParams().accept(this );
238:
239: Statement init = node.getInitializer();
240: if (init != null) {
241: init.accept(this );
242: }
243:
244: Block subParam = node.getSubstitutionParam();
245: if (subParam != null) {
246: subParam.accept(this );
247: }
248:
249: return null;
250: }
251:
252: public Object visit(VariableRef node) {
253: Variable v = node.getVariable();
254: if (v != null) {
255: v.accept(this );
256: }
257:
258: return null;
259: }
260:
261: public Object visit(Lookup node) {
262: node.getExpression().accept(this );
263: return null;
264: }
265:
266: public Object visit(ArrayLookup node) {
267: node.getExpression().accept(this );
268: node.getLookupIndex().accept(this );
269: return null;
270: }
271:
272: public Object visit(NegateExpression node) {
273: node.getExpression().accept(this );
274: return null;
275: }
276:
277: public Object visit(NotExpression node) {
278: node.getExpression().accept(this );
279: return null;
280: }
281:
282: private Object visit(BinaryExpression node) {
283: node.getLeftExpression().accept(this );
284: node.getRightExpression().accept(this );
285: return null;
286: }
287:
288: public Object visit(ConcatenateExpression node) {
289: return visit((BinaryExpression) node);
290: }
291:
292: public Object visit(ArithmeticExpression node) {
293: return visit((BinaryExpression) node);
294: }
295:
296: public Object visit(RelationalExpression node) {
297: if (node.getIsaTypeName() != null) {
298: node.getLeftExpression().accept(this );
299: node.getIsaTypeName().accept(this );
300: return null;
301: } else {
302: return visit((BinaryExpression) node);
303: }
304: }
305:
306: public Object visit(AndExpression node) {
307: return visit((BinaryExpression) node);
308: }
309:
310: public Object visit(OrExpression node) {
311: return visit((BinaryExpression) node);
312: }
313:
314: public Object visit(NullLiteral node) {
315: return null;
316: }
317:
318: public Object visit(BooleanLiteral node) {
319: return null;
320: }
321:
322: public Object visit(StringLiteral node) {
323: return null;
324: }
325:
326: public Object visit(NumberLiteral node) {
327: return null;
328: }
329: }
|