001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.antlr; 005 006 import antlr.collections.AST; 007 import biz.hammurapi.util.PoliteVisitor; 008 import biz.hammurapi.util.Visitable; 009 import biz.hammurapi.util.Visitor; 010 011 012 /** 013 * Navigates Visitor through DOM tree. 014 * @author Pavel Vlasov 015 * @version $Revision: 1.2 $ 016 */ 017 public class AstVisitable implements Visitable { 018 private AST node; 019 private boolean withSiblings; 020 021 public boolean accept(Visitor visitor) { 022 if (withSiblings) { 023 for (AST ast=node; ast!=null; ast=ast.getNextSibling()) { 024 accept(ast, visitor); 025 } 026 return true; 027 } 028 029 return accept(node, visitor); 030 } 031 032 /** 033 * @param node 034 * @param visitor 035 */ 036 private boolean accept(AST node, Visitor visitor) { 037 if (visitor.visit(node)) { 038 for (AST child=node.getFirstChild(); child!=null; child=child.getNextSibling()) { 039 accept(child, visitor); 040 } 041 if (visitor instanceof PoliteVisitor) { 042 ((PoliteVisitor) visitor).leave(node); 043 } 044 return true; 045 } 046 047 return false; 048 } 049 050 /** 051 * @param node 052 */ 053 public AstVisitable(AST node, boolean withSiblings) { 054 super(); 055 this.node = node; 056 this.withSiblings=withSiblings; 057 } 058 }