Merge pull request #417 from sharwell/fix-370

Fix 370
This commit is contained in:
Sam Harwell 2014-01-15 12:33:11 -08:00
commit 511c60fe7b
2 changed files with 7 additions and 3 deletions

View File

@ -12,6 +12,8 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
/** Represent a subset of XPath XML path syntax for use in identifying nodes in /** Represent a subset of XPath XML path syntax for use in identifying nodes in
@ -176,14 +178,14 @@ loop:
*/ */
public Collection<ParseTree> evaluate(final ParseTree t) { public Collection<ParseTree> evaluate(final ParseTree t) {
ParserRuleContext dummyRoot = new ParserRuleContext(); ParserRuleContext dummyRoot = new ParserRuleContext();
dummyRoot.children = new ArrayList<ParseTree>() {{add(t);}}; // don't set t's parent. dummyRoot.children = Collections.singletonList(t); // don't set t's parent.
Collection<ParseTree> work = new ArrayList<ParseTree>(); Collection<ParseTree> work = Collections.<ParseTree>singleton(dummyRoot);
work.add(dummyRoot); work.add(dummyRoot);
int i = 0; int i = 0;
while ( i < elements.length ) { while ( i < elements.length ) {
Collection<ParseTree> next = new ArrayList<ParseTree>(); Collection<ParseTree> next = new LinkedHashSet<ParseTree>();
for (ParseTree node : work) { for (ParseTree node : work) {
if ( node.getChildCount()>0 ) { if ( node.getChildCount()>0 ) {
// only try to match next element if it has children // only try to match next element if it has children

View File

@ -76,6 +76,7 @@ public class TestXPath extends BaseTest {
"//expr/!primary", // anything but primary under any expr node "//expr/!primary", // anything but primary under any expr node
"//!*", // nothing anywhere "//!*", // nothing anywhere
"/!*", // nothing at root "/!*", // nothing at root
"//expr//ID", // any ID under any expression (tests antlr/antlr4#370)
}; };
String expected[] = { String expected[] = {
"[func, func]", "[func, func]",
@ -97,6 +98,7 @@ public class TestXPath extends BaseTest {
"[expr, expr, expr, expr, expr, expr]", "[expr, expr, expr, expr, expr, expr]",
"[]", "[]",
"[]", "[]",
"[y, x]",
}; };
for (int i=0; i<xpath.length; i++) { for (int i=0; i<xpath.length; i++) {