1 using System;
2 using System.Collections;
3 using System.Lex;
4 using System.Parsing;
5 using JsonTokens;
6
7
8
9 public static class JsonParser
10 {
11 public static UniquePtr<System.Json.JsonValue> Parse(JsonLexer& lexer)
12 {
13 UniquePtr<System.Json.JsonValue> value;
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 = Value(lexer);
24 value.Reset(cast<System.Json.JsonValue*>(match.value));
25 #if (DEBUG)
26 if (lexer.Log() != null)
27 {
28 lexer.Log()->DecIndent();
29 lexer.Log()->WriteEndRule(u"parse");
30 }
31 #endif
32 if (match.hit)
33 {
34 if (*lexer == System.Lex.END_TOKEN)
35 {
36 return value;
37 }
38 else
39 {
40 lexer.ThrowExpectationFailure(lexer.GetSpan(), GetEndTokenInfo());
41 }
42 }
43 else
44 {
45 lexer.ThrowExpectationFailure(span, u"Value");
46 }
47 return value;
48 }
49 public static Match Value(JsonLexer& lexer)
50 {
51 #if (DEBUG)
52 Span debugSpan;
53 bool writeToLog = lexer.Log() != null;
54 if (writeToLog)
55 {
56 debugSpan = lexer.GetSpan();
57 System.Lex.WriteBeginRuleToLog(lexer, u"Value");
58 }
59 #endif
60 UniquePtr<System.Json.JsonObject> o;
61 UniquePtr<System.Json.JsonArray> a;
62 Match match(false);
63 Match* parentMatch0 = &match;
64 {
65 long save = lexer.GetPos();
66 Match match(false);
67 Match* parentMatch1 = &match;
68 {
69 long save = lexer.GetPos();
70 Match match(false);
71 Match* parentMatch2 = &match;
72 {
73 long save = lexer.GetPos();
74 Match match(false);
75 Match* parentMatch3 = &match;
76 {
77 long save = lexer.GetPos();
78 Match match(false);
79 Match* parentMatch4 = &match;
80 {
81 long save = lexer.GetPos();
82 long pos = lexer.GetPos();
83 Span span = lexer.GetSpan();
84 switch (*lexer)
85 {
86 case STRING:
87 {
88 ++lexer;
89 Token token = lexer.GetToken(pos);
90 {
91 #if (DEBUG)
92 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
93 #endif
94 return Match(true, new System.Json.JsonString(System.Json.ParseStringLiteral(token)));
95 }
96 break;
97 }
98 case NUMBER:
99 {
100 ++lexer;
101 Token token = lexer.GetToken(pos);
102 {
103 #if (DEBUG)
104 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
105 #endif
106 return Match(true, new System.Json.JsonNumber(System.Json.ParseNumber(token)));
107 }
108 break;
109 }
110 }
111 *parentMatch4 = match;
112 if (!match.hit)
113 {
114 Match match(false);
115 Match* parentMatch5 = &match;
116 lexer.SetPos(save);
117 {
118 Match match(false);
119 Match* parentMatch6 = &match;
120 {
121 long pos = lexer.GetPos();
122 Match match = JsonParser.Object(lexer);
123 o.Reset(cast<System.Json.JsonObject*>(match.value));
124 if (match.hit)
125 {
126 {
127 #if (DEBUG)
128 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
129 #endif
130 return Match(true, o.Release());
131 }
132 }
133 *parentMatch6 = match;
134 }
135 *parentMatch5 = match;
136 }
137 *parentMatch4 = match;
138 }
139 }
140 *parentMatch3 = match;
141 if (!match.hit)
142 {
143 Match match(false);
144 Match* parentMatch7 = &match;
145 lexer.SetPos(save);
146 {
147 Match match(false);
148 Match* parentMatch8 = &match;
149 {
150 long pos = lexer.GetPos();
151 Match match = JsonParser.Array(lexer);
152 a.Reset(cast<System.Json.JsonArray*>(match.value));
153 if (match.hit)
154 {
155 {
156 #if (DEBUG)
157 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
158 #endif
159 return Match(true, a.Release());
160 }
161 }
162 *parentMatch8 = match;
163 }
164 *parentMatch7 = match;
165 }
166 *parentMatch3 = match;
167 }
168 }
169 *parentMatch2 = match;
170 if (!match.hit)
171 {
172 Match match(false);
173 Match* parentMatch9 = &match;
174 lexer.SetPos(save);
175 {
176 Match match(false);
177 Match* parentMatch10 = &match;
178 {
179 long pos = lexer.GetPos();
180 Match match(false);
181 if (*lexer == TRUE)
182 {
183 ++lexer;
184 match.hit = true;
185 }
186 if (match.hit)
187 {
188 {
189 #if (DEBUG)
190 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
191 #endif
192 return Match(true, new System.Json.JsonBool(true));
193 }
194 }
195 *parentMatch10 = match;
196 }
197 *parentMatch9 = match;
198 }
199 *parentMatch2 = match;
200 }
201 }
202 *parentMatch1 = match;
203 if (!match.hit)
204 {
205 Match match(false);
206 Match* parentMatch11 = &match;
207 lexer.SetPos(save);
208 {
209 Match match(false);
210 Match* parentMatch12 = &match;
211 {
212 long pos = lexer.GetPos();
213 Match match(false);
214 if (*lexer == FALSE)
215 {
216 ++lexer;
217 match.hit = true;
218 }
219 if (match.hit)
220 {
221 {
222 #if (DEBUG)
223 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
224 #endif
225 return Match(true, new System.Json.JsonBool(false));
226 }
227 }
228 *parentMatch12 = match;
229 }
230 *parentMatch11 = match;
231 }
232 *parentMatch1 = match;
233 }
234 }
235 *parentMatch0 = match;
236 if (!match.hit)
237 {
238 Match match(false);
239 Match* parentMatch13 = &match;
240 lexer.SetPos(save);
241 {
242 Match match(false);
243 Match* parentMatch14 = &match;
244 {
245 long pos = lexer.GetPos();
246 Match match(false);
247 if (*lexer == NULL)
248 {
249 ++lexer;
250 match.hit = true;
251 }
252 if (match.hit)
253 {
254 {
255 #if (DEBUG)
256 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
257 #endif
258 return Match(true, new System.Json.JsonNull());
259 }
260 }
261 *parentMatch14 = match;
262 }
263 *parentMatch13 = match;
264 }
265 *parentMatch0 = match;
266 }
267 }
268 #if (DEBUG)
269 if (writeToLog)
270 {
271 if (match.hit)
272 {
273 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Value");
274 }
275 else
276 {
277 System.Lex.WriteFailureToLog(lexer, u"Value");
278 }
279 }
280 #endif
281 if (!match.hit)
282 {
283 match.value = null;
284 }
285 return match;
286 }
287 public static Match Object(JsonLexer& lexer)
288 {
289 #if (DEBUG)
290 Span debugSpan;
291 bool writeToLog = lexer.Log() != null;
292 if (writeToLog)
293 {
294 debugSpan = lexer.GetSpan();
295 System.Lex.WriteBeginRuleToLog(lexer, u"Object");
296 }
297 #endif
298 UniquePtr<System.Json.JsonObject> o;
299 Match match(false);
300 Match* parentMatch0 = &match;
301 {
302 Match match(false);
303 Match* parentMatch1 = &match;
304 {
305 Match match(false);
306 Match* parentMatch2 = &match;
307 {
308 long pos = lexer.GetPos();
309 Match match(false);
310 if (*lexer == LBRACE)
311 {
312 ++lexer;
313 match.hit = true;
314 }
315 if (match.hit)
316 {
317 o.Reset(new System.Json.JsonObject());
318 }
319 *parentMatch2 = match;
320 }
321 *parentMatch1 = match;
322 }
323 if (match.hit)
324 {
325 Match match(false);
326 Match* parentMatch3 = &match;
327 {
328 Match match(true);
329 long save = lexer.GetPos();
330 Match* parentMatch4 = &match;
331 {
332 Match match(false);
333 Match* parentMatch5 = &match;
334 {
335 Match match(false);
336 Match* parentMatch6 = &match;
337 {
338 Match match = JsonParser.Field(lexer, o.Get());
339 *parentMatch6 = match;
340 }
341 if (match.hit)
342 {
343 Match match(false);
344 Match* parentMatch7 = &match;
345 {
346 Match match(true);
347 Match* parentMatch8 = &match;
348 {
349 while (true)
350 {
351 long save = lexer.GetPos();
352 {
353 Match match(false);
354 Match* parentMatch9 = &match;
355 {
356 Match match(false);
357 if (*lexer == COMMA)
358 {
359 ++lexer;
360 match.hit = true;
361 }
362 *parentMatch9 = match;
363 }
364 if (match.hit)
365 {
366 Match match(false);
367 Match* parentMatch10 = &match;
368 {
369 Match match = JsonParser.Field(lexer, o.Get());
370 *parentMatch10 = match;
371 }
372 *parentMatch9 = match;
373 }
374 if (match.hit)
375 {
376 *parentMatch8 = match;
377 }
378 else
379 {
380 lexer.SetPos(save);
381 break;
382 }
383 }
384 }
385 }
386 *parentMatch7 = match;
387 }
388 *parentMatch6 = match;
389 }
390 *parentMatch5 = match;
391 }
392 if (match.hit)
393 {
394 *parentMatch4 = match;
395 }
396 else
397 {
398 lexer.SetPos(save);
399 }
400 }
401 *parentMatch3 = match;
402 }
403 *parentMatch1 = match;
404 }
405 *parentMatch0 = match;
406 }
407 if (match.hit)
408 {
409 Match match(false);
410 Match* parentMatch11 = &match;
411 {
412 Match match(false);
413 Match* parentMatch12 = &match;
414 {
415 long pos = lexer.GetPos();
416 Match match(false);
417 if (*lexer == RBRACE)
418 {
419 ++lexer;
420 match.hit = true;
421 }
422 if (match.hit)
423 {
424 {
425 #if (DEBUG)
426 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Object");
427 #endif
428 return Match(true, o.Release());
429 }
430 }
431 *parentMatch12 = match;
432 }
433 *parentMatch11 = match;
434 }
435 *parentMatch0 = match;
436 }
437 #if (DEBUG)
438 if (writeToLog)
439 {
440 if (match.hit)
441 {
442 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Object");
443 }
444 else
445 {
446 System.Lex.WriteFailureToLog(lexer, u"Object");
447 }
448 }
449 #endif
450 if (!match.hit)
451 {
452 match.value = null;
453 }
454 return match;
455 }
456 public static Match Field(JsonLexer& lexer, System.Json.JsonObject* o)
457 {
458 #if (DEBUG)
459 Span debugSpan;
460 bool writeToLog = lexer.Log() != null;
461 if (writeToLog)
462 {
463 debugSpan = lexer.GetSpan();
464 System.Lex.WriteBeginRuleToLog(lexer, u"Field");
465 }
466 #endif
467 ustring n;
468 UniquePtr<System.Json.JsonValue> v;
469 Match match(false);
470 Match* parentMatch0 = &match;
471 {
472 Match match(false);
473 Match* parentMatch1 = &match;
474 {
475 Match match(false);
476 Match* parentMatch2 = &match;
477 {
478 long pos = lexer.GetPos();
479 Match match(false);
480 if (*lexer == STRING)
481 {
482 ++lexer;
483 match.hit = true;
484 }
485 if (match.hit)
486 {
487 Token token = lexer.GetToken(pos);
488 n = System.Json.ParseStringLiteral(token);
489 }
490 *parentMatch2 = match;
491 }
492 *parentMatch1 = match;
493 }
494 if (match.hit)
495 {
496 Match match(false);
497 Match* parentMatch3 = &match;
498 {
499 Match match(false);
500 if (*lexer == COLON)
501 {
502 ++lexer;
503 match.hit = true;
504 }
505 *parentMatch3 = match;
506 }
507 *parentMatch1 = match;
508 }
509 *parentMatch0 = match;
510 }
511 if (match.hit)
512 {
513 Match match(false);
514 Match* parentMatch4 = &match;
515 {
516 Match match(false);
517 Match* parentMatch5 = &match;
518 {
519 long pos = lexer.GetPos();
520 Match match = JsonParser.Value(lexer);
521 v.Reset(cast<System.Json.JsonValue*>(match.value));
522 if (match.hit)
523 {
524 o->AddField(n, v.Release());
525 }
526 *parentMatch5 = match;
527 }
528 *parentMatch4 = match;
529 }
530 *parentMatch0 = match;
531 }
532 #if (DEBUG)
533 if (writeToLog)
534 {
535 if (match.hit)
536 {
537 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Field");
538 }
539 else
540 {
541 System.Lex.WriteFailureToLog(lexer, u"Field");
542 }
543 }
544 #endif
545 if (!match.hit)
546 {
547 match.value = null;
548 }
549 return match;
550 }
551 public static Match Array(JsonLexer& lexer)
552 {
553 #if (DEBUG)
554 Span debugSpan;
555 bool writeToLog = lexer.Log() != null;
556 if (writeToLog)
557 {
558 debugSpan = lexer.GetSpan();
559 System.Lex.WriteBeginRuleToLog(lexer, u"Array");
560 }
561 #endif
562 UniquePtr<System.Json.JsonArray> a;
563 UniquePtr<System.Json.JsonValue> v;
564 Match match(false);
565 Match* parentMatch0 = &match;
566 {
567 Match match(false);
568 Match* parentMatch1 = &match;
569 {
570 Match match(false);
571 Match* parentMatch2 = &match;
572 {
573 long pos = lexer.GetPos();
574 Match match(false);
575 if (*lexer == LBRACKET)
576 {
577 ++lexer;
578 match.hit = true;
579 }
580 if (match.hit)
581 {
582 a.Reset(new System.Json.JsonArray());
583 }
584 *parentMatch2 = match;
585 }
586 *parentMatch1 = match;
587 }
588 if (match.hit)
589 {
590 Match match(false);
591 Match* parentMatch3 = &match;
592 {
593 Match match(true);
594 long save = lexer.GetPos();
595 Match* parentMatch4 = &match;
596 {
597 Match match(false);
598 Match* parentMatch5 = &match;
599 {
600 Match match(false);
601 Match* parentMatch6 = &match;
602 {
603 Match match(false);
604 Match* parentMatch7 = &match;
605 {
606 long pos = lexer.GetPos();
607 Match match = JsonParser.Value(lexer);
608 v.Reset(cast<System.Json.JsonValue*>(match.value));
609 if (match.hit)
610 {
611 a->AddItem(v.Release());
612 }
613 *parentMatch7 = match;
614 }
615 *parentMatch6 = match;
616 }
617 if (match.hit)
618 {
619 Match match(false);
620 Match* parentMatch8 = &match;
621 {
622 Match match(true);
623 Match* parentMatch9 = &match;
624 {
625 while (true)
626 {
627 long save = lexer.GetPos();
628 {
629 Match match(false);
630 Match* parentMatch10 = &match;
631 {
632 Match match(false);
633 if (*lexer == COMMA)
634 {
635 ++lexer;
636 match.hit = true;
637 }
638 *parentMatch10 = match;
639 }
640 if (match.hit)
641 {
642 Match match(false);
643 Match* parentMatch11 = &match;
644 {
645 Match match(false);
646 Match* parentMatch12 = &match;
647 {
648 long pos = lexer.GetPos();
649 Match match = JsonParser.Value(lexer);
650 v.Reset(cast<System.Json.JsonValue*>(match.value));
651 if (match.hit)
652 {
653 a->AddItem(v.Release());
654 }
655 *parentMatch12 = match;
656 }
657 *parentMatch11 = match;
658 }
659 *parentMatch10 = match;
660 }
661 if (match.hit)
662 {
663 *parentMatch9 = match;
664 }
665 else
666 {
667 lexer.SetPos(save);
668 break;
669 }
670 }
671 }
672 }
673 *parentMatch8 = match;
674 }
675 *parentMatch6 = match;
676 }
677 *parentMatch5 = match;
678 }
679 if (match.hit)
680 {
681 *parentMatch4 = match;
682 }
683 else
684 {
685 lexer.SetPos(save);
686 }
687 }
688 *parentMatch3 = match;
689 }
690 *parentMatch1 = match;
691 }
692 *parentMatch0 = match;
693 }
694 if (match.hit)
695 {
696 Match match(false);
697 Match* parentMatch13 = &match;
698 {
699 Match match(false);
700 Match* parentMatch14 = &match;
701 {
702 long pos = lexer.GetPos();
703 Match match(false);
704 if (*lexer == RBRACKET)
705 {
706 ++lexer;
707 match.hit = true;
708 }
709 if (match.hit)
710 {
711 {
712 #if (DEBUG)
713 if (writeToLog) System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Array");
714 #endif
715 return Match(true, a.Release());
716 }
717 }
718 *parentMatch14 = match;
719 }
720 *parentMatch13 = match;
721 }
722 *parentMatch0 = match;
723 }
724 #if (DEBUG)
725 if (writeToLog)
726 {
727 if (match.hit)
728 {
729 System.Lex.WriteSuccessToLog(lexer, debugSpan, u"Array");
730 }
731 else
732 {
733 System.Lex.WriteFailureToLog(lexer, u"Array");
734 }
735 }
736 #endif
737 if (!match.hit)
738 {
739 match.value = null;
740 }
741 return match;
742 }
743 }