1 22 23 package org.javacc.jjtree; 24 25 public class SimpleNode implements Node { 26 private Node parent; 27 private Node[] children; 28 private int id; 29 private int myOrdinal; 30 31 public SimpleNode(int i) { 32 id = i; 33 } 34 35 public SimpleNode(JJTreeParser p, int i) { 36 this(i); 37 } 38 39 public void jjtOpen() {} 40 public void jjtClose() {} 41 42 public void jjtSetParent(Node n) { parent = n; } 43 public Node jjtGetParent() { return parent; } 44 45 public void jjtAddChild(Node n, int i) { 46 if (children == null) { 47 children = new Node[i + 1]; 48 } else if (i >= children.length) { 49 Node c[] = new Node[i + 1]; 50 System.arraycopy(children, 0, c, 0, children.length); 51 children = c; 52 } 53 children[i] = n; 54 ((SimpleNode)n).setOrdinal(i); 55 } 56 57 public Node jjtGetChild(int i) { 58 return children[i]; 59 } 60 61 public int jjtGetNumChildren() { 62 return (children == null) ? 0 : children.length; 63 } 64 65 70 71 public String toString() { return JJTreeParserTreeConstants.jjtNodeName[id]; } 72 public String toString(String prefix) { return prefix + toString(); } 73 74 76 77 public void dump(String prefix) { 78 System.out.println(toString(prefix)); 79 if (children != null) { 80 for (int i = 0; i < children.length; ++i) { 81 SimpleNode n = (SimpleNode)children[i]; 82 if (n != null) { 83 n.dump(prefix + " "); 84 } 85 } 86 } 87 } 88 89 public int getOrdinal() 90 { 91 return myOrdinal; 92 } 93 94 public void setOrdinal(int o) 95 { 96 myOrdinal = o; 97 } 98 99 100 108 109 private Token first, last; 110 111 public Token getFirstToken() { return first; } 112 public void setFirstToken(Token t) { first = t; } 113 public Token getLastToken() { return last; } 114 public void setLastToken(Token t) { last = t; } 115 116 120 121 public void print(IO io) { 122 124 if (getLastToken().next == getFirstToken()) { 125 return; 126 } 127 128 Token t1 = getFirstToken(); 129 Token t = new Token(); 130 t.next = t1; 131 SimpleNode n; 132 for (int ord = 0; ord < jjtGetNumChildren(); ord++) { 133 n = (SimpleNode)jjtGetChild(ord); 134 while (true) { 135 t = t.next; 136 if (t == n.getFirstToken()) break; 137 print(t, io); 138 } 139 n.print(io); 140 t = n.getLastToken(); 141 } 142 while (t != getLastToken()) { 143 t = t.next; 144 print(t, io); 145 } 146 } 147 148 149 String translateImage(Token t) 150 { 151 return t.image; 152 } 153 154 String whiteOut(Token t) 155 { 156 String s = ""; 157 158 for (int i = 0; i < t.image.length(); ++i) { 159 s += " "; 160 } 161 return s; 162 } 163 164 165 166 168 private boolean whitingOut = false; 169 170 protected void print(Token t, IO io) { 171 Token tt = t.specialToken; 172 if (tt != null) { 173 while (tt.specialToken != null) tt = tt.specialToken; 174 while (tt != null) { 175 io.print(addUnicodeEscapes(translateImage(tt))); 176 tt = tt.next; 177 } 178 } 179 180 188 189 NodeScope s = NodeScope.getEnclosingNodeScope(this); 190 if (s == null) { 191 193 io.print(addUnicodeEscapes(translateImage(t))); 194 return; 195 } 196 197 if (t.image.equals("jjtThis")) { 198 io.print(s.getNodeVariable()); 199 return; 200 } else if (t.image.equals("jjtree")) { 201 if (t.next.image.equals(".")) { 202 if (t.next.next.image.equals("currentNode")) { 203 if (t.next.next.next.image.equals("(")) { 204 if (t.next.next.next.next.image.equals(")")) { 205 208 whitingOut = true; 209 } 210 } 211 } 212 } 213 } 214 if (whitingOut) { 215 if (t.image.equals("jjtree")) { 216 io.print(s.getNodeVariable()); 217 io.print(" "); 218 } else if (t.image.equals(")")) { 219 io.print(" "); 220 whitingOut = false; 221 } else { 222 for (int i = 0; i < t.image.length(); ++i) { 223 io.print(" "); 224 } 225 } 226 return; 227 } 228 229 io.print(addUnicodeEscapes(translateImage(t))); 230 } 231 232 233 protected String addUnicodeEscapes(String str) { 234 String retval = ""; 235 char ch; 236 for (int i = 0; i < str.length(); i++) { 237 ch = str.charAt(i); 238 if ((ch < 0x20 || ch > 0x7e) && ch != '\t' && ch != '\n' && ch != '\r' && ch != '\f') { 239 String s = "0000" + Integer.toString(ch, 16); 240 retval += "\\u" + s.substring(s.length() - 4, s.length()); 241 } else { 242 retval += ch; 243 } 244 } 245 return retval; 246 } 247 248 249 static void openJJTreeComment(IO io, String arg) 250 { 251 if (arg != null) { 252 io.print("/*@bgen(jjtree) " + arg + " */"); 253 } else { 254 io.print("/*@bgen(jjtree)*/"); 255 } 256 } 257 258 259 static void closeJJTreeComment(IO io) 260 { 261 io.print("/*@egen*/"); 262 } 263 264 265 String getIndentation(SimpleNode n) 266 { 267 return getIndentation(n, 0); 268 } 269 270 271 String getIndentation(SimpleNode n, int offset) 272 { 273 String s = ""; 274 for (int i = offset + 1; i < n.getFirstToken().beginColumn; ++i) { 275 s += " "; 276 } 277 return s; 278 } 279 280 } 281 282 283 | Popular Tags |