-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathbs_builtin_ppx.ml
528 lines (510 loc) · 18.4 KB
/
bs_builtin_ppx.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(* When we design a ppx, we should keep it simple, and also think about
how it would work with other tools like merlin and ocamldep *)
(**
1. extension point
{[
[%bs.raw{| blabla |}]
]}
will be desugared into
{[
let module Js =
struct unsafe_js : string -> 'a end
in Js.unsafe_js {| blabla |}
]}
The major benefit is to better error reporting (with locations).
Otherwise
{[
let f u = Js.unsafe_js u
let _ = f (1 + 2)
]}
And if it is inlined some where
*)
let () =
Ast_derive_projector.init ();
Ast_derive_js_mapper.init ()
let succeed attr attrs =
match attrs with
| [ _ ] -> ()
| _ ->
Bs_ast_invariant.mark_used_bs_attribute attr;
Bs_ast_invariant.warn_discarded_unused_attributes attrs
type mapper = Bs_ast_mapper.mapper
let default_mapper = Bs_ast_mapper.default_mapper
let default_expr_mapper = Bs_ast_mapper.default_mapper.expr
let default_pat_mapper = Bs_ast_mapper.default_mapper.pat
let pat_mapper (self : mapper) (e : Parsetree.pattern) =
match e.ppat_desc with
| Ppat_constant (Pconst_integer (s, Some 'l')) ->
{ e with ppat_desc = Ppat_constant (Pconst_integer (s, None)) }
| _ -> default_pat_mapper self e
let expr_mapper ~async_context ~in_function_def (self : mapper)
(e : Parsetree.expression) =
let old_in_function_def = !in_function_def in
in_function_def := false;
match e.pexp_desc with
(* Its output should not be rewritten anymore *)
| Pexp_extension extension ->
Ast_exp_extension.handle_extension e self extension
| Pexp_setinstvar ({ txt; loc }, expr) ->
if Stack.is_empty Js_config.self_stack then
Location.raise_errorf ~loc:e.pexp_loc
"This assignment can only happen in object context";
let name = Stack.top Js_config.self_stack in
if name = "" then
Location.raise_errorf ~loc:e.pexp_loc
"The current object does not assign a name";
let open Ast_helper in
self.expr self
(Exp.apply ~loc:e.pexp_loc
(Exp.ident ~loc { loc; txt = Lident "#=" })
[
( Nolabel,
Exp.send ~loc
(Exp.ident ~loc { loc; txt = Lident name })
{ loc; txt } );
(Nolabel, expr);
])
| Pexp_constant (Pconst_string (s, Some delim)) ->
Ast_utf8_string_interp.transform e s delim
| Pexp_constant (Pconst_integer (s, Some 'l')) ->
{ e with pexp_desc = Pexp_constant (Pconst_integer (s, None)) }
(* End rewriting *)
| Pexp_function cases -> (
(* {[ function [@bs.exn]
| Not_found -> 0
| Invalid_argument -> 1
]}*)
async_context := false;
match
Ast_attributes.process_pexp_fun_attributes_rev e.pexp_attributes
with
| false, _ -> default_expr_mapper self e
| true, pexp_attributes ->
Ast_bs_open.convertBsErrorFunction e.pexp_loc self pexp_attributes
cases)
| _
when Ast_uncurried.exprIsUncurriedFun e
&&
match
Ast_attributes.process_attributes_rev
(Ast_uncurried.exprExtractUncurriedFun e).pexp_attributes
with
| Meth_callback _, _ -> true
| _ -> false ->
(* Treat @this (. x, y, z) => ... just like @this (x, y, z) => ... *)
let fun_expr = Ast_uncurried.exprExtractUncurriedFun e in
self.expr self fun_expr
| Pexp_fun (label, _, pat, body) -> (
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
match Ast_attributes.process_attributes_rev e.pexp_attributes with
| Nothing, _ ->
(* Handle @async x => y => ... is in async context *)
async_context := (old_in_function_def && !async_context) || async;
in_function_def := true;
Ast_async.make_function_async ~async (default_expr_mapper self e)
| Uncurry _, pexp_attributes ->
async_context := async;
Ast_uncurry_gen.to_uncurry_fn { e with pexp_attributes } self label
pat body async
| Method _, _ ->
Location.raise_errorf ~loc:e.pexp_loc
"%@meth is not supported in function expression"
| Meth_callback _, pexp_attributes ->
(* FIXME: does it make sense to have a label for [this] ? *)
async_context := false;
{
e with
pexp_desc =
Ast_uncurry_gen.to_method_callback e.pexp_loc self label pat body;
pexp_attributes;
})
| Pexp_apply (fn, args) -> Ast_exp_apply.app_exp_mapper e self fn args
| Pexp_match
( b,
[
{
pc_lhs =
{ ppat_desc = Ppat_construct ({ txt = Lident "true" }, None) };
pc_guard = None;
pc_rhs = t_exp;
};
{
pc_lhs =
{ ppat_desc = Ppat_construct ({ txt = Lident "false" }, None) };
pc_guard = None;
pc_rhs = f_exp;
};
] )
| Pexp_match
( b,
[
{
pc_lhs =
{ ppat_desc = Ppat_construct ({ txt = Lident "false" }, None) };
pc_guard = None;
pc_rhs = f_exp;
};
{
pc_lhs =
{ ppat_desc = Ppat_construct ({ txt = Lident "true" }, None) };
pc_guard = None;
pc_rhs = t_exp;
};
] ) ->
default_expr_mapper self
{ e with pexp_desc = Pexp_ifthenelse (b, t_exp, Some f_exp) }
| Pexp_let
( Nonrecursive,
[
{
pvb_pat =
( { ppat_desc = Ppat_record _ }
| { ppat_desc = Ppat_alias ({ ppat_desc = Ppat_record _ }, _) } )
as p;
pvb_expr;
pvb_attributes;
pvb_loc = _;
};
],
body ) -> (
match pvb_expr.pexp_desc with
| Pexp_pack _ -> default_expr_mapper self e
| _ ->
default_expr_mapper self
{
e with
pexp_desc =
Pexp_match
(pvb_expr, [ { pc_lhs = p; pc_guard = None; pc_rhs = body } ]);
pexp_attributes = e.pexp_attributes @ pvb_attributes;
})
(* let [@warning "a"] {a;b} = c in body
The attribute is attached to value binding,
after the transformation value binding does not exist so we attach
the attribute to the whole expression, in general, when shuffuling the ast
it is very hard to place attributes correctly
*)
| _ -> default_expr_mapper self e
let expr_mapper ~async_context ~in_function_def (self : mapper)
(e : Parsetree.expression) =
let async_saved = !async_context in
let result = expr_mapper ~async_context ~in_function_def self e in
async_context := async_saved;
match Ast_attributes.has_await_payload e.pexp_attributes with
| None -> result
| Some _ ->
if !async_context = false then
Location.raise_errorf ~loc:e.pexp_loc
"Await on expression not in an async context";
Ast_await.create_await_expression result
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
Ast_core_type_class_type.typ_mapper self typ
let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
Parsetree.signature_item =
match sigi.psig_desc with
| Psig_type (rf, tdcls) -> Ast_tdcls.handleTdclsInSigi self sigi rf tdcls
| Psig_value ({ pval_attributes; pval_prim } as value_desc) -> (
let pval_attributes = self.attributes self pval_attributes in
if Ast_attributes.rs_externals pval_attributes pval_prim then
Ast_external.handleExternalInSig self value_desc sigi
else
match Ast_attributes.has_inline_payload pval_attributes with
| Some
((_, PStr [ { pstr_desc = Pstr_eval ({ pexp_desc }, _) } ]) as attr)
-> (
match pexp_desc with
| Pexp_constant (Pconst_string (s, dec)) ->
succeed attr pval_attributes;
{
sigi with
psig_desc =
Psig_value
{
value_desc with
pval_prim =
External_ffi_types.inline_string_primitive s dec;
pval_attributes = [];
};
}
| Pexp_constant (Pconst_integer (s, None)) ->
succeed attr pval_attributes;
let s = Int32.of_string s in
{
sigi with
psig_desc =
Psig_value
{
value_desc with
pval_prim = External_ffi_types.inline_int_primitive s;
pval_attributes = [];
};
}
| Pexp_constant (Pconst_integer (s, Some 'L')) ->
let s = Int64.of_string s in
succeed attr pval_attributes;
{
sigi with
psig_desc =
Psig_value
{
value_desc with
pval_prim = External_ffi_types.inline_int64_primitive s;
pval_attributes = [];
};
}
| Pexp_constant (Pconst_float (s, None)) ->
succeed attr pval_attributes;
{
sigi with
psig_desc =
Psig_value
{
value_desc with
pval_prim = External_ffi_types.inline_float_primitive s;
pval_attributes = [];
};
}
| Pexp_construct ({ txt = Lident (("true" | "false") as txt) }, None)
->
succeed attr pval_attributes;
{
sigi with
psig_desc =
Psig_value
{
value_desc with
pval_prim =
External_ffi_types.inline_bool_primitive (txt = "true");
pval_attributes = [];
};
}
| _ -> default_mapper.signature_item self sigi)
| Some _ | None -> default_mapper.signature_item self sigi)
| _ -> default_mapper.signature_item self sigi
let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
Parsetree.structure_item =
match str.pstr_desc with
| Pstr_type (rf, tdcls) (* [ {ptype_attributes} as tdcl ] *) ->
Ast_tdcls.handleTdclsInStru self str rf tdcls
| Pstr_primitive prim
when Ast_attributes.rs_externals prim.pval_attributes prim.pval_prim ->
Ast_external.handleExternalInStru self prim str
| Pstr_value
( Nonrecursive,
[
{
pvb_pat = { ppat_desc = Ppat_var pval_name } as pvb_pat;
pvb_expr;
pvb_attributes;
pvb_loc;
};
] ) -> (
let pvb_expr = self.expr self pvb_expr in
let pvb_attributes = self.attributes self pvb_attributes in
let has_inline_property =
Ast_attributes.has_inline_payload pvb_attributes
in
match (has_inline_property, pvb_expr.pexp_desc) with
| Some attr, Pexp_constant (Pconst_string (s, dec)) ->
succeed attr pvb_attributes;
{
str with
pstr_desc =
Pstr_primitive
{
pval_name;
pval_type = Ast_literal.type_string ();
pval_loc = pvb_loc;
pval_attributes = [];
pval_prim = External_ffi_types.inline_string_primitive s dec;
};
}
| Some attr, Pexp_constant (Pconst_integer (s, None)) ->
let s = Int32.of_string s in
succeed attr pvb_attributes;
{
str with
pstr_desc =
Pstr_primitive
{
pval_name;
pval_type = Ast_literal.type_int ();
pval_loc = pvb_loc;
pval_attributes = [];
pval_prim = External_ffi_types.inline_int_primitive s;
};
}
| Some attr, Pexp_constant (Pconst_integer (s, Some 'L')) ->
let s = Int64.of_string s in
succeed attr pvb_attributes;
{
str with
pstr_desc =
Pstr_primitive
{
pval_name;
pval_type = Ast_literal.type_int64;
pval_loc = pvb_loc;
pval_attributes = [];
pval_prim = External_ffi_types.inline_int64_primitive s;
};
}
| Some attr, Pexp_constant (Pconst_float (s, None)) ->
succeed attr pvb_attributes;
{
str with
pstr_desc =
Pstr_primitive
{
pval_name;
pval_type = Ast_literal.type_float;
pval_loc = pvb_loc;
pval_attributes = [];
pval_prim = External_ffi_types.inline_float_primitive s;
};
}
| ( Some attr,
Pexp_construct ({ txt = Lident (("true" | "false") as txt) }, None) )
->
succeed attr pvb_attributes;
{
str with
pstr_desc =
Pstr_primitive
{
pval_name;
pval_type = Ast_literal.type_bool ();
pval_loc = pvb_loc;
pval_attributes = [];
pval_prim =
External_ffi_types.inline_bool_primitive (txt = "true");
};
}
| _ ->
{
str with
pstr_desc =
Pstr_value
( Nonrecursive,
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
})
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
| _ -> default_mapper.structure_item self str
let local_module_name =
let v = ref 0 in
fun () ->
incr v;
"local_" ^ string_of_int !v
let expand_reverse (stru : Ast_structure.t) (acc : Ast_structure.t) :
Ast_structure.t =
if stru = [] then acc
else (
Typemod_hide.check stru;
let local_module_name = local_module_name () in
let last_loc = (List.hd stru).pstr_loc in
let stru = List.rev stru in
let first_loc = (List.hd stru).pstr_loc in
let loc = { first_loc with loc_end = last_loc.loc_end } in
let open Ast_helper in
Str.module_ ~loc
{
pmb_name = { txt = local_module_name; loc };
pmb_expr =
{
pmod_desc = Pmod_structure stru;
pmod_loc = loc;
pmod_attributes = [];
};
pmb_attributes = Typemod_hide.attrs;
pmb_loc = loc;
}
:: Str.open_ ~loc
{
popen_lid = { txt = Lident local_module_name; loc };
popen_override = Override;
popen_loc = loc;
popen_attributes = [];
}
:: acc)
let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
match stru with
| [] -> []
| item :: rest -> (
match item.pstr_desc with
| Pstr_extension (({ txt = "bs.raw" | "raw"; loc }, payload), _attrs) ->
Ast_exp_handle_external.handle_raw_structure loc payload
:: structure_mapper self rest
(* | Pstr_extension (({txt = "i"}, _),_)
->
structure_mapper self rest *)
| Pstr_extension (({ txt = "private" }, _), _) ->
let rec aux acc (rest : Ast_structure.t) =
match rest with
| {
pstr_desc =
Pstr_extension (({ txt = "private"; loc }, payload), _);
}
:: next -> (
match payload with
| PStr work ->
aux
(Ext_list.rev_map_append work acc (fun x ->
self.structure_item self x))
next
| PSig _ | PTyp _ | PPat _ ->
Location.raise_errorf ~loc
"private extension is not support")
| _ -> expand_reverse acc (structure_mapper self rest)
in
aux [] stru
| _ -> self.structure_item self item :: structure_mapper self rest)
let mapper : mapper =
{
default_mapper with
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
pat = pat_mapper;
typ = typ_mapper;
signature_item = signature_item_mapper;
value_bindings = Ast_tuple_pattern_flatten.value_bindings_mapper;
structure_item = structure_item_mapper;
structure = structure_mapper;
(* Ad-hoc way to internalize stuff *)
label_declaration =
(fun self lbl ->
let lbl = default_mapper.label_declaration self lbl in
match lbl.pld_attributes with
| [ ({ txt = "internal" }, _) ] ->
{
lbl with
pld_name =
{
lbl.pld_name with
txt = String.capitalize_ascii lbl.pld_name.txt;
};
pld_attributes = [];
}
| _ -> lbl);
}