/** Return ordered list of all children of this node */ public static List<Tree> getChildren(Tree t) { List<Tree> kids = new ArrayList<Tree>(); for (int i=0; i<t.getChildCount(); i++) { kids.add(t.getChild(i)); } return kids; }
/** Return true if t is u's parent or a node on path to root from u. * Use == not equals(). * * @since 4.5.1 */ public static boolean isAncestorOf(Tree t, Tree u) { if ( t==null || u==null || t.getParent()==null ) return false; Tree p = u.getParent(); while ( p!=null ) { if ( t==p ) return true; p = p.getParent(); } return false; }
public static String getNodeText(Tree t, List<String> ruleNames) { if ( ruleNames!=null ) { if ( t instanceof RuleContext ) { int ruleIndex = ((RuleContext)t).getRuleContext().getRuleIndex(); String ruleName = ruleNames.get(ruleIndex); int altNumber = ((RuleContext) t).getAltNumber(); if ( altNumber!=ATN.INVALID_ALT_NUMBER ) { return ruleName+":"+altNumber; } return ruleName; } else if ( t instanceof ErrorNode) { return t.toString(); } else if ( t instanceof TerminalNode) { Token symbol = ((TerminalNode)t).getSymbol(); if (symbol != null) { String s = symbol.getText(); return s; } } } // no recog for rule names Object payload = t.getPayload(); if ( payload instanceof Token ) { return ((Token)payload).getText(); } return t.getPayload().toString(); }
@Override public Tree getFirstChild(Tree parentNode) { return parentNode.getChild(0); }
@Override public boolean isLeaf(Tree node) { return node.getChildCount() == 0; }
@Override public Tree getFirstChild(Tree parentNode) { return parentNode.getChild(0); }
@Override public boolean hasNext() { return tree.getChildCount() > i; }
/** Return first node satisfying the pred * * @since 4.5.1 */ public static Tree findNodeSuchThat(Tree t, Predicate<Tree> pred) { if ( pred.test(t) ) return t; if ( t==null ) return null; int n = t.getChildCount(); for (int i = 0 ; i < n ; i++){ Tree u = findNodeSuchThat(t.getChild(i), pred); if ( u!=null ) return u; } return null; }
@Override public Tree getFirstChild(Tree parentNode) { return parentNode.getChild(0); }
@Override public boolean isLeaf(Tree node) { return node.getChildCount() == 0; }
/** Return a list of all ancestors of this node. The first node of * list is the root and the last is the parent of this node. * * @since 4.5.1 */ public static List<? extends Tree> getAncestors(Tree t) { if ( t.getParent()==null ) return Collections.emptyList(); List<Tree> ancestors = new ArrayList<Tree>(); t = t.getParent(); while ( t!=null ) { ancestors.add(0, t); // insert at start t = t.getParent(); } return ancestors; }
@Override public boolean apply(Tree t) { return ((Token) t.getPayload()).getTokenIndex()>=first; } });
/** Print out a whole tree in LISP form. {@link #getNodeText} is used on the * node payloads to get the text for the nodes. */ public static String toStringTree(final Tree t, final List<String> ruleNames) { String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false); if ( t.getChildCount()==0 ) return s; StringBuilder buf = new StringBuilder(); buf.append("("); s = Utils.escapeWhitespace(getNodeText(t, ruleNames), false); buf.append(s); buf.append(' '); for (int i = 0; i<t.getChildCount(); i++) { if ( i>0 ) buf.append(' '); buf.append(toStringTree(t.getChild(i), ruleNames)); } buf.append(")"); return buf.toString(); }
@Override public Tree getFirstChild(Tree parentNode) { return parentNode.getChild(0); }
@Override public boolean isLeaf(Tree node) { return node.getChildCount() == 0; }
@Override public boolean isChildOfParent(Tree node, Tree parentNode) { return node.getParent() == parentNode; }
@Override public boolean apply(Tree t) { return ((Token) t.getPayload()).getTokenIndex()>=first; } });
@Override public Tree getLastChild(Tree parentNode) { return parentNode.getChild(parentNode.getChildCount() - 1); }
@Override public Tree getFirstChild(Tree parentNode) { return parentNode.getChild(0); }
@Override public boolean isLeaf(Tree node) { return node.getChildCount() == 0; }