1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2007 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
20 // Since: Dec 19, 2007 5:54:17 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/ANTLRWalker.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.util.bean;
\r
27 import java.util.ArrayList;
\r
28 import java.util.List;
\r
30 import org.antlr.runtime.tree.Tree;
\r
31 import org.xerial.core.XerialException;
\r
32 import org.xerial.util.tree.TreeNode;
\r
33 import org.xerial.util.tree.TreeVisitor;
\r
34 import org.xerial.util.tree.TreeWalker;
\r
37 * A walker implementation for ANTLR parse trees
\r
42 public class ANTLRWalker implements TreeWalker
\r
44 private final String[] parserTokenNames;
\r
45 private Tree currentNode = null;
\r
47 private boolean skipDescendants = false;
\r
49 public ANTLRWalker(final String[] parserTokenNames, Tree parseTree)
\r
51 this.parserTokenNames = parserTokenNames;
\r
52 this.currentNode = parseTree;
\r
55 public void walk(TreeVisitor visitor) throws XerialException
\r
58 walk(currentNode, visitor);
\r
59 visitor.finish(this);
\r
62 public void walk(Tree t, TreeVisitor visitor) throws XerialException
\r
68 int tokenType = t.getType();
\r
69 String nodeName = parserTokenNames[tokenType];
\r
72 visitor.visitNode(nodeName, t.getText(), this);
\r
74 // visit child nodes
\r
75 if (!skipDescendants)
\r
77 for (int i = 0; i < t.getChildCount(); i++)
\r
79 Tree child = t.getChild(i);
\r
80 walk(child, visitor);
\r
82 skipDescendants = false;
\r
85 // leave the current node
\r
86 visitor.leaveNode(nodeName, this);
\r
89 public void skipDescendants() throws XerialException
\r
91 skipDescendants = true;
\r
94 public TreeNode getSubTree() throws XerialException
\r
97 return new ANTLRTreeNodeWrapper(currentNode);
\r
100 class ANTLRTreeNodeWrapper implements TreeNode
\r
104 public ANTLRTreeNodeWrapper(Tree t)
\r
109 public List<TreeNode> getChildren()
\r
111 ArrayList<TreeNode> childList = new ArrayList<TreeNode>();
\r
112 for (int i = 0; i < t.getChildCount(); i++)
\r
114 childList.add(new ANTLRTreeNodeWrapper(t.getChild(i)));
\r
119 public String getNodeName()
\r
121 return parserTokenNames[t.getType()];
\r
124 public String getNodeValue()
\r
126 return t.getText();
\r