Skip to content

Commit 4b75f60

Browse files
authored
Merge pull request #7 from kit-ty-kate/ppxlib
Switch from OMP to ppxlib
2 parents cae3854 + 40485c3 commit 4b75f60

File tree

5 files changed

+21
-38
lines changed

5 files changed

+21
-38
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ env:
77
global:
88
- PACKAGE="ppx_getenv"
99
matrix:
10-
- DISTRO=debian-stable OCAML_VERSION=4.02
11-
- DISTRO=debian-stable OCAML_VERSION=4.03
1210
- DISTRO=debian-stable OCAML_VERSION=4.04
1311
- DISTRO=debian-stable OCAML_VERSION=4.05
1412
- DISTRO=debian-stable OCAML_VERSION=4.06

dune-project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(synopsis "A sample syntax extension using OCaml's new extension points API")
1515
(tags ("syntax"))
1616
(depends
17-
(ocaml (>= 4.02.0))
18-
(ocaml-migrate-parsetree (>= 1.7.0))
17+
(ocaml (>= 4.04.0))
18+
(ppxlib (>= 0.9.0))
1919
(ounit2 :with-test)
2020
(odoc :with-doc)))

ppx_getenv.opam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ homepage: "https://github.com/ocaml-ppx/ppx_getenv"
1010
bug-reports: "https://github.com/ocaml-ppx/ppx_getenv/issues"
1111
depends: [
1212
"dune" {>= "2.0"}
13-
"ocaml" {>= "4.02.0"}
14-
"ocaml-migrate-parsetree" {>= "1.7.0"}
13+
"ocaml" {>= "4.04.0"}
14+
"ppxlib" {>= "0.9.0"}
1515
"ounit2" {with-test}
1616
"odoc" {with-doc}
1717
]

src/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
(name ppx_getenv)
33
(public_name ppx_getenv)
44
(kind ppx_rewriter)
5-
(libraries ocaml-migrate-parsetree))
5+
(libraries ppxlib))

src/ppx_getenv.ml

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
open Migrate_parsetree.OCaml_411.Ast
2-
let ocaml_version = Migrate_parsetree.Versions.ocaml_411
3-
4-
open Ast_mapper
5-
open Ast_helper
6-
open Asttypes
7-
open Parsetree
1+
open Ppxlib
2+
open Ppxlib.Ast_helper
83

94
let getenv s = try Sys.getenv s with Not_found -> ""
105

11-
let getenv_mapper _config _cookies =
12-
(* Our getenv_mapper only overrides the handling of expressions in the default mapper. *)
13-
{ default_mapper with
14-
expr = fun mapper expr ->
15-
match expr with
16-
(* Is this an extension node? *)
17-
| { pexp_desc =
18-
(* Should have name "getenv". *)
19-
Pexp_extension ({ txt = "getenv"; loc }, pstr); _ } ->
20-
begin match pstr with
21-
| (* Should have a single structure item, which is evaluation of a constant string. *)
22-
PStr [{ pstr_desc =
23-
Pstr_eval ({ pexp_loc = loc;
24-
pexp_desc = Pexp_constant (Pconst_string (sym, s_loc, None)); _ }, _); _ }] ->
25-
(* Replace with a constant string with the value from the environment. *)
26-
Exp.constant ~loc (Pconst_string (getenv sym, s_loc, None))
27-
| _ ->
28-
raise (Location.Error (
29-
Location.error ~loc "[%getenv] accepts a string, e.g. [%getenv \"USER\"]"))
30-
end
31-
(* Delegate to the default mapper. *)
32-
| x -> default_mapper.expr mapper x;
33-
}
6+
let expander ~loc ~path:_ = function
7+
| (* Should have a single structure item, which is evaluation of a constant string. *)
8+
PStr [{ pstr_desc =
9+
Pstr_eval ({ pexp_loc = loc;
10+
pexp_desc = Pexp_constant (Pconst_string (sym, None)); _ }, _); _ }] ->
11+
(* Replace with a constant string with the value from the environment. *)
12+
Exp.constant ~loc (Pconst_string (getenv sym, None))
13+
| _ ->
14+
Location.raise_errorf ~loc "[%%getenv] accepts a string, e.g. [%%getenv \"USER\"]"
15+
16+
let extension =
17+
Context_free.Rule.extension
18+
(Extension.declare "getenv" Expression Ast_pattern.(__) expander)
3419

35-
let () = Migrate_parsetree.Driver.register ~name:"getenv" ocaml_version getenv_mapper
20+
let () = Ppxlib.Driver.register_transformation ~rules:[extension] "ppx_getenv"

0 commit comments

Comments
 (0)