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

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

Introduction

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

Prototype

BailErrorStrategy

Source Link

Usage

From source file:ai.grakn.graql.internal.parser.QueryParserImpl.java

License:Open Source License

/**
 * @param reader a reader representing several queries
 * @return a list of queries// w  ww.  j  a  va2  s . c  o  m
 */
@Override
public <T extends Query<?>> Stream<T> parseList(Reader reader) {
    UnbufferedCharStream charStream = new UnbufferedCharStream(reader);
    GraqlErrorListener errorListener = GraqlErrorListener.withoutQueryString();
    GraqlLexer lexer = createLexer(charStream, errorListener);

    /*
    We tell the lexer to copy the text into each generated token.
    Normally when calling `Token#getText`, it will look into the underlying `TokenStream` and call
    `TokenStream#size` to check it is in-bounds. However, `UnbufferedTokenStream#size` is not supported
    (because then it would have to read the entire input). To avoid this issue, we set this flag which will
    copy over the text into each `Token`, s.t. that `Token#getText` will just look up the copied text field.
    */
    lexer.setTokenFactory(new CommonTokenFactory(true));

    // Use an unbuffered token stream so we can handle extremely large input strings
    UnbufferedTokenStream tokenStream = new UnbufferedTokenStream(ChannelTokenSource.of(lexer));

    GraqlParser parser = createParser(tokenStream, errorListener);

    /*
    The "bail" error strategy prevents us reading all the way to the end of the input, e.g.
            
    ```
    match $x isa person; insert $x has name "Bob"; match $x isa movie; get;
                                                   ^
    ```
            
    In this example, when ANTLR reaches the indicated `match`, it considers two possibilities:
            
    1. this is the end of the query
    2. the user has made a mistake. Maybe they accidentally pasted the `match` here.
            
    Because of case 2, ANTLR will parse beyond the `match` in order to produce a more helpful error message.
    This causes memory issues for very large queries, so we use the simpler "bail" strategy that will
    immediately stop when it hits `match`.
    */
    parser.setErrorHandler(new BailErrorStrategy());

    // This is a lazy iterator that will only consume a single query at a time, without parsing any further.
    // This means it can pass arbitrarily long streams of queries in constant memory!
    Iterable<T> queryIterator = () -> new AbstractIterator<T>() {
        @Nullable
        @Override
        protected T computeNext() {
            int latestToken = tokenStream.LA(1);
            if (latestToken == Token.EOF) {
                endOfData();
                return null;
            } else {
                // This will parse and consume a single query, even if it doesn't reach an EOF
                // When we next run it, it will start where it left off in the stream
                return (T) QUERY.parse(parser, errorListener);
            }
        }
    };

    return StreamSupport.stream(queryIterator.spliterator(), false);
}

From source file:com.antsdb.saltedfish.sql.mysql.ExprGenerator.java

License:Open Source License

public static Operator gen(GeneratorContext ctx, Planner cursorMeta, String expr) {
    CharStream cs = new ANTLRInputStream(expr);
    MysqlLexer lexer = new MysqlLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.setTokenSource(lexer);/*w  w w. j av  a  2  s.c o  m*/
    MysqlParser parser = new MysqlParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    MysqlParser.ExprContext rule = parser.expr();
    return gen(ctx, cursorMeta, rule);
}

From source file:com.antsdb.saltedfish.sql.mysql.MysqlParserFactory.java

License:Open Source License

static MysqlParser.ScriptContext parse(CharStream cs) {
    if (isCommtedStatement(cs)) {
        String s = cs.toString();
        s = s.substring(9);//from   w  w w.j ava2  s. c  o m
        s = s.substring(0, s.length() - 3);
        cs = new ANTLRInputStream(s);
    }
    MysqlLexer lexer = new MysqlLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.setTokenSource(lexer);
    MysqlParser parser = new MysqlParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    boolean success = false;
    try {
        MysqlParser.ScriptContext script = parser.script();
        success = true;
        return script;
    } finally {
        if (!success && (parser.lastStatement != null)) {
            _log.debug("last passed statement: {}", ((ParseTree) parser.lastStatement).getText());
        }
    }
}

From source file:com.boylesoftware.web.impl.routes.RoutesRouterConfiguration.java

License:Apache License

@Override
protected void buildRoutes(final ServletContext sc, final RoutesBuilder routes) throws UnavailableException {

    try (final InputStream in = sc.getResourceAsStream(ROUTES_PATH)) {

        if (in == null)
            throw new UnavailableException("No " + ROUTES_PATH + " found in the web-application.");

        final RoutesParser parser = new RoutesParser(
                new CommonTokenStream(new RoutesLexer(new ANTLRInputStream(in))));
        parser.setErrorHandler(new BailErrorStrategy());
        parser.setRoutesBuilder(routes);
        try {//w w w  .  j a v a  2 s.c  o  m
            parser.config();
        } catch (final Exception e) {
            this.error(e);
        }

    } catch (final IOException e) {
        this.error(e);
    }
}

From source file:com.github.gfx.android.orma.migration.sqliteparser.SQLiteParserUtils.java

License:Apache License

@NonNull
public static SQLiteParser createParser(@NonNull String sql) {
    CharStream source = new ANTLRInputStream(sql);
    Lexer lexer = new SQLiteLexer(source);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    SQLiteParser parser = new SQLiteParser(tokenStream);
    parser.setErrorHandler(new BailErrorStrategy());
    return parser;
}

From source file:com.github.sip.SipUtils.java

License:Apache License

private static SipParser getSipParser(String input) {

    SipErrorListener listener = new SipErrorListener();

    // Get our lexer
    SipLexer lexer = new SipLexer(new ANTLRInputStream(input));
    lexer.addErrorListener(listener);//from ww  w.  ja  v a2  s  . c  o m

    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Pass the tokens to the parser
    SipParser parser = new SipParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    parser.addErrorListener(listener);
    return parser;
}

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//from   w  w  w  .j a  va  2s .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.jaeksoft.searchlib.query.QueryParser.java

License:Open Source License

public final Query parse(String query) throws IOException {
    try {//from  w  w w.java2  s . c om
        currentOperator = -1;
        currentField = defaultField;
        holdQuery = null;
        booleanQuery = new BooleanQuery();
        ioError = null;
        ANTLRInputStream input = new ANTLRInputStream(query);
        BooleanQueryLexer lexer = new BooleanQueryLexer(input);
        ErrorListener errorListener = new ErrorListener();
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        BooleanQueryParser parser = new BooleanQueryParser(tokens);
        BailErrorStrategy errorHandler = new BailErrorStrategy();
        parser.setErrorHandler(errorHandler);
        parser.addParseListener(this);
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);
        parser.expression();
        if (ioError != null)
            throw ioError;
        if (holdQuery != null)
            addBooleanClause(holdQuery, currentOperator);
        return booleanQuery;
    } catch (org.antlr.v4.runtime.RecognitionException e) {
        if (ioError != null)
            throw ioError;
        throw new IOException(e);
    } catch (org.antlr.v4.runtime.misc.ParseCancellationException e) {
        if (ioError != null)
            throw ioError;
        throw new IOException(e);
    }
}

From source file:com.jmcalc.ExpressionParser.java

License:Apache License

protected static JMCalcParser produceParse(String expression) {
    JMCalcLexer lexer = new JMCalcLexer(new ANTLRInputStream(expression));
    lexer.removeErrorListeners();// w w  w.  j  ava  2  s.com
    lexer.addErrorListener(DescriptiveErrorListener.INSTANCE);
    JMCalcParser parser = new JMCalcParser(new CommonTokenStream(lexer));
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(DescriptiveErrorListener.INSTANCE);
    return parser;
}

From source file:com.liferay.dynamic.data.mapping.expression.internal.DDMExpressionImpl.java

License:Open Source License

protected ExpressionContext createExpressionContext() throws DDMExpressionException {

    try {/* w w  w .  j a  v a  2 s .  c  om*/
        CharStream charStream = new ANTLRInputStream(_expressionString);

        DDMExpressionLexer ddmExpressionLexer = new DDMExpressionLexer(charStream);

        DDMExpressionParser ddmExpressionParser = new DDMExpressionParser(
                new CommonTokenStream(ddmExpressionLexer));

        ddmExpressionParser.setErrorHandler(new BailErrorStrategy());

        return ddmExpressionParser.expression();
    } catch (Exception e) {
        throw new DDMExpressionException.InvalidSyntax(e);
    }
}