Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ end
module Ezjsonm_expander : EXPANDER = struct
include Common

let expand_intlit ~loc _ = Raise.unsupported_payload ~loc
let expand_intlit ~loc _ = Ast_builder.Default.pexp_extension ~loc
@@ Location.error_extensionf ~loc "ppx_yojson: unsupported payload"

let expand_int ~loc ~pexp_loc s =
match int_of_string_opt s with
| Some i ->
[%expr `Float [%e Ast_builder.Default.efloat ~loc (string_of_int i)]]
| _ -> Raise.unsupported_payload ~loc:pexp_loc
| _ -> Ast_builder.Default.pexp_extension ~loc
@@ Location.error_extensionf ~loc:pexp_loc "ppx_yojson: unsupported payload"

let expand_list ~loc exprs =
expand_list ~loc (fun e -> [%expr `A [%e e]]) exprs
Expand All @@ -73,11 +75,14 @@ module Yojson_expander : EXPANDER = struct
match int_of_string_opt s with
| Some i -> [%expr `Int [%e Ast_builder.Default.eint ~loc i]]
| None when Integer_const.is_binary s ->
Raise.unsupported_payload ~loc:pexp_loc
Ast_builder.Default.pexp_extension ~loc
@@ Location.error_extensionf ~loc:pexp_loc "ppx_yojson: unsupported payload"
| None when Integer_const.is_octal s ->
Raise.unsupported_payload ~loc:pexp_loc
Ast_builder.Default.pexp_extension ~loc
@@ Location.error_extensionf ~loc:pexp_loc "ppx_yojson: unsupported payload"
| None when Integer_const.is_hexadecimal s ->
Raise.unsupported_payload ~loc:pexp_loc
Ast_builder.Default.pexp_extension ~loc
@@ Location.error_extensionf ~loc:pexp_loc "ppx_yojson: unsupported payload"
| None -> expand_intlit ~loc s

let expand_list ~loc exprs =
Expand Down Expand Up @@ -117,7 +122,8 @@ module Make (Expander : EXPANDER) = struct
| { pexp_desc = Pexp_extension ({ txt = "y" | "aq"; _ }, p); pexp_loc; _ }
->
expand_anti_quotation ~pexp_loc p
| _ -> Raise.unsupported_payload ~loc:expr.pexp_loc
| _ -> Ast_builder.Default.pexp_extension ~loc
@@ Location.error_extensionf ~loc:expr.pexp_loc "ppx_yojson: unsupported payload"

and expand_list ~loc ~path = function
| [%expr []] -> []
Expand Down
13 changes: 9 additions & 4 deletions lib/pattern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ let expand_int ~loc ~ppat_loc s =
match int_of_string_opt s with
| Some i -> [%pat? `Int [%p Ast_builder.Default.pint ~loc i]]
| None when Integer_const.is_binary s ->
Raise.unsupported_payload ~loc:ppat_loc
Ast_builder.Default.ppat_extension ~loc
@@ Location.error_extensionf ~loc:ppat_loc "ppx_yojson: unsupported payload"
| None when Integer_const.is_octal s ->
Raise.unsupported_payload ~loc:ppat_loc
Ast_builder.Default.ppat_extension ~loc
@@ Location.error_extensionf ~loc:ppat_loc "ppx_yojson: unsupported payload"
| None when Integer_const.is_hexadecimal s ->
Raise.unsupported_payload ~loc:ppat_loc
Ast_builder.Default.ppat_extension ~loc
@@ Location.error_extensionf ~loc:ppat_loc "ppx_yojson: unsupported payload"
| None -> expand_intlit ~loc s

let expand_float ~loc s = [%pat? `Float [%p Ast_builder.Default.pfloat ~loc s]]
Expand Down Expand Up @@ -75,7 +78,9 @@ and expand_record ~loc ~ppat_loc ~path l =
[%pat? `Assoc [%p Ast_builder.Default.plist ~loc pat_list]]
in
if List.length l > 4 then
Raise.too_many_fields_in_record_pattern ~loc:ppat_loc
Ast_builder.Default.ppat_extension ~loc
@@ Location.error_extensionf ~loc:ppat_loc "ppx_yojson: record patterns with more than 4 fields aren't supported. \
Consider using ppx_deriving_yojson to handle more complex json objects."
else
let pat_list = List.map expand_one l in
let permutations = Utils.permutations pat_list in
Expand Down