Example usage for org.antlr.v4 Tool parse

List of usage examples for org.antlr.v4 Tool parse

Introduction

In this page you can find the example usage for org.antlr.v4 Tool parse.

Prototype

public GrammarRootAST parse(String fileName, CharStream in) 

Source Link

Usage

From source file:org.opencypher.tools.grammar.Antlr4ToolFacade.java

License:Apache License

public static void assertGeneratesValidParser(String resource) throws Exception {
    Output.Readable buffer = stringBuilder();
    Tool antlr = new Tool();
    Antlr4ToolFacade facade = new Antlr4ToolFacade(antlr, buffer);
    try {//from  w  ww . ja va2 s .  c  om
        Antlr4.write(Fixture.grammarResource(Antlr4.class, resource), buffer);
    } catch (Throwable e) {
        try {
            facade.reportFailureIn("generating grammar");
        } catch (AssertionError x) {
            throw e;
        }
    }
    antlr.addListener(facade);
    GrammarRootAST ast = antlr.parse(resource, new ANTLRReaderStream(buffer.reader()));
    if (ast.hasErrors) {
        RuleAST lastGood = lastGoodRule(ast);
        if (lastGood == null) {
            facade.reportFailureIn("parsing grammar");
        } else {
            facade.reportFailureIn(
                    "parsing grammar, after " + lastGood.getRuleName() + " on line " + lastGood.getLine());
        }
    }
    antlr.process(antlr.createGrammar(ast), false);
    if (facade.hasErrors()) {
        facade.reportFailureIn("processing grammar");
    }
}