Debian Codegraph @

expander/attrs.ml

raw symbols SCIP
1 open! Base
2 open! Ppxlib
3
4 module To_lift = struct
5 type 'a t = { to_lift : 'a } [@@unboxed]
6 end
7
8 open To_lift
9
10 let default =
11 Attribute.declare
12 "sexp.default"
13 Attribute.Context.label_declaration
14 Ast_pattern.(pstr (pstr_eval __ nil ^:: nil))
15 (fun x -> { to_lift = x })
16 ;;
17
18 let drop_default =
19 Attribute.declare
20 "sexp.sexp_drop_default"
21 Attribute.Context.label_declaration
22 Ast_pattern.(pstr (alt_option (pstr_eval __ nil ^:: nil) nil))
23 (function
24 | None -> None
25 | Some x -> Some { to_lift = x })
26 ;;
27
28 let drop_default_equal =
29 Attribute.declare
30 "sexp.@sexp_drop_default.equal"
31 Attribute.Context.label_declaration
32 Ast_pattern.(pstr nil)
33 ()
34 ;;
35
36 let drop_default_compare =
37 Attribute.declare
38 "sexp.@sexp_drop_default.compare"
39 Attribute.Context.label_declaration
40 Ast_pattern.(pstr nil)
41 ()
42 ;;
43
44 let drop_default_sexp =
45 Attribute.declare
46 "sexp.@sexp_drop_default.sexp"
47 Attribute.Context.label_declaration
48 Ast_pattern.(pstr nil)
49 ()
50 ;;
51
52 let drop_if =
53 Attribute.declare
54 "sexp.sexp_drop_if"
55 Attribute.Context.label_declaration
56 Ast_pattern.(pstr (pstr_eval __ nil ^:: nil))
57 (fun x -> { to_lift = x })
58 ;;
59
60 let opaque =
61 Attribute.declare "sexp.opaque" Attribute.Context.core_type Ast_pattern.(pstr nil) ()
62 ;;
63
64 let omit_nil =
65 Attribute.declare
66 "sexp.omit_nil"
67 Attribute.Context.label_declaration
68 Ast_pattern.(pstr nil)
69 ()
70 ;;
71
72 let option =
73 Attribute.declare
74 "sexp.option"
75 Attribute.Context.label_declaration
76 Ast_pattern.(pstr nil)
77 ()
78 ;;
79
80 let list =
81 Attribute.declare
82 "sexp.list"
83 Attribute.Context.label_declaration
84 Ast_pattern.(pstr nil)
85 ()
86 ;;
87
88 let array =
89 Attribute.declare
90 "sexp.array"
91 Attribute.Context.label_declaration
92 Ast_pattern.(pstr nil)
93 ()
94 ;;
95
96 let bool =
97 Attribute.declare
98 "sexp.bool"
99 Attribute.Context.label_declaration
100 Ast_pattern.(pstr nil)
101 ()
102 ;;
103
104 let list_variant =
105 Attribute.declare
106 "sexp.list"
107 Attribute.Context.constructor_declaration
108 Ast_pattern.(pstr nil)
109 ()
110 ;;
111
112 let list_exception =
113 Attribute.declare "sexp.list" Attribute.Context.type_exception Ast_pattern.(pstr nil) ()
114 ;;
115
116 let list_poly =
117 Attribute.declare "sexp.list" Attribute.Context.rtag Ast_pattern.(pstr nil) ()
118 ;;
119
120 let allow_extra_fields_td =
121 Attribute.declare
122 "sexp.allow_extra_fields"
123 Attribute.Context.type_declaration
124 Ast_pattern.(pstr nil)
125 ()
126 ;;
127
128 let allow_extra_fields_cd =
129 Attribute.declare
130 "sexp.allow_extra_fields"
131 Attribute.Context.constructor_declaration
132 Ast_pattern.(pstr nil)
133 ()
134 ;;
135
136 let grammar_custom =
137 Attribute.declare
138 "sexp_grammar.custom"
139 Attribute.Context.core_type
140 Ast_pattern.(single_expr_payload __)
141 (fun x -> x)
142 ;;
143
144 let grammar_any =
145 Attribute.declare
146 "sexp_grammar.any"
147 Attribute.Context.core_type
148 Ast_pattern.(alt_option (single_expr_payload (estring __)) (pstr nil))
149 (fun x -> x)
150 ;;
151
152 let tag_attribute_for_context context =
153 let open Ast_pattern in
154 let key_equals_value =
155 Ast_pattern.(
156 pexp_apply (pexp_ident (lident (string "="))) (no_label __ ^:: no_label __ ^:: nil)
157 |> pack2)
158 in
159 let get_captured_values ast_pattern context expression =
160 Ast_pattern.to_func ast_pattern context expression.pexp_loc expression (fun x -> x)
161 in
162 let rec collect_sequence expression =
163 match expression.pexp_desc with
164 | Pexp_sequence (l, r) -> l :: collect_sequence r
165 | _ -> [ expression ]
166 in
167 let esequence ast_pattern =
168 Ast_pattern.of_func (fun context _loc expression k ->
169 collect_sequence expression
170 |> List.map ~f:(get_captured_values ast_pattern context)
171 |> k)
172 in
173 Attribute.declare
174 "sexp_grammar.tag"
175 context
176 (pstr (pstr_eval (esequence key_equals_value) nil ^:: nil))
177 (fun x -> x)
178 ;;
179
180 let tag_type = tag_attribute_for_context Core_type
181 let tag_ld = tag_attribute_for_context Label_declaration
182 let tag_cd = tag_attribute_for_context Constructor_declaration
183 let tag_poly = tag_attribute_for_context Rtag
184
185 let tags_attribute_for_context context =
186 Attribute.declare
187 "sexp_grammar.tags"
188 context
189 Ast_pattern.(single_expr_payload __)
190 (fun x -> x)
191 ;;
192
193 let tags_type = tags_attribute_for_context Core_type
194 let tags_ld = tags_attribute_for_context Label_declaration
195 let tags_cd = tags_attribute_for_context Constructor_declaration
196 let tags_poly = tags_attribute_for_context Rtag
197
198 let invalid_attribute ~loc attr description =
199 Location.raise_errorf
200 ~loc
201 "ppx_sexp_conv: [@%s] is only allowed on type [%s]."
202 (Attribute.name attr)
203 description
204 ;;
205
206 let fail_if_allow_extra_field_cd ~loc x =
207 if Option.is_some (Attribute.get allow_extra_fields_cd x)
208 then
209 Location.raise_errorf
210 ~loc
211 "ppx_sexp_conv: [@@allow_extra_fields] is only allowed on inline records."
212 ;;
213
214 let fail_if_allow_extra_field_td ~loc x =
215 if Option.is_some (Attribute.get allow_extra_fields_td x)
216 then (
217 match x.ptype_kind with
218 | Ptype_variant cds
219 when List.exists cds ~f:(fun cd ->
220 match cd.pcd_args with
221 | Pcstr_record _ -> true
222 | _ -> false) ->
223 Location.raise_errorf
224 ~loc
225 "ppx_sexp_conv: [@@@@allow_extra_fields] only works on records. For inline \
226 records, do: type t = A of { a : int } [@@allow_extra_fields] | B [@@@@deriving \
227 sexp]"
228 | _ ->
229 Location.raise_errorf
230 ~loc
231 "ppx_sexp_conv: [@@@@allow_extra_fields] is only allowed on records.")
232 ;;