Friday, 27 March 2009

In my last post, I finished up by mentioning that the first task was to outline a DSL syntax containing metadata and build instructions for each package dependency.

We are basing our package manager on the Portage build system and hope to replicate their metaphor of the Portage Tree.The DSL build instructions will reside in our package tree structure in the form of a build.boo file.

The choice of language for the DSL was an obvious one and we went with Boo which is a statically typed language for the CLI with a python inspired syntax and a special focus on language and compiler extensibility.

Below is the current DSL syntax for defining the package metadata and build instructions:install horn: description "This is a description of horn" get_from svn("https://scotaltdotnet.googlecode.com/svn/trunk/") build_with msbuild, buildfile("source/Horn/horn.sln"), frameworkVersion35

This is our first iteration DSL and I expect the above prose to go through multiple metamorphic transformations as the ebb and flow of horn twists and turns through its development evolutionary cycle. I am also expecting that this is a fairly readable grammar and should explain what the purpose is.

First we are defining some basic metadata by way of the description of the install package.I want to further open the metadata details out to contain other information points like the project’s home page URL and a category definition containing key words to make the packages searchable through the horn application.I have pencilled in lucene.net as a proposed means of indexing the horn package tree.

Next we have the following line:

get_from svn("https://scotaltdotnet.googlecode.com/svn/trunk/")

Hopefully this sentence structure is perfectly readable and it is indeed clear that the sentence is a command that gets the source from subversion at the parenthesisised URI.

I should add at this point that we are using the excellent Rhino.DSL tool developed by Oren Eini. It makes getting started with Boo DSL much easier and I highly recommend its use to develop your own DSL.The steps that were completed to allow the horn Boo DSL syntax to be parsed are as follows:

A subclass was christened from the DslEngine base class of Rhino.DSL named ConfigReaderEngine. Then an AnonymousBaseClassCompilerStep was added to the compiler pipeline.

created an abstract class named BaseConfigReader that the AnonymousBaseClassCompilerStep will use to parse the DSL syntax into.

Below is the listing of the horn install Dsl’s AnonymousBaseClassCompilerStep:

I will now attempt and I must stress attempt to explain how the build_with grammar can both be constructed and parsed out with regular C# code.Let us remind ourselves of the syntax we want to parse:

build_with msbuild, buildfile("source/Horn/horn.sln"), frameworkVersion35The BaseConfigReader abstract class mentioned previously will be entrusted with the job of making sense of our install grammar. Let us now start breaking the sentence structure down into it’s constituent parts.

The first part of our sentence is the build_with construct and this verb will be used to inform horn which build engine to use, where the build file is situated relative to where the package’s position is in the package tree directory hive and lastly which .NET framework version to build against.

A programmatic view of how to view this sentence is to observe build_with as the method name and the other parts as parameters delimited by commas that will be passed to the method. One of the reasons that boo is such a good fit for authoring DSLs is the absence of parenthesis needed for defining methods.

The following C# method is charged with making sense of the boo statement:

The first thing of note is the Boo.Lang Meta attribute.Static methods tagged with the Meta attribute should be perceived as a shortcut to the compiler that accepts AST (Abstract Syntax Tree) Nodes and returns an AST node.

An AST is the most common way for computers to work with structured text.They turn the structured text into a graph on which operations can be performed against instead of manual parsing.

If we can return to the boo sentence under examination:build_with msbuild, buildfile("source/Horn/horn.sln"), frameworkVersion35Each sentence part after the build_with expression can be viewed as an AST node that will be passed into the buildWith static meta method in the form of the boo language construct of an expression.

The msbuild snippet is translated as a ReferenceExpression. A ReferenceExpression can be viewed as string literal without the double quotes.

The buildFile node is a MethodInvocationExpression. A MethodInvocationExpression contains the method name we want to invoke an the arguments if any that are needed to complete the method call.

This leaves the door open to call other build engines like nant for example.We then satisfy the method call by selecting the argument from the buildFile method invocation and the framework version reference expression.

While constructing this DSL, I found the boo documentation pretty non existent but was able to extract knowledge both from Ayendes’s DSL book and the Rhino.DSL test fixtures.

If any of this is of interest to you then please join the Horn user group for updates or check out the source here.