package ppx_deriving
Type-driven code generation for OCaml
Install
Dune Dependency
Authors
Maintainers
Sources
ppx_deriving-6.0.3.tbz
sha256=374aa97b32c5e01c09a97810a48bfa218c213b5b649e4452101455ac19c94a6d
sha512=971443a5df0acbdad577360deed8c9af137695bec6d826ef517a382941371f3546aef53456dda7c89d0ed30fefadf45d5dae2a8b1940a75aee7f7382c68cedb0
doc/src/ppx_deriving_iter/ppx_deriving_iter.ml.html
Source file ppx_deriving_iter.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
open Ppxlib open Asttypes open Parsetree open Ast_helper open Ppx_deriving.Ast_convenience let deriver = "iter" let raise_errorf = Ppx_deriving.raise_errorf let ct_attr_nobuiltin = Attribute.declare_flag "deriving.iter.nobuiltin" Attribute.Context.core_type let argn = Printf.sprintf "a%d" let argl = Printf.sprintf "a%s" let pattn typs = List.mapi (fun i _ -> pvar (argn i)) typs let pattl labels = List.map (fun { pld_name = { txt = n } } -> n, pvar (argl n)) labels let pconstrrec name fields = pconstr name [precord ~closed:Closed fields] let rec expr_of_typ typ = let loc = !Ast_helper.default_loc in let typ = Ppx_deriving.remove_pervasives ~deriver typ in match typ with | _ when Ppx_deriving.free_vars_in_core_type typ = [] -> [%expr fun _ -> ()] | { ptyp_desc = Ptyp_constr _ } -> let builtin = not (Attribute.has_flag ct_attr_nobuiltin typ) in begin match builtin, typ with | true, [%type: [%t? typ] ref] -> [%expr fun x -> [%e expr_of_typ typ] !x] | true, [%type: [%t? typ] list] -> [%expr Ppx_deriving_runtime.List.iter [%e expr_of_typ typ]] | true, [%type: [%t? typ] array] -> [%expr Ppx_deriving_runtime.Array.iter [%e expr_of_typ typ]] | true, [%type: [%t? typ] option] -> [%expr function None -> () | Some x -> [%e expr_of_typ typ] x] | true, [%type: ([%t? ok_t], [%t? err_t]) result] | true, [%type: ([%t? ok_t], [%t? err_t]) Result.result] -> [%expr function | Ok ok -> ignore ([%e expr_of_typ ok_t] ok) | Error err -> ignore ([%e expr_of_typ err_t] err)] | _, { ptyp_desc = Ptyp_constr ({ txt = lid }, args) } -> app (Exp.ident (mknoloc (Ppx_deriving.mangle_lid (`Prefix deriver) lid))) (List.map expr_of_typ args) | _ -> assert false end | { ptyp_desc = Ptyp_tuple typs } -> [%expr fun [%p ptuple (List.mapi (fun i _ -> pvar (argn i)) typs)] -> [%e Ppx_deriving.(fold_exprs seq_reduce (List.mapi (fun i typ -> app (expr_of_typ typ) [evar (argn i)]) typs))]]; | { ptyp_desc = Ptyp_variant (fields, _, _); ptyp_loc } -> let cases = fields |> List.map (fun field -> let variant label popt = Pat.variant label.txt popt in match field.prf_desc with | Rtag(label, true (*empty*), []) -> Exp.case (variant label None) [%expr ()] | Rtag(label, false, [typ]) -> Exp.case (variant label (Some [%pat? x])) [%expr [%e expr_of_typ typ] x] | Rinherit({ ptyp_desc = Ptyp_constr (tname, _) } as typ) -> Exp.case [%pat? [%p Pat.type_ tname] as x] [%expr [%e expr_of_typ typ] x] | _ -> raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s" deriver (Ppx_deriving.string_of_core_type typ)) in Exp.function_ cases | { ptyp_desc = Ptyp_var name } -> [%expr ([%e evar ("poly_"^name)] : [%t Typ.var name] -> unit)] | { ptyp_desc = Ptyp_alias (typ, name) } -> [%expr fun x -> [%e evar ("poly_"^name)] x; [%e expr_of_typ typ] x] | { ptyp_loc } -> raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s" deriver (Ppx_deriving.string_of_core_type typ) and expr_of_label_decl { pld_type; pld_attributes } = let attrs = pld_type.ptyp_attributes @ pld_attributes in expr_of_typ { pld_type with ptyp_attributes = attrs } let str_of_type ({ ptype_loc = loc } as type_decl) = let iterator = match type_decl.ptype_kind, type_decl.ptype_manifest with | Ptype_abstract, Some manifest -> expr_of_typ manifest | Ptype_variant constrs, _ -> constrs |> List.map (fun { pcd_name = { txt = name' }; pcd_args } -> match pcd_args with | Pcstr_tuple(typs) -> let args = List.mapi (fun i typ -> app (expr_of_typ typ) [evar (argn i)]) typs in let result = match args with | [] -> [%expr ()] | args -> Ppx_deriving.(fold_exprs seq_reduce) args in Exp.case (pconstr name' (pattn typs)) result | Pcstr_record(labels) -> let args = labels |> List.map (fun ({ pld_name = { txt = n }; _ } as pld) -> [%expr [%e expr_of_label_decl pld] [%e evar (argl n)]]) in Exp.case (pconstrrec name' (pattl labels)) (Ppx_deriving.(fold_exprs seq_reduce) args) ) |> Exp.function_ | Ptype_record labels, _ -> let fields = labels |> List.mapi (fun i ({ pld_name = { txt = name }; _ } as pld) -> [%expr [%e expr_of_label_decl pld] [%e Exp.field (evar "x") (mknoloc (Lident name))]]) in [%expr fun x -> [%e Ppx_deriving.(fold_exprs seq_reduce) fields]] | Ptype_abstract, None -> raise_errorf ~loc "%s cannot be derived for fully abstract types" deriver | Ptype_open, _ -> raise_errorf ~loc "%s cannot be derived for open types" deriver in let polymorphize = Ppx_deriving.poly_fun_of_type_decl type_decl in [Vb.mk ~attrs:[Ppx_deriving.attr_warning [%expr "-39"]] (pvar (Ppx_deriving.mangle_type_decl (`Prefix deriver) type_decl)) (polymorphize iterator)] let sig_of_type type_decl = let loc = !Ast_helper.default_loc in let typ = Ppx_deriving.core_type_of_type_decl type_decl in let polymorphize = Ppx_deriving.poly_arrow_of_type_decl (fun var -> [%type: [%t var] -> Ppx_deriving_runtime.unit]) type_decl in [Sig.value (Val.mk (mknoloc (Ppx_deriving.mangle_type_decl (`Prefix deriver) type_decl)) (polymorphize [%type: [%t typ] -> Ppx_deriving_runtime.unit]))] let impl_generator = Deriving.Generator.V2.make_noarg (fun ~ctxt:_ (_, type_decls) -> [Str.value Recursive (List.concat (List.map str_of_type type_decls))]) let intf_generator = Deriving.Generator.V2.make_noarg (fun ~ctxt:_ (_, type_decls) -> List.concat (List.map sig_of_type type_decls)) let deriving: Deriving.t = Deriving.add deriver ~str_type_decl:impl_generator ~sig_type_decl:intf_generator
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>