-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathag_oj_emit.ml
1321 lines (1210 loc) · 33.8 KB
/
ag_oj_emit.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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
OCaml code generator for the json format.
*)
open Printf
open Atd_ast
open Ag_error
open Ag_mapping
open Ag_oj_mapping
(*
OCaml code generator (json readers and writers)
*)
let name_of_var s = "_" ^ s
type param = {
deref : (Ag_ocaml.atd_ocaml_repr, Ag_json.json_repr) Ag_mapping.mapping ->
(Ag_ocaml.atd_ocaml_repr, Ag_json.json_repr) Ag_mapping.mapping;
std : bool;
unknown_field_handler : string option;
(* Optional handler that takes a field name as argument
and does something with it such as displaying a warning message. *)
force_defaults : bool;
}
let make_ocaml_json_intf ~with_create buf deref defs =
List.iter (
fun x ->
let s = x.def_name in
if s <> "" && s.[0] <> '_' && x.def_value <> None then (
let full_name = Ag_ocaml.get_full_type_name x in
let writer_params =
String.concat "" (
List.map
(fun s -> sprintf "\n (Bi_outbuf.t -> '%s -> unit) ->" s)
x.def_param
)
in
let reader_params =
String.concat "" (
List.map
(fun s ->
sprintf "\n (Yojson.Safe.lexer_state -> \
Lexing.lexbuf -> '%s) ->" s)
x.def_param
)
in
bprintf buf "\
val write_%s :%s
Bi_outbuf.t -> %s -> unit
(** Output a JSON value of type {!%s}. *)
"
s writer_params
full_name
s;
bprintf buf "\
val string_of_%s :%s
?len:int -> %s -> string
(** Serialize a value of type {!%s}
into a JSON string.
@param len specifies the initial length
of the buffer used internally.
Default: 1024. *)
"
s writer_params
full_name
s;
bprintf buf "\
val read_%s :%s
Yojson.Safe.lexer_state -> Lexing.lexbuf -> %s
(** Input JSON data of type {!%s}. *)
"
s reader_params
full_name
s;
bprintf buf "\
val %s_of_string :%s
string -> %s
(** Deserialize JSON data of type {!%s}. *)
"
s reader_params
full_name
s;
if with_create then
let create_record_intf, create_record_impl =
Ag_ocaml.make_record_creator deref x
in
bprintf buf "%s" create_record_intf;
bprintf buf "\n";
)
)
(flatten defs)
let get_assoc_type deref loc x =
match deref x with
`Tuple (loc2, [| k; v |], `Tuple, `Tuple) ->
(match deref k.cel_value with
`String _ -> ()
| _ ->
error loc "Due to <json repr=\"object\"> keys must be strings");
v.cel_value
| _ ->
error loc "Expected due to <json repr=\"object\">: (string * _) list"
let nth name i len =
let l =
Array.to_list (Array.init len (fun j -> if i = j then name else "_")) in
String.concat ", " l
let get_fields p a =
List.map (
fun x ->
let ocaml_fname, ocaml_default, json_fname, optional, unwrapped =
match x.f_arepr, x.f_brepr with
`Field o, `Field j ->
let ocaml_default =
match x.f_kind with
`With_default ->
(match o.Ag_ocaml.ocaml_default with
None ->
let d =
Ag_ocaml.get_implicit_ocaml_default
p.deref x.f_value in
if d = None then
error x.f_loc "Missing default field value"
else
d
| Some _ as default -> default
)
| `Optional -> Some "None"
| `Required -> None
in
let optional =
match x.f_kind with
`Optional | `With_default -> true
| `Required -> false
in
o.Ag_ocaml.ocaml_fname,
ocaml_default,
j.Ag_json.json_fname,
optional,
j.Ag_json.json_unwrapped
| _ -> assert false
in
(x, ocaml_fname, ocaml_default, json_fname, optional, unwrapped)
)
(Array.to_list a)
let insert sep l =
let rec ins sep = function
[] -> []
| x :: l -> sep :: x :: ins sep l
in
match l with
[] -> []
| x :: l -> x :: ins sep l
let make_json_string s = Yojson.Safe.to_string (`String s)
let unopt = function None -> assert false | Some x -> x
(*
('a, 'b) t -> write_t write__a write__b
('a, foo) t -> write_t write__a write_foo
('a, (foo, 'b) bar) t -> write_t write__a (write_bar write_foo write__b)
*)
let rec get_writer_name
?(paren = false)
?(name_f = fun s -> "write_" ^ s)
p (x : oj_mapping) : string =
match x with
`Unit (loc, `Unit, `Unit) ->
"Yojson.Safe.write_null"
| `Bool (loc, `Bool, `Bool) ->
"Yojson.Safe.write_bool"
| `Int (loc, `Int o, `Int) ->
(match o with
`Int -> "Yojson.Safe.write_int"
| `Char -> "Ag_oj_run.write_int8"
| `Int32 -> "Ag_oj_run.write_int32"
| `Int64 -> "Ag_oj_run.write_int64"
)
| `Float (loc, `Float, `Float) ->
if p.std then "Yojson.Safe.write_std_float"
else "Yojson.Safe.write_float"
| `String (loc, `String, `String) ->
"Yojson.Safe.write_string"
| `Tvar (loc, s) -> "write_" ^ name_of_var s
| `Name (loc, s, args, None, None) ->
let l = List.map (get_writer_name ~paren:true p) args in
let s = String.concat " " (name_f s :: l) in
if paren && l <> [] then "(" ^ s ^ ")"
else s
| `External (loc, s, args,
`External (types_module, main_module, ext_name),
`External) ->
let f = main_module ^ "." ^ name_f ext_name in
let l = List.map (get_writer_name ~paren:true p) args in
let s = String.concat " " (f :: l) in
if paren && l <> [] then "(" ^ s ^ ")"
else s
| _ -> assert false
let get_left_writer_name p name param =
let args = List.map (fun s -> `Tvar (dummy_loc, s)) param in
get_writer_name p (`Name (dummy_loc, name, args, None, None))
let get_left_to_string_name p name param =
let name_f s = "string_of_" ^ s in
let args = List.map (fun s -> `Tvar (dummy_loc, s)) param in
get_writer_name ~name_f p (`Name (dummy_loc, name, args, None, None))
let rec get_reader_name
?(paren = false)
?(name_f = fun s -> "read_" ^ s)
p (x : oj_mapping) : string =
match x with
`Unit (loc, `Unit, `Unit) -> "Ag_oj_run.read_null"
| `Bool (loc, `Bool, `Bool) -> "Ag_oj_run.read_bool"
| `Int (loc, `Int o, `Int) ->
(match o with
`Int -> "Ag_oj_run.read_int"
| `Char -> "Ag_oj_run.read_int8"
| `Int32 -> "Ag_oj_run.read_int32"
| `Int64 -> "Ag_oj_run.read_int64"
)
| `Float (loc, `Float, `Float) -> "Ag_oj_run.read_number"
| `String (loc, `String, `String) -> "Ag_oj_run.read_string"
| `Tvar (loc, s) -> "read_" ^ name_of_var s
| `Name (loc, s, args, None, None) ->
let l = List.map (get_reader_name ~paren:true p) args in
let s = String.concat " " (name_f s :: l) in
if paren && l <> [] then "(" ^ s ^ ")"
else s
| `External (loc, s, args,
`External (types_module, main_module, ext_name),
`External) ->
let f = main_module ^ "." ^ name_f ext_name in
let l = List.map (get_reader_name ~paren:true p) args in
let s = String.concat " " (f :: l) in
if paren && l <> [] then "(" ^ s ^ ")"
else s
| _ -> assert false
let get_left_reader_name p name param =
let args = List.map (fun s -> `Tvar (dummy_loc, s)) param in
get_reader_name p (`Name (dummy_loc, name, args, None, None))
let get_left_of_string_name p name param =
let name_f s = s ^ "_of_string" in
let args = List.map (fun s -> `Tvar (dummy_loc, s)) param in
get_reader_name ~name_f p (`Name (dummy_loc, name, args, None, None))
let rec make_writer p (x : oj_mapping) : Ag_indent.t list =
match x with
`Unit _
| `Bool _
| `Int _
| `Float _
| `String _
| `Name _
| `External _
| `Tvar _ -> [ `Line (get_writer_name p x) ]
| `Sum (loc, a, `Sum x, `Sum) ->
let tick =
match x with
`Classic -> ""
| `Poly -> "`"
in
let body : Ag_indent.t list =
[
`Line "match x with";
`Block (
Array.to_list (
Array.map
(fun x -> `Inline (make_variant_writer p tick x))
a
)
)
]
in
[
`Annot ("fun", `Line "fun ob x ->");
`Block body
]
| `Record (loc, a, `Record o, `Record) ->
[
`Annot ("fun", `Line "fun ob x ->");
`Block (make_record_writer p a o);
]
| `Tuple (loc, a, `Tuple, `Tuple) ->
let len = Array.length a in
let a =
Array.mapi (
fun i x ->
`Inline [
`Line (sprintf "(let %s = x in" (nth "x" i len));
`Line "(";
`Block (make_writer p x.cel_value);
`Line ") ob x";
`Line ");"
]
) a
in
let l =
insert (`Line "Bi_outbuf.add_char ob ',';") (Array.to_list a)
in
let op, cl =
if p.std then '[', ']'
else '(', ')'
in
[
`Annot ("fun", `Line "fun ob x ->");
`Block [
`Line (sprintf "Bi_outbuf.add_char ob %C;" op);
`Inline l;
`Line (sprintf "Bi_outbuf.add_char ob %C;" cl);
]
]
| `List (loc, x, `List o, `List j) ->
(match j with
`Array ->
let write =
match o with
`List -> "Ag_oj_run.write_list ("
| `Array -> "Ag_oj_run.write_array ("
in
[
`Line write;
`Block (make_writer p x);
`Line ")";
]
| `Object ->
let x = get_assoc_type p.deref loc x in
let write =
match o with
`List -> "Ag_oj_run.write_assoc_list ("
| `Array -> "Ag_oj_run.write_assoc_array ("
in
[
`Line write;
`Block (make_writer p x);
`Line ")";
]
)
| `Option (loc, x, `Option, `Option) ->
[
`Line (sprintf "Ag_oj_run.write_%soption ("
(if p.std then "std_" else ""));
`Block (make_writer p x);
`Line ")";
]
| `Shared (loc, _, _, _, _) ->
error loc "Sharing is not supported by the JSON interface"
| _ -> assert false
and make_variant_writer p tick x :
Ag_indent.t list =
let o, j =
match x.var_arepr, x.var_brepr with
`Variant o, `Variant j -> o, j
| _ -> assert false
in
let ocaml_cons = o.Ag_ocaml.ocaml_cons in
let json_cons = j.Ag_json.json_cons in
match x.var_arg with
None ->
let enclose s =
if p.std then s
else "<" ^ s ^ ">"
in
[
`Line (sprintf "| %s%s -> Bi_outbuf.add_string ob %S"
tick ocaml_cons
(enclose (make_json_string json_cons)))
]
| Some v ->
let op, sep, cl =
if p.std then "[", ",", ']'
else "<", ":", '>'
in
[
`Line (sprintf "| %s%s x ->" tick ocaml_cons);
`Block [
`Line (sprintf "Bi_outbuf.add_string ob %S;"
(op ^ make_json_string json_cons ^ sep));
`Line "(";
`Block (make_writer p v);
`Line ") ob x;";
`Line (sprintf "Bi_outbuf.add_char ob %C" cl);
]
]
and make_record_writer p a record_kind =
let dot =
match record_kind with
`Record -> "."
| `Object -> "#"
in
let fields = get_fields p a in
let sep =
[
`Line "if !is_first then";
`Block [
`Line "is_first := false"
];
`Line "else";
`Block [
`Line "Bi_outbuf.add_char ob ',';";
];
]
in
let write_fields =
List.map (
fun (x, ocaml_fname, ocaml_default, json_fname, optional, unwrapped) ->
let f_value =
if unwrapped then Ag_ocaml.unwrap_option p.deref x.f_value
else x.f_value
in
let write_field_tag =
sprintf "Bi_outbuf.add_string ob %S;"
(make_json_string json_fname ^ ":")
in
let app v =
[
`Inline sep;
`Line write_field_tag;
`Line "(";
`Block (make_writer p f_value);
`Line ")";
`Block [`Line (sprintf "ob %s;" v)]
]
in
let v =
if optional then
sprintf "x.%s" ocaml_fname
else
sprintf "x%s%s" dot ocaml_fname
in
let l =
if unwrapped then
[
`Line (sprintf "(match %s with None -> () | Some x ->" v);
`Block (app "x");
`Line ");"
]
else if optional && not p.force_defaults then
[
`Line (sprintf "if %s != %s then (" v (unopt ocaml_default));
`Block (app v);
`Line ");"
]
else
app v
in
`Inline l
) fields
in
[
`Line "Bi_outbuf.add_char ob '{';";
`Line "let is_first = ref true in";
`Inline write_fields;
`Line "Bi_outbuf.add_char ob '}';";
]
let study_record deref fields =
let maybe_constant =
List.for_all (function (_, _, Some _, _, _, _) -> true | _ -> false) fields
in
let _, init_fields =
List.fold_right (
fun (x, oname, default, jname, opt, unwrap) (maybe_constant, l) ->
let maybe_constant, v =
match default with
None ->
assert (not opt);
(*
The initial value is a float because the record may be
represented as a double_array (unboxed floats).
Float values work in all cases.
*)
let v = "Obj.magic 0.0" in
maybe_constant, v
| Some s ->
false, (if maybe_constant then sprintf "(fun x -> x) (%s)" s
else s)
in
(maybe_constant, `Line (sprintf "%s = %s;" oname v) :: l)
) fields (maybe_constant, [])
in
let n, mapping =
List.fold_left (
fun (i, acc) (x, oname, default, jname, opt, unwrap) ->
if not opt then
(i+1, (Some i :: acc))
else
(i, (None :: acc))
) (0, []) fields
in
let mapping = Array.of_list (List.rev mapping) in
let init_val = [ `Line "{"; `Block init_fields; `Line "}" ] in
let k = n / 31 + (if n mod 31 > 0 then 1 else 0) in
let init_bits =
Array.to_list (
Array.init k (
fun i -> `Line (sprintf "let bits%i = ref 0 in" i)
)
)
in
let final_bits = Array.make k 0 in
for z0 = 0 to List.length fields - 1 do
match mapping.(z0) with
None -> ()
| Some z ->
let i = z / 31 in
let j = z mod 31 in
final_bits.(i) <- final_bits.(i) lor (1 lsl j);
done;
let set_bit z0 =
match mapping.(z0) with
None -> []
| Some z ->
let i = z / 31 in
let j = z mod 31 in
[ `Line (sprintf "bits%i := !bits%i lor 0x%x;" i i (1 lsl j)) ]
in
let check_bits =
let bool_expr =
String.concat " || " (
Array.to_list (
Array.mapi (
fun i x -> sprintf "!bits%i <> 0x%x" i x
) final_bits
)
)
in
let bit_fields =
let a = Array.init k (fun i -> sprintf "!bits%i" i) in
sprintf "[| %s |]" (String.concat "; " (Array.to_list a))
in
let field_names =
let l =
List.fold_right (
fun (x, oname, default, jname, opt, unwrap) acc ->
if default = None && not opt then
sprintf "%S" x.f_name :: acc
else
acc
) fields []
in
sprintf "[| %s |]" (String.concat "; " l)
in
if k = 0 then []
else
[ `Line (sprintf "if %s then Ag_oj_run.missing_fields %s %s;"
bool_expr bit_fields field_names) ]
in
init_val, init_bits, set_bit, check_bits
let rec make_reader p (x : oj_mapping) : Ag_indent.t list =
match x with
`Unit _
| `Bool _
| `Int _
| `Float _
| `String _
| `Name _
| `External _
| `Tvar _ -> [ `Line (get_reader_name p x) ]
| `Sum (loc, a, `Sum x, `Sum) ->
let tick =
match x with
`Classic -> ""
| `Poly -> "`"
in
let cases =
Array.to_list (
Array.map
(make_variant_reader p tick false)
a
)
in
let l0, l1 =
List.partition (fun x -> x.var_arg = None) (Array.to_list a)
in
let cases0 = List.map (make_variant_reader p tick true) l0 in
let cases1 = List.map (make_variant_reader p tick true) l1 in
let error_expr1 =
[ `Line "Ag_oj_run.invalid_variant_tag (String.sub s pos len)" ]
in
let int_mapping_function, int_matching =
Ag_string_match.make_ocaml_int_mapping
~error_expr1
cases
in
let std_int_mapping_function0, std_int_matching0 =
Ag_string_match.make_ocaml_int_mapping
~error_expr1
cases0
in
let std_int_mapping_function1, std_int_matching1 =
Ag_string_match.make_ocaml_int_mapping
~error_expr1
cases1
in
let read_tag =
[
`Line "Yojson.Safe.read_space p lb;";
`Line "match Yojson.Safe.start_any_variant p lb with";
`Block [
`Line "| `Edgy_bracket -> (";
`Block [
`Block [
`Line "Yojson.Safe.read_space p lb;";
`Line "let f =";
`Block int_mapping_function;
`Line "in";
`Line "let i = Yojson.Safe.map_ident p f lb in";
`Inline int_matching;
];
`Line ")";
];
`Line "| `Double_quote -> (";
`Block [
`Block [
`Line "let f =";
`Block std_int_mapping_function0;
`Line "in";
`Line "let i = Yojson.Safe.map_string p f lb in";
`Inline std_int_matching0;
];
`Line ")";
];
`Line "| `Square_bracket -> (";
`Block [
`Block [
`Line "Yojson.Safe.read_space p lb;";
`Line "let f =";
`Block std_int_mapping_function1;
`Line "in";
`Line "let i = Yojson.Safe.map_ident p f lb in";
`Inline std_int_matching1;
];
`Line ")";
];
];
]
in
[
`Annot ("fun", `Line "fun p lb ->");
`Block [
`Inline read_tag;
]
]
| `Record (loc, a, `Record o, `Record) ->
(match o with
`Record -> ()
| `Object ->
error loc "Sorry, OCaml objects are not supported"
);
[
`Annot ("fun", `Line "fun p lb ->");
`Block (make_record_reader p loc a o)
]
| `Tuple (loc, a, `Tuple, `Tuple) ->
[
`Annot ("fun", `Line "fun p lb ->");
`Block (make_tuple_reader p a);
]
| `List (loc, x, `List o, `List j) ->
(match j with
`Array ->
let read =
match o with
`List -> "Ag_oj_run.read_list ("
| `Array -> "Ag_oj_run.read_array ("
in
[
`Line read;
`Block (make_reader p x);
`Line ")";
]
| `Object ->
let x = get_assoc_type p.deref loc x in
let read =
match o with
`List -> "Ag_oj_run.read_assoc_list ("
| `Array -> "Ag_oj_run.read_assoc_array ("
in
[
`Line read;
`Block (make_reader p x);
`Line ")";
]
)
| `Option (loc, x, `Option, `Option) ->
let a = [|
{
var_loc = loc;
var_cons = "None";
var_arg = None;
var_arepr = `Variant { Ag_ocaml.ocaml_cons = "None";
ocaml_vdoc = None };
var_brepr = `Variant { Ag_json.json_cons = "None" };
};
{
var_loc = loc;
var_cons = "Some";
var_arg = Some x;
var_arepr = `Variant { Ag_ocaml.ocaml_cons = "Some";
ocaml_vdoc = None };
var_brepr = `Variant { Ag_json.json_cons = "Some" };
};
|]
in
make_reader p (`Sum (loc, a, `Sum `Classic, `Sum))
| `Shared (loc, _, _, _, _) ->
error loc "Sharing is not supported by the JSON interface"
| _ -> assert false
and make_variant_reader p tick std x : (string * Ag_indent.t list) =
let o, j =
match x.var_arepr, x.var_brepr with
`Variant o, `Variant j -> o, j
| _ -> assert false
in
let ocaml_cons = o.Ag_ocaml.ocaml_cons in
let json_cons = j.Ag_json.json_cons in
let expr =
match x.var_arg with
None ->
if std then
[
`Line (sprintf "%s%s" tick ocaml_cons);
]
else
[
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_gt p lb;";
`Line (sprintf "%s%s" tick ocaml_cons);
]
| Some v ->
if std then
[
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_comma p lb;";
`Line "Yojson.Safe.read_space p lb;";
`Line "let x = (";
`Block [
`Block (make_reader p v);
`Line ") p lb";
];
`Line "in";
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_rbr p lb;";
`Line (sprintf "%s%s x" tick ocaml_cons);
]
else
[
`Line "Ag_oj_run.read_until_field_value p lb;";
`Line "let x = (";
`Block [
`Block (make_reader p v);
`Line ") p lb";
];
`Line "in";
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_gt p lb;";
`Line (sprintf "%s%s x" tick ocaml_cons);
]
in
(json_cons, expr)
and make_record_reader p loc a record_kind =
let fields = get_fields p a in
let init_val, init_bits, set_bit, check_bits = study_record p.deref fields in
let read_field =
let a = Array.of_list fields in
let cases =
Array.mapi (
fun i (x, ocaml_fname, ocaml_default, json_fname, opt, unwrapped) ->
let f_value =
if unwrapped then Ag_ocaml.unwrap_option p.deref x.f_value
else x.f_value
in
let wrap l =
if unwrapped then
[
`Line "Some (";
`Block l;
`Line ")"
]
else l
in
let read_value =
[
`Line "(";
`Block (make_reader p f_value);
`Line ") p lb";
]
in
let expr =
[
`Line "let v =";
`Block (wrap read_value);
`Line "in";
`Line (sprintf "Obj.set_field (Obj.repr x) %i (Obj.repr v);" i);
`Inline (set_bit i);
]
in
let opt_expr =
if opt then
[
`Line "if not (Yojson.Safe.read_null_if_possible p lb) then (";
`Block expr;
`Line ")"
]
else
expr
in
(json_fname, opt_expr)
) a
in
let int_mapping_function, int_matching =
let error_expr1 =
match p.unknown_field_handler with
None -> [ `Line "-1" ]
| Some f ->
[ `Line (sprintf "(%s) %S (String.sub s pos len); -1"
f (Atd_ast.string_of_loc loc)) ]
in
Ag_string_match.make_ocaml_int_mapping
~exit_with: `Expr
~error_expr1
~error_expr2: [ `Line "Yojson.Safe.skip_json p lb" ]
(Array.to_list cases)
in
[
`Line "Yojson.Safe.read_space p lb;";
`Line "let f =";
`Block int_mapping_function;
`Line "in";
`Line "let i = Yojson.Safe.map_ident p f lb in";
`Line "Ag_oj_run.read_until_field_value p lb;";
`Line "(";
`Block int_matching;
`Line ");";
]
in
[
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_lcurl p lb;";
`Line "let x =";
`Block init_val;
`Line "in";
`Inline init_bits;
`Line "try";
`Block [
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_object_end lb;";
`Inline read_field;
`Line "while true do";
`Block [
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_object_sep p lb;";
`Inline read_field;
];
`Line "done;";
`Line "assert false;";
];
`Line "with Yojson.End_of_object -> (";
`Block [
`Block [
`Inline check_bits;
`Line "Ag_oj_run.identity x";
];
`Line ")";
];
]
and make_tuple_reader deref a =
let cells =
Array.map (
fun x ->
match x.cel_arepr with
`Cell f -> x, f.Ag_ocaml.ocaml_default
| _ -> assert false
) a
in
let min_length =
let n = ref (Array.length cells) in
(try
for i = Array.length cells - 1 downto 0 do
let x, default = cells.(i) in
if default = None then (
n := i + 1;
raise Exit
)
done
with Exit -> ());
!n
in
let read_cells =
List.flatten (
Array.to_list (
Array.mapi (
fun i (x, default) ->
let read_value =
[
`Line "(";
`Block (make_reader deref x.cel_value);
`Line ") p lb";
]
in
let get_value =
if i < min_length - 1 then
[
`Line "let x =";
`Block read_value;
`Line "in";
`Line "incr len;";
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_tuple_sep2 p std_tuple lb;";
`Line "x"
]
else if i = min_length - 1 then
[
`Line "let x =";
`Block read_value;
`Line "in";
`Line "incr len;";
`Line "(try";
`Block [
`Line "Yojson.Safe.read_space p lb;";
`Line "Yojson.Safe.read_tuple_sep2 p std_tuple lb;";