@@ -127,6 +127,54 @@ end = struct
127
127
Location. print_report Format. err_formatter report;
128
128
exit 2
129
129
130
+ let exp_construct loc txt args =
131
+ Ast_helper. with_default_loc loc @@ fun () ->
132
+ match args with
133
+ | [] -> Ast_helper.Exp. construct { loc; txt } None
134
+ | [arg] -> Ast_helper.Exp. construct { loc; txt } (Some arg)
135
+ | _ ->
136
+ Ast_helper.Exp. construct { loc; txt }
137
+ (Some (Ast_helper.Exp. tuple args))
138
+
139
+ let pat_construct loc txt args =
140
+ Ast_helper. with_default_loc loc @@ fun () ->
141
+ match args with
142
+ | [] -> Ast_helper.Pat. construct { loc; txt } None
143
+ | [arg] -> Ast_helper.Pat. construct { loc; txt } (Some arg)
144
+ | _ ->
145
+ Ast_helper.Pat. construct { loc; txt }
146
+ (Some (Ast_helper.Pat. tuple args))
147
+
148
+ let get_literal_extension ~construct ~none ~loc_exp ~of_payload name attrs
149
+ arg =
150
+ match name with
151
+ | "lit.integer" ->
152
+ let suffix =
153
+ match find_attr_loc " suffix" attrs with
154
+ | Some attr -> of_payload attr.loc attr.txt
155
+ | None -> none in
156
+ Some (construct (Longident. Lident " Pconst_integer" ) [arg; suffix])
157
+ | "lit.char" ->
158
+ Some (construct (Longident. Lident " Pconst_char" ) [arg])
159
+ | "lit.string" ->
160
+ let quotation_delimiter =
161
+ match find_attr_loc " quotation_delimiter" attrs with
162
+ | Some attr -> of_payload attr.loc attr.txt
163
+ | None -> none in
164
+ let inside_loc =
165
+ match find_attr_loc " loc" attrs with
166
+ | Some attr -> of_payload attr.loc attr.txt
167
+ | None -> loc_exp in
168
+ Some (construct (Longident. Lident " Pconst_string" )
169
+ [arg; inside_loc; quotation_delimiter])
170
+ | "lit.float" ->
171
+ let suffix =
172
+ match find_attr_loc " suffix" attrs with
173
+ | Some attr -> of_payload attr.loc attr.txt
174
+ | None -> none in
175
+ Some (construct (Longident. Lident " Pconst_float" ) [arg; suffix])
176
+ | _ -> None
177
+
130
178
let exp_lifter loc map =
131
179
let map = map.Ast_mapper. expr map in
132
180
object
@@ -137,9 +185,31 @@ end = struct
137
185
method! lift_Location_t _ = loc
138
186
139
187
(* Support for antiquotations *)
140
- method! lift_Parsetree_expression = function
188
+ method! lift_Parsetree_expression x =
189
+ let loc_exp = loc in
190
+ match x with
141
191
| {pexp_desc =Pexp_extension ({txt ="e" ;loc} , e ); _} -> map (get_exp loc e)
142
- | x -> super # lift_Parsetree_expression x
192
+ | {pexp_desc =Pexp_extension ({txt;loc} , e ); pexp_attributes; _} ->
193
+ begin match
194
+ get_literal_extension txt pexp_attributes (get_exp loc e)
195
+ ~construct: (exp_construct loc)
196
+ ~none: (exp_construct loc (Lident " None" ) [] ) ~loc_exp
197
+ ~of_payload: get_exp
198
+ with
199
+ | Some e ->
200
+ let e = Ast_helper.Exp. record [
201
+ { loc;
202
+ txt = Longident. Ldot (Lident " Parsetree" , " pexp_desc" ) },
203
+ exp_construct loc (Lident " Pexp_constant" ) [e];
204
+ { loc; txt = Lident " pexp_loc" }, loc_exp;
205
+ { loc; txt = Lident " pexp_loc_stack" },
206
+ exp_construct loc (Lident " []" ) [];
207
+ { loc; txt = Lident " pexp_attributes" },
208
+ exp_construct loc (Lident " []" ) []] None in
209
+ map e
210
+ | _ -> super # lift_Parsetree_expression x
211
+ end
212
+ | _ -> super # lift_Parsetree_expression x
143
213
144
214
method! lift_Parsetree_pattern = function
145
215
| {ppat_desc =Ppat_extension ({txt ="p" ;loc} , e ); _} -> map (get_exp loc e)
@@ -188,8 +258,25 @@ end = struct
188
258
builder#record n fields
189
259
190
260
(* Support for antiquotations *)
191
- method! lift_Parsetree_expression = function
261
+ method! lift_Parsetree_expression x =
262
+ match x with
192
263
| {pexp_desc =Pexp_extension ({txt ="e" ;loc} , e ); _} -> map (get_pat loc e)
264
+ | {pexp_desc =Pexp_extension ({txt;loc} , e ); pexp_attributes; _} ->
265
+ begin match
266
+ get_literal_extension txt pexp_attributes (get_pat loc e)
267
+ ~construct: (pat_construct loc)
268
+ ~none: (Ast_helper.Pat. any ~loc () )
269
+ ~loc_exp: (Ast_helper.Pat. any ~loc () )
270
+ ~of_payload: get_pat
271
+ with
272
+ | Some e ->
273
+ let e = Ast_helper.Pat. record [
274
+ { loc;
275
+ txt = Longident. Ldot (Lident " Parsetree" , " pexp_desc" ) },
276
+ pat_construct loc (Lident " Pexp_constant" ) [e]] Open in
277
+ map e
278
+ | _ -> super # lift_Parsetree_expression x
279
+ end
193
280
| x -> super # lift_Parsetree_expression x
194
281
195
282
method! lift_Parsetree_pattern = function
0 commit comments