Fix incorrect reports of label type conflicts for labels aliased across separate *labeled* outer alternatives (fixes #195)

This commit is contained in:
Sam Harwell 2013-03-27 11:21:32 -05:00
parent 507f331bd0
commit 961f68c865
2 changed files with 25 additions and 0 deletions

View File

@ -147,6 +147,10 @@ public class SymbolChecks {
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) {

View File

@ -307,4 +307,25 @@ public class TestParserExec extends BaseTest {
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);
}
}