C # DSL Syntax Ideas

I am thinking of implementing a template template using only plain C # /. NET 4 syntax with regard to static typing.

Then on top of this template language, we could create domain-specific languages (say, HTML4, XHTML, HTML5, RSS, Atom, multi-page emails, etc.).

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

Looking at SharpDOM, I'm really impressed with what you can do using .NET (4).

So, I believe that there are some not very well-known ways to implement custom DSL in .NET 4. Perhaps this is not the case in Ruby, but still.

So my question is: what are the features of C # (4) syntax that can be used to implement custom DSLs?

Examples I can think of 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");

It is very difficult for me to come up with the syntax with the least amount of noise .

Please note that I am not talking about another language (Boo, IronRuby, etc.), and I'm not talking about different template mechanisms (NHaml, Spark, StringTemplate, etc.).

+3
source share
2 answers

, DSL, . # 4.0, DSL, . , #, LINQ DSL. LINQ , .

+1

T4 Templates? DSL, , , , . , TextTemplate1.tt:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".thml" #>
<#@ import namespace="System.Collections.Generic" #>

<div class="head">
    <ul class="menu" id="main-menu">
<#
foreach (var item in allItems)
{
#>
        <li><#= item.Name #></li>
<#
}
#>
    </ul>
</div>
<#+
public class DummyItem
{
    public string Name {get;set;}
}

public List<DummyItem> allItems = new List<DummyItem>
                                  {
                                      new DummyItem {Name="Name1"},
                                      new DummyItem {Name="Name2"},
                                  };
#>

:

<div class="head">
    <ul class="menu" id="main-menu">
        <li>Name1</li>
        <li>Name2</li>
    </ul>
</div>

, - , !

+2

Source: https://habr.com/ru/post/1745781/


All Articles