forked from ocaml-ppx/ppxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mangling utilities from ppx_deriving (issue ocaml-ppx#317)
Signed-off-by: Simmo Saan <[email protected]>
- Loading branch information
Showing
6 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]. *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|}] |