forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_json_parse.ml
687 lines (608 loc) · 23.7 KB
/
ext_json_parse.ml
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# 1 "ext/ext_json_parse.mll"
type error =
| Illegal_character of char
| Unterminated_string
| Unterminated_comment
| Illegal_escape of string
| Unexpected_token
| Expect_comma_or_rbracket
| Expect_comma_or_rbrace
| Expect_colon
| Expect_string_or_rbrace
| Expect_eof
(* | Trailing_comma_in_obj *)
(* | Trailing_comma_in_array *)
let fprintf = Format.fprintf
let report_error ppf = function
| Illegal_character c ->
fprintf ppf "Illegal character (%s)" (Char.escaped c)
| Illegal_escape s ->
fprintf ppf "Illegal backslash escape in string or character (%s)" s
| Unterminated_string ->
fprintf ppf "Unterminated_string"
| Expect_comma_or_rbracket ->
fprintf ppf "Expect_comma_or_rbracket"
| Expect_comma_or_rbrace ->
fprintf ppf "Expect_comma_or_rbrace"
| Expect_colon ->
fprintf ppf "Expect_colon"
| Expect_string_or_rbrace ->
fprintf ppf "Expect_string_or_rbrace"
| Expect_eof ->
fprintf ppf "Expect_eof"
| Unexpected_token
->
fprintf ppf "Unexpected_token"
(* | Trailing_comma_in_obj *)
(* -> fprintf ppf "Trailing_comma_in_obj" *)
(* | Trailing_comma_in_array *)
(* -> fprintf ppf "Trailing_comma_in_array" *)
| Unterminated_comment
-> fprintf ppf "Unterminated_comment"
exception Error of Lexing.position * Lexing.position * error
let () =
Printexc.register_printer
(function x ->
match x with
| Error (loc_start,loc_end,error) ->
Some (Format.asprintf
"@[%a:@ %a@ -@ %a)@]"
report_error error
Ext_position.print loc_start
Ext_position.print loc_end
)
| _ -> None
)
type token =
| Comma
| Eof
| False
| Lbrace
| Lbracket
| Null
| Colon
| Number of string
| Rbrace
| Rbracket
| String of string
| True
let error (lexbuf : Lexing.lexbuf) e =
raise (Error (lexbuf.lex_start_p, lexbuf.lex_curr_p, e))
let lexeme_len (x : Lexing.lexbuf) =
x.lex_curr_pos - x.lex_start_pos
let update_loc ({ lex_curr_p; _ } as lexbuf : Lexing.lexbuf) diff =
lexbuf.lex_curr_p <-
{
lex_curr_p with
pos_lnum = lex_curr_p.pos_lnum + 1;
pos_bol = lex_curr_p.pos_cnum - diff;
}
let char_for_backslash = function
| 'n' -> '\010'
| 'r' -> '\013'
| 'b' -> '\008'
| 't' -> '\009'
| c -> c
let dec_code c1 c2 c3 =
100 * (Char.code c1 - 48) + 10 * (Char.code c2 - 48) + (Char.code c3 - 48)
let hex_code c1 c2 =
let d1 = Char.code c1 in
let val1 =
if d1 >= 97 then d1 - 87
else if d1 >= 65 then d1 - 55
else d1 - 48 in
let d2 = Char.code c2 in
let val2 =
if d2 >= 97 then d2 - 87
else if d2 >= 65 then d2 - 55
else d2 - 48 in
val1 * 16 + val2
let lf = '\010'
# 124 "ext/ext_json_parse.ml"
let __ocaml_lex_tables = {
Lexing.lex_base =
"\000\000\239\255\240\255\241\255\000\000\025\000\011\000\244\255\
\245\255\246\255\247\255\248\255\249\255\000\000\000\000\000\000\
\041\000\001\000\254\255\005\000\005\000\253\255\001\000\002\000\
\252\255\000\000\000\000\003\000\251\255\001\000\003\000\250\255\
\079\000\089\000\099\000\121\000\131\000\141\000\153\000\163\000\
\001\000\253\255\254\255\023\000\255\255\006\000\246\255\189\000\
\248\255\215\000\255\255\249\255\249\000\181\000\252\255\009\000\
\063\000\075\000\234\000\251\255\032\001\250\255";
Lexing.lex_backtrk =
"\255\255\255\255\255\255\255\255\013\000\013\000\016\000\255\255\
\255\255\255\255\255\255\255\255\255\255\016\000\016\000\016\000\
\016\000\016\000\255\255\000\000\012\000\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\013\000\255\255\013\000\255\255\013\000\255\255\
\255\255\255\255\255\255\001\000\255\255\255\255\255\255\008\000\
\255\255\255\255\255\255\255\255\006\000\006\000\255\255\006\000\
\001\000\002\000\255\255\255\255\255\255\255\255";
Lexing.lex_default =
"\001\000\000\000\000\000\000\000\255\255\255\255\255\255\000\000\
\000\000\000\000\000\000\000\000\000\000\255\255\255\255\255\255\
\255\255\255\255\000\000\255\255\020\000\000\000\255\255\255\255\
\000\000\255\255\255\255\255\255\000\000\255\255\255\255\000\000\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\042\000\000\000\000\000\255\255\000\000\047\000\000\000\047\000\
\000\000\051\000\000\000\000\000\255\255\255\255\000\000\255\255\
\255\255\255\255\255\255\000\000\255\255\000\000";
Lexing.lex_trans =
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\019\000\018\000\018\000\019\000\017\000\019\000\255\255\
\048\000\019\000\255\255\057\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\019\000\000\000\003\000\000\000\000\000\019\000\000\000\000\000\
\050\000\000\000\000\000\043\000\008\000\006\000\033\000\016\000\
\004\000\005\000\005\000\005\000\005\000\005\000\005\000\005\000\
\005\000\005\000\007\000\004\000\005\000\005\000\005\000\005\000\
\005\000\005\000\005\000\005\000\005\000\032\000\044\000\033\000\
\056\000\005\000\005\000\005\000\005\000\005\000\005\000\005\000\
\005\000\005\000\005\000\021\000\057\000\000\000\000\000\000\000\
\020\000\000\000\000\000\012\000\000\000\011\000\032\000\056\000\
\000\000\025\000\049\000\000\000\000\000\032\000\014\000\024\000\
\028\000\000\000\000\000\057\000\026\000\030\000\013\000\031\000\
\000\000\000\000\022\000\027\000\015\000\029\000\023\000\000\000\
\000\000\000\000\039\000\010\000\039\000\009\000\032\000\038\000\
\038\000\038\000\038\000\038\000\038\000\038\000\038\000\038\000\
\038\000\034\000\034\000\034\000\034\000\034\000\034\000\034\000\
\034\000\034\000\034\000\034\000\034\000\034\000\034\000\034\000\
\034\000\034\000\034\000\034\000\034\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\037\000\000\000\037\000\000\000\
\035\000\036\000\036\000\036\000\036\000\036\000\036\000\036\000\
\036\000\036\000\036\000\036\000\036\000\036\000\036\000\036\000\
\036\000\036\000\036\000\036\000\036\000\036\000\036\000\036\000\
\036\000\036\000\036\000\036\000\036\000\036\000\036\000\255\255\
\035\000\038\000\038\000\038\000\038\000\038\000\038\000\038\000\
\038\000\038\000\038\000\038\000\038\000\038\000\038\000\038\000\
\038\000\038\000\038\000\038\000\038\000\000\000\000\000\255\255\
\000\000\056\000\000\000\000\000\055\000\058\000\058\000\058\000\
\058\000\058\000\058\000\058\000\058\000\058\000\058\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\054\000\
\000\000\054\000\000\000\000\000\000\000\000\000\054\000\000\000\
\002\000\041\000\000\000\000\000\000\000\255\255\046\000\053\000\
\053\000\053\000\053\000\053\000\053\000\053\000\053\000\053\000\
\053\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\255\255\059\000\059\000\059\000\059\000\059\000\059\000\
\059\000\059\000\059\000\059\000\000\000\000\000\000\000\000\000\
\000\000\060\000\060\000\060\000\060\000\060\000\060\000\060\000\
\060\000\060\000\060\000\054\000\000\000\000\000\000\000\000\000\
\000\000\054\000\060\000\060\000\060\000\060\000\060\000\060\000\
\000\000\000\000\000\000\000\000\000\000\054\000\000\000\000\000\
\000\000\054\000\000\000\054\000\000\000\000\000\000\000\052\000\
\061\000\061\000\061\000\061\000\061\000\061\000\061\000\061\000\
\061\000\061\000\060\000\060\000\060\000\060\000\060\000\060\000\
\000\000\061\000\061\000\061\000\061\000\061\000\061\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\061\000\061\000\061\000\061\000\061\000\061\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\255\255\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\255\255\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000";
Lexing.lex_check =
"\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\000\000\000\000\017\000\000\000\000\000\019\000\020\000\
\045\000\019\000\020\000\055\000\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\000\000\255\255\000\000\255\255\255\255\019\000\255\255\255\255\
\045\000\255\255\255\255\040\000\000\000\000\000\004\000\000\000\
\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\
\000\000\000\000\000\000\006\000\006\000\006\000\006\000\006\000\
\006\000\006\000\006\000\006\000\006\000\004\000\043\000\005\000\
\056\000\005\000\005\000\005\000\005\000\005\000\005\000\005\000\
\005\000\005\000\005\000\016\000\057\000\255\255\255\255\255\255\
\016\000\255\255\255\255\000\000\255\255\000\000\005\000\056\000\
\255\255\014\000\045\000\255\255\255\255\004\000\000\000\023\000\
\027\000\255\255\255\255\057\000\025\000\029\000\000\000\030\000\
\255\255\255\255\015\000\026\000\000\000\013\000\022\000\255\255\
\255\255\255\255\032\000\000\000\032\000\000\000\005\000\032\000\
\032\000\032\000\032\000\032\000\032\000\032\000\032\000\032\000\
\032\000\033\000\033\000\033\000\033\000\033\000\033\000\033\000\
\033\000\033\000\033\000\034\000\034\000\034\000\034\000\034\000\
\034\000\034\000\034\000\034\000\034\000\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\035\000\255\255\035\000\255\255\
\034\000\035\000\035\000\035\000\035\000\035\000\035\000\035\000\
\035\000\035\000\035\000\036\000\036\000\036\000\036\000\036\000\
\036\000\036\000\036\000\036\000\036\000\037\000\037\000\037\000\
\037\000\037\000\037\000\037\000\037\000\037\000\037\000\047\000\
\034\000\038\000\038\000\038\000\038\000\038\000\038\000\038\000\
\038\000\038\000\038\000\039\000\039\000\039\000\039\000\039\000\
\039\000\039\000\039\000\039\000\039\000\255\255\255\255\047\000\
\255\255\049\000\255\255\255\255\049\000\053\000\053\000\053\000\
\053\000\053\000\053\000\053\000\053\000\053\000\053\000\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\049\000\
\255\255\049\000\255\255\255\255\255\255\255\255\049\000\255\255\
\000\000\040\000\255\255\255\255\255\255\020\000\045\000\049\000\
\049\000\049\000\049\000\049\000\049\000\049\000\049\000\049\000\
\049\000\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\047\000\058\000\058\000\058\000\058\000\058\000\058\000\
\058\000\058\000\058\000\058\000\255\255\255\255\255\255\255\255\
\255\255\052\000\052\000\052\000\052\000\052\000\052\000\052\000\
\052\000\052\000\052\000\049\000\255\255\255\255\255\255\255\255\
\255\255\049\000\052\000\052\000\052\000\052\000\052\000\052\000\
\255\255\255\255\255\255\255\255\255\255\049\000\255\255\255\255\
\255\255\049\000\255\255\049\000\255\255\255\255\255\255\049\000\
\060\000\060\000\060\000\060\000\060\000\060\000\060\000\060\000\
\060\000\060\000\052\000\052\000\052\000\052\000\052\000\052\000\
\255\255\060\000\060\000\060\000\060\000\060\000\060\000\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\060\000\060\000\060\000\060\000\060\000\060\000\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\047\000\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\049\000\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\
\255\255";
Lexing.lex_base_code =
"";
Lexing.lex_backtrk_code =
"";
Lexing.lex_default_code =
"";
Lexing.lex_trans_code =
"";
Lexing.lex_check_code =
"";
Lexing.lex_code =
"";
}
let rec lex_json buf lexbuf =
__ocaml_lex_lex_json_rec buf lexbuf 0
and __ocaml_lex_lex_json_rec buf lexbuf __ocaml_lex_state =
match Lexing.engine __ocaml_lex_tables __ocaml_lex_state lexbuf with
| 0 ->
# 142 "ext/ext_json_parse.mll"
( lex_json buf lexbuf)
# 314 "ext/ext_json_parse.ml"
| 1 ->
# 143 "ext/ext_json_parse.mll"
(
update_loc lexbuf 0;
lex_json buf lexbuf
)
# 322 "ext/ext_json_parse.ml"
| 2 ->
# 147 "ext/ext_json_parse.mll"
( comment buf lexbuf)
# 327 "ext/ext_json_parse.ml"
| 3 ->
# 148 "ext/ext_json_parse.mll"
( True)
# 332 "ext/ext_json_parse.ml"
| 4 ->
# 149 "ext/ext_json_parse.mll"
(False)
# 337 "ext/ext_json_parse.ml"
| 5 ->
# 150 "ext/ext_json_parse.mll"
(Null)
# 342 "ext/ext_json_parse.ml"
| 6 ->
# 151 "ext/ext_json_parse.mll"
(Lbracket)
# 347 "ext/ext_json_parse.ml"
| 7 ->
# 152 "ext/ext_json_parse.mll"
(Rbracket)
# 352 "ext/ext_json_parse.ml"
| 8 ->
# 153 "ext/ext_json_parse.mll"
(Lbrace)
# 357 "ext/ext_json_parse.ml"
| 9 ->
# 154 "ext/ext_json_parse.mll"
(Rbrace)
# 362 "ext/ext_json_parse.ml"
| 10 ->
# 155 "ext/ext_json_parse.mll"
(Comma)
# 367 "ext/ext_json_parse.ml"
| 11 ->
# 156 "ext/ext_json_parse.mll"
(Colon)
# 372 "ext/ext_json_parse.ml"
| 12 ->
# 157 "ext/ext_json_parse.mll"
(lex_json buf lexbuf)
# 377 "ext/ext_json_parse.ml"
| 13 ->
# 159 "ext/ext_json_parse.mll"
( Number (Lexing.lexeme lexbuf))
# 382 "ext/ext_json_parse.ml"
| 14 ->
# 161 "ext/ext_json_parse.mll"
(
let pos = Lexing.lexeme_start_p lexbuf in
scan_string buf pos lexbuf;
let content = (Buffer.contents buf) in
Buffer.clear buf ;
String content
)
# 393 "ext/ext_json_parse.ml"
| 15 ->
# 168 "ext/ext_json_parse.mll"
(Eof )
# 398 "ext/ext_json_parse.ml"
| 16 ->
let
# 169 "ext/ext_json_parse.mll"
c
# 404 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf lexbuf.Lexing.lex_start_pos in
# 169 "ext/ext_json_parse.mll"
( error lexbuf (Illegal_character c ))
# 408 "ext/ext_json_parse.ml"
| __ocaml_lex_state -> lexbuf.Lexing.refill_buff lexbuf;
__ocaml_lex_lex_json_rec buf lexbuf __ocaml_lex_state
and comment buf lexbuf =
__ocaml_lex_comment_rec buf lexbuf 40
and __ocaml_lex_comment_rec buf lexbuf __ocaml_lex_state =
match Lexing.engine __ocaml_lex_tables __ocaml_lex_state lexbuf with
| 0 ->
# 171 "ext/ext_json_parse.mll"
(lex_json buf lexbuf)
# 420 "ext/ext_json_parse.ml"
| 1 ->
# 172 "ext/ext_json_parse.mll"
(comment buf lexbuf)
# 425 "ext/ext_json_parse.ml"
| 2 ->
# 173 "ext/ext_json_parse.mll"
(error lexbuf Unterminated_comment)
# 430 "ext/ext_json_parse.ml"
| __ocaml_lex_state -> lexbuf.Lexing.refill_buff lexbuf;
__ocaml_lex_comment_rec buf lexbuf __ocaml_lex_state
and scan_string buf start lexbuf =
__ocaml_lex_scan_string_rec buf start lexbuf 45
and __ocaml_lex_scan_string_rec buf start lexbuf __ocaml_lex_state =
match Lexing.engine __ocaml_lex_tables __ocaml_lex_state lexbuf with
| 0 ->
# 177 "ext/ext_json_parse.mll"
( () )
# 442 "ext/ext_json_parse.ml"
| 1 ->
# 179 "ext/ext_json_parse.mll"
(
let len = lexeme_len lexbuf - 2 in
update_loc lexbuf len;
scan_string buf start lexbuf
)
# 452 "ext/ext_json_parse.ml"
| 2 ->
# 186 "ext/ext_json_parse.mll"
(
let len = lexeme_len lexbuf - 3 in
update_loc lexbuf len;
scan_string buf start lexbuf
)
# 461 "ext/ext_json_parse.ml"
| 3 ->
let
# 191 "ext/ext_json_parse.mll"
c
# 467 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 1) in
# 192 "ext/ext_json_parse.mll"
(
Buffer.add_char buf (char_for_backslash c);
scan_string buf start lexbuf
)
# 474 "ext/ext_json_parse.ml"
| 4 ->
let
# 196 "ext/ext_json_parse.mll"
c1
# 480 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 1)
and
# 196 "ext/ext_json_parse.mll"
c2
# 485 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 2)
and
# 196 "ext/ext_json_parse.mll"
c3
# 490 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 3)
and
# 196 "ext/ext_json_parse.mll"
s
# 495 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme lexbuf lexbuf.Lexing.lex_start_pos (lexbuf.Lexing.lex_start_pos + 4) in
# 197 "ext/ext_json_parse.mll"
(
let v = dec_code c1 c2 c3 in
if v > 255 then
error lexbuf (Illegal_escape s) ;
Buffer.add_char buf (Char.chr v);
scan_string buf start lexbuf
)
# 506 "ext/ext_json_parse.ml"
| 5 ->
let
# 205 "ext/ext_json_parse.mll"
c1
# 512 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 2)
and
# 205 "ext/ext_json_parse.mll"
c2
# 517 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 3) in
# 206 "ext/ext_json_parse.mll"
(
let v = hex_code c1 c2 in
Buffer.add_char buf (Char.chr v);
scan_string buf start lexbuf
)
# 526 "ext/ext_json_parse.ml"
| 6 ->
let
# 212 "ext/ext_json_parse.mll"
c
# 532 "ext/ext_json_parse.ml"
= Lexing.sub_lexeme_char lexbuf (lexbuf.Lexing.lex_start_pos + 1) in
# 213 "ext/ext_json_parse.mll"
(
Buffer.add_char buf '\\';
Buffer.add_char buf c;
scan_string buf start lexbuf
)
# 541 "ext/ext_json_parse.ml"
| 7 ->
# 220 "ext/ext_json_parse.mll"
(
update_loc lexbuf 0;
Buffer.add_char buf lf;
scan_string buf start lexbuf
)
# 551 "ext/ext_json_parse.ml"
| 8 ->
# 227 "ext/ext_json_parse.mll"
(
let ofs = lexbuf.lex_start_pos in
let len = lexbuf.lex_curr_pos - ofs in
Buffer.add_subbytes buf lexbuf.lex_buffer ofs len;
scan_string buf start lexbuf
)
# 562 "ext/ext_json_parse.ml"
| 9 ->
# 235 "ext/ext_json_parse.mll"
(
error lexbuf Unterminated_string
)
# 569 "ext/ext_json_parse.ml"
| __ocaml_lex_state -> lexbuf.Lexing.refill_buff lexbuf;
__ocaml_lex_scan_string_rec buf start lexbuf __ocaml_lex_state
;;
# 239 "ext/ext_json_parse.mll"
let parse_json lexbuf =
let buf = Buffer.create 64 in
let look_ahead = ref None in
let token () : token =
match !look_ahead with
| None ->
lex_json buf lexbuf
| Some x ->
look_ahead := None ;
x
in
let push e = look_ahead := Some e in
let rec json (lexbuf : Lexing.lexbuf) : Ext_json_types.t =
match token () with
| True -> True lexbuf.lex_start_p
| False -> False lexbuf.lex_start_p
| Null -> Null lexbuf.lex_start_p
| Number s -> Flo {flo = s; loc = lexbuf.lex_start_p}
| String s -> Str { str = s; loc = lexbuf.lex_start_p}
| Lbracket -> parse_array lexbuf.lex_start_p lexbuf.lex_curr_p [] lexbuf
| Lbrace -> parse_map lexbuf.lex_start_p Map_string.empty lexbuf
| _ -> error lexbuf Unexpected_token
(** Note if we remove [trailing_comma] support
we should report errors (actually more work), for example
{[
match token () with
| Rbracket ->
if trailing_comma then
error lexbuf Trailing_comma_in_array
else
]}
{[
match token () with
| Rbrace ->
if trailing_comma then
error lexbuf Trailing_comma_in_obj
else
]}
*)
and parse_array loc_start loc_finish acc lexbuf
: Ext_json_types.t =
match token () with
| Rbracket ->
Arr {loc_start ; content = Ext_array.reverse_of_list acc ;
loc_end = lexbuf.lex_curr_p }
| x ->
push x ;
let new_one = json lexbuf in
begin match token () with
| Comma ->
parse_array loc_start loc_finish (new_one :: acc) lexbuf
| Rbracket
-> Arr {content = (Ext_array.reverse_of_list (new_one::acc));
loc_start ;
loc_end = lexbuf.lex_curr_p }
| _ ->
error lexbuf Expect_comma_or_rbracket
end
and parse_map loc_start acc lexbuf : Ext_json_types.t =
match token () with
| Rbrace ->
Obj { map = acc ; loc = loc_start}
| String key ->
begin match token () with
| Colon ->
let value = json lexbuf in
begin match token () with
| Rbrace -> Obj {map = Map_string.add acc key value ; loc = loc_start}
| Comma ->
parse_map loc_start (Map_string.add acc key value ) lexbuf
| _ -> error lexbuf Expect_comma_or_rbrace
end
| _ -> error lexbuf Expect_colon
end
| _ -> error lexbuf Expect_string_or_rbrace
in
let v = json lexbuf in
match token () with
| Eof -> v
| _ -> error lexbuf Expect_eof
let parse_json_from_string s =
parse_json (Lexing.from_string s )
let parse_json_from_chan fname in_chan =
let lexbuf =
Ext_position.lexbuf_from_channel_with_fname
in_chan fname in
parse_json lexbuf
let parse_json_from_file s =
let in_chan = open_in s in
let lexbuf =
Ext_position.lexbuf_from_channel_with_fname
in_chan s in
match parse_json lexbuf with
| exception e -> close_in in_chan ; raise e
| v -> close_in in_chan; v
# 688 "ext/ext_json_parse.ml"