示例#1
0
        /// <summary>
        /// calls to Enum.Parse get re-written as calls to our special Parse methods on each enum.  We assume the first parameter to Enum.Parse is a a typeof()
        /// </summary>
        private static void WriteEnumParse(ScalaWriter writer, InvocationExpressionSyntax invocationExpression)
        {
            var args = invocationExpression.ArgumentList.Arguments;

            if (args.Count < 2 || args.Count > 3)
            {
                throw new Exception("Expected 2-3 args to Enum.Parse");
            }

            if (args.Count == 3 && (!(args[2].Expression is LiteralExpressionSyntax) || args[2].Expression.As <LiteralExpressionSyntax>().ToString() != "false"))
            {
                throw new NotImplementedException("Case-insensitive Enum.Parse is not supported " + Utility.Descriptor(invocationExpression));
            }

            if (!(args[0].Expression is TypeOfExpressionSyntax))
            {
                throw new Exception("Expected a typeof() expression as the first parameter of Enum.Parse " + Utility.Descriptor(invocationExpression));
            }

            var type = Program.GetModel(invocationExpression).GetTypeInfo(args[0].Expression.As <TypeOfExpressionSyntax>().Type).Type;

            writer.Write(type.ContainingNamespace.FullNameWithDot());
            writer.Write(WriteType.TypeName((INamedTypeSymbol)type));
            writer.Write(".Parse(");
            Core.Write(writer, args[1].Expression);
            writer.Write(")");
        }
示例#2
0
        public static void Go(ScalaWriter writer, ElementAccessExpressionSyntax expression)
        {
            var typeStr = TypeProcessor.GenericTypeName(Program.GetModel(expression).GetTypeInfo(expression.Expression).Type);
            var trans   = ElementAccessTranslation.Get(typeStr);

            Core.Write(writer, expression.Expression);

            if (trans != null)
            {
                writer.Write(".");
                writer.Write(trans.ReplaceGet);
            }

            writer.Write("(");

            bool first = true;

            foreach (var argument in expression.ArgumentList.Arguments)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write(", ");
                }

                Core.Write(writer, argument.Expression);
            }
            writer.Write(")");
        }
示例#3
0
        public static void Write(ScalaWriter writer, SyntaxNode node, bool isConst = false)
        {
            TriviaProcessor.ProcessNode(writer, node);

            if (Program.DoNotWrite.ContainsKey(node))
            {
                return;
            }

            var    exprOpt     = node as ExpressionSyntax;
            string postFactory = null;

            if (exprOpt != null)
            {
                var type = Program.GetModel(node).GetTypeInfo(exprOpt);

                if (type.Type != null && type.Type.SpecialType == SpecialType.System_Byte && type.ConvertedType.SpecialType != SpecialType.System_Byte && Utility.IsNumeric(type.ConvertedType))
                {
                    //Bytes are signed in the JVM, so we have to take care when up-converting them
                    writer.Write("System.CsScala.ByteTo");
                    writer.Write(TypeProcessor.ConvertType(type.ConvertedType));
                    writer.Write("(");
                    postFactory = ")";
                }
            }


            Factory(writer, node, isConst);
            writer.Write(postFactory);
        }
        public static void Go(ScalaWriter writer, ImplicitArrayCreationExpressionSyntax array)
        {
            writer.Write("Array");

            //var t = Program.GetModel(array).GetTypeInfo(array).Type;

            //if (t is IArrayTypeSymbol)
            //{
            //    writer.Write("[");
            //    writer.Write(TypeProcessor.ConvertType(t.As<IArrayTypeSymbol>().ElementType));
            //    writer.Write("]");
            //}


            writer.Write("(");

            bool first = true;

            foreach (var expression in array.Initializer.Expressions)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write(", ");
                }

                Core.Write(writer, expression);
            }

            writer.Write(")");
        }
示例#5
0
        public static void Go(ScalaWriter writer, ReturnStatementSyntax statement)
        {
            if (TypeState.Instance.InLambdaBreakable > 0)
            {
                if (statement.Expression != null)
                {
                    writer.WriteIndent();
                    writer.Write("__lambdareturn = ");
                    Core.Write(writer, statement.Expression);
                    writer.Write(";\r\n");
                }

                writer.WriteLine("__lambdabreak.break();");
            }
            else
            {
                writer.WriteIndent();
                writer.Write("return");

                if (statement.Expression != null)
                {
                    writer.Write(" ");
                    Core.Write(writer, statement.Expression);
                }
                writer.Write(";\r\n");
            }
        }
示例#6
0
 public static void Go(ScalaWriter writer, ConditionalExpressionSyntax expression)
 {
     writer.Write("(if (");
     Core.Write(writer, expression.Condition);
     writer.Write(") ");
     Core.Write(writer, expression.WhenTrue);
     writer.Write(" else ");
     Core.Write(writer, expression.WhenFalse);
     writer.Write(")");
 }
示例#7
0
        private static void WriteEnumGetValues(ScalaWriter writer, InvocationExpressionSyntax invocationExpression)
        {
            if (!(invocationExpression.ArgumentList.Arguments[0].Expression is TypeOfExpressionSyntax))
            {
                throw new Exception("Expected a typeof() expression as the first parameter of Enum.GetValues " + Utility.Descriptor(invocationExpression));
            }

            var type = Program.GetModel(invocationExpression).GetTypeInfo(invocationExpression.ArgumentList.Arguments[0].Expression.As <TypeOfExpressionSyntax>().Type).Type;

            writer.Write(type.ContainingNamespace.FullNameWithDot());
            writer.Write(WriteType.TypeName((INamedTypeSymbol)type));
            writer.Write(".Values");
        }
示例#8
0
        private static void WriteGetEnumeratorFunction(ScalaWriter writer, MethodDeclarationSyntax method, IMethodSymbol methodSymbol)
        {
            var returnType = TypeProcessor.ConvertType(methodSymbol.ReturnType);

            if (!returnType.StartsWith("System.Collections.Generic.IEnumerator["))
            {
                return; //we only support the generic IEnumerator form of GetEnumerator.  Anything else, just don't write out the method.
            }
            var enumerableType = returnType.RemoveFromStartOfString("System.Collections.Generic.IEnumerator[").RemoveFromEndOfString("]");

            //We only support very simple GetEnumerator functions that pass on their call to some other collection.  The body should be like "return <expr>.GetEnumerator();", otherwise don't write out the function at all.
            if (method.Body == null)
            {
                return;
            }
            if (method.Body.Statements.Count > 1)
            {
                return;
            }
            var returnStatement = method.Body.Statements.Single() as ReturnStatementSyntax;

            if (returnStatement == null)
            {
                return;
            }
            var invocation = returnStatement.Expression as InvocationExpressionSyntax;

            if (invocation == null)
            {
                return;
            }
            var member = invocation.Expression as MemberAccessExpressionSyntax;

            if (member == null)
            {
                return;
            }

            writer.WriteIndent();
            writer.Write("def foreach[U](fn: ");
            writer.Write(enumerableType);
            writer.Write(" => U)\r\n");
            writer.WriteOpenBrace();

            writer.WriteIndent();
            Core.Write(writer, member.Expression);
            writer.Write(".foreach(fn);\r\n");
            writer.WriteCloseBrace();
        }
示例#9
0
 public static void WriteFieldModifiers(ScalaWriter writer, SyntaxTokenList modifiers)
 {
     if (modifiers.Any(SyntaxKind.PrivateKeyword))
     {
         writer.Write("private ");
     }
 }
示例#10
0
        public static void Go(ScalaWriter writer, WhileStatementSyntax whileStatement)
        {
            var info = new LoopInfo(whileStatement);

            info.WritePreLoop(writer);
            writer.WriteIndent();
            writer.Write("while (");
            Core.Write(writer, whileStatement.Condition);
            writer.Write(")\r\n");

            writer.WriteOpenBrace();
            info.WriteLoopOpening(writer);
            Core.WriteStatementAsBlock(writer, whileStatement.Statement, false);
            info.WriteLoopClosing(writer);
            writer.WriteCloseBrace();
            info.WritePostLoop(writer);
        }
 public static void WritePostfix(ScalaWriter writer, PostfixUnaryExpressionSyntax expression)
 {
     if (expression.OperatorToken.Kind() == SyntaxKind.MinusMinusToken)
     {
         Core.Write(writer, expression.Operand);
         writer.Write(" -= 1");
     }
     else if (expression.OperatorToken.Kind() == SyntaxKind.PlusPlusToken)
     {
         Core.Write(writer, expression.Operand);
         writer.Write(" += 1");
     }
     else
     {
         throw new Exception("No support for " + expression.OperatorToken.Kind() + " at " + Utility.Descriptor(expression));
     }
 }
示例#12
0
        public static void Go(ScalaWriter writer, LockStatementSyntax statement)
        {
            if (statement.DescendantNodes().OfType <ReturnStatementSyntax>().Any())
            {
                throw new Exception("Cannot return from within a lock statement " + Utility.Descriptor(statement));
            }

            writer.WriteIndent();
            writer.Write("CsLock.Lock(");
            Core.Write(writer, statement.Expression);
            writer.Write(", () =>\r\n");
            writer.WriteOpenBrace();
            Core.WriteStatementAsBlock(writer, statement.Statement, false);
            writer.Indent--;
            writer.WriteIndent();
            writer.Write("});\r\n");
        }
示例#13
0
        public static void Go(ScalaWriter writer, SyntaxTokenList modifiers, string name, TypeSyntax type, EqualsValueClauseSyntax initializerOpt = null)
        {
            writer.WriteIndent();

            var isConst = IsConst(modifiers, initializerOpt, type);

            WriteFieldModifiers(writer, modifiers);
            if (isConst)
            {
                writer.Write("final val ");
            }
            else
            {
                writer.Write("var ");
            }

            writer.Write(name);
            writer.Write(TypeProcessor.ConvertTypeWithColon(type));
            writer.Write(" = ");

            if (initializerOpt != null)
            {
                Core.Write(writer, initializerOpt.Value);
            }
            else
            {
                writer.Write(TypeProcessor.DefaultValue(type));
            }

            writer.Write(";");
            writer.WriteLine();
        }
        public static void Go(ScalaWriter writer, ThrowStatementSyntax statement)
        {
            writer.WriteIndent();

            writer.Write("throw ");

            if (statement.Expression == null)
            {
                //On just "throw" with no exception name, navigate up the stack to find the nearest catch block and insert the exception's name
                CatchClauseSyntax catchBlock;
                SyntaxNode        node = statement;
                do
                {
                    catchBlock = (node = node.Parent) as CatchClauseSyntax;
                }while (catchBlock == null);

                if (catchBlock == null)
                {
                    throw new Exception("throw statement with no exception name, and could not locate a catch block " + Utility.Descriptor(statement));
                }

                if (catchBlock.Declaration == null)
                {
                    writer.Write("__ex");
                }
                else
                {
                    var exName = WriteIdentifierName.TransformIdentifier(catchBlock.Declaration.Identifier.ValueText);

                    if (string.IsNullOrWhiteSpace(exName))
                    {
                        writer.Write("__ex");
                    }
                    else
                    {
                        writer.Write(exName);
                    }
                }
            }
            else
            {
                Core.Write(writer, statement.Expression);
            }
            writer.Write(";\r\n");
        }
 public static void WritePrefix(ScalaWriter writer, PrefixUnaryExpressionSyntax expression)
 {
     if (expression.OperatorToken.Kind() == SyntaxKind.MinusMinusToken)
     {
         Core.Write(writer, expression.Operand);
         writer.Write(" -= 1");
     }
     else if (expression.OperatorToken.Kind() == SyntaxKind.PlusPlusToken)
     {
         Core.Write(writer, expression.Operand);
         writer.Write(" += 1");
     }
     else
     {
         writer.Write(expression.OperatorToken.ToString());
         Core.Write(writer, expression.Operand);
     }
 }
        public void Write(ScalaWriter writer)
        {
            if (this.StringOpt != null)
            {
                writer.Write(this.StringOpt);
            }
            else
            {
                if (ArgumentOpt.NameColon != null)
                {
                    Core.Write(writer, ArgumentOpt.NameColon.Name);
                    writer.Write(" = ");
                }

                Core.Write(writer, this.ArgumentOpt.Expression);

                WriteEnum.Check(this.ArgumentOpt.Expression);
            }
        }
示例#17
0
        public static void Go(ScalaWriter writer, TryStatementSyntax tryStatement)
        {
            writer.WriteLine("try");
            Core.Write(writer, tryStatement.Block);

            var catches = tryStatement.Catches.Where(o => Program.DoNotWrite.ContainsKey(o) == false).ToList();

            if (catches.Count > 0)
            {
                writer.WriteLine("catch");
                writer.WriteOpenBrace();

                foreach (var catchClause in catches)
                {
                    writer.WriteIndent();

                    //In C#, the base exception type is Exception, but on the JVM it is Throwable.  Normally, JVM programs should not catch throwable, so we map the C# Exception type to the JVM Exception type by default.  We attempted to change Exception to map to Throwable but ran into issues with things getting caught that shouldn't, such as Scala's "BreakControl" that's used on break statements.
                    //if C# code really wants to catch all throwables, catch Exception and name the variable "allThrowables".  This is a signal to CSScala that all throwables should be caught.  However, use it with care, as it can cause complications.
                    if (catchClause.Declaration == null)
                    {
                        writer.Write("case __ex: java.lang.Exception => ");
                    }
                    else
                    {
                        writer.Write("case ");
                        writer.Write(string.IsNullOrWhiteSpace(catchClause.Declaration.Identifier.ValueText) ? "__ex" : WriteIdentifierName.TransformIdentifier(catchClause.Declaration.Identifier.ValueText));
                        writer.Write(": ");


                        if (catchClause.Declaration.Identifier.ValueText == "allThrowables")
                        {
                            writer.Write("java.lang.Throwable");
                        }
                        else
                        {
                            writer.Write(TypeProcessor.ConvertType(catchClause.Declaration.Type));
                        }

                        writer.Write(" =>\r\n");
                    }

                    writer.Indent++;
                    foreach (var statement in catchClause.Block.Statements)
                    {
                        Core.Write(writer, statement);
                    }
                    writer.Indent--;
                }

                writer.WriteCloseBrace();
            }

            if (tryStatement.Finally != null)
            {
                writer.WriteLine("finally");
                Core.Write(writer, tryStatement.Finally.Block);
            }
        }
示例#18
0
        public static void Go(ScalaWriter writer, LiteralExpressionSyntax expression, bool isConst)
        {
            var str = expression.ToString();

            if (str.StartsWith("@"))
            {
                str = "\"" + str.RemoveFromStartOfString("@\"").RemoveFromEndOfString("\"").Replace("\\", "\\\\").Replace("\"\"", "\\\"").Replace("\n", "\\n").Replace("\r", "") + "\"";
            }


            if (str.Length > 65000)
            {
                //Big strings have to be broken up, scala can't render big string constants, even when concatenated.  So we have to pass them to a function to concat at runtime
                writer.Write("System.CsScala.JoinConstants(");
                var raw = str.RemoveFromStartOfString("\"").RemoveFromEndOfString("\"");

                var subLength = 65000;
                for (int i = 0; i < raw.Length; i += subLength)
                {
                    var sub = raw.SubstringSafe(i, subLength);
                    //Make sure we never break in the middle of a backslash sequence.  TODO: This assumes backslash sequences are only ever two characters long, we could break on longer ones.
                    if (sub[sub.Length - 1] == '\\' && sub[sub.Length - 2] != '\\')
                    {
                        sub += raw[i + subLength];
                        i++;
                    }

                    writer.Write("\"");
                    writer.Write(sub);
                    writer.Write("\"");
                    if (i + subLength < raw.Length)
                    {
                        writer.Write(", ");
                    }
                }
                writer.Write(")");
            }
            else
            {
                writer.Write(str);
            }

            var typeInfo = Program.GetModel(expression).GetTypeInfo(expression);

            if (typeInfo.Type != null && typeInfo.ConvertedType != null)
            {
                if (isConst == false && typeInfo.ConvertedType.SpecialType == SpecialType.System_Byte && typeInfo.Type.SpecialType == SpecialType.System_Int32)
                {
                    writer.Write(".toByte");
                }
            }
        }
        public static void Go(ScalaWriter writer, ArrayCreationExpressionSyntax array)
        {
            if (array.Type.RankSpecifiers.Count > 1 || array.Type.RankSpecifiers.Single().Sizes.Count > 1)
            {
                throw new Exception("Multi-dimensional arrays are not supported " + Utility.Descriptor(array));
            }

            var rankExpression = array.Type.RankSpecifiers.Single().Sizes.Single();

            if (rankExpression is OmittedArraySizeExpressionSyntax)
            {
                writer.Write("Array[");
                writer.Write(TypeProcessor.ConvertType(array.Type.ElementType));
                writer.Write("](");

                bool first = true;
                foreach (var expression in array.Initializer.Expressions)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        writer.Write(", ");
                    }

                    Core.Write(writer, expression);
                }

                writer.Write(")");
            }
            else
            {
                if (array.Initializer != null)
                {
                    throw new Exception("Initalizers along with array sizes are not supported - please use a size or an initializer " + Utility.Descriptor(array));
                }

                writer.Write("new Array[");
                writer.Write(TypeProcessor.ConvertType(array.Type.ElementType));
                writer.Write("](");
                Core.Write(writer, array.Type.RankSpecifiers.Single().Sizes.Single());
                writer.Write(")");
            }
        }
        public static void Go(ScalaWriter writer, IdentifierNameSyntax identifier, bool byRef = false)
        {
            var symbol = Program.GetModel(identifier).GetSymbolInfo(identifier).Symbol;

            if (symbol.IsStatic)
            {
                writer.Write(symbol.ContainingNamespace.FullNameWithDot());
                writer.Write(symbol.ContainingType.Name);
                writer.Write(".");
            }

            writer.Write(TransformIdentifier(identifier.Identifier.ToString()));

            if (!byRef)
            {
                if (Program.RefOutSymbols.ContainsKey(symbol))
                {
                    writer.Write(".Value");
                }
            }
        }
示例#21
0
        public static void Go(ScalaWriter writer, IfStatementSyntax ifStatement, bool indent = true)
        {
            if (indent)
            {
                writer.WriteIndent();
            }

            writer.Write("if (");
            Core.Write(writer, ifStatement.Condition);
            writer.Write(")\r\n");

            Core.WriteStatementAsBlock(writer, ifStatement.Statement);

            if (ifStatement.Else != null)
            {
                writer.WriteIndent();
                writer.Write("else");

                if (ifStatement.Else.Statement is BlockSyntax)
                {
                    writer.Write("\r\n");
                    Core.WriteBlock(writer, ifStatement.Else.Statement.As <BlockSyntax>());
                }
                else if (ifStatement.Else.Statement is IfStatementSyntax)
                {
                    writer.Write(" ");
                    WriteIfStatement.Go(writer, ifStatement.Else.Statement.As <IfStatementSyntax>(), false);
                }
                else
                {
                    writer.Write("\r\n");
                    Core.WriteStatementAsBlock(writer, ifStatement.Else.Statement);
                }
            }
        }
示例#22
0
        public static void WriteMember(ScalaWriter writer, ExpressionSyntax expression)
        {
            var symbol = Program.GetModel(expression).GetSymbolInfo(expression).Symbol;

            if (symbol is INamedTypeSymbol)
            {
                var translateOpt = TypeTranslation.Get(symbol.ContainingNamespace.FullNameWithDot() + symbol.Name, symbol.As <INamedTypeSymbol>());

                if (translateOpt != null)
                {
                    writer.Write(translateOpt.ReplaceWith);
                }
                else
                {
                    writer.Write(symbol.ContainingNamespace.FullNameWithDot() + WriteType.TypeName(symbol.As <INamedTypeSymbol>()));
                }
            }
            else
            {
                Core.Write(writer, expression);
            }
        }
示例#23
0
        public static void Go(ScalaWriter writer, IEnumerable <EnumMemberDeclarationSyntax> allChildren)
        {
            writer.Write("object ");
            writer.Write(TypeState.Instance.TypeName);
            writer.Write("\r\n");
            writer.WriteOpenBrace();

            int lastEnumValue = -1;

            var values = allChildren.Select(o => new { Syntax = o, Value = DetermineEnumValue(o, ref lastEnumValue) }).ToList();

            foreach (var value in values)
            {
                writer.WriteLine("final val " + WriteIdentifierName.TransformIdentifier(value.Syntax.Identifier.ValueText) + ":Int = " + value.Value + ";");
            }

            writer.WriteLine();
            writer.WriteLine(@"def ToString(n:java.lang.Integer):String = if (n == null) """" else ToString(n.intValue());");

            writer.WriteLine("def ToString(e:Int):String =");
            writer.WriteOpenBrace();
            writer.WriteLine("return e match");
            writer.WriteOpenBrace();

            foreach (var value in values)
            {
                writer.WriteLine("case " + value.Value + " => \"" + value.Syntax.Identifier.ValueText + "\";");
            }

            writer.WriteLine("case u => u.toString;");

            writer.WriteCloseBrace();
            writer.WriteCloseBrace();

            writer.WriteLine();
            writer.WriteLine("def Parse(s:String):Int =");
            writer.WriteOpenBrace();
            writer.WriteLine("return s match");
            writer.WriteOpenBrace();

            foreach (var value in values)
            {
                writer.WriteLine("case \"" + value.Syntax.Identifier.ValueText + "\" => " + value.Value + ";");
            }

            writer.WriteLine("case u => u.toInt;");

            writer.WriteCloseBrace();
            writer.WriteCloseBrace();

            writer.WriteLine();
            writer.WriteIndent();
            writer.Write("final val Values:Array[Int] = Array(");
            writer.Write(string.Join(", ", values.Select(o => o.Value.ToString())));
            writer.Write(");\r\n");


            writer.WriteCloseBrace();
        }
示例#24
0
        public static void Go(ScalaWriter writer, LiteralExpressionSyntax expression, bool isConst)
        {
            var str = expression.ToString();

            if (str.StartsWith("@"))
            {
                str = "\"" + str.RemoveFromStartOfString("@\"").RemoveFromEndOfString("\"").Replace("\\", "\\\\").Replace("\"\"", "\\\"").Replace("\r\n", "\\n") + "\"";
            }

            var typeInfo = Program.GetModel(expression).GetTypeInfo(expression);


            writer.Write(str);

            if (typeInfo.Type != null && typeInfo.ConvertedType != null)
            {
                if (isConst == false && typeInfo.ConvertedType.SpecialType == SpecialType.System_Byte && typeInfo.Type.SpecialType == SpecialType.System_Int32)
                {
                    writer.Write(".toByte");
                }
            }
        }
示例#25
0
        public static void Go(ScalaWriter writer, GenericNameSyntax name)
        {
            writer.Write(WriteIdentifierName.TransformIdentifier(name.Identifier.ValueText));
            writer.Write("[");

            bool first = true;

            foreach (var gen in name.TypeArgumentList.Arguments)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write(", ");
                }

                writer.Write(TypeProcessor.ConvertType(gen));
            }
            writer.Write("]");
        }
        public static void Go(ScalaWriter writer, ConversionOperatorDeclarationSyntax method)
        {
            if (method.ImplicitOrExplicitKeyword.Kind() != SyntaxKind.ExplicitKeyword)
            {
                throw new Exception("Implicit cast operators are not supported " + Utility.Descriptor(method));
            }

            writer.WriteIndent();
            writer.Write("def op_Explicit_");
            writer.Write(TypeProcessor.ConvertType(method.Type));
            writer.Write("(");

            bool firstParam = true;

            foreach (var param in method.ParameterList.Parameters)
            {
                if (firstParam)
                {
                    firstParam = false;
                }
                else
                {
                    writer.Write(", ");
                }

                writer.Write(WriteIdentifierName.TransformIdentifier(param.Identifier.ValueText));
                writer.Write(TypeProcessor.ConvertTypeWithColon(param.Type));
            }

            writer.Write(")");
            writer.Write(TypeProcessor.ConvertTypeWithColon(method.Type));
            writer.Write(" =\r\n");

            writer.WriteOpenBrace();

            foreach (var statement in method.Body.Statements)
            {
                Core.Write(writer, statement);
            }

            writer.WriteCloseBrace();
        }
示例#27
0
        private static void WriteTypeParameters(ScalaWriter writer, MethodTranslation translateOpt, string typeParameters, InvocationExpressionSyntax invoke)
        {
            if (translateOpt != null)
            {
                if (translateOpt.WriteTypeParameters(writer, invoke))
                {
                    return;
                }
            }

            if (typeParameters != null)
            {
                writer.Write(typeParameters);
            }
        }
示例#28
0
        public static void Go(ScalaWriter writer, AnonymousObjectCreationExpressionSyntax expression)
        {
            writer.Write("new ");
            writer.Write(TypeName(expression));
            writer.Write("(");

            bool first = true;

            foreach (var field in expression.Initializers.OrderBy(o => o.Name()))
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write(", ");
                }

                Core.Write(writer, field.Expression);
            }

            writer.Write(")");
        }
示例#29
0
        /// <summary>
        /// calls to Enum.Parse get re-written as calls to CsScala.EnumTryParse, where we pass in our special Parse methods on each enum.
        /// </summary>
        private static void WriteEnumTryParse(ScalaWriter writer, InvocationExpressionSyntax invocationExpression)
        {
            var args = invocationExpression.ArgumentList.Arguments;

            if (args.Count != 2)
            {
                throw new Exception("Expected 2 args to Enum.TryParse.  Other overloads are not supported");
            }

            writer.Write("System.CsScala.EnumTryParse(");
            Core.Write(writer, args[0].Expression);
            writer.Write(", ");
            writer.Write(args[1].Expression.As <IdentifierNameSyntax>().Identifier.ToString());
            writer.Write(", ");

            var type = Program.GetModel(invocationExpression).GetTypeInfo(args[1].Expression).Type;

            writer.Write(type.ContainingNamespace.FullNameWithDot());
            writer.Write(WriteType.TypeName((INamedTypeSymbol)type));
            writer.Write(".Parse)");
        }
示例#30
0
        public static void ProcessTrivias(ScalaWriter writer, IEnumerable <SyntaxTrivia> trivias)
        {
            bool literalCode = false; //if we encounter a #if CSSCALA, we set this to true, which indicates that the next DisabledTextTrivia should be written as pure code.

            foreach (var trivia in trivias)
            {
                if (_triviaProcessed.TryAdd(trivia, null)) //ensure we don't look at the same trivia multiple times
                {
                    if (trivia.Kind() == SyntaxKind.IfDirectiveTrivia)
                    {
                        if (GetConditions(trivia, "#if ").Contains("CSSCALA"))
                        {
                            literalCode = true;
                        }
                    }
                    else if (trivia.Kind() == SyntaxKind.DisabledTextTrivia && literalCode)
                    {
                        writer.Write(trivia.ToString());
                        literalCode = false;
                    }
                }
            }
        }