1 using System;
2 using System.Collections;
3 using System.Lex;
4 using System.Parsing;
5 using System.Net.Http;
6 using HttpTokens;
7
8
9
10 public static class HttpMediaTypeParser
11 {
12 public static void Parse(HttpLexer& lexer, MimeType* mimeType)
13 {
14 #if (DEBUG)
15 if (lexer.Log() != null)
16 {
17 lexer.Log()->WriteBeginRule(u"parse");
18 lexer.Log()->IncIndent();
19 }
20 #endif
21 ++lexer;
22 System.Lex.Span span = lexer.GetSpan();
23 Match match = MediaType(lexer, mimeType);
24 if (match.hit)
25 {
26 if (*lexer == System.Lex.END_TOKEN)
27 {
28 return;
29 }
30 else
31 {
32 lexer.ThrowExpectationFailure(lexer.GetSpan(), GetEndTokenInfo());
33 }
34 }
35 else
36 {
37 lexer.ThrowExpectationFailure(span, u"MediaType");
38 }
39 return;
40 }
41 public static Match MediaType(HttpLexer& lexer, MimeType* mimeType)
42 {
43 #if (DEBUG)
44 Span debugSpan;
45 bool writeToLog = lexer.Log() != null;
46 if (writeToLog)
47 {
48 debugSpan = lexer.GetSpan();
49 System.Lex.WriteBeginRuleToLog(lexer, u"MediaType");
50 }
51 #endif
52 Pair<string, string> param;
53 UniquePtr<Value<string>> t;
54 UniquePtr<Value<string>> st;
55 UniquePtr<Value<Pair<string, string>>> p;
56 Match match(false);
57 Match* parentMatch0 = &match;
58 {
59 Match match(false);
60 Match* parentMatch1 = &match;
61 {
62 long pos = lexer.GetPos();
63 Match match(false);
64 Match* parentMatch2 = &match;
65 {
66 Match match(false);
67 Match* parentMatch3 = &match;
68 {
69 Match match(false);
70 Match* parentMatch4 = &match;
71 {
72 Match match = HttpMediaTypeParser.Type(lexer);
73 t.Reset(cast<Value<string>*>(match.value));
74 *parentMatch4 = match;
75 }
76 if (match.hit)
77 {
78 Match match(false);
79 Match* parentMatch5 = &match;
80 {
81 Match match(false);
82 if (*lexer == SLASH)
83 {
84 ++lexer;
85 match.hit = true;
86 }
87 *parentMatch5 = match;
88 }
89 *parentMatch4 = match;
90 }
91 *parentMatch3 = match;
92 }
93 if (match.hit)
94 {
95 Match match(false);
96 Match* parentMatch6 = &match;
97 {
98 Match match = HttpMediaTypeParser.Subtype(lexer);
99 st.Reset(cast<Value<string>*>(match.value));
100 *parentMatch6 = match;
101 }
102 *parentMatch3 = match;
103 }
104 *parentMatch2 = match;
105 }
106 if (match.hit)
107 {
108 mimeType->SetValue(t->value, st->value);
109 }
110 *parentMatch1 = match;
111 }
112 *parentMatch0 = match;
113 }
114 if (match.hit)
115 {
116 Match match(false);
117 Match* parentMatch7 = &match;
118 {
119 Match match(true);
120 Match* parentMatch8 = &match;
121 {
122 while (true)
123 {
124 long save = lexer.GetPos();
125 {
126 Match match(false);
127 Match* parentMatch9 = &match;
128 {
129 Match match(false);
130 Match* parentMatch10 = &match;
131 {
132 Match match(false);
133 Match* parentMatch11 = &match;
134 {
135 Match match(false);
136 Match* parentMatch12 = &match;
137 {
138 Match match(true);
139 long save = lexer.GetPos();
140 Match* parentMatch13 = &match;
141 {
142 Match match = Http.lws(lexer);
143 if (match.hit)
144 {
145 *parentMatch13 = match;
146 }
147 else
148 {
149 lexer.SetPos(save);
150 }
151 }
152 *parentMatch12 = match;
153 }
154 if (match.hit)
155 {
156 Match match(false);
157 Match* parentMatch14 = &match;
158 {
159 Match match(false);
160 if (*lexer == SEMICOLON)
161 {
162 ++lexer;
163 match.hit = true;
164 }
165 *parentMatch14 = match;
166 }
167 *parentMatch12 = match;
168 }
169 *parentMatch11 = match;
170 }
171 if (match.hit)
172 {
173 Match match(false);
174 Match* parentMatch15 = &match;
175 {
176 Match match(true);
177 long save = lexer.GetPos();
178 Match* parentMatch16 = &match;
179 {
180 Match match = Http.lws(lexer);
181 if (match.hit)
182 {
183 *parentMatch16 = match;
184 }
185 else
186 {
187 lexer.SetPos(save);
188 }
189 }
190 *parentMatch15 = match;
191 }
192 *parentMatch11 = match;
193 }
194 *parentMatch10 = match;
195 }
196 if (match.hit)
197 {
198 Match match(false);
199 Match* parentMatch17 = &match;
200 {
201 Match match(false);
202 Match* parentMatch18 = &match;
203 {
204 long pos = lexer.GetPos();
205 Match match = Http.parameter(lexer);
206 p.Reset(cast<Value<Pair<string, string>>*>(match.value));
207 if (match.hit)
208 {
209 param = p->value;
210 mimeType->SetParameter(param.first, param.second);
211 }
212 *parentMatch18 = match;
213 }
214 *parentMatch17 = match;
215 }
216 *parentMatch10 = match;
217 }
218 *parentMatch9 = match;
219 }
220 if (match.hit)
221 {
222 *parentMatch8 = match;
223 }
224 else
225 {
226 lexer.SetPos(save);
227 break;
228 }
229 }
230 }
231 }
232 *parentMatch7 = match;
233 }
234 *parentMatch0 = match;
235 }
236 #if (DEBUG)
237 if (writeToLog)
238 {
239 if (match.hit)
240 {
241 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"MediaType");
242 }
243 else
244 {
245 System.Lex.WriteFailureToLog(lexer, u"MediaType");
246 }
247 }
248 #endif
249 if (!match.hit)
250 {
251 match.value = null;
252 }
253 return match;
254 }
255 public static Match Type(HttpLexer& lexer)
256 {
257 #if (DEBUG)
258 Span debugSpan;
259 bool writeToLog = lexer.Log() != null;
260 if (writeToLog)
261 {
262 debugSpan = lexer.GetSpan();
263 System.Lex.WriteBeginRuleToLog(lexer, u"Type");
264 }
265 #endif
266 UniquePtr<Value<string>> t;
267 Match match(false);
268 Match* parentMatch0 = &match;
269 {
270 long pos = lexer.GetPos();
271 Match match = Http.token(lexer);
272 t.Reset(cast<Value<string>*>(match.value));
273 if (match.hit)
274 {
275 {
276 #if (DEBUG)
277 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Type");
278 #endif
279 return Match(true, new Value<string>(t->value));
280 }
281 }
282 *parentMatch0 = match;
283 }
284 #if (DEBUG)
285 if (writeToLog)
286 {
287 if (match.hit)
288 {
289 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Type");
290 }
291 else
292 {
293 System.Lex.WriteFailureToLog(lexer, u"Type");
294 }
295 }
296 #endif
297 if (!match.hit)
298 {
299 match.value = null;
300 }
301 return match;
302 }
303 public static Match Subtype(HttpLexer& lexer)
304 {
305 #if (DEBUG)
306 Span debugSpan;
307 bool writeToLog = lexer.Log() != null;
308 if (writeToLog)
309 {
310 debugSpan = lexer.GetSpan();
311 System.Lex.WriteBeginRuleToLog(lexer, u"Subtype");
312 }
313 #endif
314 UniquePtr<Value<string>> t;
315 Match match(false);
316 Match* parentMatch0 = &match;
317 {
318 long pos = lexer.GetPos();
319 Match match = Http.token(lexer);
320 t.Reset(cast<Value<string>*>(match.value));
321 if (match.hit)
322 {
323 {
324 #if (DEBUG)
325 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Subtype");
326 #endif
327 return Match(true, new Value<string>(t->value));
328 }
329 }
330 *parentMatch0 = match;
331 }
332 #if (DEBUG)
333 if (writeToLog)
334 {
335 if (match.hit)
336 {
337 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Subtype");
338 }
339 else
340 {
341 System.Lex.WriteFailureToLog(lexer, u"Subtype");
342 }
343 }
344 #endif
345 if (!match.hit)
346 {
347 match.value = null;
348 }
349 return match;
350 }
351 }