OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / main / java / org / xerial / util / bean / ANTLRWalker.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2007 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // ANTLRWalker.java\r
20 // Since: Dec 19, 2007 5:54:17 PM\r
21 //\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/ANTLRWalker.java $\r
23 // $Author: leo $\r
24 //--------------------------------------\r
25 package org.xerial.util.bean;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \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
35 \r
36 /**\r
37  * A walker implementation for ANTLR parse trees\r
38  * \r
39  * @author leo\r
40  * \r
41  */\r
42 public class ANTLRWalker implements TreeWalker\r
43 {\r
44     private final String[] parserTokenNames;\r
45     private Tree currentNode = null;\r
46 \r
47     private boolean skipDescendants = false;\r
48 \r
49     public ANTLRWalker(final String[] parserTokenNames, Tree parseTree)\r
50     {\r
51         this.parserTokenNames = parserTokenNames;\r
52         this.currentNode = parseTree;\r
53     }\r
54 \r
55     public void walk(TreeVisitor visitor) throws XerialException\r
56     {\r
57         visitor.init(this);\r
58         walk(currentNode, visitor);\r
59         visitor.finish(this);\r
60     }\r
61 \r
62     public void walk(Tree t, TreeVisitor visitor) throws XerialException\r
63     {\r
64         if (t == null)\r
65             return;\r
66 \r
67         currentNode = t;\r
68         int tokenType = t.getType();\r
69         String nodeName = parserTokenNames[tokenType];\r
70 \r
71         // invoke visitor\r
72         visitor.visitNode(nodeName, t.getText(), this);\r
73 \r
74         // visit child nodes\r
75         if (!skipDescendants)\r
76         {\r
77             for (int i = 0; i < t.getChildCount(); i++)\r
78             {\r
79                 Tree child = t.getChild(i);\r
80                 walk(child, visitor);\r
81             }\r
82             skipDescendants = false;\r
83         }\r
84 \r
85         // leave the current node\r
86         visitor.leaveNode(nodeName, this);\r
87     }\r
88 \r
89     public void skipDescendants() throws XerialException\r
90     {\r
91         skipDescendants = true;\r
92     }\r
93 \r
94     public TreeNode getSubTree() throws XerialException\r
95     {\r
96         skipDescendants();\r
97         return new ANTLRTreeNodeWrapper(currentNode);\r
98     }\r
99 \r
100     class ANTLRTreeNodeWrapper implements TreeNode\r
101     {\r
102         private Tree t;\r
103 \r
104         public ANTLRTreeNodeWrapper(Tree t)\r
105         {\r
106             this.t = t;\r
107         }\r
108 \r
109         public List<TreeNode> getChildren()\r
110         {\r
111             ArrayList<TreeNode> childList = new ArrayList<TreeNode>();\r
112             for (int i = 0; i < t.getChildCount(); i++)\r
113             {\r
114                 childList.add(new ANTLRTreeNodeWrapper(t.getChild(i)));\r
115             }\r
116             return childList;\r
117         }\r
118 \r
119         public String getNodeName()\r
120         {\r
121             return parserTokenNames[t.getType()];\r
122         }\r
123 \r
124         public String getNodeValue()\r
125         {\r
126             return t.getText();\r
127         }\r
128 \r
129     }\r
130 \r
131 }\r