Example usage for org.antlr.v4 Tool Tool

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

Introduction

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

Prototype

public Tool() 

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 w w.  j av  a 2 s . c o  m*/
        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");
    }
}