Merge pull request #23 from parrt/master

forgot to add dispatch methods in context objects.  in-parse listeners should work now.
This commit is contained in:
Terence Parr 2012-02-19 11:26:02 -08:00
commit f53b9820ba
5 changed files with 96 additions and 12 deletions

View File

@ -155,13 +155,63 @@ public class TestA {
printMethodName(ctx);
}
public void printMethodName(ParserRuleContext ctx) {
Throwable t = new Throwable();
StackTraceElement[] stack = t.getStackTrace();
String m = stack[1].getMethodName();
System.out.println(m+"("+ctx.getClass().getSimpleName()+")");
}
public static class TraceDuringParse extends ABaseParseListener {
@Override
public void enterNonLRRule(ParserRuleContext<Token> ctx) {
printMethodName(ctx);
}
@Override
public void enterS(ParserRuleContext<Token> ctx) {
printMethodName(ctx);
}
@Override
public void exitAdd(AParser.AddContext ctx) {
printMethodName(ctx);
}
@Override
public void exitEveryRule(ParserRuleContext<Token> ctx) {
printMethodName(ctx);
}
@Override
public void exitMult(AParser.MultContext ctx) {
printMethodName(ctx);
}
@Override
public void exitParens(AParser.ParensContext ctx) {
printMethodName(ctx);
}
@Override
public void exitPrimary(AParser.PrimaryContext ctx) {
printMethodName(ctx);
}
@Override
public void exitS(AParser.SContext ctx) {
printMethodName(ctx);
}
@Override
public void visitTerminal(ParserRuleContext<Token> ctx, Token symbol) {
printMethodName(ctx);
System.out.println("visiting "+symbol);
}
}
public static void printMethodName(ParserRuleContext ctx) {
Throwable t = new Throwable();
StackTraceElement[] stack = t.getStackTrace();
String m = stack[1].getMethodName();
System.out.println(m+"("+ctx.getClass().getSimpleName()+")");
}
public static void main(String[] args) throws Exception {
InputStream is = System.in;
if ( args.length>0 && args[0]!=null ) {
@ -171,7 +221,7 @@ public class TestA {
CommonTokenStream tokens = new CommonTokenStream(lexer);
AParser p = new AParser(tokens);
p.setBuildParseTree(true);
// p.addParseListener(new Tracer());
p.addParseListener(new TraceDuringParse());
ParserRuleContext<Token> t = p.s();
System.out.println("tree = "+t.toStringTree(p));

View File

@ -63,7 +63,7 @@ import org.antlr.v4.runtime.*;
public interface <file.grammarName>ParseListener extends ParseListener\<<InputSymbolType()>\> {
<file.listenerEnterNames:{lname |
void enter<lname; format="cap">(ParseListener\<<InputSymbolType()>\> ctx);}; separator="\n">
void enter<lname; format="cap">(ParserRuleContext\<<InputSymbolType()>\> ctx);}; separator="\n">
<file.listenerExitNames:{lname |
void exit<lname; format="cap">(<file.parserName>.<lname; format="cap">Context ctx);}; separator="\n">
}
@ -76,7 +76,7 @@ import org.antlr.v4.runtime.*;
public class <file.grammarName>BaseParseListener implements <file.grammarName>ParseListener {
<file.listenerEnterNames:{lname |
public void enter<lname; format="cap">(ParseListener\<<InputSymbolType()>\> ctx) { \}}; separator="\n">
public void enter<lname; format="cap">(ParserRuleContext\<<InputSymbolType()>\> ctx) { \}}; separator="\n">
<file.listenerExitNames:{lname |
public void exit<lname; format="cap">(<file.parserName>.<lname; format="cap">Context ctx) { \}}; separator="\n">
@ -629,6 +629,12 @@ public \<T> T accept(ParseTreeVisitor\<? extends T> visitor) {
}
>>
ParseListenerDispatchMethod(method) ::= <<
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseListener\<<InputSymbolType()>\> listener) {
if ( listener instanceof <parser.grammarName>ParseListener ) ((<parser.grammarName>ParseListener)listener).<if(method.isEnter)>enter<else>exit<endif><struct.derivedFromName; format="cap">(this);
}
>>
AttributeDecl(d) ::= "public <d.decl>;"
/** If we don't know location of label def x, use this template */

View File

@ -0,0 +1,13 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
public class ParseListenerDispatchMethod extends DispatchMethod {
public boolean isEnter;
public ParseListenerDispatchMethod(OutputModelFactory factory, boolean isEnter) {
super(factory);
this.isEnter = isEnter;
}
}

View File

@ -31,7 +31,7 @@ package org.antlr.v4.codegen.model.decl;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.*;
import java.util.ArrayList;
@ -44,18 +44,27 @@ public class AltLabelStructDecl extends StructDecl {
super(factory, r);
this.altNum = altNum;
this.name = // override name set in super to the label ctx
factory.getGenerator().target.getAltLabelContextStructName(label);
factory.getGenerator().target.getAltLabelContextStructName(label);
derivedFromName = label;
}
@Override
public void addDispatchMethods(Rule r) {
dispatchMethods = new ArrayList<DispatchMethod>();
dispatchMethods.add(new ListenerDispatchMethod(factory, true));
dispatchMethods.add(new ListenerDispatchMethod(factory, false));
if ( factory.getGrammar().tool.gen_listener ) {
dispatchMethods.add(new ListenerDispatchMethod(factory, true));
dispatchMethods.add(new ListenerDispatchMethod(factory, false));
}
if ( factory.getGrammar().tool.gen_visitor ) {
dispatchMethods.add(new VisitorDispatchMethod(factory));
}
if ( factory.getGrammar().tool.gen_parse_listener ) {
if ( !(r instanceof LeftRecursiveRule) ) {
dispatchMethods.add(new ParseListenerDispatchMethod(factory, true));
}
dispatchMethods.add(new ParseListenerDispatchMethod(factory, false));
}
}
@Override

View File

@ -65,6 +65,12 @@ public class StructDecl extends Decl {
if ( factory.getGrammar().tool.gen_visitor ) {
dispatchMethods.add(new VisitorDispatchMethod(factory));
}
if ( factory.getGrammar().tool.gen_parse_listener ) {
if ( !(r instanceof LeftRecursiveRule) ) {
dispatchMethods.add(new ParseListenerDispatchMethod(factory, true));
}
dispatchMethods.add(new ParseListenerDispatchMethod(factory, false));
}
}
}