Merge pull request #279 from sharwell/fix-270

Support list labels on a set of tokens (fixes #270)
This commit is contained in:
Sam Harwell 2013-06-08 10:03:11 -07:00
commit 6bf738cabd
2 changed files with 29 additions and 4 deletions

View File

@ -181,12 +181,16 @@ public class ParserFactory extends DefaultOutputModelFactory {
else matchOp = new MatchSet(this, setAST);
if ( labelAST!=null ) {
String label = labelAST.getText();
Decl d = getTokenLabelDecl(label);
matchOp.labels.add(d);
getCurrentRuleFunction().addContextDecl(setAST.getAltLabel(), d);
RuleFunction rf = getCurrentRuleFunction();
if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
defineImplicitLabel(setAST, matchOp);
TokenListDecl l = getTokenListLabelDecl(label);
getCurrentRuleFunction().addContextDecl(setAST.getAltLabel(), l);
rf.addContextDecl(setAST.getAltLabel(), l);
}
else {
Decl d = getTokenLabelDecl(label);
matchOp.labels.add(d);
rf.addContextDecl(setAST.getAltLabel(), d);
}
}
if ( controller.needsImplicitLabel(setAST, matchOp) ) defineImplicitLabel(setAST, matchOp);

View File

@ -82,6 +82,27 @@ public class TestParserExec extends BaseTest {
assertEquals(null, stderrDuringParse);
}
/**
* This is a regression test for #270 "Fix operator += applied to a set of
* tokens".
* https://github.com/antlr/antlr4/issues/270
*/
@Test public void testListLabelOnSet() {
String grammar =
"grammar T;\n" +
"a : b b* ';' ;\n" +
"b : ID val+=(INT | FLOAT)*;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+;\n" +
"FLOAT : [0-9]+ '.' [0-9]+;\n" +
"WS : (' '|'\\n') -> skip ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a",
"abc 34;", false);
assertEquals("", found);
assertEquals(null, stderrDuringParse);
}
@Test public void testBasic() throws Exception {
String grammar =
"grammar T;\n" +