Example usage for org.antlr.v4.runtime DefaultErrorStrategy DefaultErrorStrategy

List of usage examples for org.antlr.v4.runtime DefaultErrorStrategy DefaultErrorStrategy

Introduction

In this page you can find the example usage for org.antlr.v4.runtime DefaultErrorStrategy DefaultErrorStrategy.

Prototype

DefaultErrorStrategy

Source Link

Usage

From source file:com.ibm.bi.dml.parser.python.PyDMLParserWrapper.java

License:Open Source License

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
 * @param fileName/*ww  w. ja  v a  2  s  . co m*/
 * @return null if atleast one error
 */
public DMLProgram doParse(String fileName, String dmlScript, HashMap<String, String> argVals)
        throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = DMLParserWrapper.readDMLScript(fileName);
        }

        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
        //         else {
        //            if(!(new File(fileName)).exists()) {
        //               throw new ParseException("ERROR: Cannot open file:" + fileName);
        //            }
        //            in = new ANTLRInputStream(new FileInputStream(fileName));
        //         }
    } catch (FileNotFoundException e) {
        throw new ParseException("ERROR: Cannot find file:" + fileName);
    } catch (IOException e) {
        throw new ParseException("ERROR: Cannot open file:" + fileName);
    } catch (LanguageException e) {
        throw new ParseException("ERROR: " + e.getMessage());
    }

    PmlprogramContext ast = null;
    CustomDmlErrorListener errorListener = new CustomDmlErrorListener();

    try {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(tokens);

        boolean tryOptimizedParsing = false; // For now no optimization, since it is not able to parse integer value. 

        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.pmlprogram();
                // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.pushCurrentFileName(fileName);
                } else {
                    errorListener.pushCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.pmlprogram();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.pushCurrentFileName(fileName);

            // Now do the parsing
            ast = antlr4Parser.pmlprogram();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName);
    }

    try {
        // Now convert the parse tree into DMLProgram
        // Do syntactic validation while converting 
        ParseTree tree = ast;
        // And also do syntactic validation
        ParseTreeWalker walker = new ParseTreeWalker();
        PydmlSyntacticValidatorHelper helper = new PydmlSyntacticValidatorHelper(errorListener);
        PydmlSyntacticValidator validator = new PydmlSyntacticValidator(helper, fileName, argVals);
        walker.walk(validator, tree);
        errorListener.popFileName();
        if (errorListener.isAtleastOneError()) {
            return null;
        }
        dmlPgm = createDMLProgram(ast);
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot translate the parse tree into DMLProgram:" + e.getMessage());
    }

    return dmlPgm;
}

From source file:com.jmcalc.ExpressionParser.java

License:Apache License

protected static JMCalcParser setDefaultErrorMode(JMCalcParser parser) {
    parser.setErrorHandler(new DefaultErrorStrategy());
    return parser;
}

From source file:io.prestosql.sql.parser.SqlParser.java

License:Apache License

private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction,
        ParsingOptions parsingOptions) {
    try {/*from   w w w  . j  a v a 2 s  . co  m*/
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);

        // 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())));

        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.reset(); // 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)");
    }
}

From source file:org.apache.olingo.server.core.uri.testutil.ParserWithLogging.java

License:Apache License

@Override
protected void addStage2ErrorStategy(final UriParserParser parser) {
    // Don't throw an at first syntax error, so the error listener will be called
    parser.setErrorHandler(new DefaultErrorStrategy());
}

From source file:org.apache.sysml.parser.dml.DMLParserWrapper.java

License:Apache License

/**
 * This function is supposed to be called directly only from DmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name/*ww  w .  j  a  v  a 2  s  .  com*/
 * @param dmlScript script file contents
 * @param sourceNamespace namespace from source statement
 * @param argVals script arguments
 * @return dml program, or null if at least one error
 * @throws ParseException if ParseException occurs
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace,
        Map<String, String> argVals) throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }

        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }

    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();

    try {
        DmlLexer lexer = new DmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DmlParser antlr4Parser = new DmlParser(tokens);

        boolean tryOptimizedParsing = false; // For now no optimization, since it is not able to parse integer value. 

        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
                // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);

            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }

    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting 
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    DmlPreprocessor prep = new DmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    DmlSyntacticValidator validator = new DmlSyntacticValidator(errorListener, argVals, sourceNamespace,
            prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);

    return dmlPgm;
}

From source file:org.apache.sysml.parser.pydml.PyDMLParserWrapper.java

License:Apache License

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name//from   w w w.j a  va 2 s  .  c o m
 * @param dmlScript script file contents
 * @param sourceNamespace namespace from source statement
 * @param argVals script arguments
 * @return dml program, or null if at least one error
 * @throws ParseException if ParseException occurs
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace,
        Map<String, String> argVals) throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }

        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }

    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();

    try {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(tokens);

        boolean tryOptimizedParsing = false; // For now no optimization, since it is not able to parse integer value. 

        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
                // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);

            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }

    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting 
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    PydmlPreprocessor prep = new PydmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    PydmlSyntacticValidator validator = new PydmlSyntacticValidator(errorListener, argVals, sourceNamespace,
            prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);

    return dmlPgm;
}

From source file:org.apache.sysml.parser.python.PyDMLParserWrapper.java

License:Apache License

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
 * @param fileName/*  ww  w  .  ja  v  a 2 s.  co  m*/
 * @return null if atleast one error
 */
public DMLProgram doParse(String fileName, String dmlScript, HashMap<String, String> argVals)
        throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = DMLParserWrapper.readDMLScript(fileName);
        }

        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("ERROR: Cannot find file:" + fileName, e);
    } catch (IOException e) {
        throw new ParseException("ERROR: Cannot open file:" + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException("ERROR: " + e.getMessage(), e);
    }

    PmlprogramContext ast = null;
    CustomDmlErrorListener errorListener = new CustomDmlErrorListener();

    try {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(tokens);

        boolean tryOptimizedParsing = false; // For now no optimization, since it is not able to parse integer value. 

        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.pmlprogram();
                // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.pushCurrentFileName(fileName);
                } else {
                    errorListener.pushCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.pmlprogram();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.pushCurrentFileName(fileName);

            // Now do the parsing
            ast = antlr4Parser.pmlprogram();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }

    try {
        // Now convert the parse tree into DMLProgram
        // Do syntactic validation while converting 
        ParseTree tree = ast;
        // And also do syntactic validation
        ParseTreeWalker walker = new ParseTreeWalker();
        PydmlSyntacticValidatorHelper helper = new PydmlSyntacticValidatorHelper(errorListener);
        PydmlSyntacticValidator validator = new PydmlSyntacticValidator(helper, fileName, argVals);
        walker.walk(validator, tree);
        errorListener.popFileName();
        if (errorListener.isAtleastOneError()) {
            return null;
        }
        dmlPgm = createDMLProgram(ast);
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot translate the parse tree into DMLProgram" + e.getMessage(), e);
    }

    return dmlPgm;
}

From source file:org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser.java

License:Open Source License

/**
 * Parses the given rule source and optionally generates a Java class for it if the classloader is not null.
 *
 * @param id the id of the rule, necessary to generate code
 * @param rule rule source code//from   w w  w.j  a  va2 s . c  o  m
 * @param silent don't emit status messages during parsing
 * @param ruleClassLoader the classloader to load the generated code into (can be null)
 * @return the parse rule
 * @throws ParseException if a one or more parse errors occur
 */
public Rule parseRule(String id, String rule, boolean silent, PipelineClassloader ruleClassLoader)
        throws ParseException {
    final ParseContext parseContext = new ParseContext(silent);
    final SyntaxErrorListener errorListener = new SyntaxErrorListener(parseContext);

    final RuleLangLexer lexer = new RuleLangLexer(new ANTLRInputStream(rule));
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);

    final RuleLangParser parser = new RuleLangParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new DefaultErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    final RuleLangParser.RuleDeclarationContext ruleDeclaration = parser.ruleDeclaration();

    // parsing stages:
    // 1. build AST nodes, checks for invalid var, function refs
    // 2. type annotator: infer type information from var refs, func refs
    // 3. checker: static type check w/ coercion nodes
    // 4. optimizer: TODO

    WALKER.walk(new RuleAstBuilder(parseContext), ruleDeclaration);
    WALKER.walk(new RuleTypeAnnotator(parseContext), ruleDeclaration);
    WALKER.walk(new RuleTypeChecker(parseContext), ruleDeclaration);

    if (parseContext.getErrors().isEmpty()) {
        Rule parsedRule = parseContext.getRules().get(0).withId(id);
        if (ruleClassLoader != null && ConfigurationStateUpdater.isAllowCodeGeneration()) {
            try {
                final Class<? extends GeneratedRule> generatedClass = codeGenerator
                        .generateCompiledRule(parsedRule, ruleClassLoader);
                if (generatedClass != null) {
                    parsedRule = parsedRule.toBuilder().generatedRuleClass(generatedClass).build();
                }
            } catch (Exception e) {
                log.warn("Unable to compile rule {} to native code, falling back to interpreting it: {}",
                        parsedRule.name(), e.getMessage());
            }
        }
        return parsedRule;
    }
    throw new ParseException(parseContext.getErrors());
}

From source file:org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser.java

License:Open Source License

public List<Pipeline> parsePipelines(String pipelines) throws ParseException {
    final ParseContext parseContext = new ParseContext(false);
    final SyntaxErrorListener errorListener = new SyntaxErrorListener(parseContext);

    final RuleLangLexer lexer = new RuleLangLexer(new ANTLRInputStream(pipelines));
    lexer.removeErrorListeners();/*  w  w w. jav  a  2 s . co  m*/
    lexer.addErrorListener(errorListener);

    final RuleLangParser parser = new RuleLangParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new DefaultErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    final RuleLangParser.PipelineDeclsContext pipelineDeclsContext = parser.pipelineDecls();

    WALKER.walk(new PipelineAstBuilder(parseContext), pipelineDeclsContext);

    if (parseContext.getErrors().isEmpty()) {
        return parseContext.pipelines;
    }
    throw new ParseException(parseContext.getErrors());
}

From source file:org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser.java

License:Open Source License

public Pipeline parsePipeline(String id, String source) {
    final ParseContext parseContext = new ParseContext(false);
    final SyntaxErrorListener errorListener = new SyntaxErrorListener(parseContext);

    final RuleLangLexer lexer = new RuleLangLexer(new ANTLRInputStream(source));
    lexer.removeErrorListeners();// w ww .j  a  v a2 s  .c  om
    lexer.addErrorListener(errorListener);

    final RuleLangParser parser = new RuleLangParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new DefaultErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    final RuleLangParser.PipelineContext pipelineContext = parser.pipeline();

    WALKER.walk(new PipelineAstBuilder(parseContext), pipelineContext);

    if (parseContext.getErrors().isEmpty()) {
        final Pipeline pipeline = parseContext.pipelines.get(0);
        return pipeline.withId(id);
    }
    throw new ParseException(parseContext.getErrors());
}