コード例 #1
0
        internal InterpolatedStringExpressionSyntax ToInterpolatedStringExpression()
        {
            ThrowInvalidOperationIfNotInitialized();

            StringBuilder sb = StringBuilderCache.GetInstance();

            sb.Append('$');

            bool containsRegular = Analyze().ContainsNonVerbatimExpression;

            if (!containsRegular)
            {
                sb.Append('@');
            }

            sb.Append('"');

            foreach (ExpressionSyntax expression in Expressions(leftToRight: true))
            {
                SyntaxKind kind = expression.Kind();

                StringLiteralExpressionInfo stringLiteral = SyntaxInfo.StringLiteralExpressionInfo(expression);

                if (stringLiteral.Success)
                {
                    int startIndex = sb.Length;

                    if (containsRegular &&
                        stringLiteral.IsVerbatim)
                    {
                        sb.Append(stringLiteral.ValueText);
                        sb.Replace(@"\", @"\\", startIndex);
                        sb.Replace("\"", @"\" + "\"", startIndex);
                        sb.Replace("{", "{{", startIndex);
                        sb.Replace("}", "}}", startIndex);
                        sb.Replace("\n", @"\n", startIndex);
                        sb.Replace("\r", @"\r", startIndex);
                    }
                    else
                    {
                        sb.Append(stringLiteral.InnerText);
                        sb.Replace("{", "{{", startIndex);
                        sb.Replace("}", "}}", startIndex);
                    }
                }
                else if (kind == SyntaxKind.InterpolatedStringExpression)
                {
                    var interpolatedString = (InterpolatedStringExpressionSyntax)expression;

                    bool isVerbatimInterpolatedString = interpolatedString.IsVerbatim();

                    foreach (InterpolatedStringContentSyntax content in interpolatedString.Contents)
                    {
                        Debug.Assert(content.IsKind(SyntaxKind.Interpolation, SyntaxKind.InterpolatedStringText), content.Kind().ToString());

                        switch (content.Kind())
                        {
                        case SyntaxKind.InterpolatedStringText:
                        {
                            var text = (InterpolatedStringTextSyntax)content;

                            if (containsRegular &&
                                isVerbatimInterpolatedString)
                            {
                                int startIndex = sb.Length;
                                sb.Append(text.TextToken.ValueText);
                                sb.Replace(@"\", @"\\", startIndex);
                                sb.Replace("\"", @"\" + "\"", startIndex);
                                sb.Replace("\n", @"\n", startIndex);
                                sb.Replace("\r", @"\r", startIndex);
                            }
                            else
                            {
                                sb.Append(content.ToString());
                            }

                            break;
                        }

                        case SyntaxKind.Interpolation:
                        {
                            sb.Append(content.ToString());
                            break;
                        }
                        }
                    }
                }
                else
                {
                    sb.Append('{');
                    sb.Append(expression.ToString());
                    sb.Append('}');
                }
            }

            sb.Append("\"");

            return((InterpolatedStringExpressionSyntax)ParseExpression(StringBuilderCache.GetStringAndFree(sb)));
        }