Пример #1
0
 public UnexpectedTokenException(LexicalMode mode, CssToken token)
     : base($"Unexpected token reading {mode}. Was '{token.Kind}'.")
 {
     this.token = token;
 }
Пример #2
0
 public UnexpectedTokenException(CssToken token)
     : base($"Unexpected token. Was '{token.Kind}:{token.Text}'.", token.Position)
 {
     this.token = token;
 }
Пример #3
0
 // "Current mode is:" + current + ". Leaving " + mode + "."
 public UnbalancedBlock(LexicalMode currentMode, CssToken startToken)
     : base("The block is unclosed, '}' expected", startToken.Position)
 {
 }
Пример #4
0
 public UnexpectedTokenException(LexicalMode mode, TokenKind expectedKind, CssToken token)
     : base($"Unexpected token at {token.Position} reading {mode}. Expected '{expectedKind}'. Was '{token.Kind}'.", token.Position)
 {
     this.token = token;
 }
Пример #5
0
        public CssFunction ReadFunctionCall(CssToken name)
        {
            Read();                 // (

            ReadTrivia();           // trivia

            var args = ReadValueList();

            Read(TokenKind.RightParenthesis, LexicalMode.Function); // )

            if (name.Text == "url")
            {
                return new CssUrl(name, args) {
                    Trailing = ReadTrivia()
                };
            }

            return new CssFunction(name.Text, args) {
                Trailing = ReadTrivia()
            };
        }
Пример #6
0
        public CssToken Next()
        {
            current = ReadNext();

            return current;
        }
Пример #7
0
        private CssToken ReadNext()
        {
            if (reader.IsEof)
            {
                throw new Exception("Cannot read past EOF. Current: " + current.ToString() + ".");
            }

            if (stack.Count > 0)
            {
                return(stack.Pop());
            }

            if (char.IsWhiteSpace(reader.Current) || reader.Current == '\uFEFF')
            {
                return(ReadTrivia());
            }

            switch (reader.Current)
            {
            case '@': return(new CssToken(TokenKind.AtSymbol, reader.Read(), reader.Position));

            case '$':
                mode.Enter(LexicalMode.Symbol);

                return(new CssToken(TokenKind.Dollar, reader.Read(), reader.Position));

            case '/':
                var peek = reader.Peek();

                if (peek == '/' || peek == '*')
                {
                    return(ReadComment());
                }
                else if (peek == ' ')
                {
                    return(new CssToken(TokenKind.Divide, reader.Read(), reader.Position));
                }
                else
                {
                    return(new CssToken(TokenKind.String, reader.Read(), reader.Position));
                }

            case ':':
                // Pseudo-elements
                if (reader.Peek() == ':' || reader.Peek() == '-')
                {
                    return(ReadValue());
                }
                else
                {
                    var colon = new CssToken(TokenKind.Colon, reader.Read(), reader.Position);

                    return(MaybeColonOrPseudoClass(colon));
                }

            case ',': return(new CssToken(TokenKind.Comma, reader.Read(), reader.Position));

            case ';':
                LeaveValueMode();
                return(new CssToken(TokenKind.Semicolon, reader.Read(), reader.Position));

            case '{':
                mode.Enter(LexicalMode.Block);

                return(new CssToken(TokenKind.BlockStart, reader.Read(), reader.Position));

            case '}':

                LeaveValueMode();

                if (mode.Current == LexicalMode.InterpolatedString)
                {
                    mode.Leave(LexicalMode.InterpolatedString);

                    return(new CssToken(TokenKind.InterpolatedStringEnd, reader.Read(), reader.Position));
                }
                else
                {
                    mode.Leave(LexicalMode.Block, this.Current.Position);

                    return(new CssToken(TokenKind.BlockEnd, reader.Read(), reader.Position));
                }

            case '(': return(new CssToken(TokenKind.LeftParenthesis, reader.Read(), reader.Position));

            case ')': return(new CssToken(TokenKind.RightParenthesis, reader.Read(), reader.Position));

            case '&':
                if (reader.Peek() == '&')
                {
                    return(new CssToken(TokenKind.And, reader.Read(2), reader.Position - 1));
                }
                else
                {
                    return(new CssToken(TokenKind.Ampersand, reader.Read(), reader.Position));
                }

            case '|':
                if (reader.Peek() == '|')
                {
                    return(new CssToken(TokenKind.Or, reader.Read(2), reader.Position - 1));
                }

                else
                {
                    break;
                }

            case '!':     // !=
                if (reader.Peek() == '=')
                {
                    return(new CssToken(TokenKind.NotEquals, reader.Read(2), reader.Position - 1));
                }

                else
                {
                    break;
                }

            case '=':     // ==
                if (reader.Peek() == '=')
                {
                    return(new CssToken(TokenKind.Equals, reader.Read(2), reader.Position - 1));
                }

                else
                {
                    break;
                }

            case '>':     // >=
                if (reader.Peek() == '=')
                {
                    return(new CssToken(TokenKind.Gte, reader.Read(2), reader.Position - 1));
                }
                else
                {
                    return(new CssToken(TokenKind.Gt, reader.Read(), reader.Position));
                }

            case '<':     // <=
                if (reader.Peek() == '=')
                {
                    return(new CssToken(TokenKind.Lte, reader.Read(2), reader.Position - 1));
                }
                else
                {
                    return(new CssToken(TokenKind.Lt, reader.Read(), reader.Position));
                }

            case '#' when reader.Peek() == '{':
                mode.Enter(LexicalMode.InterpolatedString);

                return(new CssToken(TokenKind.InterpolatedStringStart, reader.Read(2), reader.Position - 1));

            case '+' when reader.Peek() == ' ':
                return(new CssToken(TokenKind.Add, reader.Read(), reader.Position));

            case '*': return(new CssToken(TokenKind.Multiply, reader.Read(), reader.Position));

            case '.' when char.IsDigit(reader.Peek()):
                return(ReadNumber());

            case '-':
                if (char.IsDigit(reader.Peek()))
                {
                    return(ReadNumber());
                }
                else if (reader.Peek() == ' ')
                {
                    return(new CssToken(TokenKind.Subtract, reader.Read(), reader.Position));
                }
                else
                {
                    break;
                }

            case '\'':
            case '"':
                return(ReadQuotedString(reader.Current));


            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return(ReadNumber());
            }

            return(mode.Current switch
            {
                LexicalMode.Symbol => ReadSymbol(),
                LexicalMode.Value => ReadValue(),
                LexicalMode.Unit => ReadUnit(),
                _ => ReadName()
            });
Пример #8
0
        public CssToken Next()
        {
            current = ReadNext();

            return(current);
        }