Example usage for org.antlr.v4.runtime Recognizer EOF

List of usage examples for org.antlr.v4.runtime Recognizer EOF

Introduction

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

Prototype

int EOF

To view the source code for org.antlr.v4.runtime Recognizer EOF.

Click Source Link

Usage

From source file:com.sri.ai.praise.demo.HOGMPanel.java

License:Open Source License

/**
 * Checks to ensure the passed in string is not whitespace or comments only.
 * @param string//from  ww w.  j  a v  a2 s  .  co  m
 * @return
 */
private boolean containsRules(String string) {
    boolean result = true;

    ANTLRInputStream input = new ANTLRInputStream(string.trim());
    RuleLexer lexer = new RuleLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    try {
        Token token = tokens.LT(1);
        if (token.getType() == Recognizer.EOF) {
            result = false;
        }
    } catch (RuntimeException ex) {
        // This is another problem, i.e. invalid token, so will let follow
        // on logic handle this when it tries to parse.
    }

    return result;
}

From source file:com.sri.ai.praise.model.imports.church.TranslateChurchToModel.java

License:Open Source License

public Triple<String, Model, List<Expression>> translate(String churchProgramName, String churchProgram) {
    Triple<String, Model, List<Expression>> result = null;
    try {/*  w w  w.  ja va2 s  .  c  o m*/
        ErrorListener lexerErrorListener = new ErrorListener("Lexer Error");
        ErrorListener parseErrorListener = new ErrorListener("Parse Error");

        ANTLRInputStream input = new ANTLRInputStream(churchProgram);
        ChurchLexer lexer = new ChurchLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ChurchParser parser = new ChurchParser(tokens);

        lexer.removeErrorListeners();
        parser.removeErrorListeners();
        lexer.addErrorListener(lexerErrorListener);
        parser.addErrorListener(parseErrorListener);

        ParseTree tree = parser.parse();

        boolean eof = parser.getInputStream().LA(1) == Recognizer.EOF;

        if (!lexerErrorListener.errorsDetected && !parseErrorListener.errorsDetected) {
            if (!eof) {
                System.err.println("Unable to parse the complete input model: " + input);
            } else {
                lexer.removeErrorListeners();
                parser.removeErrorListeners();
                ChurchToModelVisitor churchToModelVisitor = new ChurchToModelVisitor();
                churchToModelVisitor.setChurchProgramInformation(churchProgramName, churchProgram);
                Expression hogmAndModelAndQueriesTuple = churchToModelVisitor.visit(tree);

                result = new Triple<String, Model, List<Expression>>(
                        Tuple.get(hogmAndModelAndQueriesTuple, 0).getValue().toString(),
                        new Model(Tuple.get(hogmAndModelAndQueriesTuple, 1), Collections.<String>emptySet()),
                        ExtensionalSet.getElements(Tuple.get(hogmAndModelAndQueriesTuple, 2)));
            }
        } else {
            if (lexerErrorListener.errorsDetected) {
                throw new RuntimeException(
                        lexerErrorListener.name + ":\n" + lexerErrorListener.errorMsgs.toString());
            }
            if (parseErrorListener.errorsDetected) {
                throw new RuntimeException(
                        parseErrorListener.name + ":\n" + parseErrorListener.errorMsgs.toString());
            }
        }
    } catch (RecognitionException re) {
        re.printStackTrace();
        throw re;
    } catch (ModelException me) {
        System.err.println("Model Errors");
        for (ModelError error : me.getErrors()) {
            System.err.println(error);
        }
        me.printStackTrace();
        throw me;
    } catch (RuntimeException re) {
        re.printStackTrace();
        throw re;
    }

    return result;
}

From source file:de.huberlin.wbi.cuneiform.core.repl.QueryParseCtlLexer.java

License:Apache License

public boolean isReady() {

    int depth;/*from   w  w w .j a v a2s  .c  o  m*/
    Token t;
    boolean ready;

    reset();

    depth = 0;
    ready = true;
    while ((t = nextToken()).getType() != Recognizer.EOF) {

        switch (t.getType()) {

        case ParseCtlLexer.LXCOMMENT:
        case ParseCtlLexer.LCCOMMENT:
        case ParseCtlLexer.LBRACE:
            depth++;
            break;

        case ParseCtlLexer.RXCOMMENT:
        case ParseCtlLexer.RCCOMMENT:
        case ParseCtlLexer.RBRACE:
            depth--;
            if (depth == 0)
                ready = true;
            break;

        case ParseCtlLexer.RMMECB:
            if (depth == 0)
                ready = true;
            break;
        case ParseCtlLexer.ANY:
            if (depth == 0)
                ready = false;
            break;
        case ParseCtlLexer.SEMICOLON:
            if (depth == 0)
                ready = true;
            break;
        default:
            break; // ignore
        }
    }

    if (depth != 0)
        return false;

    return ready;
}