001 
002 /*
003  *  JavaDoq 1.0 - DOCUment JAVA In Source
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.javadoq.com>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package com.javadoq.jjtree.ast;
021 
022 import com.javadoq.jjtree.ASTCompilationUnit;
023 import com.javadoq.jjtree.ASTGrammar;
024 import com.javadoq.jjtree.ASTImportName;
025 import com.javadoq.jjtree.ASTName;
026 import com.javadoq.jjtree.ASTNewName;
027 import com.javadoq.jjtree.ASTNewType;
028 import com.javadoq.jjtree.ASTNode;
029 import com.javadoq.jjtree.ASTPackageName;
030 import com.javadoq.jjtree.ASTSuperList;
031 import com.javadoq.jjtree.JJTreeParserVisitor;
032 import com.javadoq.jjtree.SimpleNode;
033 import com.javadoq.jjtree.Token;
034 
035 /**
036  * <p>An abstract base class for JJTree visitor to visit JJTree nodes and tokens.</p>
037  * 
038  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.javadoq.com" target="_blank">http://www.javadoq.com</a>
039  */
040 public abstract class TokenVisitor implements JJTreeParserVisitor
041 {
042     /**
043      * <p>Visits a JJTree node with the specific type.</p>
044      * @param node The JJTree node to visit.
045      * @param data Visitor data.
046      * @return A visitor data.
047      * @since 1.0
048      */
049     public Object visit(SimpleNode node, Object data) {
050         return visit((ASTNode)node, data);
051     }
052 
053     /**
054      * <p>Visits a JJTree node with the specific type.</p>
055      * @param node The JJTree node to visit.
056      * @param data Visitor data.
057      * @return A visitor data.
058      * @since 1.0
059      */
060     public Object visit(ASTCompilationUnit node, Object data) {
061         return visit((ASTNode)node, data);
062     }
063 
064     /**
065      * <p>Visits a JJTree node with the specific type.</p>
066      * @param node The JJTree node to visit.
067      * @param data Visitor data.
068      * @return A visitor data.
069      * @since 1.0
070      */
071     public Object visit(ASTGrammar node, Object data) {
072         return visit((ASTNode)node, data);
073     }
074 
075     /**
076      * <p>Visits a JJTree node with the specific type.</p>
077      * @param node The JJTree node to visit.
078      * @param data Visitor data.
079      * @return A visitor data.
080      * @since 1.0
081      */
082     public Object visit(ASTImportName node, Object data) {
083         return visit((ASTNode)node, data);
084     }
085 
086     /**
087      * <p>Visits a JJTree node with the specific type.</p>
088      * @param node The JJTree node to visit.
089      * @param data Visitor data.
090      * @return A visitor data.
091      * @since 1.0
092      */
093     public Object visit(ASTName node, Object data) {
094         return visit((ASTNode)node, data);
095     }
096 
097     /**
098      * <p>Visits a JJTree node with the specific type.</p>
099      * @param node The JJTree node to visit.
100      * @param data Visitor data.
101      * @return A visitor data.
102      * @since 1.0
103      */
104     public Object visit(ASTNewType node, Object data) {
105         return visit((ASTNode)node, data);
106     }
107 
108     /**
109      * <p>Visits a JJTree node with the specific type.</p>
110      * @param node The JJTree node to visit.
111      * @param data Visitor data.
112      * @return A visitor data.
113      * @since 1.0
114      */
115     public Object visit(ASTNewName node, Object data) {
116         return visit((ASTNode)node, data);
117     }
118 
119     /**
120      * <p>Visits a JJTree node with the specific type.</p>
121      * @param node The JJTree node to visit.
122      * @param data Visitor data.
123      * @return A visitor data.
124      * @since 1.0
125      */
126     public Object visit(ASTPackageName node, Object data) {
127         return visit((ASTNode)node, data);
128     }
129 
130     /**
131      * <p>Visits a JJTree node with the specific type.</p>
132      * @param node The JJTree node to visit.
133      * @param data Visitor data.
134      * @return A visitor data.
135      * @since 1.0
136      */
137     public Object visit(ASTSuperList node, Object data) {
138         return visit((ASTNode)node, data);
139     }
140 
141     /**
142      * <p>Visits a JJTree node with the specific type.</p>
143      * @param node The JJTree node to visit.
144      * @param data Visitor data.
145      * @return A visitor data.
146      * @since 1.0
147      */
148     protected Object visit(ASTNode node, Object data) {
149         return visitChildren(node, data);
150     }
151 
152     /**
153      * <p>Visits all children nodes and tokens of a JJTree node.</p>
154      * @param node The JJTree node to visit children and tokens.
155      * @param data Visitor data.
156      * @return A visitor data.
157      * @since 1.0
158      */
159     protected Object visitChildren(ASTNode node, Object data) {
160         Token t = node.firstToken;
161         ASTNode n;
162         for (int i = 0; i < node.jjtGetNumChildren(); i++) {
163             n = (ASTNode)node.jjtGetChild(i);
164             do {
165                 if (t == n.firstToken) {
166                     break;
167                 }
168                 visit(t);
169                 t = t.next;
170             } while (true);
171             n.jjtAccept(this, data);
172             t = n.lastToken;
173         }
174         if (t != node.lastToken) do {
175             visit(t);
176             if (t == node.lastToken) {
177                 break;
178             }
179             t = t.next;
180         } while (true);
181         return data;
182     }
183 
184     /**
185      * <p>Visits a token.</p>
186      * @param t The JJTree token to visit.
187      * @since 1.0
188      */
189     protected abstract void visit(Token t);
190 }