[stringtemplate-interest] StringTemplate rules
Terence Parr
parrt at cs.usfca.edu
Thu Sep 22 11:02:02 PDT 2005
On Sep 2, 2005, at 3:22 AM, Jose San Leandro wrote:
> 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
Parameters are actually parameter assignments so you would need the
wacky
source(year, name, package) ::= <<
<header_declaration(year=year,name=name)>
>>
or as of 2.2 you can just use "...":
source(year, name, package) ::= <<
<header_declaration(...)>
>>
which explicitly allows all attributes in source to flow thru.
> 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?
Not necessarily. Your nested subtemplates could actually have the
attributes set in them directly as you build up the big template.
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
More information about the stringtemplate-interest
mailing list