4
4
RANDLE, JOSH ROBERTSON, OR OTHER MEMEBERS OF THE BLOOM BUILT TEAM.
5
5
*/
6
6
7
- open Migrate_parsetree ;
8
- open Ast_406 ;
7
+ open Ppxlib ;
9
8
10
9
/*
11
10
* https://ocsigen.org/lwt/dev/api/Ppx_lwt
12
11
* https://github.com/zepalmer/ocaml-monadic
13
12
*/
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
+
15
24
16
25
let rec process_bindings = (bindings, ident) =>
17
26
Parsetree . (
@@ -29,7 +38,7 @@ let rec process_bindings = (bindings, ident) =>
29
38
~loc= binding. pvb_loc,
30
39
Ast_helper . Exp . ident(
31
40
~loc= binding. pvb_loc,
32
- Location . mkloc(Longident . Ldot (ident, "and_" ), binding. pvb_loc),
41
+ mkloc(Longident . Ldot (ident, "and_" ), binding. pvb_loc),
33
42
),
34
43
[ (Nolabel , binding. pvb_expr), (Nolabel , expr)] ,
35
44
),
@@ -50,10 +59,12 @@ let parseLongident = txt => {
50
59
loop(None , parts);
51
60
};
52
61
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 */
57
68
switch (expr. pexp_desc) {
58
69
| Pexp_extension ((
59
70
{txt, loc},
@@ -63,15 +74,15 @@ let mapper = (_config, _cookies) => {
63
74
Pstr_eval (
64
75
{pexp_loc, pexp_desc: Pexp_try (value , handlers ), _ },
65
76
_attributes ,
66
- ),
77
+ ),
67
78
_
68
79
},
69
80
] ),
70
81
)) =>
71
82
let ident = parseLongident(txt);
72
- let last = Longident . last (ident);
83
+ let last = lid_last (ident);
73
84
if (last != String . capitalize_ascii(last)) {
74
- Ast_mapper . default_mapper . expr(mapper , expr);
85
+ super # expression( expr);
75
86
} else {
76
87
let handlerLocStart = List . hd(handlers).pc_lhs. ppat_loc;
77
88
let handlerLocEnd =
@@ -80,13 +91,13 @@ let mapper = (_config, _cookies) => {
80
91
let try_ =
81
92
Ast_helper . Exp . ident(
82
93
~loc= pexp_loc,
83
- Location . mkloc(Longident . Ldot (ident, "try_" ), loc),
94
+ mkloc(Longident . Ldot (ident, "try_" ), loc),
84
95
);
85
96
Ast_helper . Exp . apply(
86
97
~loc,
87
98
try_,
88
99
[
89
- (Nolabel , mapper . expr(mapper , value)),
100
+ (Nolabel , super # expression( value)),
90
101
(Nolabel , Ast_helper . Exp . function_(~loc= handlerLoc, handlers)),
91
102
] ,
92
103
);
@@ -105,42 +116,43 @@ let mapper = (_config, _cookies) => {
105
116
] ),
106
117
)) =>
107
118
let ident = parseLongident(txt);
108
- let last = Longident . last (ident);
119
+ let last = lid_last (ident);
109
120
if (last != String . capitalize_ascii(last)) {
110
- Ast_mapper . default_mapper . expr(mapper , expr);
121
+ super # expression( expr);
111
122
} else {
112
123
let (pat , expr ) = process_bindings(bindings, ident);
113
124
let let_ =
114
125
Ast_helper . Exp . ident(
115
126
~loc,
116
- Location . mkloc(Longident . Ldot (ident, "let_" ), loc),
127
+ mkloc(Longident . Ldot (ident, "let_" ), loc),
117
128
);
118
129
Ast_helper . Exp . apply(
119
130
~loc,
120
131
let_,
121
132
[
122
- (Nolabel , mapper . expr(mapper , expr)),
133
+ (Nolabel , super # expression( expr)),
123
134
(
124
135
Nolabel ,
125
136
Ast_helper . Exp . fun_(
126
137
~loc,
127
138
Nolabel ,
128
139
None ,
129
140
pat,
130
- mapper . expr(mapper , continuation),
141
+ super # expression( continuation),
131
142
),
132
143
),
133
144
] ,
134
145
);
135
146
};
136
- | _ => Ast_mapper . default_mapper. expr(mapper, expr)
137
- },
147
+ | _ => super# expression(expr)
148
+ };
149
+ };
138
150
};
139
151
152
+ let structure_mapper = s => (new mapper)# structure(s);
153
+
140
154
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" ,
146
158
);
0 commit comments