Creating a Domain Specific Language for Parsing
dotnet, geek, insanity, programming, tech 1 Comment »It’s with some trepidation that I start on what is sure to be the most geeky post I’ve created to date. First a little background. I thought it would be a worth-while exercise to create a view engine for the Asp.Net Mvc framework. This is a result of a series of comments I took part in on Haack’s blog post about using lambdas from aspx.
The specific comment I made was in response to a green-field question about what an ideal view engine would look like in your mind. For me, based on my experience with NVelocity and the MVC pattern in MonoRail, the less you put in the view the better and a simplistic view language is actually a strength because it encourages that.
I’ve also had some difficulty working with aspx as text because what you have are two formats, csharp and xml, both of which have a natural flow and indentation which can’t coexist peacefully. Angles, percents, and curly braces become a disordered chaos where nothing lines up rationally. (Readability is vital to maintainability.)
So I proposed something like the following.
<var i="0"/>
<var css="new string[] {'row', 'row-alt'}"/>
<table>
<for each="var hobby in Hobbies" i="i+1">
<tr class="$css[i%2];">
<td>$i;</td>
<td>$hobby.Title;</td>
<td>$Html.Function("arg","arg");</td>
</tr>
</for>
</table>
See, the theory is you use the same syntactical conventions for both markup and code and they’ll no longer fight each to dominate the file format. It’s very zen. And the editor’s normal assistance with colorization and indentation of xml helps you close loops, represent scope, etc. Since I’m the sort of person who feels he should put his brain where his mouth is, e.g. a fool or a masochist, I set out to implement exactly such a view engine.

Recent Comments