+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2007 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// ANTLRWalker.java\r
-// Since: Dec 19, 2007 5:54:17 PM\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/ANTLRWalker.java $\r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.util.bean;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.antlr.runtime.tree.Tree;\r
-import org.xerial.core.XerialException;\r
-import org.xerial.util.tree.TreeNode;\r
-import org.xerial.util.tree.TreeVisitor;\r
-import org.xerial.util.tree.TreeWalker;\r
-\r
-/**\r
- * A walker implementation for ANTLR parse trees\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class ANTLRWalker implements TreeWalker\r
-{\r
- private final String[] parserTokenNames;\r
- private Tree currentNode = null;\r
-\r
- private boolean skipDescendants = false;\r
-\r
- public ANTLRWalker(final String[] parserTokenNames, Tree parseTree)\r
- {\r
- this.parserTokenNames = parserTokenNames;\r
- this.currentNode = parseTree;\r
- }\r
-\r
- public void walk(TreeVisitor visitor) throws XerialException\r
- {\r
- visitor.init(this);\r
- walk(currentNode, visitor);\r
- visitor.finish(this);\r
- }\r
-\r
- public void walk(Tree t, TreeVisitor visitor) throws XerialException\r
- {\r
- if (t == null)\r
- return;\r
-\r
- currentNode = t;\r
- int tokenType = t.getType();\r
- String nodeName = parserTokenNames[tokenType];\r
-\r
- // invoke visitor\r
- visitor.visitNode(nodeName, t.getText(), this);\r
-\r
- // visit child nodes\r
- if (!skipDescendants)\r
- {\r
- for (int i = 0; i < t.getChildCount(); i++)\r
- {\r
- Tree child = t.getChild(i);\r
- walk(child, visitor);\r
- }\r
- skipDescendants = false;\r
- }\r
-\r
- // leave the current node\r
- visitor.leaveNode(nodeName, this);\r
- }\r
-\r
- public void skipDescendants() throws XerialException\r
- {\r
- skipDescendants = true;\r
- }\r
-\r
- public TreeNode getSubTree() throws XerialException\r
- {\r
- skipDescendants();\r
- return new ANTLRTreeNodeWrapper(currentNode);\r
- }\r
-\r
- class ANTLRTreeNodeWrapper implements TreeNode\r
- {\r
- private Tree t;\r
-\r
- public ANTLRTreeNodeWrapper(Tree t)\r
- {\r
- this.t = t;\r
- }\r
-\r
- public List<TreeNode> getChildren()\r
- {\r
- ArrayList<TreeNode> childList = new ArrayList<TreeNode>();\r
- for (int i = 0; i < t.getChildCount(); i++)\r
- {\r
- childList.add(new ANTLRTreeNodeWrapper(t.getChild(i)));\r
- }\r
- return childList;\r
- }\r
-\r
- public String getNodeName()\r
- {\r
- return parserTokenNames[t.getType()];\r
- }\r
-\r
- public String getNodeValue()\r
- {\r
- return t.getText();\r
- }\r
-\r
- }\r
-\r
-}\r