use of io.crate.sql.parser.antlr.v4.SqlBaseParser 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)");
}
}
use of io.crate.sql.parser.antlr.v4.SqlBaseParser 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);
}
Aggregations