Permalink
Browse files

Applied patch from Nathan Stott, adding MethodSubstitutionBaseClassCo…

…mpilerStep and refactoring AnonymousBaseClassCompilerStep to a class hierarchy.

Breaking change: Renamed AnonymousBaseClassCompilerStep to ImplicitBaseClassCompilerStep - feedback showed that this is a better name.
Making _sure_ we wouldn't break if the asm.info is locked by VS.
Modifying Binsor to match new version of Rhino DSL.

git-svn-id: https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk@1470 079b0acf-d9fa-0310-9935-e5ade295c882
  • Loading branch information...
1 parent 368eae5 commit 33b1e5f9cf2b8006b59469c0f1f53a97b59a348a ayenderahien committed Jun 26, 2008
View
@@ -23,29 +23,3 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
-?
-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio 2008
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rhino.DSL-vs2008", "Rhino.DSL\Rhino.DSL-vs2008.csproj", "{B788F27C-E00A-434A-81AA-1D592E030934}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rhino.DSL.Tests-v2008", "Rhino.DSL.Tests\Rhino.DSL.Tests-v2008.csproj", "{9BCD2185-4653-45D7-99B2-3043E55A6746}"
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Debug|Any CPU = Debug|Any CPU
- Release|Any CPU = Release|Any CPU
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {B788F27C-E00A-434A-81AA-1D592E030934}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {B788F27C-E00A-434A-81AA-1D592E030934}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {B788F27C-E00A-434A-81AA-1D592E030934}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {B788F27C-E00A-434A-81AA-1D592E030934}.Release|Any CPU.Build.0 = Release|Any CPU
- {9BCD2185-4653-45D7-99B2-3043E55A6746}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {9BCD2185-4653-45D7-99B2-3043E55A6746}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {9BCD2185-4653-45D7-99B2-3043E55A6746}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {9BCD2185-4653-45D7-99B2-3043E55A6746}.Release|Any CPU.Build.0 = Release|Any CPU
- EndGlobalSection
- GlobalSection(SolutionProperties) = preSolution
- HideSolutionNode = FALSE
- EndGlobalSection
-EndGlobal
@@ -45,12 +45,12 @@ public void CanExecuteNewTypeAndGetCodeFromFile()
protected override void AddCompilerSteps(BooCompiler compiler, string filename, CompilerPipeline pipeline)
{
compiler.Parameters.Pipeline.Insert(1,
- new AnonymousBaseClassCompilerStep(typeof (MyAnonymousBaseClass), "Run"));
+ new ImplicitBaseClassCompilerStep(typeof (MyAnonymousBaseClass), "Run"));
}
}
public abstract class MyAnonymousBaseClass
{
public abstract void Run();
}
-}
+}
@@ -67,7 +67,7 @@ protected override void CustomizeCompiler(BooCompiler compiler, CompilerPipeline
new ParameterDeclaration("input", new SimpleTypeReference("System.String"));
parameters.Add(newParameterDeclaration);
- pipeline.Insert(1, new AnonymousBaseClassCompilerStep(typeof(MyClassWithParams),
+ pipeline.Insert(1, new ImplicitBaseClassCompilerStep(typeof(MyClassWithParams),
"Hello",
parameters,
"System"));
@@ -53,7 +53,7 @@ public class DemoDslEngine : DslEngine
{
protected override void CustomizeCompiler(BooCompiler compiler, CompilerPipeline pipeline, string[] urls)
{
- pipeline.Insert(1, new AnonymousBaseClassCompilerStep(typeof (DemoDslBase), "Execute"));
+ pipeline.Insert(1, new ImplicitBaseClassCompilerStep(typeof (DemoDslBase), "Execute"));
}
}
-}
+}
@@ -51,7 +51,7 @@ public class WithActionsDslEngine : DslEngine
protected override void CustomizeCompiler(BooCompiler compiler, CompilerPipeline pipeline, string[] urls)
{
pipeline.Insert(1,
- new AnonymousBaseClassCompilerStep(typeof(WithAction), "Execute",
+ new ImplicitBaseClassCompilerStep(typeof(WithAction), "Execute",
//default namespaces
"Rhino.DSL.Tests.FeaturesDSL"));
pipeline.Insert(2, new UseSymbolsStep());
@@ -83,4 +83,4 @@ public ActionMacro() : base("Action")
}
}
-}
+}
@@ -0,0 +1,5 @@
+SomeAbstractMethod:
+ System.Console.WriteLine("abstract")
+
+SomeVirtualMethod:
+ System.Console.WriteLine("virtual")
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Text;
+using Boo.Lang.Compiler;
+using MbUnit.Framework;
+
+namespace Rhino.DSL.Tests
+{
+ [TestFixture]
+ public class MethodSubstitutionBaseClassTestFixture : BaseCompilerTestFixture
+ {
+ private Assembly assembly;
+
+ [SetUp]
+ public override void SetUp()
+ {
+ base.SetUp();
+ assembly = Compile(@"MethodSubstitutionBaseClass.boo");
+ }
+
+ [Test]
+ public void CanCreateAnonymousBaseClass()
+ {
+ Assert.IsNotNull(assembly);
+ }
+
+ [Test]
+ public void WillCreateTypeWithSameNameAsFile()
+ {
+ Assert.IsNotNull(
+ assembly.GetType("MethodSubstitutionBaseClass")
+ );
+ }
+
+ [Test]
+ public void CanExecuteNewTypeAndGetCodeFromFile()
+ {
+ MyMethodSubstitutionBaseClass instance = (MyMethodSubstitutionBaseClass)assembly.CreateInstance("MethodSubstitutionBaseClass");
+ instance.SomeAbstractMethod();
+ instance.SomeVirtualMethod();
+ Assert.AreEqual("abstract" + Environment.NewLine +
+ "virtual" + Environment.NewLine,
+ consoleOutput.GetStringBuilder().ToString());
+ }
+
+ protected override void AddCompilerSteps(BooCompiler compiler, string filename, CompilerPipeline pipeline)
+ {
+ compiler.Parameters.AddAssembly(typeof(Boo.Lang.Compiler.Ast.DepthFirstTransformer).Assembly);
+ compiler.Parameters.Pipeline.Insert(1,
+ new MethodSubstitutionBaseClassCompilerStep(typeof(MyMethodSubstitutionBaseClass),
+ "System",
+ "Boo.Lang.Compiler.Ast.DepthFirstTransformer"));
+ }
+ }
+
+ public abstract class MyMethodSubstitutionBaseClass
+ {
+ public abstract void SomeAbstractMethod();
+ public virtual void SomeVirtualMethod()
+ {
+
+ }
+ }
+}
@@ -10,11 +10,11 @@ public class OrderActionsDslEngine : DslEngine
protected override void CustomizeCompiler(BooCompiler compiler, CompilerPipeline pipeline, string[] urls)
{
pipeline.Insert(1,
- new AnonymousBaseClassCompilerStep(typeof (BaseOrderActionsDSL), "Prepare",
+ new ImplicitBaseClassCompilerStep(typeof (BaseOrderActionsDSL), "Prepare",
//default namespaces
"Rhino.DSL.Tests.SchedulingDSL"));
pipeline.InsertBefore(typeof (ProcessMethodBodiesWithDuckTyping),
new UnderscorNamingConventionsToPascalCaseCompilerStep());
}
}
-}
+}
@@ -71,6 +71,7 @@
<Compile Include="ExternalDSL\ExternalDSLDemo.cs" />
<Compile Include="ExternalDSL\Parser.cs" />
<Compile Include="FeaturesDSL\WithActionsDSLFixture.cs" />
+ <Compile Include="MethodSubstitutionBaseClassTestFixture.cs" />
<Compile Include="OrderDSL\BaseOrderActionsDSL.cs" />
<Compile Include="OrderDSL\Order.cs" />
<Compile Include="OrderDSL\OrderActionsDslEngine.cs" />
@@ -108,6 +109,9 @@
<None Include="FeaturesDSL\HasAction.boo">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
+ <None Include="MethodSubstitutionBaseClass.boo">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
<None Include="namespacesImports.boo">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
@@ -138,4 +142,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
@@ -1,14 +1,13 @@
namespace Rhino.DSL.Tests.SchedulingDSL
{
- using System;
using Boo.Lang.Compiler;
public class SchedulingDslEngine : DslEngine
{
protected override void CustomizeCompiler(BooCompiler compiler, CompilerPipeline pipeline, string[] urls)
{
- pipeline.Insert(1, new AnonymousBaseClassCompilerStep(typeof (BaseScheduler), "Prepare",
+ pipeline.Insert(1, new ImplicitBaseClassCompilerStep(typeof (BaseScheduler), "Prepare",
"Rhino.DSL.Tests.SchedulingDSL"));
}
}
-}
+}
@@ -1,118 +0,0 @@
-namespace Rhino.DSL
-{
- using System;
- using System.Reflection;
- using Boo.Lang.Compiler.Ast;
- using Boo.Lang.Compiler.Steps;
- using Boo.Lang.Compiler.Util;
- using Module=Boo.Lang.Compiler.Ast.Module;
-
- /// <summary>
- /// Takes all the code that exists in a module's global section and put it in a specificied
- /// method of a class. Allow easy handling of the Anonymous Base Class pattern
- /// </summary>
- public class AnonymousBaseClassCompilerStep : AbstractCompilerStep
- {
- private readonly Type baseClass;
- private readonly string methodName;
- private readonly string[] namespaces;
- private readonly ParameterDeclarationCollection parameters;
-
- /// <summary>
- /// Create new instance of <seealso cref="AnonymousBaseClassCompilerStep"/>
- /// </summary>
- /// <param name="baseClass">The base class that will be used</param>
- /// <param name="methodName">The name of the method that will get all the code from globals moved to it.</param>
- /// <param name="namespaces">Namespaces that would be automatically imported into all modules</param>
- public AnonymousBaseClassCompilerStep(Type baseClass, string methodName, params string[] namespaces)
- : this(baseClass, methodName, null, namespaces)
- {
- }
-
- /// <summary>
- /// Create new instance of <seealso cref="AnonymousBaseClassCompilerStep"/>
- /// </summary>
- /// <param name="baseClass">The base class that will be used</param>
- /// <param name="methodName">The name of the method that will get all the code from globals moved to it.</param>
- /// <param name="parameters">The parameters of this method</param>
- /// <param name="namespaces">Namespaces that would be automatically imported into all modules</param>
- public AnonymousBaseClassCompilerStep(Type baseClass, string methodName, ParameterDeclarationCollection parameters, params string[] namespaces)
- {
- this.namespaces = namespaces;
- this.baseClass = baseClass;
- this.methodName = methodName;
- this.parameters = parameters;
- }
-
- ///<summary>
- /// Run this compiler step
- ///</summary>
- public override void Run()
- {
- if (Context.References.Contains(baseClass.Assembly) == false)
- Context.Parameters.References.Add(baseClass.Assembly);
-
- foreach (Module module in CompileUnit.Modules)
- {
- foreach (string ns in namespaces)
- {
- module.Imports.Add(new Import(module.LexicalInfo, ns));
- }
-
- ClassDefinition definition = new ClassDefinition();
- definition.Name = module.FullName;
- definition.BaseTypes.Add(new SimpleTypeReference(baseClass.FullName));
- Method method = new Method(methodName);
-
-
- if (parameters != null)
- {
- foreach (ParameterDeclaration parameter in parameters)
- {
- method.Parameters.Add(parameter);
- }
- }
-
- method.Body = module.Globals;
- module.Globals = new Block();
- definition.Members.Add(method);
- module.Members.Add(definition);
-
- GenerateConstructors(definition);
-
- ExtendBaseClass(definition);
- }
- }
-
- private void GenerateConstructors(TypeDefinition definition)
- {
- ConstructorInfo[] ctors =
- baseClass.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- foreach (ConstructorInfo ctor in ctors)
- {
- if (ctor.IsPrivate)
- continue;
- Constructor constructor = new Constructor(definition.LexicalInfo);
- definition.Members.Add(constructor);
- MethodInvocationExpression super = new MethodInvocationExpression(new SuperLiteralExpression());
- constructor.Body.Add(super);
- foreach (ParameterInfo info in ctor.GetParameters())
- {
- SimpleTypeReference typeReference =
- new SimpleTypeReference(TypeUtilities.GetFullName(info.ParameterType));
- constructor.Parameters.Add(new ParameterDeclaration(info.Name,
- typeReference)
- );
- super.Arguments.Add(new ReferenceExpression(info.Name));
- }
- }
- }
-
- /// <summary>
- /// Allow a derived class to perform additional operations on the newly created type definition.
- /// </summary>
- protected virtual void ExtendBaseClass(TypeDefinition definition)
- {
- }
- }
-}
Oops, something went wrong.

0 comments on commit 33b1e5f

Please sign in to comment.