Permalink
Browse files

Merge branch 'fix-195'

  • Loading branch information...
2 parents 2242948 + 961f68c commit 0e5cf32bf48157ec9faedda0337053c0ffa9a622 @sharwell sharwell committed Mar 27, 2013
@@ -147,6 +147,10 @@ public void checkForLabelConflicts(Collection<Rule> rules) {
Map<String, LabelElementPair> labelNameSpace =
new HashMap<String, LabelElementPair>();
for (int i=1; i<=r.numberOfAlts; i++) {
+ if (r.hasAltSpecificContexts()) {
+ labelNameSpace.clear();
+ }
+
Alternative a = r.alt[i];
for (List<LabelElementPair> pairs : a.labelDefs.values() ) {
for (LabelElementPair p : pairs) {
@@ -307,4 +307,25 @@ public void testPredicatedIfIfElse() throws Exception {
assertNull(this.stderrDuringParse);
}
+ /**
+ * This is a regression test for antlr/antlr4#195 "label 'label' type
+ * mismatch with previous definition: TOKEN_LABEL!=RULE_LABEL"
+ * https://github.com/antlr/antlr4/issues/195
+ */
+ @Test public void testLabelAliasingAcrossLabeledAlternatives() throws Exception {
+ String grammar =
+ "grammar T;\n" +
+ "start : a* EOF;\n" +
+ "a\n" +
+ " : label=subrule {System.out.println($label.text);} #One\n" +
+ " | label='y' {System.out.println($label.text);} #Two\n" +
+ " ;\n" +
+ "subrule : 'x';\n" +
+ "WS : (' '|'\\n') -> skip ;\n";
+
+ String found = execParser("T.g4", grammar, "TParser", "TLexer", "start",
+ "xy", false);
+ assertEquals("x\ny\n", found);
+ }
+
}

0 comments on commit 0e5cf32

Please sign in to comment.