1 package antlr; 2 3 9 10 import antlr.collections.AST; 11 12 public class ASTIterator { 13 protected AST cursor = null; 14 protected AST original = null; 15 16 17 public ASTIterator(AST t) { 18 original = cursor = t; 19 } 20 21 22 public boolean isSubtree(AST t, AST sub) { 23 AST sibling; 24 25 if (sub == null) { 27 return true; 28 } 29 30 if (t == null) { 32 if (sub != null) return false; 33 return true; 34 } 35 36 for (sibling = t; 38 sibling != null && sub != null; 39 sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) { 40 if (sibling.getType() != sub.getType()) return false; 42 if (sibling.getFirstChild() != null) { 44 if (!isSubtree(sibling.getFirstChild(), sub.getFirstChild())) return false; 45 } 46 } 47 return true; 48 } 49 50 53 public AST next(AST template) { 54 AST t = null; 55 AST sibling = null; 56 57 if (cursor == null) { return null; 59 } 60 61 for (; cursor != null; cursor = cursor.getNextSibling()) { 63 if (cursor.getType() == template.getType()) { 65 if (cursor.getFirstChild() != null) { 67 if (isSubtree(cursor.getFirstChild(), template.getFirstChild())) { 68 return cursor; 69 } 70 } 71 } 72 } 73 return t; 74 } 75 } 76 | Popular Tags |