| 1 | open! Base |
| 2 | open! Ppxlib |
| 3 | open Attrs |
| 4 | |
| 5 | module Generic = struct |
| 6 | type 'specific t = |
| 7 | | Omit_nil |
| 8 | | Sexp_array of core_type |
| 9 | | Sexp_bool |
| 10 | | Sexp_list of core_type |
| 11 | | Sexp_option of core_type |
| 12 | | Specific of 'specific |
| 13 | end |
| 14 | |
| 15 | open Generic |
| 16 | |
| 17 | let get_attribute attr ld ~f = |
| 18 | Option.map (Attribute.get attr ld) ~f:(fun x -> f x, Attribute.name attr) |
| 19 | ;; |
| 20 | |
| 21 | let create ~loc specific_getters ld ~if_no_attribute = |
| 22 | let generic_getters = |
| 23 | [ get_attribute omit_nil ~f:(fun () -> Omit_nil) |
| 24 | ; (fun ld -> |
| 25 | match ld.pld_type with |
| 26 | | ty when Option.is_some (Attribute.get bool ld) -> |
| 27 | (match ty with |
| 28 | | [%type: bool] -> Some (Sexp_bool, "[@sexp.bool]") |
| 29 | | _ -> invalid_attribute ~loc bool "bool") |
| 30 | | ty when Option.is_some (Attribute.get option ld) -> |
| 31 | (match ty with |
| 32 | | [%type: [%t? ty] option] -> Some (Sexp_option ty, "[@sexp.option]") |
| 33 | | _ -> invalid_attribute ~loc option "_ option") |
| 34 | | ty when Option.is_some (Attribute.get list ld) -> |
| 35 | (match ty with |
| 36 | | [%type: [%t? ty] list] -> Some (Sexp_list ty, "[@sexp.list]") |
| 37 | | _ -> invalid_attribute ~loc list "_ list") |
| 38 | | ty when Option.is_some (Attribute.get array ld) -> |
| 39 | (match ty with |
| 40 | | [%type: [%t? ty] array] -> Some (Sexp_array ty, "[@sexp.array]") |
| 41 | | _ -> invalid_attribute ~loc array "_ array") |
| 42 | | _ -> None) |
| 43 | ] |
| 44 | in |
| 45 | let getters = |
| 46 | let wrapped_getters = |
| 47 | List.map specific_getters ~f:(fun get ld -> |
| 48 | Option.map (get ld) ~f:(fun (specific, string) -> Specific specific, string)) |
| 49 | in |
| 50 | List.concat [ wrapped_getters; generic_getters ] |
| 51 | in |
| 52 | match List.filter_map getters ~f:(fun f -> f ld) with |
| 53 | | [] -> Specific if_no_attribute |
| 54 | | [ (v, _) ] -> v |
| 55 | | _ :: _ :: _ as attributes -> |
| 56 | Location.raise_errorf |
| 57 | ~loc |
| 58 | "The following elements are mutually exclusive: %s" |
| 59 | (String.concat ~sep:" " (List.map attributes ~f:snd)) |
| 60 | ;; |
| 61 | |
| 62 | let strip_attributes = |
| 63 | object |
| 64 | inherit Ast_traverse.map |
| 65 | method! attributes _ = [] |
| 66 | end |
| 67 | ;; |
| 68 | |
| 69 | let lift_default ~loc ld expr = |
| 70 | let ty = strip_attributes#core_type ld.pld_type in |
| 71 | Lifted.create ~loc ~prefix:"default" ~ty expr |
| 72 | ;; |
| 73 | |
| 74 | let lift_drop_default ~loc ld expr = |
| 75 | let ty = strip_attributes#core_type ld.pld_type in |
| 76 | Lifted.create |
| 77 | ~loc |
| 78 | ~prefix:"drop_default" |
| 79 | ~ty:[%type: [%t ty] -> [%t ty] -> Stdlib.Bool.t] |
| 80 | expr |
| 81 | ;; |
| 82 | |
| 83 | let lift_drop_if ~loc ld expr = |
| 84 | let ty = strip_attributes#core_type ld.pld_type in |
| 85 | Lifted.create ~loc ~prefix:"drop_if" ~ty:[%type: [%t ty] -> Stdlib.Bool.t] expr |
| 86 | ;; |
| 87 | |
| 88 | module Of_sexp = struct |
| 89 | type t = |
| 90 | | Default of expression Lifted.t |
| 91 | | Required |
| 92 | |
| 93 | let create ~loc ld = |
| 94 | create |
| 95 | ~loc |
| 96 | [ get_attribute default ~f:(fun { to_lift = default } -> |
| 97 | Default (lift_default ~loc ld default)) |
| 98 | ] |
| 99 | ld |
| 100 | ~if_no_attribute:Required |
| 101 | ;; |
| 102 | end |
| 103 | |
| 104 | module Sexp_of = struct |
| 105 | module Drop = struct |
| 106 | type t = |
| 107 | | No_arg |
| 108 | | Compare |
| 109 | | Equal |
| 110 | | Sexp |
| 111 | | Func of expression Lifted.t |
| 112 | end |
| 113 | |
| 114 | type t = |
| 115 | | Drop_default of Drop.t |
| 116 | | Drop_if of expression Lifted.t |
| 117 | | Keep |
| 118 | |
| 119 | let create ~loc ld = |
| 120 | create |
| 121 | ~loc |
| 122 | [ get_attribute drop_default ~f:(function |
| 123 | | None -> Drop_default No_arg |
| 124 | | Some { to_lift = e } -> Drop_default (Func (lift_drop_default ~loc ld e))) |
| 125 | ; get_attribute drop_default_equal ~f:(fun () -> Drop_default Equal) |
| 126 | ; get_attribute drop_default_compare ~f:(fun () -> Drop_default Compare) |
| 127 | ; get_attribute drop_default_sexp ~f:(fun () -> Drop_default Sexp) |
| 128 | ; get_attribute drop_if ~f:(fun { to_lift = x } -> Drop_if (lift_drop_if ~loc ld x)) |
| 129 | ] |
| 130 | ld |
| 131 | ~if_no_attribute:Keep |
| 132 | ;; |
| 133 | end |