antlr / stringtemplate4

StringTemplate 4
http://www.stringtemplate.org
Other
956 stars 231 forks source link

StackOverFlow when template is recursive #110

Open fwang2002 opened 9 years ago

fwang2002 commented 9 years ago

I use the template as recursion, it will cause stack overflow. I'm sure there will not be an endless recursive call. The template looks as following:

struct(name, struct) ::= <<
<struct.fields:{field|<anonymousStruct(name, field)>}>
>>

anonymousStruct(name1, field) ::= <<
<if(field.isStruct && field.isAnonymous)>
<struct({<name1><field.name>}, field.asStruct)>
<endif>
>>

In the above code, the struct() in anonymousStruct() can be call one time, the second time will cause stack overflow. If I change "name1" to "name", the first time when call struct() in annonymousStruct() will cause stack overflow.

When I try to solve the problem, I find even there is no recursion, the stack overflow may happen. Here I try to combine name1 and name2 into one name.

entry() ::= <<
<combine("abc", "def")>
>>

combine(name1, name2) ::= <<
<combineoutput({<name1><name2>})>
>>

combineoutput(name) ::= <<
<name>
>>

combineoutput2(name1) ::= <<
<name1>
>>

combineoutput2 and combineoutput is just like the situation when I mentioned above.

If I really want to combine names in template, How can I solve the problem?