Skip to content

Commit

Permalink
Add mangling utilities from ppx_deriving (issue ocaml-ppx#317)
Browse files Browse the repository at this point in the history
Signed-off-by: Simmo Saan <[email protected]>
  • Loading branch information
sim642 committed Aug 27, 2022
1 parent 1baaa0b commit 10b1705
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
unreleased
-------------------

- Add `Ppxlib.Mangle` with name mangling utilities from ppx_deriving (#<PR_NUMBER>, @sim642)

- Make `esequence` right-associative. (#366, @ceastlund)

- Deprecate unused attributes in `Deriving.Generator` (#368, @sim642)
Expand Down
21 changes: 21 additions & 0 deletions src/mangle.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Import

type affix =
[ `Prefix of string | `Suffix of string | `PrefixSuffix of string * string ]

let mangle ?(fixpoint = "t") affix name =
match (String.(name = fixpoint), affix) with
| true, (`Prefix x | `Suffix x) -> x
| true, `PrefixSuffix (p, s) -> p ^ "_" ^ s
| false, `PrefixSuffix (p, s) -> p ^ "_" ^ name ^ "_" ^ s
| false, `Prefix x -> x ^ "_" ^ name
| false, `Suffix x -> name ^ "_" ^ x

let mangle_type_decl ?fixpoint affix { ptype_name = { txt = name; _ }; _ } =
mangle ?fixpoint affix name

let mangle_lid ?fixpoint affix lid =
match lid with
| Lident s -> Lident (mangle ?fixpoint affix s)
| Ldot (p, s) -> Ldot (p, mangle ?fixpoint affix s)
| Lapply _ -> invalid_arg "Ppxlib.Mangle.mangle_lid: Lapply"
23 changes: 23 additions & 0 deletions src/mangle.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(** Derive mangled names from type names in a deriver. *)

open Import

type affix =
[ `Prefix of string (** [`Prefix p] adds prefix [p]. *)
| `Suffix of string (** [`Suffix s] adds suffix [s]. *)
| `PrefixSuffix of string * string
(** [`PrefixSuffix (p, s)] adds both prefix [p] and suffix [s]. *) ]
(** Specification for name mangling. *)

val mangle : ?fixpoint:string -> affix -> string -> string
(** [mangle ~fixpoint affix s] derives a mangled name from [s] with the mangling
specified by [affix]. If [s] is equal to [fixpoint] (["t"] by default), then
[s] is omitted from the mangled name. *)

val mangle_type_decl : ?fixpoint:string -> affix -> type_declaration -> string
(** [mangle_type_decl ~fixpoint affix td] does the same as {!mangle}, but for
the name of [td]. *)

val mangle_lid : ?fixpoint:string -> affix -> Longident.t -> Longident.t
(** [mangle_lid ~fixpoint affix lid] does the same as {!mangle}, but for the
last component of [lid]. *)
1 change: 1 addition & 0 deletions src/ppxlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module Keyword = Keyword
module Loc = Loc
module Location = Location
module Longident = Longident
module Mangle = Mangle
module Merlin_helpers = Merlin_helpers
module Reserved_namespaces = Name.Reserved_namespaces
module Spellcheck = Spellcheck
Expand Down
13 changes: 13 additions & 0 deletions test/mangle/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(rule
(alias runtest)
(enabled_if
(>= %{ocaml_version} "4.08.0"))
(deps
(:test test.ml)
(package ppxlib))
(action
(chdir
%{project_root}
(progn
(run expect-test %{test})
(diff? %{test} %{test}.corrected)))))
46 changes: 46 additions & 0 deletions test/mangle/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
open Ppxlib;;

Mangle.mangle (`Prefix "pre") "foo";;
[%%expect{|
- : string = "pre_foo"
|}]

Mangle.mangle (`Suffix "suf") "foo";;
[%%expect{|
- : string = "foo_suf"
|}]

Mangle.mangle (`PrefixSuffix ("pre", "suf")) "foo";;
[%%expect{|
- : string = "pre_foo_suf"
|}]

Mangle.mangle (`Prefix "pre") "t";;
[%%expect{|
- : string = "pre"
|}]

Mangle.mangle (`Suffix "suf") "t";;
[%%expect{|
- : string = "suf"
|}]

Mangle.mangle (`PrefixSuffix ("pre", "suf")) "t";;
[%%expect{|
- : string = "pre_suf"
|}]

Mangle.mangle ~fixpoint:"foo" (`Prefix "pre") "foo";;
[%%expect{|
- : string = "pre"
|}]

Mangle.mangle ~fixpoint:"foo" (`Suffix "suf") "foo";;
[%%expect{|
- : string = "suf"
|}]

Mangle.mangle ~fixpoint:"foo" (`PrefixSuffix ("pre", "suf")) "foo";;
[%%expect{|
- : string = "pre_suf"
|}]

0 comments on commit 10b1705

Please sign in to comment.