Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
Convert to ppxlib
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Apr 3, 2021
1 parent 2f1558e commit 971f44d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
5 changes: 2 additions & 3 deletions bin/Bin.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open Migrate_parsetree;
open Let_anything_lib;
open Ppxlib;

let _ = Driver.run_as_ppx_rewriter();
let _ = Driver.standalone();
62 changes: 37 additions & 25 deletions src/Let_anything.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
RANDLE, JOSH ROBERTSON, OR OTHER MEMEBERS OF THE BLOOM BUILT TEAM.
*/

open Migrate_parsetree;
open Ast_406;
open Ppxlib;

/*
* https://ocsigen.org/lwt/dev/api/Ppx_lwt
* https://github.com/zepalmer/ocaml-monadic
*/
let fail = (loc, txt) => raise(Location.Error(Location.error(~loc, txt)));
let fail = (loc, txt) => Location.raise_errorf(~loc, txt);

let mkloc = (txt, loc) => {
{Location.txt, loc};
};

let lid_last = fun
| Lident(s) => s
| Ldot(_, s) => s
| Lapply(_, _) => failwith("lid_last on functor application")


let rec process_bindings = (bindings, ident) =>
Parsetree.(
Expand All @@ -29,7 +38,7 @@ let rec process_bindings = (bindings, ident) =>
~loc=binding.pvb_loc,
Ast_helper.Exp.ident(
~loc=binding.pvb_loc,
Location.mkloc(Longident.Ldot(ident, "and_"), binding.pvb_loc),
mkloc(Longident.Ldot(ident, "and_"), binding.pvb_loc),
),
[(Nolabel, binding.pvb_expr), (Nolabel, expr)],
),
Expand All @@ -50,10 +59,12 @@ let parseLongident = txt => {
loop(None, parts);
};

let mapper = (_config, _cookies) => {
...Ast_mapper.default_mapper,
/* TODO throw error on structure items */
expr: (mapper, expr) =>
class mapper = {
as _;
inherit class Ast_traverse.map as super;

pub! expression = expr => {
/* TODO throw error on structure items */
switch (expr.pexp_desc) {
| Pexp_extension((
{txt, loc},
Expand All @@ -63,15 +74,15 @@ let mapper = (_config, _cookies) => {
Pstr_eval(
{pexp_loc, pexp_desc: Pexp_try(value, handlers), _},
_attributes,
),
),
_
},
]),
)) =>
let ident = parseLongident(txt);
let last = Longident.last(ident);
let last = lid_last(ident);
if (last != String.capitalize_ascii(last)) {
Ast_mapper.default_mapper.expr(mapper, expr);
super#expression(expr);
} else {
let handlerLocStart = List.hd(handlers).pc_lhs.ppat_loc;
let handlerLocEnd =
Expand All @@ -80,13 +91,13 @@ let mapper = (_config, _cookies) => {
let try_ =
Ast_helper.Exp.ident(
~loc=pexp_loc,
Location.mkloc(Longident.Ldot(ident, "try_"), loc),
mkloc(Longident.Ldot(ident, "try_"), loc),
);
Ast_helper.Exp.apply(
~loc,
try_,
[
(Nolabel, mapper.expr(mapper, value)),
(Nolabel, super#expression(value)),
(Nolabel, Ast_helper.Exp.function_(~loc=handlerLoc, handlers)),
],
);
Expand All @@ -105,42 +116,43 @@ let mapper = (_config, _cookies) => {
]),
)) =>
let ident = parseLongident(txt);
let last = Longident.last(ident);
let last = lid_last(ident);
if (last != String.capitalize_ascii(last)) {
Ast_mapper.default_mapper.expr(mapper, expr);
super#expression(expr);
} else {
let (pat, expr) = process_bindings(bindings, ident);
let let_ =
Ast_helper.Exp.ident(
~loc,
Location.mkloc(Longident.Ldot(ident, "let_"), loc),
mkloc(Longident.Ldot(ident, "let_"), loc),
);
Ast_helper.Exp.apply(
~loc,
let_,
[
(Nolabel, mapper.expr(mapper, expr)),
(Nolabel, super#expression(expr)),
(
Nolabel,
Ast_helper.Exp.fun_(
~loc,
Nolabel,
None,
pat,
mapper.expr(mapper, continuation),
super#expression(continuation),
),
),
],
);
};
| _ => Ast_mapper.default_mapper.expr(mapper, expr)
},
| _ => super#expression(expr)
};
};
};

let structure_mapper = s => (new mapper)#structure(s);

let () =
Migrate_parsetree.Driver.register(
~name="let-anything",
~args=[],
Migrate_parsetree.Versions.ocaml_406,
mapper,
Ppxlib.Driver.register_transformation(
~preprocess_impl=structure_mapper,
"let-anything",
);
3 changes: 2 additions & 1 deletion src/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(library
(name let_anything_lib)
(libraries ocaml-migrate-parsetree str))
(libraries ppxlib str)
(preprocess (pps ppxlib.metaquot)))

0 comments on commit 971f44d

Please sign in to comment.