1 package antlr; 2 3 9 10 import antlr.collections.AST; 11 import antlr.collections.ASTEnumeration; 12 import antlr.collections.impl.ASTEnumerator; 13 import antlr.collections.impl.Vector; 14 15 import java.io.Serializable ; 16 import java.io.IOException ; 17 import java.io.Writer ; 18 19 44 public abstract class BaseAST implements AST, Serializable { 45 protected BaseAST down; 46 protected BaseAST right; 47 48 private static boolean verboseStringConversion = false; 49 private static String [] tokenNames = null; 50 51 52 public void addChild(AST node) { 53 if (node == null) return; 54 BaseAST t = this.down; 55 if (t != null) { 56 while (t.right != null) { 57 t = t.right; 58 } 59 t.right = (BaseAST)node; 60 } 61 else { 62 this.down = (BaseAST)node; 63 } 64 } 65 66 private void doWorkForFindAll(Vector v, AST target, boolean partialMatch) { 67 AST sibling; 68 69 siblingWalk: 71 for (sibling = this; 72 sibling != null; 73 sibling = sibling.getNextSibling()) { 74 if ((partialMatch && sibling.equalsTreePartial(target)) || 75 (!partialMatch && sibling.equalsTree(target))) { 76 v.appendElement(sibling); 77 } 78 if (sibling.getFirstChild() != null) { 80 ((BaseAST)sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch); 81 } 82 } 83 } 84 85 86 public boolean equals(AST t) { 87 if (t == null) return false; 88 return this.getText().equals(t.getText()) && 89 this.getType() == t.getType(); 90 } 91 92 95 public boolean equalsList(AST t) { 96 AST sibling; 97 98 if (t == null) { 100 return false; 101 } 102 103 for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling()) { 105 if (!sibling.equals(t)) { 107 return false; 108 } 109 if (sibling.getFirstChild() != null) { 111 if (!sibling.getFirstChild().equalsList(t.getFirstChild())) { 112 return false; 113 } 114 } 115 else if (t.getFirstChild() != null) { 117 return false; 118 } 119 } 120 if (sibling == null && t == null) { 121 return true; 122 } 123 return false; 125 } 126 127 130 public boolean equalsListPartial(AST sub) { 131 AST sibling; 132 133 if (sub == null) { 135 return true; 136 } 137 138 for (sibling = this; 140 sibling != null && sub != null; 141 sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) { 142 if (!sibling.equals(sub)) return false; 144 if (sibling.getFirstChild() != null) { 146 if (!sibling.getFirstChild().equalsListPartial(sub.getFirstChild())) return false; 147 } 148 } 149 if (sibling == null && sub != null) { 150 return false; 152 } 153 return true; 155 } 156 157 160 public boolean equalsTree(AST t) { 161 if (!this.equals(t)) return false; 163 if (this.getFirstChild() != null) { 165 if (!this.getFirstChild().equalsList(t.getFirstChild())) return false; 166 } 167 else if (t.getFirstChild() != null) { 169 return false; 170 } 171 return true; 172 } 173 174 177 public boolean equalsTreePartial(AST sub) { 178 if (sub == null) { 180 return true; 181 } 182 183 if (!this.equals(sub)) return false; 185 if (this.getFirstChild() != null) { 187 if (!this.getFirstChild().equalsListPartial(sub.getFirstChild())) return false; 188 } 189 return true; 190 } 191 192 196 public ASTEnumeration findAll(AST target) { 197 Vector roots = new Vector(10); 198 AST sibling; 199 200 if (target == null) { 202 return null; 203 } 204 205 doWorkForFindAll(roots, target, false); 207 return new ASTEnumerator(roots); 208 } 209 210 214 public ASTEnumeration findAllPartial(AST sub) { 215 Vector roots = new Vector(10); 216 AST sibling; 217 218 if (sub == null) { 220 return null; 221 } 222 223 doWorkForFindAll(roots, sub, true); 225 return new ASTEnumerator(roots); 226 } 227 228 229 public AST getFirstChild() { 230 return down; 231 } 232 233 234 public AST getNextSibling() { 235 return right; 236 } 237 238 239 public String getText() { 240 return ""; 241 } 242 243 244 public int getType() { 245 return 0; 246 } 247 248 public abstract void initialize(int t, String txt); 249 250 public abstract void initialize(AST t); 251 252 public abstract void initialize(Token t); 253 254 255 public void removeChildren() { 256 down = null; 257 } 258 259 public void setFirstChild(AST c) { 260 down = (BaseAST)c; 261 } 262 263 public void setNextSibling(AST n) { 264 right = (BaseAST)n; 265 } 266 267 268 public void setText(String text) { 269 } 270 271 272 public void setType(int ttype) { 273 } 274 275 public static void setVerboseStringConversion(boolean verbose, String [] names) { 276 verboseStringConversion = verbose; 277 tokenNames = names; 278 } 279 280 public String toString() { 281 StringBuffer b = new StringBuffer (); 282 if (verboseStringConversion && 284 !getText().equalsIgnoreCase(tokenNames[getType()]) && 285 !getText().equalsIgnoreCase(StringUtils.stripFrontBack(tokenNames[getType()], "\"", "\""))) { 286 b.append('['); 287 b.append(getText()); 288 b.append(",<"); 289 b.append(tokenNames[getType()]); 290 b.append(">]"); 291 return b.toString(); 292 } 293 return getText(); 294 } 295 296 297 public String toStringList() { 298 AST t = this; 299 String ts = ""; 300 if (t.getFirstChild() != null) ts += " ("; 301 ts += " " + this.toString(); 302 if (t.getFirstChild() != null) { 303 ts += ((BaseAST)t.getFirstChild()).toStringList(); 304 } 305 if (t.getFirstChild() != null) ts += " )"; 306 if (t.getNextSibling() != null) { 307 ts += ((BaseAST)t.getNextSibling()).toStringList(); 308 } 309 return ts; 310 } 311 312 public String toStringTree() { 313 AST t = this; 314 String ts = ""; 315 if (t.getFirstChild() != null) ts += " ("; 316 ts += " " + this.toString(); 317 if (t.getFirstChild() != null) { 318 ts += ((BaseAST)t.getFirstChild()).toStringList(); 319 } 320 if (t.getFirstChild() != null) ts += " )"; 321 return ts; 322 } 323 324 public static String decode(String text) { 325 char c, c1, c2, c3, c4, c5; 326 StringBuffer n = new StringBuffer (); 327 for (int i = 0; i < text.length(); i++) { 328 c = text.charAt(i); 329 if (c == '&') { 330 c1 = text.charAt(i + 1); 331 c2 = text.charAt(i + 2); 332 c3 = text.charAt(i + 3); 333 c4 = text.charAt(i + 4); 334 c5 = text.charAt(i + 5); 335 336 if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';') { 337 n.append("&"); 338 i += 5; 339 } 340 else if (c1 == 'l' && c2 == 't' && c3 == ';') { 341 n.append("<"); 342 i += 4; 343 } 344 else if (c1 == 'g' && c2 == 't' && c3 == ';') { 345 n.append(">"); 346 i += 4; 347 } 348 else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && 349 c4 == 't' && c5 == ';') { 350 n.append("\""); 351 i += 6; 352 } 353 else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && 354 c4 == 's' && c5 == ';') { 355 n.append("'"); 356 i += 6; 357 } 358 else 359 n.append("&"); 360 } 361 else 362 n.append(c); 363 } 364 return new String (n); 365 } 366 367 public static String encode(String text) { 368 char c; 369 StringBuffer n = new StringBuffer (); 370 for (int i = 0; i < text.length(); i++) { 371 c = text.charAt(i); 372 switch (c) { 373 case '&': 374 { 375 n.append("&"); 376 break; 377 } 378 case '<': 379 { 380 n.append("<"); 381 break; 382 } 383 case '>': 384 { 385 n.append(">"); 386 break; 387 } 388 case '"': 389 { 390 n.append("""); 391 break; 392 } 393 case '\'': 394 { 395 n.append("'"); 396 break; 397 } 398 default : 399 { 400 n.append(c); 401 break; 402 } 403 } 404 } 405 return new String (n); 406 } 407 408 public void xmlSerializeNode(Writer out) 409 throws IOException { 410 StringBuffer buf = new StringBuffer (100); 411 buf.append("<"); 412 buf.append(getClass().getName() + " "); 413 buf.append("text=\"" + encode(getText()) + "\" type=\"" + 414 getType() + "\"/>"); 415 out.write(buf.toString()); 416 } 417 418 public void xmlSerializeRootOpen(Writer out) 419 throws IOException { 420 StringBuffer buf = new StringBuffer (100); 421 buf.append("<"); 422 buf.append(getClass().getName() + " "); 423 buf.append("text=\"" + encode(getText()) + "\" type=\"" + 424 getType() + "\">\n"); 425 out.write(buf.toString()); 426 } 427 428 public void xmlSerializeRootClose(Writer out) 429 throws IOException { 430 out.write("</" + getClass().getName() + ">\n"); 431 } 432 433 public void xmlSerialize(Writer out) throws IOException { 434 for (AST node = this; 436 node != null; 437 node = node.getNextSibling()) { 438 if (node.getFirstChild() == null) { 439 ((BaseAST)node).xmlSerializeNode(out); 441 } 442 else { 443 ((BaseAST)node).xmlSerializeRootOpen(out); 444 445 ((BaseAST)node.getFirstChild()).xmlSerialize(out); 447 448 ((BaseAST)node).xmlSerializeRootClose(out); 450 } 451 } 452 } 453 454 } 455 | Popular Tags |