Ejemplo n.º 1
0
        /// <summary>
        /// Turn a (parsed) interpolated string nonterminal into an interpolated string token.
        /// </summary>
        /// <param name="interpolatedString"></param>
        /// <returns></returns>
        static internal SyntaxToken RescanInterpolatedString(InterpolatedStringSyntax interpolatedString)
        {
            var text = interpolatedString.ToString();

            // TODO: scan the contents to reconstruct any lexical errors such as // inside an expression hole
            return(SyntaxFactory.Literal(interpolatedString.GetLeadingTrivia(), text, SyntaxKind.InterpolatedStringToken, text, interpolatedString.GetTrailingTrivia()));
        }
Ejemplo n.º 2
0
        private BoundExpression BindInterpolatedString(InterpolatedStringSyntax node, DiagnosticBag diagnostics)
        {
            var builder = ArrayBuilder <BoundExpression> .GetInstance();

            SyntaxToken stringStart = node.StringStart;

            Debug.Assert(stringStart.CSharpKind() == SyntaxKind.InterpolatedStringStartToken);
            var stringType = GetSpecialType(SpecialType.System_String, diagnostics, node);
            var objectType = GetSpecialType(SpecialType.System_Object, diagnostics, node);

            builder.Add(new BoundLiteral(node, ConstantValue.Create(stringStart.ValueText, SpecialType.System_String), stringType));
            var inserts = node.InterpolatedInserts.GetWithSeparators();

            for (int i = 0; i < inserts.Count; i++)
            {
                if (i % 2 == 0)
                {
                    // an expression hole
                    var             expr      = (InterpolatedStringInsertSyntax)inserts[i].AsNode();
                    var             bound     = this.GenerateConversionForAssignment(objectType, this.BindExpression(expr.Expression, diagnostics), diagnostics);
                    BoundExpression alignment = expr.Alignment != null?BindExpression(expr.Alignment, diagnostics) : null;

                    BoundExpression format = null;
                    if (expr.Format != default(SyntaxToken))
                    {
                        switch (expr.Format.CSharpKind())
                        {
                        case SyntaxKind.IdentifierToken:
                        case SyntaxKind.StringLiteralToken:
                            format = new BoundLiteral(expr, ConstantValue.Create(expr.Format.ValueText), stringType);
                            break;

                        default:
                            Debug.Assert(expr.HasErrors);
                            break;
                        }
                    }

                    builder.Add(new BoundStringInsert(expr, bound, alignment, format, null));
                }
                else
                {
                    // the separator token, which is part of the string literal
                    var token = inserts[i].AsToken();
                    var bound = new BoundLiteral(node, ConstantValue.Create(token.ValueText, SpecialType.System_String), stringType);
                    builder.Add(bound);
                }
            }

            SyntaxToken stringEnd = node.StringEnd;

            builder.Add(new BoundLiteral(node, ConstantValue.Create(stringEnd.ValueText, SpecialType.System_String), stringType));
            return(new BoundInterpolatedString(node, builder.ToImmutableAndFree(), stringType));
        }