Search in sources :

Example 31 with ParseCancellationException

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();
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener)

Example 32 with ParseCancellationException

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));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException)

Aggregations

ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)30 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)24 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)12 IOException (java.io.IOException)10 RecognitionException (org.antlr.v4.runtime.RecognitionException)10 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)9 ParseTree (org.antlr.v4.runtime.tree.ParseTree)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)7 FileNotFoundException (java.io.FileNotFoundException)6 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)6 Token (org.antlr.v4.runtime.Token)5 BoaParser (boa.parser.BoaParser)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)3 DMLProgram (org.apache.sysml.parser.DMLProgram)3 LanguageException (org.apache.sysml.parser.LanguageException)3 ParseException (org.apache.sysml.parser.ParseException)3