예제 #1
0
  public List<String> getNodeStrings(
      String input, String xpath, String startRuleName, String parserName, String lexerName)
      throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    ParseTree tree = execStartRule(startRuleName, parser);

    List<String> nodes = new ArrayList<String>();
    for (ParseTree t : XPath.findAll(tree, xpath, parser)) {
      if (t instanceof RuleContext) {
        RuleContext r = (RuleContext) t;
        nodes.add(parser.getRuleNames()[r.getRuleIndex()]);
      } else {
        TerminalNode token = (TerminalNode) t;
        nodes.add(token.getText());
      }
    }
    return nodes;
  }
예제 #2
0
  protected void testError(
      String input,
      String path,
      String expected,
      String startRuleName,
      String parserName,
      String lexerName)
      throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    ParseTree tree = execStartRule(startRuleName, parser);

    IllegalArgumentException e = null;
    try {
      XPath.findAll(tree, path, parser);
    } catch (IllegalArgumentException iae) {
      e = iae;
    }
    assertNotNull(e);
    assertEquals(expected, e.getMessage());
  }
예제 #3
0
파일: XPath.java 프로젝트: antlr/antlr4
 public static Collection<ParseTree> findAll(ParseTree tree, String xpath, Parser parser) {
   XPath p = new XPath(parser, xpath);
   return p.evaluate(tree);
 }