views:

130

answers:

1

Hi,

I am thinking about implementing a templating engine using only the plain C#/.NET 4 syntax with the benefit of static typing.

Then on top of that templating language we could create Domain Specific Languages (let's say HTML4, XHTML, HTML5, RSS, Atom, Multipart Emails and so on).

One of the best DSLs in .NET 4 (if not only one) is SharpDOM. It implements HTML-specific DSL.

Looking at SharpDOM, I am really impressed of what you can do using .NET (4).

So I believe there are some not-so-well-known ways for implementing custom DSLs in .NET 4. Possibly not as well as in Ruby, but still.

So my the question would be: what are the C# (4) specific syntax features that can be used for implementing custom DSLs?

Examples I can think of right now:

// HTML - doesn't look tooo readable :)
div(clas: "head",
  ul(clas: "menu", id: "main-menu", () => {
    foreach(var item in allItems) {
      li(item.Name)
    }
  }) // See how much noise it has with all the closing brackets?
)

// Plain text (Email or something) - probably too simple
Line("Dear {0}", user.Name);
Line("You have been kicked off from this site");

For me it is really hard to come up with the syntax with least amount of noise.

Please NOTE that I am not talking about another language (Boo, IronRuby etc), neither I am not talking about different templating engines (NHaml, Spark, StringTemplate etc).

Thanks,
Dmitriy.

A: 

I would guess you are familiar with Martin Fowlers DSL book, but if not definitely look at it. It does not contain anything specific to C# 4.0, but has some general patterns for internal DSLs which you could use. Also look at how generic types and type inference works in C#, LINQ might be a good example of using advanced language features to implement a DSL. In the LINQ stuff should also be some AST manipulation, which might interest you as well.

Gabriel Ščerbák
I am not currently looking for implementation details, but rather at good examples of how people would like to write the code
Dmytrii Nagirniak
@Dmitriy Nagirnyak for any DSL if you are thinking about the design of concrete syntax, look if there already exists any notations for that particular domain (templateing engines in your case) and than consult domain experts (C# GUI developers) and than domain users, who are target audience for your DSL (that is your choice). So I would say, this is not an answerable question per se, it depends on target audience. I would suggest creating a survey comparing syntaxes of different templating engines to find out what people like using.
Gabriel Ščerbák
The target audience is C# developer. I cannot create any survey comparing syntaxes as I just do not have any syntax yet. The puprose of this question is to find out possible usable syntaxes in C# 4.
Dmytrii Nagirniak
You have those already existing templating engines. If some uses nested blocks, you can do them using functin call nesting. If something uses keyword structure, go for the builder pattern. I thought you should look at what is used and popular in those existing engines and implement it using the C#.
Gabriel Ščerbák
Gabriel, can you give the examples of the C# syntax? I don't care at this moment about the technicalities, I only need the syntax.
Dmytrii Nagirniak
As I said I am not much of a C# guy.
Gabriel Ščerbák