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

Commit 1a55362

Browse files
committed
Convert to ppxlib
1 parent 8e81c14 commit 1a55362

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

bin/Bin.re

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
open Migrate_parsetree;
2-
open Let_anything_lib;
1+
open Ppxlib;
32

4-
let _ = Driver.run_as_ppx_rewriter();
3+
let _ = Driver.standalone();

src/Let_anything.re

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
RANDLE, JOSH ROBERTSON, OR OTHER MEMEBERS OF THE BLOOM BUILT TEAM.
55
*/
66

7-
open Migrate_parsetree;
8-
open Ast_406;
7+
open Ppxlib;
98

109
/*
1110
* https://ocsigen.org/lwt/dev/api/Ppx_lwt
1211
* https://github.com/zepalmer/ocaml-monadic
1312
*/
14-
let fail = (loc, txt) => raise(Location.Error(Location.error(~loc, txt)));
13+
let fail = (loc, txt) => Location.raise_errorf(~loc, txt);
14+
15+
let mkloc = (txt, loc) => {
16+
{Location.txt, loc};
17+
};
18+
19+
let lid_last = fun
20+
| Lident(s) => s
21+
| Ldot(_, s) => s
22+
| Lapply(_, _) => failwith("lid_last on functor application")
23+
1524

1625
let rec process_bindings = (bindings, ident) =>
1726
Parsetree.(
@@ -29,7 +38,7 @@ let rec process_bindings = (bindings, ident) =>
2938
~loc=binding.pvb_loc,
3039
Ast_helper.Exp.ident(
3140
~loc=binding.pvb_loc,
32-
Location.mkloc(Longident.Ldot(ident, "and_"), binding.pvb_loc),
41+
mkloc(Longident.Ldot(ident, "and_"), binding.pvb_loc),
3342
),
3443
[(Nolabel, binding.pvb_expr), (Nolabel, expr)],
3544
),
@@ -50,10 +59,12 @@ let parseLongident = txt => {
5059
loop(None, parts);
5160
};
5261

53-
let mapper = (_config, _cookies) => {
54-
...Ast_mapper.default_mapper,
55-
/* TODO throw error on structure items */
56-
expr: (mapper, expr) =>
62+
class mapper = {
63+
as _;
64+
inherit class Ast_traverse.map as super;
65+
66+
pub! expression = expr => {
67+
/* TODO throw error on structure items */
5768
switch (expr.pexp_desc) {
5869
| Pexp_extension((
5970
{txt, loc},
@@ -63,15 +74,15 @@ let mapper = (_config, _cookies) => {
6374
Pstr_eval(
6475
{pexp_loc, pexp_desc: Pexp_try(value, handlers), _},
6576
_attributes,
66-
),
77+
),
6778
_
6879
},
6980
]),
7081
)) =>
7182
let ident = parseLongident(txt);
72-
let last = Longident.last(ident);
83+
let last = lid_last(ident);
7384
if (last != String.capitalize_ascii(last)) {
74-
Ast_mapper.default_mapper.expr(mapper, expr);
85+
super#expression(expr);
7586
} else {
7687
let handlerLocStart = List.hd(handlers).pc_lhs.ppat_loc;
7788
let handlerLocEnd =
@@ -80,13 +91,13 @@ let mapper = (_config, _cookies) => {
8091
let try_ =
8192
Ast_helper.Exp.ident(
8293
~loc=pexp_loc,
83-
Location.mkloc(Longident.Ldot(ident, "try_"), loc),
94+
mkloc(Longident.Ldot(ident, "try_"), loc),
8495
);
8596
Ast_helper.Exp.apply(
8697
~loc,
8798
try_,
8899
[
89-
(Nolabel, mapper.expr(mapper, value)),
100+
(Nolabel, super#expression(value)),
90101
(Nolabel, Ast_helper.Exp.function_(~loc=handlerLoc, handlers)),
91102
],
92103
);
@@ -105,42 +116,43 @@ let mapper = (_config, _cookies) => {
105116
]),
106117
)) =>
107118
let ident = parseLongident(txt);
108-
let last = Longident.last(ident);
119+
let last = lid_last(ident);
109120
if (last != String.capitalize_ascii(last)) {
110-
Ast_mapper.default_mapper.expr(mapper, expr);
121+
super#expression(expr);
111122
} else {
112123
let (pat, expr) = process_bindings(bindings, ident);
113124
let let_ =
114125
Ast_helper.Exp.ident(
115126
~loc,
116-
Location.mkloc(Longident.Ldot(ident, "let_"), loc),
127+
mkloc(Longident.Ldot(ident, "let_"), loc),
117128
);
118129
Ast_helper.Exp.apply(
119130
~loc,
120131
let_,
121132
[
122-
(Nolabel, mapper.expr(mapper, expr)),
133+
(Nolabel, super#expression(expr)),
123134
(
124135
Nolabel,
125136
Ast_helper.Exp.fun_(
126137
~loc,
127138
Nolabel,
128139
None,
129140
pat,
130-
mapper.expr(mapper, continuation),
141+
super#expression(continuation),
131142
),
132143
),
133144
],
134145
);
135146
};
136-
| _ => Ast_mapper.default_mapper.expr(mapper, expr)
137-
},
147+
| _ => super#expression(expr)
148+
};
149+
};
138150
};
139151

152+
let structure_mapper = s => (new mapper)#structure(s);
153+
140154
let () =
141-
Migrate_parsetree.Driver.register(
142-
~name="let-anything",
143-
~args=[],
144-
Migrate_parsetree.Versions.ocaml_406,
145-
mapper,
155+
Ppxlib.Driver.register_transformation(
156+
~preprocess_impl=structure_mapper,
157+
"let-anything",
146158
);

src/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
(library
22
(name let_anything_lib)
3-
(libraries ocaml-migrate-parsetree str))
3+
(libraries ppxlib str)
4+
(preprocess (pps ppxlib.metaquot)))

0 commit comments

Comments
 (0)