Skip to content

Commit

Permalink
Merge pull request #7 from kit-ty-kate/ppxlib
Browse files Browse the repository at this point in the history
Switch from OMP to ppxlib
  • Loading branch information
kit-ty-kate authored Aug 12, 2020
2 parents cae3854 + 40485c3 commit 4b75f60
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 38 deletions.
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"

0 comments on commit 4b75f60

Please sign in to comment.