[stringtemplate-interest] StringTemplate rules
Jose San Leandro
jose.sanleandro at ventura24.es
Fri Sep 2 03:22:20 PDT 2005
Hi,
I have some questions regarding template groups and rules.
I'm trying to define a template to generate some kind of Java source files.
type1.st :
//;-*- mode: antlr-*-
group javaSourceType1Group;
source(year, name, package) ::= <<
<header_declaration()>
<package_declaration(package)>
>>
header_declaration() ::= <<
/*
(GPL License) Copyright <year>
Description: <name> does whatever.
*/
>>
package_declaration(package) ::= <<
package <package>;
>>
I test it through a beanshell script.
type1.bsh:
import org.antlr.stringtemplate.*;
import org.antlr.stringtemplate.language.*;
import java.util.*;
import java.io.*;
StringTemplateGroup group =
new StringTemplateGroup(
new FileReader("path-to/type1.st"), AngleBracketTemplateLexer.class);
StringTemplate template = group.getInstanceOf("source");
template.setAttribute("year", "2005");
template.setAttribute("name", "Euler");
template.setAttribute("package", "com.foo.bar");
print(template.toString());
The template below works. However, I'd rather use something like:
type1.st :
//;-*- mode: antlr-*-
group javaSourceType1Group;
source(year, name, package) ::= <<
<header_declaration(year,name)>
<package_declaration(package)>
>>
header_declaration(year,name) ::= <<
/*
(GPL License) Copyright <year>
Description: <name> does whatever.
*/
>>
package_declaration(package) ::= <<
package <package>;
>>
My questions are:
1) I cannot define "header_declaration" rule parameters, since, if I use
header_declaration(year,name) ::= <<
...
>>
the parameters are not being passed by the "source" rule (which is the one I
want to use to trigger the whole generation process), and they're not
printed. If "source" rule passes them explicitly
source(year, name, package) ::= <<
<header_declaration(year,name)>
<package_declaration(package)>
>>
I get an ANTLR saying
StringTemplate: error: template parse error: line 1:1: unexpected token:
header_declaration
StringTemplate: error: template parse error: line 1:24: unexpected token: ,
StringTemplate: error: problem parsing template 'source':
java.lang.NullPointerException
Also, if I pass only "year" parameter to "header_declaration", I get
StringTemplate: error: template header_declaration must have exactly one
formal arg in template context [source <invoke header_declaration arg
context>]
I wonder if I can define more than one parameter on nested rules, without
taking it from the context. That would allow me to use "header_declaration"
directly or not without having to define two identical rules. If I use
<header_declaration()> inside "source" rule, and define
header_declaration(year,name) ::= << ..., the rule cannot see the parameters.
2) My "source" rule will act as the entry point in the generation process for
each type. I don't know in advance the parameters its nested rules will
require. Do I have to declare them all in its declaration?
Thank you for you time!
Jose.
More information about the stringtemplate-interest
mailing list