예제 #1
0
        protected override void Visit(MacroChunk chunk)
        {
            _source
            .Write("Public Function ")
            .Write(chunk.Name)
            .Write("(");
            var delimiter = "";

            foreach (var parameter in chunk.Parameters)
            {
                _source
                .Write(delimiter)
                .Write(parameter.Name)
                .Write(" As ")
                .WriteCode(parameter.Type);
                delimiter = ", ";
            }
            _source
            .WriteLine(") As String")
            .AddIndent();
            _source
            .WriteLine("Using OutputScope(new Global.System.IO.StringWriter())")
            .AddIndent();

            var variables = new Dictionary <string, object>();

            foreach (var param in chunk.Parameters)
            {
                variables.Add(param.Name, null);
            }

            var generator = new GeneratedCodeVisitor(_source, variables, _nullBehaviour);

            generator.Accept(chunk.Body);

            _source
            .WriteLine("Return Output.ToString()")
            .RemoveIndent().WriteLine("End Using")
            .RemoveIndent().WriteLine("End Function");
        }
예제 #2
0
        protected override void Visit(MacroChunk chunk)
        {
            _source
                .Write("Public Function ")
                .Write(chunk.Name)
                .Write("(");
            var delimiter = "";
            foreach (var parameter in chunk.Parameters)
            {
                _source
                    .Write(delimiter)
                    .Write(parameter.Name)
                    .Write(" As ")
                    .WriteCode(parameter.Type);
                delimiter = ", ";
            }
            _source
                .WriteLine(") As String")
                .AddIndent();
            _source
                .WriteLine("Using OutputScope(new Global.System.IO.StringWriter())")
                .AddIndent();

            var variables = new Dictionary<string, object>();
            foreach (var param in chunk.Parameters)
            {
                variables.Add(param.Name, null);
            }

            var generator = new GeneratedCodeVisitor(_source, variables, _nullBehaviour);
            generator.Accept(chunk.Body);

            _source
                .WriteLine("Return Output.ToString()")
                .RemoveIndent().WriteLine("End Using")
                .RemoveIndent().WriteLine("End Function");
        }
예제 #3
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;
        }