Include initial value for attributes in generated code (fixes #672)
This commit is contained in:
parent
6e581b3be6
commit
afadc2d976
|
@ -770,7 +770,7 @@ public \<T> T accept(ParseTreeVisitor\<? extends T> visitor) {
|
|||
}
|
||||
>>
|
||||
|
||||
AttributeDecl(d) ::= "<d.type> <d.name>"
|
||||
AttributeDecl(d) ::= "<d.type> <d.name><if(d.initValue)> = <d.initValue><endif>"
|
||||
|
||||
/** If we don't know location of label def x, use this template */
|
||||
labelref(x) ::= "<if(!x.isLocal)>((<x.ctx.name>)_localctx).<endif><x.name>"
|
||||
|
|
|
@ -36,8 +36,10 @@ import org.antlr.v4.tool.Attribute;
|
|||
/** */
|
||||
public class AttributeDecl extends Decl {
|
||||
public String type;
|
||||
public String initValue;
|
||||
public AttributeDecl(OutputModelFactory factory, Attribute a) {
|
||||
super(factory, a.name, a.decl);
|
||||
this.type = a.type;
|
||||
this.initValue = a.initValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -567,4 +567,31 @@ public class TestParserExec extends BaseTest {
|
|||
assertEquals("", found);
|
||||
assertNull(stderrDuringParse);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a regression test for antlr/antlr4#672 "Initialization failed in
|
||||
* locals".
|
||||
* https://github.com/antlr/antlr4/issues/672
|
||||
*/
|
||||
@Test public void testAttributeValueInitialization() throws Exception {
|
||||
String grammar =
|
||||
"grammar Data; \n" +
|
||||
"\n" +
|
||||
"file : group+ EOF; \n" +
|
||||
"\n" +
|
||||
"group: INT sequence {System.out.println($sequence.values.size());} ; \n" +
|
||||
"\n" +
|
||||
"sequence returns [List<Integer> values = new ArrayList<Integer>()] \n" +
|
||||
" locals[List<Integer> localValues = new ArrayList<Integer>()]\n" +
|
||||
" : (INT {$localValues.add($INT.int);})* {$values.addAll($localValues);}\n" +
|
||||
"; \n" +
|
||||
"\n" +
|
||||
"INT : [0-9]+ ; // match integers \n" +
|
||||
"WS : [ \\t\\n\\r]+ -> skip ; // toss out all whitespace\n";
|
||||
|
||||
String input = "2 9 10 3 1 2 3";
|
||||
String found = execParser("Data.g4", grammar, "DataParser", "DataLexer", "file", input, false);
|
||||
assertEquals("6\n", found);
|
||||
assertNull(stderrDuringParse);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue