forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppx_entry.ml
310 lines (278 loc) · 11.1 KB
/
ppx_entry.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
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* 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
*)
open Ast_helper
let record_as_js_object = ref false (* otherwise has an attribute *)
let no_export = ref false
let () =
Ast_derive_projector.init ();
Ast_derive_js_mapper.init ()
let reset () =
record_as_js_object := false ;
no_export := false
let rec unsafe_mapper : Bs_ast_mapper.mapper =
{ Bs_ast_mapper.default_mapper with
expr = (fun self ({ pexp_loc = loc } as e) ->
match e.pexp_desc with
(** Its output should not be rewritten anymore *)
| Pexp_extension extension ->
Ast_exp_extension.handle_extension record_as_js_object e self extension
| Pexp_constant (Const_string (s, (Some delim)))
->
if Ext_string.equal delim Literals.unescaped_js_delimiter then
let js_str = Ast_utf8_string.transform loc s in
{ e with pexp_desc =
Pexp_constant (Const_string (js_str, Some Literals.escaped_j_delimiter))}
else if Ext_string.equal delim Literals.unescaped_j_delimiter then
Ast_utf8_string_interp.transform_interp loc s
else e
(** End rewriting *)
| Pexp_function cases ->
(* {[ function [@bs.exn]
| Not_found -> 0
| Invalid_argument -> 1
]}*)
begin match
Ast_attributes.process_pexp_fun_attributes_rev e.pexp_attributes
with
| `Nothing, _ ->
Bs_ast_mapper.default_mapper.expr self e
| `Exn, pexp_attributes ->
Ast_util.convertBsErrorFunction loc self pexp_attributes cases
end
| Pexp_fun (arg_label, _, pat , body)
when Ast_compatible.is_arg_label_simple arg_label
->
begin match Ast_attributes.process_attributes_rev e.pexp_attributes with
| Nothing, _
-> Bs_ast_mapper.default_mapper.expr self e
| Uncurry _, pexp_attributes
->
{e with
pexp_desc = Ast_util.to_uncurry_fn loc self pat body ;
pexp_attributes}
| Method _ , _
-> Location.raise_errorf ~loc "bs.meth is not supported in function expression"
| Meth_callback _, pexp_attributes
->
{e with pexp_desc = Ast_util.to_method_callback loc self pat body ;
pexp_attributes }
end
| Pexp_apply (fn, args ) ->
Ast_exp_apply.handle_exp_apply e self fn args
| Pexp_record (label_exprs, opt_exp) ->
if !record_as_js_object then
(match opt_exp with
| None ->
{ e with
pexp_desc =
Ast_util.record_as_js_object loc self label_exprs;
}
| Some e ->
Location.raise_errorf
~loc:e.pexp_loc "`with` construct is not supported in bs.obj ")
else
(* could be supported using `Object.assign`?
type
{[
external update : 'a Js.t -> 'b Js.t -> 'a Js.t = ""
constraint 'b :> 'a
]}
*)
Bs_ast_mapper.default_mapper.expr self e
| Pexp_object {pcstr_self; pcstr_fields} ->
begin match Ast_attributes.process_bs e.pexp_attributes with
| `Has, pexp_attributes
->
{e with
pexp_desc =
Ast_util.ocaml_obj_as_js_object
loc self pcstr_self pcstr_fields;
pexp_attributes
}
| `Nothing , _ ->
Bs_ast_mapper.default_mapper.expr self e
end
| _ -> Bs_ast_mapper.default_mapper.expr self e
);
typ = (fun self typ ->
Ast_core_type_class_type.handle_core_type self typ record_as_js_object);
class_type =
(fun self ({pcty_attributes; pcty_loc} as ctd) ->
match Ast_attributes.process_bs pcty_attributes with
| `Nothing, _ ->
Bs_ast_mapper.default_mapper.class_type self ctd
| `Has, pcty_attributes ->
(match ctd.pcty_desc with
| Pcty_signature ({pcsig_self; pcsig_fields })
->
let pcsig_self = self.typ self pcsig_self in
{ctd with
pcty_desc = Pcty_signature {
pcsig_self ;
pcsig_fields = Ast_core_type_class_type.handle_class_type_fields self pcsig_fields
};
pcty_attributes
}
| Pcty_constr _
| Pcty_extension _
| Pcty_arrow _ ->
Location.raise_errorf ~loc:pcty_loc "invalid or unused attribute `bs`")
(* {[class x : int -> object
end [@bs]
]}
Actually this is not going to happpen as below is an invalid syntax
{[class type x = int -> object
end[@bs]]}
*)
);
signature_item = begin fun
(self : Bs_ast_mapper.mapper)
(sigi : Parsetree.signature_item) ->
match sigi.psig_desc with
| Psig_type (_ :: _ as tdcls) ->
Ast_tdcls.handleTdclsInSigi self sigi tdcls
| Psig_value prim
when Ast_attributes.process_external prim.pval_attributes
->
Ast_primitive.handlePrimitiveInSig self prim sigi
| _ -> Bs_ast_mapper.default_mapper.signature_item self sigi
end;
pat = begin fun self (pat : Parsetree.pattern) ->
match pat with
| { ppat_desc = Ppat_constant(Const_string (_, Some "j")); ppat_loc = loc} ->
Location.raise_errorf ~loc "Unicode string is not allowed in pattern match"
| _ -> Bs_ast_mapper.default_mapper.pat self pat
end;
value_bindings = Ast_tuple_pattern_flatten.handle_value_bindings;
structure_item = begin fun self (str : Parsetree.structure_item) ->
begin match str.pstr_desc with
| Pstr_extension ( ({txt = ("bs.raw"| "raw") ; loc}, payload), _attrs)
->
Ast_util.handle_raw_structure loc payload
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, payload),_)
->
if !Js_config.debug then
let open Ast_helper in
Str.eval ~loc (Ast_compatible.app1 ~loc
(Exp.ident ~loc {txt = Ldot(Ldot (Lident"Belt","Debug"), "setupChromeDebugger");loc} )
(Ast_literal.val_unit ~loc ())
)
else Ast_structure.dummy_item loc
| Pstr_type (_ :: _ as tdcls ) (* [ {ptype_attributes} as tdcl ] *)->
Ast_tdcls.handleTdclsInStru self str tdcls
| Pstr_primitive prim
when Ast_attributes.process_external prim.pval_attributes
->
Ast_primitive.handlePrimitiveInStru self prim str
| _ -> Bs_ast_mapper.default_mapper.structure_item self str
end
end
}
(** global configurations below *)
let common_actions_table :
(string * (Parsetree.expression option -> unit)) list =
[
]
let structural_config_table =
String_map.of_list
(( "no_export" ,
(fun x ->
no_export := (
match x with
|Some e -> Ast_payload.assert_bool_lit e
| None -> true)
))
:: common_actions_table)
let signature_config_table :
(Parsetree.expression option -> unit) String_map.t=
String_map.of_list common_actions_table
let dummy_unused_attribute : Warnings.t = (Bs_unused_attribute "")
let rewrite_signature :
(Parsetree.signature -> Parsetree.signature) ref =
ref (fun x ->
let result =
match (x : Parsetree.signature) with
| {psig_desc = Psig_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
->
begin
Ast_payload.ident_or_record_as_config loc payload
|> List.iter (Ast_payload.table_dispatch signature_config_table) ;
unsafe_mapper.signature unsafe_mapper rest
end
| _ ->
unsafe_mapper.signature unsafe_mapper x in
reset ();
(* Keep this check, since the check is not inexpensive*)
if Warnings.is_active dummy_unused_attribute then
Bs_ast_invariant.emit_external_warnings.signature Bs_ast_invariant.emit_external_warnings result ;
result
)
let rewrite_implementation : (Parsetree.structure -> Parsetree.structure) ref =
ref (fun (x : Parsetree.structure) ->
let result =
match x with
| {pstr_desc = Pstr_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
->
begin
Ast_payload.ident_or_record_as_config loc payload
|> List.iter (Ast_payload.table_dispatch structural_config_table) ;
let rest = unsafe_mapper.structure unsafe_mapper rest in
if !no_export then
[Str.include_ ~loc
(Incl.mk ~loc
(Mod.constraint_ ~loc
(Mod.structure ~loc rest )
(Mty.signature ~loc [])
))]
else rest
end
| _ ->
unsafe_mapper.structure unsafe_mapper x in
reset ();
(* Keep this check since it is not inexpensive*)
(if Warnings.is_active dummy_unused_attribute then
Bs_ast_invariant.emit_external_warnings.structure Bs_ast_invariant.emit_external_warnings result);
result
)