Search in sources :

Example 1 with SqlBaseLexer

use of io.crate.sql.parser.antlr.v4.SqlBaseLexer 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 2 with SqlBaseLexer

use of io.crate.sql.parser.antlr.v4.SqlBaseLexer in project crate by crate.

the class SqlParser method getParser.

private static SqlBaseParser getParser(String sql) {
    CharStream stream = new CaseInsensitiveStream(new ANTLRInputStream(sql));
    SqlBaseLexer lexer = new SqlBaseLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    return new SqlBaseParser(tokenStream);
}
Also used : SqlBaseParser(io.crate.sql.parser.antlr.v4.SqlBaseParser) SqlBaseLexer(io.crate.sql.parser.antlr.v4.SqlBaseLexer)

Aggregations

SqlBaseLexer (io.crate.sql.parser.antlr.v4.SqlBaseLexer)2 SqlBaseParser (io.crate.sql.parser.antlr.v4.SqlBaseParser)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1