Inside of MetaSharp is a CLS compliant language using the same system and patterns that you would use to create an external DSL. One of the parts of creating your own DSL in MetaSharp is declaring Node objects to represent your parsed graph. MetaSharp will convert your parsed MGrammar graph into your strongly Typed AST (Nodes) which you can then use to transform however you wish.
Declaring your AST nodes requires a certain design pattern and after some prompting from Attila the Hun I have created a DSL specifically for creating nodes. Here it is before the DSL:
//----------------------------------------------------------------------- // <copyright company="MetaSharp"> // Copyright (c) MetaSharp. All rights reserved. // </copyright> //----------------------------------------------------------------------- namespace MetaSharp.Lang.Ast.Standard { using System.Collections.Generic; using MetaSharp.Lang.Ast.Common; /// <summary> /// A foreach statement node. /// </summary> public class ForEachStatement : Statement { /// <summary> /// The VariableType NodeProperty. /// </summary> public static readonly NodeProperty VariableTypeProperty = NodeProperty.Register<ForEachStatement>(n => n.VariableType); /// <summary> /// The VariableName NodeProperty. /// </summary> public static readonly NodeProperty VariableNameProperty = NodeProperty.Register<ForEachStatement>(n => n.VariableName); /// <summary> /// The Expression NodeProperty. /// </summary> public static readonly NodeProperty ExpressionProperty = NodeProperty.Register<ForEachStatement>(n => n.Expression); /// <summary> /// The Statements NodeProperty. /// </summary> public static readonly NodeProperty StatementsProperty = NodeProperty.Register<ForEachStatement>(n => n.Statements); /// <summary> /// Gets the variable type. /// </summary> public TypeReference VariableType { get { return (TypeReference)this.GetValue(VariableTypeProperty); } set { this.SetValue(ForEachStatement.VariableTypeProperty, value); } } /// <summary> /// Gets the variable name. /// </summary> public string VariableName { get { return (string)this.GetValue(VariableNameProperty); } set { this.SetValue(ForEachStatement.VariableNameProperty, value); } } /// <summary> /// Gets the enumeration expression. /// </summary> public Expression Expression { get { return (Expression)this.GetValue(ExpressionProperty); } set { this.SetValue(ForEachStatement.ExpressionProperty, value); } } /// <summary> /// Gets the statements. /// </summary> public IEnumerable<Statement> Statements { get { return (IEnumerable<Statement>)this.GetValue(StatementsProperty); } set { this.SetValue(ForEachStatement.StatementsProperty, value); } } } }
And here it is as a DSL:
//----------------------------------------------------------------------- // <copyright company="MetaSharp"> // Copyright (c) MetaSharp. All rights reserved. // </copyright> //----------------------------------------------------------------------- namespace MetaSharp.Lang.Ast.Standard: import System.Collections.Generic; import MetaSharp.Lang.Ast.Common; import MetaSharp.Transformation; /// <summary> /// A foreach statement node. /// </summary> node ForEachStatement as Statement: /// <summary> /// The VariableType NodeProperty. /// </summary> property VariableType as TypeReference; /// <summary> /// The VariableName NodeProperty. /// </summary> property VariableName as string; /// <summary> /// The Expression NodeProperty. /// </summary> property Expression as Expression; /// <summary> /// The Statements NodeProperty. /// </summary> property Statements as IEnumerable<Statement>; end end
It went from 76 lines to 38, so that’s a win in my book. Plus most of the lines that are there are much shorter. The only downside is the lack of intellisense and syntax hilighting but I have reason to believe that that is a solvable problem in general if you’re using MGrammar as your parser, since it’s already possible in Intellipad.
To use this DSL all you have to do is import MetaSharp.Lang.targets into your .csproj file.
<Import Project="$(NodeBuilderBinPath)\MetaSharp.Lang.targets" />
Then in your project you simply add your items to the project as Nodes. Like so:
This will generate a file for you at compile time in your projects language (i.e. this should work in VB as well) and that file will get compiled along with the assembly. Next I want to build a Pipeline DSL, then the process of building your own DSL will all be done in DSLs as well!
You must log in to post a comment.