[nem-en] Macros and syntax extensions.
vc
vc at rsdn.ru
Mon Apr 30 20:00:31 CEST 2007
> Sure, I think that $-string is good for one line formatting of text.
+1
> would like to have a way to reuse existing StringBuilder there,
You have any ideas of sintay?
> so I
> could e.g. do:
>
> foreach (x in xs)
> acc << $"insert into $(x.table) ($(x.colums) ) values ($(x.values));";
In ST syntax code like this can by expressed by recursive called template
(or by inline template).
Example which use inline template:
InsertAll(xs : Some) : string {<#
..$(xs; "\n"; x =>
<# insert into $(x.table) (..$(x.colums)) values (..$(x.values)); #>)
#>
Example which use 2 templates:
InsertAll(xs : list[Some]) : string {<#
..$(xs; "\n"; Insert)
#>
Insert(x : Some) : string {<#
insert into $(x.table) (..$(x.colums)) values (..$(x.values));
#>
> Is StringTemplate something similar to ASP/JSP/Ruby notation for
> building web pages?
> Like:
> <p>
> <b><%= name %></b> : <%= val %>
> </p>
You can make and web paje too, but it'a a common text generation engine. It
be right for code generation too.
Terence Parr use StringTemplate for generate parser/lexer code (in meny
languages) in ANTLR 3.0.
> It would be great if you paste some example of using StringTemplate in
> this context and in contex mentioned at the beginning of this thread.
> What is your vision of integrating it with Nemerle?
Below I show example of using Nemerle version of ST which generate "CREATE
INDEX" DDL statement:
using System;
using System.Console;
using Nemerle.Utility;
using Nemerle.StringTemplate;
[Record]
class Index
{
public Name : string;
public Entries : list[IndexEntry];
}
[Record]
class IndexEntry
{
public Name : string;
public IsDescend : bool;
}
[StringTemplateGroup]
public class MyTemplateGroup
{
CreateIndex(index : Index) : string {<#
CREATE INDEX $(index.Name)
(
..$(index.Entries; "\n"; IndxEntry)
)
#>}
IndxEntry(entry : IndexEntry) : string {<#
$(entry.Name)$(Descend(entry))
#>}
Descend(entry : IndexEntry) : string
{
if (entry.IsDescend) " DESC" else ""
}
}
The CreateIndex and IndxEntry is a String Template (ST). The MyTemplateGroup
is a String Template Group (STG). We can inherit from MyTemplateGroup some
other STG and override in it some templates.
In compile time for each template method macros StringTemplateGroup add one
hiden method which contains generative code (which use shared for STG
StringBuilder instance). Body of source template methods rewrites for use
hiden methods.
Vlad
More information about the devel-en
mailing list