label+='foo' wasn't generating good code. It was generating token type as variable name. Now, I gen "s<ttype>" for implicit labels on string literals. Augmented unit test. Fixes antlr/antlr4#90

This commit is contained in:
Terence Parr 2012-12-01 16:26:06 -08:00
parent 84c34d2391
commit 35202df715
3 changed files with 10 additions and 3 deletions

View File

@ -11,6 +11,9 @@ December 1, 2012
some tests in TestSets appeared to allow ~('a'|B) but it was randomly working.
('a'|B) works, though doesn't collapse to a set.
* label+='foo' wasn't generating good code. It was generating token type as
variable name. Now, I gen "s<ttype>" for implicit labels on string literals.
November 30, 2012
* Maven updates (cleanup, unification, and specify Java 6 bootstrap classpath)

View File

@ -370,10 +370,14 @@ public class Target {
return Utils.capitalize(r.name)+gen.templates.getInstanceOf("RuleContextNameSuffix").render();
}
// should be same for all refs to same token like $ID within single rule function
// should be same for all refs to same token like ctx.ID within single rule function
// for literals like 'while', we gen _s<ttype>
public String getImplicitTokenLabel(String tokenName) {
ST st = gen.templates.getInstanceOf("ImplicitTokenLabel");
int ttype = gen.g.getTokenType(tokenName);
if ( tokenName.startsWith("'") ) {
return "s"+ttype;
}
String text = getTokenTypeAsTargetLabel(gen.g, ttype);
st.add("tokenName", text);
return st.render();

View File

@ -37,14 +37,14 @@ public class TestParserExec extends BaseTest {
@Test public void testLabels() throws Exception {
String grammar =
"grammar T;\n" +
"a : b1=b b2+=b*;\n" +
"a : b1=b b2+=b* b3+=';' ;\n" +
"b : id=ID val+=INT*;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+;\n" +
"WS : (' '|'\\n') -> skip ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a",
"abc 34", false);
"abc 34;", false);
assertEquals("", found);
assertEquals(null, stderrDuringParse);
}