Exemplo n.º 1
0
 public virtual void VisitStringSyntax(StringSyntax syntax)
 {
     for (var i = 0; i < syntax.Expressions.Length; i++)
     {
         this.Visit(syntax.StringTokens[i]);
         this.Visit(syntax.Expressions[i]);
     }
     this.Visit(syntax.StringTokens.Last());
 }
Exemplo n.º 2
0
        /// <summary>
        /// Try to get the string literal value for a syntax node. This method should only be called after verifying the string is not interpolated.
        /// </summary>
        /// <param name="syntax">The string syntax node</param>
        public static string GetLiteralValue(this StringSyntax syntax)
        {
            var value = syntax.SegmentValues.SingleOrDefault();

            if (value == null)
            {
                throw new ArgumentException("Cannot return literal value for interpolated string");
            }

            return(value);
        }
Exemplo n.º 3
0
        protected virtual SyntaxBase ReplaceStringSyntax(StringSyntax syntax)
        {
            var hasChanges = TryRewriteStrict(syntax.StringTokens, out var stringTokens);

            hasChanges |= TryRewrite(syntax.Expressions, out var expressions);

            if (!hasChanges)
            {
                return(syntax);
            }

            var segmentValues = Lexer.TryGetRawStringSegments(stringTokens.ToArray());

            if (segmentValues == null)
            {
                throw new ArgumentException($"Failed to parse string tokens");
            }

            return(new StringSyntax(stringTokens, expressions, segmentValues));
        }
Exemplo n.º 4
0
 void ISyntaxVisitor.VisitStringSyntax(StringSyntax syntax) => ReplaceCurrent(syntax, ReplaceStringSyntax);
Exemplo n.º 5
0
 /// <summary>
 /// Checks if the syntax node contains an interpolated string or a literal string.
 /// </summary>
 /// <param name="syntax">The string syntax node</param>
 public static bool IsInterpolated(this StringSyntax syntax)
 => syntax.SegmentValues.Length > 1;
 /// <summary>
 /// Try to get the string literal value for a syntax node. Returns null if the string is interpolated.
 /// </summary>
 /// <param name="syntax">The string syntax node</param>
 public static string?TryGetLiteralValue(this StringSyntax syntax)
 => syntax.IsInterpolated() ? null : syntax.SegmentValues[0];