Search in sources :

Example 26 with BailErrorStrategy

use of org.antlr.v4.runtime.BailErrorStrategy in project systemml by apache.

the class PyDMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name
 * @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
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) {
    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);
        // For now no optimization, since it is not able to parse integer value.
        boolean tryOptimizedParsing = false;
        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;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) LanguageException(org.apache.sysml.parser.LanguageException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ProgramrootContext(org.apache.sysml.parser.pydml.PydmlParser.ProgramrootContext) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.sysml.parser.ParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 27 with BailErrorStrategy

use of org.antlr.v4.runtime.BailErrorStrategy in project systemml by apache.

the class DMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from DmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name
 * @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
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) {
    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);
        // For now no optimization, since it is not able to parse integer value.
        boolean tryOptimizedParsing = false;
        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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) FileNotFoundException(java.io.FileNotFoundException) ProgramrootContext(org.apache.sysml.parser.dml.DmlParser.ProgramrootContext) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.sysml.parser.ParseException) LanguageException(org.apache.sysml.parser.LanguageException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 28 with BailErrorStrategy

use of org.antlr.v4.runtime.BailErrorStrategy in project hibernate-orm by hibernate.

the class StandardHqlTranslator method parseHql.

private HqlParser.StatementContext parseHql(String hql) {
    // Build the lexer
    final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer(hql);
    // Build the parse tree
    final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser(hql, hqlLexer);
    // try to use SLL(k)-based parsing first - its faster
    hqlLexer.addErrorListener(ERR_LISTENER);
    hqlParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    hqlParser.removeErrorListeners();
    hqlParser.addErrorListener(ERR_LISTENER);
    hqlParser.setErrorHandler(new BailErrorStrategy());
    try {
        return hqlParser.statement();
    } catch (ParseCancellationException e) {
        // reset the input token stream and parser state
        hqlLexer.reset();
        hqlParser.reset();
        // fall back to LL(k)-based parsing
        hqlParser.getInterpreter().setPredictionMode(PredictionMode.LL);
        hqlParser.setErrorHandler(new DefaultErrorStrategy());
        return hqlParser.statement();
    } catch (ParsingException ex) {
        throw new SemanticException("A query exception occurred", hql, ex);
    }
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) HqlLexer(org.hibernate.grammars.hql.HqlLexer) HqlParser(org.hibernate.grammars.hql.HqlParser) ParsingException(org.hibernate.query.sqm.ParsingException) SemanticException(org.hibernate.query.SemanticException)

Example 29 with BailErrorStrategy

use of org.antlr.v4.runtime.BailErrorStrategy in project hibernate-orm by hibernate.

the class MultiLineSqlScriptExtractor method buildScriptParseTree.

private static SqlScriptParser.ScriptContext buildScriptParseTree(SqlScriptLexer lexer, SqlScriptParser parser) {
    // try to use SLL(k)-based parsing first - its faster
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());
    parser.addErrorListener(new VerboseListener());
    try {
        return parser.script();
    } catch (ParseCancellationException e) {
        // reset the input token stream and parser state
        lexer.reset();
        parser.reset();
        // fall back to LL(k)-based parsing
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        parser.addErrorListener(ConsoleErrorListener.INSTANCE);
        parser.setErrorHandler(new DefaultErrorStrategy());
        return parser.script();
    }
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy)

Example 30 with BailErrorStrategy

use of org.antlr.v4.runtime.BailErrorStrategy in project antlr4 by tunnelvisionlabs.

the class GrammarParserInterpreter method deriveTempParserInterpreter.

/**
 * Derive a new parser from an old one that has knowledge of the grammar.
 *  The Grammar object is used to correctly compute outer alternative
 *  numbers for parse tree nodes. A parser of the same type is created
 *  for subclasses of {@link ParserInterpreter}.
 */
public static ParserInterpreter deriveTempParserInterpreter(Grammar g, Parser originalParser, TokenStream tokens) {
    ParserInterpreter parser;
    if (originalParser instanceof ParserInterpreter) {
        Class<? extends ParserInterpreter> c = originalParser.getClass().asSubclass(ParserInterpreter.class);
        try {
            Constructor<? extends ParserInterpreter> ctor = c.getConstructor(Grammar.class, ATN.class, TokenStream.class);
            parser = ctor.newInstance(g, originalParser.getATN(), originalParser.getInputStream());
        } catch (Exception e) {
            throw new IllegalArgumentException("can't create parser to match incoming " + originalParser.getClass().getSimpleName(), e);
        }
    } else {
        // must've been a generated parser
        char[] serializedAtn = ATNSerializer.getSerializedAsChars(originalParser.getATN(), Arrays.asList(originalParser.getRuleNames()));
        ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
        parser = new ParserInterpreter(originalParser.getGrammarFileName(), originalParser.getVocabulary(), Arrays.asList(originalParser.getRuleNames()), deserialized, tokens);
    }
    parser.setInputStream(tokens);
    // Make sure that we don't get any error messages from using this temporary parser
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.removeParseListeners();
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    return parser;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ATN(org.antlr.v4.runtime.atn.ATN) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)23 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)19 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)16 ParseTree (org.antlr.v4.runtime.tree.ParseTree)13 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)12 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)9 RecognitionException (org.antlr.v4.runtime.RecognitionException)8 IOException (java.io.IOException)7 CharStream (org.antlr.v4.runtime.CharStream)6 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)6 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 InputStream (java.io.InputStream)4 ATN (org.antlr.v4.runtime.atn.ATN)4 ASPCore2Lexer (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer)3 ASPCore2Parser (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)3 DMLProgram (org.apache.sysml.parser.DMLProgram)3 LanguageException (org.apache.sysml.parser.LanguageException)3 ParseException (org.apache.sysml.parser.ParseException)3