| 1 | open Base |
| 2 | open Ppxlib |
| 3 | open Ppx_compare_expander |
| 4 | |
| 5 | let add_deriver name (module E : Ppx_compare_expander.S) = |
| 6 | let flags () = Deriving.Args.(empty +> flag "localize") in |
| 7 | let str_type_decl = |
| 8 | Deriving.Generator.V2.make (flags ()) E.str_type_decl ~attributes:E.str_attributes |
| 9 | in |
| 10 | let sig_type_decl = Deriving.Generator.V2.make (flags ()) E.sig_type_decl in |
| 11 | Deriving.add name ~str_type_decl ~sig_type_decl |
| 12 | ;; |
| 13 | |
| 14 | let compare = add_deriver "compare" (module Compare) |
| 15 | let equal = add_deriver "equal" (module Equal) |
| 16 | |
| 17 | let replace_underscores_by_variables = |
| 18 | let map = |
| 19 | object |
| 20 | inherit Ast_traverse.map as super |
| 21 | |
| 22 | method! core_type_desc = |
| 23 | function |
| 24 | | Ptyp_any -> Ptyp_var (gen_symbol ~prefix:"a" ()) |
| 25 | | t -> super#core_type_desc t |
| 26 | end |
| 27 | in |
| 28 | map#core_type |
| 29 | ;; |
| 30 | |
| 31 | let () = |
| 32 | [ "compare", "compare_local", Compare.type_, Compare.core_type |
| 33 | ; "equal", "equal_local", Equal.type_, Equal.core_type |
| 34 | ; "@compare.equal", "@compare_local.equal", Equal.type_, Compare.equal_core_type |
| 35 | ] |
| 36 | |> List.concat_map ~f:(fun (name, local_name, type_, core_type) -> |
| 37 | [ name, type_ ~with_local:false, core_type ~with_local:false |
| 38 | ; local_name, type_ ~with_local:true, core_type ~with_local:true |
| 39 | ]) |
| 40 | |> List.iter ~f:(fun (name, type_, core_type) -> |
| 41 | Driver.register_transformation |
| 42 | (String.strip name ~drop:(Char.equal '@')) |
| 43 | ~rules: |
| 44 | [ Context_free.Rule.extension |
| 45 | (Extension.declare |
| 46 | name |
| 47 | Core_type |
| 48 | Ast_pattern.(ptyp __) |
| 49 | (fun ~loc ~path:_ ty -> |
| 50 | type_ ~hide:true ~loc (replace_underscores_by_variables ty))) |
| 51 | ; Context_free.Rule.extension |
| 52 | (Extension.declare |
| 53 | name |
| 54 | Expression |
| 55 | Ast_pattern.(ptyp __) |
| 56 | (fun ~loc:_ ~path:_ ty -> core_type ty)) |
| 57 | ]) |
| 58 | ;; |