From 1cc7ac3410dbd959f7cd03a660b5cf3bdb532f0a Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Mon, 30 Jan 2012 16:51:55 -0800 Subject: [PATCH] added -trace to TestRig. fixed code gen issue for trace listener --- .../src/org/antlr/v4/runtime/misc/TestRig.java | 16 ++++++++++++++-- .../v4/tool/templates/codegen/Java/Java.stg | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java index c5c747789..829d2510e 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java @@ -41,7 +41,11 @@ import java.lang.reflect.Method; /** Run a lexer/parser combo, optionally printing tree string or generating * postscript file. Optionally taking input file. * - * $ java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-tokens] [-gui] [-ps file.ps] [input-filename] + * $ java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName + * [-print] + * [-tokens] [-gui] [-ps file.ps] + * [-trace] + * [input-filename] */ public class TestRig { public static void main(String[] args) throws Exception { @@ -52,9 +56,12 @@ public class TestRig { boolean gui = false; String psFile = null; boolean showTokens = false; + boolean trace = false; String encoding = null; if ( args.length < 2 ) { - System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName [-print] [-tokens] [-gui] [-encoding encodingname] [-ps file.ps] [input-filename]"); + System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName" + + " [-print] [-tokens] [-gui] [-encoding encodingname]" + + " [-ps file.ps] [-trace] [input-filename]"); return; } int i=0; @@ -78,6 +85,9 @@ public class TestRig { if ( arg.equals("-tokens") ) { showTokens = true; } + else if ( arg.equals("-trace") ) { + trace = true; + } else if ( arg.equals("-encoding") ) { if ( i>=args.length ) { System.err.println("missing encoding on -encoding"); @@ -143,6 +153,8 @@ public class TestRig { parser.setBuildParseTree(true); } + parser.setTrace(trace); + Method startRule = parserClass.getMethod(startRuleName, (Class[])null); ParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null); diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 1246282a2..2be14e565 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -538,7 +538,7 @@ public static class Context extends Context { VisitorDispatchMethod(method) ::= << @Override public void enterexitRule(ParseTreeListener\<\> listener) { - if ( listener!=null ) ((Listener)listener).enterexitRule(this); + if ( listener!=null && listener instanceof Listener ) ((Listener)listener).enterexitRule(this); } >> @@ -547,7 +547,7 @@ AttributeDecl(d) ::= "" /** If we don't know location of label def x, use this template */ labelref(x) ::= "_localctx." -// used for left-recursive rules +// used for left-recursive rules recRuleDefArg() ::= "int _p" recRuleArg() ::= "$_p" recRuleAltPredicate(ruleName,opPrec) ::= " >= "