Ejemplo n.º 1
0
        public IEnumerable <FormatSpecifier> CalculateMainFormatSpecifiers(string formatString)
        {
            // The user has specified a custom string
            if (string.IsNullOrWhiteSpace(formatString))
            {
                return(null);
            }

            if (!ValidStringFormat(formatString))
            {
                return(null);
            }

            /*
             * Validated a valid DateTime Format from the user.
             * Calculate the positions of each data type.
             */
            IFormatSpecifierFactory factory = new MainControlFormatSpecifierFactory();

            string[]          specifiers       = formatString.Split(FormatStringSeperators.Seperators);
            FormatSpecifier[] formatSpecifiers = new FormatSpecifier[specifiers.Length];
            for (int i = 0; i < specifiers.Length; i++)
            {
                FormatSpecifier formatSpecifier = factory.CreateFormatSpecifier(specifiers[i], i);
                formatSpecifiers[i] = formatSpecifier;
            }

            return(formatSpecifiers);
        }
Ejemplo n.º 2
0
            /// <summary>
            /// Parse a string field
            /// </summary>
            private bool ParseString(TextParser input, FormatSpecifier spec)
            {
                // Skip any whitespace
                input.MovePastWhitespace();

                // Parse string characters
                int start = input.Position;

                while (!input.EndOfText && !Char.IsWhiteSpace(input.Peek()))
                {
                    input.MoveAhead();
                }

                // Don't exceed field width
                if (spec.Width > 0)
                {
                    int count = input.Position - start;
                    if (spec.Width < count)
                    {
                        input.MoveAhead(spec.Width - count);
                    }
                }

                // Extract token
                if (input.Position > start)
                {
                    if (!spec.NoResult)
                    {
                        Results.Add(input.Extract(start, input.Position));
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 3
0
        // TODO: handle the situation when raw and expand formatter are both present
        public virtual IVariableInformation Create(
            RemoteValue remoteValue, string displayName = null,
            FormatSpecifier formatSpecifier             = null,
            CustomVisualizer customVisualizer           = CustomVisualizer.None)
        {
            IVariableInformation varInfo =
                _varInfoFactory.Create(remoteValue, displayName, formatSpecifier, customVisualizer);

            // Don't use Natvis for raw format specifier (!), e.g. "myvar,!".
            if (FormatSpecifierUtil.HasRawFormatSpecifier(varInfo.FormatSpecifier))
            {
                return(new CachingVariableInformation(varInfo));
            }

            var natvisVarInfo = new NatvisVariableInformation(_natvisExpander, varInfo);

            if (ExpandFormatSpecifierUtil.TryParseExpandFormatSpecifier(
                    natvisVarInfo.FormatSpecifier, out int expandedIndex))
            {
                return(new CachingVariableInformation(
                           _expandVariableFactory.Create(natvisVarInfo, expandedIndex)));
            }

            return(new CachingVariableInformation(natvisVarInfo));
        }
Ejemplo n.º 4
0
 public string FormatAsTree(int indentLevel, FormatSpecifier formatSpecifier)
 {
     return(new PPGroup(
                new PPMoveToIndent(),
                this)
            .Format(indentLevel, formatSpecifier));
 }
Ejemplo n.º 5
0
        public override string Format(int indentLevel, FormatSpecifier formatSpecifier)
        {
            formatSpecifier = formatSpecifier.Recalc(Tag);

            return formatSpecifier.NewlineString +
                   PPMoveToIndent.CalcIndent(indentLevel, formatSpecifier.IndentString);
        }
Ejemplo n.º 6
0
        /* Converts the value of a specified reflection object to an equivalent
           string representation using specified format */
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (arg == null)
                throw new ArgumentNullException("arg");

            string result = null;
            FormatSpecifier specifier = new FormatSpecifier(format);

            if (specifier.IsValid)
            {
                if (arg is Assembly)
                    result = formatAssembly(specifier,arg as Assembly);
                else if (arg is Module)
                    result = formatModule(specifier,arg as Module);
                else if (arg is Type)
                    result = formatType(specifier,arg as Type,false);
                else if (arg is ConstructorInfo)
                    result = formatConstructorInfo(specifier,arg as ConstructorInfo);
                else if (arg is MethodInfo)
                    result = formatMethodInfo(specifier,arg as MethodInfo);
                else if (arg is FieldInfo)
                    result = formatFieldInfo(specifier,arg as FieldInfo);
                else if (arg is ParameterInfo)
                    result = formatParameterInfo(specifier,arg as ParameterInfo);
                else if (arg is Instruction)
                    result = (arg as Instruction).ToString(format,this);
            }

            if (result == null)
                    result = arg.ToString();

            return result;
        }
Ejemplo n.º 7
0
            /// <summary>
            /// Parse an octal field
            /// </summary>
            private bool ParseOctal(TextParser input, FormatSpecifier spec)
            {
                // Skip any whitespace
                input.MovePastWhitespace();

                // Parse digits
                int start = input.Position;

                while (IsValidDigit(input.Peek(), 8))
                {
                    input.MoveAhead();
                }

                // Don't exceed field width
                if (spec.Width > 0)
                {
                    int count = input.Position - start;
                    if (spec.Width < count)
                    {
                        input.MoveAhead(spec.Width - count);
                    }
                }

                // Extract token
                if (input.Position > start)
                {
                    if (!spec.NoResult)
                    {
                        AddUnsigned(input.Extract(start, input.Position), spec.Modifier, 8);
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 8
0
 public string FormatAsTree(int indentLevel, FormatSpecifier formatSpecifier)
 {
     return new PPGroup(
                     new PPMoveToIndent(),
                     this)
                 .Format(indentLevel, formatSpecifier);
 }
Ejemplo n.º 9
0
            /// <summary>
            /// Parse a character field
            /// </summary>
            private bool ParseCharacter(TextParser input, FormatSpecifier spec)
            {
                // Parse character(s)
                int start = input.Position;
                int count = (spec.Width > 1) ? spec.Width : 1;

                while (!input.EndOfText && count-- > 0)
                {
                    input.MoveAhead();
                }

                // Extract token
                if (count <= 0 && input.Position > start)
                {
                    if (!spec.NoResult)
                    {
                        string token = input.Extract(start, input.Position);
                        if (token.Length > 1)
                        {
                            Results.Add(token.ToCharArray());
                        }
                        else
                        {
                            Results.Add(token[0]);
                        }
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 10
0
        public override string Format(int indentLevel, FormatSpecifier formatSpecifier)
        {
            formatSpecifier = formatSpecifier.Recalc(Tag);

            return(formatSpecifier.NewlineString +
                   PPMoveToIndent.CalcIndent(indentLevel, formatSpecifier.IndentString));
        }
Ejemplo n.º 11
0
            /// <summary>
            /// Parse integer field
            /// </summary>
            private bool ParseDecimal(TextParser input, FormatSpecifier spec)
            {
                int radix = 10;

                // Skip any whitespace
                input.MovePastWhitespace();

                // Parse leading sign
                int start = input.Position;

                if (input.Peek() == '+' || input.Peek() == '-')
                {
                    input.MoveAhead();
                    // Disable Octal et Hexadecimal format
                    //} else if (input.Peek() == '0') {
                    //    if (Char.ToLower(input.Peek(1)) == 'x') {
                    //        radix = 16;
                    //        input.MoveAhead(2);
                    //    } else {
                    //        radix = 8;
                    //        input.MoveAhead();
                    //    }
                }

                // Parse digits
                while (IsValidDigit(input.Peek(), radix))
                {
                    input.MoveAhead();
                }

                // Don't exceed field width
                if (spec.Width > 0)
                {
                    int count = input.Position - start;
                    if (spec.Width < count)
                    {
                        input.MoveAhead(spec.Width - count);
                    }
                }

                // Extract token
                if (input.Position > start)
                {
                    if (!spec.NoResult)
                    {
                        if (spec.Type == Types.Decimal)
                        {
                            AddSigned(input.Extract(start, input.Position), spec.Modifier, radix);
                        }
                        else
                        {
                            AddUnsigned(input.Extract(start, input.Position), spec.Modifier, radix);
                        }
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 12
0
        private string formatParameters(FormatSpecifier format, ParameterInfo[] parms)
        {
            string result = "(";

            for (int i = 0; i < parms.Length; i++)
            {
                result += formatParameterInfo(format, parms[i]) + (i < parms.Length - 1 ? ", " : "");
            }

            return(result + ')');
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Parse integer field
        /// </summary>
        static bool ParseDecimal(Action <object> add, TextVisiter input, FormatSpecifier spec)
        {
            // Skip any whitespace
            input.MovePastWhitespace();

            // Parse leading sign
            var radix = 10;
            var start = input.Position;

            if (input.Peek() == '+' || input.Peek() == '-')
            {
                input.MoveAhead();
            }
            else if (input.Peek() == '0')
            {
                if (char.ToLower(input.Peek(1)) == 'x')
                {
                    radix = 16; input.MoveAhead(2);
                }
                else
                {
                    radix = 8; input.MoveAhead();
                }
            }

            // Parse digits
            while (IsValidDigit(input.Peek(), radix))
            {
                input.MoveAhead();
            }

            // Don't exceed field width
            if (spec.Width > 0)
            {
                var count = input.Position - start;
                if (spec.Width < count)
                {
                    input.MoveAhead(spec.Width - count);
                }
            }

            // Extract token
            if (input.Position > start)
            {
                if (!spec.NoResult)
                {
                    add(spec.Type == Types.Decimal
                        ? Signed(input.Extract(start, input.Position), spec.Modifier, radix)
                        : Unsigned(input.Extract(start, input.Position), spec.Modifier, radix));
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 14
0
        public Task <Result <MediaLocation> > HandleVideoDownloadRequest(MediaDownloadJob job)
        {
            if (!Uri.TryCreate(job.DownloadLink, UriKind.Absolute, out _))
            {
                return(Task.FromResult(Result.Failure <MediaLocation>("The URL is not valid")));
            }

            var commandlineArguments = new List <string> {
                $"\"{job.DownloadLink}\"",
                "-c",
                "-i",
                "-w",
                "--no-part",
                "--no-call-home",
                $"-o \"{_vidloadConfiguration.FilesystemConfiguration.DownloadDirectory}/{job.TraceId}.%(ext)s\"",
            };

            if (_vidloadConfiguration.NetworkConfiguration.UseProxy)
            {
                commandlineArguments.Add($"--proxy \"{_vidloadConfiguration.NetworkConfiguration.Proxy}\"");
            }


            if (FormatSpecifier.IsAudioFormat(job.TargetFormat))
            {
                commandlineArguments.Add("--extract-audio");
                commandlineArguments.Add($"--audio-format {job.TargetFormat.ToString().ToLower()}");
                commandlineArguments.Add("-f \"bestaudio\"");
            }

            if (FormatSpecifier.IsVideoFormat(job.TargetFormat))
            {
                commandlineArguments.Add("-f \"bestvideo\"");
                commandlineArguments.Add($"--recode-video {job.TargetFormat.ToString().ToLower()}");
            }

            var downloadResult = _shellCommandExecutor
                                 .Execute("youtube-dl", commandlineArguments, TimeSpan.FromHours(1));

            if (downloadResult.IsSuccess)
            {
                var fileName = $"{job.TraceId}.{FormatSpecifier.GetFileExtensionsForFormat(job.TargetFormat)}";
                var filePath = Path.Join(_vidloadConfiguration.FilesystemConfiguration.DownloadDirectory, fileName);

                return(Task.FromResult(File.Exists(filePath)
          ? Result.Success(new MediaLocation {
                    FilePath = filePath, TraceId = job.TraceId, DownloadLink = job.DownloadLink
                })
          : Result.Failure <MediaLocation>("Could not post-process the video. Output File not found")));
            }

            return(Task.FromResult(Result.Failure <MediaLocation>("WHOOOP")));
        }
Ejemplo n.º 15
0
        /* Converts the value of a specified reflection object to an equivalent
         * string representation using specified format */
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (arg == null)
            {
                throw new ArgumentNullException("arg");
            }

            string          result    = null;
            FormatSpecifier specifier = new FormatSpecifier(format);

            if (specifier.IsValid)
            {
                if (arg is Assembly)
                {
                    result = formatAssembly(specifier, arg as Assembly);
                }
                else if (arg is Module)
                {
                    result = formatModule(specifier, arg as Module);
                }
                else if (arg is Type)
                {
                    result = formatType(specifier, arg as Type, false);
                }
                else if (arg is ConstructorInfo)
                {
                    result = formatConstructorInfo(specifier, arg as ConstructorInfo);
                }
                else if (arg is MethodInfo)
                {
                    result = formatMethodInfo(specifier, arg as MethodInfo);
                }
                else if (arg is FieldInfo)
                {
                    result = formatFieldInfo(specifier, arg as FieldInfo);
                }
                else if (arg is ParameterInfo)
                {
                    result = formatParameterInfo(specifier, arg as ParameterInfo);
                }
                else if (arg is Instruction)
                {
                    result = (arg as Instruction).ToString(format, this);
                }
            }

            if (result == null)
            {
                result = arg.ToString();
            }

            return(result);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Convert given value into a number format.
        /// </summary>
        /// <param name="value">Value</param>
        /// <param name="numericFormat">https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings</param>
        /// <returns></returns>
        public static string NumberFormat(string value, FormatSpecifier numericFormat)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException(nameof(value));
            }
            var success = decimal.TryParse(value, out var result);

            if (!success)
            {
                throw new FormatException("String value is not a valid numeric value.");
            }
            return(NumberFormat(result, numericFormat));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Parses the input string according to the rules in the
        /// format string. Similar to the standard C library's
        /// sscanf() function. Parsed fields are placed in the
        /// class' Results member.
        /// </summary>
        /// <param name="input">String to parse</param>
        /// <param name="format">Specifies rules for parsing input</param>
        public int Parse(string input, string format)
        {
            input = JumpToText(input, format);

            TextParser      inp     = new TextParser(input);
            TextParser      fmt     = new TextParser(format);
            List <object>   results = new List <object>();
            FormatSpecifier spec    = new FormatSpecifier();
            int             count   = 0;

            // Clear any previous results
            Results.Clear();

            // Process input string as indicated in format string
            while (!fmt.EndOfText && !inp.EndOfText)
            {
                if (ParseFormatSpecifier(fmt, spec))
                {
                    // Found a format specifier
                    TypeParser parser = _typeParsers.First(tp => tp.Type == spec.Type);
                    if (parser.Parser(inp, spec))
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (Char.IsWhiteSpace(fmt.Peek()))
                {
                    // Whitespace
                    inp.MovePastWhitespace();
                    fmt.MoveAhead();
                }
                else if (fmt.Peek() == inp.Peek())
                {
                    // Matching character
                    inp.MoveAhead();
                    fmt.MoveAhead();
                }
                else
                {
                    break;     // Break at mismatch
                }
            }

            // Return number of fields successfully parsed
            return(count);
        }
Ejemplo n.º 18
0
        public IVariableInformation Clone(FormatSpecifier formatSpecifier)
        {
            RemoteValue clonedValue = _remoteValue.Clone();

            if (clonedValue == null)
            {
                return(null);
            }

            IVariableInformation clonedVarInfo =
                _varInfoBuilder.Create(clonedValue, DisplayName, formatSpecifier);

            clonedVarInfo.FallbackValueFormat = FallbackValueFormat;
            return(clonedVarInfo);
        }
Ejemplo n.º 19
0
 public static void PrintAsNumber(object number, FormatSpecifier formatSpecifier)
 {
     switch (formatSpecifier)
     {
         case FormatSpecifier.Precision:
             Console.WriteLine("{0:f2}", number);
             break;
         case FormatSpecifier.Percent:
             Console.WriteLine("{0:p0}", number);
             break;
         case FormatSpecifier.RightAlignment:
             Console.WriteLine("{0,8}", number);
             break;
         default:
             throw new ArgumentException("Invalid format specifier given.");
     }
 }
Ejemplo n.º 20
0
 public virtual IVariableInformation Create(
     RemoteValue remoteValue, string displayName = null,
     FormatSpecifier formatSpecifier             = null,
     CustomVisualizer customVisualizer           =
     CustomVisualizer
     .None) => new RemoteValueVariableInformation(_varInfoBuilder,
                                                  formatSpecifier != null
                                                              ? formatSpecifier.Expression
                                                              : string.Empty,
                                                  RemoteValueFormatProvider.Get(
                                                      formatSpecifier?.Expression,
                                                      formatSpecifier?.Size),
                                                  ValueFormat.Default, remoteValue,
                                                  displayName
                                                  ?? remoteValue.GetName(),
                                                  customVisualizer,
                                                  _childAdapterFactory);
Ejemplo n.º 21
0
        /// <summary>
        /// Parses the input string according to the rules in the
        /// format string. Similar to the standard C library's
        /// sscanf() function. Parsed fields are placed in the
        /// class' Results member.
        /// </summary>
        /// <param name="input">String to parse</param>
        /// <param name="format">Specifies rules for parsing input</param>
        public static int Parse(string input, string format, out IList <object> values)
        {
            var inp     = new TextVisiter(input);
            var fmt     = new TextVisiter(format);
            var results = new List <object>();
            var spec    = new FormatSpecifier();
            var count   = 0;

            // Process input string as indicated in format string
            while (!fmt.EndOfText && !inp.EndOfText)
            {
                if (ParseFormatSpecifier(fmt, spec))
                {
                    // Found a format specifier
                    var parser = _typeParsers.First(tp => tp.Type == spec.Type);
                    if (parser.Parser(results.Add, inp, spec))
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (char.IsWhiteSpace(fmt.Peek()))
                {
                    // Whitespace
                    inp.MovePastWhitespace();
                    fmt.MoveAhead();
                }
                else if (fmt.Peek() == inp.Peek())
                {
                    // Matching character
                    inp.MoveAhead();
                    fmt.MoveAhead();
                }
                else
                {
                    break;     // Break at mismatch
                }
            }
            // Return number of fields successfully parsed
            values = results;
            return(count);
        }
        public DateTimeContext Apply(TextBox textBox, DateTime?value, FormatSpecifier[] formatSpecifiers, out int start, out int length)
        {
            // No value or no text selected then prevent increment
            if (!value.HasValue || string.IsNullOrWhiteSpace(textBox.SelectedText))
            {
                start  = -1;
                length = -1;
                return(null);
            }

            string text = textBox.SelectedText.TrimEnd();

            if (!Regex.IsMatch(text, "^[0-9]*$"))
            {
                start  = -1;
                length = -1;
                return(null);
            }

            start  = textBox.SelectionStart;
            length = text.Length;

            int numberOfPreviousNumbers = 0;

            for (int i = 0; i < textBox.Text.Length; i++)
            {
                if (i == start) // Found start index
                {
                    break;
                }

                if (char.IsDigit(textBox.Text[i]))
                {
                    continue;
                }

                numberOfPreviousNumbers++; // Must have encountered as seperator. Inc
            }

            FormatSpecifier formatSpecifier = formatSpecifiers.FirstOrDefault(x => x.Index == numberOfPreviousNumbers);
            DateTimeContext dateTimeContext = new DateTimeContext(formatSpecifier);

            return(dateTimeContext);
        }
Ejemplo n.º 23
0
            /// <summary>
            /// Parse a scan-set field
            /// </summary>
            protected bool ParseScanSet(TextParser input, FormatSpecifier spec)
            {
                // Parse characters
                int start = input.Position;

                if (!spec.ScanSetExclude)
                {
                    while (spec.ScanSet.Contains(input.Peek()))
                    {
                        input.MoveAhead();
                    }
                }
                else
                {
                    while (!input.EndOfText && !spec.ScanSet.Contains(input.Peek()))
                    {
                        input.MoveAhead();
                    }
                }

                // Don't exceed field width
                if (spec.Width > 0)
                {
                    int count = input.Position - start;
                    if (spec.Width < count)
                    {
                        input.MoveAhead(spec.Width - count);
                    }
                }

                // Extract token
                if (input.Position > start)
                {
                    if (!spec.NoResult)
                    {
                        Results.Add(input.Extract(start, input.Position));
                        input.MoveAhead();
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 24
0
        public virtual IVariableInformation Create(
            RemoteValue remoteValue, string displayName = null,
            FormatSpecifier formatSpecifier             = null,
            CustomVisualizer customVisualizer           = CustomVisualizer.None)
        {
            IVariableInformation varInfo =
                _varInfoFactory.Create(remoteValue, displayName, formatSpecifier, customVisualizer);

            if (customVisualizer != CustomVisualizer.None)
            {
                varInfo = new NatvisVariableInformation(_natvisExpander, varInfo);
            }

            if (ExpandFormatSpecifierUtil.TryParseExpandFormatSpecifier(varInfo.FormatSpecifier,
                                                                        out int expandedIndex))
            {
                varInfo = _expandVariableFactory.Create(varInfo, expandedIndex);
            }

            return(new CachingVariableInformation(varInfo));
        }
Ejemplo n.º 25
0
            /// <summary>
            /// Parse hexadecimal field
            /// </summary>
            protected bool ParseHexadecimal(TextParser input, FormatSpecifier spec)
            {
                // Skip any whitespace
                input.MovePastWhitespace();

                // Parse 0x prefix
                int start = input.Position;

                if (input.Peek() == '0' && input.Peek(1) == 'x')
                {
                    input.MoveAhead(2);
                }

                // Parse digits
                while (IsValidDigit(input.Peek(), 16))
                {
                    input.MoveAhead();
                }

                // Don't exceed field width
                if (spec.Width > 0)
                {
                    int count = input.Position - start;
                    if (spec.Width < count)
                    {
                        input.MoveAhead(spec.Width - count);
                    }
                }

                // Extract token
                if (input.Position > start)
                {
                    if (!spec.NoResult)
                    {
                        AddUnsigned(input.Extract(start, input.Position), spec.Modifier, 16);
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 26
0
        public IEnumerable <FormatSpecifier> CalculateTimeFormatSpecifiers(string formatString)
        {
            // The user has specified a custom string
            if (string.IsNullOrWhiteSpace(formatString))
            {
                return(null);
            }

            if (!ValidStringFormat(formatString))
            {
                return(null);
            }

            /*
             * Validated a valid DateTime Format from the user.
             * Calculate the positions of each data type.
             */
            IFormatSpecifierFactory factory = new SubControlFormatSpecifierFactory();

            int index = 0;

            string[] specifiers = formatString.Split(FormatStringSeperators.Seperators);
            IList <FormatSpecifier> formatSpecifiers = new List <FormatSpecifier>();

            foreach (string specifier in specifiers)
            {
                string s = specifier;
                if (!(s.Equals("HH") || s.Equals("mm") || s.Equals("ss")))
                {
                    continue;
                }

                FormatSpecifier formatSpecifier = factory.CreateFormatSpecifier(specifier, index);
                formatSpecifiers.Add(formatSpecifier);
                index++;
            }

            return(formatSpecifiers);
        }
Ejemplo n.º 27
0
        private string formatConstructorInfo(FormatSpecifier format, ConstructorInfo ctor)
        {
            string result = null;

            if (format.Style == Style.ILDasm)
            {
                if (format.View == ViewType.Tree)
                {
                    result = ".ctor : void" + formatParameters(format, ctor.GetParameters());
                }
                else
                {
                    result = "instance void " + formatType(format, ctor.DeclaringType, true) +
                             "::.ctor" + formatParameters(format, ctor.GetParameters());
                }
            }
            else
            {
                if (format.View == ViewType.Tree)
                {
                    result = ctor.DeclaringType.Name + formatParameters(format, ctor.GetParameters());
                }
                else
                {
                    if (format.Style == Style.CSharp)
                    {
                        result = formatType(format, ctor.DeclaringType, true) +
                                 formatParameters(format, ctor.GetParameters());
                    }
                    else
                    {
                        result = formatType(format, ctor.DeclaringType, true) + "(.)";
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Parse a character field
        /// </summary>
        static bool ParseCharacter(Action <object> add, TextVisiter input, FormatSpecifier spec)
        {
            // Parse character(s)
            var start = input.Position;
            var count = (spec.Width > 1) ? spec.Width : 1;

            while (!input.EndOfText && count-- > 0)
            {
                input.MoveAhead();
            }

            // Extract token
            if (count <= 0 && input.Position > start)
            {
                if (!spec.NoResult)
                {
                    var token = input.Extract(start, input.Position);
                    add(token.Length > 1 ? token.ToCharArray() : token[0]);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 29
0
        private string formatParameterInfo(FormatSpecifier format, ParameterInfo parm)
        {
            Type   type   = parm.ParameterType;
            string result = null;

            if (type.IsByRef && parm.IsOut)
            {
                if (format.Style == Style.ILDasm)
                {
                    result = "[out] " + formatType(format, type, false);
                }
                else
                {
                    result = "out " + formatType(format, type.GetElementType(), false);
                }
            }
            else
            {
                result = formatType(format, type, false);
            }

            return(result);
        }
Ejemplo n.º 30
0
        private string formatMethodInfo(FormatSpecifier format, MethodInfo method)
        {
            string result = null;

            if (format.Style == Style.ILDasm)
            {
                if (format.View == ViewType.Tree)
                {
                    result = method.Name + " : " + formatType(format, method.ReturnType, false) +
                             ' ' + formatParameters(format, method.GetParameters());
                }
                else
                {
                    result = (method.IsStatic ? "" : "instance ") +
                             formatType(format, method.ReturnType, false) + ' ' +
                             (method.DeclaringType != null ? formatType(format, method.DeclaringType, true) + "::" : "") +
                             method.Name + formatParameters(format, method.GetParameters());
                }
            }
            else
            {
                string declType = format.View == ViewType.Body && method.DeclaringType != null?
                                  formatType(format, method.DeclaringType, true) + '.' : "";

                if (format.Style == Style.CSharp)
                {
                    result = formatType(format, method.ReturnType, false) + ' ' + declType +
                             method.Name + formatParameters(format, method.GetParameters());
                }
                else
                {
                    result = declType + method.Name + "(.)";
                }
            }

            return(result);
        }
Ejemplo n.º 31
0
        private Expression MaybeReadFormatSpecifier()
        {
            Debug.Assert(_buffer.Length == 0);

            Expression formatSpecifier = null;

            if (!EndOfFString && CurrentChar == ':')
            {
                Read(':');
                var position = _position;

                /* Ideally we would just call the FStringParser here. But we are relying on
                 * an already cut of string, so we need to find the end of the format
                 * specifier. */
                BufferFormatSpecifier();

                // If we got to the end, there will be an error when we try to read '}'
                if (!EndOfFString)
                {
                    var options = _options.Clone();
                    options.InitialSourceLocation = new SourceLocation(
                        StartIndex + position,
                        _currentLineNumber,
                        _currentColNumber
                        );
                    var formatStr = _buffer.ToString();
                    _buffer.Clear();
                    var formatSpecifierChildren = new List <Node>();
                    new FStringParser(formatSpecifierChildren, formatStr, _isRaw, options, _langVersion).Parse();
                    formatSpecifier = new FormatSpecifier(formatSpecifierChildren.ToArray(), formatStr);
                    formatSpecifier.SetLoc(new IndexSpan(StartIndex + position, formatStr.Length));
                }
            }

            return(formatSpecifier);
        }
Ejemplo n.º 32
0
        /// <summary>
        /// Parse a string field
        /// </summary>
        private bool ParseString(TextParser input, FormatSpecifier spec)
        {
            // Skip any whitespace
            input.MovePastWhitespace();

            // Parse string characters
            int start = input.Position;
            while (!input.EndOfText && !Char.IsWhiteSpace(input.Peek()))
                input.MoveAhead();

            // Don't exceed field width
            if (spec.Width > 0)
            {
                int count = input.Position - start;
                if (spec.Width < count)
                    input.MoveAhead(spec.Width - count);
            }

            // Extract token
            if (input.Position > start)
            {
                if (!spec.NoResult)
                    Results.Add(input.Extract(start, input.Position));
                return true;
            }
            return false;
        }
Ejemplo n.º 33
0
        /// <summary>
        /// Parse an octal field
        /// </summary>
        private bool ParseOctal(TextParser input, FormatSpecifier spec)
        {
            // Skip any whitespace
            input.MovePastWhitespace();

            // Parse digits
            int start = input.Position;
            while (IsValidDigit(input.Peek(), 8))
                input.MoveAhead();

            // Don't exceed field width
            if (spec.Width > 0)
            {
                int count = input.Position - start;
                if (spec.Width < count)
                    input.MoveAhead(spec.Width - count);
            }

            // Extract token
            if (input.Position > start)
            {
                if (!spec.NoResult)
                    AddUnsigned(input.Extract(start, input.Position), spec.Modifier, 8);
                return true;
            }
            return false;
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Parse a floating-point field
        /// </summary>
        private bool ParseFloat(TextParser input, FormatSpecifier spec)
        {
            // Skip any whitespace
            input.MovePastWhitespace();

            // Parse leading sign
            int start = input.Position;
            if (input.Peek() == '+' || input.Peek() == '-')
                input.MoveAhead();

            // Parse digits
            bool hasPoint = false;
            while (Char.IsDigit(input.Peek()) || input.Peek() == '.')
            {
                if (input.Peek() == '.')
                {
                    if (hasPoint)
                        break;
                    hasPoint = true;
                }
                input.MoveAhead();
            }

            // Parse exponential notation
            if (Char.ToLower(input.Peek()) == 'e')
            {
                input.MoveAhead();
                if (input.Peek() == '+' || input.Peek() == '-')
                    input.MoveAhead();
                while (Char.IsDigit(input.Peek()))
                    input.MoveAhead();
            }

            // Don't exceed field width
            if (spec.Width > 0)
            {
                int count = input.Position - start;
                if (spec.Width < count)
                    input.MoveAhead(spec.Width - count);
            }

            // Because we parse the exponential notation before we apply
            // any field-width constraint, it becomes awkward to verify
            // we have a valid floating point token. To prevent an
            // exception, we use TryParse() here instead of Parse().
            double result;

            // Extract token
            if (input.Position > start
            &&	double.TryParse(input.Extract(start, input.Position), NumberStyles.Float, CultureInfo.InvariantCulture, out result)
                )
            {
                if (!spec.NoResult)
                {
                    if (spec.Modifier == Modifiers.Long ||
                        spec.Modifier == Modifiers.LongLong)
                        Results.Add(result);
                    else
                        Results.Add((float)result);
                }
                return true;
            }
            return false;
        }
Ejemplo n.º 35
0
        /// <summary>
        /// Parse integer field
        /// </summary>
        private bool ParseDecimal(TextParser input, FormatSpecifier spec)
        {
            int radix = 10;

            // Skip any whitespace
            input.MovePastWhitespace();

            // Parse leading sign
            int start = input.Position;
            if (input.Peek() == '+' || input.Peek() == '-')
            {
                input.MoveAhead();
            }
            else if (input.Peek() == '0')
            {
                if (Char.ToLower(input.Peek(1)) == 'x')
                {
                    radix = 16;
                    input.MoveAhead(2);
                }
                else
                {
                    radix = 8;
                    input.MoveAhead();
                }
            }

            // Parse digits
            while (IsValidDigit(input.Peek(), radix))
                input.MoveAhead();

            // Don't exceed field width
            if (spec.Width > 0)
            {
                int count = input.Position - start;
                if (spec.Width < count)
                    input.MoveAhead(spec.Width - count);
            }

            // Extract token
            if (input.Position > start)
            {
                if (!spec.NoResult)
                {
                    if (spec.Type == Types.Decimal)
                        AddSigned(input.Extract(start, input.Position), spec.Modifier, radix);
                    else
                        AddUnsigned(input.Extract(start, input.Position), spec.Modifier, radix);
                }
                return true;
            }
            return false;
        }
Ejemplo n.º 36
0
        /// <summary>
        /// Parse a character field
        /// </summary>
        private bool ParseCharacter(TextParser input, FormatSpecifier spec)
        {
            // Parse character(s)
            int start = input.Position;
            int count = (spec.Width > 1) ? spec.Width : 1;
            while (!input.EndOfText && count-- > 0)
                input.MoveAhead();

            // Extract token
            if (count <= 0 && input.Position > start)
            {
                if (!spec.NoResult)
                {
                    string token = input.Extract(start, input.Position);
                    if (token.Length > 1)
                        Results.Add(token.ToCharArray());
                    else
                        Results.Add(token[0]);
                }
                return true;
            }
            return false;
        }
Ejemplo n.º 37
0
 private string formatModule(FormatSpecifier format, Module module)
 {
     return format.Style == Style.ILDasm ? module.FullyQualifiedName : module.Name;
 }
Ejemplo n.º 38
0
            /// <summary>
            /// Attempts to parse a field format specifier from the format string.
            /// </summary>
            protected bool ParseFormatSpecifier(TextParser format, FormatSpecifier spec)
            {
                // Return if not a field format specifier
                if (format.Peek() != '%')
                {
                    return(false);
                }
                format.MoveAhead();

                // Return if "%%" (treat as '%' literal)
                if (format.Peek() == '%')
                {
                    return(false);
                }

                // Test for asterisk, which indicates result is not stored
                if (format.Peek() == '*')
                {
                    spec.NoResult = true;
                    format.MoveAhead();
                }
                else
                {
                    spec.NoResult = false;
                }

                // Parse width
                int start = format.Position;

                while (Char.IsDigit(format.Peek()))
                {
                    format.MoveAhead();
                }
                if (format.Position > start)
                {
                    spec.Width = int.Parse(format.Extract(start, format.Position));
                }
                else
                {
                    spec.Width = 0;
                }

                // Parse modifier
                if (format.Peek() == 'h')
                {
                    format.MoveAhead();
                    if (format.Peek() == 'h')
                    {
                        format.MoveAhead();
                        spec.Modifier = Modifiers.ShortShort;
                    }
                    else
                    {
                        spec.Modifier = Modifiers.Short;
                    }
                }
                else if (Char.ToLower(format.Peek()) == 'l')
                {
                    format.MoveAhead();
                    if (format.Peek() == 'l')
                    {
                        format.MoveAhead();
                        spec.Modifier = Modifiers.LongLong;
                    }
                    else
                    {
                        spec.Modifier = Modifiers.Long;
                    }
                }
                else
                {
                    spec.Modifier = Modifiers.None;
                }

                // Parse type
                switch (format.Peek())
                {
                case 'c':
                    spec.Type = Types.Character;
                    break;

                case 'd':
                case 'i':
                    spec.Type = Types.Decimal;
                    break;

                case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'f':
                case 'F':
                case 'g':
                case 'G':
                    spec.Type = Types.Float;
                    break;

                case 'o':
                    spec.Type = Types.Octal;
                    break;

                case 's':
                    spec.Type = Types.String;
                    break;

                case 'u':
                    spec.Type = Types.Unsigned;
                    break;

                case 'x':
                case 'X':
                    spec.Type = Types.Hexadecimal;
                    break;

                case '[':
                    spec.Type = Types.ScanSet;
                    format.MoveAhead();
                    // Parse scan set characters
                    if (format.Peek() == '^')
                    {
                        spec.ScanSetExclude = true;
                        format.MoveAhead();
                    }
                    else
                    {
                        spec.ScanSetExclude = false;
                    }
                    start = format.Position;
                    // Treat immediate ']' as literal
                    if (format.Peek() == ']')
                    {
                        format.MoveAhead();
                    }
                    format.MoveTo(']');
                    if (format.EndOfText)
                    {
                        throw new Exception("Type specifier expected character : ']'");
                    }
                    spec.ScanSet = format.Extract(start, format.Position);
                    break;

                default:
                    string msg = String.Format("Unknown format type specified : '{0}'", format.Peek());
                    throw new Exception(msg);
                }
                format.MoveAhead();
                return(true);
            }
Ejemplo n.º 39
0
        /// <summary>
        /// Parses the input string according to the rules in the
        /// format string. Similar to the standard C library's
        /// sscanf() function. Parsed fields are placed in the
        /// class' Results member.
        /// </summary>
        /// <param name="input">String to parse</param>
        /// <param name="format">Specifies rules for parsing input</param>
        public int Parse(string input, string format)
        {
            TextParser inp = new TextParser(input);
            TextParser fmt = new TextParser(format);
            List<object> results = new List<object>();
            FormatSpecifier spec = new FormatSpecifier();
            int count = 0;

            // Clear any previous results
            Results.Clear();

            // Process input string as indicated in format string
            while (!fmt.EndOfText && !inp.EndOfText)
            {
                if (ParseFormatSpecifier(fmt, spec))
                {
                    // Found a format specifier
                    TypeParser parser = null;
                    foreach(TypeParser tp in _typeParsers)
                        if (tp.Type == spec.Type)
                        {
                            parser = tp;
                            break;
                        }
                    if (parser == null)
                        break;

                    if (parser.Parser(inp, spec))
                        count++;
                    else
                        break;
                }
                else if (Char.IsWhiteSpace(fmt.Peek()))
                {
                    // Whitespace
                    inp.MovePastWhitespace();
                    fmt.MoveAhead();
                }
                else if (fmt.Peek() == inp.Peek())
                {
                    // Matching character
                    inp.MoveAhead();
                    fmt.MoveAhead();
                }
                else break;    // Break at mismatch
            }

            // Return number of fields successfully parsed
            return count;
        }
Ejemplo n.º 40
0
        private string formatFieldInfo(FormatSpecifier format, FieldInfo field)
        {
            string result = null;

            if (format.Style == Style.ILDasm)
            {
                if (format.View == ViewType.Tree)
                {
                    result = field.Name + " : ";

                    if (field.IsPublic)
                        result += "public ";
                    else if (field.IsPrivate)
                        result += "private ";
                    else if (field.IsAssembly)
                        result += "assembly ";
                    else if (field.IsFamily)
                        result += "family ";
                    else if (field.IsFamilyAndAssembly)
                        result += "famandassem ";
                    else if (field.IsFamilyOrAssembly)
                        result += "famorassem ";

                    result += formatType(format,field.FieldType,false);
                }
                else
                    result = formatType(format,field.FieldType,false) + ' ' +
                        (field.DeclaringType != null ? formatType(format,field.DeclaringType,true) + "::" : "") +
                        field.Name;
            }
            else
            {
                string declType = format.View == ViewType.Body && field.DeclaringType != null ?
                    formatType(format,field.DeclaringType,true) + '.' : "";

                if (format.Style == Style.CSharp)
                    result = formatType(format,field.FieldType,false) + ' ' + declType + field.Name;
                else
                    result = declType + field.Name;
            }

            return result;
        }
Ejemplo n.º 41
0
        private string formatParameters(FormatSpecifier format, ParameterInfo[] parms)
        {
            string result = "(";

            for (int i = 0; i < parms.Length; i++)
                result += formatParameterInfo(format,parms[i]) + (i < parms.Length-1 ? ", " : "");

            return result + ')';
        }
Ejemplo n.º 42
0
        private string formatMethodInfo(FormatSpecifier format, MethodInfo method)
        {
            string result = null;

            if (format.Style == Style.ILDasm)
            {
                if (format.View == ViewType.Tree)
                    result = method.Name + " : " + formatType(format,method.ReturnType,false) +
                        ' ' + formatParameters(format,method.GetParameters());
                else
                    result = (method.IsStatic ? "" : "instance ") +
                        formatType(format,method.ReturnType,false) + ' ' +
                        (method.DeclaringType != null ? formatType(format,method.DeclaringType,true) + "::" : "") +
                        method.Name + formatParameters(format,method.GetParameters());
            }
            else
            {
                string declType = format.View == ViewType.Body && method.DeclaringType != null ?
                    formatType(format,method.DeclaringType,true) + '.' : "";

                if (format.Style == Style.CSharp)
                    result = formatType(format,method.ReturnType,false) + ' ' + declType +
                        method.Name + formatParameters(format,method.GetParameters());
                else
                    result = declType + method.Name + "(.)";
            }

            return result;
        }
Ejemplo n.º 43
0
        public override string Format(int indentLevel, FormatSpecifier formatSpecifier)
        {
            formatSpecifier = formatSpecifier.Recalc(Tag);

            return Child.Format(indentLevel + 1, formatSpecifier);
        }
Ejemplo n.º 44
0
 public abstract string Format(int indentLevel, FormatSpecifier formatSpecifier);
Ejemplo n.º 45
0
        private string formatType(FormatSpecifier format, Type type, bool NameRes)
        {
            string result = null;

            if (type.HasElementType)
            {
                String elemType = formatType(format,type.GetElementType(),NameRes);

                if (type.IsArray)
                {
                    int rank = type.GetArrayRank();

                    if (rank == 1)
                        result = elemType + "[]";
                    else
                    {
                        result = elemType + '[';

                        for (int i = 0; i < rank; i++)
                        {
                            if (format.Style == Style.ILDasm)
                                result += "0...";

                            if (i < rank-1)
                                result += ',';
                        }

                        result += ']';
                    }
                }
                else if (type.IsByRef)
                {
                    if (format.Style == Style.ILDasm)
                        result = elemType + '&';
                    else
                        result = "ref " + elemType;
                }
            }
            else
            {
                if (format.View == ViewType.Tree)
                    result = format.Style == Style.ILDasm ? type.FullName : type.Name;
                else
                {
                    if (format.Style == Style.ILDasm)
                    {
                        if (type == typeof(void) && ! NameRes)
                            result = "void";
                        else if (type.IsPrimitive && ! NameRes)
                        {
                            if (type == typeof(bool))
                                result = "bool";
                            else if (type == typeof(char))
                                result = "char";
                            else if (type == typeof(float))
                                result = "float32";
                            else if (type == typeof(double))
                                result = "float64";
                            else if (type == typeof(sbyte))
                                result = "int8";
                            else if (type == typeof(short))
                                result = "int16";
                            else if (type == typeof(int))
                                result = "int32";
                            else if (type == typeof(long))
                                result = "int64";
                            else if (type == typeof(byte))
                                result = "unsigned int8";
                            else if (type == typeof(ushort))
                                result = "unsigned int16";
                            else if (type == typeof(uint))
                                result = "unsigned int32";
                            else if (type == typeof(ulong))
                                result = "unsigned int64";
                            else if (type == typeof(System.IntPtr))
                                result = "native int";
                            else if (type == typeof(System.UIntPtr))
                                result = "native unsigned int";
                        }
                        else if (type.IsValueType || type.IsEnum)
                        {
                            if (type == typeof(System.IntPtr) && ! NameRes)
                                result = "native int";
                            else if (type == typeof(System.UIntPtr) && ! NameRes)
                                result = "native unsigned int";
                            else if (type == typeof(System.TypedReference) && ! NameRes)
                                result = "typedref";
                            else
                                result = (NameRes ? "" : "valuetype ") +
                                    formatTypeName(format,type);
                        }
                        else if (type.IsClass || type.IsInterface)
                        {
                            if (type == typeof(System.Object) && ! NameRes)
                                result = "object";
                            else if (type == typeof(System.String) && ! NameRes)
                                result = "string";
                            else
                                result = (NameRes ? "" : "class ") +
                                    formatTypeName(format,type);
                        }
                    }
                    else
                    {
                        if (type == typeof(void) && ! NameRes)
                            result = "void";
                        else if (type.IsPrimitive && ! NameRes)
                        {
                            if (type == typeof(bool))
                                result = "bool";
                            else if (type == typeof(char))
                                result = "char";
                            else if (type == typeof(float))
                                result = "float";
                            else if (type == typeof(double))
                                result = "double";
                            else if (type == typeof(sbyte))
                                result = "sbyte";
                            else if (type == typeof(short))
                                result = "short";
                            else if (type == typeof(int))
                                result = "int";
                            else if (type == typeof(long))
                                result = "long";
                            else if (type == typeof(byte))
                                result = "byte";
                            else if (type == typeof(ushort))
                                result = "ushort";
                            else if (type == typeof(uint))
                                result = "uint";
                            else if (type == typeof(ulong))
                                result = "ulong";
                            else if (type == typeof(System.IntPtr))
                                result = "IntPtr";
                            else if (type == typeof(System.UIntPtr))
                                result = "IntPtr";
                        }
                        else if (type.IsValueType || type.IsEnum)
                        {
                            if (type == typeof(System.Decimal) && ! NameRes)
                                result = "decimal";
                            else
                                result = formatTypeName(format,type);
                        }
                        else if (type.IsClass || type.IsInterface)
                        {
                            if (type == typeof(System.Object) && ! NameRes)
                                result = "object";
                            else if (type == typeof(System.String) && ! NameRes)
                                result = "string";
                            else
                                result = formatTypeName(format,type);
                        }
                    }
                }
            }

            return result;
        }
Ejemplo n.º 46
0
 private IMember GetValueFromFormatSpecifier(FormatSpecifier formatSpecifier)
     => new PythonFString(formatSpecifier.Unparsed, Interpreter);
Ejemplo n.º 47
0
 private string formatTypeName(FormatSpecifier format, Type type)
 {
     return format.QualifiedNames ? type.FullName : type.Name;
 }
Ejemplo n.º 48
0
        private string formatConstructorInfo(FormatSpecifier format, ConstructorInfo ctor)
        {
            string result = null;

            if (format.Style == Style.ILDasm)
            {
                if (format.View == ViewType.Tree)
                    result = ".ctor : void" + formatParameters(format,ctor.GetParameters());
                else
                    result = "instance void " + formatType(format,ctor.DeclaringType,true) +
                        "::.ctor" + formatParameters(format,ctor.GetParameters());
            }
            else
            {
                if (format.View == ViewType.Tree)
                    result = ctor.DeclaringType.Name + formatParameters(format,ctor.GetParameters());
                else
                {
                    if (format.Style == Style.CSharp)
                        result = formatType(format,ctor.DeclaringType,true) +
                            formatParameters(format,ctor.GetParameters());
                    else
                        result = formatType(format,ctor.DeclaringType,true) + "(.)";
                }
            }

            return result;
        }
Ejemplo n.º 49
0
        private string formatParameterInfo(FormatSpecifier format, ParameterInfo parm)
        {
            Type type = parm.ParameterType;
            string result = null;

            if (type.IsByRef && parm.IsOut)
            {
                if (format.Style == Style.ILDasm)
                    result = "[out] " + formatType(format,type,false);
                else
                    result = "out " + formatType(format,type.GetElementType(),false);
            }
            else
                result = formatType(format,type,false);

             return result;
        }
Ejemplo n.º 50
0
            /// <summary>
            /// Parse a floating-point field
            /// </summary>
            private bool ParseFloat(TextParser input, FormatSpecifier spec)
            {
                // Skip any whitespace
                input.MovePastWhitespace();

                // Parse leading sign
                int start = input.Position;

                if (input.Peek() == '+' || input.Peek() == '-')
                {
                    input.MoveAhead();
                }

                // Parse digits
                bool hasPoint = false;

                while (Char.IsDigit(input.Peek()) || input.Peek() == '.')
                {
                    if (input.Peek() == '.')
                    {
                        if (hasPoint)
                        {
                            break;
                        }
                        hasPoint = true;
                    }
                    input.MoveAhead();
                }

                // Parse exponential notation
                if (Char.ToLower(input.Peek()) == 'e')
                {
                    input.MoveAhead();
                    if (input.Peek() == '+' || input.Peek() == '-')
                    {
                        input.MoveAhead();
                    }
                    while (Char.IsDigit(input.Peek()))
                    {
                        input.MoveAhead();
                    }
                }

                // Don't exceed field width
                if (spec.Width > 0)
                {
                    int count = input.Position - start;
                    if (spec.Width < count)
                    {
                        input.MoveAhead(spec.Width - count);
                    }
                }

                // Because we parse the exponential notation before we apply
                // any field-width constraint, it becomes awkward to verify
                // we have a valid floating point token. To prevent an
                // exception, we use TryParse() here instead of Parse().
                double result;

                // Extract token
                if (input.Position > start &&
                    double.TryParse(input.Extract(start, input.Position), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out result))
                {
                    if (!spec.NoResult)
                    {
                        if (spec.Modifier == Modifiers.Long ||
                            spec.Modifier == Modifiers.LongLong)
                        {
                            Results.Add(result);
                        }
                        else
                        {
                            Results.Add((float)result);
                        }
                    }
                    return(true);
                }
                return(false);
            }
Ejemplo n.º 51
0
        /// <summary>
        /// Attempts to parse a field format specifier from the format string.
        /// </summary>
        protected bool ParseFormatSpecifier(TextParser format, FormatSpecifier spec)
        {
            // Return if not a field format specifier
            if (format.Peek() != '%')
                return false;
            format.MoveAhead();

            // Return if "%%" (treat as '%' literal)
            if (format.Peek() == '%')
                return false;

            // Test for asterisk, which indicates result is not stored
            if (format.Peek() == '*')
            {
                spec.NoResult = true;
                format.MoveAhead();
            }
            else spec.NoResult = false;

            // Parse width
            int start = format.Position;
            while (Char.IsDigit(format.Peek()))
                format.MoveAhead();
            if (format.Position > start)
                spec.Width = int.Parse(format.Extract(start, format.Position));
            else
                spec.Width = 0;

            // Parse modifier
            if (format.Peek() == 'h')
            {
                format.MoveAhead();
                if (format.Peek() == 'h')
                {
                    format.MoveAhead();
                    spec.Modifier = Modifiers.ShortShort;
                }
                else spec.Modifier = Modifiers.Short;
            }
            else if (Char.ToLower(format.Peek()) == 'l')
            {
                format.MoveAhead();
                if (format.Peek() == 'l')
                {
                    format.MoveAhead();
                    spec.Modifier = Modifiers.LongLong;
                }
                else spec.Modifier = Modifiers.Long;
            }
            else spec.Modifier = Modifiers.None;

            // Parse type
            switch (format.Peek())
            {
                case 'c':
                    spec.Type = Types.Character;
                    break;
                case 'd':
                case 'i':
                    spec.Type = Types.Decimal;
                    break;
                case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'f':
                case 'F':
                case 'g':
                case 'G':
                    spec.Type = Types.Float;
                    break;
                case 'o':
                    spec.Type = Types.Octal;
                    break;
                case 's':
                    spec.Type = Types.String;
                    break;
                case 'u':
                    spec.Type = Types.Unsigned;
                    break;
                case 'x':
                case 'X':
                    spec.Type = Types.Hexadecimal;
                    break;
                case '[':
                    spec.Type = Types.ScanSet;
                    format.MoveAhead();
                    // Parse scan set characters
                    if (format.Peek() == '^')
                    {
                        spec.ScanSetExclude = true;
                        format.MoveAhead();
                    }
                    else spec.ScanSetExclude = false;
                    start = format.Position;
                    // Treat immediate ']' as literal
                    if (format.Peek() == ']')
                        format.MoveAhead();
                    format.MoveTo(']');
                    if (format.EndOfText)
                        throw new Exception("Type specifier expected character : ']'");
                    spec.ScanSet = format.Extract(start, format.Position);
                    break;
                default:
                    string msg = String.Format("Unknown format type specified : '{0}'", format.Peek());
                    throw new Exception(msg);
            }
            format.MoveAhead();
            return true;
        }
Ejemplo n.º 52
0
 public override string Format(int indentLevel, FormatSpecifier formatSpecifier)
 {
     return Value;
 }
Ejemplo n.º 53
0
        /// <summary>
        /// Parse hexadecimal field
        /// </summary>
        protected bool ParseHexadecimal(TextParser input, FormatSpecifier spec)
        {
            // Skip any whitespace
            input.MovePastWhitespace();

            // Parse 0x prefix
            int start = input.Position;
            if (input.Peek() == '0' && input.Peek(1) == 'x')
                input.MoveAhead(2);

            // Parse digits
            while (IsValidDigit(input.Peek(), 16))
                input.MoveAhead();

            // Don't exceed field width
            if (spec.Width > 0)
            {
                int count = input.Position - start;
                if (spec.Width < count)
                    input.MoveAhead(spec.Width - count);
            }

            // Extract token
            if (input.Position > start)
            {
                if (!spec.NoResult)
                    AddUnsigned(input.Extract(start, input.Position), spec.Modifier, 16);
                return true;
            }
            return false;
        }
Ejemplo n.º 54
0
        /// <summary>
        /// Parse a scan-set field
        /// </summary>
        protected bool ParseScanSet(TextParser input, FormatSpecifier spec)
        {
            // Parse characters
            int start = input.Position;
            if (!spec.ScanSetExclude)
            {
                while (spec.ScanSet.Contains(input.Peek().ToString()))
                    input.MoveAhead();
            }
            else
            {
                while (!input.EndOfText && !spec.ScanSet.Contains(input.Peek().ToString()))
                    input.MoveAhead();
            }

            // Don't exceed field width
            if (spec.Width > 0)
            {
                int count = input.Position - start;
                if (spec.Width < count)
                    input.MoveAhead(spec.Width - count);
            }

            // Extract token
            if (input.Position > start)
            {
                if (!spec.NoResult)
                    Results.Add(input.Extract(start, input.Position));
                return true;
            }
            return false;
        }
Ejemplo n.º 55
0
 private string formatAssembly(FormatSpecifier format, Assembly assembly)
 {
     return format.Style == Style.ILDasm ? assembly.Location : assembly.FullName;
 }