Wednesday, March 18, 2015

We've got grammars


It's been a couple of weeks, release time! Our new feature is custom grammars, or the ability for users to embed their languages (or existing ones) in their favorite language. The implications of this feature are clear: domain specific languages can now be a seamlessly integrated into mainstream languages.

Much  have been written about DSLs, with the consensus been:

a) These languages are incredibly useful.
b) These languages are incredibly hard to be made useful.

Just think for a second the amount of work it would take to write one such language and make it mainstream. Now compare that mental image to this:





Now, I know this programming language stuff is hard, so I apologize. It should actually take more than 2 minutes to create an useful extension like this one. However, clocking in at 125 lines of code, I assume an afternoon is enough, Not bad for something you wouldn't have otherwise.

What about the grammars, tho? Well, the grammars used are strictly ANTLR grammars. For the uninitiated, antlr would be the de facto standard for grammar writing. In fact, we do not even process the grammar themselves, letting their compiler generate the basic c# infrastructure. This makes the whole thing very tested and solid. Thanks, @antlrguy.

What we do, after the grammar is processed, is to augment the generated code so it calls your transform functions. As seen in the video, we can generate these functions automatically for you. All you have to do is provide the right transformations.

These transformations are in essence the same as traditional Roslyn transformations. Only the starting point is not a SyntaxNode, but a node derived from the grammar in question. In  this particular example the pairs [name, value] are transformed into a c# anonymous object. Straightforward stuff.




No comments:

Post a Comment