use of org.antlr.v4.runtime.misc.ParseCancellationException in project jwt by emweb.
the class CssParser method parse.
private StyleSheet parse(CharStream stream) throws RecognitionException, ParseCancellationException {
errorListener_.reset();
Listener listener = new Listener();
Css22Lexer lex = new Css22Lexer(stream);
lex.removeErrorListener(ConsoleErrorListener.INSTANCE);
lex.addErrorListener(errorListener_);
CommonTokenStream tokens = new CommonTokenStream(lex);
Css22Parser parser = new Css22Parser(tokens);
parser.addParseListener(listener);
parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
parser.addErrorListener(errorListener_);
parser.styleSheet();
if (!getLastError().isEmpty()) {
return null;
} else {
return listener.getCurrentStylesheet();
}
}
use of org.antlr.v4.runtime.misc.ParseCancellationException in project ksql by confluentinc.
the class GrammarParseUtil method getParseTree.
static <T extends ParserRuleContext> T getParseTree(final String text, final Function<SqlBaseParser, T> parseFunction) {
final SqlBaseLexer sqlBaseLexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString(text)));
final CommonTokenStream tokenStream = new CommonTokenStream(sqlBaseLexer);
final SqlBaseParser sqlBaseParser = new SqlBaseParser(tokenStream);
sqlBaseLexer.removeErrorListeners();
sqlBaseLexer.addErrorListener(ERROR_LISTENER);
sqlBaseParser.removeErrorListeners();
sqlBaseParser.addErrorListener(ERROR_LISTENER);
try {
// first, try parsing w/ potentially faster SLL mode
sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
return castContext(parseFunction.apply(sqlBaseParser));
} catch (final ParseCancellationException ex) {
// if we fail, parse with LL mode
// rewind input stream
tokenStream.seek(0);
sqlBaseParser.reset();
sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.LL);
return castContext(parseFunction.apply(sqlBaseParser));
}
}
Aggregations