Exemplo n.º 1
0
        public static void Go(ScalaWriter writer, PropertyDeclarationSyntax property)
        {
            Action <AccessorDeclarationSyntax, bool> writeRegion = (region, get) =>
            {
                writer.WriteIndent();

                if (property.Modifiers.Any(SyntaxKind.OverrideKeyword))
                {
                    writer.Write("override ");
                }
                if (property.Modifiers.Any(SyntaxKind.PrivateKeyword))
                {
                    writer.Write("private ");
                }

                writer.Write("def ");
                writer.Write(WriteIdentifierName.TransformIdentifier(property.Identifier.ValueText));

                if (get)
                {
                    writer.Write(TypeProcessor.ConvertTypeWithColon(property.Type));
                }
                else
                {
                    writer.Write("_=(value");
                    writer.Write(TypeProcessor.ConvertTypeWithColon(property.Type));
                    writer.Write(")");
                }

                if (property.Modifiers.Any(SyntaxKind.AbstractKeyword) || region.Body == null)
                {
                    writer.Write(";\r\n");
                }
                else
                {
                    writer.Write(" =\r\n");
                    Core.WriteBlock(writer, region.Body.As <BlockSyntax>());
                }
            };

            var getter = property.AccessorList.Accessors.SingleOrDefault(o => o.Keyword.Kind() == SyntaxKind.GetKeyword);
            var setter = property.AccessorList.Accessors.SingleOrDefault(o => o.Keyword.Kind() == SyntaxKind.SetKeyword);

            if (getter == null && setter == null)
            {
                throw new Exception("Property must have either a get or a set");
            }

            if (getter != null && setter != null && setter.Body == null && getter.Body == null)
            {
                //Both get and set are null, which means this is an automatic property.  For our purposes, this is the equivilant of a field
                WriteField.Go(writer, property.Modifiers, WriteIdentifierName.TransformIdentifier(property.Identifier.ValueText), property.Type);
            }
            else
            {
                if (getter != null)
                {
                    writeRegion(getter, true);
                }
                else if (setter != null)
                {
                    //Scala does not allow having a setter without a getter. Write out a getter.
                    writer.Write("def ");
                    writer.Write(WriteIdentifierName.TransformIdentifier(property.Identifier.ValueText));
                    writer.Write(TypeProcessor.ConvertTypeWithColon(property.Type));
                    writer.Write(" =\r\n");
                    writer.WriteOpenBrace();
                    writer.WriteLine("throw new Exception(\"No getter defined\");");
                    writer.WriteCloseBrace();
                }

                if (setter != null)
                {
                    writeRegion(setter, false);
                }
            }
        }
Exemplo n.º 2
0
 private static void Factory(ScalaWriter writer, SyntaxNode node, bool isConst)
 {
     if (node is MethodDeclarationSyntax)
     {
         WriteMethod.Go(writer, node.As <MethodDeclarationSyntax>());
     }
     else if (node is PropertyDeclarationSyntax)
     {
         WriteProperty.Go(writer, node.As <PropertyDeclarationSyntax>());
     }
     else if (node is FieldDeclarationSyntax)
     {
         WriteField.Go(writer, node.As <FieldDeclarationSyntax>());
     }
     else if (node is ConstructorDeclarationSyntax)
     {
         WriteConstructorBody.Go(writer, node.As <ConstructorDeclarationSyntax>());
     }
     else if (node is ExpressionStatementSyntax)
     {
         WriteStatement(writer, node.As <ExpressionStatementSyntax>());
     }
     else if (node is LocalDeclarationStatementSyntax)
     {
         WriteLocalDeclaration.Go(writer, node.As <LocalDeclarationStatementSyntax>());
     }
     else if (node is BlockSyntax)
     {
         WriteBlock(writer, node.As <BlockSyntax>());
     }
     else if (node is InvocationExpressionSyntax)
     {
         WriteInvocationExpression.Go(writer, node.As <InvocationExpressionSyntax>());
     }
     else if (node is LiteralExpressionSyntax)
     {
         WriteLiteralExpression.Go(writer, node.As <LiteralExpressionSyntax>(), isConst);
     }
     else if (node is IdentifierNameSyntax)
     {
         WriteIdentifierName.Go(writer, node.As <IdentifierNameSyntax>());
     }
     else if (node is ImplicitArrayCreationExpressionSyntax)
     {
         WriteArrayCreationExpression.Go(writer, node.As <ImplicitArrayCreationExpressionSyntax>());
     }
     else if (node is ArrayCreationExpressionSyntax)
     {
         WriteArrayCreationExpression.Go(writer, node.As <ArrayCreationExpressionSyntax>());
     }
     else if (node is MemberAccessExpressionSyntax)
     {
         WriteMemberAccessExpression.Go(writer, node.As <MemberAccessExpressionSyntax>());
     }
     else if (node is ParenthesizedLambdaExpressionSyntax)
     {
         WriteLambdaExpression.Go(writer, node.As <ParenthesizedLambdaExpressionSyntax>());
     }
     else if (node is SimpleLambdaExpressionSyntax)
     {
         WriteLambdaExpression.Go(writer, node.As <SimpleLambdaExpressionSyntax>());
     }
     else if (node is ReturnStatementSyntax)
     {
         WriteReturnStatement.Go(writer, node.As <ReturnStatementSyntax>());
     }
     else if (node is ObjectCreationExpressionSyntax)
     {
         WriteObjectCreationExpression.Go(writer, node.As <ObjectCreationExpressionSyntax>());
     }
     else if (node is ElementAccessExpressionSyntax)
     {
         WriteElementAccessExpression.Go(writer, node.As <ElementAccessExpressionSyntax>());
     }
     else if (node is ForEachStatementSyntax)
     {
         WriteForEachStatement.Go(writer, node.As <ForEachStatementSyntax>());
     }
     else if (node is IfStatementSyntax)
     {
         WriteIfStatement.Go(writer, node.As <IfStatementSyntax>());
     }
     else if (node is BinaryExpressionSyntax)
     {
         WriteBinaryExpression.Go(writer, node.As <BinaryExpressionSyntax>());
     }
     else if (node is AssignmentExpressionSyntax)
     {
         WriteBinaryExpression.Go(writer, node.As <AssignmentExpressionSyntax>());
     }
     else if (node is ConditionalExpressionSyntax)
     {
         WriteConditionalExpression.Go(writer, node.As <ConditionalExpressionSyntax>());
     }
     else if (node is BaseExpressionSyntax)
     {
         WriteBaseExpression.Go(writer, node.As <BaseExpressionSyntax>());
     }
     else if (node is ThisExpressionSyntax)
     {
         WriteThisExpression.Go(writer, node.As <ThisExpressionSyntax>());
     }
     else if (node is CastExpressionSyntax)
     {
         WriteCastExpression.Go(writer, node.As <CastExpressionSyntax>());
     }
     else if (node is ThrowStatementSyntax)
     {
         WriteThrowStatement.Go(writer, node.As <ThrowStatementSyntax>());
     }
     else if (node is ThrowExpressionSyntax)
     {
         WriteThrowStatement.WriteThrowExpression(writer, node.As <ThrowExpressionSyntax>());
     }
     else if (node is EqualsValueClauseSyntax)
     {
         WriteEqualsValueClause.Go(writer, node.As <EqualsValueClauseSyntax>());
     }
     else if (node is ForStatementSyntax)
     {
         WriteForStatement.Go(writer, node.As <ForStatementSyntax>());
     }
     else if (node is WhileStatementSyntax)
     {
         WriteWhileStatement.Go(writer, node.As <WhileStatementSyntax>());
     }
     else if (node is BreakStatementSyntax)
     {
         WriteBreakStatement.Go(writer, node.As <BreakStatementSyntax>());
     }
     else if (node is ContinueStatementSyntax)
     {
         WriteContinueStatement.Go(writer, node.As <ContinueStatementSyntax>());
     }
     else if (node is DoStatementSyntax)
     {
         WriteDoStatement.Go(writer, node.As <DoStatementSyntax>());
     }
     else if (node is SwitchStatementSyntax)
     {
         WriteSwitchStatement.Go(writer, node.As <SwitchStatementSyntax>());
     }
     else if (node is TryStatementSyntax)
     {
         WriteTryStatement.Go(writer, node.As <TryStatementSyntax>());
     }
     else if (node is UsingStatementSyntax)
     {
         WriteUsingStatement.Go(writer, node.As <UsingStatementSyntax>());
     }
     else if (node is ParenthesizedExpressionSyntax)
     {
         WriteParenthesizedExpression.Go(writer, node.As <ParenthesizedExpressionSyntax>());
     }
     else if (node is LockStatementSyntax)
     {
         WriteLockStatement.Go(writer, node.As <LockStatementSyntax>());
     }
     else if (node is TypeOfExpressionSyntax)
     {
         WriteTypeOfExpression.Go(writer, node.As <TypeOfExpressionSyntax>());
     }
     else if (node is AnonymousObjectCreationExpressionSyntax)
     {
         WriteAnonymousObjectCreationExpression.Go(writer, node.As <AnonymousObjectCreationExpressionSyntax>());
     }
     else if (node is EmptyStatementSyntax)
     {
         return; //ignore empty statements
     }
     else if (node is DelegateDeclarationSyntax)
     {
         return; //don't write delegates - TypeProcessor converts them to function types directly
     }
     else if (node is DefaultExpressionSyntax)
     {
         WriteDefaultExpression.Go(writer, node.As <DefaultExpressionSyntax>());
     }
     else if (node is GenericNameSyntax)
     {
         WriteGenericName.Go(writer, node.As <GenericNameSyntax>());
     }
     else if (node is ConversionOperatorDeclarationSyntax)
     {
         WriteConversionOperatorDeclaration.Go(writer, node.As <ConversionOperatorDeclarationSyntax>());
     }
     else if (node is PrefixUnaryExpressionSyntax)
     {
         WriteUnaryExpression.WritePrefix(writer, node.As <PrefixUnaryExpressionSyntax>());
     }
     else if (node is PostfixUnaryExpressionSyntax)
     {
         WriteUnaryExpression.WritePostfix(writer, node.As <PostfixUnaryExpressionSyntax>());
     }
     else if (node is SizeOfExpressionSyntax)
     {
         WriteSizeOfExpression.Go(writer, node.As <SizeOfExpressionSyntax>());
     }
     else
     {
         throw new NotImplementedException(node.GetType().Name + " is not supported. " + Utility.Descriptor(node));
     }
 }