Beispiel #1
0
        protected internal void Accept(SyntaxToken token)
        {
            if (token != null)
            {
                foreach (var error in token.GetDiagnostics())
                {
                    Context.ErrorSink.OnError(error);
                }

                TokenBuilder.Add(token);
            }
        }
Beispiel #2
0
        protected internal void Accept(IEnumerable <SyntaxToken> tokens)
        {
            foreach (var token in tokens)
            {
                foreach (var error in token.GetDiagnostics())
                {
                    Context.ErrorSink.OnError(error);
                }

                TokenBuilder.Add(token);
            }
        }
Beispiel #3
0
        protected internal void Accept(SyntaxToken token)
        {
            if (token != null)
            {
                if (token.Kind == SyntaxKind.NewLine)
                {
                    Context.StartOfLine = true;
                }
                else if (token.Kind != SyntaxKind.Whitespace)
                {
                    Context.StartOfLine = false;
                }

                foreach (var error in token.GetDiagnostics())
                {
                    Context.ErrorSink.OnError(error);
                }

                TokenBuilder.Add(token);
            }
        }
        // TODO(krait): Can't it render messages directly to TextWriter? But let's not change it yet..
        /// <summary>
        /// <para>Renders the template to a fully formed log message, replacing placeholders with values from <paramref name="properties"/>.</para>
        /// <para>See <see cref="LogEvent.MessageTemplate"/> for details on <paramref name="template"/> format.</para>
        /// <para>This method never throws exceptions.</para>
        /// </summary>
        /// <param name="template">A message template with zero or more placeholders to substitute.</param>
        /// <param name="properties">A dictionary of properties to be used for substitution.</param>
        public static string FormatMessage([CanBeNull] string template, [CanBeNull] IReadOnlyDictionary <string, object> properties)
        {
            if (template == null)
            {
                return(null);
            }

            if (properties == null)
            {
                return(template);
            }

            var resultBuilder     = StringBuilderCache.Acquire(template.Length * 2);
            var tokenBuilderChars = CharArrayCache.Acquire(template.Length);
            var tokenBuilder      = new TokenBuilder(tokenBuilderChars);

            for (var i = 0; i < template.Length; i++)
            {
                var currentChar = template[i];

                if (currentChar != '{' && currentChar != '}')
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                if (!tokenBuilder.IsEmpty)
                {
                    tokenBuilder.MoveToResult(resultBuilder);
                }

                if (i == template.Length - 1)
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                var nextChar = template[i + 1];
                if (currentChar == nextChar)
                {
                    tokenBuilder.Add(currentChar);
                    i++;
                    continue;
                }

                if (currentChar == '}')
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                var findTokenResult = tokenBuilder.TryFindToken(template, i);

                i += tokenBuilder.Length - 1;

                if (findTokenResult)
                {
                    var key = tokenBuilder.GetKeyFromBuffer();
                    if (properties.TryGetValue(key, out var value))
                    {
                        resultBuilder.Append(FormatPropertyValue(value));
                        tokenBuilder.Clear();
                    }
                }
            }

            if (!tokenBuilder.IsEmpty)
            {
                tokenBuilder.MoveToResult(resultBuilder);
            }

            CharArrayCache.Return(tokenBuilderChars);

            return(StringBuilderCache.GetStringAndRelease(resultBuilder));
        }