Esempio n. 1
0
        /// <inheritdoc />
        protected override bool HandleDynamic(CodeLambdaDeclarationExpression obj, Context ctx)
        {
            ctx.Writer.Write("Function"); //it is safe to declare it as a function, but will give a warning
            ctx.Writer.Write("(");
            GeneralUtils.HandleCollectionCommaSeparated(obj.Parameters, ctx.HandlerProvider.ExpressionHandler, ctx);
            ctx.Writer.Write(")");
            if (obj.Statements.Count == 1 && obj.Statements[0] is CodeExpressionStatement statement)
            {
                ctx.Writer.Write(" ");
                VisualBasicUtils.BeginBlock(BlockType.Function, ctx, false);
                ctx.HandlerProvider.ExpressionHandler.Handle(statement.Expression, ctx);
                VisualBasicUtils.EndBlock(ctx, false, false);
            }
            else
            {
                ctx.Writer.NewLine();
                ctx.Indent();
                VisualBasicUtils.BeginBlock(BlockType.Function, ctx, false);
                VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx);
                ctx.Unindent();
                ctx.Writer.Indent(ctx);
                VisualBasicUtils.EndBlock(ctx, false);
            }

            return(true);
        }
Esempio n. 2
0
        /// <inheritdoc />
        protected override void HandleConstructor(CodeConstructor obj, Context ctx)
        {
            //no return type, only access modifier
            ctx.HandlerProvider.MemberAttributesHandler.Handle(obj.Attributes, ctx);
            ctx.Writer.Write("Sub New");
            //no type parameters
            HandleMethodParameters(obj, ctx);
            VisualBasicUtils.BeginBlock(BlockType.Sub, ctx);
            if (obj.BaseConstructorArgs.Count > 0 || obj.ChainedConstructorArgs.Count > 0)
            {
                //TODO : this()

                if (obj.BaseConstructorArgs.Count > 0)
                {
                    ctx.Writer.IndentAndWrite("MyBase.New(", ctx);
                    GeneralUtils.HandleCollectionCommaSeparated(obj.BaseConstructorArgs.Cast <CodeExpression>(),
                                                                ctx.HandlerProvider.ExpressionHandler, ctx);
                }
                else
                {
                    ctx.Writer.IndentAndWrite("Me.New(", ctx);
                    GeneralUtils.HandleCollectionCommaSeparated(obj.ChainedConstructorArgs.Cast <CodeExpression>(),
                                                                ctx.HandlerProvider.ExpressionHandler, ctx);
                }

                ctx.Writer.WriteLine(")");
            }
            VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx);
            VisualBasicUtils.EndBlock(ctx);
        }
Esempio n. 3
0
 /// <inheritdoc />
 protected override void HandleCondition(CodeConditionStatement obj, Context ctx)
 {
     VisualBasicUtils.BeginBlock(BlockType.If, ctx, false);
     HandleConditionNoBlock(obj, ctx);
     ctx.Writer.Indent(ctx);
     VisualBasicUtils.EndBlock(ctx, false);
 }
Esempio n. 4
0
 /// <inheritdoc />
 public bool Handle(CodeNamespaceImport obj, Context ctx)
 {
     if (ctx.ImportedNamespaces.Add(obj.Namespace))
     {
         ctx.Writer.WriteLine(
             $"Imports {VisualBasicUtils.GetValidNamespaceIdentifier(obj.Namespace)}");
     }
     return(true);
 }
Esempio n. 5
0
 public bool Handle(CodeCatchClause obj, Context ctx)
 {
     ctx.Writer.Write($"Catch {obj.LocalName?.AsVbId() ?? DefaultExceptionName} As ");
     ctx.HandlerProvider.TypeReferenceHandler.Handle(obj.CatchExceptionType, ctx);
     ctx.Indent();
     ctx.Writer.NewLine();
     VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx);
     ctx.Unindent();
     return(true);
 }
Esempio n. 6
0
 /// <inheritdoc />
 protected override void DoHandle(CodeNamespace obj, Context ctx)
 {
     ctx.CurrentNamespace = obj.Name;
     ctx.Writer.Write($"Namespace {VisualBasicUtils.GetValidNamespaceIdentifier(ctx.CurrentNamespace)}");
     VisualBasicUtils.BeginBlock(BlockType.Namespace, ctx);
     GeneralUtils.HandleCollection(obj.Types.Cast <CodeTypeDeclaration>(), ctx.HandlerProvider.TypeDeclarationHandler, ctx,
                                   preAction: (c) => c.Writer.Indent(c),
                                   postAction: (c) => c.Writer.NewLine(), doPostActionOnLast: false);
     VisualBasicUtils.EndBlock(ctx);
 }
Esempio n. 7
0
 /// <inheritdoc />
 protected override void HandleDoWhile(CodePostTestIterationStatement obj, Context ctx)
 {
     VisualBasicUtils.BeginBlock(BlockType.Do, ctx, false);
     ctx.Writer.WriteLine("Do");
     ctx.Indent();
     VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx);
     ctx.Unindent();
     ctx.Writer.IndentAndWrite("Loop While ", ctx);
     ctx.HandlerProvider.ExpressionHandler.Handle(obj.TestExpression, ctx);
     VisualBasicUtils.EndBlock(ctx, false, false);
 }
Esempio n. 8
0
        /// <inheritdoc />
        protected override void HandleUsing(CodeUsingStatement obj, Context ctx)
        {
            ctx.Writer.Write($"Using {obj.VariableName} As ");
            if (!GeneralUtils.IsNullOrVoidType(obj.Type))
            {
                ctx.HandlerProvider.TypeReferenceHandler.Handle(obj.Type, ctx);
                ctx.Writer.Write(" = ");
            }

            ctx.HandlerProvider.ExpressionHandler.Handle(obj.InitializerExpression, ctx);
            VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx, BlockType.Using, false);
        }
        /// <summary>
        /// Returns the provided string as a valid VB identifier
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static string AsVbId(this string self)
        {
            if (self.Length == 0)
            {
                return(null);
            }

            if (self.Contains("."))
            {
                return(VisualBasicUtils.GetValidNamespaceIdentifier(self));
            }
            if (self.IsVbKeyword())
            {
                return("[" + self + "]");
            }
            return(self);
        }
Esempio n. 10
0
 /// <inheritdoc />
 protected override void HandleForEach(CodeForEachStatement obj, Context ctx)
 {
     VisualBasicUtils.BeginBlock(BlockType.For, ctx, false);
     ctx.Writer.Write($"For Each {obj.ItemName.AsVbId()}");
     if (!GeneralUtils.IsNullOrVoidType(obj.ItemType))
     {
         ctx.Writer.Write(" As ");
         ctx.HandlerProvider.TypeReferenceHandler.Handle(obj.ItemType, ctx);
     }
     ctx.Writer.Write(" In ");
     ctx.HandlerProvider.ExpressionHandler.Handle(obj.ObjectToIterate, ctx);
     ctx.Writer.NewLine();
     ctx.Indent();
     VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx);
     ctx.Unindent();
     ctx.Writer.IndentAndWrite("Next", ctx);
     VisualBasicUtils.EndBlock(ctx, false, false);
 }
Esempio n. 11
0
 /// <inheritdoc />
 protected override void HandleTryCatchFinally(CodeTryCatchFinallyStatement obj, Context ctx)
 {
     VisualBasicUtils.BeginBlock(BlockType.Try, ctx, false);
     ctx.Writer.Write("Try");
     ctx.Indent();
     ctx.Writer.NewLine();
     VisualBasicUtils.HandleStatementCollection(obj.TryStatements, ctx);
     ctx.Unindent();
     GeneralUtils.HandleCollection(obj.CatchClauses.Cast <CodeCatchClause>(), _catchClauseHandler, ctx,
                                   preAction: (c) => c.Writer.Indent(c));
     if (obj.FinallyStatements.Count > 0)
     {
         ctx.Writer.Indent(ctx);
         ctx.Writer.WriteLine("Finally");
         ctx.Indent();
         VisualBasicUtils.HandleStatementCollection(obj.FinallyStatements, ctx);
         ctx.Unindent();
     }
     ctx.Writer.Indent(ctx);
     VisualBasicUtils.EndBlock(ctx, false);
 }
Esempio n. 12
0
 private void HandleConditionNoBlock(CodeConditionStatement obj, Context ctx)
 {
     ctx.Writer.Write("If ");
     ctx.HandlerProvider.ExpressionHandler.Handle(obj.Condition, ctx);
     ctx.Writer.Write(" Then");
     ctx.Indent();
     ctx.Writer.NewLine();
     VisualBasicUtils.HandleStatementCollection(obj.TrueStatements, ctx);
     ctx.Unindent();
     if (obj.FalseStatements.Count == 1 && obj.FalseStatements[0] is CodeConditionStatement elseIf)
     {
         ctx.Writer.IndentAndWrite("Else", ctx);
         HandleConditionNoBlock(elseIf, ctx);
     }
     else if (obj.FalseStatements.Count > 0)
     {
         ctx.Writer.IndentAndWriteLine("Else", ctx);
         ctx.Indent();
         VisualBasicUtils.HandleStatementCollection(obj.FalseStatements, ctx);
         ctx.Unindent();
     }
 }
Esempio n. 13
0
        /// <inheritdoc />
        protected override void HandleProperty(CodeMemberProperty obj, Context ctx, bool isExt,
                                               CodeMemberPropertyExt objExt, bool doDefaultImplementation)
        {
            if (ctx.Options.DoConsistencyChecks)
            {
                if (!doDefaultImplementation && isExt && objExt.PropertyInitializer != null)
                {
                    throw new ConsistencyException($"Property {obj.Name} can't have initializer if it's not auto-property");
                }
            }

            if (GeneralUtils.IsNullOrVoidType(obj.PrivateImplementationType))
            {
                ctx.HandlerProvider.MemberAttributesHandler.Handle(obj.Attributes, ctx);
            }

            if (obj.HasGet && !obj.HasSet)
            {
                ctx.Writer.Write("ReadOnly ");
            }
            else if (obj.HasSet && !obj.HasGet)
            {
                ctx.Writer.Write("WriteOnly ");
            }

            ctx.Writer.Write($"Property ");
            if (GeneralUtils.IsNullOrVoidType(obj.PrivateImplementationType))
            {
                ctx.Writer.Write(obj.Name.AsVbId());
            }
            else
            {
                HandlePrivateImplementationTypeMemberName(obj.Name, obj.PrivateImplementationType, ctx);
            }
            ctx.Writer.Write(" As ");
            ctx.HandlerProvider.TypeReferenceHandler.Handle(obj.Type, ctx);
            if (doDefaultImplementation)
            {
                if (isExt && objExt.PropertyInitializer != null)
                {
                    ctx.Writer.Write(" = ");
                    ctx.HandlerProvider.ExpressionHandler.Handle(objExt.PropertyInitializer, ctx);
                }
                HandleImplementationTypes(obj.ImplementationTypes, obj.Name, ctx);
                HandlePrivateImplType(obj.PrivateImplementationType, obj.Name, ctx);
                ctx.Writer.NewLine();
            }
            else
            {
                HandleImplementationTypes(obj.ImplementationTypes, obj.Name, ctx);
                HandlePrivateImplType(obj.PrivateImplementationType, obj.Name, ctx);
                VisualBasicUtils.BeginBlock(BlockType.Property, ctx);

                if (obj.HasGet)
                {
                    ctx.Writer.Indent(ctx);
                    if (isExt && objExt.GetAccessibilityLevel != obj.Attributes.GetAccessibilityLevel())
                    {
                        ctx.Writer.Write($"{VisualBasicKeywordsUtils.AccessibilityLevelKeyword(objExt.GetAccessibilityLevel)} ");
                    }
                    ctx.Writer.Write("Get");
                    VisualBasicUtils.HandleStatementCollection(obj.GetStatements, ctx, BlockType.Get);
                }

                if (obj.HasSet)
                {
                    ctx.Writer.Indent(ctx);
                    if (isExt && objExt.SetAccessibilityLevel != obj.Attributes.GetAccessibilityLevel())
                    {
                        ctx.Writer.Write($"{VisualBasicKeywordsUtils.AccessibilityLevelKeyword(objExt.SetAccessibilityLevel)} ");
                    }
                    ctx.Writer.Write("Set");
                    VisualBasicUtils.HandleStatementCollection(obj.SetStatements, ctx, BlockType.Set);
                }

                VisualBasicUtils.EndBlock(ctx);
            }
        }
Esempio n. 14
0
 private void HandleMethodStatements(CodeMemberMethod obj, Context ctx, bool isSub)
 {
     VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx, isSub ? BlockType.Sub : BlockType.Function);
 }
Esempio n. 15
0
 /// <inheritdoc />
 protected override void HandleWhile(CodeIterationStatement obj, Context ctx)
 {
     ctx.Writer.Write("While ");
     ctx.HandlerProvider.ExpressionHandler.Handle(obj.TestExpression, ctx);
     VisualBasicUtils.HandleStatementCollection(obj.Statements, ctx, BlockType.While, false);
 }
 /// <inheritdoc />
 protected override string AsValidNamespace(string s)
 {
     return(VisualBasicUtils.GetValidNamespaceIdentifier(s));
 }