Example usage for org.antlr.v4 Tool process

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

Introduction

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

Prototype

public void process(Grammar g, boolean gencode) 

Source Link

Document

To process a grammar, we load all of its imported grammars into subordinate grammar objects.

Usage

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

License:Apache License

private static Grammar createGrammar(String resource, org.opencypher.grammar.Grammar.ParserOption... options) {
    // We need to do some custom post-processing to get the lexer rules right
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String grammarString = null;// w  ww. j av a2  s.  c  o  m
    try {
        Antlr4.write(Fixture.grammarResource(Antlr4.class, resource, options), out);
        grammarString = out.toString(UTF_8.name());
        System.out.println(grammarString);
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected error while writing antlr grammar");
    }

    org.antlr.v4.Tool tool = new org.antlr.v4.Tool();

    GrammarRootAST ast = tool.parseGrammarFromString(grammarString);
    Grammar grammar = tool.createGrammar(ast);
    tool.process(grammar, false);
    return grammar;
}

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 {//  w w  w  .j  a  v a 2 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");
    }
}