-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from kit-ty-kate/ppxlib
Switch from OMP to ppxlib
- Loading branch information
Showing
5 changed files
with
21 additions
and
38 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
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
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 |
---|---|---|
@@ -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" |