File: pa_json_static.ml.309

package info (click to toggle)
json-static 0.9.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 264 kB
  • ctags: 32
  • sloc: makefile: 82; ml: 65
file content (938 lines) | stat: -rw-r--r-- 27,181 bytes parent folder | download | duplicates (2)
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
(*
  Conversion between OCaml types and JSON types as provided by the json-wheel
  library. 
  
  Author: Martin Jambon <martin_jambon@emailuser.net>

Copyright (c) 2007 Burnham Institute for Medical Research
Copyright (c) 2007 Wink Technologies Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)


open Printf


let light_mode = ref false

let reserved_prefix = "__json_static_"

let new_id =
  let n = ref 0 in
  fun name -> 
    incr n; 
    reserved_prefix ^ name ^ string_of_int !n


let check_unique f l =
  let tbl = Hashtbl.create 50 in
  List.iter
    (fun x -> 
       let (_loc, id) = f x in
       if Hashtbl.mem tbl id then
	 Stdpp.raise_with_loc _loc
	   (Failure "this tag or label is not unique")
       else Hashtbl.add tbl id ())
    l

let unopt default = function
    None -> default
  | Some x -> x

let rec optmap f = function
    [] -> []
  | hd :: tl ->
      match f hd with
	  None -> optmap f tl
	| Some x -> x :: optmap f tl
    

type field = { field_caml_name : string;
	       field_json_name : string;
	       field_type : t;
	       field_caml_loc : Token.flocation;
	       field_json_loc : Token.flocation;
	       optional : bool;
	       default : MLast.expr option;
	       subset : subset option;
	       validator_name : string option;
	       is_mutable : bool }

and subset =
    [ `List of MLast.expr 
    | `Dynlist of MLast.expr
    | `Abstract_set of MLast.expr ]

and constructor = { cons_caml_name : string;
		    cons_json_name : string;
		    cons_args : t list;
		    cons_caml_loc : Token.flocation;
		    cons_json_loc : Token.flocation }

and type_expr =
    List of t
  | Array of t
  | Option of t
  | Object of field list
  | Record of field list
  | Hashtbl of t
  | Assoc of t
  | Tuple of t list
  | Variant of constructor list
  | Poly of constructor list
  | Name of string
  | String
  | Bool
  | Int
  | Float
  | Number
  | Raw
  | Custom of string

and t = Token.flocation * type_expr

and typedef = { def : t;
		is_predefined : bool;
		is_private : bool (* unused at the moment *) }

module StringMap = Map.Make (String)



let error _loc =
  <:str_item< 
    value __json_static_error obj msg =
      let m = 400 in
      let s = Json_io.string_of_json obj in
      let obj_string =
	if String.length s > m then String.sub s 0 (m - 4) ^ " ..."
	else s in
      Json_type.json_error (msg ^ ":\n" ^ obj_string) 
  >>


let numbered_list l =
  Array.to_list
    (Array.mapi 
       (fun i x -> (x, "x" ^ string_of_int i))
       (Array.of_list l))

let eta_expand = function
    (<:expr< fun [ $list:_$ ] >>) as f -> f
  | e -> let _loc = MLast.loc_of_expr e in (<:expr< fun x -> $e$ x >>)

let make_ofjson_defs _loc l =
  let browse _loc f = <:expr< Json_type.Browse.$lid:f$ >> in

  let rec convert (_loc, def) =
    match def with
	List x -> <:expr< $browse _loc "list"$ $convert x$ >>
      | Array x -> 
	  <:expr< fun x -> 
	    Array.of_list (($browse _loc "list"$ $convert x$) x) >>
      | Option x -> 
	  <:expr< $browse _loc "optional"$ $convert x$ >>
      | Object l -> convert_object _loc l
      | Record r -> convert_record _loc r
      | Hashtbl x -> 
	  <:expr< 
	     fun x -> 
	       let l = $browse _loc "objekt"$ x in
	       let tbl = Hashtbl.create (List.length l) in
               do { List.iter (fun (s, x) -> 
				 Hashtbl.add tbl s ($convert x$ x)) l;
		    tbl } >>
      | Assoc x -> 
	  <:expr< fun x ->
	            List.map (fun (key, data) -> (key, $convert x$ data))
	              ($browse _loc "objekt"$ x) >>
      | Tuple l ->
	  let nl = numbered_list l in
	  let pl = 
	    List.fold_right 
	      (fun ((_loc, _), name) tl -> <:patt< [ $lid:name$ :: $tl$ ] >>) 
	      nl <:patt< [] >> in
	  let el = 
	    List.map (fun ((_loc, _) as x, name) ->
			<:expr< $convert x$ $lid:name$ >>)
	      nl in
	  <:expr< fun [ Json_type.Array [ $list:pl$ ] -> ( $list:el$ )
		      | Json_type.Array _ as x ->
			  __json_static_error x
			    "wrong number of elements in JSON array"
		      | x ->
			  __json_static_error x
			    "not a JSON array" ] >>
      | Poly l ->
	  convert_variants (fun _loc name -> <:expr< ` $name$ >>) _loc l
      | Variant l ->
	  convert_variants (fun _loc name -> <:expr< $uid:name$ >>) _loc l
      | Name x -> <:expr< $lid: x ^ "_of_json"$ >>
      | String -> browse _loc "string"
      | Bool -> browse _loc "bool"
      | Int -> browse _loc "int"
      | Float -> browse _loc "float"
      | Number -> browse _loc "number"
      | Raw -> <:expr< fun x -> x >>
      | Custom modul -> <:expr< $uid:modul$ . of_json >>

   and convert_object _loc l =
     let pel = convert_field_list _loc l in
     let ml = 
       List.map 
	 (fun x -> 
	    let name = x.field_caml_name in
	    <:class_str_item< method $name$ = $lid:name$ >>)
	 l in
     let obj = 
       (* <:expr< object $list:ml$ end >> *)
       MLast.ExObj (_loc, None, ml)
     in
     eval_with_tbl _loc <:expr< let $list:pel$ in $obj$ >>

  and convert_record _loc r =
     let pel = convert_field_list _loc r in
     eval_with_tbl _loc <:expr< { $list:pel$ } >>

  and convert_field_list _loc l =
     List.map 
       (fun { field_caml_name = name;
	      field_json_name = json_name;
	      field_type = x;
	      optional = optional;
	      default = default;
	      validator_name = validate_opt } ->
	  let validate e =
	    match validate_opt with
		None -> e
	      | Some f -> <:expr< let x = $e$ in do { $lid:f$ x; x } >>
          in
	  let e1 = 
	    let f = if optional then "fieldx" else "field" in
	    <:expr< 
	  (Json_type.Browse.$lid:f$ tbl $str:json_name$) >> in
	  let e2 =
	    match default with
		Some e -> 
		  (<:expr< 
		   match $e1$ with 
		       [ Json_type.Null -> $e$
		       | x -> $convert x$ x ] >>)
	      | None -> <:expr< $convert x$ $e1$ >> in
	  
	  (<:patt< $lid:name$ >>, validate e2))
       l

  and convert_variants make_cons _loc l =
    let l0, l1 =
      List.partition (fun x -> x.cons_args = []) l in
    let pwel0 =
      List.map
	(fun { cons_caml_name = name;
	       cons_json_name = json_name } ->
	   (<:patt< $str:json_name$ >>, 
	    None,
	    make_cons _loc name))
	l0 in
    let pwel1 =
      List.map
	(fun { cons_caml_name = name;
	       cons_json_name = json_name;
	       cons_args = args } ->
	   let argnames = numbered_list args in
	   let list_patt =
	     List.fold_right 
	       (fun (arg, s) l -> 
		  <:patt< [ $lid:s$ :: $l$ ] >>)
	       argnames <:patt< [] >> in
	   let e =
	     List.fold_left
	       (fun cons (arg, s) -> 
		  <:expr< $cons$ ($convert arg$ $lid:s$) >>)
	     (make_cons _loc name) argnames in
	   (<:patt< ($str:json_name$, $list_patt$) >>, 
	    None, 
	    e))
	l1 in
    let full_pwel pwel =
      pwel @ [ <:patt< _ >>, None, 
	       <:expr< __json_static_error x
                 "invalid variant name or \
                  wrong number of arguments" >> ]
    in
    
    (<:expr< 
     fun
	 [ Json_type.String s as x -> 
	     match s with [ $list:full_pwel pwel0$ ]
	       | Json_type.Array 
		   [ Json_type.String s :: ([ _ :: _ ] as args) ] as x -> 
		   match (s, args) with [ $list:full_pwel pwel1$ ]
	       | x -> __json_static_error x
		   "not able to read this as \
                    a variant" ]
     >>)


  and eval_with_tbl _loc e =
    (<:expr< 
     fun x ->
       let tbl = 
	 Json_type.Browse.make_table (Json_type.Browse.objekt x) in
       $e$ >>)
  in

  let defs = 
    optmap
      (fun ((_loc, name), x) -> 
	 (*if x.is_private then None
	 else*)
	   let fname = name ^ "_of_json" in
	   Some (<:patt< ( $lid:fname$ : Json_type.t -> $lid:name$ ) >>, 
		 eta_expand (convert x.def))) l in
  if defs = [] then 
    <:str_item< declare end >>
  else
   <:str_item< declare value rec $list:defs$; end >>


let make_tojson_val, make_tojson_defs =
  let build _loc s = <:expr< Json_type.Build. $lid:s$ >> in

  let rec convert (_loc, def) =
    match def with
	List x -> <:expr< Json_type.Build.list $convert x$ >>
      | Array x -> 
	  <:expr< fun x -> 
                    Json_type.Build.list $convert x$ (Array.to_list x) >>
      | Option x -> <:expr< Json_type.Build.optional $convert x$ >>
      | Object l ->
	  convert_field_list (fun name -> <:expr< x#$lid:name$ >>) 
	    _loc l
      | Record r -> 
	  convert_field_list (fun name -> <:expr< x.$lid:name$ >>)
	    _loc r
      | Hashtbl x ->
	  <:expr< fun tbl -> 
	    Json_type.Object 
	      (Hashtbl.fold (fun key data tl -> 
			       [ (key, $convert x$ data) :: tl ])
		 tbl []) >>
      | Assoc x ->
	  <:expr< 
	    fun x ->
	      Json_type.Object
	        ((List.map (fun (key, data) -> (key, $convert x$ data))) x) >>
      | Tuple l ->
	  let nl = numbered_list l in
	  let pl = List.map (fun (_, name) -> <:patt< $lid:name$ >>) nl in
	  let a = List.fold_right 
		    (fun (x, name) tl -> 
		       <:expr< [ $convert x$ $lid:name$ :: $tl$ ] >>)
		    nl <:expr< [] >> in
	  <:expr< fun [ ( $list:pl$ ) -> Json_type.Array $a$ ] >>
      | Poly l -> 
	  let pwel =
	    List.map
	      (fun { cons_caml_name = name;
		     cons_json_name = json_name;
		     cons_args = args } ->
		 match args with
		     [] -> 
		       (<:patt< ` $name$ >>, 
			None,
			<:expr< Json_type.String $str:json_name$ >>)
		   | [x] ->
		       (<:patt< ` $name$ arg >>, 
			None, 
			<:expr<
			Json_type.Array 
			  [ Json_type.String $str:json_name$;
			    $convert x$ arg ] >>)
		   | _ -> assert false)
	      l in
	  <:expr< fun [ $list:pwel$ ] >>
      | Variant v -> 
	  let pwel =
	    List.map
	      (fun { cons_caml_name = name;
		     cons_json_name = json_name;
		     cons_args = args } ->
		 match args with
		     [] -> 
		       (<:patt< $uid:name$ >>, 
			None,
			<:expr< Json_type.String $str:json_name$ >>)
		   | l ->
		       let args = numbered_list l in
		       let p =
			 List.fold_left
			   (fun cons (_, s) -> <:patt< $cons$ $lid:s$ >>)
			   <:patt< $uid:name$ >> args in
		       let e =
			 List.fold_right
			   (fun (x, s) l -> 
			      <:expr< [ $convert x$ $lid:s$ :: $l$ ] >>)
			   args <:expr< [] >> in
		       (p, 
			None, 
			<:expr<
			Json_type.Array 
			  [ Json_type.String $str:json_name$ :: $e$ ] >>))
	      v in
	  <:expr< fun [ $list:pwel$ ] >>
      | Name x -> <:expr< $lid: "json_of_" ^ x$ >>
      | String -> build _loc "string"
      | Bool -> build _loc "bool"
      | Int -> build _loc "int"
      | Float -> build _loc "float"
      | Number -> build _loc "float"
      | Raw -> <:expr< fun x -> x >>
      | Custom modul -> <:expr< $uid:modul$ . to_json >>

  and convert_field_list access _loc l =
    let pairs = 
      List.fold_right
	(fun { field_caml_name = name;
	       field_json_name = json_name;
	       field_type = x } tl ->
	   <:expr< [ ( $str:json_name$, $convert x$ $access name$ )
		     :: $tl$ ] >>)
	l <:expr< [] >> in
    <:expr< fun x -> Json_type.Object $pairs$ >>
  in

  let make_defs _loc l = 
    let defs = 
      List.map
	(fun ((_loc, name), x) -> 
	   let fname = "json_of_" ^ name in
	   (<:patt< ( $lid:fname$ : $lid:name$ -> Json_type.t ) >>, 
	    eta_expand (convert x.def)))
	l in
    
    <:str_item< value rec $list:defs$ >> in

  (convert, make_defs)


let make_typedef _loc names l =
  let rec convert (_loc, def) =
    match def with
	List x -> <:ctyp< list $convert x$ >>
      | Array x -> <:ctyp< array $convert x$ >>
      | Option x -> <:ctyp< option $convert x$ >>
      | Object l -> 
	  let ml = List.map (fun x -> (x.field_caml_name,
				       convert x.field_type)) l in
	  <:ctyp< < $list:ml$ > >>
      | Record r -> 
	  let l = 
	    List.map (fun x -> (x.field_caml_loc,
				x.field_caml_name,
				x.is_mutable,
				convert x.field_type)) r in
	  <:ctyp< { $list:l$ } >>
      | Hashtbl x -> <:ctyp< Hashtbl.t string $convert x$ >>
      | Assoc x -> <:ctyp< list (string * $convert x$) >>
      | Tuple l -> 
	  let tl = List.map convert l in
	  <:ctyp< ( $list:tl$ ) >>
      | Poly l -> 
	  let rfl = 
	    List.map (fun c ->
			let name = c.cons_caml_name in
			match c.cons_args with
			    [] -> MLast.RfTag (name, true, [])
			  | [x] -> MLast.RfTag (name, false, [convert x])
			  | _ -> assert false)
	      l in
	  <:ctyp< [ = $list:rfl$ ] >>
      | Variant v -> 
	  let l = 
	    List.map 
	      (fun x ->
		 let cal = List.map convert x.cons_args in
		 (x.cons_caml_loc, x.cons_caml_name, cal))
	      v in
	  <:ctyp< [ $list:l$ ] >>
      | Name x -> 
	  if StringMap.mem x names then <:ctyp< $lid:x$ >>
	  else
	    Stdpp.raise_with_loc _loc 
	      (Failure ("type name " ^ x ^ 
			" is undefined or not defined in the same \
                         'type ... and ...' block"))
      | String -> <:ctyp< string >>
      | Bool -> <:ctyp< bool >>
      | Int -> <:ctyp< int >>
      | Float -> <:ctyp< float >>
      | Number -> <:ctyp< float >>
      | Raw -> <:ctyp< Json_type.t >>
      | Custom s -> <:ctyp< $uid:s$ . t >> in

  let tdl = 
    optmap 
      (fun (name, x) ->
	 if x.is_predefined then None
	 else
	   let ctyp = convert x.def in
	   Some (name, [], ctyp, []))
      l in
  if tdl <> [] then
    <:str_item< type $list:tdl$ >>
  else
    <:str_item< declare end >>


let make_typeval _loc l =
  let bool _loc b =
    if b then <:expr< True >>
    else <:expr< False >> in 
  let run = <:expr< $uid:"Run_json_static"$ >> in

  let additional_defs = ref [] in

  let rec convert (_loc, def) =
    match def with
	List x -> <:expr< $run$.List $convert x$ >>
      | Array x -> <:expr< $run$.Array $convert x$ >>
      | Option x -> <:expr< $run$.Option $convert x$ >>
      | Object l -> <:expr< $run$.Object $make_fields _loc l$ >>
      | Record l -> <:expr< $run$.Record $make_fields _loc l$ >>
      | Hashtbl x -> <:expr< $run$.Hashtbl $convert x$ >>
      | Assoc x -> <:expr< $run$.Assoc $convert x$ >>
      | Tuple l -> 
	  let tl = 
	    List.fold_right (fun x l -> 
			       <:expr< [ $convert x$ :: $l$ ] >>)
	      l <:expr< [] >> in
	  <:expr< $run$.Tuple $tl$ >>
      | Poly l -> <:expr< $run$.Poly $make_constructors _loc l$ >>
      | Variant l -> <:expr< $run$.Variant $make_constructors _loc l$ >>
      | Name x -> <:expr< $run$.Name $str:x$ >>
      | String -> <:expr< $run$.String >>
      | Bool -> <:expr< $run$.Bool >>
      | Int -> <:expr< $run$.Int >>
      | Float -> <:expr< $run$.Float >>
      | Number -> <:expr< $run$.Number >>
      | Raw -> <:expr< $run$.Raw >>
      | Custom s -> <:expr< $uid:s$ . typedef >>


  and make_field _loc (x : field) =
    let to_json = new_id "to_json" in
    let json_default_value = new_id "json_default_value" in
    let get_json_subset = new_id "get_json_subset" in
    let validate_abstract_subset mem =
      <:expr< 
	fun x ->
	  if not $mem$ then
	    __json_static_error ($lid:to_json$ x)
	      "invalid field value: not in valid subset"
	  else ()
      >>
    in
    let validate_list_subset list =
      validate_abstract_subset <:expr< List.mem x $list$ >>
    in
    let validate_dynlist_subset get_list =
      validate_list_subset <:expr< $get_list$ () >>
    in

    let has_default = x.default <> None in
    let has_subset = x.subset <> None in

    let tojson_def =
      if not has_default && not has_subset then <:str_item< declare end >>
      else
	let e1 = make_tojson_val x.field_type in
	<:str_item< value $lid:to_json$ = $e1$ >>
    in

    let validator =
      match x.subset with
	  None -> <:str_item< value $lid:get_json_subset$ = None >>
	| Some set ->
	    let lid s = <:expr< $lid:s$ >> in
	    let lidx s = <:expr< $lid:s$ x >> in
	    let e1 =
	      match set with
		  `List e -> 
		    <:expr< 
		      let list = $e$ in
		      let json_list = List.map $lid:to_json$ list in
		      ( Some (`List json_list),
			$validate_list_subset (lid "list")$ )
		    >>
		| `Dynlist get_list ->
		    <:expr< 
		      let get_list = $get_list$ in
		      let get_json_list () =
			List.map $lid:to_json$ (get_list ()) in
		      ( Some (`Dynlist get_json_list),
			$validate_dynlist_subset (lid "get_list")$ )
		    >>
		| `Abstract_set mem ->
		    <:expr<
		      let mem = $mem$ in
		      ( Some `Abstract_set,
			$validate_abstract_subset (lidx "mem")$ )
		    >>
	    in
	    let validator_name =
	      match x.validator_name with
		  Some s -> s
		| None -> assert false in
	    <:str_item< 
	      value ( $lid:get_json_subset$, $lid:validator_name$ ) = $e1$
	    >>
    in

    additional_defs := tojson_def :: !additional_defs;
    additional_defs := validator :: !additional_defs;

    let insert_default e2 =
      (* default value must be in subset *)
      match x.default with
	  None -> <:expr< let $lid:json_default_value$ = None in $e2$ >>
	| Some e -> 
	    let _loc = MLast.loc_of_expr e in
	    let e1 =
	      if has_subset then
		let validator_name =
		  match x.validator_name with
		      Some s -> s
		    | None -> assert false in
		<:expr< 
		  Some ( 
		    let default_value = $e$ in
		    do { $lid:validator_name$ default_value;
			 $lid:to_json$ default_value }
                  )
		>>
	      else
		<:expr< Some ( $lid:to_json$ $e$ ) >>
	    in
	    <:expr< let $lid:json_default_value$ = $e1$ in $e2$ >>
    in
    (insert_default
     <:expr<
     { Run_json_static.field_caml_name = $str:x.field_caml_name$;
       field_json_name = $str:x.field_json_name$;
       field_type = $convert x.field_type$;
       optional = $bool _loc x.optional$;
       json_default = $lid:json_default_value$;
       json_subset = $lid:get_json_subset$;
       is_mutable = $bool _loc x.is_mutable$ }
     >>)

  and make_fields _loc (l : field list) =
    List.fold_right
      (fun field l -> 
	 let e = make_field _loc field in
	 <:expr< [ $e$ :: $l$ ] >>)
      l <:expr< [] >>
      
  and make_constructors _loc l =
    List.fold_right
      (fun x l ->
	 let args =
	   List.fold_right 
	     (fun x l -> <:expr< [ $convert x$ :: $l$ ] >>)
	     x.cons_args <:expr< [] >> in
	 <:expr< [ { Run_json_static.cons_caml_name = $str:x.cons_caml_name$;
		     cons_json_name = $str:x.cons_json_name$;
		     cons_args = $args$ } :: $l$ ] >>)
      l <:expr< [] >>
  in

  let defs = 
    optmap
      (fun ((_loc, name), x) -> 
	 let xname = name ^ "_typedef" in
	 Some (<:patt< ( $lid:xname$ : Run_json_static.typedef ) >>, 
	       <:expr< { Run_json_static.typename = $str:name$;
			 def = $convert x.def$;
			 is_predefined = $bool _loc x.is_predefined$;
			 is_private = $bool _loc x.is_private$ } >>)) l in
  if defs <> [] then
    let x =
      List.rev_append !additional_defs
	[ <:str_item< value $list:defs$ >> ] in
    <:str_item< 
      declare
        $list:x$
      end
    >>
  else
    <:str_item< declare end >>




let expand_typedefs _loc l =
  check_unique (fun (name, x) -> name) l;
  let names = 
    List.fold_left 
      (fun m (((_loc, name), x) as data) -> StringMap.add name data m)
      StringMap.empty l in
  let typedef = make_typedef _loc names l in
  let typeval =
    if !light_mode then <:str_item< declare end >>
    else make_typeval _loc l in
  let ofjson = make_ofjson_defs _loc l in
  let tojson = make_tojson_defs _loc l in
  <:str_item< 
    declare 
      $error _loc$; 
      $typedef$;
      $typeval$;
      $ofjson$;
      $tojson$; 
    end
  >>


let o2b = function None -> false | _ -> true

let is_reserved =
  let l = [ "json"; "json_type";
	    "string"; "bool"; "int"; "float"; 
	    "number"; "assoc" ] in
  let tbl = Hashtbl.create 20 in
  List.iter (fun s -> Hashtbl.add tbl s ()) l;
  Hashtbl.mem tbl


let find_and_remove k0 l0 =
  try 
    let _loc, _, v = List.find (fun (_, k, _) -> k = k0) l0 in
    let removed, l = List.partition (fun (_, k, _) -> k = k0) l0 in
    if List.length removed <> 1 then
      Stdpp.raise_with_loc _loc
	(Failure ("this option is specified multiple times: " ^ k0))
    else
      Some v, l
  with Not_found -> None, l0

let read_options _loc default options =
  let options = match options with None -> [] | Some l -> l in
  let options =
    match default with
	None -> options
      | Some e -> (MLast.loc_of_expr e, "default", e) :: options in
  let default, options = find_and_remove "default" options in
  let list, options = find_and_remove "list_subset" options in
  let dynlist, options = find_and_remove "dynlist_subset" options in
  let abstract, options = find_and_remove "abstract_subset" options in
  if options <> [] then
    Stdpp.raise_with_loc _loc
      (Failure (sprintf "unknown options: %s"
	 (String.concat ", " (List.map (fun (_, k, _) -> k) options))))
  else
    let subset = ref None in
    let set r f o = 
      match !r, o with
	  None, Some x -> r := Some (f x)
	| Some _, Some _ ->
	    Stdpp.raise_with_loc _loc
	      (Failure "incompatible subset options")
	| _ -> ()
    in
    set subset (fun e -> `List e) list;
    set subset (fun e -> `Dynlist e) dynlist;
    set subset (fun e -> `Abstract_set e) abstract;
    default, !subset


open Pcaml

let list_of_opt = function None -> [] | Some x -> [x]
let list_of_optlist = function None -> [] | Some x -> x

let check_methods l =
  List.iter (fun x ->
	       if x.is_mutable then
		 Stdpp.raise_with_loc x.field_caml_loc 
		   (Failure "object fields cannot be made mutable")) l

let string_assoc _loc = function
    (_loc, Tuple [ (_, String); (_, x) ]) -> (_loc, x)
  | (_, _) -> 
      Stdpp.raise_with_loc _loc
	(Failure "must be of the form (string * ...) assoc")

EXTEND
  GLOBAL: str_item;
  str_item: LEVEL "top" [
    [ "type"; LIDENT "json"; 
      l = LIST1 type_binding SEP "and" -> expand_typedefs _loc l ]
  ];

  type_binding: [
    [ name = [ s = LIDENT -> 
		 if is_reserved s then
		   Stdpp.raise_with_loc _loc 
		     (Failure ("you can't use '" ^ s ^ "' as a type name"))
		 else (_loc, s) ]; 
      "=";
      p = OPT [ LIDENT "predefined" (* ; 
		priv = OPT "private"*) -> (* priv <> None *) false ];
      t = [ t = type_expr -> (t : t)
	  | r = record -> (_loc, Record r)
	  | v = variants -> (_loc, Variant v) ] ->
	let typedef =
	  match p with
	      None -> { is_predefined = false;
			is_private = false;
			def = t }
	    | Some is_private -> 
		{ is_predefined = true;
		  is_private = is_private;
		  def = t } in
	(name, typedef) ]
  ];

  record: [
    [ "{"; l = methods; "}" -> l ]
  ];

  variants: [
    [ l = 
	LIST1 [ id = [ id = UIDENT -> (_loc, id) ]; 
		label = OPT [ s = STRING -> 
				(_loc, Token.eval_string _loc s) ];
		typ = OPT [ "of";
			    x = LIST1 type_expr LEVEL "simple" 
				SEP "*" -> x ] -> 
		  let id' = unopt id label in
		  { cons_caml_loc = fst id;
		    cons_caml_name = snd id;
		    cons_json_loc = fst id';
		    cons_json_name = snd id';
		    cons_args = list_of_optlist typ } ] 
	  SEP "|" -> 
	    check_unique (fun x -> (x.cons_caml_loc, x.cons_caml_name)) l;
            check_unique (fun x -> (x.cons_json_loc, x.cons_json_name)) l;
	    l ]
  ];

  type_expr: [
    "top" [
      x = type_expr; "*"; l = LIST1 type_expr LEVEL "simple" SEP "*" ->
	(_loc, Tuple (x :: l)) 
    ]
		
  | "simple" [
      x = type_expr; LIDENT "list" -> (_loc, List x)
    | x = type_expr; LIDENT "array" -> (_loc, Array x)
    | x = type_expr; LIDENT "option" -> (_loc, Option x)
    | x = type_expr; LIDENT "assoc" -> (_loc, Assoc (string_assoc _loc x))
    | "<"; l = methods; ">" -> check_methods l; (_loc, Object l)
    | "["; l = polymorphic_variants; "]" -> (_loc, Poly l)
    | "("; x = type_expr; ")" -> x
    | "("; LIDENT "string"; ","; x = type_expr; ")"; 
      UIDENT "Hashtbl"; "."; LIDENT "t" -> 
	(_loc, Hashtbl x)
    | name = LIDENT -> (_loc, Name name)
    | LIDENT "string" -> (_loc, String)
    | LIDENT "bool" -> (_loc, Bool)
    | LIDENT "int" -> (_loc, Int)
    | LIDENT "float" -> (_loc, Float)
    | LIDENT "number" -> (_loc, Number)
    | [ UIDENT "Json_type"; "."; LIDENT "json_type"
      | LIDENT "json_type" ] -> (_loc, Raw)
    | module_name = UIDENT; "."; LIDENT "t" -> 
	if module_name = "Json_type" then (_loc, Raw)
	else (_loc, Custom module_name) ]
  ];

  polymorphic_variants: [
    [ l = 
	LIST1 [ "`"; id = [ id = [ LIDENT | UIDENT ] -> (_loc, id) ]; 
		label = OPT [ s = STRING -> 
				(_loc, Token.eval_string _loc s) ];
		typ = OPT [ "of"; x = type_expr -> x ] -> 
		  let id' = unopt id label in
		  { cons_caml_loc = fst id;
		    cons_caml_name = snd id;
		    cons_json_loc = fst id';
		    cons_json_name = snd id';
		    cons_args = list_of_opt typ } ] 
	  SEP "|" -> 
	    check_unique (fun x -> (x.cons_caml_loc, x.cons_caml_name)) l;
            check_unique (fun x -> (x.cons_json_loc, x.cons_json_name)) l;
	    l ]
  ];

  methods: [
    [ l = LIST0 
	    [ mut = OPT "mutable";
	      lab = method_label; x = type_expr; 
	      options = OPT [ "{"; l = LIST0 option SEP ";"; "}" -> l ];
	      default = OPT [ "="; e = expr LEVEL "apply" -> e ]-> 
		let ((id, optional), label) = lab in
		let id' = unopt id label in
		let default, subset = read_options _loc default options in
		let validator_name =
		  if subset = None then None
		  else Some (new_id "validate") in
		{ field_caml_loc = fst id;
		  field_caml_name = snd id;
		  field_json_loc = fst id';
		  field_json_name = snd id';
		  field_type = x;
		  optional = optional;
		  default = default;
		  subset = subset;
		  validator_name = validator_name;
		  is_mutable = (mut <> None) } ]
	    SEP ";" ->
	check_unique (fun x -> (x.field_caml_loc, x.field_caml_name)) l;
        check_unique (fun x -> (x.field_json_loc, x.field_json_name)) l;
	l ]
  ];

  option: [
    [ k = LIDENT; "="; v = expr LEVEL "expr1" -> (_loc, k, v) ]
  ];

  method_label: [
    [ id_opt = [ id = LIDENT -> ((_loc, id), false)
	       | id = QUESTIONIDENT -> ((_loc, id), true) ]; 
      label = OPT [ s = STRING -> 
		      (_loc, Token.eval_string _loc s) ];
      ":" -> (id_opt, label)
    | id = OPTLABEL -> (((_loc, id), true), None) ]
  ];

END
;;

let _ =
  Pcaml.add_option "-js-light" 
    (Arg.Set light_mode)
    " no typedefs would be generated"