String lexerName,
String parserStartRuleName,
boolean debug,
boolean profile)
{
ST outputFileST = new ST(
"import org.antlr.v4.runtime.*;\n" +
"import org.antlr.v4.runtime.tree.*;\n" +
"import org.antlr.v4.runtime.atn.*;\n" +
"import java.util.Arrays;\n"+
"\n" +
"public class Test {\n" +
" public static void main(String[] args) throws Exception {\n" +
" CharStream input = new ANTLRFileStream(args[0]);\n" +
" <lexerName> lex = new <lexerName>(input);\n" +
" CommonTokenStream tokens = new CommonTokenStream(lex);\n" +
" <createParser>\n"+
" parser.setBuildParseTree(true);\n" +
" <profile>\n"+
" ParserRuleContext tree = parser.<parserStartRuleName>();\n" +
" <if(profile)>System.out.println(Arrays.toString(profiler.getDecisionInfo()));<endif>\n" +
" ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);\n" +
" }\n" +
"\n" +
" static class TreeShapeListener implements ParseTreeListener {\n" +
" @Override public void visitTerminal(TerminalNode node) { }\n" +
" @Override public void visitErrorNode(ErrorNode node) { }\n" +
" @Override public void exitEveryRule(ParserRuleContext ctx) { }\n" +
"\n" +
" @Override\n" +
" public void enterEveryRule(ParserRuleContext ctx) {\n" +
" for (int i = 0; i \\< ctx.getChildCount(); i++) {\n" +
" ParseTree parent = ctx.getChild(i).getParent();\n" +
" if (!(parent instanceof RuleNode) || ((RuleNode)parent).getRuleContext() != ctx) {\n" +
" throw new IllegalStateException(\"Invalid parse tree shape detected.\");\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}"
);
ST createParserST = new ST(" <parserName> parser = new <parserName>(tokens);\n");
if ( debug ) {
createParserST =
new ST(
" <parserName> parser = new <parserName>(tokens);\n" +
" parser.addErrorListener(new DiagnosticErrorListener());\n");
}
if ( profile ) {
outputFileST.add("profile",