Skip to content

Switch from OMP to ppxlib #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ env:
global:
- PACKAGE="ppx_getenv"
matrix:
- DISTRO=debian-stable OCAML_VERSION=4.02
- DISTRO=debian-stable OCAML_VERSION=4.03
- DISTRO=debian-stable OCAML_VERSION=4.04
- DISTRO=debian-stable OCAML_VERSION=4.05
- DISTRO=debian-stable OCAML_VERSION=4.06
Expand Down
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(synopsis "A sample syntax extension using OCaml's new extension points API")
(tags ("syntax"))
(depends
(ocaml (>= 4.02.0))
(ocaml-migrate-parsetree (>= 1.7.0))
(ocaml (>= 4.04.0))
(ppxlib (>= 0.9.0))
(ounit2 :with-test)
(odoc :with-doc)))
4 changes: 2 additions & 2 deletions ppx_getenv.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ homepage: "https://github.com/ocaml-ppx/ppx_getenv"
bug-reports: "https://github.com/ocaml-ppx/ppx_getenv/issues"
depends: [
"dune" {>= "2.0"}
"ocaml" {>= "4.02.0"}
"ocaml-migrate-parsetree" {>= "1.7.0"}
"ocaml" {>= "4.04.0"}
"ppxlib" {>= "0.9.0"}
"ounit2" {with-test}
"odoc" {with-doc}
]
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
(name ppx_getenv)
(public_name ppx_getenv)
(kind ppx_rewriter)
(libraries ocaml-migrate-parsetree))
(libraries ppxlib))
47 changes: 16 additions & 31 deletions src/ppx_getenv.ml
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
open Migrate_parsetree.OCaml_411.Ast
let ocaml_version = Migrate_parsetree.Versions.ocaml_411

open Ast_mapper
open Ast_helper
open Asttypes
open Parsetree
open Ppxlib
open Ppxlib.Ast_helper

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

let getenv_mapper _config _cookies =
(* Our getenv_mapper only overrides the handling of expressions in the default mapper. *)
{ default_mapper with
expr = fun mapper expr ->
match expr with
(* Is this an extension node? *)
| { pexp_desc =
(* Should have name "getenv". *)
Pexp_extension ({ txt = "getenv"; loc }, pstr); _ } ->
begin match pstr with
| (* Should have a single structure item, which is evaluation of a constant string. *)
PStr [{ pstr_desc =
Pstr_eval ({ pexp_loc = loc;
pexp_desc = Pexp_constant (Pconst_string (sym, s_loc, None)); _ }, _); _ }] ->
(* Replace with a constant string with the value from the environment. *)
Exp.constant ~loc (Pconst_string (getenv sym, s_loc, None))
| _ ->
raise (Location.Error (
Location.error ~loc "[%getenv] accepts a string, e.g. [%getenv \"USER\"]"))
end
(* Delegate to the default mapper. *)
| x -> default_mapper.expr mapper x;
}
let expander ~loc ~path:_ = function
| (* Should have a single structure item, which is evaluation of a constant string. *)
PStr [{ pstr_desc =
Pstr_eval ({ pexp_loc = loc;
pexp_desc = Pexp_constant (Pconst_string (sym, None)); _ }, _); _ }] ->
(* Replace with a constant string with the value from the environment. *)
Exp.constant ~loc (Pconst_string (getenv sym, None))
| _ ->
Location.raise_errorf ~loc "[%%getenv] accepts a string, e.g. [%%getenv \"USER\"]"

let extension =
Context_free.Rule.extension
(Extension.declare "getenv" Expression Ast_pattern.(__) expander)

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