Source file ppx_string_conv.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
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
open! Base
open! Ppxlib
module A = Ast_builder.Default
let ppx_name = "ppx_string_conv"
module Fn_name = struct
type t =
| To_string
| Of_string
let for_type t ~type_name =
match t, type_name with
| To_string, "t" -> "to_string"
| Of_string, "t" -> "of_string"
| To_string, _ -> [%string "string_of_%{type_name}"]
| Of_string, _ -> [%string "%{type_name}_of_string"]
;;
let call' t ~(type_ : longident_loc) ~(args : expression list) =
let loc = type_.loc in
match type_.txt with
| Lident "string" ->
(match args with
| [ expr ] -> expr
| _ -> A.eapply ~loc [%expr fun s -> s] args)
| _ ->
A.type_constr_conv type_ ~loc ~f:(fun type_name -> for_type t ~type_name) args
;;
let call t arg ~type_ = call' t ~type_ ~args:[ arg ]
let get_function t ~type_ = call' t ~type_ ~args:[]
end
module What_to_generate = struct
type t =
| Only of Fn_name.t (** [to_string] or [of_string] *)
| Both (** just [string] *)
let build t ~f =
let fns =
match t with
| Only fn -> [ fn ]
| Both -> [ To_string; Of_string ]
in
List.map fns ~f
;;
end
let type_of_decl (decl : type_declaration) : core_type =
let loc = decl.ptype_name.loc in
A.ptyp_constr ~loc (Loc.make ~loc (Lident decl.ptype_name.txt)) []
;;
let error_ext ~loc message : extension =
( Loc.make ~loc "ocaml.error"
, PStr [ A.pstr_eval ~loc (A.estring ~loc [%string "%{ppx_name}: %{message}"]) [] ] )
;;
let psig_error ~loc message = A.psig_extension ~loc (error_ext ~loc message) []
let pstr_error ~loc message = A.pstr_extension ~loc (error_ext ~loc message) []
let lstring (loc : string loc) = A.estring ~loc:loc.loc loc.txt
let make_stringable_sig_for_type ~loc ~what_to_generate (decl : type_declaration)
: signature
=
match List.is_empty decl.ptype_params with
| false ->
let loc = decl.ptype_loc in
[ psig_error ~loc "types with parameters not supported" ]
| true ->
let t = type_of_decl decl in
let make_val fn_name sig_ =
A.psig_value
~loc
(A.value_description
~loc
~name:
(Loc.map decl.ptype_name ~f:(fun type_name ->
Fn_name.for_type fn_name ~type_name))
~prim:[]
~type_:(sig_ t))
in
let build_fn (fn_name : Fn_name.t) =
let type_ t =
match fn_name with
| To_string -> [%type: [%t t] -> string]
| Of_string -> [%type: string -> [%t t]]
in
make_val fn_name type_
in
What_to_generate.build what_to_generate ~f:build_fn
;;
let generate_intf ~what_to_generate =
Deriving.Generator.V2.make_noarg (fun ~ctxt (_rec_flag, types) ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
List.concat_map types ~f:(make_stringable_sig_for_type ~loc ~what_to_generate))
;;
module Constructor_kind = struct
type t =
| Base_string of { name : string Loc.t option }
| Fallback
| Nested of { prefix : string Loc.t option }
module Attr = struct
let fallback =
Attribute.declare_with_attr_loc
"stringable.fallback"
Attribute.Context.constructor_declaration
Ast_pattern.(pstr nil)
(fun ~attr_loc:loc -> loc)
;;
let nested =
Attribute.declare_with_attr_loc
"stringable.nested"
Attribute.Context.constructor_declaration
Ast_pattern.(single_expr_payload (estring __'))
(fun ~attr_loc:loc prefix -> loc, prefix)
;;
let rename =
Attribute.declare
"stringable.rename"
Attribute.Context.constructor_declaration
Ast_pattern.(single_expr_payload (estring __'))
(fun s -> s)
;;
end
let get (constr : constructor_declaration) =
let fallback = Attribute.get Attr.fallback constr in
let nested = Attribute.get Attr.nested constr in
let rename = Attribute.get Attr.rename constr in
let multiple_attribute_error ~loc =
Error
(error_ext ~loc "at most one of 'rename', 'nested' and 'fallback' may be used")
in
match fallback, nested, rename with
| Some _, None, None -> Ok Fallback
| None, Some (_, prefix), None ->
let prefix = if String.is_empty prefix.txt then None else Some prefix in
Ok (Nested { prefix })
| None, None, name -> Ok (Base_string { name })
| Some loc, _, _ | _, Some (loc, _), _ -> multiple_attribute_error ~loc
;;
end
module Variant_impl = struct
type t =
{ loc : Location.t
; code_path : Code_path.t
; case_insensitive : bool
; capitalization : Capitalization.t option
; decl : type_declaration
; base_string_variants : (constructor_declaration * string Loc.t) Queue.t
; nested_variants :
(constructor_declaration * longident_loc * string Loc.t option) Queue.t
; mutable fallback_variant : (constructor_declaration * longident_loc) option
; errors : extension Queue.t
}
let capitalization_of_string deriving_arg =
match deriving_arg with
| None -> Ok None
| Some s ->
(try Ok (Some (Capitalization.of_string s)) with
| _ ->
let can_be = Lazy.force Capitalization.can_be |> String.concat ~sep:", " in
Error [%string "invalid capitalize argument: (can be: %{can_be})"])
;;
let create' ~loc ~code_path ~case_insensitive ~capitalize_string ~decl =
let%map.Result capitalization = capitalization_of_string capitalize_string in
{ loc
; code_path
; case_insensitive
; capitalization
; decl
; base_string_variants = Queue.create ()
; nested_variants = Queue.create ()
; fallback_variant = None
; errors = Queue.create ()
}
;;
let parse_base_string t ~constr ~name =
match (constr : constructor_declaration) with
| { pcd_args = Pcstr_tuple []; pcd_vars = []; pcd_res = None; pcd_name; _ } ->
Ok
(Option.value_or_thunk name ~default:(fun () ->
match t.capitalization with
| None -> pcd_name
| Some capitalization ->
Loc.map pcd_name ~f:(Capitalization.apply_to_snake_case capitalization)))
| _ -> Error (error_ext ~loc:constr.pcd_loc "expected unit variant without arguments")
;;
let get_nested_type constr =
match (constr : constructor_declaration) with
| { pcd_args = Pcstr_tuple [ ty ]; pcd_vars = []; pcd_res = None; _ } ->
(match ty with
| { ptyp_desc = Ptyp_constr (nested_type, []); _ } -> Ok nested_type
| _ -> Error (error_ext ~loc:ty.ptyp_loc "types with parameters not supported"))
| _ ->
Error
(error_ext
~loc:constr.pcd_loc
[%string "expected exactly one argument ([| %{constr.pcd_name.txt} of ...])"])
;;
let add_variant t constr =
match%bind.Result Constructor_kind.get constr with
| Base_string { name } ->
let%map.Result name_to_use = parse_base_string t ~constr ~name in
Queue.enqueue t.base_string_variants (constr, name_to_use)
| Fallback ->
let%bind.Result type_ = get_nested_type constr in
(match t.fallback_variant with
| None ->
t.fallback_variant <- Some (constr, type_);
Ok ()
| Some _ ->
Error
(error_ext ~loc:constr.pcd_loc "'fallback' can only be used on one variant"))
| Nested { prefix } ->
let%map.Result nested_type = get_nested_type constr in
Queue.enqueue t.nested_variants (constr, nested_type, prefix)
;;
let create ~loc ~code_path ~case_insensitive ~capitalize_string ~decl ~constructors =
let%map.Result t =
create' ~loc ~code_path ~case_insensitive ~capitalize_string ~decl
in
List.iter constructors ~f:(fun constructor ->
match add_variant t constructor with
| Ok () -> ()
| Error error -> Queue.enqueue t.errors error);
t
;;
let build_function t ~fn_name ~types ~arg_name ~match_ ~cases =
let loc = t.loc in
let fn_name =
A.pvar ~loc (Fn_name.for_type fn_name ~type_name:t.decl.ptype_name.txt)
in
let base_type = type_of_decl t.decl in
let arg, ret = types base_type in
[%stri
let [%p fn_name] =
fun ([%p arg_name] : [%t arg]) : [%t ret] -> [%e A.pexp_match ~loc match_ cases]
;;]
;;
let render_to_string t =
let loc = t.loc in
let build_case ?args constr ~body =
A.case ~lhs:(A.pconstruct constr args) ~guard:None ~rhs:body
in
let build_base_string (constr, text) =
let string_lit = A.estring ~loc:text.Loc.loc text.txt in
build_case constr ~body:string_lit
in
let build_nested (constr, nested_type, prefix) =
let body x =
let unprefixed = Fn_name.call ~type_:nested_type To_string x in
match prefix with
| None -> unprefixed
| Some prefix -> [%expr Base.String.( ^ ) [%e lstring prefix] [%e unprefixed]]
in
build_case constr ~args:[%pat? x] ~body:(body [%expr x])
in
let build_fallback (constr, nested_type) = build_nested (constr, nested_type, None) in
let maybe_error_wildcard =
match Queue.is_empty t.errors with
| true -> []
| false -> [ A.case ~lhs:[%pat? _] ~guard:None ~rhs:[%expr assert false] ]
in
build_function
t
~fn_name:To_string
~types:(fun t -> t, [%type: string])
~arg_name:[%pat? t]
~match_:[%expr t]
~cases:
(List.join
[ Queue.to_list t.base_string_variants |> List.map ~f:build_base_string
; Queue.to_list t.nested_variants |> List.map ~f:build_nested
; Option.to_list t.fallback_variant |> List.map ~f:build_fallback
; maybe_error_wildcard
])
;;
let build_raise_for_of_string t ~value =
let loc = t.loc in
let fn_name = Fn_name.for_type Of_string ~type_name:t.decl.ptype_name.txt in
let path = Code_path.fully_qualified_path t.code_path in
let message = A.estring ~loc [%string "%{path}.%{fn_name}: invalid string"] in
[%expr
Base.raise_s (List [ Atom [%e message]; List [ Atom "value"; Atom [%e value] ] ])]
;;
let render_of_string t =
let loc = t.loc in
let build_base_string (constr, text) =
let string_pat =
A.pstring
~loc:text.Loc.loc
(if t.case_insensitive then String.lowercase text.txt else text.txt)
in
A.case ~lhs:string_pat ~guard:None ~rhs:(A.econstruct constr None)
in
let call_nested ~constr ~nested_type x =
A.econstruct constr (Some (Fn_name.call ~type_:nested_type Of_string x))
in
let build_fallback expr =
match t.fallback_variant with
| Some (constr, type_) ->
call_nested expr ~constr ~nested_type:type_
| None ->
build_raise_for_of_string t ~value:expr
in
let build_nested_unprefixed ~constr ~nested_type ~expr ~or_else =
[%expr
try [%e call_nested ~constr ~nested_type expr] with
| _ -> [%e or_else]]
in
let build_nested_prefixed ~constr ~nested_type ~expr:s ~or_else ~prefix =
match t.case_insensitive with
| false ->
[%expr
match Base.String.chop_prefix [%e s] ~prefix:[%e lstring prefix] with
| Some x -> [%e call_nested ~constr ~nested_type [%expr x]]
| None -> [%e or_else]]
| true ->
let prefix_length = A.eint ~loc:prefix.loc (String.length prefix.txt) in
[%expr
match Base.String.Caseless.is_prefix [%e s] ~prefix:[%e lstring prefix] with
| true ->
[%e
call_nested
~constr
~nested_type
[%expr String.drop_prefix [%e s] [%e prefix_length]]]
| false -> [%e or_else]]
in
let build_nested ~constr ~nested_type ~expr ~or_else ~prefix =
match prefix with
| None -> build_nested_unprefixed ~constr ~nested_type ~expr ~or_else
| Some prefix -> build_nested_prefixed ~constr ~nested_type ~expr ~or_else ~prefix
in
let fallback_case_body expr =
List.fold_right
(Queue.to_list t.nested_variants)
~f:(fun (constr, nested_type, prefix) or_else ->
build_nested ~constr ~nested_type ~or_else ~expr ~prefix)
~init:(build_fallback expr)
in
build_function
t
~fn_name:Of_string
~types:(fun t -> [%type: string], t)
~arg_name:[%pat? s]
~match_:
(match t.case_insensitive with
| false -> [%expr s]
| true -> [%expr Base.String.lowercase s])
~cases:
((Queue.to_list t.base_string_variants |> List.map ~f:build_base_string)
@ [ A.case ~lhs:[%pat? _] ~guard:None ~rhs:(fallback_case_body [%expr s]) ])
;;
let render_errors t =
let loc = t.loc in
Queue.to_list t.errors |> List.map ~f:(fun error -> A.pstr_extension ~loc error [])
;;
let render t ~what_to_generate =
What_to_generate.build what_to_generate ~f:(function
| To_string -> render_to_string t
| Of_string -> render_of_string t)
@ render_errors t
;;
end
let build_alias_impl ~(type_name : string loc) ~(to_ : core_type) ~loc ~what_to_generate =
match to_ with
| { ptyp_desc = Ptyp_constr (other_type, []); _ } ->
let build_fn fn_name =
let pat_fn_name = A.pvar ~loc (Fn_name.for_type fn_name ~type_name:type_name.txt) in
let other_fn = Fn_name.get_function fn_name ~type_:other_type in
[%stri let [%p pat_fn_name] = [%e other_fn]]
in
What_to_generate.build what_to_generate ~f:build_fn
| _ -> [ pstr_error ~loc:to_.ptyp_loc "types with parameters not supported" ]
;;
let build_impl_or_error
~loc
~code_path
~case_insensitive
~capitalize_string
~what_to_generate
(decl : type_declaration)
=
match List.is_empty decl.ptype_params with
| false -> Error "types with parameters not supported"
| true ->
(match decl.ptype_kind, decl.ptype_manifest with
| Ptype_record _, _ -> Error "records not supported"
| Ptype_variant constructors, _ ->
let%map.Result impl =
Variant_impl.create
~loc
~code_path
~case_insensitive
~capitalize_string
~decl
~constructors
in
Variant_impl.render impl ~what_to_generate
| Ptype_abstract, Some type_ ->
Ok (build_alias_impl ~loc ~type_name:decl.ptype_name ~to_:type_ ~what_to_generate)
| Ptype_abstract, None -> Error "abstract types not supported"
| Ptype_open, _ -> Error "open types not supported")
;;
let build_impl ~loc ~code_path ~case_insensitive ~capitalize_string ~what_to_generate decl
=
match
build_impl_or_error
~loc
~code_path
~case_insensitive
~capitalize_string
~what_to_generate
decl
with
| Error message -> [ pstr_error ~loc message ]
| Ok x -> x
;;
let generate_impl ~what_to_generate =
Deriving.Generator.V2.make
Deriving.Args.(empty +> flag "case_insensitive" +> arg "capitalize" (estring __))
(fun ~ctxt (_rec_flag, types) case_insensitive capitalize_string ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
let code_path = Expansion_context.Deriver.code_path ctxt in
List.concat_map
types
~f:
(build_impl
~loc
~code_path
~case_insensitive
~capitalize_string
~what_to_generate))
;;
let add_deriving ~name ~what_to_generate =
Deriving.add
name
~sig_type_decl:(generate_intf ~what_to_generate)
~str_type_decl:(generate_impl ~what_to_generate)
|> Deriving.ignore
;;
let () =
add_deriving ~name:"string" ~what_to_generate:Both;
add_deriving ~name:"to_string" ~what_to_generate:(Only To_string);
add_deriving ~name:"of_string" ~what_to_generate:(Only Of_string)
;;