Skip to content

Commit cde86b2

Browse files
pitag-hahhugo
andauthored
Ppx: port all PPX's to ppxlib (#1041)
Before, the two extension node rewriters in the compiler and in ppx_js were based on the omp driver and the omp Ast_mapper. The deriver in ppx_deriving_json was already partly based on ppxlib, but also on omp's Ast_408. All of those omp modules will be dropped in omp 2.0.0. Co-authored-by: Hugo Heuzard <[email protected]>
1 parent eb1cbcd commit cde86b2

File tree

24 files changed

+401
-398
lines changed

24 files changed

+401
-398
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# unreleased
2+
## Features/Changes
3+
* compiler, ppx_js, ppx_deriving_json: port PPX's to ppxlib (#1041)
4+
15
# 3.7.1 (2020-09-29) - London
26
## Features/Changes
37
* lib: Add Navigator.{vendor,maxTouchPoints} (#1062)

compiler/ppx/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(library
22
(name ppx_optcomp_light)
33
(libraries compiler-libs.common
4-
ocaml-migrate-parsetree)
4+
ppxlib)
55
(kind ppx_rewriter)
66
)

compiler/ppx/ppx_optcomp_light.ml

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
*)
2727

2828
open StdLabels
29-
open Migrate_parsetree
30-
open OCaml_407.Ast
31-
open Parsetree
29+
open Ppxlib.Parsetree
3230

3331
module Version : sig
3432
type t
@@ -86,7 +84,7 @@ let keep loc (attrs : attributes) =
8684
try
8785
let keep =
8886
List.for_all attrs ~f:(function
89-
| { Location.txt = ("if" | "ifnot") as ifnot; _ }, attr_payload -> (
87+
| { attr_name = { txt = ("if" | "ifnot") as ifnot; _ }; attr_payload; _ } -> (
9088
let norm =
9189
match ifnot with
9290
| "if" -> fun x -> x
@@ -186,32 +184,53 @@ let rec filter_pattern = function
186184
| { ppat_attributes; ppat_loc; _ } as p ->
187185
if keep ppat_loc ppat_attributes then Some p else None
188186

189-
let mapper =
190-
{ Ast_mapper.default_mapper with
191-
cases =
192-
(fun mapper cases ->
193-
let cases =
194-
filter_map cases ~f:(fun case ->
195-
match filter_pattern case.pc_lhs with
196-
| None -> None
197-
| Some pattern -> Some { case with pc_lhs = pattern })
198-
in
199-
Ast_mapper.default_mapper.cases mapper cases)
200-
; structure =
201-
(fun mapper items ->
202-
let items =
203-
List.filter items ~f:(fun item ->
204-
match item.pstr_desc with
205-
| Pstr_module { pmb_attributes; pmb_loc; _ } -> keep pmb_loc pmb_attributes
206-
| Pstr_primitive { pval_attributes; pval_loc; _ } ->
207-
keep pval_loc pval_attributes
208-
| _ -> true)
209-
in
210-
Ast_mapper.default_mapper.structure mapper items)
211-
}
187+
(* TODO: This class is useful while we transition to ppxlib.0.17 that provides the `cases`
188+
method. Remove this once we drop support for ppxlib < 0.17 *)
189+
class map =
190+
object (self)
191+
inherit Ppxlib.Ast_traverse.map as super
192+
193+
method cases = self#list self#case [@@ocaml.warning "-7"]
194+
195+
method expression_desc : expression_desc -> expression_desc =
196+
fun x ->
197+
match x with
198+
| Pexp_function a ->
199+
let a = self#cases a in
200+
Pexp_function a
201+
| Pexp_match (a, b) ->
202+
let a = self#expression a in
203+
let b = self#cases b in
204+
Pexp_match (a, b)
205+
| Pexp_try (a, b) ->
206+
let a = self#expression a in
207+
let b = self#cases b in
208+
Pexp_try (a, b)
209+
| _ -> super#expression_desc x
210+
[@@ocaml.warning "-7"]
211+
end
212+
213+
let traverse =
214+
object
215+
inherit map as super
216+
217+
method! structure items =
218+
let items =
219+
List.filter items ~f:(fun item ->
220+
match item.pstr_desc with
221+
| Pstr_module { pmb_attributes; pmb_loc; _ } -> keep pmb_loc pmb_attributes
222+
| Pstr_primitive { pval_attributes; pval_loc; _ } ->
223+
keep pval_loc pval_attributes
224+
| _ -> true)
225+
in
226+
super#structure items
227+
228+
method! cases =
229+
filter_map ~f:(fun case ->
230+
match filter_pattern case.pc_lhs with
231+
| None -> None
232+
| Some pattern -> Some { case with pc_lhs = pattern })
233+
end
212234

213235
let () =
214-
Driver.register
215-
~name:"ppx_optcomp_light"
216-
Migrate_parsetree.Versions.ocaml_407
217-
(fun _config _cookies -> mapper)
236+
Ppxlib.Driver.register_transformation ~impl:traverse#structure "ppx_optcomp_light"

js_of_ocaml-compiler.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ depends: [
1919
"ppx_expect" {with-test & >= "v0.12.0"}
2020
"cmdliner"
2121
"menhir"
22-
"ocaml-migrate-parsetree" {< "2.0.0"}
22+
"ppxlib" {>= "0.15.0"}
2323
"yojson" # It's optional, but we want users to be able to use source-map without pain.
2424
]
2525

js_of_ocaml-ppx.opam

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ build: [["dune" "build" "-p" name "-j" jobs]]
1616
depends: [
1717
"ocaml" {>= "4.02.0"}
1818
"dune" {>= "2.5"}
19-
"ocaml-migrate-parsetree" {>= "1.4" & < "2.0.0" }
20-
"ppx_tools_versioned" {>= "5.2.3"}
19+
"ppxlib" {>= "0.15.0"}
2120
"js_of_ocaml" {= version}
2221
]

js_of_ocaml-ppx_deriving_json.opam

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ depends: [
1717
"ocaml" {>= "4.04.1"}
1818
"dune" {>= "2.5"}
1919
"js_of_ocaml" {= version}
20-
"ocaml-migrate-parsetree" {< "2.0.0"}
21-
"ppxlib" {>= "0.14.0"}
20+
"ppxlib" {>= "0.15.0"}
2221
]

js_of_ocaml.opam

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ build: [["dune" "build" "-p" name "-j" jobs]]
1818
depends: [
1919
"ocaml" {>= "4.02.0"}
2020
"dune" {>= "2.5"}
21-
"ocaml-migrate-parsetree" {>= "1.4" & < "2.0.0" }
22-
"ppx_tools_versioned" {>= "5.2.3"}
21+
"ppxlib" {>= "0.15.0" }
2322
"uchar"
2423
"js_of_ocaml-compiler" {= version}
2524
]

0 commit comments

Comments
 (0)