示例#1
0
文件: XPath.java 项目: antlr/antlr4
  /**
   * Return a list of all nodes starting at {@code t} as root that satisfy the path. The root {@code
   * /} is relative to the node passed to {@link #evaluate}.
   */
  public Collection<ParseTree> evaluate(final ParseTree t) {
    ParserRuleContext dummyRoot = new ParserRuleContext();
    dummyRoot.children = Collections.singletonList(t); // don't set t's parent.

    Collection<ParseTree> work = Collections.<ParseTree>singleton(dummyRoot);

    int i = 0;
    while (i < elements.length) {
      Collection<ParseTree> next = new LinkedHashSet<ParseTree>();
      for (ParseTree node : work) {
        if (node.getChildCount() > 0) {
          // only try to match next element if it has children
          // e.g., //func/*/stat might have a token node for which
          // we can't go looking for stat nodes.
          Collection<? extends ParseTree> matching = elements[i].evaluate(node);
          next.addAll(matching);
        }
      }
      i++;
      work = next;
    }

    return work;
  }