示例#1
0
        public void CancelEscrowPreventsOutputOfPendingLine()
        {
            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            source
            .WriteLine()
            .WriteLine("begin")
            .AddIndent()
            .WriteLine("if")
            .AddIndent()
            .WriteLine("do this")
            .RemoveIndent()
            .EscrowLine("endif")
            .ClearEscrowLine()
            .WriteLine("else")
            .AddIndent()
            .WriteLine("do that")
            .RemoveIndent()
            .EscrowLine("endif")
            .RemoveIndent()
            .WriteLine("done");
            Assert.That(source.ToString(), Is.EqualTo(@"
begin
    if
        do this
    else
        do that
    endif
done
"));
        }
示例#2
0
        public void EscrowLineWritesFirstAtIndentationWhenItWasAdded()
        {
            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            source.WriteLine().AddIndent();

            source
            .WriteLine("one")
            .AddIndent()
            .WriteLine("two")
            .EscrowLine("two-b")
            .RemoveIndent()
            .WriteLine("three")
            .RemoveIndent();

            var expected = new StringBuilder()
                           .AppendLine()
                           .AppendLine("    one")
                           .AppendLine("        two")
                           .AppendLine("        two-b")
                           .AppendLine("    three");

            Assert.That(source.ToString(), Is.EqualTo(expected.ToString()));
        }
示例#3
0
        private Method BuildMethod(MethodConfig methodConfig)
        {
            var w = new SourceWriter();

            MethodDef def = this;

            var builder = new MethodRunner(methodConfig, w);

            builder.WriteMethod();

            return(new Method(methodConfig.Method.Name, methodConfig.MethodType, w.ToString()));
        }
示例#4
0
        public string BaseWithNoArguments()
        {
            var methodCall = MethodCall.For <MethodCallBenchmarks>(b => b.AnExampleMethod());

            methodCall.Target = new Variable(typeof(MethodCallBenchmarks));

            generatedMethod.Frames.Add(methodCall);

            generatedMethod.WriteMethod(sourceWriter);

            return(sourceWriter.ToString());
        }
示例#5
0
        public void IndentationShouldAddLeadingSpace()
        {
            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            source
            .WriteLine()
            .WriteLine("one")
            .AddIndent()
            .WriteLine("two")
            .RemoveIndent()
            .WriteLine("three");
            Assert.That(source.ToString(), Is.EqualTo(@"
one
    two
three
"));
        }
示例#6
0
        private void VerifyObjectMethod(string regionName, MethodType type, string expected)
        {
            var doc    = GetDocument();
            var config = GetDocumentConfig(doc);

            var methodImpl = GetObjectMethod(doc);
            var template   = GetResolvedTemplate(methodImpl, config);

            var region    = template.Regions.First(r => r.Name == regionName);
            var methodDef = region.MethodDefs.First();

            var writer = new SourceWriter();

            var builder = new MethodRunner(new MethodConfig(methodImpl, methodDef, type, config), writer);

            builder.WriteMethod();

            expected = expected.TrimStart('\r', '\n');

            Assert.AreEqual(expected, writer.ToString());
        }
示例#7
0
        public void Execute(GeneratorExecutionContext context)
        {
            var effectFiles  = new List <string>();
            var contentFiles = context.AdditionalFiles.Where(file => file.Path.EndsWith(".mgcb")).ToList().AsReadOnly();

            try
            {
                foreach (var file in contentFiles)
                {
                    var directory = Path.GetDirectoryName(file.Path);
                    var content   = file.GetText(context.CancellationToken);

                    var parser = new MGCBParser();
                    parser.Parse(content.Lines);

                    var effects = parser.Blocks.Where(b => b.Importer.Argument.Equals("EffectImporter"));
                    foreach (var effect in effects)
                    {
                        var path = Path.GetFullPath(Path.Combine(directory, effect.Build.Argument));
                        effectFiles.Add(path);
                    }
                }

                var generator = new EffectWrapperGenerator();
                foreach (var effectFile in effectFiles)
                {
                    var effect     = new Effect(effectFile);
                    var sourceFile = generator.Generate(effect);
                    var sourceText = SourceWriter.ToString(sourceFile);

                    context.AddSource(sourceFile.FileName, sourceText);
                }

                Report(context, "GENG01", $"Generated {effectFiles.Count} effect wrappers", DiagnosticSeverity.Warning);
            }
            catch (Exception ex)
            {
                Report(context, "GENG0X", $"Error: {ex.Message}", DiagnosticSeverity.Error);
            }
        }
示例#8
0
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            var globalSymbols = new Dictionary <string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            // debug symbols not adjusted until the matching-directive issue resolved
            source.AdjustDebugSymbols = false;

            var usingGenerator     = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor {
                BaseClass = BaseClass
            };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);

            // needed for proper vb functionality
            source.WriteLine("Option Infer On");

            usingGenerator.UsingNamespace("Microsoft.VisualBasic");
            foreach (var ns in UseNamespaces ?? new string[0])
            {
                usingGenerator.UsingNamespace(ns);
            }

            usingGenerator.UsingAssembly("Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            foreach (var assembly in UseAssemblies ?? new string[0])
            {
                usingGenerator.UsingAssembly(assembly);
            }

            foreach (var resource in allResources)
            {
                usingGenerator.Accept(resource);
            }

            foreach (var resource in allResources)
            {
                baseClassGenerator.Accept(resource);
            }

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                source.WriteLine();
                source.WriteLine(string.Format("Namespace {0}", TargetNamespace));
            }

            source.WriteLine();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.WriteLine("<Global.Spark.SparkViewAttribute( _");
                if (TargetNamespace != null)
                {
                    source.WriteFormat("    TargetNamespace:=\"{0}\", _", TargetNamespace).WriteLine();
                }
                source.WriteLine("    Templates := New String() { _");
                source.Write("      ").Write(string.Join(", _\r\n      ",
                                                         Descriptor.Templates.Select(
                                                             t => "\"" + t + "\"").ToArray()));
                source.WriteLine("    })> _");
            }

            // public class ViewName : BasePageType
            source
            .Write("Public Class ")
            .WriteLine(viewClassName)
            .Write("    Inherits ")
            .WriteLine(baseClassGenerator.BaseClassTypeName)
            .AddIndent();

            source.WriteLine();
            source.WriteLine(string.Format("    Private Shared ReadOnly _generatedViewId As Global.System.Guid = New Global.System.Guid(\"{0:n}\")", GeneratedViewId));


            source
            .WriteLine("    Public Overrides ReadOnly Property GeneratedViewId() As Global.System.Guid")
            .WriteLine("      Get")
            .WriteLine("        Return _generatedViewId")
            .WriteLine("      End Get")
            .WriteLine("    End Property");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                //TODO: correct this
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("    public ").WriteLine(accessor.Property);
                    source.Write("    { get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            foreach (var resource in allResources)
            {
                globalsGenerator.Accept(resource);
            }

            // public void RenderViewLevelx()
            int renderLevel = 0;

            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                EditorBrowsableStateNever(source, 4);
                source
                .WriteLine("Private Sub RenderViewLevel{0}()", renderLevel)
                .AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source
                .RemoveIndent()
                .WriteLine("End Sub");
                ++renderLevel;
            }

            // public void RenderView()
            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source
            .WriteLine("Public Overrides Sub Render()")
            .AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)
            {
                if (invokeLevel != renderLevel - 1)
                {
                    source
                    .WriteLine("Using OutputScope()")
                    .AddIndent()
                    .WriteLine("RenderViewLevel{0}()", invokeLevel)
                    .WriteLine("Content(\"view\") = Output")
                    .RemoveIndent()
                    .WriteLine("End Using");
                }
                else
                {
                    source
                    .WriteLine("RenderViewLevel{0}()", invokeLevel);
                }
            }
            source
            .RemoveIndent()
            .WriteLine("End Sub");


            source
            .RemoveIndent()
            .WriteLine("End Class");

            if (!string.IsNullOrEmpty(TargetNamespace))
            {
                source.WriteLine("End Namespace");
            }

            SourceCode     = source.ToString();
            SourceMappings = source.Mappings;
        }
示例#9
0
        public override void GenerateSourceCode(IEnumerable<IList<Chunk>> viewTemplates, IEnumerable<IList<Chunk>> allResources)
        {
            var globalSymbols = new Dictionary<string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            // debug symbols not adjusted until the matching-directive issue resolved
            source.AdjustDebugSymbols = false;

            var usingGenerator = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor { BaseClass = BaseClass };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);

            // needed for proper vb functionality
            source.WriteLine("Option Infer On");

            usingGenerator.UsingNamespace("Microsoft.VisualBasic");
            foreach (var ns in UseNamespaces ?? new string[0])
                usingGenerator.UsingNamespace(ns);

            usingGenerator.UsingAssembly("Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            foreach (var assembly in UseAssemblies ?? new string[0])
                usingGenerator.UsingAssembly(assembly);

            foreach (var resource in allResources)
                usingGenerator.Accept(resource);

            foreach (var resource in allResources)
                baseClassGenerator.Accept(resource);

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                source.WriteLine();
                source.WriteLine(string.Format("Namespace {0}", TargetNamespace));
            }

            source.WriteLine();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.WriteLine("<Global.Spark.SparkViewAttribute( _");
                if (TargetNamespace != null)
                    source.WriteFormat("    TargetNamespace:=\"{0}\", _", TargetNamespace).WriteLine();
                source.WriteLine("    Templates := New String() { _");
                source.Write("      ").Write(string.Join(", _\r\n      ",
                    Descriptor.Templates.Select(t => "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray()));

                source.WriteLine("    })> _");
            }

            // public class ViewName : BasePageType
            source
                .Write("Public Class ")
                .WriteLine(viewClassName)
                .Write("    Inherits ")
                .WriteLine(baseClassGenerator.BaseClassTypeName)
                .AddIndent();

            source.WriteLine();
            source.WriteLine(string.Format("    Private Shared ReadOnly _generatedViewId As Global.System.Guid = New Global.System.Guid(\"{0:n}\")", GeneratedViewId));

            source
                .WriteLine("    Public Overrides ReadOnly Property GeneratedViewId() As Global.System.Guid")
                .WriteLine("      Get")
                .WriteLine("        Return _generatedViewId")
                .WriteLine("      End Get")
                .WriteLine("    End Property");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                //TODO: correct this
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("    public ").WriteLine(accessor.Property);
                    source.Write("    { get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            foreach (var resource in allResources)
                globalsGenerator.Accept(resource);

            // public void RenderViewLevelx()
            int renderLevel = 0;
            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                EditorBrowsableStateNever(source, 4);
                source
                    .WriteLine("Private Sub RenderViewLevel{0}()", renderLevel)
                    .AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source
                    .RemoveIndent()
                    .WriteLine("End Sub");
                ++renderLevel;
            }

            // public void RenderView()
            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source
                .WriteLine("Public Overrides Sub Render()")
                .AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)
            {
                if (invokeLevel != renderLevel - 1)
                {
                    source
                        .WriteLine("Using OutputScope()")
                        .AddIndent()
                        .WriteLine("RenderViewLevel{0}()", invokeLevel)
                        .WriteLine("Content(\"view\") = Output")
                        .RemoveIndent()
                        .WriteLine("End Using");
                }
                else
                {
                    source
                        .WriteLine("RenderViewLevel{0}()", invokeLevel);
                }
            }
            source
                .RemoveIndent()
                .WriteLine("End Sub");

            source
                .RemoveIndent()
                .WriteLine("End Class");

            if (!string.IsNullOrEmpty(TargetNamespace))
            {
                source.WriteLine("End Namespace");
            }

            SourceCode = source.ToString();
            SourceMappings = source.Mappings;
        }
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            Dictionary <string, object> globalSymbols = new Dictionary <string, object>();
            StringWriter          writer   = new StringWriter();
            SourceWriter          output   = new SourceWriter(writer);
            UsingNamespaceVisitor visitor  = new UsingNamespaceVisitor(output);
            BaseClassVisitor      visitor2 = new BaseClassVisitor {
                BaseClass = base.BaseClass
            };
            GlobalMembersVisitor visitor3 = new GlobalMembersVisitor(output, globalSymbols, base.NullBehaviour);

            foreach (string str in base.UseNamespaces ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingNamespace(str);
            }
            foreach (string str2 in base.UseAssemblies ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingAssembly(str2);
            }
            foreach (IList <Chunk> list in allResources)
            {
                visitor.Accept(list);
            }
            foreach (IList <Chunk> list2 in allResources)
            {
                visitor2.Accept(list2);
            }
            string str3 = "View" + base.GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(base.TargetNamespace))
            {
                base.ViewClassFullName = str3;
            }
            else
            {
                base.ViewClassFullName = base.TargetNamespace + "." + str3;
                output.WriteLine().WriteLine(string.Format("namespace {0}", base.TargetNamespace)).WriteLine("{").AddIndent();
            }
            output.WriteLine();
            if (base.Descriptor != null)
            {
                output.WriteLine("[global::Spark.SparkViewAttribute(");
                if (base.TargetNamespace != null)
                {
                    output.WriteFormat("    TargetNamespace=\"{0}\",", new object[] { base.TargetNamespace }).WriteLine();
                }
                output.WriteLine("    Templates = new string[] {");
                output.Write("      ").WriteLine(string.Join(",\r\n      ", (from t in base.Descriptor.Templates select "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray <string>()));
                output.WriteLine("    })]");
            }
            output.Write("public class ").Write(str3).Write(" : ").WriteCode(visitor2.BaseClassTypeName).WriteLine();
            output.WriteLine("{").AddIndent();
            output.WriteLine();
            EditorBrowsableStateNever(output, 4);
            output.WriteLine("private static System.Guid _generatedViewId = new System.Guid(\"{0:n}\");", new object[] { base.GeneratedViewId });
            output.WriteLine("public override System.Guid GeneratedViewId");
            output.WriteLine("{ get { return _generatedViewId; } }");
            if ((base.Descriptor != null) && (base.Descriptor.Accessors != null))
            {
                foreach (SparkViewDescriptor.Accessor accessor in base.Descriptor.Accessors)
                {
                    output.WriteLine();
                    output.Write("public ").WriteLine(accessor.Property);
                    output.Write("{ get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }
            foreach (IList <Chunk> list3 in allResources)
            {
                visitor3.Accept(list3);
            }
            int num = 0;

            foreach (IList <Chunk> list4 in viewTemplates)
            {
                output.WriteLine();
                EditorBrowsableStateNever(output, 4);
                output.WriteLine(string.Format("private void RenderViewLevel{0}()", num));
                output.WriteLine("{").AddIndent();
                new GeneratedCodeVisitor(output, globalSymbols, base.NullBehaviour).Accept(list4);
                output.RemoveIndent().WriteLine("}");
                num++;
            }
            output.WriteLine();
            EditorBrowsableStateNever(output, 4);
            output.WriteLine("public override void Render()");
            output.WriteLine("{").AddIndent();
            for (int i = 0; i != num; i++)
            {
                if (i != (num - 1))
                {
                    output.WriteLine("using (OutputScope()) {{RenderViewLevel{0}(); Content[\"view\"] = Output;}}", new object[] { i });
                }
                else
                {
                    output.WriteLine("        RenderViewLevel{0}();", new object[] { i });
                }
            }
            output.RemoveIndent().WriteLine("}");
            output.RemoveIndent().WriteLine("}");
            if (!string.IsNullOrEmpty(base.TargetNamespace))
            {
                output.RemoveIndent().WriteLine("}");
            }
            base.SourceCode     = output.ToString();
            base.SourceMappings = output.Mappings;
        }
示例#11
0
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            var globalSymbols = new Dictionary <string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            var usingGenerator     = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor {
                BaseClass = BaseClass
            };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);



            // using <namespaces>;
            foreach (var ns in UseNamespaces ?? new string[0])
            {
                usingGenerator.UsingNamespace(ns);
            }

            foreach (var assembly in UseAssemblies ?? new string[0])
            {
                usingGenerator.UsingAssembly(assembly);
            }

            foreach (var resource in allResources)
            {
                usingGenerator.Accept(resource);
            }

            foreach (var resource in allResources)
            {
                baseClassGenerator.Accept(resource);
            }

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                source
                .WriteLine()
                .WriteLine(string.Format("namespace {0}", TargetNamespace))
                .WriteLine("{").AddIndent();
            }

            source.WriteLine();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.WriteLine("[global::Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                {
                    source.WriteFormat("    TargetNamespace=\"{0}\",", TargetNamespace).WriteLine();
                }
                source.WriteLine("    Templates = new string[] {");
                source.Write("      ").WriteLine(string.Join(",\r\n      ",
                                                             Descriptor.Templates.Select(
                                                                 t => "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray()));
                source.WriteLine("    })]");
            }

            // public class ViewName : BasePageType
            source
            .Write("public class ")
            .Write(viewClassName)
            .Write(" : ")
            .WriteCode(baseClassGenerator.BaseClassTypeName)
            .WriteLine();
            source.WriteLine("{").AddIndent();

            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("private static System.Guid _generatedViewId = new System.Guid(\"{0:n}\");", GeneratedViewId);
            source.WriteLine("public override System.Guid GeneratedViewId");
            source.WriteLine("{ get { return _generatedViewId; } }");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("public ").WriteLine(accessor.Property);
                    source.Write("{ get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            foreach (var resource in allResources)
            {
                globalsGenerator.Accept(resource);
            }

            // public void RenderViewLevelx()
            int renderLevel = 0;

            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                EditorBrowsableStateNever(source, 4);
                source.WriteLine(string.Format("private void RenderViewLevel{0}()", renderLevel));
                source.WriteLine("{").AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source.RemoveIndent().WriteLine("}");
                ++renderLevel;
            }

            // public void RenderView()

            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("public override void Render()");
            source.WriteLine("{").AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)

            {
                if (invokeLevel != renderLevel - 1)
                {
                    source.WriteLine("using (OutputScope()) {{DelegateFirstRender(RenderViewLevel{0}); Content[\"view\"] = Output;}}", invokeLevel);
                }
                else
                {
                    if (renderLevel <= 1)
                    {
                        source.WriteLine("        DelegateFirstRender(RenderViewLevel{0});", invokeLevel);
                    }
                    else
                    {
                        source.WriteLine("        RenderViewLevel{0}();", invokeLevel);
                    }
                }
            }
            source.RemoveIndent().WriteLine("}");

            // end class
            source.RemoveIndent().WriteLine("}");

            if (!string.IsNullOrEmpty(TargetNamespace))
            {
                source.RemoveIndent().WriteLine("}");
            }

            SourceCode     = source.ToString();
            SourceMappings = source.Mappings;
        }
示例#12
0
        public override void GenerateSourceCode(IEnumerable<IList<Chunk>> viewTemplates, IEnumerable<IList<Chunk>> allResources)
        {
            var script = new SourceWriter();
            var globals = new Dictionary<string, object>();

            var globalMembersVisitor = new GlobalMembersVisitor(script, globals);
            foreach(var resource in allResources)
                globalMembersVisitor.Accept(resource);

            var globalFunctionsVisitor = new GlobalFunctionsVisitor(script, globals);
            foreach (var resource in allResources)
                globalFunctionsVisitor.Accept(resource);

            var templateIndex = 0;
            foreach (var template in viewTemplates)
            {
                script.Write("def RenderViewLevel").Write(templateIndex).WriteLine("():");
                script.Indent++;
                foreach (var global in globals.Keys)
                    script.Write("global ").WriteLine(global);
                var generator = new GeneratedCodeVisitor(script, globals);
                generator.Accept(template);
                script.Indent--;
                script.WriteLine();
                templateIndex++;
            }

            for (var renderIndex = 0; renderIndex != templateIndex; ++renderIndex)
            {
                if (renderIndex < templateIndex - 1)
                {
                    script.WriteLine("scope=OutputScopeAdapter(None)");
                    script.Write("RenderViewLevel").Write(renderIndex).WriteLine("()");
                    script.WriteLine("Content[\"view\"] = Output");
                    script.WriteLine("scope.Dispose()");
                }
                else
                {
                    script.Write("RenderViewLevel").Write(renderIndex).WriteLine("()");
                }
            }

            var baseClassGenerator = new BaseClassVisitor { BaseClass = BaseClass };
            foreach (var resource in allResources)
                baseClassGenerator.Accept(resource);

            BaseClass = baseClassGenerator.BaseClassTypeName;

            var source = new StringBuilder();

            var viewClassName = "View" + GeneratedViewId.ToString("n");
            if (Descriptor != null && !string.IsNullOrEmpty(Descriptor.TargetNamespace))
            {
                ViewClassFullName = Descriptor.TargetNamespace + "." + viewClassName;
                source.Append("namespace ").AppendLine(Descriptor.TargetNamespace);
                source.AppendLine("{");
            }
            else
            {
                ViewClassFullName = viewClassName;
            }

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.AppendLine("[global::Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                    source.AppendFormat("    TargetNamespace=\"{0}\",", TargetNamespace).AppendLine();
                source.AppendLine("    Templates = new string[] {");
                source.Append("      ").AppendLine(string.Join(",\r\n      ",
                                                               Descriptor.Templates.Select(
                                                                   t => "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray()));
                source.AppendLine("    })]");
            }

            source.Append("public class ").Append(viewClassName).Append(" : ").Append(BaseClass).AppendLine(", global::Spark.Python.IScriptingSparkView");
            source.AppendLine("{");

            source.Append("static System.Guid _generatedViewId = new System.Guid(\"").Append(GeneratedViewId).AppendLine("\");");
            source.AppendLine("public override System.Guid GeneratedViewId");
            source.AppendLine("{");
            source.AppendLine("get { return _generatedViewId; }");
            source.AppendLine("}");

            source.AppendLine("public global::System.IDisposable OutputScopeAdapter(object arg) ");
            source.AppendLine("{");
            source.AppendLine("if (arg == null) return OutputScope();");
            source.AppendLine("if (arg is string) return OutputScope((string)arg);");
            source.AppendLine("if (arg is global::System.IO.TextWriter) return OutputScope((global::System.IO.TextWriter)arg);");
            source.AppendLine("throw new global::Spark.Compiler.CompilerException(\"Invalid argument for OutputScopeAdapter\");");
            source.AppendLine("}");

            source.AppendLine("public void OutputWriteAdapter(object arg) ");
            source.AppendLine("{");
            source.AppendLine("Output.Write(arg);");
            source.AppendLine("}");

            source.AppendLine("public global::Microsoft.Scripting.Hosting.CompiledCode CompiledCode {get;set;}");

            source.AppendLine("public string ScriptSource");
            source.AppendLine("{");
            source.Append("get { return @\"").Append(script.ToString().Replace("\"", "\"\"")).AppendLine("\"; }");
            source.AppendLine("}");

            source.AppendLine("public override void Render()");
            source.AppendLine("{");
            source.AppendLine("CompiledCode.Execute(");
            source.AppendLine("CompiledCode.Engine.CreateScope(");
            source.AppendLine("new global::Spark.Python.ScriptingViewSymbolDictionary(this)");
            source.AppendLine("));");
            source.AppendLine("}");

            source.AppendLine("}");

            if (Descriptor != null && !string.IsNullOrEmpty(Descriptor.TargetNamespace))
            {
                source.AppendLine("}");
            }

            SourceCode = source.ToString();
        }
示例#13
0
        public override void GenerateSourceCode(IEnumerable<IList<Chunk>> viewTemplates, IEnumerable<IList<Chunk>> allResources)
        {
            var globalSymbols = new Dictionary<string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            var usingGenerator = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor { BaseClass = BaseClass };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);
            var globalsImplementationGenerator = new GlobalMembersImplementationVisitor(source, globalSymbols, NullBehaviour);

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                source.WriteLine("namespace ;");
            }
            else
            {
                source.WriteLine(string.Format("namespace {0};", TargetNamespace));
            }

            source.WriteLine("interface");

            // using <namespaces>;

            if (UseNamespaces != null)
            {
                foreach (var ns in UseNamespaces ?? new string[0])
                {
                    usingGenerator.UsingNamespace(ns);
                }

                if (usingGenerator._namespaceAdded.Count > 0)
                {
                    source.Write(";").WriteLine("");
                }
            }

            //foreach (var ns in UseNamespaces ?? new string[0])
            //{
            //    usingGenerator.UsingNamespace(ns);
            //}

            foreach (var assembly in UseAssemblies ?? new string[0])
            {
                usingGenerator.UsingAssembly(assembly);
            }

            foreach (var resource in allResources)
            {
                usingGenerator.Accept(resource);
            }

            foreach (var resource in allResources)
            {
                baseClassGenerator.Accept(resource);
            }

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                //source
                //    .WriteLine()
                //    .WriteLine(string.Format("namespace {0}", TargetNamespace))
                //    .WriteLine("{").AddIndent();
            }

            source.WriteLine();

            var descriptorText = new StringBuilder();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                descriptorText.Append("[Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                    descriptorText.Append(String.Format("    TargetNamespace:=\"{0}\",", TargetNamespace));
                descriptorText.Append("    Templates := array of System.String ([");
                descriptorText.Append("      ");
                descriptorText.Append(string.Join(",\r\n      ",
                                                               Descriptor.Templates.Select(
                                                                   //t => "\"" + t.Replace("\\", "\\\\") + "\"").ToArray()));
                                                                   t => "'" + t+ "'").ToArray()));
                descriptorText.Append("    ]))]");
            }

            // public class ViewName : BasePageType
            source
                .WriteLine("type ")
                .WriteLine(descriptorText.ToString())
                .Write(viewClassName)
                .Write(" = public class (")
                .WriteCode(baseClassGenerator.BaseClassTypeName)
                .Write(")")
                .WriteLine();

            source.WriteLine();
            source.WriteLine("private class var ");
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("_generatedViewId : System.Guid  := new System.Guid('{0:n}');", GeneratedViewId);

            source.WriteLine("public property GeneratedViewId : System.Guid ");
            source.WriteLine("read _generatedViewId; override;");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("public ").WriteLine(accessor.Property);
                    source.Write("{ get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            // Note: macros are methods, implementation is delegated to later on...
            foreach (var resource in allResources)
            {
                globalsGenerator.Accept(resource);
            }

            // public void RenderViewLevelx()
            int renderLevel = 0;
            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                source.WriteLine("public");
                EditorBrowsableStateNever(source, 4);
                source.WriteLine(string.Format("method RenderViewLevel{0};", renderLevel));
                ++renderLevel;
            }

            // public void RenderView()

            source.WriteLine();
            source.WriteLine("public");
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("method Render; override;");
            source.WriteLine();

            source.WriteLine("end;");

            source.WriteLine("");

            source.WriteLine("implementation");

            globalsImplementationGenerator.ViewClassName = viewClassName;

            foreach (var resource in allResources)
            {
                globalsImplementationGenerator.Accept(resource);
            }

            // public void RenderViewLevelx()
            renderLevel = 0;
            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                source.WriteLine(string.Format("method {0}.RenderViewLevel{1}();", viewClassName, renderLevel));
                source.WriteLine("begin").AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source.RemoveIndent().WriteLine("end;");
                ++renderLevel;
            }

            // public void RenderView()

            source.WriteLine();
            source.WriteLine(string.Format("method {0}.Render();", viewClassName));
            source.WriteLine("begin").AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)
            {
                if (invokeLevel != renderLevel - 1)
                {
                    source.WriteLine("using OutputScope do begin RenderViewLevel{0}(); Content['view'] := Output; end;", invokeLevel);
                }
                else
                {

                    source.WriteLine("        RenderViewLevel{0};", invokeLevel);
                }
            }
            source.RemoveIndent().WriteLine("end;");

            // end class
            source.WriteLine("end.");

            SourceCode = source.ToString();
            SourceMappings = source.Mappings;
        }
 public override void Write(string outputDirectory)
 {
     WriteFile(Path.Combine(outputDirectory, "bindings.h"), headers.ToString());
     WriteFile(Path.Combine(outputDirectory, "bindings.m"), implementation.ToString());
 }
示例#15
0
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            Dictionary <string, object> globalSymbols = new Dictionary <string, object>();
            StringWriter writer = new StringWriter();
            SourceWriter output = new SourceWriter(writer)
            {
                AdjustDebugSymbols = false
            };
            UsingNamespaceVisitor visitor  = new UsingNamespaceVisitor(output);
            BaseClassVisitor      visitor2 = new BaseClassVisitor {
                BaseClass = base.BaseClass
            };
            GlobalMembersVisitor visitor3 = new GlobalMembersVisitor(output, globalSymbols, base.NullBehaviour);

            output.WriteLine("Option Infer On");
            visitor.UsingNamespace("Microsoft.VisualBasic");
            foreach (string str in base.UseNamespaces ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingNamespace(str);
            }
            visitor.UsingAssembly("Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            foreach (string str2 in base.UseAssemblies ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingAssembly(str2);
            }
            foreach (IList <Chunk> list in allResources)
            {
                visitor.Accept(list);
            }
            foreach (IList <Chunk> list2 in allResources)
            {
                visitor2.Accept(list2);
            }
            string str3 = "View" + base.GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(base.TargetNamespace))
            {
                base.ViewClassFullName = str3;
            }
            else
            {
                base.ViewClassFullName = base.TargetNamespace + "." + str3;
                output.WriteLine();
                output.WriteLine(string.Format("Namespace {0}", base.TargetNamespace));
            }
            output.WriteLine();
            if (base.Descriptor != null)
            {
                output.WriteLine("<Global.Spark.SparkViewAttribute( _");
                if (base.TargetNamespace != null)
                {
                    output.WriteFormat("    TargetNamespace:=\"{0}\", _", new object[] { base.TargetNamespace }).WriteLine();
                }
                output.WriteLine("    Templates := New String() { _");
                output.Write("      ").Write(string.Join(", _\r\n      ", (from t in base.Descriptor.Templates select "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray <string>()));
                output.WriteLine("    })> _");
            }
            output.Write("Public Class ").WriteLine(str3).Write("    Inherits ").WriteLine((string)visitor2.BaseClassTypeName).AddIndent();
            output.WriteLine();
            output.WriteLine(string.Format("    Private Shared ReadOnly _generatedViewId As Global.System.Guid = New Global.System.Guid(\"{0:n}\")", base.GeneratedViewId));
            output.WriteLine("    Public Overrides ReadOnly Property GeneratedViewId() As Global.System.Guid").WriteLine("      Get").WriteLine("        Return _generatedViewId").WriteLine("      End Get").WriteLine("    End Property");
            if ((base.Descriptor != null) && (base.Descriptor.Accessors != null))
            {
                foreach (SparkViewDescriptor.Accessor accessor in base.Descriptor.Accessors)
                {
                    output.WriteLine();
                    output.Write("    public ").WriteLine(accessor.Property);
                    output.Write("    { get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }
            foreach (IList <Chunk> list3 in allResources)
            {
                visitor3.Accept(list3);
            }
            int num = 0;

            foreach (IList <Chunk> list4 in viewTemplates)
            {
                output.WriteLine();
                EditorBrowsableStateNever(output, 4);
                output.WriteLine("Private Sub RenderViewLevel{0}()", new object[] { num }).AddIndent();
                new GeneratedCodeVisitor(output, globalSymbols, base.NullBehaviour).Accept(list4);
                output.RemoveIndent().WriteLine("End Sub");
                num++;
            }
            output.WriteLine();
            EditorBrowsableStateNever(output, 4);
            output.WriteLine("Public Overrides Sub Render()").AddIndent();
            for (int i = 0; i != num; i++)
            {
                if (i != (num - 1))
                {
                    output.WriteLine("Using OutputScope()").AddIndent().WriteLine("RenderViewLevel{0}()", new object[] { i }).WriteLine("Content(\"view\") = Output").RemoveIndent().WriteLine("End Using");
                }
                else
                {
                    output.WriteLine("RenderViewLevel{0}()", new object[] { i });
                }
            }
            output.RemoveIndent().WriteLine("End Sub");
            output.RemoveIndent().WriteLine("End Class");
            if (!string.IsNullOrEmpty(base.TargetNamespace))
            {
                output.WriteLine("End Namespace");
            }
            base.SourceCode     = output.ToString();
            base.SourceMappings = output.Mappings;
        }
示例#16
0
        public override void GenerateSourceCode(IEnumerable<IList<Chunk>> viewTemplates, IEnumerable<IList<Chunk>> allResources)
        {
            var globalSymbols = new Dictionary<string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            var usingGenerator = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor { BaseClass = BaseClass };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);

            // using <namespaces>;
            foreach (var ns in UseNamespaces ?? new string[0])
                usingGenerator.UsingNamespace(ns);

            foreach (var assembly in UseAssemblies ?? new string[0])
                usingGenerator.UsingAssembly(assembly);

            foreach (var resource in allResources)
                usingGenerator.Accept(resource);

            foreach (var resource in allResources)
                baseClassGenerator.Accept(resource);

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                source
                    .WriteLine()
                    .WriteLine(string.Format("namespace {0}", TargetNamespace))
                    .WriteLine("{").AddIndent();
            }

            source.WriteLine();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.WriteLine("[global::Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                    source.WriteFormat("    TargetNamespace=\"{0}\",", TargetNamespace).WriteLine();
                source.WriteLine("    Templates = new string[] {");
                source.Write("      ").WriteLine(string.Join(",\r\n      ",
                                                               Descriptor.Templates.Select(
                                                                   t => "\"" + t.Replace("\\", "\\\\") + "\"").ToArray()));
                source.WriteLine("    })]");
            }

            // public class ViewName : BasePageType
            source
                .Write("public class ")
                .Write(viewClassName)
                .Write(" : ")
                .WriteCode(baseClassGenerator.BaseClassTypeName)
                .WriteLine();
            source.WriteLine("{").AddIndent();

            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("private static System.Guid _generatedViewId = new System.Guid(\"{0:n}\");", GeneratedViewId);
            source.WriteLine("public override System.Guid GeneratedViewId");
            source.WriteLine("{ get { return _generatedViewId; } }");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("public ").WriteLine(accessor.Property);
                    source.Write("{ get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            foreach (var resource in allResources)
                globalsGenerator.Accept(resource);

            // public void RenderViewLevelx()
            int renderLevel = 0;
            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                EditorBrowsableStateNever(source, 4);
                source.WriteLine(string.Format("private void RenderViewLevel{0}()", renderLevel));
                source.WriteLine("{").AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source.RemoveIndent().WriteLine("}");
                ++renderLevel;
            }

            // public void RenderView()

            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("public override void Render()");
            source.WriteLine("{").AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)

            {
                if (invokeLevel != renderLevel - 1)
                {
                    source.WriteLine("using (OutputScope()) {{RenderViewLevel{0}(); Content[\"view\"] = Output;}}", invokeLevel);
                }
                else
                {

                    source.WriteLine("        RenderViewLevel{0}();", invokeLevel);
                }
            }
            source.RemoveIndent().WriteLine("}");

            // end class
            source.RemoveIndent().WriteLine("}");

            if (!string.IsNullOrEmpty(TargetNamespace))
            {
                source.RemoveIndent().WriteLine("}");
            }

            SourceCode = source.ToString();
            SourceMappings = source.Mappings;
        }
示例#17
0
        public override void GenerateSourceCode(IEnumerable<IList<Chunk>> viewTemplates, IEnumerable<IList<Chunk>> allResources)
        {
            var script = new SourceWriter();
            var globals = new Dictionary<string, object>();

            script.WriteLine(ScriptHeader);

            script.WriteLine("class<<view");
            script.Indent++;

            var globalMembersVisitor = new GlobalMembersVisitor(script, globals);
            foreach (var resource in allResources)
                globalMembersVisitor.Accept(resource);

            var globalFunctionsVisitor = new GlobalFunctionsVisitor(script, globals);
            foreach (var resource in allResources)
                globalFunctionsVisitor.Accept(resource);

            var templateIndex = 0;
            foreach (var template in viewTemplates)
            {
                script.Write("def render_view_level").WriteLine(templateIndex);
                script.Indent++;

                var generator = new GeneratedCodeVisitor(script, globals);
                generator.Accept(template);

                script.Indent--;
                script.WriteLine("end");

                templateIndex++;
            }

            script.WriteLine("def render");
            script.Indent++;

            var globalInitializeVisitor = new GlobalInitializeVisitor(script);
            foreach(var resource in allResources)
                globalInitializeVisitor.Accept(resource);

            for (var renderIndex = 0; renderIndex != templateIndex; ++renderIndex)
            {
                if (renderIndex < templateIndex - 1)
                {
                    script.WriteLine("scope = output_scope");
                    script. Write("render_view_level").WriteLine(renderIndex);
                    script.WriteLine("content.set_Item \"view\", output");
                    script.WriteLine("scope.dispose");
                }
                else
                {
                    script.Write("render_view_level").WriteLine(renderIndex);
                }
            }
            script.Indent--;
            script.WriteLine("end");

            script.Indent--;
            script.WriteLine("end");
            script.WriteLine("view.view_data.each {|kv| view.instance_variable_set \"@\"+kv.key, kv.value}");
            script.WriteLine("view.render");

            var baseClassGenerator = new BaseClassVisitor { BaseClass = BaseClass };
            foreach (var resource in allResources)
                baseClassGenerator.Accept(resource);

            BaseClass = baseClassGenerator.BaseClassTypeName;

            var source = new StringBuilder();

            var viewClassName = "View" + GeneratedViewId.ToString("n");
            if (Descriptor != null && !string.IsNullOrEmpty(Descriptor.TargetNamespace))
            {
                ViewClassFullName = Descriptor.TargetNamespace + "." + viewClassName;
                source.Append("namespace ").AppendLine(Descriptor.TargetNamespace);
                source.AppendLine("{");
            }
            else
            {
                ViewClassFullName = viewClassName;
            }

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.AppendLine("[global::Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                    source.AppendFormat("    TargetNamespace=\"{0}\",", TargetNamespace).AppendLine();
                source.AppendLine("    Templates = new string[] {");
                source.Append("      ").AppendLine(string.Join(",\r\n      ",
                                                               Descriptor.Templates.Select(
                                                                   t => "\"" + t.Replace("\\", "\\\\") + "\"").ToArray()));
                source.AppendLine("    })]");
            }

            source.Append("public class ").Append(viewClassName).Append(" : ").Append(BaseClass).AppendLine(", global::Spark.Ruby.IScriptingSparkView");
            source.AppendLine("{");

            source.Append("static System.Guid _generatedViewId = new System.Guid(\"").Append(GeneratedViewId).AppendLine("\");");
            source.AppendLine("public override System.Guid GeneratedViewId");
            source.AppendLine("{");
            source.AppendLine("get { return _generatedViewId; }");
            source.AppendLine("}");

            source.AppendLine("public global::System.IDisposable OutputScopeAdapter(object arg) ");
            source.AppendLine("{");
            source.AppendLine("if (arg == null) return OutputScope();");
            source.AppendLine("if (arg is global::System.IO.TextWriter) return OutputScope((global::System.IO.TextWriter)arg);");
            source.AppendLine("return OutputScope(global::System.Convert.ToString(arg));");
            source.AppendLine("}");

            source.AppendLine("public void OutputWriteAdapter(object arg) ");
            source.AppendLine("{");
            source.AppendLine("Output.Write(arg);");
            source.AppendLine("}");

            source.AppendLine("public global::Microsoft.Scripting.Hosting.CompiledCode CompiledCode {get;set;}");

            source.AppendLine("public string ScriptSource");
            source.AppendLine("{");
            source.Append("get { return @\"").Append(script.ToString().Replace("\"", "\"\"")).AppendLine("\"; }");
            source.AppendLine("}");

            source.AppendLine("public override void Render()");
            source.AppendLine("{");
            source.AppendLine("CompiledCode.Execute(");
            source.AppendLine("CompiledCode.Engine.CreateScope(");
            source.AppendLine("new global::Spark.Ruby.ScriptingViewSymbolDictionary(this)");
            source.AppendLine("));");
            source.AppendLine("}");

            source.AppendLine("}");

            if (Descriptor != null && !string.IsNullOrEmpty(Descriptor.TargetNamespace))
            {
                source.AppendLine("}");
            }

            SourceCode = source.ToString();
        }