diff --git a/CHANGES.txt b/CHANGES.txt index bda424b8f..8e8edc4bc 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,7 @@ Jan 14, 2012 * labels on tokens in left-recursive rules caused codegen exception. * leave start/stop char index alone in CommonTokenFactory; refers to original text. +* reuse of -> label on multiple alts in rule caused dup ctx object defs. Jan 11, 2012 diff --git a/tool/src/org/antlr/v4/codegen/model/ListenerFile.java b/tool/src/org/antlr/v4/codegen/model/ListenerFile.java index 04abcc8e6..6081041ff 100644 --- a/tool/src/org/antlr/v4/codegen/model/ListenerFile.java +++ b/tool/src/org/antlr/v4/codegen/model/ListenerFile.java @@ -5,8 +5,9 @@ import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.ActionAST; -import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** A model object representing a parse tree listener file. * These are the rules specific events triggered by a parse tree visitor. @@ -14,7 +15,7 @@ import java.util.List; public class ListenerFile extends OutputFile { public String grammarName; public String parserName; - public List listenerNames = new ArrayList(); + public Set listenerNames = new HashSet(); // public List ruleNames = new ArrayList(); @ModelElement public Action header; diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index aa1986a08..f6154d1d6 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -49,7 +49,6 @@ public class RuleFunction extends OutputModelObject { public String ctxType; public Collection ruleLabels; public Collection tokenLabels; - public List elementsReferencedInRewrite; public List exceptions; public ATNState startState; public int index; @@ -59,7 +58,7 @@ public class RuleFunction extends OutputModelObject { @ModelElement public List code; @ModelElement public OrderedHashSet locals; // TODO: move into ctx? @ModelElement public StructDecl ruleCtx; - @ModelElement public List altLabelCtxs; + @ModelElement public Set altLabelCtxs; @ModelElement public Map namedActions; @ModelElement public Action finallyAction; @ModelElement public List postamble; @@ -80,7 +79,7 @@ public class RuleFunction extends OutputModelObject { List labels = r.getAltLabels(); if ( labels!=null ) { - altLabelCtxs = new ArrayList(); + altLabelCtxs = new HashSet(); for (String label : labels) { altLabelCtxs.add(new AltLabelStructDecl(factory, r, label)); } diff --git a/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java b/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java index ef8f21f24..0ccdbba80 100644 --- a/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java +++ b/tool/src/org/antlr/v4/codegen/model/decl/AltLabelStructDecl.java @@ -39,4 +39,19 @@ public class AltLabelStructDecl extends StructDecl { super(factory, r); this.label = label; } + + @Override + public int hashCode() { + return label.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) return true; + if ( obj.hashCode() != this.hashCode() ) return false; + if ( obj instanceof AltLabelStructDecl ) { + return label.equals(((AltLabelStructDecl)obj).label); + } + return false; + } }