1 2 3 6 package org.python.parser; 7 8 class JJTPythonGrammarState { 9 private java.util.Stack nodes; 10 private IntStack marks; 11 private IntStack lines; 12 private IntStack columns; 13 14 private int sp; private int mk; private boolean node_created; 17 18 private TreeBuilder builder; 19 20 JJTPythonGrammarState() { 21 nodes = new java.util.Stack (); 22 marks = new IntStack(); 23 lines = new IntStack(); 24 columns = new IntStack(); 25 sp = 0; 26 mk = 0; 27 builder = new TreeBuilder(this); 28 } 29 30 33 boolean nodeCreated() { 34 return node_created; 35 } 36 37 39 void reset() { 40 nodes.removeAllElements(); 41 marks.removeAllElements(); 42 sp = 0; 43 mk = 0; 44 } 45 46 48 Node rootNode() { 49 return (Node)nodes.elementAt(0); 50 } 51 52 53 void pushNode(Node n) { 54 nodes.push(n); 55 ++sp; 56 } 57 58 60 Node popNode() { 61 if (--sp < mk) { 62 mk = marks.pop(); 63 } 64 return (Node)nodes.pop(); 65 } 66 67 68 Node peekNode() { 69 return (Node)nodes.peek(); 70 } 71 72 74 int nodeArity() { 75 return sp - mk; 76 } 77 78 void pushNodePos(int line, int col) { 79 lines.push(line); 80 columns.push(col); 81 } 82 83 void setNodePos() { 84 SimpleNode n = (SimpleNode) peekNode(); 85 n.beginLine = lines.pop(); 86 n.beginColumn = columns.pop(); 87 } 88 89 90 void clearNodeScope(Node n) { 91 while (sp > mk) { 92 popNode(); 93 } 94 mk = marks.pop(); 95 } 96 97 98 void openNodeScope(Node n) { 99 marks.push(mk); 100 mk = sp; 101 } 102 103 104 108 void closeNodeScope(Node n, int num) throws ParseException { 109 SimpleNode sn = (SimpleNode) n; 110 mk = marks.pop(); 111 SimpleNode newNode = null; 112 try { 113 newNode = builder.closeNode(sn, num); 114 } catch (ParseException exc) { 115 throw exc; 116 } catch (Exception exc) { 117 exc.printStackTrace(); 118 throw new ParseException("Internal error:" + exc); 119 } 120 if (newNode == null) { 121 throw new ParseException("Internal AST builder error"); 122 } 123 pushNode(newNode); 124 node_created = true; 125 } 126 127 128 133 void closeNodeScope(Node n, boolean condition) throws ParseException { 134 SimpleNode sn = (SimpleNode) n; 135 if (condition) { 136 SimpleNode newNode = null; 137 try { 138 newNode = builder.closeNode(sn, nodeArity()); 139 } catch (ParseException exc) { 140 throw exc; 141 } catch (Exception exc) { 142 exc.printStackTrace(); 143 throw new ParseException("Internal error:" + exc); 144 } 145 if (newNode == null) { 146 throw new ParseException("Internal AST builder error"); 147 } 148 mk = marks.pop(); 149 pushNode(newNode); 150 node_created = true; 151 } else { 152 mk = marks.pop(); 153 node_created = false; 154 } 155 } 156 157 public void dumpTop(String reason) { 158 int a = nodeArity(); 159 System.out.println("dumpTop:" + reason); 160 System.out.println("arity:" + a); 161 for (int i = 0; i < a; i++) { 162 Node n = (Node) nodes.elementAt(nodes.size() - i-1); 163 System.out.println(" " + n); 164 } 165 } 166 167 public Node openNode(int id) { 168 return builder.openNode(id); 169 } 170 171 public void dump(String reason) { 172 int a = nodeArity(); 173 System.out.println("dump:" + reason); 174 System.out.println(" mk:" + mk + " sp:" + sp); 175 for (int i = 0; i < nodes.size(); i++) { 176 Node n = (Node) nodes.elementAt(i); 177 System.out.println(" " + n); 178 } 179 for (int i = 0; i < marks.size(); i++) { 180 System.out.println(" " + marks.elementAt(i)); 181 } 182 } 183 } 184 185 186 class IntStack { 187 int[] stack; 188 int sp = 0; 189 190 public IntStack() { 191 stack = new int[50]; 192 } 193 194 195 public void removeAllElements() { 196 sp = 0; 197 } 198 199 public int size() { 200 return sp; 201 } 202 203 public int elementAt(int idx) { 204 return stack[idx]; 205 } 206 207 public void push(int val) { 208 if (sp >= stack.length) { 209 int[] newstack = new int[sp*2]; 210 System.arraycopy(stack, 0, newstack, 0, sp); 211 stack = newstack; 212 } 213 stack[sp++] = val; 214 } 215 216 public int pop() { 217 return stack[--sp]; 218 } 219 } 220 | Popular Tags |