Ask a questionAsk a question
 

AnswerExecuting a Grammar from C#

  • Monday, October 12, 2009 5:38 PMLuis D. Rojas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How can I call a grammar ( using m Engine ) developed in M from C#
    Best Regards
    Luis Diego Rojas. http://icomparable.blogspot.com
    MCP, MCTS Biztalk Server

Answers

  • Monday, October 12, 2009 6:17 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Basically you have to do the following

    Compile your .mg file into an .mx by setting some of the following values into your project file:
     <PropertyGroup>
       <MBinaries32Path Condition="'$(MBinaries32Path)'==''">$(ProgramFiles)\Microsoft Oslo\1.0\bin</MBinaries32Path>
       <MBinaries64Path Condition="'$(MBinaries64Path)'==''">$(SystemDrive)\Program Files (x86)\Microsoft Oslo\1.0\bin</MBinaries64Path>
       <MTargetsPath Condition="'$(MTargetsPath)'==''">$(ProgramFiles)\MsBuild\Microsoft\M\v1.0</MTargetsPath>
       <MgTarget>Mgx</MgTarget>
     </PropertyGroup>
     <ItemGroup>
       <Reference Include="$(MBinaries32Path)\Microsoft.M.dll">
         <HintPath>"$(MBinaries64Path)\Microsoft.M.dll</HintPath>
         <Name>Microsoft.M</Name>
       </Reference>
       <Reference Include="$(MBinaries32Path)\Xaml.dll">
         <HintPath>$(MBinaries64Path)\Xaml.dll</HintPath>
         <Name>Xaml</Name>
       </Reference>
       <Reference Include="$(MBinaries32Path)\System.DataFlow.dll">
         <HintPath>$(MBinaries64Path)\System.DataFlow.dll</HintPath>
         <Name>System.DataFlow</Name>
       </Reference>
     </ItemGroup>
     <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
     <Import Project="$(MTargetsPath)\Microsoft.M.Embedded.targets" />
    <ItemGroup>
        <MCompile Include="Lang.mg" />
    </ItemGroup>

    Then, at runtime, load the compiled .mx from the assembly resources using DynamicParser.LoadFromStream.
    using (Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".mx"))
    {
       return DynamicParser.LoadFromStream(stream, language);
    
    Then you can call the dynamic parser like so:
    DynamicParser langParser = dynamicParserService.Load();
    System.Dataflow.ISourceInfo rootNode = (System.Dataflow.ISourceInfo)langParser.Parse(new TextReaderTextStream(streamService.Uri.LocalPath, code), errorReporter);
    

    Where 'code' is the Stream of the file containing your DSL text.

    Additionally you can check out the MetaSharp source code for a more complete reference on how to not only read the DSL but how to interpret it into CLR objects:

    Also, you might be able to just use it to do whatever it is you're trying to accomplish.

All Replies

  • Monday, October 12, 2009 6:17 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Basically you have to do the following

    Compile your .mg file into an .mx by setting some of the following values into your project file:
     <PropertyGroup>
       <MBinaries32Path Condition="'$(MBinaries32Path)'==''">$(ProgramFiles)\Microsoft Oslo\1.0\bin</MBinaries32Path>
       <MBinaries64Path Condition="'$(MBinaries64Path)'==''">$(SystemDrive)\Program Files (x86)\Microsoft Oslo\1.0\bin</MBinaries64Path>
       <MTargetsPath Condition="'$(MTargetsPath)'==''">$(ProgramFiles)\MsBuild\Microsoft\M\v1.0</MTargetsPath>
       <MgTarget>Mgx</MgTarget>
     </PropertyGroup>
     <ItemGroup>
       <Reference Include="$(MBinaries32Path)\Microsoft.M.dll">
         <HintPath>"$(MBinaries64Path)\Microsoft.M.dll</HintPath>
         <Name>Microsoft.M</Name>
       </Reference>
       <Reference Include="$(MBinaries32Path)\Xaml.dll">
         <HintPath>$(MBinaries64Path)\Xaml.dll</HintPath>
         <Name>Xaml</Name>
       </Reference>
       <Reference Include="$(MBinaries32Path)\System.DataFlow.dll">
         <HintPath>$(MBinaries64Path)\System.DataFlow.dll</HintPath>
         <Name>System.DataFlow</Name>
       </Reference>
     </ItemGroup>
     <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
     <Import Project="$(MTargetsPath)\Microsoft.M.Embedded.targets" />
    <ItemGroup>
        <MCompile Include="Lang.mg" />
    </ItemGroup>

    Then, at runtime, load the compiled .mx from the assembly resources using DynamicParser.LoadFromStream.
    using (Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".mx"))
    {
       return DynamicParser.LoadFromStream(stream, language);
    
    Then you can call the dynamic parser like so:
    DynamicParser langParser = dynamicParserService.Load();
    System.Dataflow.ISourceInfo rootNode = (System.Dataflow.ISourceInfo)langParser.Parse(new TextReaderTextStream(streamService.Uri.LocalPath, code), errorReporter);
    

    Where 'code' is the Stream of the file containing your DSL text.

    Additionally you can check out the MetaSharp source code for a more complete reference on how to not only read the DSL but how to interpret it into CLR objects:

    Also, you might be able to just use it to do whatever it is you're trying to accomplish.