Skip to content

Commit 7228a05

Browse files
committed
release ppxlib constraints
1 parent 05e1c2a commit 7228a05

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

src/ppx/dune

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(public_name ez_api.ppx_common)
44
(optional)
55
(modules ppx_common)
6-
(preprocess (pps ppxlib.metaquot))
6+
(preprocess (pps ppxlib.metaquot ppxlib_optcomp))
77
(libraries ppx_deriving_encoding.lib))
88

99
(library
@@ -42,12 +42,18 @@
4242
(libraries ppx_common)
4343
(ppx_runtime_libraries ezReq_lwt))
4444

45+
(library
46+
(name ppxlib_optcomp)
47+
(modules ppxlib_optcomp)
48+
(libraries compiler-libs.common ppxlib)
49+
(kind ppx_rewriter))
50+
4551
(library
4652
(name ppx_deriving_err_case)
4753
(public_name ez_api.ppx_err_case)
4854
(optional)
4955
(modules ppx_deriving_err_case)
50-
(preprocess (pps ppxlib.metaquot))
56+
(preprocess (pps ppxlib.metaquot ppxlib_optcomp))
5157
(kind ppx_rewriter)
5258
(ppx_runtime_libraries ez_api)
5359
(libraries ppx_deriving_encoding.lib ez_api))

src/ppx/ppx_common.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,20 +697,20 @@ let handler_args ~name e =
697697
p, [%expr match [%e evar ~loc (name ^ "_req")] _req with
698698
| Error e -> EzAPIServerUtils.return ([%e !global_req_error] e)
699699
| Ok [%p pvar ~loc id] -> [%e f]] in
700-
match e.pexp_desc with
701-
| Pexp_fun (_, _, p1, {pexp_desc=Pexp_fun (_, _, p2, {pexp_desc=Pexp_fun (_, _, p3, f); _}); _}) ->
700+
match e with
701+
| [%expr fun [%p? p1] [%p? p2] [%p? p3] -> [%e? f]] ->
702702
let f = match !global_wrapper with
703703
| None -> f
704704
| Some wrap -> eapply ~loc wrap [ f ] in
705705
let p1, f = aux p1 f in
706706
[%expr fun [%p p1] [%p p2] [%p p3] -> [%e f]]
707-
| Pexp_fun (_, _, p1, {pexp_desc = Pexp_fun (_, _, p2, f); _}) ->
707+
| [%expr fun [%p? p1] [%p? p2] -> [%e? f]] ->
708708
let f = match !global_wrapper with
709709
| None -> f
710710
| Some wrap -> eapply ~loc wrap [ f ] in
711711
let p1, f = aux p1 f in
712712
[%expr fun [%p p1] _ [%p p2] -> [%e f]]
713-
| Pexp_fun (_, _, p, f) ->
713+
| [%expr fun [%p? p] -> [%e? f]] ->
714714
let f = match !global_wrapper with
715715
| None -> f
716716
| Some wrap -> eapply ~loc wrap [ f ] in

src/ppx/ppx_deriving_err_case.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ open Ppxlib
1212
open Ast_builder.Default
1313
open Ppx_deriving_encoding_lib
1414

15+
let pexp_function ~loc l =
16+
pexp_function ~loc l
17+
[@@if ast_version < 502]
18+
19+
let pexp_function ~loc l =
20+
pexp_function ~loc [] None @@ Pfunction_cases (l, loc, [])
21+
[@@if ast_version >= 502]
22+
1523
let mk ~loc ?enc ?(kind_label="kind") ~title name code =
1624
let kind_enc = Utils.(enc_apply ~loc "obj1" [
1725
enc_apply ~loc "req" [
@@ -23,11 +31,10 @@ let mk ~loc ?enc ?(kind_label="kind") ~title name code =
2331
let encoding =
2432
if title then Utils.enc_apply ~loc "def" [ estring ~loc name; encoding ]
2533
else encoding in
26-
let select = pexp_function ~loc [
27-
case ~guard:None
28-
~lhs:(ppat_variant ~loc name (Option.map (fun _ -> [%pat? x]) enc))
29-
~rhs:(Option.fold ~none:[%expr Some ()] ~some:(fun _ -> [%expr Some ((), x)]) enc);
30-
case ~guard:None ~lhs:[%pat? _] ~rhs:[%expr None] ] in
34+
let select = [%expr function
35+
| [%p ppat_variant ~loc name (Option.map (fun _ -> [%pat? x]) enc)] ->
36+
[%e Option.fold ~none:[%expr Some ()] ~some:(fun _ -> [%expr Some ((), x)]) enc]
37+
| _ -> None] in
3138
let deselect = Utils.pexp_fun
3239
(Option.fold ~none:[%pat? ()] ~some:(fun _ -> [%pat? ((), x)]) enc)
3340
(pexp_variant ~loc name (Option.map (fun _ -> [%expr x]) enc)) in

src/ppx/ppxlib_optcomp.ml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
open Ppxlib
2+
3+
let keep attrs =
4+
match List.find_map (fun a -> if a.attr_name.txt = "if" then Some a.attr_payload else None) attrs with
5+
| Some PStr [ {pstr_desc=Pstr_eval ({
6+
pexp_desc=Pexp_apply ({
7+
pexp_desc=Pexp_ident {txt=Lident cmp; _}; _}, [
8+
Nolabel, {pexp_desc=Pexp_ident {txt=Lident "ast_version"; _}; _};
9+
Nolabel, {pexp_desc=Pexp_constant Pconst_integer (v, None); _}
10+
]); _}, _); _} ] ->
11+
let f = match cmp with
12+
| "=" -> (=) | ">" -> (>) | ">=" -> (>=) | "<" -> (<) | "<=" -> (<=)
13+
| "<>" -> (<>) | _ -> (fun _ _ -> true) in
14+
f Selected_ast.version (int_of_string v)
15+
| _ -> true
16+
17+
let rec filter_pattern = function
18+
| { ppat_desc = Ppat_or (p1, p2); _ } as p ->
19+
(match filter_pattern p1, filter_pattern p2 with
20+
| None, None -> None
21+
| Some p1, None -> Some p1
22+
| None, Some p2 -> Some p2
23+
| Some p1, Some p2 -> Some { p with ppat_desc = Ppat_or (p1, p2) })
24+
| { ppat_attributes; _ } as p ->
25+
if keep ppat_attributes then Some p else None
26+
27+
28+
let transform = object inherit Ast_traverse.map as super
29+
method! structure s =
30+
let s = List.filter_map (fun it -> match it.pstr_desc with
31+
| Pstr_value (flag, l) ->
32+
let l = List.filter (fun vb -> keep vb.pvb_attributes) l in
33+
(match l with [] -> None | _ -> Some { it with pstr_desc = Pstr_value (flag, l) })
34+
| _ -> Some it
35+
) s in
36+
super#structure s
37+
38+
method! cases l =
39+
let l = List.filter_map (fun c ->
40+
let p = filter_pattern c.pc_lhs in
41+
Option.map (fun pc_lhs -> { c with pc_lhs }) p
42+
) l in
43+
super#cases l
44+
45+
method! expression e =
46+
match e.pexp_desc with
47+
| Pexp_let (flag, l, end_) ->
48+
let l = List.filter (fun vb -> keep vb.pvb_attributes) l in
49+
let e = match l with [] -> end_ | _ -> { e with pexp_desc = Pexp_let (flag, l, end_) } in
50+
super#expression e
51+
| _ -> super#expression e
52+
end
53+
54+
let () = Driver.register_transformation ~impl:transform#structure "ppxlib_optcomp"

0 commit comments

Comments
 (0)