1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.AST; 10 11 public class ASTIterator { 12 protected AST cursor = null; 13 protected AST original = null; 14 15 16 public ASTIterator(AST t) { 17 original = cursor = t; 18 } 19 20 21 public boolean isSubtree(AST t, AST sub) { 22 AST sibling; 23 24 if (sub == null) { 26 return true; 27 } 28 29 if (t == null) { 31 if (sub != null) return false; 32 return true; 33 } 34 35 for (sibling = t; 37 sibling != null && sub != null; 38 sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) { 39 if (sibling.getType() != sub.getType()) return false; 41 if (sibling.getFirstChild() != null) { 43 if (!isSubtree(sibling.getFirstChild(), sub.getFirstChild())) return false; 44 } 45 } 46 return true; 47 } 48 49 52 public AST next(AST template) { 53 AST t = null; 54 AST sibling = null; 55 56 if (cursor == null) { return null; 58 } 59 60 for (; cursor != null; cursor = cursor.getNextSibling()) { 62 if (cursor.getType() == template.getType()) { 64 if (cursor.getFirstChild() != null) { 66 if (isSubtree(cursor.getFirstChild(), template.getFirstChild())) { 67 return cursor; 68 } 69 } 70 } 71 } 72 return t; 73 } 74 } 75 | Popular Tags |