org.antlr.v4.runtime.DefaultErrorStrategy Java Examples
The following examples show how to use
org.antlr.v4.runtime.DefaultErrorStrategy.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SQLParserExecutor.java From shardingsphere with Apache License 2.0 | 5 votes |
private ParseASTNode twoPhaseParse() { SQLParser sqlParser = SQLParserFactory.newInstance(databaseTypeName, sql); try { ((Parser) sqlParser).setErrorHandler(new BailErrorStrategy()); ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.SLL); return (ParseASTNode) sqlParser.parse(); } catch (final ParseCancellationException ex) { ((Parser) sqlParser).reset(); ((Parser) sqlParser).setErrorHandler(new DefaultErrorStrategy()); ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL); return (ParseASTNode) sqlParser.parse(); } }
Example #2
Source File: SqlParser.java From presto with Apache License 2.0 | 4 votes |
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction, ParsingOptions parsingOptions) { try { SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString(sql))); CommonTokenStream tokenStream = new CommonTokenStream(lexer); SqlBaseParser parser = new SqlBaseParser(tokenStream); initializer.accept(lexer, parser); // Override the default error strategy to not attempt inserting or deleting a token. // Otherwise, it messes up error reporting parser.setErrorHandler(new DefaultErrorStrategy() { @Override public Token recoverInline(Parser recognizer) throws RecognitionException { if (nextTokensContext == null) { throw new InputMismatchException(recognizer); } else { throw new InputMismatchException(recognizer, nextTokensState, nextTokensContext); } } }); parser.addParseListener(new PostProcessor(Arrays.asList(parser.getRuleNames()), parser)); lexer.removeErrorListeners(); lexer.addErrorListener(LEXER_ERROR_LISTENER); parser.removeErrorListeners(); if (enhancedErrorHandlerEnabled) { parser.addErrorListener(PARSER_ERROR_HANDLER); } else { parser.addErrorListener(LEXER_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 tokenStream.seek(0); // rewind input stream parser.reset(); parser.getInterpreter().setPredictionMode(PredictionMode.LL); tree = parseFunction.apply(parser); } return new AstBuilder(parsingOptions).visit(tree); } catch (StackOverflowError e) { throw new ParsingException(name + " is too large (stack overflow while parsing)"); } }
Example #3
Source File: Parser.java From graql with GNU Affero General Public License v3.0 | 4 votes |
private <CONTEXT extends ParserRuleContext, RETURN> RETURN parseQuery( String queryString, Function<GraqlParser, CONTEXT> parserMethod, Function<CONTEXT, RETURN> visitor ) { if (queryString == null || queryString.isEmpty()) { throw GraqlException.create("Query String is NULL or Empty"); } ErrorListener errorListener = ErrorListener.of(queryString); CharStream charStream = CharStreams.fromString(queryString); GraqlLexer lexer = new GraqlLexer(charStream); lexer.removeErrorListeners(); lexer.addErrorListener(errorListener); CommonTokenStream tokens = new CommonTokenStream(lexer); GraqlParser parser = new GraqlParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(errorListener); // BailErrorStrategy + SLL is a very fast parsing strategy for queries // that are expected to be correct. However, it may not be able to // provide detailed/useful error message, if at all. parser.setErrorHandler(new BailErrorStrategy()); parser.getInterpreter().setPredictionMode(PredictionMode.SLL); CONTEXT queryContext; try { queryContext = parserMethod.apply(parser); } catch (ParseCancellationException e) { // We parse the query one more time, with "strict strategy" : // DefaultErrorStrategy + LL_EXACT_AMBIG_DETECTION // This was not set to default parsing strategy, but it is useful // to produce detailed/useful error message parser.setErrorHandler(new DefaultErrorStrategy()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); queryContext = parserMethod.apply(parser); throw GraqlException.create(errorListener.toString()); } return visitor.apply(queryContext); }