[stringtemplate-interest] File based templates and maps.
Terence Parr
parrt at cs.usfca.edu
Thu Oct 19 10:32:01 PDT 2006
On Oct 19, 2006, at 10:08 AM, Caleb Lyness wrote:
> Hi,
>
> Thank you for the quick response. I managed to do what I wanted
> (using a group file).
>
> Here is another interesting thing I have been playing with:
>
> $(headerTemplate)(); null=headers/default()$
>
> It includes a default header template if the attribute has not been
> set.
heh, cool :)
> My first try was this:
> $(headerTemplate; null="headers/default")()$
> This does not work? should it though? cf: <(expr)(argument-list)>
Hmm....interesting. Seems like it should.
> I think it would be nice for future newbies if there was an example
> along side the file based
> template (.st) method in the source. Would speed the learning curve
> up a bit.
> cf: src\org\antlr\stringtemplate\test.
Add a quick tutorial :)
http://www.antlr.org/wiki/display/ST/Tutorials
Ter
>
> Here is quick .stg example:
>
> te.stg:
> group groupName;
>
> typeInitMap ::= [
> "Integer":"0",
> "long":"1",
> default:"null"
> ]
>
> variable(type,name) ::= <<
> <b>$type$</b> $name$ = $typeInitMap.(type)$;$\n$
> >>
>
> t.java:
> import java.io.File;
> import java.io.FileReader;
>
> import org.antlr.stringtemplate.StringTemplate;
> import org.antlr.stringtemplate.StringTemplateGroup;
> import org.antlr.stringtemplate.language.DefaultTemplateLexer;
>
> public class t {
> /* Quick and very nasty example: */
> public static void main(String[] args)
> throws Exception
> {
> File templateFileName = new File("te.stg");
> StringTemplateGroup group = new StringTemplateGroup(
> new FileReader(templateFileName),
> DefaultTemplateLexer.class //Leave away if you want
> to use angle brace notation
> );
>
> StringTemplate template = group.getInstanceOf("variable");
> template.setAttribute("type", "Integer");
> template.setAttribute("name", "id");
> System.out.println(template.toString());
> }
> }
>
>
>
>
>
>
>
>>> Hi, I have been trying to use a map for file based templates. So
>>> far no luck. Is this actually possible or have I missed the
>>> point. I have tried too many combination to list them here, so I
>>> just list the last thing I tried: hash map defined in reason.st:
>>> --- $[ "1":"reason for 1", "2":"reason for 2", "D":"reason for
>>> D", default : "unknown reason" ]$ --- Now this would make some
>>> sense, no?
>> Hi, sorry. Those maps only work in group file formats (.stg
>> files). Terence
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list