001: /*
002: $Id: CodeVisitorSupport.java 3893 2006-07-10 15:42:26Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.ast;
047:
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import org.codehaus.groovy.ast.expr.*;
052: import org.codehaus.groovy.ast.stmt.AssertStatement;
053: import org.codehaus.groovy.ast.stmt.BlockStatement;
054: import org.codehaus.groovy.ast.stmt.BreakStatement;
055: import org.codehaus.groovy.ast.stmt.CaseStatement;
056: import org.codehaus.groovy.ast.stmt.CatchStatement;
057: import org.codehaus.groovy.ast.stmt.ContinueStatement;
058: import org.codehaus.groovy.ast.stmt.DoWhileStatement;
059: import org.codehaus.groovy.ast.stmt.ExpressionStatement;
060: import org.codehaus.groovy.ast.stmt.ForStatement;
061: import org.codehaus.groovy.ast.stmt.IfStatement;
062: import org.codehaus.groovy.ast.stmt.ReturnStatement;
063: import org.codehaus.groovy.ast.stmt.Statement;
064: import org.codehaus.groovy.ast.stmt.SwitchStatement;
065: import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
066: import org.codehaus.groovy.ast.stmt.ThrowStatement;
067: import org.codehaus.groovy.ast.stmt.TryCatchStatement;
068: import org.codehaus.groovy.ast.stmt.WhileStatement;
069:
070: /**
071: * Abstract base class for any GroovyCodeVisitory which by default
072: * just walks the code and expression tree
073: *
074: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
075: * @version $Revision: 3893 $
076: */
077: public abstract class CodeVisitorSupport implements GroovyCodeVisitor {
078:
079: public void visitBlockStatement(BlockStatement block) {
080: List statements = block.getStatements();
081: for (Iterator iter = statements.iterator(); iter.hasNext();) {
082: Statement statement = (Statement) iter.next();
083: statement.visit(this );
084: }
085: }
086:
087: public void visitForLoop(ForStatement forLoop) {
088: forLoop.getCollectionExpression().visit(this );
089: forLoop.getLoopBlock().visit(this );
090: }
091:
092: public void visitWhileLoop(WhileStatement loop) {
093: loop.getBooleanExpression().visit(this );
094: loop.getLoopBlock().visit(this );
095: }
096:
097: public void visitDoWhileLoop(DoWhileStatement loop) {
098: loop.getLoopBlock().visit(this );
099: loop.getBooleanExpression().visit(this );
100: }
101:
102: public void visitIfElse(IfStatement ifElse) {
103: ifElse.getBooleanExpression().visit(this );
104: ifElse.getIfBlock().visit(this );
105: ifElse.getElseBlock().visit(this );
106: }
107:
108: public void visitExpressionStatement(ExpressionStatement statement) {
109: statement.getExpression().visit(this );
110: }
111:
112: public void visitReturnStatement(ReturnStatement statement) {
113: statement.getExpression().visit(this );
114: }
115:
116: public void visitAssertStatement(AssertStatement statement) {
117: statement.getBooleanExpression().visit(this );
118: statement.getMessageExpression().visit(this );
119: }
120:
121: public void visitTryCatchFinally(TryCatchStatement statement) {
122: statement.getTryStatement().visit(this );
123: List list = statement.getCatchStatements();
124: for (Iterator iter = list.iterator(); iter.hasNext();) {
125: CatchStatement catchStatement = (CatchStatement) iter
126: .next();
127: catchStatement.visit(this );
128: }
129: statement.getFinallyStatement().visit(this );
130: }
131:
132: public void visitSwitch(SwitchStatement statement) {
133: statement.getExpression().visit(this );
134: List list = statement.getCaseStatements();
135: for (Iterator iter = list.iterator(); iter.hasNext();) {
136: CaseStatement caseStatement = (CaseStatement) iter.next();
137: caseStatement.visit(this );
138: }
139: statement.getDefaultStatement().visit(this );
140: }
141:
142: public void visitCaseStatement(CaseStatement statement) {
143: statement.getExpression().visit(this );
144: statement.getCode().visit(this );
145: }
146:
147: public void visitBreakStatement(BreakStatement statement) {
148: }
149:
150: public void visitContinueStatement(ContinueStatement statement) {
151: }
152:
153: public void visitSynchronizedStatement(
154: SynchronizedStatement statement) {
155: statement.getExpression().visit(this );
156: statement.getCode().visit(this );
157: }
158:
159: public void visitThrowStatement(ThrowStatement statement) {
160: statement.getExpression().visit(this );
161: }
162:
163: public void visitMethodCallExpression(MethodCallExpression call) {
164: call.getObjectExpression().visit(this );
165: call.getMethod().visit(this );
166: call.getArguments().visit(this );
167: }
168:
169: public void visitStaticMethodCallExpression(
170: StaticMethodCallExpression call) {
171: call.getArguments().visit(this );
172: }
173:
174: public void visitConstructorCallExpression(
175: ConstructorCallExpression call) {
176: call.getArguments().visit(this );
177: }
178:
179: public void visitBinaryExpression(BinaryExpression expression) {
180: expression.getLeftExpression().visit(this );
181: expression.getRightExpression().visit(this );
182: }
183:
184: public void visitTernaryExpression(TernaryExpression expression) {
185: expression.getBooleanExpression().visit(this );
186: expression.getTrueExpression().visit(this );
187: expression.getFalseExpression().visit(this );
188: }
189:
190: public void visitPostfixExpression(PostfixExpression expression) {
191: expression.getExpression().visit(this );
192: }
193:
194: public void visitPrefixExpression(PrefixExpression expression) {
195: expression.getExpression().visit(this );
196: }
197:
198: public void visitBooleanExpression(BooleanExpression expression) {
199: expression.getExpression().visit(this );
200: }
201:
202: public void visitNotExpression(NotExpression expression) {
203: expression.getExpression().visit(this );
204: }
205:
206: public void visitClosureExpression(ClosureExpression expression) {
207: expression.getCode().visit(this );
208: }
209:
210: public void visitTupleExpression(TupleExpression expression) {
211: visitListOfExpressions(expression.getExpressions());
212: }
213:
214: public void visitListExpression(ListExpression expression) {
215: visitListOfExpressions(expression.getExpressions());
216: }
217:
218: public void visitArrayExpression(ArrayExpression expression) {
219: visitListOfExpressions(expression.getExpressions());
220: }
221:
222: public void visitMapExpression(MapExpression expression) {
223: visitListOfExpressions(expression.getMapEntryExpressions());
224:
225: }
226:
227: public void visitMapEntryExpression(MapEntryExpression expression) {
228: expression.getKeyExpression().visit(this );
229: expression.getValueExpression().visit(this );
230:
231: }
232:
233: public void visitRangeExpression(RangeExpression expression) {
234: expression.getFrom().visit(this );
235: expression.getTo().visit(this );
236: }
237:
238: public void visitSpreadExpression(SpreadExpression expression) {
239: expression.getExpression().visit(this );
240: }
241:
242: public void visitSpreadMapExpression(SpreadMapExpression expression) {
243: expression.getExpression().visit(this );
244: }
245:
246: public void visitMethodPointerExpression(
247: MethodPointerExpression expression) {
248: expression.getExpression().visit(this );
249: }
250:
251: public void visitNegationExpression(NegationExpression expression) {
252: expression.getExpression().visit(this );
253: }
254:
255: public void visitBitwiseNegExpression(
256: BitwiseNegExpression expression) {
257: expression.getExpression().visit(this );
258: }
259:
260: public void visitCastExpression(CastExpression expression) {
261: expression.getExpression().visit(this );
262: }
263:
264: public void visitConstantExpression(ConstantExpression expression) {
265: }
266:
267: public void visitClassExpression(ClassExpression expression) {
268: }
269:
270: public void visitVariableExpression(VariableExpression expression) {
271: }
272:
273: public void visitDeclarationExpression(
274: DeclarationExpression expression) {
275: visitBinaryExpression(expression);
276: }
277:
278: public void visitPropertyExpression(PropertyExpression expression) {
279: expression.getObjectExpression().visit(this );
280: expression.getProperty().visit(this );
281: }
282:
283: public void visitAttributeExpression(AttributeExpression expression) {
284: expression.getObjectExpression().visit(this );
285: expression.getProperty().visit(this );
286: }
287:
288: public void visitFieldExpression(FieldExpression expression) {
289: }
290:
291: public void visitRegexExpression(RegexExpression expression) {
292: }
293:
294: public void visitGStringExpression(GStringExpression expression) {
295: visitListOfExpressions(expression.getStrings());
296: visitListOfExpressions(expression.getValues());
297: }
298:
299: protected void visitListOfExpressions(List list) {
300: Expression expression, expr2, expr3;
301: for (Iterator iter = list.iterator(); iter.hasNext();) {
302: expression = (Expression) iter.next();
303: if (expression instanceof SpreadExpression) {
304: expr2 = ((SpreadExpression) expression).getExpression();
305: expr2.visit(this );
306: } else {
307: expression.visit(this );
308: }
309: }
310: }
311:
312: public void visitCatchStatement(CatchStatement statement) {
313: statement.getCode().visit(this );
314: }
315:
316: public void visitArgumentlistExpression(ArgumentListExpression ale) {
317: visitTupleExpression(ale);
318: }
319: }
|