public override void Get(IInputBuffer buffer)
        {
            char ch;           // current character
            StringBuilder tokenBuilder = new StringBuilder();

            tokenBuilder.Append('\'');  // opening quote

            //--Get the string.
            ch = buffer.GetChar();  // first char after opening quote
            while (ch != 0x7f)
            {
                if (ch == '\'')
                {     // look for another quote

                    //--Fetched a quote.  Now check for an adjacent quote,
                    //--since two consecutive quotes represent a single
                    //--quote in the string.
                    ch = buffer.GetChar();
                    if (ch != '\'') break;  // not another quote, so previous
                    //   quote ended the string
                }
                //--Replace the end of line character with a blank.
                else if (ch == '\0') ch = ' ';

                //--Append current char to string, then get the next char.
                tokenBuilder.Append(ch);
                ch = buffer.GetChar();
            }

            tokenBuilder.Append('\'');  // closing quote
            tokenString = tokenBuilder.ToString();
            code = TTokenCode.String;
        }
Beispiel #2
0
        public override void Get(IInputBuffer buffer)
        {
            StringBuilder tokenBuilder = new StringBuilder();
            tokenBuilder.Append(buffer.CurrentChar);
            tokenString = tokenBuilder.ToString();
            buffer.GetChar();

            code = TTokenCode.Error;
        }
Beispiel #3
0
        public override void Get(IInputBuffer buffer)
        {
            StringBuilder tokenBuilder = new StringBuilder();
            char ch = buffer.CurrentChar;  // char fetched from input
            do
            {
                tokenBuilder.Append(ch);
                ch = buffer.GetChar();
            } while (characterMap[ch] == CharCode.Letter ||
                        characterMap[ch] == CharCode.Digit);

            tokenString = tokenBuilder.ToString();
            checkForReservedWord();
        }
        public override void Get(IInputBuffer buffer)
        {
            StringBuilder tokenBuilder = new StringBuilder();

            char ch;

            float numValue = 0;           // value of number ignoring
                                            //    the decimal point
            int wholePlaces = 0;            // no. digits before the decimal point
            int decimalPlaces = 0;          // no. digits after  the decimal point
            char exponentSign = '+';
            float eValue = 0;             // value of number after 'E'
            int exponent = 0;               // final value of exponent
            bool sawDotDotFlag = false;     // true if encountered '..',
                                            //   else false

            ch = buffer.CurrentChar;
            digitCount = 0;
            countError = false;

            code = TTokenCode.Error;    // we don't know what it is yet, but
            type = TDataType.tyInteger;  //    assume it'll be an integer

            //--Get the whole part of the number by accumulating
            //--the values of its digits into numValue.  wholePlaces keeps
            //--track of the number of digits in this part.
            if (accumulateValue(buffer, tokenBuilder, ref numValue))
            {
                wholePlaces = digitCount;

                ch = buffer.CurrentChar;

                ////--If the current character is a dot, then either we have a
                ////--fraction part or we are seeing the first character of a '..'
                ////--token.  To find out, we must fetch the next character.
                if (ch == '.')
                {
                    ch = buffer.GetChar();

                    if (ch == '.')
                    {

                        //--We have a .. token.  Back up bufferp so that the
                        //--token can be extracted next.
                        sawDotDotFlag = true;
                        buffer.PutBackChar();
                    }
                    else
                    {
                        type = TDataType.tyReal;
                        tokenBuilder.Append(".");

                        //--We have a fraction part.  Accumulate it into numValue.
                        if (!accumulateValue(buffer, tokenBuilder, ref numValue)) return;

                        // get the current character
                        ch = buffer.CurrentChar;

                        decimalPlaces = digitCount - wholePlaces;
                    }
                }

                //--Get the exponent part, if any. There cannot be an
                //--exponent part if we already saw the '..' token.
                if (!sawDotDotFlag && ((ch == 'E') || (ch == 'e')))
                {
                    type = TDataType.tyReal;
                    tokenBuilder.Append(ch);
                    ch = buffer.GetChar();

                    //--Fetch the exponent's sign, if any.
                    if ((ch == '+') || (ch == '-'))
                    {
                        exponentSign = ch;
                        tokenBuilder.Append(ch);
                        ch = buffer.GetChar();
                    }

                    //--Accumulate the value of the number after 'E' into eValue.
                    digitCount = 0;
                    if (!accumulateValue(buffer, tokenBuilder, ref eValue)) return;

                    ch = buffer.CurrentChar;
                    if (exponentSign == '-') eValue = -eValue;
                }

                //--Were there too many digits?
                if (countError)
                {
                    return;
                }

                //--Calculate and check the final exponent value,
                //--and then use it to adjust the number's value.
                exponent = (int)eValue - decimalPlaces;
                if ((exponent + wholePlaces < -maxExponent) ||
                    (exponent + wholePlaces >  maxExponent))
                {
                    return;
                }

                if (exponent != 0)
                {
                    numValue *= (float)Math.Pow(10.0, exponent);
                }

                //--Check and set the numeric value.
                if (type == TDataType.tyInteger)
                {
                    if ((numValue < -maxInteger) || (numValue > maxInteger))
                    {
                        return;
                    }

                    value.integerValue = (int)numValue;
                }
                else
                {
                    value.realValue = numValue;
                }

                tokenString = tokenBuilder.ToString();
                code = TTokenCode.Number;
            }
        }
        private bool accumulateValue(IInputBuffer buffer, StringBuilder tokenBuilder, ref float value)
        {
            char ch = buffer.CurrentChar;
            Dictionary<int, CharCode> charCodeMap = CharCodeMapManager.Instance.GetCharacterMap();

            //--Error if the first character is not a digit.
            if (charCodeMap[ch] != CharCode.Digit)
            {
                return false;           // failure
            }

            //--Accumulate the value as long as the total allowable
            //--number of digits has not been exceeded.
            do
            {
                tokenBuilder.Append(ch);

                if (++digitCount <= maxDigitCount)
                {
                    value = 10 * value + (ch - '0');  // shift left and add
                }
                else countError = true;         // too many digits, but keep reading anyway

                ch = buffer.GetChar();
            } while (charCodeMap[ch] == CharCode.Digit);

            return true;               // success
        }
        public override void Get(IInputBuffer buffer)
        {
            char ch = buffer.CurrentChar;
            StringBuilder tokenBuilder = new StringBuilder();

            tokenBuilder.Append(ch);

            switch (ch)
            {
                case '^': code = TTokenCode.UpArrow; buffer.GetChar(); break;
                case '*': code = TTokenCode.Star; buffer.GetChar(); break;
                case '(': code = TTokenCode.LParen; buffer.GetChar(); break;
                case ')': code = TTokenCode.RParen; buffer.GetChar(); break;
                case '-': code = TTokenCode.Minus; buffer.GetChar(); break;
                case '+': code = TTokenCode.Plus; buffer.GetChar(); break;
                case '=': code = TTokenCode.Equal; buffer.GetChar(); break;
                case '[': code = TTokenCode.LBracket; buffer.GetChar(); break;
                case ']': code = TTokenCode.RBracket; buffer.GetChar(); break;
                case ';': code = TTokenCode.Semicolon; buffer.GetChar(); break;
                case ',': code = TTokenCode.Comma; buffer.GetChar(); break;
                case '/': code = TTokenCode.Slash; buffer.GetChar(); break;
                case ':':   // : or :=
                    ch = buffer.GetChar();
                    if (ch == '=')
                    {
                        tokenBuilder.Append(ch);
                        code = TTokenCode.ColonEqual;
                        buffer.GetChar();
                    }
                    else
                    {
                        code = TTokenCode.Colon;
                    }
                    break;

                case '<':

                    ch = buffer.GetChar();     // < or <= or <>
                    if (ch == '=')
                    {
                        tokenBuilder.Append('=');
                        code = TTokenCode.Le;
                        buffer.GetChar();
                    }
                    else if (ch == '>')
                    {
                        tokenBuilder.Append(ch);
                        code = TTokenCode.Ne;
                        buffer.GetChar();
                    }
                    else
                    {
                        code = TTokenCode.Lt;
                    }
                    break;

                case '>':
                    ch = buffer.GetChar();     // > or >=
                    if (ch == '=')
                    {
                        tokenBuilder.Append('=');
                        code = TTokenCode.Ge;
                        buffer.GetChar();
                    }
                    else
                    {
                        code = TTokenCode.Gt;
                    }
                    break;

                case '.':
                    ch = buffer.GetChar(); // . or ..
                    if (ch == '.')
                    {
                        tokenBuilder.Append(ch);
                        code = TTokenCode.DotDot;
                        buffer.GetChar();
                    }
                    else
                    {
                        code = TTokenCode.Period;
                    }

                    break;

                default:
                    code = TTokenCode.Error;                  // error
                    buffer.GetChar();
                    break;
            }

            tokenString = tokenBuilder.ToString();
        }