internal LiteralExpressionSyntax ToStringLiteralExpression()
        {
            ThrowInvalidOperationIfNotInitialized();

            StringConcatenationAnalysis analysis = Analyze();

            ThrowIfContainsNonStringLiteralExpression(analysis);

            StringBuilder sb = StringBuilderCache.GetInstance();

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

            sb.Append('"');

            foreach (ExpressionSyntax expression in Expressions(leftToRight: true))
            {
                StringLiteralExpressionInfo literal = SyntaxInfo.StringLiteralExpressionInfo(expression);

                if (literal.Success)
                {
                    if (analysis.ContainsNonVerbatimExpression && literal.IsVerbatim)
                    {
                        int startIndex = sb.Length;
                        sb.Append(literal.ValueText);
                        sb.Replace(@"\", @"\\", startIndex);
                        sb.Replace("\"", @"\" + "\"", startIndex);
                        sb.Replace("\n", @"\n", startIndex);
                        sb.Replace("\r", @"\r", startIndex);
                    }
                    else
                    {
                        sb.Append(literal.InnerText);
                    }
                }
            }

            sb.Append('"');

            return((LiteralExpressionSyntax)ParseExpression(StringBuilderCache.GetStringAndFree(sb)));
        }
Exemple #2
0
        public static StringConcatenationAnalysis Create(StringConcatenationExpressionInfo stringConcatenation)
        {
            var flags = StringConcatenationFlags.None;

            foreach (ExpressionSyntax expression in stringConcatenation.Expressions())
            {
                StringLiteralExpressionInfo stringLiteral = SyntaxInfo.StringLiteralExpressionInfo(expression);

                if (stringLiteral.Success)
                {
                    if (stringLiteral.IsVerbatim)
                    {
                        flags |= StringConcatenationFlags.ContainsVerbatimStringLiteral;
                    }
                    else
                    {
                        flags |= StringConcatenationFlags.ContainsRegularStringLiteral;
                    }
                }
                else if (expression.Kind() == SyntaxKind.InterpolatedStringExpression)
                {
                    if (((InterpolatedStringExpressionSyntax)expression).IsVerbatim())
                    {
                        flags |= StringConcatenationFlags.ContainsVerbatimInterpolatedString;
                    }
                    else
                    {
                        flags |= StringConcatenationFlags.ContainsRegularInterpolatedString;
                    }
                }
                else
                {
                    flags |= StringConcatenationFlags.ContainsUnspecifiedExpression;
                }
            }

            return(new StringConcatenationAnalysis(flags));
        }
        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)));
        }