@@ -4,7 +4,7 @@ module type EXPANDER = sig
44 val expand_bool : loc :location -> bool -> expression
55 val expand_float : loc :location -> string -> expression
66 val expand_int : loc :location -> pexp_loc :location -> string -> expression
7- val expand_intlit : loc :location -> string -> expression
7+ val expand_intlit : loc :location -> pexp_loc : location -> string -> expression
88 val expand_list : loc :location -> expression list -> expression
99 val expand_none : loc :location -> unit -> expression
1010
4343module Ezjsonm_expander : EXPANDER = struct
4444 include Common
4545
46- let expand_intlit ~loc _ = Raise. unsupported_payload ~loc
46+ let expand_intlit ~loc :_ ~pexp_loc :loc _ =
47+ Ast_builder.Default. pexp_extension ~loc (Error. unsupported_payload ~loc )
4748
4849 let expand_int ~loc ~pexp_loc s =
4950 match int_of_string_opt s with
5051 | Some i ->
5152 [% expr `Float [% e Ast_builder.Default. efloat ~loc (string_of_int i)]]
52- | _ -> Raise. unsupported_payload ~loc: pexp_loc
53+ | _ ->
54+ Ast_builder.Default. pexp_extension ~loc: pexp_loc
55+ (Error. unsupported_payload ~loc: pexp_loc)
5356
5457 let expand_list ~loc exprs =
5558 expand_list ~loc (fun e -> [% expr `A [% e e]]) exprs
6164module Yojson_expander : EXPANDER = struct
6265 include Common
6366
64- let expand_intlit ~loc s =
67+ let expand_intlit ~loc ~ pexp_loc : _ s =
6568 [% expr `Intlit [% e Ast_builder.Default. estring ~loc s]]
6669
6770 let expand_int ~loc ~pexp_loc s =
6871 match int_of_string_opt s with
6972 | Some i -> [% expr `Int [% e Ast_builder.Default. eint ~loc i]]
7073 | None when Integer_const. is_binary s ->
71- Raise. unsupported_payload ~loc: pexp_loc
74+ Ast_builder.Default. pexp_extension ~loc: pexp_loc
75+ (Error. unsupported_payload ~loc: pexp_loc)
7276 | None when Integer_const. is_octal s ->
73- Raise. unsupported_payload ~loc: pexp_loc
77+ Ast_builder.Default. pexp_extension ~loc: pexp_loc
78+ (Error. unsupported_payload ~loc: pexp_loc)
7479 | None when Integer_const. is_hexadecimal s ->
75- Raise. unsupported_payload ~loc: pexp_loc
76- | None -> expand_intlit ~loc s
80+ Ast_builder.Default. pexp_extension ~loc: pexp_loc
81+ (Error. unsupported_payload ~loc: pexp_loc)
82+ | None -> expand_intlit ~loc ~pexp_loc s
7783
7884 let expand_list ~loc exprs =
7985 expand_list ~loc (fun e -> [% expr `List [% e e]]) exprs
@@ -86,7 +92,8 @@ module Make (Expander : EXPANDER) = struct
8692 let expand_anti_quotation ~pexp_loc = function
8793 | PStr [ { pstr_desc = Pstr_eval (expr, _); _ } ] -> expr
8894 | PStr _ | PSig _ | PTyp _ | PPat _ ->
89- Raise. bad_expr_antiquotation_payload ~loc: pexp_loc
95+ Ast_builder.Default. pexp_extension ~loc: pexp_loc
96+ (Error. bad_expr_antiquotation_payload ~loc: pexp_loc)
9097
9198 let rec expand ~loc ~path expr =
9299 match expr with
@@ -99,9 +106,10 @@ module Make (Expander : EXPANDER) = struct
99106 Expander. expand_int ~loc ~pexp_loc s
100107 | {
101108 pexp_desc = Pexp_constant (Pconst_integer (s, Some ('l' | 'L' | 'n' )));
109+ pexp_loc;
102110 _;
103111 } ->
104- Expander. expand_intlit ~loc s
112+ Expander. expand_intlit ~loc ~pexp_loc s
105113 | { pexp_desc = Pexp_constant (Pconst_float (s , None)); _ } ->
106114 Expander. expand_float ~loc s
107115 | [% expr []] -> Expander. expand_list ~loc []
@@ -112,7 +120,9 @@ module Make (Expander : EXPANDER) = struct
112120 | { pexp_desc = Pexp_extension ({ txt = " y" | " aq" ; _ }, p); pexp_loc; _ }
113121 ->
114122 expand_anti_quotation ~pexp_loc p
115- | _ -> Raise. unsupported_payload ~loc: expr.pexp_loc
123+ | _ ->
124+ Ast_builder.Default. pexp_extension ~loc: expr.pexp_loc
125+ (Error. unsupported_payload ~loc: expr.pexp_loc)
116126
117127 and expand_list ~loc ~path = function
118128 | [% expr []] -> []
@@ -124,20 +134,27 @@ module Make (Expander : EXPANDER) = struct
124134
125135 and expand_record ~path l =
126136 let expand_one (f , e ) =
137+ let as_attr =
138+ List. find_opt
139+ (fun attr -> String. equal attr.attr_name.txt " as" )
140+ e.pexp_attributes
141+ in
127142 let field =
128- match
129- ( List. find_opt
130- (fun attr -> String. equal attr.attr_name.txt " as" )
131- e.pexp_attributes,
132- f )
133- with
143+ match (as_attr, f) with
134144 | Some { attr_payload; attr_loc = loc ; _ } , _ ->
135145 Ast_pattern. (parse (single_expr_payload (estring __)))
136- loc attr_payload (fun e -> e)
137- | None , { txt = Lident s ; _ } -> Utils. rewrite_field_name s
138- | None , { txt = _ ; loc } -> Raise. unsupported_record_field ~loc
146+ loc attr_payload (fun e -> Ok e)
147+ | None , { txt = Lident s ; _ } -> Ok (Utils. rewrite_field_name s)
148+ | None , { txt = _ ; loc } ->
149+ let extension =
150+ Ast_builder.Default. pexp_extension ~loc
151+ (Error. unsupported_record_field ~loc )
152+ in
153+ Error extension
139154 in
140- (field, expand ~loc: e.pexp_loc ~path e)
155+ match field with
156+ | Ok field -> (field, expand ~loc: e.pexp_loc ~path e)
157+ | Error extension -> (" error" , extension)
141158 in
142159 List. map expand_one l
143160end
0 commit comments