Source file ppx_unstring.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
open Base
open! Ppxlib
open Ast_builder.Default
module Unstring_impl : Structure.S with type product_result = expression = struct
type product_result = expression
let product ~loc (product_args : Structure.Product_args.t) ~format =
match product_args with
| Constructor_no_arg { constructor } ->
let base = Format_string.build_unstring format ~loc ~fields:None in
[%expr
Unstring.map [%e base] ~f:(fun () ->
[%e pexp_construct ~loc { loc; txt = Lident constructor } None])]
| Tuple { constructor; args = parsers } ->
let combine args =
let inner = Ppxlib_jane.Ast_builder.Default.pexp_tuple ~loc args in
match constructor with
| None -> inner
| Some c -> pexp_construct ~loc { loc; txt = Lident c } (Some inner)
in
Format_string.build_unstring format ~loc ~fields:(Many { parsers; combine })
| Record { constructor; args } ->
let parsers = List.map args ~f:(fun (label, expr) -> Some label, expr) in
let combine args =
let fields =
List.map args ~f:(fun (label, expr) ->
let label =
Option.value_exn
label
~message:"ppx_unstring bug: record member doesn't have a label"
in
{ loc = expr.pexp_loc; txt = Lident label }, expr)
in
let record = pexp_record ~loc fields None in
match constructor with
| None -> record
| Some c -> pexp_construct ~loc { loc; txt = Lident c } (Some record)
in
Format_string.build_unstring format ~loc ~fields:(Many { parsers; combine })
;;
let sum ~loc = function
| [ parser ] -> parser
| parsers -> [%expr Unstring.choice [%e elist ~loc parsers]]
;;
let variant_row ~loc : Structure.Variant_kind.t -> _ = function
| No_arg { tag; format } ->
let base = Format_string.build_unstring format ~loc ~fields:None in
[%expr Unstring.map [%e base] ~f:(fun () -> [%e pexp_variant ~loc tag None])]
| Arg { tag; expr } ->
[%expr
Unstring.map [%e expr] ~f:(fun x -> [%e pexp_variant ~loc tag (Some [%expr x])])]
| Inherit { expr; _ } -> [%expr Unstring.map [%e expr] ~f:(fun x -> (x :> _))]
;;
let expr_of_format ~loc format ~args =
match args with
| `Zero -> Format_string.build_unstring format ~loc ~fields:None
| `One ->
let parser = [%expr __inner] in
let f = Format_string.build_unstring format ~loc ~fields:(Singleton { parser }) in
[%expr fun __inner -> [%e f]]
;;
let reads_chars_attr = true
let prefix = "unstring"
let expected_type ~loc param = [%type: [%t param] Unstring.t]
let custom_attribute = Unstring_attrs.custom_unstring_core_type
end
module Restring_impl : Structure.S with type product_result = pattern * expression =
struct
type product_result = pattern * expression
let product ~loc (product_args : Structure.Product_args.t) ~format =
match product_args with
| Constructor_no_arg { constructor } ->
let pattern = ppat_construct ~loc { loc; txt = Lident constructor } None in
pattern, Format_string.build_string format ~loc ~hole_string_exprs:[]
| Tuple { constructor; args = restrings } ->
let patterns, exprs =
List.mapi restrings ~f:(fun i (label, restring) ->
let var_name = "__field_" ^ Int.to_string i in
( (label, pvar ~loc var_name)
, (label, [%expr [%e restring] [%e evar ~loc var_name]]) ))
|> List.unzip
in
let tuple_pat = Ppxlib_jane.Ast_builder.Default.ppat_tuple ~loc patterns Closed in
let pattern =
match constructor with
| None -> tuple_pat
| Some c -> ppat_construct ~loc { loc; txt = Lident c } (Some tuple_pat)
in
pattern, Format_string.build_string format ~loc ~hole_string_exprs:exprs
| Record { constructor; args } ->
let patterns, exprs =
List.mapi args ~f:(fun i (label, restring) ->
let var_name = "__field_" ^ Int.to_string i in
( ({ loc; txt = Lident label }, pvar ~loc var_name)
, (Some label, [%expr [%e restring] [%e evar ~loc var_name]]) ))
|> List.unzip
in
let record_pat = ppat_record ~loc patterns Closed in
let pattern =
match constructor with
| None -> record_pat
| Some c -> ppat_construct ~loc { loc; txt = Lident c } (Some record_pat)
in
pattern, Format_string.build_string format ~loc ~hole_string_exprs:exprs
;;
let sum ~loc restrings =
List.map restrings ~f:(fun (lhs, rhs) -> case ~lhs ~guard:None ~rhs)
|> pexp_function ~loc
;;
let variant_row ~loc : Structure.Variant_kind.t -> _ = function
| No_arg { tag; format } ->
( ppat_variant ~loc tag None
, Format_string.build_string format ~loc ~hole_string_exprs:[] )
| Arg { tag; expr } -> ppat_variant ~loc tag (Some [%pat? __x]), [%expr [%e expr] __x]
| Inherit { type_; expr } ->
let type_longident =
match type_.ptyp_desc with
| Ptyp_constr (lid, _) -> lid
| _ ->
Location.raise_errorf
~loc
"ppx_unstring: inherited polymorphic variant must be a named type"
in
( ppat_alias ~loc (ppat_type ~loc type_longident) (Located.mk ~loc "__x")
, [%expr [%e expr] __x] )
;;
let expr_of_format ~loc format ~args =
match args with
| `Zero -> Format_string.build_string format ~loc ~hole_string_exprs:[]
| `One ->
let f =
Format_string.build_string
format
~loc
~hole_string_exprs:[ None, [%expr __f __x] ]
in
[%expr
fun __f ->
();
fun __x -> [%e f]]
;;
let reads_chars_attr = false
let prefix = "restring"
let expected_type ~loc param = [%type: [%t param] -> string]
let custom_attribute = Unstring_attrs.custom_restring_core_type
end
module Unstring = Structure.Make (Unstring_impl)
module Restring = Structure.Make (Restring_impl)
let unstring_payload =
let open Ast_pattern in
map (ptyp __) ~f:(fun f ctyp -> f (`Type ctyp))
||| map (pstr (pstr_eval __ nil ^:: nil)) ~f:(fun f expr -> f (`Expr expr))
;;
let unstring_unified =
Extension.declare
"ppx_unstring.unstring"
Extension.Context.expression
unstring_payload
(fun ~loc ~path:_ payload ->
match payload with
| `Type ctyp ->
Unstring.of_type ~type_params:(Map.empty (module String)) ~loc ctyp
|> Merlin_helpers.hide_expression
| `Expr
{ pexp_desc =
Pexp_constraint
( { pexp_desc = Pexp_constant (Pconst_string (text, string_loc, _)); _ }
, Some type_
, _ )
; _
} ->
let format = { Format_string.text; loc = string_loc } in
Unstring.extension ~loc ~format ~type_ |> Merlin_helpers.hide_expression
| `Expr { pexp_desc = Pexp_constant (Pconst_string (text, string_loc, _)); _ } ->
let format = { Format_string.text; loc = string_loc } in
Unstring.extension ~loc ~format ~type_:[%type: unit]
|> Merlin_helpers.hide_expression
| `Expr _ ->
Location.raise_errorf
~loc
"Expected [%%unstring: type] or [%%unstring (\"format\" : type)]")
;;
let unstring_with_restring =
Extension.declare
"ppx_unstring.unstring.with_restring"
Extension.Context.expression
unstring_payload
(fun ~loc ~path:_ payload ->
let unstring_expr, restring_expr =
match payload with
| `Type type_ ->
let type_params = Map.empty (module String) in
( Unstring.of_type ~type_params ~loc type_
, Restring.of_type ~type_params ~loc type_ )
| `Expr
{ pexp_desc =
Pexp_constraint
( { pexp_desc = Pexp_constant (Pconst_string (text, string_loc, _)); _ }
, Some type_
, _ )
; _
} ->
let format = { Format_string.text; loc = string_loc } in
Unstring.extension ~loc ~format ~type_, Restring.extension ~loc ~format ~type_
| `Expr { pexp_desc = Pexp_constant (Pconst_string (text, string_loc, _)); _ } ->
let format = { Format_string.text; loc = string_loc } in
( Unstring.extension ~loc ~format ~type_:[%type: unit]
, Restring.extension ~loc ~format ~type_:[%type: unit] )
| `Expr _ ->
Location.raise_errorf
~loc
"Expected [%%unstring.with_restring: type] or [%%unstring.with_restring \
(\"format\" : type)]"
in
[%expr [%e unstring_expr], [%e restring_expr]] |> Merlin_helpers.hide_expression)
;;
let () =
Ppxlib.Driver.register_transformation
"ppx_unstring"
~extensions:[ unstring_unified; unstring_with_restring ]
;;
module Deriver = struct
let of_string_name_of_type_name type_name =
if String.equal type_name "t" then "of_string" else type_name ^ "_of_string"
;;
module On_ambiguity = struct
type t =
| Raise
| Raise_opaque
| Pick_first
let of_expression_exn (expr : expression) =
match expr.pexp_desc with
| Pexp_variant ("Raise", None) -> Raise
| Pexp_variant ("Pick_first", None) -> Pick_first
| Pexp_variant ("Raise_opaque", None) -> Raise_opaque
| _ ->
Location.raise_errorf
~loc:expr.pexp_loc
"Expected `Raise, `Pick_first, or `Raise_opaque"
;;
end
let generate_of_string_binding ~loc ~type_decl ~on_ambiguity =
(match type_decl.ptype_params with
| _ :: _ ->
Location.raise_errorf ~loc "~of_string is not supported for parameterized types"
| [] -> ());
let type_name = type_decl.ptype_name.txt in
let binding_name = of_string_name_of_type_name type_name in
let on_ambiguity_expr =
match on_ambiguity with
| On_ambiguity.Raise ->
let result_type = ptyp_constr ~loc { txt = Lident type_name; loc } [] in
let sexp_of_expr =
pexp_extension ~loc ({ txt = "sexp_of"; loc }, PTyp result_type)
in
[%expr `Raise [%e sexp_of_expr]]
| Raise_opaque -> [%expr `Raise (fun _ -> Sexp.Atom "<opaque>")]
| Pick_first -> [%expr `Pick_first]
in
let of_string_expr =
[%expr
Staged.unstage
(Unstring.make_of_string
[%e evar ~loc (Unstring.binding_name type_name)]
~compile_when:`Lazily
~on_ambiguity:[%e on_ambiguity_expr])]
in
pstr_value
~loc
Nonrecursive
[ value_binding ~loc ~pat:(pvar ~loc binding_name) ~expr:of_string_expr ]
;;
let generate_unstring ~loc ~type_decl ~of_string ~on_ambiguity ~restring =
let unstring_items = [ Unstring.deriver ~type_decl ~loc ] in
let of_string_items =
match of_string, on_ambiguity with
| false, None -> []
| false, Some _ -> Location.raise_errorf ~loc "~on_ambiguity requires ~of_string"
| true, None -> Location.raise_errorf ~loc "~of_string requires ~on_ambiguity"
| true, Some on_ambiguity_expr ->
let on_ambiguity = On_ambiguity.of_expression_exn on_ambiguity_expr in
[ generate_of_string_binding ~loc ~type_decl ~on_ambiguity ]
in
let restring_items =
match restring with
| false -> []
| true -> [ Restring.deriver ~type_decl ~loc ]
in
unstring_items @ of_string_items @ restring_items
;;
let generator =
Deriving.Generator.V2.make
Deriving.Args.(
empty +> flag "of_string" +> arg "on_ambiguity" __ +> flag "restring")
(fun ~ctxt (_, type_decls) of_string on_ambiguity restring ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
match type_decls with
| [ type_decl ] ->
generate_unstring ~loc ~type_decl ~of_string ~on_ambiguity ~restring
| _ -> Location.raise_errorf ~loc "unstring deriving expects exactly one type")
;;
end
module Signature = struct
let result_type_of_type_decl ~loc type_decl =
let type_name = type_decl.ptype_name.txt in
let params = List.map type_decl.ptype_params ~f:(fun (core_type, _) -> core_type) in
match params with
| [] -> ptyp_constr ~loc { txt = Lident type_name; loc } []
| _ -> ptyp_constr ~loc { txt = Lident type_name; loc } params
;;
let generate_of_string_sig_item ~loc ~type_decl =
let binding_name = Deriver.of_string_name_of_type_name type_decl.ptype_name.txt in
let result_type = result_type_of_type_decl ~loc type_decl in
let staged_fn_type = [%type: string -> [%t result_type]] in
psig_value
~loc
(value_description
~loc
~name:{ txt = binding_name; loc }
~type_:staged_fn_type
~prim:[])
;;
let generate_sig ~loc ~type_decl ~of_string ~restring =
let unstring_item = Unstring.sig_item ~loc ~type_decl in
let of_string_items =
match of_string with
| false -> []
| true -> [ generate_of_string_sig_item ~loc ~type_decl ]
in
let restring_items =
match restring with
| false -> []
| true -> [ Restring.sig_item ~loc ~type_decl ]
in
[ unstring_item ] @ of_string_items @ restring_items
;;
let generator =
Deriving.Generator.V2.make
Deriving.Args.(empty +> flag "of_string" +> flag "restring")
(fun ~ctxt (_, type_decls) of_string restring ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
match type_decls with
| [ type_decl ] -> generate_sig ~loc ~type_decl ~of_string ~restring
| _ -> Location.raise_errorf ~loc "unstring deriving expects exactly one type")
;;
end
let () =
Deriving.add
"unstring"
~str_type_decl:Deriver.generator
~sig_type_decl:Signature.generator
|> Deriving.ignore
;;