use Location.error_extensionf nodes

This commit is contained in:
xenia 2024-04-24 22:59:58 -04:00
parent 4b281e349b
commit 34156743b1
1 changed files with 24 additions and 21 deletions

View File

@ -2,32 +2,35 @@ open Ppxlib
let log_expander ~ctxt (e : expression) (l : (arg_label * expression) list) : expression =
let loc = Expansion_context.Extension.extension_point_loc ctxt in
let is_exn = begin match e.pexp_desc with
let open (val Ast_builder.make loc) in
let (is_exn, err) = begin match e.pexp_desc with
| Pexp_ident { txt = Lident "trace"; _ }
| Pexp_ident { txt = Lident "debug"; _ }
| Pexp_ident { txt = Lident "info"; _ }
| Pexp_ident { txt = Lident "warn"; _ }
| Pexp_ident { txt = Lident "error"; _ } -> false
| Pexp_ident { txt = Lident "exn"; _ } -> true
| Pexp_ident { txt = Lident "error"; _ } -> (false, None)
| Pexp_ident { txt = Lident "exn"; _ } -> (true, None)
| _ ->
raise (Location.Error (Location.Error.make ~loc ~sub:[]
"log type must be one of: trace, debug, info, warn, error, exn"))
end in
let open (val Ast_builder.make loc) in
if is_exn then
match l with
| [] ->
raise (Location.Error (Location.Error.make ~loc ~sub:[]
"must provide exn value"))
| (_, v)::l ->
let application =
eapply (pexp_ident { txt = Lident "m"; loc}) (List.map snd l) in
[%expr [%e e] [%e v] (Printexc.get_raw_backtrace ()) ~__POS__ ~__FUNCTION__
(fun m -> [%e application])]
else
let application =
eapply (pexp_ident { txt = Lident "m"; loc}) (List.map snd l) in
[%expr [%e e] ~__POS__ ~__FUNCTION__ (fun m -> [%e application])]
(false,
Some (pexp_extension @@ Location.error_extensionf ~loc
"log type must be one of: trace, debug, info, warn, error, exn"))
end in match err with
| Some err -> err
| None ->
if is_exn then
match l with
| [] ->
pexp_extension @@ Location.error_extensionf ~loc
"must provide exn value"
| (_, v)::l ->
let application =
eapply (pexp_ident { txt = Lident "m"; loc}) (List.map snd l) in
[%expr [%e e] [%e v] (Printexc.get_raw_backtrace ()) ~__POS__ ~__FUNCTION__
(fun m -> [%e application])]
else
let application =
eapply (pexp_ident { txt = Lident "m"; loc}) (List.map snd l) in
[%expr [%e e] ~__POS__ ~__FUNCTION__ (fun m -> [%e application])]
let log_extension =
let pattern =