move getChildren() from Tree into Trees

This commit is contained in:
Terence Parr 2013-11-13 16:18:50 -08:00
parent 9cd32f36b4
commit b26926570d
8 changed files with 22 additions and 21 deletions

View File

@ -193,11 +193,6 @@ public class ParserRuleContext extends RuleContext {
return null;
}
@Override
public List<ParseTree> getChildren() {
return children;
}
public TerminalNode getToken(int ttype, int i) {
if ( children==null || i < 0 || i >= children.size() ) {
return null;

View File

@ -150,11 +150,6 @@ public class RuleContext implements RuleNode {
return 0;
}
@Override
public List<ParseTree> getChildren() {
return null;
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) { return visitor.visitChildren(this); }

View File

@ -65,11 +65,6 @@ public class TerminalNodeImpl implements TerminalNode {
@Override
public int getChildCount() { return 0; }
@Override
public List<ParseTree> getChildren() {
return null;
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitTerminal(this);

View File

@ -62,9 +62,6 @@ public interface Tree {
*/
int getChildCount();
/** Return ordered list of all children of this node */
List<? extends Tree> getChildren();
/** Print out a whole tree, not just a node, in LISP format
* {@code (root child1 .. childN)}. Print just a node if this is a leaf.
*/

View File

@ -154,6 +154,16 @@ public class Trees {
return t.getPayload().toString();
}
/** 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 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.
*/

View File

@ -3,6 +3,7 @@ package org.antlr.v4.runtime.tree.xpath;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.runtime.tree.Trees;
import java.util.ArrayList;
import java.util.Collection;
@ -19,7 +20,7 @@ public class XPathRuleElement extends XPathElement {
public Collection<ParseTree> evaluate(ParseTree t) {
// return all children of t that match nodeName
List<ParseTree> nodes = new ArrayList<ParseTree>();
for (Tree c : t.getChildren()) {
for (Tree c : Trees.getChildren(t)) {
if ( c instanceof ParserRuleContext ) {
ParserRuleContext ctx = (ParserRuleContext)c;
if ( (ctx.getRuleIndex() == ruleIndex && !invert) ||

View File

@ -3,6 +3,7 @@ package org.antlr.v4.runtime.tree.xpath;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.runtime.tree.Trees;
import java.util.ArrayList;
import java.util.Collection;
@ -19,7 +20,7 @@ public class XPathTokenElement extends XPathElement {
public Collection<ParseTree> evaluate(ParseTree t) {
// return all children of t that match nodeName
List<ParseTree> nodes = new ArrayList<ParseTree>();
for (Tree c : t.getChildren()) {
for (Tree c : Trees.getChildren(t)) {
if ( c instanceof TerminalNode ) {
TerminalNode tnode = (TerminalNode)c;
if ( (tnode.getSymbol().getType() == tokenType && !invert) ||

View File

@ -1,9 +1,12 @@
package org.antlr.v4.runtime.tree.xpath;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.runtime.tree.Trees;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class XPathWildcardElement extends XPathElement {
@ -14,6 +17,10 @@ public class XPathWildcardElement extends XPathElement {
@Override
public Collection<ParseTree> evaluate(final ParseTree t) {
if ( invert ) return new ArrayList<ParseTree>(); // !* is weird but valid (empty)
return new ArrayList<ParseTree>() {{addAll((List<ParseTree>)t.getChildren());}};
List<ParseTree> kids = new ArrayList<ParseTree>();
for (Tree c : Trees.getChildren(t)) {
kids.add((ParseTree)c);
}
return kids;
}
}