1 22 23 package org.javacc.jjtree; 24 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 28 public class NodeScope 29 { 30 private ASTProduction production; 31 private ASTNodeDescriptor node_descriptor; 32 33 private String closedVar; 34 private String exceptionVar; 35 private String nodeVar; 36 private int scopeNumber; 37 38 NodeScope(ASTProduction p, ASTNodeDescriptor n) 39 { 40 production = p; 41 42 if (n == null) { 43 String 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 getNodeDescriptorText() 72 { 73 return node_descriptor.getDescriptor(); 74 } 75 76 77 String getNodeVariable() 78 { 79 return nodeVar; 80 } 81 82 83 private String constructVariable(String id) 84 { 85 String 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 indent) 97 { 98 insertOpenNodeCode(io, indent); 99 } 100 101 102 void insertOpenNodeCode(IO io, String indent) 103 { 104 String type = node_descriptor.getNodeType(); 105 106 108 NodeFiles.ensure(io, type); 109 110 io.print(indent + type + " " + nodeVar + " = "); 111 if (JJTreeOptions.getNodeFactory()) { 112 if (JJTreeOptions.getNodeUsesParser()) { 113 String 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 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 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 indent) 153 { 154 io.println(indent + "{"); 155 insertOpenNodeCode(io, indent + " "); 156 io.println(indent + "}"); 157 } 158 159 160 void insertCloseNodeAction(IO io, String 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 thrown_names, 169 String indent) 170 { 171 String 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 )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 194 io.println(indent + " throw (Error)" + exceptionVar + ";"); 195 } 196 197 } 198 199 200 void tryTokenSequence(IO io, String indent, Token first, Token last) 201 { 202 io.println(indent + "try {"); 203 SimpleNode.closeJJTreeComment(io); 204 205 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 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 thrown_set, 229 SimpleNode expansion_unit) 230 { 231 if (expansion_unit instanceof ASTBNFNonTerminal) { 232 234 String nt = expansion_unit.getFirstToken().image; 235 ASTProduction prod = (ASTProduction)JJTreeGlobals.productions.get(nt); 236 if (prod != null) { 237 Enumeration e = prod.throws_list.elements(); 238 while (e.hasMoreElements()) { 239 String t = (String )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 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 thrown_set = new Hashtable (); 262 findThrown(thrown_set, expansion_unit); 263 Enumeration 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 297 | Popular Tags |