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 6a624fccb..349984cbf 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 @@ -76,12 +76,22 @@ import org.antlr.v4.runtime.tree.ParseTreeListener; public interface Listener extends ParseTreeListener { + * Enter a parse tree produced by the {@code \} + * labeled alternative in {@link #\}. + * Enter a parse tree produced by {@link #\}. + * @param ctx the parse tree */ void enter(@NotNull .Context ctx); /** + + * Exit a parse tree produced by the {@code \} + * labeled alternative in {@link #\}. + * Exit a parse tree produced by {@link #\}. + * @param ctx the parse tree */ void exit(@NotNull .Context ctx);}; separator="\n"> @@ -166,7 +176,12 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor; public interface Visitor\ extends ParseTreeVisitor\ { + * Visit a parse tree produced by the {@code \} + * labeled alternative in {@link #\}. + * Visit a parse tree produced by {@link #\}. + * @param ctx the parse tree * @return the visitor result */ diff --git a/tool/src/org/antlr/v4/codegen/model/ListenerFile.java b/tool/src/org/antlr/v4/codegen/model/ListenerFile.java index c66c4b67f..8db1d18c3 100644 --- a/tool/src/org/antlr/v4/codegen/model/ListenerFile.java +++ b/tool/src/org/antlr/v4/codegen/model/ListenerFile.java @@ -36,6 +36,7 @@ import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.ActionAST; import org.antlr.v4.tool.ast.AltAST; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -48,7 +49,16 @@ public class ListenerFile extends OutputFile { public String genPackage; // from -package cmd-line public String grammarName; public String parserName; + /** + * The names of all listener contexts. + */ public Set listenerNames = new HashSet(); + /** + * For listener contexts created for a labeled outer alternative, maps from + * a listener context name to the name of the rule which defines the + * context. + */ + public Map listenerLabelRuleNames = new HashMap(); @ModelElement public Action header; @@ -62,6 +72,7 @@ public class ListenerFile extends OutputFile { if ( labels!=null ) { for (Map.Entry>> pair : labels.entrySet()) { listenerNames.add(pair.getKey()); + listenerLabelRuleNames.put(pair.getKey(), r.name); } } else { diff --git a/tool/src/org/antlr/v4/codegen/model/VisitorFile.java b/tool/src/org/antlr/v4/codegen/model/VisitorFile.java index dc8da94ac..cd1732730 100644 --- a/tool/src/org/antlr/v4/codegen/model/VisitorFile.java +++ b/tool/src/org/antlr/v4/codegen/model/VisitorFile.java @@ -36,6 +36,7 @@ import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.ActionAST; import org.antlr.v4.tool.ast.AltAST; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -45,7 +46,16 @@ public class VisitorFile extends OutputFile { public String genPackage; // from -package cmd-line public String grammarName; public String parserName; + /** + * The names of all rule contexts which may need to be visited. + */ public Set visitorNames = new HashSet(); + /** + * For rule contexts created for a labeled outer alternative, maps from + * a listener context name to the name of the rule which defines the + * context. + */ + public Map visitorLabelRuleNames = new HashMap(); @ModelElement public Action header; @@ -59,6 +69,7 @@ public class VisitorFile extends OutputFile { if ( labels!=null ) { for (Map.Entry>> pair : labels.entrySet()) { visitorNames.add(pair.getKey()); + visitorLabelRuleNames.put(pair.getKey(), r.name); } } else {