Example #1
0
        public bool ExpectNumericToken(NumericTokenSubType subType, out NumericToken token)
        {
            token = null;
            Token t;

            if (!this.ReadToken(out t))
            {
                this.ScriptError("couldn't read expected token");

                return(false);
            }

            if (t.Type != TokenType.Number)
            {
                this.ScriptError("expected a number, found {0}", t.Value);

                return(false);
            }

            token = t as NumericToken;

            if (!token.SubType.Has(subType))
            {
                string str = String.Empty;

                if (subType.Has(NumericTokenSubType.Decimal))
                {
                    str = "decimal";
                }
                if (subType.Has(NumericTokenSubType.Hex))
                {
                    str = "hex";
                }
                if (subType.Has(NumericTokenSubType.Octal))
                {
                    str = "octal";
                }
                if (subType.Has(NumericTokenSubType.Binary))
                {
                    str = "binary";
                }

                if (subType.Has(NumericTokenSubType.Long))
                {
                    str += " long";
                }
                if (subType.Has(NumericTokenSubType.Unsigned))
                {
                    str += " unsigned";
                }
                if (subType.Has(NumericTokenSubType.Float))
                {
                    str += " float";
                }
                if (subType.Has(NumericTokenSubType.Integer))
                {
                    str += " integer";
                }

                this.ScriptError("expected {0}, found {1}", str, token.Value);

                return(false);
            }

            return(true);
        }
Example #2
0
        private bool ReadNumber(out NumericToken token)
        {
            token = new NumericToken();

            var builder = new StringBuilder();
            int c       = this.reader.PeekChar();
            NumericTokenSubType subType = NumericTokenSubType.Undefined;

            if (c == '0' && this.reader.PeekChar(1) == 'x' || this.reader.PeekChar(1) == 'X')
            {
                // hexadecimal
                builder.Append(this.reader.ReadChars(2));
                c = this.reader.PeekChar();

                while (IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'A'))
                {
                    builder.Append(this.reader.ReadChar());

                    if (builder.Length >= ScriptTokenizer.MaxTokenLength)
                    {
                        this.ScriptError("hexadecimal number longer than MAX_TOKEN = {0}", ScriptTokenizer.MaxTokenLength);

                        return(false);
                    }

                    c = this.reader.PeekChar();
                }

                subType = subType.Add(NumericTokenSubType.Hex);
            }
            else if (c == '0' && this.reader.PeekChar(1) == 'b' || this.reader.PeekChar(1) == 'B')
            {
                // binary number
                builder.Append(this.reader.ReadChars(2));
                c = this.reader.PeekChar();

                while (c == '0' || c == '1')
                {
                    builder.Append(this.reader.ReadChar());

                    if (builder.Length >= ScriptTokenizer.MaxTokenLength)
                    {
                        this.ScriptError("binary number longer than MAX_TOKEN = {0}", ScriptTokenizer.MaxTokenLength);

                        return(false);
                    }

                    c = this.reader.PeekChar();
                }

                subType = subType.Add(NumericTokenSubType.Binary);
            }
            else
            {
                // decimal or octal integer or floating point number
                bool octal = false;
                bool dot   = false;

                if (this.reader.PeekChar() == '0')
                {
                    octal = true;
                }

                while (true)
                {
                    c = this.reader.PeekChar();

                    if (c == '.')
                    {
                        dot = true;
                    }
                    else if (c == '8' || c == '9')
                    {
                        octal = false;
                    }
                    else if (c < '0' || c > '9')
                    {
                        break;
                    }

                    builder.Append(this.reader.ReadChar());

                    if (builder.Length >= ScriptTokenizer.MaxTokenLength - 1)
                    {
                        this.ScriptError("number longer than MAX_TOKEN = {0}", ScriptTokenizer.MaxTokenLength);

                        return(false);
                    }
                }

                subType = subType.Add(octal ? NumericTokenSubType.Octal : NumericTokenSubType.Decimal);
                subType = subType.Add(dot ? NumericTokenSubType.Float : NumericTokenSubType.Integer);
            }

            for (int i = 0; i < 2; i++)
            {
                c = this.reader.PeekChar();

                if ((c == 'l' || c == 'L') && !subType.Has(NumericTokenSubType.Long))
                {
                    // check for a LONG number
                    // bk001204 - brackets
                    this.reader.ReadChar();
                    subType = subType.Add(NumericTokenSubType.Long);
                }
                else if ((c == 'u' || c == 'U') && !subType.Has(NumericTokenSubType.Unsigned | NumericTokenSubType.Float))
                {
                    // check for an UNSIGNED number
                    // bk001204 - brackets
                    this.reader.ReadChar();
                    subType = subType.Add(NumericTokenSubType.Unsigned);
                }
            }

            token.Value        = builder.ToString();
            token.IntegerValue = UInt64.Parse(token.Value);
            token.FloatValue   = Single.Parse(token.Value);
            token.SubType      = subType;

            return(true);
        }