KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > DoNothingVisitor


1 /* @author rich
2  * Created on 16-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep;
9
10 import org.nfunk.jep.*;
11
12 /**
13  * A Visitor which visits each node of a expression tree.
14  * It returns the top node.
15  * This visitor should be extended by Visitors which modify trees in place.
16  *
17  * @author Rich Morris
18  * Created on 16-Nov-2003
19  */

20 public abstract class DoNothingVisitor implements ParserVisitor {
21
22     /*
23      * The following methods was used to facilitate
24      * using visitors which implemented a interface
25      * which subclassed ParserVisitor.
26      *
27      * If subclassed to extend to implement a different visitor
28      * this method should be overwritten to ensure the correct
29      * accept method is called.
30      * This method simply calls the jjtAccept(ParserVisitor this,Object data) of node.
31      *
32      * We no longer need this as we use ParseVisitor everywhere,
33      * but kept for future reference.
34      *
35     private Object nodeAccept(Node node, Object data) throws ParseException
36     {
37         return node.jjtAccept(this,data);
38     }
39     */

40     
41     /**
42      * Gets the result of visiting children of a array of nodes.
43      */

44     
45     protected Node[] acceptChildrenAsArray(Node node,Object JavaDoc data) throws ParseException
46     {
47         int n = node.jjtGetNumChildren();
48         Node children[] = new Node[n];
49         for(int i=0;i<n;++i)
50             children[i]= (Node) node.jjtGetChild(i).jjtAccept(this,data);
51         return children;
52     }
53     
54
55
56     public Object JavaDoc visit(SimpleNode node, Object JavaDoc data) throws ParseException
57     {
58         throw new ParseException(this.toString()+": encountered a simple node, problem with visitor.");
59     }
60
61     public Object JavaDoc visit(ASTStart node, Object JavaDoc data) throws ParseException
62     {
63         throw new ParseException(this.toString()+": encountered a start node, problem with visitor.");
64     }
65
66
67     public Object JavaDoc visit(ASTConstant node, Object JavaDoc data) throws ParseException
68     {
69         return node;
70     }
71
72     public Object JavaDoc visit(ASTVarNode node, Object JavaDoc data) throws ParseException
73     {
74         return node;
75     }
76
77     public Object JavaDoc visit(ASTFunNode node, Object JavaDoc data) throws ParseException
78     {
79         Node children[] = acceptChildrenAsArray(node,data);
80         TreeUtils.copyChildrenIfNeeded(node,children);
81         return node;
82     }
83 }
84
Popular Tags