KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjtree > NodeScope


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.jjtree;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 public class NodeScope
29 {
30   private ASTProduction production;
31   private ASTNodeDescriptor node_descriptor;
32
33   private String JavaDoc closedVar;
34   private String JavaDoc exceptionVar;
35   private String JavaDoc nodeVar;
36   private int scopeNumber;
37
38   NodeScope(ASTProduction p, ASTNodeDescriptor n)
39   {
40     production = p;
41
42     if (n == null) {
43       String JavaDoc nm = production.name;
44       if (JJTreeOptions.getNodeDefaultVoid()) {
45     nm = "void";
46       }
47       node_descriptor = ASTNodeDescriptor.indefinite(nm);
48     } else {
49       node_descriptor = n;
50     }
51
52     scopeNumber = production.getNodeScopeNumber(this);
53     nodeVar = constructVariable("n");
54     closedVar = constructVariable("c");
55     exceptionVar = constructVariable("e");
56   }
57
58
59   boolean isVoid()
60   {
61     return node_descriptor.isVoid();
62   }
63
64
65   ASTNodeDescriptor getNodeDescriptor()
66   {
67     return node_descriptor;
68   }
69
70
71   String JavaDoc getNodeDescriptorText()
72   {
73     return node_descriptor.getDescriptor();
74   }
75
76
77   String JavaDoc getNodeVariable()
78   {
79     return nodeVar;
80   }
81
82
83   private String JavaDoc constructVariable(String JavaDoc id)
84   {
85     String JavaDoc s = "000" + scopeNumber;
86     return "jjt" + id + s.substring(s.length() - 3, s.length());
87   }
88
89
90   boolean usesCloseNodeVar()
91   {
92     return true;
93   }
94
95
96   void insertOpenNodeDeclaration(IO io, String JavaDoc indent)
97   {
98     insertOpenNodeCode(io, indent);
99   }
100
101
102   void insertOpenNodeCode(IO io, String JavaDoc indent)
103   {
104     String JavaDoc type = node_descriptor.getNodeType();
105
106     /* Ensure that there is a template definition file for the node
107        type. */

108     NodeFiles.ensure(io, type);
109
110     io.print(indent + type + " " + nodeVar + " = ");
111     if (JJTreeOptions.getNodeFactory()) {
112       if (JJTreeOptions.getNodeUsesParser()) {
113     String JavaDoc p = JJTreeOptions.getStatic() ? "null" : "this";
114     io.println("(" + type + ")" + type + ".jjtCreate(" + p + ", " +
115            node_descriptor.getNodeId() +");");
116       } else {
117     io.println("(" + type + ")" + type + ".jjtCreate(" +
118            node_descriptor.getNodeId() +");");
119       }
120     } else {
121       if (JJTreeOptions.getNodeUsesParser()) {
122     String JavaDoc p = JJTreeOptions.getStatic() ? "null" : "this";
123     io.println("new " + type + "(" + p + ", " +
124            node_descriptor.getNodeId() + ");");
125       } else {
126     io.println("new " + type + "(" + node_descriptor.getNodeId() + ");");
127       }
128     }
129
130     if (usesCloseNodeVar()) {
131       io.println(indent + "boolean " + closedVar + " = true;");
132     }
133     io.println(indent + node_descriptor.openNode(nodeVar));
134     if (JJTreeOptions.getNodeScopeHook()) {
135       io.println(indent + "jjtreeOpenNodeScope(" + nodeVar + ");");
136     }
137   }
138
139
140   void insertCloseNodeCode(IO io, String JavaDoc indent, boolean isFinal)
141   {
142     io.println(indent + node_descriptor.closeNode(nodeVar));
143     if (usesCloseNodeVar() && !isFinal) {
144       io.println(indent + closedVar + " = false;");
145     }
146     if (JJTreeOptions.getNodeScopeHook()) {
147       io.println(indent + "jjtreeCloseNodeScope(" + nodeVar + ");");
148     }
149   }
150
151
152   void insertOpenNodeAction(IO io, String JavaDoc indent)
153   {
154     io.println(indent + "{");
155     insertOpenNodeCode(io, indent + " ");
156     io.println(indent + "}");
157   }
158
159
160   void insertCloseNodeAction(IO io, String JavaDoc indent)
161   {
162     io.println(indent + "{");
163     insertCloseNodeCode(io, indent + " ", false);
164     io.println(indent + "}");
165   }
166
167
168   private void insertCatchBlocks(IO io, Enumeration JavaDoc thrown_names,
169                  String JavaDoc indent)
170   {
171     String JavaDoc thrown;
172     if (thrown_names.hasMoreElements()) {
173       io.println(indent + "} catch (Throwable " + exceptionVar + ") {");
174
175       if (usesCloseNodeVar()) {
176     io.println(indent + " if (" + closedVar + ") {");
177     io.println(indent + " jjtree.clearNodeScope(" + nodeVar + ");");
178     io.println(indent + " " + closedVar + " = false;");
179     io.println(indent + " } else {");
180     io.println(indent + " jjtree.popNode();");
181     io.println(indent + " }");
182       }
183
184       while (thrown_names.hasMoreElements()) {
185     thrown = (String JavaDoc)thrown_names.nextElement();
186     io.println(indent + " if (" + exceptionVar + " instanceof " +
187            thrown + ") {");
188     io.println(indent + " throw (" + thrown + ")" + exceptionVar + ";");
189     io.println(indent + " }");
190       }
191       /* This is either an Error or an undeclared Exception. If it's
192          an Error then the cast is good, otherwise we want to force
193          the user to declare it by crashing on the bad cast. */

194       io.println(indent + " throw (Error)" + exceptionVar + ";");
195     }
196
197   }
198
199
200   void tryTokenSequence(IO io, String JavaDoc indent, Token first, Token last)
201   {
202     io.println(indent + "try {");
203     SimpleNode.closeJJTreeComment(io);
204
205     /* Print out all the tokens, converting all references to
206        `jjtThis' into the current node variable. */

207     for (Token t = first; t != last.next; t = t.next) {
208       TokenUtils.print(t, io, "jjtThis", nodeVar);
209     }
210
211     SimpleNode.openJJTreeComment(io, null);
212     io.println();
213
214     Enumeration JavaDoc thrown_names = production.throws_list.elements();
215     insertCatchBlocks(io, thrown_names, indent);
216
217     io.println(indent + "} finally {");
218     if (usesCloseNodeVar()) {
219       io.println(indent + " if (" + closedVar + ") {");
220       insertCloseNodeCode(io, indent + " ", true);
221       io.println(indent + " }");
222     }
223     io.println(indent + "}");
224     SimpleNode.closeJJTreeComment(io);
225   }
226
227
228   private static void findThrown(Hashtable JavaDoc thrown_set,
229                  SimpleNode expansion_unit)
230   {
231     if (expansion_unit instanceof ASTBNFNonTerminal) {
232       /* Should really make the nonterminal explicitly maintain its
233          name. */

234       String JavaDoc nt = expansion_unit.getFirstToken().image;
235       ASTProduction prod = (ASTProduction)JJTreeGlobals.productions.get(nt);
236       if (prod != null) {
237     Enumeration JavaDoc e = prod.throws_list.elements();
238     while (e.hasMoreElements()) {
239       String JavaDoc t = (String JavaDoc)e.nextElement();
240       thrown_set.put(t, t);
241     }
242       }
243     }
244     for (int i = 0; i < expansion_unit.jjtGetNumChildren(); ++i) {
245       SimpleNode n = (SimpleNode)expansion_unit.jjtGetChild(i);
246       findThrown(thrown_set, n);
247     }
248   }
249
250
251   void tryExpansionUnit(IO io, String JavaDoc indent, SimpleNode expansion_unit)
252   {
253     io.println(indent + "try {");
254     SimpleNode.closeJJTreeComment(io);
255
256     expansion_unit.print(io);
257
258     SimpleNode.openJJTreeComment(io, null);
259     io.println();
260
261     Hashtable JavaDoc thrown_set = new Hashtable JavaDoc();
262     findThrown(thrown_set, expansion_unit);
263     Enumeration JavaDoc thrown_names = thrown_set.elements();
264     insertCatchBlocks(io, thrown_names, indent);
265
266     io.println(indent + "} finally {");
267     if (usesCloseNodeVar()) {
268       io.println(indent + " if (" + closedVar + ") {");
269       insertCloseNodeCode(io, indent + " ", true);
270       io.println(indent + " }");
271     }
272     io.println(indent + "}");
273     SimpleNode.closeJJTreeComment(io);
274   }
275
276
277   static NodeScope getEnclosingNodeScope(Node node)
278   {
279     if (node instanceof ASTBNFDeclaration) {
280       return ((ASTBNFDeclaration)node).node_scope;
281     }
282     for (Node n = node.jjtGetParent(); n != null; n = n.jjtGetParent()) {
283       if (n instanceof ASTBNFDeclaration) {
284     return ((ASTBNFDeclaration)n).node_scope;
285       } else if (n instanceof ASTBNFNodeScope) {
286     return ((ASTBNFNodeScope)n).node_scope;
287       } else if (n instanceof ASTExpansionNodeScope) {
288     return ((ASTExpansionNodeScope)n).node_scope;
289       }
290     }
291     return null;
292   }
293
294 }
295
296 /*end*/
297
Popular Tags