1 22 23 package org.javacc.jjtree; 24 25 import java.io.BufferedWriter ; 26 import java.io.File ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 31 class JJTreeState 32 { 33 34 static void insertParserMembers(IO io) { 35 String s; 36 37 if (JJTreeOptions.getStatic()) { 38 s = "static "; 39 } else { 40 s = ""; 41 } 42 43 io.println(); 44 io.println(" protected " + s + nameState() + 45 " jjtree = new " + nameState() + "();"); 46 io.println(); 47 } 48 49 50 private static String nameState() { 51 return "JJT" + JJTreeGlobals.parserName + "State"; 52 } 53 54 55 static void generateTreeState_java() 56 { 57 File file = new File (JJTreeOptions.getJJTreeOutputDirectory(), nameState() + ".java"); 58 59 if (file.exists()) { 60 return; 61 } 62 63 try { 64 PrintWriter ostr = new PrintWriter (new BufferedWriter ( 65 new FileWriter (file), 66 8096)); 67 NodeFiles.generatePrologue(ostr, file.toString()); 68 insertState(ostr); 69 ostr.close(); 70 } catch (IOException e) { 71 throw new Error (e.toString()); 72 } 73 } 74 75 76 private static void insertState(PrintWriter ostr) { 77 ostr.println("class " + nameState() + " {"); 78 79 if (!JJTreeOptions.getJdkVersion().equals("1.5")) 80 ostr.println(" private java.util.Stack nodes;"); 81 else 82 ostr.println(" private java.util.Stack<Node> nodes;"); 83 84 if (!JJTreeOptions.getJdkVersion().equals("1.5")) 85 ostr.println(" private java.util.Stack marks;"); 86 else 87 ostr.println(" private java.util.Stack<Integer> marks;"); 88 89 ostr.println(""); 90 ostr.println(" private int sp; // number of nodes on stack"); 91 ostr.println(" private int mk; // current mark"); 92 ostr.println(" private boolean node_created;"); 93 ostr.println(""); 94 ostr.println(" " + nameState() + "() {"); 95 96 if (!JJTreeOptions.getJdkVersion().equals("1.5")) 97 ostr.println(" nodes = new java.util.Stack();"); 98 else 99 ostr.println(" nodes = new java.util.Stack<Node>();"); 100 101 if (!JJTreeOptions.getJdkVersion().equals("1.5")) 102 ostr.println(" marks = new java.util.Stack();"); 103 else 104 ostr.println(" marks = new java.util.Stack<Integer>();"); 105 106 ostr.println(" sp = 0;"); 107 ostr.println(" mk = 0;"); 108 ostr.println(" }"); 109 ostr.println(""); 110 ostr.println(" /* Determines whether the current node was actually closed and"); 111 ostr.println(" pushed. This should only be called in the final user action of a"); 112 ostr.println(" node scope. */"); 113 ostr.println(" boolean nodeCreated() {"); 114 ostr.println(" return node_created;"); 115 ostr.println(" }"); 116 ostr.println(""); 117 ostr.println(" /* Call this to reinitialize the node stack. It is called"); 118 ostr.println(" automatically by the parser's ReInit() method. */"); 119 ostr.println(" void reset() {"); 120 ostr.println(" nodes.removeAllElements();"); 121 ostr.println(" marks.removeAllElements();"); 122 ostr.println(" sp = 0;"); 123 ostr.println(" mk = 0;"); 124 ostr.println(" }"); 125 ostr.println(""); 126 ostr.println(" /* Returns the root node of the AST. It only makes sense to call"); 127 ostr.println(" this after a successful parse. */"); 128 ostr.println(" Node rootNode() {"); 129 ostr.println(" return (Node)nodes.elementAt(0);"); 130 ostr.println(" }"); 131 ostr.println(""); 132 ostr.println(" /* Pushes a node on to the stack. */"); 133 ostr.println(" void pushNode(Node n) {"); 134 ostr.println(" nodes.push(n);"); 135 ostr.println(" ++sp;"); 136 ostr.println(" }"); 137 ostr.println(""); 138 ostr.println(" /* Returns the node on the top of the stack, and remove it from the"); 139 ostr.println(" stack. */"); 140 ostr.println(" Node popNode() {"); 141 ostr.println(" if (--sp < mk) {"); 142 ostr.println(" mk = ((Integer)marks.pop()).intValue();"); 143 ostr.println(" }"); 144 ostr.println(" return (Node)nodes.pop();"); 145 ostr.println(" }"); 146 ostr.println(""); 147 ostr.println(" /* Returns the node currently on the top of the stack. */"); 148 ostr.println(" Node peekNode() {"); 149 ostr.println(" return (Node)nodes.peek();"); 150 ostr.println(" }"); 151 ostr.println(""); 152 ostr.println(" /* Returns the number of children on the stack in the current node"); 153 ostr.println(" scope. */"); 154 ostr.println(" int nodeArity() {"); 155 ostr.println(" return sp - mk;"); 156 ostr.println(" }"); 157 ostr.println(""); 158 ostr.println(""); 159 ostr.println(" void clearNodeScope(Node n) {"); 160 ostr.println(" while (sp > mk) {"); 161 ostr.println(" popNode();"); 162 ostr.println(" }"); 163 ostr.println(" mk = ((Integer)marks.pop()).intValue();"); 164 ostr.println(" }"); 165 ostr.println(""); 166 ostr.println(""); 167 ostr.println(" void openNodeScope(Node n) {"); 168 ostr.println(" marks.push(new Integer(mk));"); 169 ostr.println(" mk = sp;"); 170 ostr.println(" n.jjtOpen();"); 171 ostr.println(" }"); 172 ostr.println(""); 173 ostr.println(""); 174 ostr.println(" /* A definite node is constructed from a specified number of"); 175 ostr.println(" children. That number of nodes are popped from the stack and"); 176 ostr.println(" made the children of the definite node. Then the definite node"); 177 ostr.println(" is pushed on to the stack. */"); 178 ostr.println(" void closeNodeScope(Node n, int num) {"); 179 ostr.println(" mk = ((Integer)marks.pop()).intValue();"); 180 ostr.println(" while (num-- > 0) {"); 181 ostr.println(" Node c = popNode();"); 182 ostr.println(" c.jjtSetParent(n);"); 183 ostr.println(" n.jjtAddChild(c, num);"); 184 ostr.println(" }"); 185 ostr.println(" n.jjtClose();"); 186 ostr.println(" pushNode(n);"); 187 ostr.println(" node_created = true;"); 188 ostr.println(" }"); 189 ostr.println(""); 190 ostr.println(""); 191 ostr.println(" /* A conditional node is constructed if its condition is true. All"); 192 ostr.println(" the nodes that have been pushed since the node was opened are"); 193 ostr.println(" made children of the the conditional node, which is then pushed"); 194 ostr.println(" on to the stack. If the condition is false the node is not"); 195 ostr.println(" constructed and they are left on the stack. */"); 196 ostr.println(" void closeNodeScope(Node n, boolean condition) {"); 197 ostr.println(" if (condition) {"); 198 ostr.println(" int a = nodeArity();"); 199 ostr.println(" mk = ((Integer)marks.pop()).intValue();"); 200 ostr.println(" while (a-- > 0) {"); 201 ostr.println(" Node c = popNode();"); 202 ostr.println(" c.jjtSetParent(n);"); 203 ostr.println(" n.jjtAddChild(c, a);"); 204 ostr.println(" }"); 205 ostr.println(" n.jjtClose();"); 206 ostr.println(" pushNode(n);"); 207 ostr.println(" node_created = true;"); 208 ostr.println(" } else {"); 209 ostr.println(" mk = ((Integer)marks.pop()).intValue();"); 210 ostr.println(" node_created = false;"); 211 ostr.println(" }"); 212 ostr.println(" }"); 213 ostr.println("}"); 214 } 215 216 } 217 218 219 | Popular Tags |