mv findAll to XPath; rm old test in playground.

This commit is contained in:
Terence Parr 2013-09-16 14:56:35 -07:00
parent 76633525bf
commit 8aeb90ef82
6 changed files with 9 additions and 64 deletions

View File

@ -36,13 +36,11 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.Trees;
import org.antlr.v4.runtime.tree.gui.TreeViewer;
import org.antlr.v4.runtime.tree.xpath.XPath;
import javax.print.PrintException;
import javax.swing.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Future;
@ -157,12 +155,6 @@ public class RuleContext implements RuleNode {
return null;
}
@Override
public Collection<ParseTree> findAll(Parser parser, String xpath) {
XPath p = new XPath(parser, xpath);
return p.evaluate(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) { return visitor.visitChildren(this); }

View File

@ -34,8 +34,6 @@ import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.Token;
import java.util.Collection;
/** An interface to access the tree of {@link RuleContext} objects created
* during a parse that makes the data structure look like a simple parse tree.
* This node represents both internal nodes, rule invocations,
@ -53,8 +51,6 @@ public interface ParseTree extends SyntaxTree {
/** The {@link ParseTreeVisitor} needs a double dispatch method. */
<T> T accept(ParseTreeVisitor<? extends T> visitor);
Collection<ParseTree> findAll(Parser parser, String xpath);
/** Return the combined text of all leaf nodes. Does not get any
* off-channel tokens (if any) so won't return whitespace and
* comments if they are sent to parser on hidden channel.

View File

@ -33,9 +33,7 @@ package org.antlr.v4.runtime.tree;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.xpath.XPath;
import java.util.Collection;
import java.util.List;
public class TerminalNodeImpl implements TerminalNode {
@ -72,12 +70,6 @@ public class TerminalNodeImpl implements TerminalNode {
return null;
}
@Override
public Collection<ParseTree> findAll(Parser parser, String xpath) {
XPath p = new XPath(parser, xpath);
return p.evaluate(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitTerminal(this);

View File

@ -12,7 +12,6 @@ import org.antlr.v4.runtime.tree.ParseTree;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -170,6 +169,12 @@ loop:
}
}
public static Collection<ParseTree> findAll(ParseTree tree, String xpath, Parser parser) {
XPath p = new XPath(parser, xpath);
return p.evaluate(tree);
}
/** Return a list of all nodes starting at t as root that satisfy the path.
* The root / is relative to the node passed to evaluate().
*/

View File

@ -1,41 +0,0 @@
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.io.IOException;
public class TestXPath {
public static void main(String[] args) throws IOException {
CharStream input = new ANTLRFileStream("TestXPath.java");
JavaLexer lex = new JavaLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
JavaParser parser = new JavaParser(tokens);
parser.setBuildParseTree(true);
ParserRuleContext tree = parser.compilationUnit();
System.out.println(tree.toStringTree(parser));
// "/compilationUnit/*"
// "//blockStatement"
// "//StringLiteral"
// "//Identifier"
// "//expression/primary/Identifier"
// "//primary/*"
// "//expression//Identifier"
for (ParseTree t : tree.findAll(parser, "//expression//Identifier") ) {
if ( t instanceof RuleContext ) {
RuleContext r = (RuleContext)t;
System.out.println(" "+parser.getRuleNames()[r.getRuleIndex()]);
}
else {
TerminalNode token = (TerminalNode)t;
System.out.println(" "+token.getText());
}
}
}
}

View File

@ -6,6 +6,7 @@ import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.Pair;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.xpath.XPath;
import org.junit.Test;
import java.util.ArrayList;
@ -206,7 +207,7 @@ public class TestXPath extends BaseTest {
IllegalArgumentException e = null;
try {
tree.findAll(parser, path);
XPath.findAll(tree, path, parser);
}
catch (IllegalArgumentException iae) {
e = iae;
@ -225,7 +226,7 @@ public class TestXPath extends BaseTest {
ParseTree tree = execStartRule(startRuleName, parser);
List<String> nodes = new ArrayList<String>();
for (ParseTree t : tree.findAll(parser, xpath) ) {
for (ParseTree t : XPath.findAll(tree, xpath, parser) ) {
if ( t instanceof RuleContext) {
RuleContext r = (RuleContext)t;
nodes.add(parser.getRuleNames()[r.getRuleIndex()]);