Exemplo n.º 1
0
        /// <summary>
        /// Parses the number.
        /// </summary>
        /// <param name="tokenStart">The token start.</param>
        /// <param name="signum">The signum.</param>
        protected void ParseNumber(int tokenStart, int signum)
        {
            int n = 0;
            double divide = 1;

            char c;
            while (this.HasMoreChar())
            {
                c = this.GetCurrentChar();
                if (Char.IsDigit(c))
                {
                    n = (n * 10) + (c - '0');
                    this.Advance();
                }
                else
                {
                    break;
                }
            }

            if (this.HasMoreChar())
            {
                c = this.GetCurrentChar();
                if (c == '.')
                {
                    this.Advance();

                    while (this.HasMoreChar())
                    {
                        c = this.GetCurrentChar();
                        if (Char.IsDigit(c))
                        {
                            n = (n * 10) + (c - '0');
                            divide *= 10;
                            this.Advance();
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            decimal num = new decimal(signum * (divide > 1 ? (n / divide) : n));

            int tokenEnd;
            if (this.HasMoreChar())
            {
                c = this.GetCurrentChar();
                if (c == '%')
                {
                    this.Advance();

                    tokenEnd = this.GetCurrentOffset();

                    this.Token = new NumericToken(
                        TokenTypeEnum.Percentage,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd),
                        num);

                    this.Flush(tokenEnd);
                    return;
                }

                if (Char.IsLetter(c))
                {
                    int unitStart = this.GetCurrentOffset();
                    this.Advance();

                    while (this.HasMoreChar() && Char.IsLetter(this.GetCurrentChar()))
                    {
                        this.Advance();
                    }

                    tokenEnd = this.GetCurrentOffset();

                    this.Token = new NumericWithUnitToken(
                        TokenTypeEnum.Dimension,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd),
                        num,
                        this.GetText(unitStart, tokenEnd));

                    this.Flush(tokenEnd);
                    return;
                }
            }

            tokenEnd = this.GetCurrentOffset();

            this.Token = new NumericToken(
                TokenTypeEnum.Number,
                tokenStart + this.LocalTokenOffset,
                tokenEnd + this.LocalTokenOffset,
                this.GetText(tokenStart, tokenEnd),
                num);

            this.Flush(tokenEnd);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the rest of URI.
        /// </summary>
        /// <param name="tokenStart">The token start.</param>
        /// <param name="prefix">The prefix.</param>
        protected void ParseRestOfURI(int tokenStart, string prefix)
        {
            this.SwallowWhitespace(false);

            string value = string.Empty;
            if (this.HasMoreChar())
            {
                char c = this.GetCurrentChar();
                if (c == '"' || c == '\'')
                {
                    this.Advance();

                    value = this.ParseString(c);
                }
                else
                {
                    int valueStart = this.GetCurrentOffset();
                    this.Advance();

                    while (this.HasMoreChar())
                    {
                        c = this.GetCurrentChar();
                        if (IsWhitespace(c) || c == ')')
                        {
                            break;
                        }

                        this.Advance();
                    }

                    value = this.GetText(valueStart, this.GetCurrentOffset());
                }
            }

            this.SwallowWhitespace(false);

            if (this.HasMoreChar())
            {
                char c = this.GetCurrentChar();
                if (c == ')')
                {
                    this.Advance();
                }
            }

            int tokenEnd = this.GetCurrentOffset();
            this.Token = new UriToken(
                tokenStart + this.LocalTokenOffset,
                tokenEnd + this.LocalTokenOffset,
                this.GetText(tokenStart, tokenEnd),
                prefix,
                value);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns>
        /// true if the enumerator was successfully Advanced to the next element; false if the enumerator has passed the end of the collection.
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
        public bool MoveNext()
        {
            this.Token = null;
            if (this.HasMoreChar())
            {
                int tokenStart = this.GetCurrentOffset();
                char c = this.GetCurrentChar();

                int tokenEnd;
                if (IsWhitespace(c))
                {
                    int distance = this.LookOverWhitespace(tokenStart);
                    tokenEnd = tokenStart + distance;

                    this.Token = new Token(
                        TokenTypeEnum.Whitespace,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd));

                    this.Advance(distance);
                    return true;
                }

                if (Char.IsDigit(c))
                {
                    this.ParseNumber(tokenStart, 1);
                    return true;
                }

                if ("{}()[]:;,".IndexOf(c) >= 0)
                {
                    tokenEnd = tokenStart + 1;

                    this.Token = new Token(
                        TokenTypeEnum.Delimiter,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd));

                    this.Advance();
                    return true;
                }

                if (c == '~' || c == '|')
                {
                    if (this.HasMoreChar(1) && this.GetCharRelative(1) == '=')
                    {
                        this.Advance();
                    } // fall through
                }
                else if (c == '/')
                {
                    if (this.HasMoreChar(1))
                    {
                        char c2 = this.GetCharRelative(1);
                        if (c2 == '/')
                        {
                            this.Advance(2, false);

                            int commentStart = this.GetCurrentOffset();
                            while (this.HasMoreChar())
                            {
                                c2 = this.GetCurrentChar();
                                if (c2 == '\r' || c2 == '\n' || c2 == '\f')
                                {
                                    break;
                                }

                                this.Advance();
                            }

                            tokenEnd = this.GetCurrentOffset();

                            this.Token = new StringValueToken(
                                TokenTypeEnum.Comment,
                                tokenStart + this.LocalTokenOffset,
                                tokenEnd + this.LocalTokenOffset,
                                this.GetText(tokenStart, tokenEnd),
                                this.GetText(commentStart, tokenEnd));

                            return true;
                        }

                        if (c2 == '*')
                        {
                            this.Advance(2, false);

                            if ((this.Options & TokenizerOptions.MultilineCommentBeginEndTokens) == TokenizerOptions.MultilineCommentBeginEndTokens)
                            {
                                tokenEnd = this.GetCurrentOffset();
                                this.Token = new Token(
                                    TokenTypeEnum.MultiLineCommentStart,
                                    tokenStart + this.LocalTokenOffset,
                                    tokenEnd + this.LocalTokenOffset,
                                    this.GetText(tokenStart, tokenEnd));

                                return true;
                            }

                            int commentStart = this.GetCurrentOffset();
                            int commentEnd = commentStart;

                            while (this.HasMoreChar())
                            {
                                c2 = this.GetCurrentChar();
                                if (c2 == '*' && this.HasMoreChar(1) && this.GetCharRelative(1) == '/')
                                {
                                    this.Advance(2, false);
                                    break;
                                }

                                commentEnd++;
                                this.Advance();
                            }

                            tokenEnd = this.GetCurrentOffset();

                            this.Token = new StringValueToken(
                                TokenTypeEnum.Comment,
                                tokenStart + this.LocalTokenOffset,
                                tokenEnd + this.LocalTokenOffset,
                                this.GetText(tokenStart, tokenEnd),
                                this.GetText(commentStart, commentEnd));

                            this.Flush(tokenEnd);

                            return true;
                        } // fall through
                    }  // fall through
                }
                else if (c == '@')
                {
                    int distance = this.LookOverIdentifier(1);
                    if (distance > 0)
                    {
                        tokenEnd = tokenStart + 1 + distance;

                        this.Token = new Token(
                            TokenTypeEnum.AtIdentifier,
                            tokenStart + this.LocalTokenOffset,
                            tokenEnd + this.LocalTokenOffset,
                            this.GetText(tokenStart, tokenEnd));

                        this.Advance(1 + distance);
                        return true;
                    } // fall through
                }
                else if (c == '#')
                {
                    int distance = this.LookOverName(1);
                    if (distance > 0)
                    {
                        tokenEnd = tokenStart + 1 + distance;

                        this.Token = new Token(
                            TokenTypeEnum.HashName,
                            tokenStart + this.LocalTokenOffset,
                            tokenEnd + this.LocalTokenOffset,
                            this.GetText(tokenStart, tokenEnd));

                        this.Advance(1 + distance);
                        return true;
                    }    // fall through
                }
                else if (c == '<' &&
                         this.HasMoreChar(3) &&
                         this.GetCharRelative(1) == '!' &&
                         this.GetCharRelative(2) == '-' &&
                         this.GetCharRelative(3) == '-')
                {
                    tokenEnd = tokenStart + 4;

                    this.Token = new Token(
                        TokenTypeEnum.CDataOpen,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd));

                    this.Advance(4);
                    return true;
                }
                else if (c == '-')
                {
                    if (this.HasMoreChar(2) &&
                        this.GetCharRelative(1) == '-' &&
                        this.GetCharRelative(2) == '>')
                    {
                        tokenEnd = tokenStart + 3;

                        this.Token = new Token(
                            TokenTypeEnum.CDataClose,
                            tokenStart + this.LocalTokenOffset,
                            tokenEnd + this.LocalTokenOffset,
                            this.GetText(tokenStart, tokenEnd));

                        this.Advance(3);
                        return true;
                    }

                    if (this.HasMoreChar(1))
                    {
                        if (Char.IsDigit(this.GetCharRelative(1)))
                        {
                            this.Advance(); // swallow -

                            this.ParseNumber(tokenStart, -1);
                            return true;
                        }

                        int distance = this.LookOverName(1);
                        if (distance > 0)
                        {
                            tokenEnd = tokenStart + 1 + distance;

                            this.Token = new Token(
                                TokenTypeEnum.Identifier,
                                tokenStart + this.LocalTokenOffset,
                                tokenEnd + this.LocalTokenOffset,
                                this.GetText(tokenStart, tokenEnd));

                            this.Advance(1 + distance);
                            return true;
                        }  // fall through
                    }   // fall through
                }
                else if (IsNameStartChar(c))
                {
                    if (c == 'u' && this.HasMoreChar(1) && this.GetCharRelative(1) == '+')
                    {
                        int unicodeCodeDistance = this.LookOverUnicodeCode(2);
                        if (unicodeCodeDistance > 0)
                        {
                            tokenEnd = tokenStart + 2 + unicodeCodeDistance;

                            this.Advance(2 + unicodeCodeDistance, false);

                            if (this.HasMoreChar() && this.GetCurrentChar() == '-')
                            {
                                int unicodeCodeDistance2 = this.LookOverUnicodeCode(1);
                                if (unicodeCodeDistance2 > 0)
                                {
                                    tokenEnd += 1 + unicodeCodeDistance2;

                                    this.Advance(1 + unicodeCodeDistance2, false);
                                }
                            }

                            this.Token = new Token(
                                TokenTypeEnum.UnicodeRange,
                                tokenStart + this.LocalTokenOffset,
                                tokenEnd + this.LocalTokenOffset,
                                this.GetText(tokenStart, tokenEnd));

                            this.Flush(tokenEnd);
                            return true;
                        }
                    }

                    int distance = this.LookOverName(0);
                    tokenEnd = tokenStart + distance;

                    string name = this.GetText(tokenStart, tokenEnd);

                    if (this.HasMoreChar(distance) &&
                        this.GetCharRelative(distance) == '(')
                    {
                        this.Advance(distance + 1, false);

                        if ("uri".Equals(name) || "url".Equals(name))
                        {
                            this.ParseRestOfURI(tokenStart, name);
                            return true;
                        }

                        tokenEnd++;

                        this.Token = new Token(
                            TokenTypeEnum.Function,
                            tokenStart + this.LocalTokenOffset,
                            tokenEnd + this.LocalTokenOffset,
                            this.GetText(tokenStart, tokenEnd));
                        return true;
                    }

                    this.Token = new Token(
                        TokenTypeEnum.Identifier,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd));

                    this.Advance(distance);
                    return true;
                }
                else if (c == '"' || c == '\'')
                {
                    this.Advance();

                    string value = this.ParseString(c);

                    tokenEnd = this.GetCurrentOffset();

                    this.Token = new StringValueToken(
                        TokenTypeEnum.String,
                        tokenStart + this.LocalTokenOffset,
                        tokenEnd + this.LocalTokenOffset,
                        this.GetText(tokenStart, tokenEnd),
                        value);

                    return true;
                }
                else if (c == '$' && this.HasMoreChar(1))
                {
                    char c2 = this.GetCharRelative(1);
                    if (c2 == '{')
                    {
                        int nameStart = tokenStart + 2;
                        int nameEnd = nameStart;

                        int distance = 2;
                        while (this.HasMoreChar(distance))
                        {
                            c2 = this.GetCharRelative(distance);
                            if (c2 == '}')
                            {
                                distance++;
                                break;
                            }

                            nameEnd++;
                            distance++;
                        }

                        tokenEnd = tokenStart + distance;

                        this.Token = new StringValueToken(
                            TokenTypeEnum.Variable,
                            tokenStart + this.LocalTokenOffset,
                            tokenEnd + this.LocalTokenOffset,
                            this.GetText(tokenStart, tokenEnd),
                            this.GetText(nameStart, nameEnd));

                        this.Advance(distance);

                        return true;
                    }

                    if (Char.IsLetter(c2))
                    {
                        int nameStart = tokenStart + 1;

                        int distance = 1;
                        while (this.HasMoreChar(distance))
                        {
                            c2 = this.GetCharRelative(distance);
                            if (Char.IsLetter(c2))
                            {
                                distance++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        tokenEnd = tokenStart + distance;

                        this.Token = new StringValueToken(
                            TokenTypeEnum.Variable,
                            tokenStart + this.LocalTokenOffset,
                            tokenEnd + this.LocalTokenOffset,
                            this.GetText(tokenStart, tokenEnd),
                            this.GetText(nameStart, tokenEnd));

                        this.Advance(distance);

                        return true;
                    }
                }

                if ((this.Options & TokenizerOptions.MultilineCommentBeginEndTokens) == TokenizerOptions.MultilineCommentBeginEndTokens)
                {
                    if (c == '*')
                    {
                        if (this.HasMoreChar(1))
                        {
                            char c2 = this.GetCharRelative(1);
                            if (c2 == '/')
                            {
                                this.Advance(2, false);

                                tokenEnd = this.GetCurrentOffset();
                                this.Token = new Token(
                                    TokenTypeEnum.MultiLineCommentEnd,
                                    tokenStart + this.LocalTokenOffset,
                                    tokenEnd + this.LocalTokenOffset,
                                    this.GetText(tokenStart, tokenEnd));

                                return true;
                            }
                        }
                    }
                }

                this.Advance();

                tokenEnd = this.GetCurrentOffset();

                this.Token = new Token(
                    TokenTypeEnum.Operator,
                    tokenStart + this.LocalTokenOffset,
                    tokenEnd + this.LocalTokenOffset,
                    this.GetText(tokenStart, tokenEnd));
            }
            else
            {
                return false;
            }

            return true;
        }