Search in sources :

Example 1 with PgArrayLexer

use of io.crate.protocols.postgres.antlr.v4.PgArrayLexer in project crate by crate.

the class PgArrayParser method invokeParser.

private Object invokeParser(InputStream inputStream, Function<io.crate.protocols.postgres.antlr.v4.PgArrayParser, ParserRuleContext> parseFunction, Function<byte[], Object> convert) {
    try {
        var lexer = new PgArrayLexer(CharStreams.fromStream(inputStream, StandardCharsets.UTF_8));
        var tokenStream = new CommonTokenStream(lexer);
        var parser = new io.crate.protocols.postgres.antlr.v4.PgArrayParser(tokenStream);
        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.seek(0);
            parser.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }
        return tree.accept(new PgArrayASTVisitor(convert));
    } catch (StackOverflowError e) {
        throw new PgArrayParsingException("stack overflow while parsing: " + e.getLocalizedMessage());
    } catch (IOException e) {
        return new IllegalArgumentException(e);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) PgArrayLexer(io.crate.protocols.postgres.antlr.v4.PgArrayLexer) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException)

Aggregations

PgArrayLexer (io.crate.protocols.postgres.antlr.v4.PgArrayLexer)1 IOException (java.io.IOException)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1