MetaSharp mentioned on “M” Language Gallery

by Justin Chase 7. July 2009 08:17

http://msdn.microsoft.com/en-us/oslo/cc749619.aspx

MetaSharp

A simple Common Language Specification (CLS)-compliant, general purpose programming language for MetaSharp, a fully extensible, pipelined transformation engine that can be used for templated textual transformations (code generation), AST transformations and any combination thereof.
Justin Chase

There are a bunch of other cool grammars and Oslo related projects on there as well. Check em’ out.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Oslo | Microsoft | MetaSharp | DSL

Creating an internal DSL with MetaSharp

by Justin Chase 1. July 2009 10:38

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:

image

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!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Oslo | MetaSharp | DSL | C# | .NET

MGrammar Quick Challenge 2 - Unordered and optional multi-projections

by Justin Chase 26. June 2009 03:43

Given this grammar:

module test
{
    language test
    {
        syntax Main = OneTwo*;
        
        syntax OneTwo
            =  o:One t:Two "\r\n"
            => Result { One => true, Two => true }
            |  Two One "\r\n"
            => Result { One => true, Two => true }
            |  One "\r\n"
            => Result { One => true, Two => false }
            |  Two "\r\n"
            => Result { One => false, Two => true };
                
        token One = "one";
        token Two = "two";
        
        interleave Ignore = " ";
    }
}

How could I refactor the syntax "OneTwo" to have only a single projection?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

MGrammar | MetaSharp | DSL | Oslo

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Justin Chase

sweetest hat ever

I am a software developer from St. Paul MN and I work for Microsoft on the Expression team. This blog is about various technical topics I find myself encountering here and there. In addition to loving WPF and Xaml and Expression studio in general I have a special interest in DSLs, programming languages and games.

RecentComments

Comment RSS

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar