Beispiel #1
0
        public RqlToken Next()
        {
            if (stopToken != null)
            {
                return(stopToken);
            }

            if (nextToken != null)
            {
                RqlToken token = nextToken;

                nextToken = null;
                return(token);
            }

            char c = ReadChar();

            while (c != '\0' && Char.IsWhiteSpace(c))
            {
                c = ReadChar();
            }

            int tokenOffset = this.offset - 1;

            if (c == '\0')
            {
                return(stopToken = RqlToken.End(offset));
            }

            if (Char.IsLetter(c))
            {
                sb.Clear();
                sb.Append(c);

                c = ReadChar();

                bool lastCharWasDot = false;

                while (c != 0 && (Char.IsLetterOrDigit(c)) || c == '.')
                {
                    sb.Append(c);

                    if (c == '.')
                    {
                        if (lastCharWasDot)
                        {
                            goto Error;
                        }

                        lastCharWasDot = true;
                    }
                    else
                    {
                        lastCharWasDot = false;
                    }

                    c = ReadChar();
                }

                UnreadChar();

                string s = sb.ToString();

                if (s == "true")
                {
                    return(RqlToken.Constant(tokenOffset, (object)true));
                }
                else if (s == "false")
                {
                    return(RqlToken.Constant(tokenOffset, (object)false));
                }
                else if (s == "null")
                {
                    return(RqlToken.Constant(tokenOffset, null));
                }
                else
                {
                    return(RqlToken.Identifier(tokenOffset, s));
                }
            }
            else if (Char.IsDigit(c) || c == '-')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                if (sb[0] == '-' && !Char.IsDigit(c))
                {
                    goto Error;
                }

                while (Char.IsDigit(c))
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                if (c == '.')
                {
                    sb.Append(c);
                    c = ReadChar();

                    while (Char.IsDigit(c))
                    {
                        sb.Append(c);
                        c = ReadChar();
                    }

                    UnreadChar();

                    Double n;

                    if (Double.TryParse(sb.ToString(), out n))
                    {
                        return(RqlToken.Constant(tokenOffset, n));
                    }
                }
                else
                {
                    UnreadChar();

                    Int32 n;

                    if (Int32.TryParse(sb.ToString(), out n))
                    {
                        return(RqlToken.Constant(tokenOffset, n));
                    }
                }
            }
            else if (c == '$')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (Char.IsLetterOrDigit(c))
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                UnreadChar();

                if (sb.Length > 0)
                {
                    return(RqlToken.Constant(tokenOffset, new RqlId(sb.ToString())));
                }
            }
            else if (c == '@')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (c != 'Z')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                sb.Append(c);

                RqlDateTime dateTime;

                if (RqlDateTime.TryParse(sb.ToString(), out dateTime))
                {
                    return(RqlToken.Constant(tokenOffset, dateTime));
                }
            }
            else if (c == '~')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (c != 'S')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                sb.Append(c);

                RqlTimeSpan timeSpan;

                if (RqlTimeSpan.TryParse(sb.ToString(), out timeSpan))
                {
                    return(RqlToken.Constant(tokenOffset, timeSpan));
                }
            }
            else if (c == '\'')
            {
                sb.Clear();
                c = ReadChar();

                while (c != '\0' && c != '\'')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                if (c == '\'')
                {
                    return(RqlToken.Constant(tokenOffset, sb.ToString()));
                }
            }
            else
            {
                switch ((char)c)
                {
                case ',':
                    return(RqlToken.Comma(tokenOffset));

                case '(':
                    return(RqlToken.LeftParen(tokenOffset));

                case ')':
                    return(RqlToken.RightParen(tokenOffset));
                }
            }

Error:
            return(stopToken = RqlToken.Error(tokenOffset));
        }