public Visitor(
     CommonObjectFormatter formatter,
     BuilderOptions builderOptions,
     CommonPrimitiveFormatterOptions primitiveOptions,
     CommonTypeNameFormatterOptions typeNameOptions,
     MemberDisplayFormat memberDisplayFormat)
 {
     _formatter           = formatter;
     _builderOptions      = builderOptions;
     _primitiveOptions    = primitiveOptions;
     _typeNameOptions     = typeNameOptions;
     _memberDisplayFormat = memberDisplayFormat;
 }
 public Visitor(
     CommonObjectFormatter formatter,
     BuilderOptions builderOptions,
     CommonPrimitiveFormatterOptions primitiveOptions,
     CommonTypeNameFormatterOptions typeNameOptions,
     MemberDisplayFormat memberDisplayFormat)
 {
     _formatter = formatter;
     _builderOptions = builderOptions;
     _primitiveOptions = primitiveOptions;
     _typeNameOptions = typeNameOptions;
     _memberDisplayFormat = memberDisplayFormat;
 }
        private void AppendArrayBound(StringBuilder sb, long bound, int numberRadix)
        {
            var options = new CommonPrimitiveFormatterOptions(
                numberRadix: numberRadix,
                includeCodePoints: false,
                quoteStringsAndCharacters: true,
                escapeNonPrintableCharacters: true,
                cultureInfo: CultureInfo.InvariantCulture);
            var formatted = int.MinValue <= bound && bound <= int.MaxValue
                ? PrimitiveFormatter.FormatPrimitive((int)bound, options)
                : PrimitiveFormatter.FormatPrimitive(bound, options);

            sb.Append(formatted);
        }
        /// <summary>
        /// Returns null if the type is not considered primitive in the target language.
        /// </summary>
        public string FormatPrimitive(object obj, CommonPrimitiveFormatterOptions options)
        {
            if (ReferenceEquals(obj, VoidValue))
            {
                return string.Empty;
            }

            if (obj == null)
            {
                return NullLiteral;
            }

            var type = obj.GetType();

            if (type.GetTypeInfo().IsEnum)
            {
                return obj.ToString();
            }

            switch (GetPrimitiveSpecialType(type))
            {
                case SpecialType.System_Int32:
                    return FormatLiteral((int)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_String:
                    return FormatLiteral((string)obj, options.QuoteStringsAndCharacters, options.EscapeNonPrintableCharacters, options.NumberRadix);

                case SpecialType.System_Boolean:
                    return FormatLiteral((bool)obj);

                case SpecialType.System_Char:
                    return FormatLiteral((char)obj, options.QuoteStringsAndCharacters, options.EscapeNonPrintableCharacters, options.IncludeCharacterCodePoints, options.NumberRadix);

                case SpecialType.System_Int64:
                    return FormatLiteral((long)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_Double:
                    return FormatLiteral((double)obj, options.CultureInfo);

                case SpecialType.System_Byte:
                    return FormatLiteral((byte)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_Decimal:
                    return FormatLiteral((decimal)obj, options.CultureInfo);

                case SpecialType.System_UInt32:
                    return FormatLiteral((uint)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_UInt64:
                    return FormatLiteral((ulong)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_Single:
                    return FormatLiteral((float)obj, options.CultureInfo);

                case SpecialType.System_Int16:
                    return FormatLiteral((short)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_UInt16:
                    return FormatLiteral((ushort)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_DateTime:
                    return FormatLiteral((DateTime)obj, options.CultureInfo);

                case SpecialType.System_SByte:
                    return FormatLiteral((sbyte)obj, options.NumberRadix, options.CultureInfo);

                case SpecialType.System_Object:
                case SpecialType.System_Void:
                case SpecialType.None:
                    return null;

                default:
                    throw ExceptionUtilities.UnexpectedValue(GetPrimitiveSpecialType(type));
            }
        }
 private void AppendArrayBound(StringBuilder sb, long bound, int numberRadix)
 {
     var options = new CommonPrimitiveFormatterOptions(
         numberRadix: numberRadix,
         includeCodePoints: false,
         quoteStringsAndCharacters: true,
         escapeNonPrintableCharacters: true,
         cultureInfo: CultureInfo.InvariantCulture);
     var formatted = int.MinValue <= bound && bound <= int.MaxValue
         ? PrimitiveFormatter.FormatPrimitive((int)bound, options)
         : PrimitiveFormatter.FormatPrimitive(bound, options);
     sb.Append(formatted);
 }
Beispiel #6
0
            private Builder FormatWithEmbeddedExpressions(Builder result, string format, object obj)
            {
                int i = 0;

                while (i < format.Length)
                {
                    char c = format[i++];
                    if (c == '{')
                    {
                        if (i >= 2 && format[i - 2] == '\\')
                        {
                            result.Append('{');
                        }
                        else
                        {
                            int expressionEnd = format.IndexOf('}', i);

                            bool   noQuotes, callableOnly;
                            string memberName;
                            if (expressionEnd == -1 || (memberName = ParseSimpleMemberName(format, i, expressionEnd, out noQuotes, out callableOnly)) == null)
                            {
                                // the expression isn't properly formatted
                                result.Append(format, i - 1, format.Length - i + 1);
                                break;
                            }

                            MemberInfo member = ResolveMember(obj, memberName, callableOnly);
                            if (member == null)
                            {
                                result.AppendFormat(callableOnly ? "!<Method '{0}' not found>" : "!<Member '{0}' not found>", memberName);
                            }
                            else
                            {
                                Exception exception;
                                object    value = GetMemberValue(member, obj, out exception);

                                if (exception != null)
                                {
                                    FormatException(result, exception);
                                }
                                else
                                {
                                    MemberDisplayFormat             oldMemberDisplayFormat = _memberDisplayFormat;
                                    CommonPrimitiveFormatterOptions oldPrimitiveOptions    = _primitiveOptions;

                                    _memberDisplayFormat = MemberDisplayFormat.Hidden;
                                    _primitiveOptions    = new CommonPrimitiveFormatterOptions(
                                        _primitiveOptions.NumberRadix,
                                        _primitiveOptions.IncludeCharacterCodePoints,
                                        quoteStringsAndCharacters: !noQuotes,
                                        escapeNonPrintableCharacters: _primitiveOptions.EscapeNonPrintableCharacters,
                                        cultureInfo: _primitiveOptions.CultureInfo);

                                    string _;
                                    FormatObjectRecursive(result, value, isRoot: false, debuggerDisplayName: out _);

                                    _primitiveOptions    = oldPrimitiveOptions;
                                    _memberDisplayFormat = oldMemberDisplayFormat;
                                }
                            }
                            i = expressionEnd + 1;
                        }
                    }
                    else
                    {
                        result.Append(c);
                    }
                }

                return(result);
            }
            private Builder FormatWithEmbeddedExpressions(Builder result, string format, object obj)
            {
                int i = 0;
                while (i < format.Length)
                {
                    char c = format[i++];
                    if (c == '{')
                    {
                        if (i >= 2 && format[i - 2] == '\\')
                        {
                            result.Append('{');
                        }
                        else
                        {
                            int expressionEnd = format.IndexOf('}', i);

                            bool noQuotes, callableOnly;
                            string memberName;
                            if (expressionEnd == -1 || (memberName = ParseSimpleMemberName(format, i, expressionEnd, out noQuotes, out callableOnly)) == null)
                            {
                                // the expression isn't properly formatted
                                result.Append(format, i - 1, format.Length - i + 1);
                                break;
                            }

                            MemberInfo member = ResolveMember(obj, memberName, callableOnly);
                            if (member == null)
                            {
                                result.AppendFormat(callableOnly ? "!<Method '{0}' not found>" : "!<Member '{0}' not found>", memberName);
                            }
                            else
                            {
                                Exception exception;
                                object value = GetMemberValue(member, obj, out exception);

                                if (exception != null)
                                {
                                    FormatException(result, exception);
                                }
                                else
                                {
                                    MemberDisplayFormat oldMemberDisplayFormat = _memberDisplayFormat;
                                    CommonPrimitiveFormatterOptions oldPrimitiveOptions = _primitiveOptions;

                                    _memberDisplayFormat = MemberDisplayFormat.Hidden;
                                    _primitiveOptions = new CommonPrimitiveFormatterOptions(
                                        _primitiveOptions.NumberRadix,
                                        _primitiveOptions.IncludeCharacterCodePoints,
                                        quoteStringsAndCharacters: !noQuotes,
                                        escapeNonPrintableCharacters: _primitiveOptions.EscapeNonPrintableCharacters,
                                        cultureInfo: _primitiveOptions.CultureInfo);

                                    string _;
                                    FormatObjectRecursive(result, value, isRoot: false, debuggerDisplayName: out _);

                                    _primitiveOptions = oldPrimitiveOptions;
                                    _memberDisplayFormat = oldMemberDisplayFormat;
                                }
                            }
                            i = expressionEnd + 1;
                        }
                    }
                    else
                    {
                        result.Append(c);
                    }
                }

                return result;
            }
Beispiel #8
0
        /// <summary>
        /// Returns null if the type is not considered primitive in the target language.
        /// </summary>
        public string FormatPrimitive(object obj, CommonPrimitiveFormatterOptions options)
        {
            if (ReferenceEquals(obj, VoidValue))
            {
                return(string.Empty);
            }

            if (obj == null)
            {
                return(NullLiteral);
            }

            var type = obj.GetType();

            if (type.GetTypeInfo().IsEnum)
            {
                return(obj.ToString());
            }

            switch (GetPrimitiveSpecialType(type))
            {
            case SpecialType.System_Int32:
                return(FormatLiteral((int)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_String:
                return(FormatLiteral((string)obj, options.QuoteStringsAndCharacters, options.EscapeNonPrintableCharacters, options.NumberRadix));

            case SpecialType.System_Boolean:
                return(FormatLiteral((bool)obj));

            case SpecialType.System_Char:
                return(FormatLiteral((char)obj, options.QuoteStringsAndCharacters, options.EscapeNonPrintableCharacters, options.IncludeCharacterCodePoints, options.NumberRadix));

            case SpecialType.System_Int64:
                return(FormatLiteral((long)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_Double:
                return(FormatLiteral((double)obj, options.CultureInfo));

            case SpecialType.System_Byte:
                return(FormatLiteral((byte)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_Decimal:
                return(FormatLiteral((decimal)obj, options.CultureInfo));

            case SpecialType.System_UInt32:
                return(FormatLiteral((uint)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_UInt64:
                return(FormatLiteral((ulong)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_Single:
                return(FormatLiteral((float)obj, options.CultureInfo));

            case SpecialType.System_Int16:
                return(FormatLiteral((short)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_UInt16:
                return(FormatLiteral((ushort)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_DateTime:
                return(FormatLiteral((DateTime)obj, options.CultureInfo));

            case SpecialType.System_SByte:
                return(FormatLiteral((sbyte)obj, options.NumberRadix, options.CultureInfo));

            case SpecialType.System_Object:
            case SpecialType.System_Void:
            case SpecialType.None:
                return(null);

            default:
                throw ExceptionUtilities.UnexpectedValue(GetPrimitiveSpecialType(type));
            }
        }