Exemplo n.º 1
0
        /**
         * Reads a number or returns null.
         */
        private Token TryReadNumber()
        {
            buffer.Mark();

            StringBuilder sb       = new StringBuilder();
            StringBuilder radix    = null;
            char          c        = GetChar();
            bool          hasDigit = false;

            // optional + or -
            if (c == '+' || c == '-')
            {
                sb.Append(c);
                c = GetChar();
            }

            // optional digits
            while (char.IsDigit(c))
            {
                sb.Append(c);
                c        = GetChar();
                hasDigit = true;
            }

            // optional .
            if (c == '.')
            {
                sb.Append(c);
                c = GetChar();
            }
            else if (c == '#')
            {
                // PostScript radix number takes the form base#number
                radix = sb;
                sb    = new StringBuilder();
                c     = GetChar();
            }
            else if (sb.Length == 0 || !hasDigit)
            {
                // failure
                buffer.Reset();
                return(null);
            }
            else
            {
                // integer
                buffer.Seek(buffer.Position - 1);
                return(new Token(sb.ToString(), TokenKind.INTEGER));
            }

            // required digit
            if (char.IsDigit(c))
            {
                sb.Append(c);
                c = GetChar();
            }
            else
            {
                // failure
                buffer.Reset();
                return(null);
            }

            // optional digits
            while (char.IsDigit(c))
            {
                sb.Append(c);
                c = GetChar();
            }

            // optional E
            if (c == 'E')
            {
                sb.Append(c);
                c = GetChar();

                // optional minus
                if (c == '-')
                {
                    sb.Append(c);
                    c = GetChar();
                }

                // required digit
                if (char.IsDigit(c))
                {
                    sb.Append(c);
                    c = GetChar();
                }
                else
                {
                    // failure
                    buffer.Reset();
                    return(null);
                }

                // optional digits
                while (char.IsDigit(c))
                {
                    sb.Append(c);
                    c = GetChar();
                }
            }

            buffer.Seek(buffer.Position - 1);
            if (radix != null)
            {
                int val = Convert.ToInt32(sb.ToString(), int.Parse(radix.ToString()));
                return(new Token(val.ToString(), TokenKind.INTEGER));
            }
            return(new Token(sb.ToString(), TokenKind.REAL));
        }