Token.cs 8.74 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
#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
{
    using System;

    /// <summary>
    /// Represent a token and optionally any text associated with it.
    /// </summary>
    public struct Token : IEquatable<Token>
    {
        /// <summary>
        /// Gets the kind/type/class of the token.
        /// </summary>
        public TokenKind Kind { get; private set; }

        /// <summary>
        /// Gets text, if any, associated with the token.
        /// </summary>
        public string Text { get; private set; }

        private Token(TokenKind kind) : this(kind, null) {}

        private Token(TokenKind kind, string text) : this()
        {
            Kind = kind;
            Text = text;
        }

        /// <summary>
        /// Creates an end-of-input token.
        /// </summary>
        public static Token Eoi()
        {
            return new Token(TokenKind.Eoi);
        }

        private static readonly Token _star = Char('*');
        private static readonly Token _dot = Char('.');
        private static readonly Token _colon = Char(':');
        private static readonly Token _comma = Char(',');
        private static readonly Token _rightParenthesis = Char(')');
        private static readonly Token _equals = Char('=');
        private static readonly Token _pipe = Char('|');
        private static readonly Token _leftBracket = Char('[');
        private static readonly Token _rightBracket = Char(']');

        /// <summary>
        /// Creates a star token.
        /// </summary>
        public static Token Star()
        {
            return _star;
        }

        /// <summary>
        /// Creates a dot token.
        /// </summary>
        public static Token Dot()
        {
            return _dot;
        }

        /// <summary>
        /// Creates a colon token.
        /// </summary>
        public static Token Colon()
        {
            return _colon;
        }

        /// <summary>
        /// Creates a comma token.
        /// </summary>
        public static Token Comma()
        {
            return _comma;
        }

        /// <summary>
        /// Creates a right parenthesis token.
        /// </summary>
        public static Token RightParenthesis()
        {
            return _rightParenthesis;
        }

        /// <summary>
        /// Creates an equals token.
        /// </summary>
        public static Token Equals()
        {
            return _equals;
        }

        /// <summary>
        /// Creates a left bracket token.
        /// </summary>
        public static Token LeftBracket()
        {
            return _leftBracket;
        }

        /// <summary>
        /// Creates a right bracket token.
        /// </summary>
        public static Token RightBracket()
        {
            return _rightBracket;
        }

        /// <summary>
        /// Creates a pipe (vertical line) token.
        /// </summary>
        public static Token Pipe()
        {
            return _pipe;
        }

        /// <summary>
        /// Creates a plus token.
        /// </summary>
        public static Token Plus()
        {
            return new Token(TokenKind.Plus);
        }

        /// <summary>
        /// Creates a greater token.
        /// </summary>
        public static Token Greater()
        {
            return new Token(TokenKind.Greater);
        }

        /// <summary>
        /// Creates an includes token.
        /// </summary>
        public static Token Includes()
        {
            return new Token(TokenKind.Includes);
        }

        /// <summary>
        /// Creates a dash-match token.
        /// </summary>
        public static Token DashMatch()
        {
            return new Token(TokenKind.DashMatch);
        }

        /// <summary>
        /// Creates a prefix-match token.
        /// </summary>
        public static Token PrefixMatch()
        {
            return new Token(TokenKind.PrefixMatch);
        }

        /// <summary>
        /// Creates a suffix-match token.
        /// </summary>
        public static Token SuffixMatch()
        {
            return new Token(TokenKind.SuffixMatch);
        }

        /// <summary>
        /// Creates a substring-match token.
        /// </summary>
        public static Token SubstringMatch()
        {
            return new Token(TokenKind.SubstringMatch);
        }

        /// <summary>
        /// Creates a general sibling token.
        /// </summary>
        public static Token Tilde()
        {
            return new Token(TokenKind.Tilde);
        }

        /// <summary>
        /// Creates an identifier token.
        /// </summary>
        public static Token Ident(string text)
        {
            ValidateTextArgument(text);
            return new Token(TokenKind.Ident, text);
        }

        /// <summary>
        /// Creates an integer token.
        /// </summary>
        public static Token Integer(string text)
        {
            ValidateTextArgument(text);
            return new Token(TokenKind.Integer, text);
        }

        /// <summary>
        /// Creates a hash-name token.
        /// </summary>
        public static Token Hash(string text)
        {
            ValidateTextArgument(text);
            return new Token(TokenKind.Hash, text);
        }

        /// <summary>
        /// Creates a white-space token.
        /// </summary>
        public static Token WhiteSpace(string space)
        {
            ValidateTextArgument(space);
            return new Token(TokenKind.WhiteSpace, space);
        }

        /// <summary>
        /// Creates a string token.
        /// </summary>
        public static Token String(string text)
        {
            return new Token(TokenKind.String, text ?? string.Empty);
        }

        /// <summary>
        /// Creates a function token.
        /// </summary>
        public static Token Function(string text)
        {
            ValidateTextArgument(text);
            return new Token(TokenKind.Function, text);
        }

        /// <summary>
        /// Creates an arbitrary character token.
        /// </summary>
        public static Token Char(char ch)
        {
            return new Token(TokenKind.Char, ch.ToString());
        }

        /// <summary>
        /// Indicates whether this instance and a specified object are equal.
        /// </summary>
        public override bool Equals(object obj)
        {
            return obj != null && obj is Token && Equals((Token) obj);
        }

        /// <summary>
        /// Returns the hash code for this instance.
        /// </summary>
        public override int GetHashCode()
        {
            return Text == null ? Kind.GetHashCode() : Kind.GetHashCode() ^ Text.GetHashCode();
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        public bool Equals(Token other)
        {
            return Kind == other.Kind && Text == other.Text;
        }

        /// <summary>
        /// Gets a string representation of the token.
        /// </summary>
        public override string ToString()
        {
            return Text == null ? Kind.ToString() : Kind + ": " + Text;
        }
        /// <summary>
        /// Performs a logical comparison of the two tokens to determine 
        /// whether they are equal. 
        /// </summary>
        public static bool operator==(Token a, Token b)
        {
            return a.Equals(b);
        }

        /// <summary>
        /// Performs a logical comparison of the two tokens to determine 
        /// whether they are inequal. 
        /// </summary>
        public static bool operator !=(Token a, Token b)
        {
            return !a.Equals(b);
        }

        private static void ValidateTextArgument(string text)
        {
            if (text == null) throw new ArgumentNullException("text");
            if (text.Length == 0) throw new ArgumentException(null, "text");
        }
    }
}