-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattr.ml
77 lines (62 loc) · 2.09 KB
/
attr.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
open Ppxlib
let get_attr ~wrap_err_ext attr ast =
match Attribute.get_res attr ast with
| Ok opt -> opt
| Error (err, _) ->
let loc = Location.Error.get_location err in
Some (wrap_err_ext ~loc (Location.Error.to_extension err))
;;
module To_dyn = struct
let name = "ppx_deriving_dyn.to_dyn"
let payload_pattern =
let open Ast_pattern in
let ident_expr = as__ (pexp_ident drop) in
let apply_expr = as__ (pexp_apply drop drop) in
let fun_expr = as__ (pexp_fun drop drop drop drop) in
let function_expr = as__ (pexp_function drop) in
single_expr_payload (ident_expr ||| apply_expr ||| fun_expr ||| function_expr)
;;
let get_attr attr ast =
get_attr ~wrap_err_ext:Ast_builder.Default.pexp_extension attr ast
;;
let core_type_attr =
Attribute.declare name Attribute.Context.core_type payload_pattern (fun expr -> expr)
;;
let from_core_type core_type = get_attr core_type_attr core_type
let label_decl_attr =
Attribute.declare
name
Attribute.Context.label_declaration
payload_pattern
(fun expr -> expr)
;;
let from_label_declaration label_declaration =
get_attr label_decl_attr label_declaration
;;
end
module Ignore = struct
let name = "ppx_deriving_dyn.to_dyn.ignore"
let payload_pattern =
let open Ast_pattern in
pstr nil
;;
let has_ignore attr ast =
(* TODO: Switch to Attribute.has_flag once it's released *)
match Attribute.get_res attr ast with
| Ok (Some ()) -> Ok true
| Ok None -> Ok false
| Error (err, _) ->
let loc = Location.Error.get_location err in
Error (Location.Error.to_extension err, loc)
;;
let core_type_attr =
(* TODO: Switch to Attribute.declare_flag once it's released *)
Attribute.declare name Attribute.Context.core_type payload_pattern ()
;;
let core_type ct = has_ignore core_type_attr ct
let label_decl_attr =
(* TODO: Switch to Attribute.declare_flag once it's released *)
Attribute.declare name Attribute.Context.label_declaration payload_pattern ()
;;
let label_declaration ld = has_ignore label_decl_attr ld
end