Java 类org.antlr.v4.runtime.tree.xpath.XPath 实例源码

项目:ftc    文件:QueryManipulator.java   
public ResolvedTableNames getAlterTableIdentifiers() {
    Check.isTrue(statementType == StatementType.ALTER);

    FusionTablesSqlParser parser = getParser();
    VerboseErrorListener errorListener = Util.addVerboseErrorListener(parser);

    String xpath = "//alter_table_stmt//string_literal";
    Iterator<ParseTree> names = XPath.findAll(parser.fusionTablesSql(), xpath, parser).iterator();

    String nameFrom = null;
    String nameTo = null;

    nameFrom = StringUtil.stripQuotes(getNextTerminal(names, parser));
    nameTo = StringUtil.stripQuotes(getNextTerminal(names, parser));

    return resolveTableNames(nameFrom, nameTo, errorListener.getErrors());
}
项目:JavaCSS    文件:ASTHelperTest.java   
@Test
public void add_new_AST_node()
    throws Exception
{
    Java8Parser parser = buildParser();
    ParseTree tree = buildAST(parser);
    Assert.assertNotNull(tree);

    String myType = ASTHelperTest.class.getName();

    ASTHelper astHelper = new ASTHelper(tree);
    astHelper.addImport(myType);

    Collection<ParseTree> imports = XPath.findAll(tree, "//importDeclaration", parser);
    Assert.assertNotNull(imports);
    boolean found = false;

    for (ParseTree node : imports) {
        if (("import" + myType + ";").equals(node.getText())) {
            found = true;
            break;
        }
    }
    Assert.assertTrue(found);
}
项目:querqy    文件:QueryGrammarTest.java   
@Test
public void testUnicode() throws Exception {
   QueryParser parser = getParser("törmÈ ዩኒኮድ ምንድን ነው? ดินสอ 铅笔 قلم עפרון");

   Collection<ParseTree> trees = XPath.findAll(parser.query(), "//termQuery", parser);
   assertThat(trees, hasSize(8));
   assertThat(trees, hasItem(text("törmÈ")));
   assertThat(trees, hasItem(text("ዩኒኮድ")));
   assertThat(trees, hasItem(text("ምንድን")));
   assertThat(trees, hasItem(text("ነው?")));
   assertThat(trees, hasItem(text("ดินสอ")));
   assertThat(trees, hasItem(text("铅笔")));
   assertThat(trees, hasItem(text("עפרון")));
   assertThat(trees, hasItem(text("قلم")));

}
项目:intellij-plugin-v4    文件:RefactorUtils.java   
public static TerminalNode getRuleDefNameNode(Parser parser, ParseTree tree, String ruleName) {
    Collection<ParseTree> ruleDefRuleNodes;
    if ( Grammar.isTokenName(ruleName) ) {
        ruleDefRuleNodes = XPath.findAll(tree, "//lexerRule/TOKEN_REF", parser);
    }
    else {
        ruleDefRuleNodes = XPath.findAll(tree, "//parserRuleSpec/RULE_REF", parser);
    }
    for (ParseTree node : ruleDefRuleNodes) {
        String r = node.getText(); // always a TerminalNode; just get rule name of this def
        if ( r.equals(ruleName) ) {
            return (TerminalNode)node;
        }
    }
    return null;
}
项目:intellij-plugin-v4    文件:RefactorUtils.java   
public static List<TerminalNode> getAllRuleRefNodes(Parser parser, ParseTree tree, String ruleName) {
    List<TerminalNode> nodes = new ArrayList<TerminalNode>();
    Collection<ParseTree> ruleRefs;
    if ( Grammar.isTokenName(ruleName) ) {
        ruleRefs = XPath.findAll(tree, "//lexerRuleBlock//TOKEN_REF", parser);
    }
    else {
        ruleRefs = XPath.findAll(tree, "//ruleBlock//RULE_REF", parser);
    }
    for (ParseTree node : ruleRefs) {
        TerminalNode terminal = (TerminalNode)node;
        Token rrefToken = terminal.getSymbol();
        String r = rrefToken.getText();
        if ( r.equals(ruleName) ) {
            nodes.add(terminal);
        }
    }
    if ( nodes.size()==0 ) return null;
    return nodes;
}
项目:ftc    文件:TestQueryManipulation.java   
public void treeXpath() {
    FusionTablesSqlParser parser = getParserStuff().parser;
    String xpath = "//sql_stmt";

    for (ParseTree t : XPath.findAll(parser.fusionTablesSql(), xpath, parser))
        System.out.println(Util.getSqlStatementType(t, parser).name());

}
项目:ftc    文件:QueryManipulator.java   
@SuppressWarnings("unused")
private StatementType getStatementType(FusionTablesSqlParser parser) {
    String xpath = "//" + Const.rulename_sql_stmt;

    Collection<ParseTree> s = XPath.findAll(parser.fusionTablesSql(), xpath, parser);
    if (s.size() == 0)
        return StatementType.UNKNOWN;
    else
        return Util.getSqlStatementType(s.iterator().next(), parser);
}
项目:ftc    文件:QueryManipulator.java   
public DiggedAliases getTableAliases() {
    Map<String, String> result = new HashMap<String, String>();
    FusionTablesSqlParser parser = getParser();
    VerboseErrorListener errorListener = Util.addVerboseErrorListener(parser);

    String xpath = "//table_name_with_alias";
    Iterator<ParseTree> trees = XPath.findAll(parser.fusionTablesSql(), xpath, parser).iterator();
    while (trees.hasNext()) {
        ParseTree parseTree = trees.next();
        addAlias(result, parseTree);
    }
    return new DiggedAliases(result, errorListener.getErrors());
}
项目:JavaCSS    文件:MethodHelper.java   
public List<String> retrieveReturnTypesOfMethods(ParseTree tree, Java8Parser parser)
{
    List<String> result = new ArrayList<>();

    for (ParseTree node : XPath.findAll(tree, "//methodHeader", parser))
    {
        ParseTreeWalker walker = new ParseTreeWalker();
        ReturnTypesOfMethodsListener listener = new ReturnTypesOfMethodsListener();
        walker.walk(listener, node.getChild(0));
        result.addAll(listener.getReturnTypesOfMethods());
    }

    return result;
}
项目:JavaCSS    文件:MethodHelper.java   
public List<String> retrieveReturnTypesOfMethodsXpath(ParseTree tree, Java8Parser parser)
{
    List<String> result = new ArrayList<>();

    for (ParseTree node : XPath.findAll(tree, "//methodHeader/result//*/!typeArguments", parser)) {
        if (node instanceof TerminalNode) {
            result.add(((TerminalNode) node).getText());
        } else {
            result.add(node.getText());
        }
    }

    return result;
}
项目:JavaCSS    文件:StringTemplateCSSHelper.java   
protected void initialize(String input)
{
    StringTemplateCSSLexer lexer = new StringTemplateCSSLexer(new ANTLRInputStream(input));

    CommonTokenStream tokens = new CommonTokenStream(lexer);

    StringTemplateCSSParser parser = new StringTemplateCSSParser(tokens);

    parser.setErrorHandler(new BailErrorStrategy());

    ParseTree tree = parser.css();

    Collection<ParseTree> selectorCombinations = XPath.findAll(tree, "//selectorCombination", parser);

    final StringUtils stringUtils = StringUtils.getInstance();

    this.selectors = new ArrayList<List<String>>(selectorCombinations.size());
    this.properties = new HashMap<List<String>, Map<String, String>>();

    for (ParseTree selectorCombination : selectorCombinations) {
        List<String> currentSelectors = new ArrayList<String>(selectorCombination.getChildCount());
        this.selectors.add(currentSelectors);

        for (int index = 0; index < selectorCombination.getChildCount(); index++) {
            String text = selectorCombination.getChild(index).getText();
            currentSelectors.add(text);
        }
        Map<String, String> block = retrieveProperties(selectorCombination, stringUtils);

        this.properties.put(currentSelectors, block);
    }
}
项目:JavaCSS    文件:SelectorMatchVisitorTest.java   
@Test
public void compares_selectors_correctly() {
    String javaInput = "package com.foo.bar;";

    Java8Lexer lexer = new Java8Lexer(new ANTLRInputStream(javaInput));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Java8Parser parser = new Java8Parser(tokens);
    ParseTree ast = parser.compilationUnit();

    Collection<ParseTree> matches = XPath.findAll(ast, "//';'", parser);

    Assert.assertNotNull(matches);
    Assert.assertEquals(1, matches.size());

    ParseTree semiColon = matches.toArray(new ParseTree[1])[0];
    Assert.assertNotNull(semiColon);

    matches = XPath.findAll(ast, "//'package'", parser);

    Assert.assertNotNull(matches);
    Assert.assertEquals(1, matches.size());

    ParseTree packageNode = matches.toArray(new ParseTree[1])[0];
    Assert.assertNotNull(packageNode);

    List<String> selectors = Arrays.asList(".packageDeclaration", "\";\"::before");

    SelectorMatchVisitor visitor = new SelectorMatchVisitor(selectors, ast);

    Assert.assertTrue(visitor.matches(ast.getChild(0), ".packageDeclaration"));
    Assert.assertTrue(visitor.matches(semiColon, "\";\"::before"));
}
项目:JavaCSS    文件:StringTemplateCSSHelperTest.java   
@Test
public void selector_found(){
    String javaInput = "package com.foo.bar;";

    String cssInput =
        ".packageDeclaration \";\"::before {\n"
        + "   content: \" \";\n"
        + "}\n";

    StringTemplateCSSHelper helper = new StringTemplateCSSHelper(cssInput);

    Java8Lexer lexer = new Java8Lexer(new ANTLRInputStream(javaInput));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Java8Parser parser = new Java8Parser(tokens);
    ParseTree ast = parser.compilationUnit();

    Collection<ParseTree> matches = XPath.findAll(ast, "//';'", parser);

    Assert.assertNotNull(matches);
    Assert.assertEquals(1, matches.size());

    ParseTree semiColon = matches.toArray(new ParseTree[1])[0];
    Assert.assertNotNull(semiColon);

    matches = XPath.findAll(ast, "//'package'", parser);

    Assert.assertNotNull(matches);
    Assert.assertEquals(1, matches.size());

    ParseTree packageNode = matches.toArray(new ParseTree[1])[0];
    Assert.assertNotNull(packageNode);

    List<String> selectors = Arrays.asList(".packageDeclaration", "\";\"::before");

    Assert.assertTrue(helper.match(selectors, semiColon, ast));
    Assert.assertFalse(helper.match(selectors, packageNode, ast));
}
项目:hibernate-semantic-query    文件:HqlParserTest.java   
@Test
public void justTestIt() throws Exception {
    HqlParser parser = HqlParseTreeBuilder.INSTANCE.parseHql( "select a.b from Something a where a.c = '1'" );

    Collection<ParseTree> fromClauses = XPath.findAll( parser.statement(), "//fromClause", parser );
    assertEquals( 1, fromClauses.size() );
}
项目:hibernate-semantic-query    文件:HqlParserTest.java   
@Test
public void testIndexAccess() throws Exception {
    final String qry = "select o from Order o where o.items[0].id = 1234";

    HqlParser parser = HqlParseTreeBuilder.INSTANCE.parseHql( qry );

    Collection<ParseTree> fromClauses = XPath.findAll( parser.statement(), "//fromClause", parser );
    assertEquals( 1, fromClauses.size() );
}
项目:hibernate-semantic-query    文件:HqlParserTest.java   
/**
 * Pass in a parse tree that has exactly one equality predicate with the right hand operand being the expected literal
 */
private void validateDateTimeLiteralInEqualityPredicate(HqlParser parser, DateTimeLiteralType expectedType) {
    Collection<ParseTree> predicates = XPath.findAll( parser.statement(), "//predicate", parser );
    assertEquals( 1, predicates.size() );

    EqualityPredicateContext predicate = (EqualityPredicateContext) predicates.iterator().next();
    // predicate[1] -> the literal
    assertTrue( predicate.expression( 1 ) instanceof LiteralExpressionContext );

    LiteralExpressionContext lec = (LiteralExpressionContext) predicate.expression( 1 );
    DateTimeLiteralType found = lec.literal().accept( new DateTimeLiteralCategorizingVisitor() );

    assertEquals( expectedType, found );
}
项目:jetbrains-plugin-st4    文件:STGroupSyntaxHighlighter.java   
@Override
public ParsingResult parse(CommonTokenStream tokens) {
    STGParser parser = new STGParser(tokens);
    parser.removeErrorListeners();
    ParserErrorListener errorListener = new ParserErrorListener();
    parser.addErrorListener(errorListener);
    ParserRuleContext tree = parser.group();
    final Collection<ParseTree> delimiterStrings = XPath.findAll(tree, "//delimiters/STRING", parser);
    int i = 0;
    for (ParseTree s : delimiterStrings) {
        delimiters[i++] = s.getText().charAt(1);
    }
    return new ParsingResult(parser, tree, errorListener);
}
项目:jetbrains-plugin-st4    文件:STSyntaxHighlighter.java   
@Override
public void highlightTree(ParserRuleContext tree, Parser parser) {
    final Collection<ParseTree> ids = XPath.findAll(tree, "//primary/ID", parser);
    for (ParseTree id : ids) {
        TerminalNode tnode = (TerminalNode)id;
        highlightToken(tnode.getSymbol(), new TextAttributesKey[]{ST_ID});
    }
}
项目:antmark    文件:MarkdownTranslator.java   
public MarkdownTranslator(ParseTree tree, MarkdownParser parser) {
    this.tree = tree;
    this.parser = parser;
    // Collect reference to be used for links and images.
    for (ParseTree t: XPath.findAll(tree, "//reference", parser) ) {
        MarkdownParser.ReferenceContext reference = (MarkdownParser.ReferenceContext)t;
        references.put(reference.referenceLabel().referenceId().getText().toLowerCase(), reference);
    }
}
项目:Scratch-ApuC    文件:ParseTreePattern.java   
/**
 * Find all nodes using XPath and then try to match those subtrees against
 * this tree pattern.
 *
 * @param tree The {@link ParseTree} to match against this pattern.
 * @param xpath An expression matching the nodes
 *
 * @return A collection of {@link ParseTreeMatch} objects describing the
 * successful matches. Unsuccessful matches are omitted from the result,
 * regardless of the reason for the failure.
 */
@NotNull
public List<ParseTreeMatch> findAll(@NotNull ParseTree tree, @NotNull String xpath) {
    Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser());
    List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>();
    for (ParseTree t : subtrees) {
        ParseTreeMatch match = match(t);
        if ( match.succeeded() ) {
            matches.add(match);
        }
    }
    return matches;
}
项目:JavaCSS    文件:StringTemplateCSSHelperTest.java   
@Test
public void finds_the_matching_css() {
    String javaInput = "package com.foo.bar;";

    String cssInput =
          ".packageDeclaration \";\"::before {\n"
        + "   content: \" \";\n"
        + "}\n";

    StringTemplateCSSHelper helper = new StringTemplateCSSHelper(cssInput);

    Java8Lexer lexer = new Java8Lexer(new ANTLRInputStream(javaInput));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Java8Parser parser = new Java8Parser(tokens);
    ParseTree ast = parser.compilationUnit();

    Collection<ParseTree> matches = XPath.findAll(ast, "//';'", parser);

    Assert.assertNotNull(matches);
    Assert.assertEquals(1, matches.size());

    ParseTree semiColon = matches.toArray(new ParseTree[1])[0];
    Assert.assertNotNull(semiColon);

    List<Css> matchedCss = helper.retrieveMatchingCss(semiColon, ast);

    Assert.assertNotNull(matchedCss);
    Assert.assertEquals(1, matchedCss.size());
    Css css = matchedCss.get(0);
    Assert.assertNotNull(css);
    List<String> matchedSelectors = css.getSelectors();
    Assert.assertNotNull(matchedSelectors);
    Assert.assertEquals(2, matchedSelectors.size());
    Assert.assertEquals(".packageDeclaration", matchedSelectors.get(0));
    Assert.assertEquals("\";\"::before", matchedSelectors.get(1));

    List<Property<?>> properties = css.getProperties();
    Assert.assertNotNull(properties);
    Assert.assertEquals(1, properties.size());
    @SuppressWarnings("unchecked")
    Property<String> content = (Property<String>) properties.get(0);
    Assert.assertNotNull(content);
    Assert.assertEquals("content", content.getKey());
    Assert.assertEquals(" ", content.getValue());
}
项目:intellij-plugin-v4    文件:GenerateLexerRulesForLiteralsAction.java   
@Override
    public void actionPerformed(AnActionEvent e) {
        LOG.info("actionPerformed GenerateLexerRulesForLiteralsAction");
        final Project project = e.getProject();

        final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        if (psiFile == null) {
            return;
        }
        String inputText = psiFile.getText();
        ParsingResult results = ParsingUtils.parseANTLRGrammar(inputText);

        final Parser parser = results.parser;
        final ParseTree tree = results.tree;
        Collection<ParseTree> literalNodes = XPath.findAll(tree, "//ruleBlock//STRING_LITERAL", parser);
        LinkedHashMap<String, String> lexerRules = new LinkedHashMap<String, String>();
        for (ParseTree node : literalNodes) {
            String literal = node.getText();
            String ruleText = String.format("%s : %s ;",
                                            RefactorUtils.getLexerRuleNameFromLiteral(literal), literal);
            lexerRules.put(literal, ruleText);
        }

        // remove those already defined
        String lexerRulesXPath = "//lexerRule";
        String treePattern = "<TOKEN_REF> : <STRING_LITERAL>;";
        ParseTreePattern p = parser.compileParseTreePattern(treePattern, ANTLRv4Parser.RULE_lexerRule);
        List<ParseTreeMatch> matches = p.findAll(tree, lexerRulesXPath);

        for (ParseTreeMatch match : matches) {
            ParseTree lit = match.get("STRING_LITERAL");
            if (lexerRules.containsKey(lit.getText())) { // we have rule for this literal already
                lexerRules.remove(lit.getText());
            }
        }

        final LiteralChooser chooser =
            new LiteralChooser(project, new ArrayList<String>(lexerRules.values()));
        chooser.show();
        List<String> selectedElements = chooser.getSelectedElements();
        // chooser disposed automatically.

        final Editor editor = e.getData(PlatformDataKeys.EDITOR);
        final Document doc = editor.getDocument();
        final CommonTokenStream tokens = (CommonTokenStream) parser.getTokenStream();
//      System.out.println(selectedElements);
        if (selectedElements != null) {
            String text = doc.getText();
            int cursorOffset = editor.getCaretModel().getOffset();
            // make sure it's not in middle of rule; put between.
//                  System.out.println("offset "+cursorOffset);
            Collection<ParseTree> allRuleNodes = XPath.findAll(tree, "//ruleSpec", parser);
            for (ParseTree r : allRuleNodes) {
                Interval extent = r.getSourceInterval(); // token indexes
                int start = tokens.get(extent.a).getStartIndex();
                int stop = tokens.get(extent.b).getStopIndex();
//                      System.out.println("rule "+r.getChild(0).getText()+": "+start+".."+stop);
                if (cursorOffset < start) {
                    // before this rule, so must be between previous and this one
                    cursorOffset = start; // put right before this rule
                    break;
                }
                else if (cursorOffset >= start && cursorOffset <= stop) {
                    // cursor in this rule
                    cursorOffset = stop + 2; // put right before this rule (after newline)
                    if (cursorOffset >= text.length()) {
                        cursorOffset = text.length();
                    }
                    break;
                }
            }

            String allRules = Utils.join(selectedElements.iterator(), "\n");
            text =
                text.substring(0, cursorOffset) +
                    "\n" + allRules + "\n" +
                    text.substring(cursorOffset, text.length());
            MyPsiUtils.replacePsiFileFromText(project, psiFile, text);
        }
    }
项目:intellij-plugin-v4    文件:RefactorUtils.java   
public static boolean ruleHasMultipleOutermostAlts(Parser parser, ParseTree ruleTree) {
    Collection<ParseTree> ors = XPath.findAll(ruleTree, "/parserRuleSpec/ruleBlock/ruleAltList/OR", parser);
    if ( ors.size()>=1 ) return true;
    ors = XPath.findAll(ruleTree, "/lexerRule/lexerRuleBlock/lexerAltList/OR", parser);
    return ors.size()>=1;
}
项目:ftc    文件:QueryManipulator.java   
public ResolvedTableNames getTableNameToDrop() {
    Check.isTrue(statementType == StatementType.DROP);

    FusionTablesSqlParser parser = getParser();
    VerboseErrorListener errorListener = Util.addVerboseErrorListener(parser);

    String xpath = "//string_literal";

    Iterator<ParseTree> name = XPath.findAll(parser.fusionTablesSql(), xpath, parser).iterator();
    String tableName = StringUtil.stripQuotes(getNextTerminal(name, parser));

    return resolveTableNames(tableName, null, errorListener.getErrors());
}
项目:querqy    文件:QueryGrammarTest.java   
@Test
public void testSingleTermQuery() {

   QueryParser parser = getParser("termOne");

   Collection<ParseTree> trees = XPath.findAll(parser.query(), "//termQuery", parser);
   assertThat(trees, hasSize(1));
   assertThat(trees, hasItem(text("termOne")));

}