Source file extension.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
open! Import
open Common
type (_, _) equality = Eq : ('a, 'a) equality | Ne : (_, _) equality
module Context = struct
type 'a t =
| Class_expr : class_expr t
| Class_field : class_field t
| Class_type : class_type t
| Class_type_field : class_type_field t
| Core_type : core_type t
| Expression : expression t
| Module_expr : module_expr t
| Module_type : module_type t
| Pattern : pattern t
| Signature_item : signature_item t
| Structure_item : structure_item t
| Ppx_import : type_declaration t
type packed = T : _ t -> packed
let class_expr = Class_expr
let class_field = Class_field
let class_type = Class_type
let class_type_field = Class_type_field
let core_type = Core_type
let expression = Expression
let module_expr = Module_expr
let module_type = Module_type
let pattern = Pattern
let signature_item = Signature_item
let structure_item = Structure_item
let desc : type a. a t -> string = function
| Class_expr -> "class expression"
| Class_field -> "class field"
| Class_type -> "class type"
| Class_type_field -> "class type field"
| Core_type -> "core type"
| Expression -> "expression"
| Module_expr -> "module expression"
| Module_type -> "module type"
| Pattern -> "pattern"
| Signature_item -> "signature item"
| Structure_item -> "structure item"
| Ppx_import -> "type declaration"
let eq : type a b. a t -> b t -> (a, b) equality =
fun a b ->
match (a, b) with
| Class_expr, Class_expr -> Eq
| Class_field, Class_field -> Eq
| Class_type, Class_type -> Eq
| Class_type_field, Class_type_field -> Eq
| Core_type, Core_type -> Eq
| Expression, Expression -> Eq
| Module_expr, Module_expr -> Eq
| Module_type, Module_type -> Eq
| Pattern, Pattern -> Eq
| Signature_item, Signature_item -> Eq
| Structure_item, Structure_item -> Eq
| Ppx_import, Ppx_import -> Eq
| _ ->
assert (Poly.( <> ) (T a) (T b));
Ne
let get_ppx_import_extension type_decl =
match type_decl with
| { ptype_manifest = Some { ptyp_desc = Ptyp_extension (name, _); _ }; _ }
->
let virtual_payload =
Ast_builder.Default.pstr_type ~loc:type_decl.ptype_loc Recursive
[ type_decl ]
in
let attr = [] in
Some ((name, PStr [ virtual_payload ]), attr)
| _ -> None
let get_extension : type a. a t -> a -> (extension * attributes) option =
fun t x ->
match (t, x) with
| Class_expr, { pcl_desc = Pcl_extension e; pcl_attributes = a; _ } ->
Some (e, a)
| Class_field, { pcf_desc = Pcf_extension e; pcf_attributes = a; _ } ->
Some (e, a)
| Class_type, { pcty_desc = Pcty_extension e; pcty_attributes = a; _ } ->
Some (e, a)
| Class_type_field, { pctf_desc = Pctf_extension e; pctf_attributes = a; _ }
->
Some (e, a)
| Core_type, { ptyp_desc = Ptyp_extension e; ptyp_attributes = a; _ } ->
Some (e, a)
| Expression, { pexp_desc = Pexp_extension e; pexp_attributes = a; _ } ->
Some (e, a)
| Module_expr, { pmod_desc = Pmod_extension e; pmod_attributes = a; _ } ->
Some (e, a)
| Module_type, { pmty_desc = Pmty_extension e; pmty_attributes = a; _ } ->
Some (e, a)
| Pattern, { ppat_desc = Ppat_extension e; ppat_attributes = a; _ } ->
Some (e, a)
| Signature_item, { psig_desc = Psig_extension (e, a); _ } -> Some (e, a)
| Structure_item, { pstr_desc = Pstr_extension (e, a); _ } -> Some (e, a)
| Ppx_import, type_decl -> get_ppx_import_extension type_decl
| _ -> None
let merge_attributes_res : type a.
a t -> a -> attributes -> (a, Location.Error.t NonEmptyList.t) result =
fun t x attrs ->
match t with
| Class_expr -> Ok { x with pcl_attributes = x.pcl_attributes @ attrs }
| Class_field -> Ok { x with pcf_attributes = x.pcf_attributes @ attrs }
| Class_type -> Ok { x with pcty_attributes = x.pcty_attributes @ attrs }
| Class_type_field ->
Ok { x with pctf_attributes = x.pctf_attributes @ attrs }
| Core_type -> Ok { x with ptyp_attributes = x.ptyp_attributes @ attrs }
| Expression -> Ok { x with pexp_attributes = x.pexp_attributes @ attrs }
| Module_expr -> Ok { x with pmod_attributes = x.pmod_attributes @ attrs }
| Module_type -> Ok { x with pmty_attributes = x.pmty_attributes @ attrs }
| Pattern -> Ok { x with ppat_attributes = x.ppat_attributes @ attrs }
| Signature_item -> (
match attributes_errors attrs with [] -> Ok x | t :: q -> Error (t, q))
| Structure_item -> (
match attributes_errors attrs with [] -> Ok x | t :: q -> Error (t, q))
| Ppx_import -> (
match attributes_errors attrs with [] -> Ok x | t :: q -> Error (t, q))
let merge_attributes : type a. a t -> a -> attributes -> a =
fun t x attrs ->
merge_attributes_res t x attrs
|> Result.handle_error ~f:(fun (err, _) -> Location.Error.raise err)
end
let registrar =
Name.Registrar.create ~kind:"extension" ~current_file:__FILE__
~string_of_context:(fun (Context.T ctx) -> Some (Context.desc ctx))
module Make (Callback : sig
type 'a t
end) =
struct
type ('a, 'b) payload_parser =
| Payload_parser :
('a, 'b, 'c) Ast_pattern.t * 'b Callback.t
-> ('a, 'c) payload_parser
type ('context, 'payload) t = {
name : Name.Pattern.t;
context : 'context Context.t;
payload : (payload, 'payload) payload_parser;
with_arg : bool;
}
let declare : type a.
with_arg:bool ->
string ->
a Context.t ->
(payload, 'b, 'payload) Ast_pattern.t ->
'b Callback.t ->
(a, 'payload) t =
fun ~with_arg name context pattern k ->
(match context with
| Context.Ppx_import ->
Name.Registrar.check_collisions registrar (Context.T Core_type) name
| Context.Core_type ->
Name.Registrar.check_collisions registrar (Context.T Ppx_import) name
| _ -> ());
Name.Registrar.register ~kind:`Extension registrar (Context.T context) name;
{
name = Name.Pattern.make name;
context;
payload = Payload_parser (pattern, k);
with_arg;
}
let find ts (ext : extension) =
let { txt = name; loc } = fst ext in
let name, arg = Name.split_path name in
match List.filter ts ~f:(fun t -> Name.Pattern.matches t.name name) with
| [] -> Ok None
| _ :: _ :: _ as l ->
Error
( Location.Error.createf ~loc "Multiple match for extensions: %s"
(String.concat ~sep:", "
(List.map l ~f:(fun t -> Name.Pattern.name t.name))),
[] )
| [ t ] ->
if (not t.with_arg) && Option.is_some arg then
Error
( Location.Error.createf ~loc
"Extension %s doesn't expect a path argument" name,
[] )
else
let arg =
Option.map arg ~f:(fun s ->
let shift = String.length name + 1 in
let start = loc.loc_start in
{
txt = Longident.parse s;
loc =
{
loc with
loc_start =
{ start with pos_cnum = start.pos_cnum + shift };
};
})
in
Ok (Some (t, arg))
end
module Expert = struct
include Make (struct
type 'a t = arg:Longident.t Loc.t option -> 'a
end)
let declare_with_path_arg name ctx patt f =
declare ~with_arg:true name ctx patt f
let declare name ctx patt f =
declare ~with_arg:false name ctx patt (fun ~arg:_ -> f)
let convert_res ts ~loc ext =
let open Result in
find ts ext >>= fun r ->
match r with
| None -> Ok None
| Some ({ payload = Payload_parser (pattern, f); _ }, arg) ->
Ast_pattern.parse_res pattern loc (snd ext) (f ~arg) >>| fun payload ->
Some payload
let convert ts ~loc ext =
convert_res ts ~loc ext
|> Result.handle_error ~f:(fun (err, _) -> Location.Error.raise err)
end
module M = Make (struct
type 'a t =
ctxt:Expansion_context.Extension.t -> arg:Longident.t Loc.t option -> 'a
end)
type 'a expander_result = Simple of 'a | Inline of 'a list
module For_context = struct
type 'a t = ('a, 'a expander_result) M.t
let convert_res ts ~ctxt ext =
let loc = Expansion_context.Extension.extension_point_loc ctxt in
let open Result in
M.find ts ext >>= fun found ->
match found with
| None -> Ok None
| Some ({ payload = M.Payload_parser (pattern, f); _ }, arg) -> (
Ast_pattern.parse_res pattern loc (snd ext) (f ~ctxt ~arg)
>>| fun payload ->
match payload with
| Simple x -> Some x
| Inline _ -> failwith "Extension.convert")
let convert ts ~ctxt ext =
convert_res ts ~ctxt ext
|> Result.handle_error ~f:(fun (err, _) -> Location.Error.raise err)
let convert_inline_res ts ~ctxt ext =
let loc = Expansion_context.Extension.extension_point_loc ctxt in
let open Result in
M.find ts ext >>= fun found ->
match found with
| None -> Ok None
| Some ({ payload = M.Payload_parser (pattern, f); _ }, arg) -> (
Ast_pattern.parse_res pattern loc (snd ext) (f ~ctxt ~arg)
>>| fun payload ->
match payload with Simple x -> Some [ x ] | Inline l -> Some l)
let convert_inline ts ~ctxt ext =
convert_inline_res ts ~ctxt ext
|> Result.handle_error ~f:(fun (err, _) -> Location.Error.raise err)
end
type t = T : _ For_context.t -> t
let check_context_for_inline : type a. func:string -> a Context.t -> unit =
fun ~func ctx ->
match ctx with
| Context.Class_field -> ()
| Context.Class_type_field -> ()
| Context.Signature_item -> ()
| Context.Structure_item -> ()
| context ->
Printf.ksprintf invalid_arg "%s: %s can't be inlined" func
(Context.desc context)
let rec filter_by_context : type a.
a Context.t -> t list -> a For_context.t list =
fun context expanders ->
match expanders with
| [] -> []
| T t :: rest -> (
match Context.eq context t.context with
| Eq -> t :: filter_by_context context rest
| Ne -> filter_by_context context rest)
let unhandled_extension_error ctx (name, _) =
if
not
(Name.Allowlisted.is_allowlisted ~kind:`Extension name.txt
|| Name.ignore_checks name.txt)
then
[
Name.Registrar.Error.createf registrar (Context.T ctx)
"Extension `%s' was not translated" name;
]
else []
let collect_unhandled_extension_errors =
object
inherit [Location.Error.t list] Ast_traverse.fold as super
method! extension (name, _) acc =
acc
@ [
Location.Error.createf ~loc:name.loc
"extension not expected here, Ppxlib.Extension needs updating!";
]
method! core_type_desc x acc =
match x with
| Ptyp_extension ext -> acc @ unhandled_extension_error Core_type ext
| x -> super#core_type_desc x acc
method! pattern_desc x acc =
match x with
| Ppat_extension ext -> acc @ unhandled_extension_error Pattern ext
| x -> super#pattern_desc x acc
method! expression_desc x acc =
match x with
| Pexp_extension ext -> acc @ unhandled_extension_error Expression ext
| x -> super#expression_desc x acc
method! class_type_desc x acc =
match x with
| Pcty_extension ext -> acc @ unhandled_extension_error Class_type ext
| x -> super#class_type_desc x acc
method! class_type_field_desc x acc =
match x with
| Pctf_extension ext ->
acc @ unhandled_extension_error Class_type_field ext
| x -> super#class_type_field_desc x acc
method! class_expr_desc x acc =
match x with
| Pcl_extension ext -> acc @ unhandled_extension_error Class_expr ext
| x -> super#class_expr_desc x acc
method! class_field_desc x acc =
match x with
| Pcf_extension ext -> acc @ unhandled_extension_error Class_field ext
| x -> super#class_field_desc x acc
method! module_type_desc x acc =
match x with
| Pmty_extension ext -> acc @ unhandled_extension_error Module_type ext
| x -> super#module_type_desc x acc
method! signature_item_desc x acc =
match x with
| Psig_extension (ext, _) ->
acc @ unhandled_extension_error Signature_item ext
| x -> super#signature_item_desc x acc
method! module_expr_desc x acc =
match x with
| Pmod_extension ext -> acc @ unhandled_extension_error Module_expr ext
| x -> super#module_expr_desc x acc
method! structure_item_desc x acc =
match x with
| Pstr_extension (ext, _) ->
acc @ unhandled_extension_error Structure_item ext
| x -> super#structure_item_desc x acc
end
let error_list_to_exception = function
| [] -> ()
| err :: _ -> Location.Error.raise err
let check_unused =
object
inherit Ast_traverse.iter
method! extension (name, _) =
Location.raise_errorf ~loc:name.loc
"extension not expected here, Ppxlib.Extension needs updating!"
method! core_type_desc x =
collect_unhandled_extension_errors#core_type_desc x []
|> error_list_to_exception
method! pattern_desc x =
collect_unhandled_extension_errors#pattern_desc x []
|> error_list_to_exception
method! expression_desc x =
collect_unhandled_extension_errors#expression_desc x []
|> error_list_to_exception
method! class_type_desc x =
collect_unhandled_extension_errors#class_type_desc x []
|> error_list_to_exception
method! class_type_field_desc x =
collect_unhandled_extension_errors#class_type_field_desc x []
|> error_list_to_exception
method! class_expr_desc x =
collect_unhandled_extension_errors#class_expr_desc x []
|> error_list_to_exception
method! class_field_desc x =
collect_unhandled_extension_errors#class_field_desc x []
|> error_list_to_exception
method! module_type_desc x =
collect_unhandled_extension_errors#module_type_desc x []
|> error_list_to_exception
method! signature_item_desc x =
collect_unhandled_extension_errors#signature_item_desc x []
|> error_list_to_exception
method! module_expr_desc x =
collect_unhandled_extension_errors#module_expr_desc x []
|> error_list_to_exception
method! structure_item_desc x =
collect_unhandled_extension_errors#structure_item_desc x []
|> error_list_to_exception
end
module V3 = struct
type nonrec t = t
let declare name context pattern k =
let pattern = Ast_pattern.map_result pattern ~f:(fun x -> Simple x) in
T
(M.declare ~with_arg:false name context pattern (fun ~ctxt ~arg:_ ->
k ~ctxt))
let declare_inline name context pattern k =
check_context_for_inline context ~func:"Extension.declare_inline";
let pattern = Ast_pattern.map_result pattern ~f:(fun x -> Inline x) in
T
(M.declare ~with_arg:false name context pattern (fun ~ctxt ~arg:_ ->
k ~ctxt))
let declare_with_path_arg name context pattern k =
let pattern = Ast_pattern.map_result pattern ~f:(fun x -> Simple x) in
T (M.declare ~with_arg:true name context pattern k)
let declare_inline_with_path_arg name context pattern k =
check_context_for_inline context
~func:"Extension.declare_inline_with_path_arg";
let pattern = Ast_pattern.map_result pattern ~f:(fun x -> Inline x) in
T (M.declare ~with_arg:true name context pattern k)
end
let declare name context pattern f =
V3.declare name context pattern
(Expansion_context.Extension.with_loc_and_path f)
let declare_inline name context pattern f =
V3.declare_inline name context pattern
(Expansion_context.Extension.with_loc_and_path f)
let declare_with_path_arg name context pattern k =
let k' = Expansion_context.Extension.with_loc_and_path k in
let pattern = Ast_pattern.map_result pattern ~f:(fun x -> Simple x) in
T (M.declare ~with_arg:true name context pattern k')
let declare_inline_with_path_arg name context pattern k =
let k' = Expansion_context.Extension.with_loc_and_path k in
check_context_for_inline context
~func:"Extension.declare_inline_with_path_arg";
let pattern = Ast_pattern.map_result pattern ~f:(fun x -> Inline x) in
T (M.declare ~with_arg:true name context pattern k')
let __declare_ppx_import name expand =
let pattern = Ast_pattern.(pstr (pstr_type recursive (__ ^:: nil) ^:: nil)) in
V3.declare name Context.Ppx_import pattern expand
module V2 = struct
type nonrec t = t
let declare = declare
let declare_inline = declare_inline
end