I’ve been pretty quiet the last couple of months because I have been very busy at work and at home I have been slowly chipping away at a new Parser for MetaSharp.
It’s very late at night here so I’m not going to go into a lot of detail right now but suffice it to say that currently my Parser is completely hand written and right now I’m working on a Transform step that will let me generate Parsers from a grammar. The goal is to generate the Parser from the Grammar grammar!
I still have a long way to go but here is what I do have. The following Grammar:
grammar Simple < MetaSharp.Transformation.Parsing.Common.BasicParser: Alphabet = (A | B | C)+; A = "a"; B = "b"; C = "c"; end
Produces the following code:
//——————————————————————————
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.21006.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//——————————————————————————
public partial class Simple : MetaSharp.Transformation.Parsing.Common.BasicParser
{
internal new static System.Collections.Generic.IEnumerable<string> Imports;
static Simple()
{
Simple.Imports = new string[0];
}
public Simple(MetaSharp.Transformation.IContext context) :
base(context)
{
MetaSharp.Transformation.IRuleService ruleService =
this.Context.Locate<MetaSharp.Transformation.IRuleService>();
this.Add(
newMetaSharp.Transformation.Patterns.OrPattern(
ruleService.GetRule(“Alphabet”, Simple.Imports),
ruleService.GetRule(“A”, Simple.Imports),
ruleService.GetRule(“B”, Simple.Imports),
ruleService.GetRule(“C”, Simple.Imports)));
}
}
[MetaSharp.Transformation.RuleExportAttribute(typeof(AlphabetRule))]
public class AlphabetRule : MetaSharp.Transformation.Rule
{
protected override void Initialize()
{
this.Body = new MetaSharp.Transformation.Patterns.OneOrMorePattern(
new MetaSharp.Transformation.Patterns.OrPattern(
new MetaSharp.Transformation.Patterns.OrPattern(
this.Rules.GetRule(“A”, Simple.Imports),
this.Rules.GetRule(“B”, Simple.Imports)),
this.Rules.GetRule(“C”, Simple.Imports)));
base.Initialize();
}
}
[MetaSharp.Transformation.RuleExportAttribute(typeof(ARule))]
public class ARule : MetaSharp.Transformation.Rule
{
protected override void Initialize()
{
this.Body = new MetaSharp.Transformation.Patterns.StringPattern(“a”);
base.Initialize();
}
}
[MetaSharp.Transformation.RuleExportAttribute(typeof(BRule))]
public class BRule : MetaSharp.Transformation.Rule
{
protected override void Initialize()
{
this.Body = new MetaSharp.Transformation.Patterns.StringPattern(“b”);
base.Initialize();
}
}
[MetaSharp.Transformation.RuleExportAttribute(typeof(CRule))]
public class CRule : MetaSharp.Transformation.Rule
{
protected override voidInitialize()
{
this.Body = new MetaSharp.Transformation.Patterns.StringPattern(“c”);
base.Initialize();
}
}
It’s a pretty trivial grammar for now but this should give you an idea of where this is going. The RuleExportAttribute inherits from the MEF ExportAttribute and the concrete IRuleService uses MEF to supply rules using these attributes.
It’s exciting!