Added Symbolicate tool.
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / Lexer.cs
1 //
2 // Lexer.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Globalization;
30
31 namespace System.Net.Http.Headers
32 {
33         struct Token
34         {
35                 public enum Type
36                 {
37                         Error,
38                         End,
39                         Token,
40                         QuotedString,
41                         SeparatorEqual,
42                         SeparatorSemicolon,
43                         SeparatorSlash,
44                         SeparatorDash,
45                         SeparatorComma,
46                         OpenParens,
47                 }
48
49                 public static readonly Token Empty = new Token (Type.Token, 0, 0);
50
51                 readonly Type type;
52
53                 public Token (Type type, int startPosition, int endPosition)
54                         : this ()
55                 {
56                         this.type = type;
57                         StartPosition = startPosition;
58                         EndPosition = endPosition;
59                 }
60
61                 public int StartPosition { get; private set; }
62                 public int EndPosition { get; private set; }
63
64                 public Type Kind {
65                         get {
66                                 return type;
67                         }
68                 }
69
70                 public static implicit operator Token.Type (Token token)
71                 {
72                         return token.type;
73                 }
74
75                 public override string ToString ()
76                 {
77                         return type.ToString ();
78                 }
79         }
80
81         class Lexer
82         {
83                 // any CHAR except CTLs or separators
84                 static readonly bool[] token_chars = {
85                         /*0*/   false, false, false, false, false, false, false, false, false, false,
86                         /*10*/  false, false, false, false, false, false, false, false, false, false,
87                         /*20*/  false, false, false, false, false, false, false, false, false, false,
88                         /*30*/  false, false, false, true, false, true, true, true, true, true,
89                         /*40*/  false, false, true, true, false, true, true, false, true, true,
90                         /*50*/  true, true, true, true, true, true, true, true, false, false,
91                         /*60*/  false, false, false, false, false, true, true, true, true, true,
92                         /*70*/  true, true, true, true, true, true, true, true, true, true,
93                         /*80*/  true, true, true, true, true, true, true, true, true, true,
94                         /*90*/  true, false, false, false, true, true, true, true, true, true,
95                         /*100*/ true, true, true, true, true, true, true, true, true, true,
96                         /*110*/ true, true, true, true, true, true, true, true, true, true,
97                         /*120*/ true, true, true, false, true, false
98                         };
99
100                 static readonly int last_token_char = token_chars.Length;
101                 static readonly string[] dt_formats = new[] {
102                                 "r",
103                                 "dddd, dd'-'MMM'-'yy HH:mm:ss 'GMT'",
104                                 "ddd MMM d HH:mm:ss yyyy",
105                                 "d MMM yy H:m:s",
106                                 "ddd, d MMM yyyy H:m:s zzz"
107                 };
108
109                 readonly string s;
110                 int pos;
111
112                 public Lexer (string stream)
113                 {
114                         this.s = stream;
115                 }
116
117                 public int Position {
118                         get {
119                                 return pos;
120                         }
121                         set {
122                                 pos = value;
123                         }
124                 }
125
126                 public string GetStringValue (Token token)
127                 {
128                         return s.Substring (token.StartPosition, token.EndPosition - token.StartPosition);
129                 }
130
131                 public string GetStringValue (Token start, Token end)
132                 {
133                         return s.Substring (start.StartPosition, end.EndPosition - start.StartPosition);
134                 }
135
136                 public string GetQuotedStringValue (Token start)
137                 {
138                         return s.Substring (start.StartPosition + 1, start.EndPosition - start.StartPosition - 2);
139                 }
140
141                 public string GetRemainingStringValue (int position)
142                 {
143                         return position > s.Length ? null : s.Substring (position);
144                 }
145
146                 public bool IsStarStringValue (Token token)
147                 {
148                         return (token.EndPosition - token.StartPosition) == 1 && s[token.StartPosition] == '*';
149                 }
150
151                 public bool TryGetNumericValue (Token token, out int value)
152                 {
153                         return int.TryParse (GetStringValue (token), NumberStyles.None, CultureInfo.InvariantCulture, out value);
154                 }
155
156                 public TimeSpan? TryGetTimeSpanValue (Token token)
157                 {
158                         int seconds;
159                         if (TryGetNumericValue (token, out seconds)) {
160                                 return TimeSpan.FromSeconds (seconds);
161                         }
162
163                         return null;
164                 }
165
166                 public bool TryGetDateValue (Token token, out DateTimeOffset value)
167                 {
168                         string text = token == Token.Type.QuotedString ?
169                                 s.Substring (token.StartPosition + 1, token.EndPosition - token.StartPosition - 2) :
170                                 GetStringValue (token);
171
172                         return TryGetDateValue (text, out value);
173                 }
174
175                 public static bool TryGetDateValue (string text, out DateTimeOffset value)
176                 {
177                         const DateTimeStyles DefaultStyles = DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces;
178
179                         return DateTimeOffset.TryParseExact (text, dt_formats, DateTimeFormatInfo.InvariantInfo, DefaultStyles, out value);
180                 }
181
182                 public bool TryGetDoubleValue (Token token, out double value)
183                 {
184                         string s = GetStringValue (token);
185                         return double.TryParse (s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out value);
186                 }
187
188                 public static bool IsValidToken (string input)
189                 {
190                         int i = 0;
191                         //
192                         // any CHAR except CTLs or separator
193                         //
194                         for (; i < input.Length; ++i) {
195                                 char s = input[i];
196                                 if (s > last_token_char || !token_chars[s])
197                                         return false;
198                         }
199
200                         return i > 0;
201                 }
202
203                 public void EatChar ()
204                 {
205                         ++pos;
206                 }
207
208                 public int PeekChar ()
209                 {
210                         return pos < s.Length ? s[pos] : -1;
211                 }
212
213                 public bool ScanCommentOptional (out string value)
214                 {
215                         Token t;
216                         if (ScanCommentOptional (out value, out t))
217                                 return true;
218
219                         return t == Token.Type.End;
220                 }
221
222                 public bool ScanCommentOptional (out string value, out Token readToken)
223                 {
224                         readToken = Scan ();
225                         if (readToken != Token.Type.OpenParens) {
226                                 value = null;
227                                 return false;
228                         }
229
230                         while (pos < s.Length) {
231                                 var ch = s[pos];
232                                 if (ch == ')') {
233                                         ++pos;
234                                         var start = readToken.StartPosition;
235                                         value = s.Substring (start, pos - start);
236                                         return true;
237                                 }
238
239                                 // any OCTET except CTLs, but including LWS
240                                 if (ch < 32 || ch > 126)
241                                         break;
242
243                                 ++pos;
244                         }
245
246                         value = null;
247                         return false;
248                 }
249
250                 public Token Scan (bool recognizeDash = false)
251                 {
252                         int start = pos;
253                         if (s == null)
254                                 return new Token (Token.Type.Error, 0, 0);
255
256                         Token.Type ttype;
257                         if (pos >= s.Length) {
258                                 ttype = Token.Type.End;
259                         } else {
260                                 ttype = Token.Type.Error;
261                         start:
262                                 char ch = s[pos++];
263                                 switch (ch) {
264                                 case ' ':
265                                 case '\t':
266                                         if (pos == s.Length) {
267                                                 ttype = Token.Type.End;
268                                                 break;
269                                         }
270
271                                         goto start;
272                                 case '=':
273                                         ttype = Token.Type.SeparatorEqual;
274                                         break;
275                                 case ';':
276                                         ttype = Token.Type.SeparatorSemicolon;
277                                         break;
278                                 case '/':
279                                         ttype = Token.Type.SeparatorSlash;
280                                         break;
281                                 case '-':
282                                         if (recognizeDash) {
283                                                 ttype = Token.Type.SeparatorDash;
284                                                 break;
285                                         }
286
287                                         goto default;
288                                 case ',':
289                                         ttype = Token.Type.SeparatorComma;
290                                         break;
291                                 case '"':
292                                         // Quoted string
293                                         start = pos - 1;
294                                         while (pos < s.Length) {
295                                                 ch = s[pos];
296                                                 if (ch == '"') {
297                                                         ++pos;
298                                                         ttype = Token.Type.QuotedString;
299                                                         break;
300                                                 }
301
302                                                 // any OCTET except CTLs, but including LWS
303                                                 if (ch < 32 || ch > 126)
304                                                         break;
305
306                                                 ++pos;
307                                         }
308
309                                         break;
310                                 case '(':
311                                         start = pos - 1;
312                                         ttype = Token.Type.OpenParens;
313                                         break;
314                                 default:
315                                         if (ch <= last_token_char && token_chars[ch]) {
316                                                 start = pos - 1;
317
318                                                 ttype = Token.Type.Token;
319                                                 while (pos < s.Length) {
320                                                         ch = s[pos];
321                                                         if (ch > last_token_char || !token_chars[ch]) {
322                                                                 break;
323                                                         }
324
325                                                         ++pos;
326                                                 }
327                                         }
328
329                                         break;
330                                 }
331                         }
332
333                         return new Token (ttype, start, pos);
334                 }
335         }
336 }