Tokener.cs 11.2 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#region Copyright and License
// 
// Fizzler - CSS Selector Engine for Microsoft .NET Framework
// Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
// 
// This library is free software; you can redistribute it and/or modify it under 
// the terms of the GNU Lesser General Public License as published by the Free 
// Software Foundation; either version 3 of the License, or (at your option) 
// any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT 
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
// details.
// 
// You should have received a copy of the GNU Lesser General Public License 
// along with this library; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
// 
#endregion

namespace Fizzler
{
    #region Imports

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    
    #endregion

    /// <summary>
    /// Lexer for tokens in CSS selector grammar.
    /// </summary>
    public static class Tokener
    {
        /// <summary>
        /// Parses tokens from a given text source.
        /// </summary>
        public static IEnumerable<Token> Tokenize(TextReader reader)
        {
            if (reader == null) throw new ArgumentNullException("reader");
            return Tokenize(reader.ReadToEnd());
        }

        /// <summary>
        /// Parses tokens from a given string.
        /// </summary>
        public static IEnumerable<Token> Tokenize(string input)
        {
            var reader = new Reader(input ?? string.Empty);

            while (reader.Read() != null)
            {
                var ch = reader.Value;

                //
                // Identifier or function
                //
                if (ch == '-' || IsNmStart(ch))
                {
                    reader.Mark();
                    if (reader.Value == '-')
                    {
                        if (!IsNmStart(reader.Read()))
                            throw new FormatException(string.Format("Invalid identifier at position {0}.", reader.Position));
                    }
                    while (IsNmChar(reader.Read())) { /* NOP */ }
                    if (reader.Value == '(')
                        yield return Token.Function(reader.Marked());
                    else
                        yield return Token.Ident(reader.MarkedWithUnread());
                }
                //
                // Integer
                //
                else if (IsDigit(ch))
                {
                    reader.Mark();
                    do { /* NOP */ } while (IsDigit(reader.Read()));
                    yield return Token.Integer(reader.MarkedWithUnread());
                }
                //
                // Whitespace, including that which is coupled with some punctuation
                //
                else if (IsS(ch))
                {
                    var space = ParseWhiteSpace(reader);
                    ch = reader.Read();
                    switch (ch)
                    {
                        case ',': yield return Token.Comma(); break;
                        case '+': yield return Token.Plus(); break;
                        case '>': yield return Token.Greater(); break;
                        case '~': yield return Token.Tilde(); break;

                        default:
                            reader.Unread();
                            yield return Token.WhiteSpace(space);
                            break;
                    }
                }
                else switch(ch)
                {
                    case '*': // * or *=
                    case '~': // ~ or ~=
                    case '|': // | or |=
                    {
                        if (reader.Read() == '=')
                        {
                            yield return ch == '*' 
                                       ? Token.SubstringMatch() 
                                       : ch == '|' ? Token.DashMatch() 
                                       : Token.Includes();
                        }
                        else
                        {
                            reader.Unread();
                            yield return ch == '*' || ch == '|'
                                ? Token.Char(ch.Value) 
                                : Token.Tilde();
                        }
                        break;
                    }
                    case '^': // ^=
                    case '$': // $=
                    {
                        if (reader.Read() != '=')
                            throw new FormatException(string.Format("Invalid character at position {0}.", reader.Position));
                        
                        switch (ch)
                        {
                            case '^': yield return Token.PrefixMatch(); break;
                            case '$': yield return Token.SuffixMatch(); break;
                        }
                        break;
                    }
                    //
                    // Single-character punctuation
                    //
                    case '.': yield return Token.Dot(); break;
                    case ':':  yield return Token.Colon(); break;
                    case ',':  yield return Token.Comma(); break;
                    case '=':  yield return Token.Equals(); break;
                    case '[':  yield return Token.LeftBracket(); break;
                    case ']':  yield return Token.RightBracket(); break;
                    case ')':  yield return Token.RightParenthesis(); break;
                    case '+': yield return Token.Plus(); break;
                    case '>':  yield return Token.Greater(); break;
                    case '#':  yield return Token.Hash(ParseHash(reader)); break;
                    //
                    // Single- or double-quoted strings
                    //
                    case '\"':
                    case '\'': yield return ParseString(reader, /* quote */ ch.Value); break;
                    
                    default:
                        throw new FormatException(string.Format("Invalid character at position {0}.", reader.Position));
                }
            }
            yield return Token.Eoi();
        }

        private static string ParseWhiteSpace(Reader reader)
        {
            Debug.Assert(reader != null);

            reader.Mark();
            while (IsS(reader.Read())) { /* NOP */ }
            return reader.MarkedWithUnread();
        }

        private static string ParseHash(Reader reader)
        {
            Debug.Assert(reader != null);

            reader.MarkFromNext(); // skipping #
            while (IsNmChar(reader.Read())) { /* NOP */ }
            var text = reader.MarkedWithUnread();
            if (text.Length == 0)
                throw new FormatException(string.Format("Invalid hash at position {0}.", reader.Position));
            return text;
        }

        private static Token ParseString(Reader reader, char quote)
        {
            Debug.Assert(reader != null);

            //
            // TODO Support full string syntax!
            //
            // string    {string1}|{string2}
            // string1   \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*\"
            // string2   \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*\'
            // nonascii  [^\0-\177]
            // escape    {unicode}|\\[^\n\r\f0-9a-f]
            // unicode   \\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?
            //

            var strpos = reader.Position;
            reader.MarkFromNext(); // skipping quote

            char? ch;
            StringBuilder sb = null;
            
            while ((ch = reader.Read()) != quote)
            {
                if (ch == null)
                    throw new FormatException(string.Format("Unterminated string at position {0}.", strpos));
                
                if (ch == '\\')
                {
                    ch = reader.Read();

                    //
                    // NOTE: Only escaping of quote and backslash supported!
                    //
                    
                    if (ch != quote && ch != '\\')
                        throw new FormatException(string.Format("Invalid escape sequence at position {0} in a string at position {1}.", reader.Position, strpos));
                    
                    if (sb == null)
                        sb = new StringBuilder();
                    
                    sb.Append(reader.MarkedExceptLast());
                    reader.Mark();
                }
            }
            
            var text = reader.Marked();

            if (sb != null)
                text = sb.Append(text).ToString();

            return Token.String(text);
        }

        private static bool IsDigit(char? ch) // [0-9]
        {
            return ch >= '0' && ch <= '9';
        }

        private static bool IsS(char? ch) // [ \t\r\n\f]
        {
            return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f';
        }

        private static bool IsNmStart(char? ch) // [_a-z]|{nonascii}|{escape}
        {
            return ch == '_'
                || (ch >= 'a' && ch <= 'z')
                || (ch >= 'A' && ch <= 'Z');                
        }

        private static bool IsNmChar(char? ch) // [_a-z0-9-]|{nonascii}|{escape}
        {
            return IsNmStart(ch) || ch == '-' || (ch >= '0' && ch <= '9');
        }

        private sealed class Reader
        {
            private readonly string _input;
            private int _index = -1;
            private int _start = -1;

            public Reader(string input)
            {
                _input = input;
            }

            private bool Ready { get { return _index >= 0 && _index < _input.Length; } }
            public char? Value { get { return Ready ? _input[_index] : (char?)null; } }
            public int Position { get { return _index + 1; } }
          
            public void Mark()
            {
                _start = _index;
            }

            public void MarkFromNext()
            {
                _start = _index + 1;
            }

            public string Marked()
            {
                return Marked(0);
            }

            public string MarkedExceptLast()
            {
                return Marked(-1);
            }

            private string Marked(int trim)
            {
                var start = _start;
                var count = Math.Min(_input.Length, _index + trim) - start;
                return count > 0
                     ? _input.Substring(start, count)
                     : string.Empty;
            }

            public char? Read()
            {
                _index = Position >= _input.Length ? _input.Length : _index + 1;
                return Value;
            }

            public void Unread()
            {
                _index = Math.Max(-1, _index - 1);
            }

            public string MarkedWithUnread()
            {
                var text = Marked();
                Unread();
                return text;
            }
        }
    }
}