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);
}
}
Aggregations