Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 895 lines (729 sloc) 25.157 kb
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
open Camlp4.PreCast
open Syntax

(* [[ let! x = a in b ]] *)

EXTEND Gram
    GLOBAL: expr;

    expr: LEVEL "top"
    [
      [ "let"; "!"; p = patt ; "=" ; e = expr ; "in" ; e' = expr ->
        <:expr< (bind ($e$) (fun $p$ -> $e'$)) >> ]
    ] ;

END;;


(* [[ List.map (#name) people ]] *)

EXTEND Gram
    GLOBAL: expr;

    expr: LEVEL "simple"
    [
      [ "#" ; i = LIDENT ->
        <:expr< (fun __obj -> __obj # $i$) >> ]
    ] ;

END;;

(* [[ type json person = < name : string > ]] *)

type id = Loc.t * string

type typemod = Loc.t * id list
type strtype = Loc.t * strtypedef
and strtypedef =
  [ `string
  | `m of typemod
  | `poly of < label : id ; name : id > list
  ]

type typexpr = Loc.t * typedef
and typedef =
  [ `variant of <
      label : id ;
      name : id ;
      typ : typexpr list
    > list
  | `record of <
      label : id ;
      name : id ;
      typ : typexpr ;
      default : Ast.expr option option ;
      mut : bool
    > list
  | `tuple of typexpr list
  | `string
  | `bool
  | `float
  | `int
  | `unit
  | `self
  | `option of typexpr
  | `list of typexpr
  | `array of typexpr
  | `param of typemod * typexpr
  | `param2 of typemod * strtype * typexpr
  | `obj of <
      label : id ;
      name : id ;
      typ : typexpr ;
      default : Ast.expr option option ;
      mut : bool
    > list
  | `poly of <
      label : id ;
      name : id ;
      typ : typexpr list
    > list
  | `m of typemod
  ]

let def v = function
  | None -> v
  | Some v -> v

let loc = fst
let ident = snd

let tn _loc i = <:ident< $"_t_" ^string_of_int i$ >>

let error _loc text src =
  let s = <:expr< $str:("Expecting " ^ text ^ ", found ")$ ^ Json.serialize $src$ >> in
  <:expr< raise (Json.Error ($s$)) >>

let fresh =
  let i = ref 0 in
  fun () -> incr i ; "_t_" ^ string_of_int !i

let parse_record src recurse _loc list return =

  let error = error _loc in

  let l = List.map (fun t -> (fresh (),t)) list in

  let set i e = <:expr< $lid:i$.val := Some $e$ >> in

  let test =
    let cases = List.fold_left begin fun acc (i,t) ->
      let v = recurse <:expr< v >> (t # typ) in
      let m = <:match_case< $str:snd (t # label)$ -> $set i v$ >> in
      <:match_case< $m$ | $acc$ >>
    end <:match_case< _ -> () >> l in
    <:expr< match k with [ $cases$ ] >>
  in
  
  let read = <:expr< List.iter (fun (k,v) -> $test$) _l_ >> in
  
  let return = return l in
  
  let expr = List.fold_left begin fun acc (i,t) ->
    let from = <:expr< $lid:i$.val >> in
    let patt = <:patt< $lid:i$ >> in
    let none = match t # default with
      | None -> error ("field \"" ^ (snd t # label) ^ "\"") src
      | Some None -> <:expr< None >>
      | Some (Some e) -> e
    in
    let expr = <:expr< match $from$ with [ Some _t_ -> _t_ | None -> $none$ ] >> in
    <:expr< let $patt$ = $expr$ in $acc$ >>
  end return l in
  
  let expr = <:expr< let () = $read$ in $expr$ >> in

  let expr = List.fold_left begin fun acc (i,t) ->
    let patt = <:patt< $lid:i$ >> in
    let expr = <:expr< ref None >> in
    <:expr< let $patt$ = $expr$ in $acc$ >>
  end expr l in

  let ok = <:match_case< Json.Object _l_ -> $expr$ >> in
  let nok = <:match_case< _ -> $error "object" src$ >> in
  <:expr< match $src$ with [ $ok$ | $nok$ ] >>

let unique list =
  let list = List.map (fun a -> a # label) list in
  let list = List.sort (fun a b -> compare (ident a) (ident b)) list in
  let rec test = function
    | [] | [_] -> ()
    | a :: (b :: _ as t) ->
      if ident a = ident b then
Loc.raise (loc a) (Failure "This JSON label should be unique") ;
      test t
  in test list

let make_variant_bind ~name ?label ?(typ=[]) () = object

  val label = def name label
  method label = label

  val name = name
  method name = name

  val typ = typ
  method typ = typ

end

let make_poly_bind ~name ?label ?(typ=[]) () = object

  val label = def name label
  method label = label

  val name = name
  method name = name

  val typ = typ
  method typ = typ

end

let make_strpoly_bind ~name ?label () = object

  val label = def name label
  method label = label

  val name = name
  method name = name

end

let make_member_bind ~mut ~name ?label ?default typ = object

  val label = def name label
  method label = label
    
  val mut = mut
  method mut = mut

  val name = name
  method name = name

  val default = default
  method default = default

  val typ = typ
  method typ = typ

end

let generate_t_of_json _loc (def:typexpr) =

  let error = error _loc in

  let str_error src =
    let s = <:expr< "Unknown variant label \"" ^ $src$ ^ "\"" >> in
    <:expr< raise (Json.Error ($s$)) >>
  in

  let in_module what ((_loc,def):typemod) =
    let rec aux = function
      | [] -> what
      | h :: t -> let _loc = fst h in
<:ident< $uid:ident h$ . $aux t$ >>
    in aux def
  in

  let strtype src (_loc,def) = match def with

    | `string -> src

    | `m m -> begin
      let f = in_module <:ident< of_string >> m in
      <:expr< $id:f$ $src$ >>
    end

    | `poly p -> begin
      let matches = List.fold_right begin fun v acc ->
let m = <:match_case< $str:snd v#label$ -> `$ident (v#name)$ >> in
<:match_case< $m$ | $acc$ >>
      end p <:match_case< _ -> $str_error src$ >> in
      <:expr< match $src$ with [ $matches$ ] >>
    end

  in

  let rec recurse src ((_loc,def):typexpr) = match def with
      
    | `string -> begin
      let ok = <:match_case< Json.String _s_ -> _s_ >> in
      let nok = <:match_case< _ -> $error "string" src$ >> in
      <:expr< match $src$ with [ $ok$ | $nok$ ] >>
    end

    | `bool -> begin
      let ok = <:match_case< Json.Bool _b_ -> _b_ >> in
      let nok = <:match_case< _ -> $error "bool" src$ >> in
      <:expr< match $src$ with [ $ok$ | $nok$ ] >>
    end

    | `int -> begin
      let ok = <:match_case< Json.Int _i_ -> _i_ >> in
      let nok = <:match_case< _ -> $error "int" src$ >> in
      <:expr< match $src$ with [ $ok$ | $nok$ ] >>
    end

    | `float -> begin
      let ok = <:match_case< Json.Float _f_ -> _f_ >> in
      let ok' = <:match_case< Json.Int _i_ -> float_of_int _i_ >> in
      let nok = <:match_case< _ -> $error "float" src$ >> in
      <:expr< match $src$ with [ $ok$ | $ok'$ | $nok$ ] >>
    end

    | `unit -> <:expr< () >>

    | `self -> <:expr< t_of_json $src$ >>

    | `m m -> begin
      let f = in_module <:ident< of_json >> m in
      <:expr< $id:f$ $src$ >>
    end

    | `option t -> begin
      let r = recurse <:expr< _t_ >> t in
      let none = <:match_case< Json.Null -> None >> in
      let some = <:match_case< _t_ -> Some $r$ >> in
      <:expr< match $src$ with [ $none$ | $some$ ] >>
    end

    | `list t -> begin
      let r = recurse <:expr< _t_ >> t in
      let list = <:match_case< Json.Array _l_ -> List.map (fun _t_ -> $r$) _l_ >> in
      let fail = <:match_case< _ -> $error "list" src$ >> in
      <:expr< match $src$ with [ $list$ | $fail$ ] >>
    end

    | `array t -> begin
      let r = recurse <:expr< _t_ >> t in
      let list = <:match_case< Json.Array _l_ -> Array.of_list (List.map (fun _t_ -> $r$) _l_) >> in
      let fail = <:match_case< _ -> $error "list" src$ >> in
      <:expr< match $src$ with [ $list$ | $fail$ ] >>
    end

    | `param (m,t) -> begin
      let r = recurse <:expr< _t_ >> t in
      let r_fun = <:expr< fun _t_ -> $r$ >> in
      let f = in_module <:ident< of_json >> m in
      <:expr< $id:f$ $r_fun$ $src$ >>
    end

    | `param2 (m,s,t) -> begin
      let t = recurse <:expr< _t_ >> t in
      let t_fun = <:expr< fun _t_ -> $t$ >> in
      let s = strtype <:expr< _t_ >> s in
      let s_fun = <:expr< fun _t_ -> $s$ >> in
      let f = in_module <:ident< of_json >> m in
      <:expr< $id:f$ $s_fun$ $t_fun$ $src$ >>
    end

    | `tuple l -> begin
      let l = List.map (fun t -> (fresh (),t)) l in
      let patt = List.fold_right begin fun (i,_) acc ->
let id = <:patt< $lid:i$ >> in
<:patt< [ $id$ :: $acc$ ] >>
      end l <:patt< [] >> in
      let patt = <:patt< Json.Array $patt$ >> in
      let expr = List.fold_right begin fun (i,t) acc ->
let id = <:expr< $lid:i$ >> in
let t = recurse id t in
<:expr< $t$, $acc$ >>
      end l <:expr< >> in
      let ok = <:match_case< $patt$ -> ($tup:expr$) >> in
      let nok = <:match_case< _ -> $error "tuple" src$ >> in
      <:expr< match $src$ with [ $ok$ | $nok$ ] >>
    end

    | `variant l -> begin

      let matches = List.fold_right begin fun v acc ->
        let p = <:patt< $str:snd v#label$ >> in
let p = <:patt< Json.String $p$ >> in
        let m = match v # typ with
| [] -> <:match_case< $p$ -> $uid:ident (v#name)$ >>
| list -> begin
let l = List.map (fun t -> (fresh (),t)) list in
let patt = List.fold_right begin fun (i,_) acc ->
let id = <:ident< $lid:i$ >> in
<:patt< [ $id:id$ :: $acc$ ] >>
end l <:patt< [] >> in
let patt = <:patt< Json.Array [ $p$ :: $patt$ ] >> in
let list = List.fold_left begin fun acc (i,t) ->
let id = <:expr< $lid:i$ >> in
let t = recurse id t in
<:expr< $acc$ $t$ >>
end <:expr< $uid:ident (v#name)$ >> l in
<:match_case< $patt$ -> $list$ >>
end
in
<:match_case< $m$ | $acc$ >>
      end l <:match_case< _ -> $error "variant" src$ >> in
      <:expr< match $src$ with [ $matches$ ] >>
    end

    | `poly l -> begin

      let matches = List.fold_right begin fun v acc ->
        let p = <:patt< $str:snd v#label$ >> in
let p = <:patt< Json.String $p$ >> in
        let m = match v # typ with
| [] -> let e = <:expr< ` $ident (v#name)$ >> in
<:match_case< $p$ -> $e$ >>
| [t] -> begin
let patt = <:patt< Json.Array [ $p$ ; _t_ ] >> in
let expr = recurse <:expr< _t_ >> t in
let expr = <:expr< `$ident (v#name)$ $expr$ >> in
<:match_case< $patt$ -> $expr$ >>
end
| list -> begin
let l = List.map (fun t -> (fresh (),t)) list in
let patt = List.fold_right begin fun (i,_) acc ->
let id = <:ident< $lid:i$ >> in
<:patt< [ $id:id$ :: $acc$ ] >>
end l <:patt< [] >> in
let patt = <:patt< Json.Array [ $p$ ; Json.Array $patt$ ] >> in
let list = List.fold_right begin fun (i,t) acc ->
let id = <:expr< $lid:i$ >> in
let t = recurse id t in
<:expr< $t$, $acc$ >>
end l <:expr< >> in
let expr = <:expr< `$ident (v#name)$ ( $tup:list$ ) >> in
<:match_case< $patt$ -> $expr$ >>
end
in
<:match_case< $m$ | $acc$ >>
      end l <:match_case< _ -> $error "variant" src$ >> in
      <:expr< match $src$ with [ $matches$ ] >>
    end


    | `record r -> begin
      parse_record src recurse _loc r begin fun l ->
let fields = List.fold_left begin fun acc (i,t) ->
let bind = <:rec_binding< $lid:ident (t # name)$ = $lid:i$>> in
<:rec_binding< $acc$ ; $bind$ >>
end <:rec_binding< >> l in
<:expr< { $fields$ } >>
      end
    end

    | `obj o -> begin
      parse_record src recurse _loc o begin fun l ->
let fields = List.fold_left begin fun acc (i,t) ->
let name = ident (t # name) in
let vbind = <:class_str_item< value $name$ = $lid:i$>> in
let mbind = <:class_str_item< method $lid:name$ = $lid:name$ >> in
<:class_str_item< $vbind$ ; $mbind$ ; $acc$ >>
end <:class_str_item< >> l in
<:expr< object $fields$ end >>
      end
    end

  in

  let b =
    let e = recurse <:expr< _t_ >> def in
    let f = <:expr< fun _t_ -> $e$ >> in
    let b = <:ident< t_of_json >> in
    let p = <:patt< $id:b$ >> in
    <:binding< $p$ = $f$ >>
  in

  <:str_item< value rec $b$ >>

let generate_json_of_t _loc (def:typexpr) =

  let in_module what ((_loc,def):typemod) =
    let rec aux = function
      | [] -> what
      | h :: t -> let _loc = fst h in
<:ident< $uid:ident h$ . $aux t$ >>
    in aux def
  in

  let strtype src (_loc,def) = match def with
    | `string -> src
    | `m m -> begin
      let f = in_module <:ident< to_string >> m in
      <:expr< $id:f$ $src$ >>
    end
    | `poly p -> begin
      let matches = List.fold_right begin fun v acc ->
        let e = <:expr< $str:snd v#label$ >> in
let m = <:match_case< `$ident (v#name)$ -> $e$ >> in
<:match_case< $m$ | $acc$ >>
      end p <:match_case< >> in
      <:expr< match $src$ with [ $matches$ ] >>
    end
  in

  let rec recurse src ((_loc,def):typexpr) = match def with

    | `string -> <:expr< Json.String $src$ >>
    | `int -> <:expr< Json.Int $src$ >>
    | `float -> <:expr< Json.Float $src$ >>
    | `bool -> <:expr< Json.Bool $src$ >>
    | `unit -> <:expr< Json.Null >>

    | `self -> <:expr< json_of_t $src$ >>
      
    | `m m -> begin
      let f = in_module <:ident< to_json >> m in
      <:expr< $id:f$ $src$ >>
    end
      
    | `option t -> begin
      let r = recurse <:expr< _t_ >> t in
      let m_none = <:match_case< None -> Json.Null >> in
      let m_some = <:match_case< Some _t_ -> $r$ >> in
      let m = <:match_case< $m_none$ | $m_some$ >> in
      <:expr< match $src$ with [ $m$ ] >>
    end
      
    | `list t -> begin
      let r = recurse <:expr< _t_ >> t in
      let e_fun = <:expr< fun _t_ -> $r$ >> in
      <:expr< Json.Array (List.map $e_fun$ $src$) >>
    end

    | `array t -> begin
      let r = recurse <:expr< _t_ >> t in
      let src = <:expr< Array.to_list $src$ >> in
      let e_fun = <:expr< fun _t_ -> $r$ >> in
      <:expr< Json.Array (List.map $e_fun$ $src$) >>
    end
      
    | `param (m,t) -> begin
      let r = recurse <:expr< _t_ >> t in
      let e_fun = <:expr< fun _t_ -> $r$ >> in
      let f = in_module <:ident< to_json >> m in
      <:expr< $id:f$ $e_fun$ $src$ >>
    end
      
    | `param2 (m,s,t) -> begin
      let t = recurse <:expr< _t_ >> t in
      let t_fun = <:expr< fun _t_ -> $t$ >> in
      let s = strtype <:expr< _t_ >> s in
      let s_fun = <:expr< fun _t_ -> $s$ >> in
      let f = in_module <:ident< to_json >> m in
      <:expr< $id:f$ $s_fun$ $t_fun$ $src$ >>
    end

    | `tuple l -> begin
      let l = List.map (fun t -> (fresh (),t)) l in
      let patt = List.fold_right begin fun (i,_) acc ->
let id = <:patt< $lid:i$ >> in
<:patt< $id$ , $acc$ >>
      end l <:patt< >> in
      let list = List.fold_right begin fun (i,t) acc ->
let id = <:ident< $lid:i$ >> in
let id = <:expr< $id:id$ >> in
let t = recurse id t in
<:expr< [ $t$ :: $acc$ ] >>
      end l <:expr< [] >> in
      let bind = <:binding< ($tup:patt$) = $src$ >> in
      <:expr< let $bind$ in Json.Array $list$ >>
    end

    | `variant l -> begin
      let matches = List.fold_right begin fun v acc ->
        let e = <:expr< $str:snd v#label$ >> in
let e = <:expr< Json.String $e$ >> in
        let m = match v # typ with
| [] -> <:match_case< $uid:ident (v#name)$ -> $e$ >>
| list -> begin
let l = List.map (fun t -> (fresh (),t)) list in
let patt = List.fold_left begin fun acc (i,_) ->
let id = <:ident< $lid:i$ >> in
<:patt< $acc$ $id:id$ >>
end <:patt< $uid:ident (v#name)$ >> l in
let list = List.fold_right begin fun (i,t) acc ->
let id = <:ident< $lid:i$ >> in
let id = <:expr< $id:id$ >> in
let t = recurse id t in
<:expr< [ $t$ :: $acc$ ] >>
end l <:expr< [] >> in
let e = <:expr< [ $e$ :: $list$ ] >> in
<:match_case< $patt$ -> Json.Array $e$ >>
end
in
<:match_case< $m$ | $acc$ >>
      end l <:match_case< >> in
      <:expr< match $src$ with [ $matches$ ] >>
    end
      
    | `poly p -> begin
      let matches = List.fold_right begin fun v acc ->
        let e = <:expr< $str:snd v#label$ >> in
let e = <:expr< Json.String $e$ >> in
        let m = match v # typ with
| [] -> <:match_case< `$ident (v#name)$ -> $e$ >>
| [t] -> begin
let patt = <:patt< `$ident (v#name)$ _t_ >> in
let t = recurse <:expr< _t_ >> t in
let e = <:expr< [ $e$ ; $t$ ] >> in
<:match_case< $patt$ -> Json.Array $e$ >>
end
| list -> begin
let l = List.map (fun t -> (fresh (),t)) list in
let patt = List.fold_right begin fun (i,_) acc ->
let id = <:ident< $lid:i$ >> in
<:patt< $id:id$ , $acc$ >>
end l <:patt< >> in
let patt = <:patt< `$ident (v#name)$ $tup:patt$ >> in
let list = List.fold_right begin fun (i,t) acc ->
let id = <:ident< $lid:i$ >> in
let id = <:expr< $id:id$ >> in
let t = recurse id t in
<:expr< [ $t$ :: $acc$ ] >>
end l <:expr< [] >> in
let e = <:expr< [ $e$ ; Json.Array $list$ ] >> in
<:match_case< $patt$ -> Json.Array $e$ >>
end
in
<:match_case< $m$ | $acc$ >>
      end p <:match_case< >> in
      <:expr< match $src$ with [ $matches$ ] >>
    end

    | `record r -> begin
      let list = List.fold_right begin fun f acc ->
let l = <:expr< $str:snd (f#label)$ >> in
let t = <:expr< $src$ . $lid:ident (f # name)$ >> in
let e = recurse t (f # typ) in
let e = <:expr< ($l$, $e$) >> in
<:expr< [ $e$ :: $acc$ ] >>
      end r <:expr< [] >> in
      <:expr< Json.Object $list$ >>
    end

    | `obj o -> begin
      let list = List.fold_right begin fun f acc ->
let l = <:expr< $str:snd (f#label)$ >> in
let t = <:ident< t >> in
let b =
let b = <:expr< $src$ # $ident (f # name)$ >> in
let p = <:patt< $id:t$ >> in
<:binding< $p$ = $b$ >>
in
let e =
let e = recurse <:expr< $id:t$ >> (f # typ) in
<:expr< let $b$ in $e$ >>
in
let e = <:expr< ($l$, $e$) >> in
<:expr< [ $e$ :: $acc$ ] >>
      end o <:expr< [] >> in
      <:expr< Json.Object $list$ >>
    end
  in

  let b =
    let e = recurse <:expr< _t_ >> def in
    let f = <:expr< fun _t_ -> $e$ >> in
    let b = <:ident< json_of_t >> in
    let p = <:patt< $id:b$ >> in
    <:binding< $p$ = $f$ >>
  in

  <:str_item< value rec $b$ >>

let generate_type _loc (def:typexpr) =

  let in_module ((_loc,def):typemod) =
    let rec aux = function
      | [] -> <:ident< t >>
      | h :: t -> let _loc = fst h in
<:ident< $uid:ident h$ . $aux t$ >>
    in aux def
  in

  let strtype ((_loc,def):strtype) = match def with
    | `string -> <:ctyp< string >>
    | `m m -> <:ctyp< $id:in_module m$ >>
    | `poly p -> let l = List.fold_right begin fun ctor acc ->
      
                   let _loc = loc (ctor # name) in
let name = ident (ctor # name) in
let ctor = <:ctyp< `$name$ >> in
<:ctyp< $ctor$ | $acc$ >>

                 end p <:ctyp< >> in
<:ctyp< [ = $l$ ] >>
  in

  let rec recurse (_loc,def) = match def with

    | `bool -> <:ctyp< bool >>
    | `string -> <:ctyp< string >>
    | `int -> <:ctyp< int >>
    | `float -> <:ctyp< float >>
    | `unit -> <:ctyp< unit >>
    | `self -> <:ctyp< t >>

    | `list t -> <:ctyp< list $recurse t$ >>
    | `array t -> <:ctyp< array $recurse t$ >>
    | `option t -> <:ctyp< option $recurse t$ >>

    | `m m -> <:ctyp< $id:in_module m$ >>

    | `record r -> let l = List.fold_right begin fun field acc ->

                     let _loc = loc (field # name) in
let name = ident (field # name) in

                     let t = recurse (field # typ) in
let t = if field # mut then <:ctyp< mutable $t$ >> else t in
let field = <:ctyp< $lid:name$ : $t$ >> in

<:ctyp< $field$; $acc$ >>

                   end r <:ctyp< >> in
<:ctyp< { $l$ } >>

    | `obj o -> let l = List.fold_right begin fun field acc ->

                  let _loc = loc (field # name) in
let name = ident (field # name) in

                  let t = recurse (field # typ) in
let field = <:ctyp< $lid:name$ : $t$ >> in

<:ctyp< $field$; $acc$ >>

                end o <:ctyp< >> in
<:ctyp< < $l$ > >>

    | `tuple t -> let t = List.fold_right begin fun t acc ->
                    <:ctyp< $recurse t$ * $acc$ >>
                  end t <:ctyp< >> in
<:ctyp< ( $tup:t$ ) >>

    | `variant v -> let l = List.fold_right begin fun ctor acc ->
      
                      let _loc = loc (ctor # name) in
let name = ident (ctor # name) in
let ctor = match ctor # typ with
| [] -> <:ctyp< $uid:name$ >>
| l -> let l = List.map recurse l in
<:ctyp< $uid:name$ of $list:l$ >>
in
<:ctyp< $ctor$ | $acc$ >>

                    end v <:ctyp< >> in
<:ctyp< [ $l$ ] >>

    | `poly p -> let l = List.fold_right begin fun ctor acc ->
      
                   let _loc = loc (ctor # name) in
let name = ident (ctor # name) in
let ctor = match ctor # typ with
| [] -> <:ctyp< `$name$ >>
| [t] ->
let t = recurse t in
<:ctyp< `$name$ of $t$ >>
| l ->
let l = List.fold_right (fun t acc -> <:ctyp< $recurse t$ * $acc$ >>) l <:ctyp< >> in
let t = <:ctyp< ( $tup:l$ ) >> in
<:ctyp< `$name$ of $t$ >>
in
<:ctyp< $ctor$ | $acc$ >>

                 end p <:ctyp< >> in
<:ctyp< [ = $l$ ] >>

    | `param (f,t) -> let t = recurse t in
let m = in_module f in
<:ctyp< $id:m$ $t$ >>

    | `param2 (f,s,t) -> let t = recurse t in
let s = strtype s in
let m = in_module f in
<:ctyp< $id:m$ $s$ $t$ >>
  in

  let dcl = Ast.TyDcl (_loc, "t", [], recurse def, []) in
  <:str_item< type $dcl$ >>

let generate _loc def =
  let typedef = generate_type _loc def in
  let to_json = generate_json_of_t _loc def in
  let of_json = generate_t_of_json _loc def in
  <:str_item< $typedef$ ; $to_json$ ; $of_json$ >>

EXTEND Gram
  GLOBAL: str_item ;

  str_item: LEVEL "top" [
    [ "type"; LIDENT "json"; loc = [ LIDENT "t" -> _loc ]; "="; def = typedef ->
      generate loc def ]
  ];

  name: [
    [ s = LIDENT -> (_loc,s) ]
  ];

  string: [
    [ s = STRING -> (_loc,Camlp4.Struct.Token.Eval.string ~strict:() s) ]
  ];

  typedef: [
    [ v = variant -> (_loc, `variant v)
    | r = record -> (_loc, `record r)
    | e = typexpr -> e ]
  ];
  
  variant: [
    [ OPT "|"; list = LIST1 variant_bind SEP "|" ->
      unique list ; list ]
  ];

  of_type: [
    [ "of" ; list = LIST1 typexpr LEVEL "simple" SEP "*" -> list ]
  ];

  variant_bind: [
    [ name = [ n = UIDENT -> (_loc,n) ]; label = OPT string ; typ = OPT of_type ->
      make_variant_bind ~name ?label ?typ () ]
  ];

  record: [
    [ "{"; list = record_members -> unique list ; list ]
  ];
      
  record_members: [
    [ m = member_bind ; "}" -> [m]
    | m = member_bind ; ";" ; "}" -> [m]
    | h = member_bind ; ";" ; t = record_members -> h :: t
    ]
  ];

  default: [
    [ "="; e = expr LEVEL "apply" -> e ]
  ];

  col_type: [
    [ ":"; t = typexpr -> t ]
  ];

  member_bind: [
    [ m = OPT "mutable"; name = name; label = OPT string; typ = col_type ->
      make_member_bind ~mut:false ~name ?label typ
    | m = OPT "mutable"; "?"; name = name; label = OPT string; typ = col_type; def = OPT default ->
      make_member_bind ~mut:false ~name ?label ~default:def typ ]
  ];

  object_members: [
    [ m = member_bind ; ">" -> [m]
    | m = member_bind ; ";" ; ">" -> [m]
    | h = member_bind ; ";" ; t = object_members -> h :: t
    ]
  ];

  typexpr: [
      "top" [
h = typexpr; "*"; t = LIST1 typexpr LEVEL "simple" SEP "*" ->
(_loc, `tuple (h :: t))
      ]

    | "simple" [
      
        LIDENT "string" -> (_loc,`string)
      | LIDENT "bool" -> (_loc,`bool)
      | LIDENT "float" -> (_loc,`float)
      | LIDENT "int" -> (_loc,`int)
      | LIDENT "unit" -> (_loc,`unit)
      | LIDENT "t" -> (_loc,`self)

      | t = typexpr ; LIDENT "option" -> (_loc, `option t)
      | t = typexpr ; LIDENT "list" -> (_loc, `list t)
      | t = typexpr ; LIDENT "array" -> (_loc, `array t)
      
      | t = typexpr ; u = typemod -> (_loc, `param (u,t))
      
      | "("; "!"; s = strtype; ","; t = typexpr; ")"; u = typemod -> (_loc,`param2 (u,s,t))
      
      | "<"; m = object_members -> (_loc,`obj m)
      
      | "("; t = typexpr; ")" -> t
      
      | "["; l = poly; "]" -> (_loc,`poly l)

      | t = typemod -> (_loc,`m t)
    ]
  ];

  poly_name: [
    [ "`"; name = [ `(LIDENT id|UIDENT id) -> (_loc,id) ] -> name ]
  ];

  poly_bind: [
    [ name = poly_name ; label = OPT string ; typ = OPT of_type ->
      make_poly_bind ~name ?label ?typ () ]
  ];

  strpoly_bind: [
    [ name = poly_name ; label = OPT string ->
      make_strpoly_bind ~name ?label () ]
  ];

  poly: [
    [ list = LIST1 poly_bind SEP "|" ->
      unique list ; list ]
  ];

  strpoly: [
    [ list = LIST1 strpoly_bind SEP "|" ->
      unique list ; list ]
  ];

  strtype: [
    [ LIDENT "string" -> (_loc,`string)
    | t = typemod -> (_loc, `m t)
    | "["; l = strpoly; "]" -> (_loc, `poly l) ]
  ];

  typemod_sub: [
    [ LIDENT "t" -> []
    | u = [ u = UIDENT -> (_loc,u) ]; "."; s = typemod_sub -> u :: s ]
  ];

  typemod: [
    [ u = [ u = UIDENT -> (_loc,u) ]; "."; s = typemod_sub -> (_loc, u::s) ]
  ];

END;;
Something went wrong with that request. Please try again.