1 22 23 package org.javacc.jjtree; 24 25 import java.io.*; 26 import java.util.Vector ; 27 28 import org.javacc.parser.JavaCCGlobals; 29 30 class NodeFiles 31 { 32 static void ensure(IO io, String nodeType) 33 { 34 File file = new File(JJTreeOptions.getJJTreeOutputDirectory(), nodeType + ".java"); 35 36 if (nodeType.equals("Node")) { 37 } else if (nodeType.equals("SimpleNode")) { 38 ensure(io, "Node"); 39 } else { 40 ensure(io, "SimpleNode"); 41 } 42 43 45 if (!(nodeType.equals("Node") || JJTreeOptions.getBuildNodeFiles())) { 46 return; 47 } 48 49 if (file.exists()) { 50 return; 51 } 52 53 io.getMsg().println("File \"" + file + 54 "\" does not exist. Will create one."); 55 56 PrintWriter ostr; 57 58 try { 59 ostr = new PrintWriter(new BufferedWriter( 60 new FileWriter(file), 8096)); 61 62 if (nodeType.equals("Node")) { 63 generateNode_java(ostr); 64 } else if (nodeType.equals("SimpleNode")) { 65 generateSimpleNode_java(ostr); 66 } else { 67 generateMULTINode_java(ostr, nodeType); 68 } 69 70 ostr.close(); 71 72 } catch (IOException e) { 73 throw new Error (e.toString()); 74 } 75 } 76 77 78 static void generatePrologue(PrintWriter ostr, String fileName) 79 { 80 ostr.println("/* " + 81 JavaCCGlobals.getIdString(JJTreeGlobals.toolList, 82 fileName) + 83 " */"); 84 ostr.println(); 85 if (!JJTreeGlobals.packageName.equals("")) { 86 ostr.println("package " + JJTreeGlobals.packageName + ";"); 87 ostr.println(); 88 } 89 } 90 91 92 static String nodeConstants() 93 { 94 return JJTreeGlobals.parserName + "TreeConstants"; 95 } 96 97 static void generateTreeConstants_java() 98 { 99 String name = nodeConstants(); 100 File file = new File(JJTreeOptions.getJJTreeOutputDirectory(), name + ".java"); 101 102 try { 103 PrintWriter ostr = new PrintWriter(new BufferedWriter( 104 new FileWriter(file), 105 8096)); 106 107 Vector nodeIds = ASTNodeDescriptor.getNodeIds(); 108 Vector nodeNames = ASTNodeDescriptor.getNodeNames(); 109 110 generatePrologue(ostr, file.toString()); 111 ostr.println("public interface " + name); 112 ostr.println("{"); 113 114 for (int i = 0; i < nodeIds.size(); ++i) { 115 String n = (String )nodeIds.elementAt(i); 116 ostr.println(" public int " + n + " = " + i + ";"); 117 } 118 119 ostr.println(); 120 ostr.println(); 121 122 ostr.println(" public String[] jjtNodeName = {"); 123 for (int i = 0; i < nodeNames.size(); ++i) { 124 String n = (String )nodeNames.elementAt(i); 125 ostr.println(" \"" + n + "\","); 126 } 127 ostr.println(" };"); 128 129 ostr.println("}"); 130 ostr.close(); 131 132 } catch (IOException e) { 133 throw new Error (e.toString()); 134 } 135 } 136 137 138 static String visitorClass() 139 { 140 return JJTreeGlobals.parserName + "Visitor"; 141 } 142 143 static void generateVisitor_java() 144 { 145 if (!JJTreeOptions.getVisitor()) { 146 return; 147 } 148 149 String name = visitorClass(); 150 File file = new File(JJTreeOptions.getJJTreeOutputDirectory(), name + ".java"); 151 152 try { 153 PrintWriter ostr = new PrintWriter(new BufferedWriter( 154 new FileWriter(file), 155 8096)); 156 157 Vector nodeNames = ASTNodeDescriptor.getNodeNames(); 158 159 generatePrologue(ostr, file.toString()); 160 ostr.println("public interface " + name); 161 ostr.println("{"); 162 163 String ve = JJTreeOptions.getVisitorException(); 164 if (!ve.equals("")) { 165 ve = " throws " + ve; 166 } 167 168 ostr.println(" public Object visit(SimpleNode node, Object data)" + 169 ve + ";"); 170 if (JJTreeOptions.getMulti()) { 171 for (int i = 0; i < nodeNames.size(); ++i) { 172 String n = (String )nodeNames.elementAt(i); 173 if (n.equals("void")) { 174 continue; 175 } 176 String nodeType = JJTreeOptions.getNodePrefix() + n; 177 ostr.println(" public Object visit(" + nodeType + 178 " node, Object data)" + ve + ";"); 179 } 180 } 181 ostr.println("}"); 182 ostr.close(); 183 184 } catch (IOException e) { 185 throw new Error (e.toString()); 186 } 187 } 188 189 190 private static void generateNode_java(PrintWriter ostr) 191 { 192 generatePrologue(ostr, "Node.java"); 193 194 ostr.println("/* All AST nodes must implement this interface. It provides basic"); 195 ostr.println(" machinery for constructing the parent and child relationships"); 196 ostr.println(" between nodes. */"); 197 ostr.println(""); 198 ostr.println("public interface Node {"); 199 ostr.println(""); 200 ostr.println(" /** This method is called after the node has been made the current"); 201 ostr.println(" node. It indicates that child nodes can now be added to it. */"); 202 ostr.println(" public void jjtOpen();"); 203 ostr.println(""); 204 ostr.println(" /** This method is called after all the child nodes have been"); 205 ostr.println(" added. */"); 206 ostr.println(" public void jjtClose();"); 207 ostr.println(""); 208 ostr.println(" /** This pair of methods are used to inform the node of its"); 209 ostr.println(" parent. */"); 210 ostr.println(" public void jjtSetParent(Node n);"); 211 ostr.println(" public Node jjtGetParent();"); 212 ostr.println(""); 213 ostr.println(" /** This method tells the node to add its argument to the node's"); 214 ostr.println(" list of children. */"); 215 ostr.println(" public void jjtAddChild(Node n, int i);"); 216 ostr.println(""); 217 ostr.println(" /** This method returns a child node. The children are numbered"); 218 ostr.println(" from zero, left to right. */"); 219 ostr.println(" public Node jjtGetChild(int i);"); 220 ostr.println(""); 221 ostr.println(" /** Return the number of children the node has. */"); 222 ostr.println(" public int jjtGetNumChildren();"); 223 224 if (JJTreeOptions.getVisitor()) { 225 String ve = JJTreeOptions.getVisitorException(); 226 if (!ve.equals("")) { 227 ve = " throws " + ve; 228 } 229 230 ostr.println(""); 231 ostr.println(" /** Accept the visitor. **/"); 232 ostr.println(" public Object jjtAccept(" + visitorClass() + 233 " visitor, Object data)" + ve + ";"); 234 } 235 236 ostr.println("}"); 237 238 ostr.close(); 239 } 240 241 242 private static void generateSimpleNode_java(PrintWriter ostr) 243 { 244 generatePrologue(ostr, "SimpleNode.java"); 245 246 ostr.print("public class SimpleNode"); 247 if (!JJTreeOptions.getNodeExtends().equals("")) 248 ostr.print(" extends " + JJTreeOptions.getNodeExtends()); 249 ostr.println(" implements Node {"); 250 ostr.println(" protected Node parent;"); 251 ostr.println(" protected Node[] children;"); 252 ostr.println(" protected int id;"); 253 ostr.println(" protected " + JJTreeGlobals.parserName + " parser;"); 254 ostr.println(""); 255 ostr.println(" public SimpleNode(int i) {"); 256 ostr.println(" id = i;"); 257 ostr.println(" }"); 258 ostr.println(""); 259 ostr.println(" public SimpleNode(" + JJTreeGlobals.parserName + " p, int i) {"); 260 ostr.println(" this(i);"); 261 ostr.println(" parser = p;"); 262 ostr.println(" }"); 263 ostr.println(""); 264 265 if (JJTreeOptions.getNodeFactory()) { 266 ostr.println(" public static Node jjtCreate(int id) {"); 267 ostr.println(" return new SimpleNode(id);"); 268 ostr.println(" }"); 269 ostr.println(""); 270 ostr.println(" public static Node jjtCreate(" + JJTreeGlobals.parserName + " p, int id) {"); 271 ostr.println(" return new SimpleNode(p, id);"); 272 ostr.println(" }"); 273 ostr.println(""); 274 } 275 276 ostr.println(" public void jjtOpen() {"); 277 ostr.println(" }"); 278 ostr.println(""); 279 ostr.println(" public void jjtClose() {"); 280 ostr.println(" }"); 281 ostr.println(" "); 282 ostr.println(" public void jjtSetParent(Node n) { parent = n; }"); 283 ostr.println(" public Node jjtGetParent() { return parent; }"); 284 ostr.println(""); 285 ostr.println(" public void jjtAddChild(Node n, int i) {"); 286 ostr.println(" if (children == null) {"); 287 ostr.println(" children = new Node[i + 1];"); 288 ostr.println(" } else if (i >= children.length) {"); 289 ostr.println(" Node c[] = new Node[i + 1];"); 290 ostr.println(" System.arraycopy(children, 0, c, 0, children.length);"); 291 ostr.println(" children = c;"); 292 ostr.println(" }"); 293 ostr.println(" children[i] = n;"); 294 ostr.println(" }"); 295 ostr.println(""); 296 ostr.println(" public Node jjtGetChild(int i) {"); 297 ostr.println(" return children[i];"); 298 ostr.println(" }"); 299 ostr.println(""); 300 ostr.println(" public int jjtGetNumChildren() {"); 301 ostr.println(" return (children == null) ? 0 : children.length;"); 302 ostr.println(" }"); 303 ostr.println(""); 304 305 if (JJTreeOptions.getVisitor()) { 306 String ve = JJTreeOptions.getVisitorException(); 307 if (!ve.equals("")) { 308 ve = " throws " + ve; 309 } 310 ostr.println(" /** Accept the visitor. **/"); 311 ostr.println(" public Object jjtAccept(" + visitorClass() + 312 " visitor, Object data)" + ve + " {"); 313 ostr.println(" return visitor.visit(this, data);"); 314 ostr.println(" }"); 315 ostr.println(""); 316 317 ostr.println(" /** Accept the visitor. **/"); 318 ostr.println(" public Object childrenAccept(" + visitorClass() + 319 " visitor, Object data)" + ve + " {"); 320 ostr.println(" if (children != null) {"); 321 ostr.println(" for (int i = 0; i < children.length; ++i) {"); 322 ostr.println(" children[i].jjtAccept(visitor, data);"); 323 ostr.println(" }"); 324 ostr.println(" }"); 325 ostr.println(" return data;"); 326 ostr.println(" }"); 327 ostr.println(""); 328 } 329 330 ostr.println(" /* You can override these two methods in subclasses of SimpleNode to"); 331 ostr.println(" customize the way the node appears when the tree is dumped. If"); 332 ostr.println(" your output uses more than one line you should override"); 333 ostr.println(" toString(String), otherwise overriding toString() is probably all"); 334 ostr.println(" you need to do. */"); 335 ostr.println(""); 336 ostr.println(" public String toString() { return " + nodeConstants() + ".jjtNodeName[id]; }"); 337 ostr.println(" public String toString(String prefix) { return prefix + toString(); }"); 338 339 ostr.println(""); 340 ostr.println(" /* Override this method if you want to customize how the node dumps"); 341 ostr.println(" out its children. */"); 342 ostr.println(""); 343 ostr.println(" public void dump(String prefix) {"); 344 ostr.println(" System.out.println(toString(prefix));"); 345 ostr.println(" if (children != null) {"); 346 ostr.println(" for (int i = 0; i < children.length; ++i) {"); 347 ostr.println(" SimpleNode n = (SimpleNode)children[i];"); 348 ostr.println(" if (n != null) {"); 349 ostr.println(" n.dump(prefix + \" \");"); 350 ostr.println(" }"); 351 ostr.println(" }"); 352 ostr.println(" }"); 353 ostr.println(" }"); 354 ostr.println("}"); 355 ostr.println(""); 356 } 357 358 359 private static void generateMULTINode_java(PrintWriter ostr, String nodeType) 360 { 361 generatePrologue(ostr, nodeType + ".java"); 362 363 ostr.println("public class " + nodeType + " extends SimpleNode {"); 364 ostr.println(" public " + nodeType + "(int id) {"); 365 ostr.println(" super(id);"); 366 ostr.println(" }"); 367 ostr.println(); 368 ostr.println(" public " + nodeType + "(" + JJTreeGlobals.parserName + " p, int id) {"); 369 ostr.println(" super(p, id);"); 370 ostr.println(" }"); 371 ostr.println(); 372 373 if (JJTreeOptions.getNodeFactory()) { 374 ostr.println(" public static Node jjtCreate(int id) {"); 375 ostr.println(" return new " + nodeType + "(id);"); 376 ostr.println(" }"); 377 ostr.println(); 378 ostr.println(" public static Node jjtCreate(" + 379 JJTreeGlobals.parserName + " p, int id) {"); 380 ostr.println(" return new " + nodeType + "(p, id);"); 381 ostr.println(" }"); 382 } 383 384 if (JJTreeOptions.getVisitor()) { 385 String ve = JJTreeOptions.getVisitorException(); 386 if (!ve.equals("")) { 387 ve = " throws " + ve; 388 } 389 ostr.println(""); 390 ostr.println(" /** Accept the visitor. **/"); 391 ostr.println(" public Object jjtAccept(" + visitorClass() + 392 " visitor, Object data)" + ve + " {"); 393 ostr.println(" return visitor.visit(this, data);"); 394 ostr.println(" }"); 395 } 396 397 ostr.println("}"); 398 ostr.close(); 399 } 400 401 } 402 403 404 405 406 407 408 | Popular Tags |