Search in sources :

Example 21 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class BasePythonTest method execStartRule.

public ParseTree execStartRule(String startRuleName, Parser parser) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Method startRule = null;
    Object[] args = null;
    try {
        startRule = parser.getClass().getMethod(startRuleName);
    } catch (NoSuchMethodException nsme) {
        // try with int _p arg for recursive func
        startRule = parser.getClass().getMethod(startRuleName, int.class);
        args = new Integer[] { 0 };
    }
    ParseTree result = (ParseTree) startRule.invoke(parser, args);
    //		System.out.println("parse tree = "+result.toStringTree(parser));
    return result;
}
Also used : Method(java.lang.reflect.Method) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 22 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class TestXPath method testError.

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());
}
Also used : Lexer(org.antlr.v4.runtime.Lexer) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Parser(org.antlr.v4.runtime.Parser)

Example 23 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class LeftRecursiveRuleTransformer method parseArtificialRule.

public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    lexer.tokens = tokens;
    ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
    p.setTreeAdaptor(adaptor);
    Token ruleStart = null;
    try {
        ParserRuleReturnScope r = p.rule();
        RuleAST tree = (RuleAST) r.getTree();
        ruleStart = (Token) r.getStart();
        GrammarTransformPipeline.setGrammarPtr(g, tree);
        GrammarTransformPipeline.augmentTokensWithOriginalPosition(g, tree);
        return tree;
    } catch (Exception e) {
        tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText);
    }
    return null;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RuleAST(org.antlr.v4.tool.ast.RuleAST) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ANTLRLexer(org.antlr.v4.parse.ANTLRLexer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Token(org.antlr.runtime.Token) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 24 with Tree

use of org.antlr.v4.runtime.tree.Tree in project crate by crate.

the class SqlParser method invokeParser.

private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);
        parser.addParseListener(new PostProcessor());
        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);
        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);
        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            // rewind input stream
            tokenStream.reset();
            parser.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }
        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
Also used : SqlBaseParser(io.crate.sql.parser.antlr.v4.SqlBaseParser) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) SqlBaseLexer(io.crate.sql.parser.antlr.v4.SqlBaseLexer)

Example 25 with Tree

use of org.antlr.v4.runtime.tree.Tree in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method convertParseTreeToDetailNode.

/**
     * Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
     *
     * @param parseTreeNode root node of ParseTree
     * @return root of DetailNode tree
     */
private DetailNode convertParseTreeToDetailNode(ParseTree parseTreeNode) {
    final JavadocNodeImpl rootJavadocNode = createRootJavadocNode(parseTreeNode);
    JavadocNodeImpl currentJavadocParent = rootJavadocNode;
    ParseTree parseTreeParent = parseTreeNode;
    while (currentJavadocParent != null) {
        // remove unnecessary children tokens
        if (currentJavadocParent.getType() == JavadocTokenTypes.TEXT) {
            currentJavadocParent.setChildren((DetailNode[]) JavadocNodeImpl.EMPTY_DETAIL_NODE_ARRAY);
        }
        final JavadocNodeImpl[] children = (JavadocNodeImpl[]) currentJavadocParent.getChildren();
        insertChildrenNodes(children, parseTreeParent);
        if (children.length > 0) {
            currentJavadocParent = children[0];
            parseTreeParent = parseTreeParent.getChild(0);
        } else {
            JavadocNodeImpl nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(currentJavadocParent);
            ParseTree nextParseTreeSibling = getNextSibling(parseTreeParent);
            if (nextJavadocSibling == null) {
                JavadocNodeImpl tempJavadocParent = (JavadocNodeImpl) currentJavadocParent.getParent();
                ParseTree tempParseTreeParent = parseTreeParent.getParent();
                while (nextJavadocSibling == null && tempJavadocParent != null) {
                    nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(tempJavadocParent);
                    nextParseTreeSibling = getNextSibling(tempParseTreeParent);
                    tempJavadocParent = (JavadocNodeImpl) tempJavadocParent.getParent();
                    tempParseTreeParent = tempParseTreeParent.getParent();
                }
            }
            currentJavadocParent = nextJavadocSibling;
            parseTreeParent = nextParseTreeSibling;
        }
    }
    return rootJavadocNode;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) JavadocNodeImpl(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)30 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)21 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)15 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)12 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)6 Tree (org.antlr.v4.runtime.tree.Tree)6 IOException (java.io.IOException)5 Tree (org.antlr.runtime.tree.Tree)4 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)4 Rectangle2D (java.awt.geom.Rectangle2D)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3