|
26 | 26 | *) |
27 | 27 |
|
28 | 28 | open StdLabels |
29 | | -open Migrate_parsetree |
30 | | -open OCaml_407.Ast |
31 | | -open Parsetree |
| 29 | +open Ppxlib.Parsetree |
32 | 30 |
|
33 | 31 | module Version : sig |
34 | 32 | type t |
@@ -86,7 +84,7 @@ let keep loc (attrs : attributes) = |
86 | 84 | try |
87 | 85 | let keep = |
88 | 86 | 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; _ } -> ( |
90 | 88 | let norm = |
91 | 89 | match ifnot with |
92 | 90 | | "if" -> fun x -> x |
@@ -186,32 +184,53 @@ let rec filter_pattern = function |
186 | 184 | | { ppat_attributes; ppat_loc; _ } as p -> |
187 | 185 | if keep ppat_loc ppat_attributes then Some p else None |
188 | 186 |
|
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 |
212 | 234 |
|
213 | 235 | 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" |
0 commit comments