[stringtemplate-interest] C# StringTemplateGroup newbie question

malcolm smith malsmith at treehousesystems.com
Tue Sep 25 09:03:10 PDT 2007


Perhaps I am missing something, but from what I see in other posts the
following scenario should work with stringtemplate V3.0.1 –

Here is what I am trying to do.

1)        Load a string template group file from a directory (thequery.stg)
in this case.  The stg file has a reference to a plain old template file
flist() (in flist.st).

2)       Assign some attributes.

3)        Convert string template object to string.

 What happens instead is that when I run ToString() I get a "Unhandled
Exception: Antlr.StringTemplate.TemplateLoadException: Can't load template '
flist.st'; context is [test]"

 On the other hand if I do not use the stringtemplategroup file and stick to
plain old templates then the flist.st file loads and executes fine.
Likewise, if I define a string template group definition for flist() ::= …
then that also works.

 I'm using the C# version of v3.0.1 relevant code is included below:

 File: Main.cs

StringTemplateGroup .*RegisterGroupLoader*( *new **CommonGroupLoader *(
errors , "..\\..\\sample"));
StringTemplateGroup group = StringTemplateGroup.*LoadGroup *("thequery" );
StringTemplate query = group.*GetInstanceOf* ("sqlSelect" );

query. *SetAttribute*( "column.{colname,coltype,len,default}", "name",
"nvarchar" ,"50" ,"'test'");
query.* SetAttribute*("column.{colname,coltype,len}" , "email" ,"nvarchar","50"
);
query .*SetAttribute*( "column.{colname,coltype,len}", "netamt","decimal" ,
"15,4" );
query.* SetAttribute*("column.{colname,coltype}" , "id" ,"int");
query.*SetAttribute* ("table" , "User");

Console. *WriteLine*( query.*ToString* ());



File: thequery.stg

group simple;


sqlSelect(table,column) ::= <<
SELECT
<flist(column)>
FROM <table> WHERE ID \> 1;
>>

/* IGNORE THIS

IGNORE_flist(column) ::= <<
<column : {
      <it.colname> <typemap.(it.coltype)><if (it.len)>(<it.len>)<endif><if (
it.default)> default <it.default><endif>};
separator=",">
>>

*/

typemap ::= [
"int" : "int",
"nvarchar" : "nvarchar",
"decimal" : "decimal",
default : "unknown"
]



File: flist.st
$column : {
      $it.colname$ $typemap.(it.coltype)$$if (it.len)$($it.len$)$endif$$if (
it.default)$ default $it.default$$endif$};
separator=","$


Any help would be greatly appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20070925/e72c53c0/attachment.html 


More information about the stringtemplate-interest mailing list