Source file selector.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
open! Base
open Ppxlib

module Per_field = struct
  type t =
    | Getters
    | Setters
    | Names
    | Fields

  let all = [ Getters; Setters; Names; Fields ]

  let to_flag_name = function
    | Getters -> "getters"
    | Setters -> "setters"
    | Names -> "names"
    | Fields -> "fields"
  ;;

  let to_expression t ~loc =
    match t with
    | Getters -> [%expr Getters]
    | Setters -> [%expr Setters]
    | Names -> [%expr Names]
    | Fields -> [%expr Fields]
  ;;
end

module Iterator = struct
  type t =
    | Create
    | Make_creator
    | Exists
    | Fold
    | Fold_right
    | For_all
    | Iter
    | Map
    | To_list
    | Map_poly

  let all =
    [ Create
    ; Make_creator
    ; Exists
    ; Fold
    ; Fold_right
    ; For_all
    ; Iter
    ; Map
    ; To_list
    ; Map_poly
    ]
  ;;

  let to_variable_name = function
    | Create -> "create"
    | Make_creator -> "make_creator"
    | Exists -> "exists"
    | Fold -> "fold"
    | Fold_right -> "fold_right"
    | For_all -> "for_all"
    | Iter -> "iter"
    | Map -> "map"
    | To_list -> "to_list"
    | Map_poly -> "map_poly"
  ;;

  let to_expression t ~loc =
    match t with
    | Create -> [%expr Create]
    | Make_creator -> [%expr Make_creator]
    | Exists -> [%expr Exists]
    | Fold -> [%expr Fold]
    | Fold_right -> [%expr Fold_right]
    | For_all -> [%expr For_all]
    | Iter -> [%expr Iter]
    | Map -> [%expr Map]
    | To_list -> [%expr To_list]
    | Map_poly -> [%expr Map_poly]
  ;;
end

module Direct_iterator = struct
  type t =
    | Exists
    | Fold
    | Fold_right
    | For_all
    | Iter
    | Map
    | To_list
    | Set_all_mutable_fields

  let all =
    [ Exists; Fold; Fold_right; For_all; Iter; Map; To_list; Set_all_mutable_fields ]
  ;;

  let to_variable_name = function
    | Exists -> "exists"
    | Fold -> "fold"
    | Fold_right -> "fold_right"
    | For_all -> "for_all"
    | Iter -> "iter"
    | Map -> "map"
    | To_list -> "to_list"
    | Set_all_mutable_fields -> "set_all_mutable_fields"
  ;;

  let to_expression t ~loc =
    match t with
    | Exists -> [%expr Exists]
    | Fold -> [%expr Fold]
    | Fold_right -> [%expr Fold_right]
    | For_all -> [%expr For_all]
    | Iter -> [%expr Iter]
    | Map -> [%expr Map]
    | To_list -> [%expr To_list]
    | Set_all_mutable_fields -> [%expr Set_all_mutable_fields]
  ;;
end

type t =
  | Per_field of Per_field.t
  | Iterator of Iterator.t
  | Direct_iterator of Direct_iterator.t

let all =
  List.concat
    [ List.map Per_field.all ~f:(fun x -> Per_field x)
    ; List.map Iterator.all ~f:(fun x -> Iterator x)
    ; List.map Direct_iterator.all ~f:(fun x -> Direct_iterator x)
    ]
;;

let to_string = function
  | Per_field x -> "~" ^ Per_field.to_flag_name x
  | Iterator x -> "~iterators:" ^ Iterator.to_variable_name x
  | Direct_iterator x -> "~direct_iterators:" ^ Direct_iterator.to_variable_name x
;;

let sexp_of_t t = Sexp.Atom (to_string t)
let compare = (Poly.compare : t -> t -> int)

include (val Comparator.make ~compare ~sexp_of_t)

let to_expression t ~loc =
  match t with
  | Per_field x ->
    [%expr Ppx_fields_conv.Selector.Per_field [%e Per_field.to_expression x ~loc]]
  | Iterator x ->
    [%expr Ppx_fields_conv.Selector.Iterator [%e Iterator.to_expression x ~loc]]
  | Direct_iterator x ->
    [%expr
      Ppx_fields_conv.Selector.Direct_iterator [%e Direct_iterator.to_expression x ~loc]]
;;

let direct_dependencies = function
  | Per_field (Getters | Setters | Names) -> []
  | Per_field Fields -> [ Per_field Getters; Per_field Setters ]
  | Iterator _ | Direct_iterator _ -> [ Per_field Fields ]
;;

let rec with_dependencies selector =
  selector :: List.concat_map ~f:with_dependencies (direct_dependencies selector)
;;

module type S = sig
  type t

  val all : t list
  val to_variable_name : t -> string
end

let select_id (type a) (module M : S with type t = a) ~arg_name ~f expr =
  match expr.pexp_desc with
  | Pexp_ident { loc; txt = Lident txt } ->
    (match List.find M.all ~f:(fun x -> String.equal txt (M.to_variable_name x)) with
     | Some x -> Ok (f x)
     | None ->
       Error
         ( loc
         , Printf.sprintf
             "[~%s] %s"
             arg_name
             (if String.equal txt arg_name
              then Printf.sprintf "requires an argument"
              else
                Printf.sprintf
                  "does not accept [%s] as an argument, valid arguments are: %s"
                  (Longident.name (Lident txt))
                  (String.concat
                     ~sep:", "
                     (List.map M.all ~f:(fun x ->
                        Printf.sprintf "[%s]" (M.to_variable_name x))))) ))
  | _ -> Error (expr.pexp_loc, "expected a variable name")
;;

let select_id_tuple m ~arg_name ~f expr =
  Result.bind
    (match expr.pexp_desc with
     | Pexp_tuple tuple -> Ok tuple
     | Pexp_ident _ -> Ok [ expr ]
     | _ ->
       Error [ expr.pexp_loc, "expected a variable name or a tuple of variable names" ])
    ~f:(fun exprs ->
      List.map exprs ~f:(select_id m ~arg_name ~f) |> Result.combine_errors)
;;

let select_iterators =
  select_id_tuple ~arg_name:"iterators" ~f:(fun x -> Iterator x) (module Iterator)
;;

let select_direct_iterators =
  select_id_tuple
    ~arg_name:"direct_iterators"
    ~f:(fun x -> Direct_iterator x)
    (module Direct_iterator)
;;

let select_fold_right expr =
  Error
    [ ( expr.pexp_loc
      , "[~fold_right] is no longer supported; use [~iterators:fold_right] and/or \
         [~direct_iterators:fold_right]" )
    ]
;;

let select_one x expr =
  match expr.pexp_desc with
  | Pexp_ident { txt = Lident txt; _ } when String.equal txt (Per_field.to_flag_name x) ->
    Ok [ Per_field x ]
  | _ ->
    Error
      [ ( expr.pexp_loc
        , Printf.sprintf
            "expected no explicit argument to [~%s]"
            (Per_field.to_flag_name x) )
      ]
;;

let select_getters = select_one Getters
let select_setters = select_one Setters
let select_names = select_one Names
let select_fields = select_one Fields

let default_selectors =
  List.filter all ~f:(function
    | Iterator Fold_right | Direct_iterator Fold_right -> false
    | _ -> true)
;;

let selection list ~add_dependencies =
  let list =
    if add_dependencies then List.concat_map list ~f:with_dependencies else list
  in
  Set.Using_comparator.of_list ~comparator list
;;

let error_of_alists ~loc alists =
  match
    List.map (List.concat alists) ~f:(fun (loc, message) ->
      loc, "deriving fields: " ^ message)
  with
  | [ (loc, message) ] -> Location.Error.make ~loc message ~sub:[]
  | sub -> Location.Error.make ~loc "deriving fields: multiple syntax errors" ~sub
;;

let generator ~add_dependencies f =
  Deriving.Generator.V2.make
    (let open Deriving.Args in
     empty
     +> arg "fold_right" (map1 __ ~f:select_fold_right)
     +> arg "getters" (map1 __ ~f:select_getters)
     +> arg "setters" (map1 __ ~f:select_setters)
     +> arg "names" (map1 __ ~f:select_names)
     +> arg "fields" (map1 __ ~f:select_fields)
     +> arg "iterators" (map1 __ ~f:select_iterators)
     +> arg "direct_iterators" (map1 __ ~f:select_direct_iterators))
    (fun ~ctxt ast arg1 arg2 arg3 arg4 arg5 arg6 arg7 ->
       let loc = Expansion_context.Deriver.derived_item_loc ctxt in
       let results =
         match List.filter_opt [ arg1; arg2; arg3; arg4; arg5; arg6; arg7 ] with
         | [] -> [ Ok default_selectors ]
         | _ :: _ as non_empty -> non_empty
       in
       let selection =
         Result.combine_errors results
         |> Result.map ~f:List.concat
         |> Result.map ~f:(selection ~add_dependencies)
         |> Result.map_error ~f:(error_of_alists ~loc)
       in
       f ~ctxt ast selection)
;;

let deriving_clause ~loc list =
  let open Ast_builder.Default in
  if List.is_empty list
  then None
  else (
    let per_field, iterators, direct_iterators =
      List.dedup_and_sort list ~compare
      |> List.partition3_map ~f:(function
        | Per_field x -> `Fst x
        | Iterator x -> `Snd x
        | Direct_iterator x -> `Trd x)
    in
    let per_field =
      List.map per_field ~f:(fun x ->
        let s = Per_field.to_flag_name x in
        Labelled s, evar ~loc s)
    in
    let iterators =
      if List.is_empty iterators
      then []
      else
        [ ( Labelled "iterators"
          , pexp_tuple
              ~loc
              (List.map iterators ~f:(fun f -> evar ~loc (Iterator.to_variable_name f))) )
        ]
    in
    let direct_iterators =
      if List.is_empty direct_iterators
      then []
      else
        [ ( Labelled "direct_iterators"
          , pexp_tuple
              ~loc
              (List.map direct_iterators ~f:(fun f ->
                 evar ~loc (Direct_iterator.to_variable_name f))) )
        ]
    in
    Some
      (pexp_apply
         ~loc
         [%expr fields]
         (List.concat [ per_field; iterators; direct_iterators ])))
;;