forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_util.ml
695 lines (621 loc) · 23.7 KB
/
ast_util.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
(* 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. *)
open Ast_helper
type 'a cxt = Ast_helper.loc -> Bs_ast_mapper.mapper -> 'a
type loc = Location.t
type args = (Ast_compatible.arg_label * Parsetree.expression) list
type label_exprs = (Longident.t Asttypes.loc * Parsetree.expression) list
type uncurry_expression_gen =
(Parsetree.pattern ->
Parsetree.expression ->
Parsetree.expression_desc) cxt
type uncurry_type_gen =
(Ast_compatible.arg_label ->
Parsetree.core_type ->
Parsetree.core_type ->
Parsetree.core_type) cxt
let uncurry_type_id =
Ast_literal.Lid.js_fn
let method_id =
Ast_literal.Lid.js_meth
let method_call_back_id =
Ast_literal.Lid.js_meth_callback
let arity_lit = "Arity_"
let mk_args loc (n : int) (tys : Parsetree.core_type list) : Parsetree.core_type =
Typ.variant ~loc
[ Rtag (
#if OCAML_VERSION =~ ">4.03.0" then
{loc; txt = arity_lit ^ string_of_int n}
#else
arity_lit ^ string_of_int n
#end
,
[], (n = 0), tys)] Closed None
let generic_lift txt loc args result =
let xs =
match args with
| [ ] -> [mk_args loc 0 [] ; result ]
| [ x ] -> [ mk_args loc 1 [x] ; result ]
| _ ->
[mk_args loc (List.length args ) [Typ.tuple ~loc args] ; result ]
in
Typ.constr ~loc {txt ; loc} xs
let lift_curry_type loc =
generic_lift uncurry_type_id loc
let lift_method_type loc =
generic_lift method_id loc
let lift_js_method_callback loc
=
generic_lift method_call_back_id loc
(** Note that currently there is no way to consume [Js.meth_callback]
so it is fine to encode it with a freedom,
but we need make it better for error message.
- all are encoded as
{[
type fn = (`Args_n of _ , 'result ) Js.fn
type method = (`Args_n of _, 'result) Js.method
type method_callback = (`Args_n of _, 'result) Js.method_callback
]}
For [method_callback], the arity is never zero, so both [method]
and [fn] requires (unit -> 'a) to encode arity zero
*)
let arrow = Ast_compatible.arrow
let js_property loc obj (name : string) =
Parsetree.Pexp_send
((Ast_compatible.app1 ~loc
(Exp.ident ~loc
{loc;
txt = Ldot (Ast_literal.Lid.js_unsafe, Literals.unsafe_downgrade)})
obj),
#if OCAML_VERSION =~ ">4.03.0" then
{loc; txt = name}
#else
name
#end
)
(* TODO:
have a final checking for property arities
[#=],
*)
let generic_apply kind loc
(self : Bs_ast_mapper.mapper)
(obj : Parsetree.expression)
(args : args ) cb =
let obj = self.expr self obj in
let args =
Ext_list.map args (fun (label,e) ->
if not (Ast_compatible.is_arg_label_simple label) then
Bs_syntaxerr.err loc Label_in_uncurried_bs_attribute;
self.expr self e
) in
let len = List.length args in
let arity, fn, args =
match args with
| [ {pexp_desc =
Pexp_construct ({txt = Lident "()"}, None)}]
->
0, cb loc obj, []
| _ ->
len, cb loc obj, args in
if arity < 10 then
let txt =
match kind with
| `Fn | `PropertyFn ->
Longident.Ldot (Ast_literal.Lid.js_unsafe,
Literals.fn_run ^ string_of_int arity)
| `Method ->
Longident.Ldot(Ast_literal.Lid.js_unsafe,
Literals.method_run ^ string_of_int arity
) in
Parsetree.Pexp_apply (Exp.ident {txt ; loc}, (Ast_compatible.no_label,fn) :: Ext_list.map args (fun x -> Ast_compatible.no_label,x))
else
let fn_type, args_type, result_type = Ast_comb.tuple_type_pair ~loc `Run arity in
let string_arity = string_of_int arity in
let pval_prim, pval_type =
match kind with
| `Fn | `PropertyFn ->
["#fn_run"; string_arity],
arrow ~loc (lift_curry_type loc args_type result_type ) fn_type
| `Method ->
["#method_run" ; string_arity],
arrow ~loc (lift_method_type loc args_type result_type) fn_type
in
Ast_external_mk.local_external_apply loc ~pval_prim ~pval_type
( fn :: args )
let uncurry_fn_apply loc self fn args =
generic_apply `Fn loc self fn args (fun _ obj -> obj )
let property_apply loc self obj name (args : args)
= generic_apply `PropertyFn loc self obj args
(fun loc obj -> Exp.mk ~loc (js_property loc obj name))
let method_apply loc self obj name args =
generic_apply `Method loc self obj args
(fun loc obj -> Exp.mk ~loc (js_property loc obj name))
let generic_to_uncurry_type kind loc (mapper : Bs_ast_mapper.mapper) label
(first_arg : Parsetree.core_type)
(typ : Parsetree.core_type) =
if not (Ast_compatible.is_arg_label_simple label) then
Bs_syntaxerr.err loc Label_in_uncurried_bs_attribute;
let rec aux acc (typ : Parsetree.core_type) =
(* in general,
we should collect [typ] in [int -> typ] before transformation,
however: when attributes [bs] and [bs.this] found in typ,
we should stop
*)
match Ast_attributes.process_attributes_rev typ.ptyp_attributes with
| Nothing, _ ->
begin match typ.ptyp_desc with
| Ptyp_arrow (label, arg, body)
->
if not (Ast_compatible.is_arg_label_simple label) then
Bs_syntaxerr.err typ.ptyp_loc Label_in_uncurried_bs_attribute;
aux (mapper.typ mapper arg :: acc) body
| _ -> mapper.typ mapper typ, acc
end
| _, _ -> mapper.typ mapper typ, acc
in
let first_arg = mapper.typ mapper first_arg in
let result, rev_extra_args = aux [first_arg] typ in
let args = List.rev rev_extra_args in
let filter_args args =
match args with
| [{Parsetree.ptyp_desc =
(Ptyp_constr ({txt = Lident "unit"}, [])
)}]
-> []
| _ -> args in
match kind with
| `Fn ->
let args = filter_args args in
lift_curry_type loc args result
| `Method ->
let args = filter_args args in
lift_method_type loc args result
| `Method_callback
-> lift_js_method_callback loc args result
let to_uncurry_type =
generic_to_uncurry_type `Fn
let to_method_type =
generic_to_uncurry_type `Method
let to_method_callback_type =
generic_to_uncurry_type `Method_callback
let generic_to_uncurry_exp kind loc (self : Bs_ast_mapper.mapper) pat body
=
let rec aux acc (body : Parsetree.expression) =
match Ast_attributes.process_attributes_rev body.pexp_attributes with
| Nothing, _ ->
begin match body.pexp_desc with
| Pexp_fun (arg_label,_, arg, body)
->
if not (Ast_compatible.is_arg_label_simple arg_label) then
Bs_syntaxerr.err loc Label_in_uncurried_bs_attribute;
aux (self.pat self arg :: acc) body
| _ -> self.expr self body, acc
end
| _, _ -> self.expr self body, acc
in
let first_arg = self.pat self pat in
let () =
match kind with
| `Method_callback ->
if not @@ Ast_pat.is_single_variable_pattern_conservative first_arg then
Bs_syntaxerr.err first_arg.ppat_loc Bs_this_simple_pattern
| _ -> ()
in
let result, rev_extra_args = aux [first_arg] body in
let body =
List.fold_left (fun e p -> Ast_compatible.fun_ ~loc p e )
result rev_extra_args in
let len = List.length rev_extra_args in
let arity =
match kind with
| `Fn ->
begin match rev_extra_args with
| [ p]
->
Ast_pat.is_unit_cont ~yes:0 ~no:len p
| _ -> len
end
| `Method_callback -> len in
if arity < 10 then
let txt =
match kind with
| `Fn ->
Longident.Ldot ( Ast_literal.Lid.js_unsafe, Literals.fn_mk ^ string_of_int arity)
| `Method_callback ->
Longident.Ldot (Ast_literal.Lid.js_unsafe, Literals.fn_method ^ string_of_int arity) in
Parsetree.Pexp_apply (Exp.ident {txt;loc} , [ Ast_compatible.no_label, body])
else
let pval_prim =
[ (match kind with
| `Fn -> "#fn_mk"
| `Method_callback -> "#fn_method");
string_of_int arity] in
let fn_type , args_type, result_type = Ast_comb.tuple_type_pair ~loc `Make arity in
let pval_type = arrow ~loc fn_type (
match kind with
| `Fn ->
lift_curry_type loc args_type result_type
| `Method_callback ->
lift_js_method_callback loc args_type result_type
) in
Ast_external_mk.local_extern_cont loc ~pval_prim ~pval_type
(fun prim -> Ast_compatible.app1 ~loc prim body)
let to_uncurry_fn =
generic_to_uncurry_exp `Fn
let to_method_callback =
generic_to_uncurry_exp `Method_callback
let handle_debugger loc (payload : Ast_payload.t) =
match payload with
| PStr [] ->
Parsetree.Pexp_apply
(Exp.ident {txt = Ldot(Ast_literal.Lid.js_unsafe, Literals.debugger ); loc},
[ Ast_compatible.no_label, Ast_literal.val_unit ~loc ()])
| _ ->
Location.raise_errorf ~loc "bs.debugger does not accept payload"
let handle_raw ~check_js_regex loc payload =
begin match Ast_payload.as_string_exp ~check_js_regex payload with
| Not_String_Lteral ->
Location.raise_errorf ~loc
"bs.raw can only be applied to a string"
| Ast_payload.JS_Regex_Check_Failed ->
Location.raise_errorf ~loc "this is an invalid js regex"
| Correct exp ->
let pexp_desc =
Parsetree.Pexp_apply (
Exp.ident {loc;
txt =
Ldot (Ast_literal.Lid.js_unsafe,
Literals.raw_expr)},
[Ast_compatible.no_label,exp]
)
in
{ exp with pexp_desc }
end
let handle_external loc x =
let raw_exp : Ast_exp.t =
Ast_compatible.app1
(Exp.ident ~loc
{loc; txt = Ldot (Ast_literal.Lid.js_unsafe,
Literals.raw_expr)})
~loc
(Ast_compatible.const_exp_string ~loc x ~delimiter:Ext_string.empty) in
let empty = (* FIXME: the empty delimiter does not make sense*)
Exp.ident ~loc
{txt = Ldot (Ldot(Lident"Js", "Undefined"), "empty");loc}
in
let undefined_typeof =
Exp.ident {loc ; txt = Ldot(Lident "Js","undefinedToOption")} in
let typeof =
Exp.ident {loc ; txt = Ldot(Lident "Js","typeof")} in
Ast_compatible.app1 ~loc undefined_typeof (
Exp.ifthenelse ~loc
(Ast_compatible.app2 ~loc
(Exp.ident ~loc {loc ; txt = Ldot (Lident "Pervasives", "=")} )
(Ast_compatible.app1 ~loc typeof raw_exp)
(Ast_compatible.const_exp_string ~loc "undefined")
)
empty
(Some raw_exp)
)
let handle_raw_structure loc payload =
begin match Ast_payload.as_string_exp ~check_js_regex:false payload with
| Correct exp
->
let pexp_desc =
Parsetree.Pexp_apply(
Exp.ident {txt = Ldot (Ast_literal.Lid.js_unsafe, Literals.raw_stmt); loc},
[ Ast_compatible.no_label,exp]) in
Ast_helper.Str.eval
{ exp with pexp_desc }
| Not_String_Lteral
->
Location.raise_errorf ~loc "bs.raw can only be applied to a string"
| JS_Regex_Check_Failed
->
Location.raise_errorf ~loc "this is an invalid js regex"
end
let ocaml_obj_as_js_object
loc (mapper : Bs_ast_mapper.mapper)
(self_pat : Parsetree.pattern)
(clfs : Parsetree.class_field list) =
let self_type_lit = "self_type" in
(** Attention: we should avoid type variable conflict for each method
Since the method name is unique, there would be no conflict
OCaml does not allow duplicate instance variable and duplicate methods,
but it does allow duplicates between instance variable and method name,
we should enforce such rules
{[
object
val x = 3
method x = 3
end [@bs]
]} should not compile with a meaningful error message
*)
let generate_val_method_pair
loc (mapper : Bs_ast_mapper.mapper)
val_name is_mutable =
let result = Typ.var ~loc val_name in
result ,
((val_name , [], result ) ::
(if is_mutable then
[val_name ^ Literals.setter_suffix,[],
to_method_type loc mapper Ast_compatible.no_label result (Ast_literal.type_unit ~loc ()) ]
else
[]) )
in
(* Note mapper is only for API compatible
* TODO: we should check label name to avoid conflict
*)
let self_type loc = Typ.var ~loc self_type_lit in
let generate_arg_type loc (mapper : Bs_ast_mapper.mapper)
method_name arity : Ast_core_type.t =
let result = Typ.var ~loc method_name in
if arity = 0 then
to_method_type loc mapper Ast_compatible.no_label (Ast_literal.type_unit ~loc ()) result
else
let tyvars =
Ext_list.init arity (fun i -> Typ.var ~loc (method_name ^ string_of_int i))
in
begin match tyvars with
| x :: rest ->
let method_rest =
Ext_list.fold_right rest result (fun v acc -> Ast_compatible.arrow ~loc v acc)
in
to_method_type loc mapper Ast_compatible.no_label x method_rest
| _ -> assert false
end in
let generate_method_type
loc
(mapper : Bs_ast_mapper.mapper)
?alias_type method_name arity =
let result = Typ.var ~loc method_name in
let self_type =
let v = self_type loc in
match alias_type with
| None -> v
| Some ty -> Typ.alias ~loc ty self_type_lit
in
if arity = 0 then
to_method_callback_type loc mapper Ast_compatible.no_label self_type result
else
let tyvars =
Ext_list.init arity (fun i -> Typ.var ~loc (method_name ^ string_of_int i))
in
begin match tyvars with
| x :: rest ->
let method_rest =
Ext_list.fold_right rest result (fun v acc -> Ast_compatible.arrow ~loc v acc)
in
(to_method_callback_type loc mapper Ast_compatible.no_label self_type
(Ast_compatible.arrow ~loc x method_rest))
| _ -> assert false
end in
(** we need calculate the real object type
and exposed object type, in some cases there are equivalent
for public object type its [@bs.meth] it does not depend on itself
while for label argument it is [@bs.this] which depends internal object
*)
let internal_label_attr_types, public_label_attr_types =
Ext_list.fold_right clfs ([], [])
(fun ({pcf_loc = loc} as x : Parsetree.class_field)
(label_attr_types, public_label_attr_types) ->
match x.pcf_desc with
| Pcf_method (
label,
public_flag,
Cfk_concrete
(Fresh, e))
->
begin match e.pexp_desc with
| Pexp_poly
(({pexp_desc = Pexp_fun (arg_label, _, pat, e)} ),
None)
when Ast_compatible.is_arg_label_simple arg_label
->
let arity = Ast_pat.arity_of_fun pat e in
let method_type =
generate_arg_type x.pcf_loc mapper label.txt arity in
((label.Asttypes.txt, [], method_type) :: label_attr_types),
(if public_flag = Public then
(label.Asttypes.txt, [], method_type) :: public_label_attr_types
else
public_label_attr_types)
| Pexp_poly( _, Some _)
->
Location.raise_errorf ~loc "polymorphic type annotation not supported yet"
| Pexp_poly (_, None) ->
Location.raise_errorf ~loc
"Unsupported syntax, expect syntax like `method x () = x ` "
| _ ->
Location.raise_errorf ~loc "Unsupported syntax in js object"
end
| Pcf_val (label, mutable_flag, Cfk_concrete(Fresh, val_exp)) ->
let label_type, label_attr =
generate_val_method_pair x.pcf_loc mapper label.txt
(mutable_flag = Mutable )
in
(Ext_list.append label_attr label_attr_types, public_label_attr_types)
| Pcf_val (label, mutable_flag, Cfk_concrete(Override, val_exp)) ->
Location.raise_errorf ~loc "override flag not support currently"
| Pcf_val (label, mutable_flag, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtual flag not support currently"
| Pcf_method (_, _, Cfk_concrete(Override, _) ) ->
Location.raise_errorf ~loc "override flag not supported"
| Pcf_method (_, _, Cfk_virtual _ )
->
Location.raise_errorf ~loc "virtural method not supported"
| Pcf_inherit _
| Pcf_initializer _
| Pcf_attribute _
| Pcf_extension _
| Pcf_constraint _ ->
Location.raise_errorf ~loc "Only method support currently"
) in
let internal_obj_type = Ast_core_type.make_obj ~loc internal_label_attr_types in
let public_obj_type = Ast_core_type.make_obj ~loc public_label_attr_types in
let (labels, label_types, exprs, _) =
Ext_list.fold_right clfs ([], [], [], false)
(fun (x : Parsetree.class_field)
(labels,
label_types,
exprs, aliased ) ->
match x.pcf_desc with
| Pcf_method (
label,
_public_flag,
Cfk_concrete
(Fresh, e))
->
begin match e.pexp_desc with
| Pexp_poly
(({pexp_desc = Pexp_fun (arg_label, None, pat, e)} as f),
None)
when Ast_compatible.is_arg_label_simple arg_label
->
let arity = Ast_pat.arity_of_fun pat e in
let alias_type =
if aliased then None
else Some internal_obj_type in
let label_type =
generate_method_type ?alias_type
x.pcf_loc mapper label.txt arity in
(label::labels,
label_type::label_types,
{f with
pexp_desc =
let f = Ast_pat.is_unit_cont pat ~yes:e ~no:f in
to_method_callback loc mapper self_pat f
} :: exprs,
true
)
| Pexp_poly( _, Some _)
->
Location.raise_errorf ~loc
"polymorphic type annotation not supported yet"
| Pexp_poly (_, None) ->
Location.raise_errorf
~loc "Unsupported syntax, expect syntax like `method x () = x ` "
| _ ->
Location.raise_errorf ~loc "Unsupported syntax in js object"
end
| Pcf_val (label, mutable_flag, Cfk_concrete(Fresh, val_exp)) ->
let label_type, label_attr =
generate_val_method_pair x.pcf_loc mapper label.txt
(mutable_flag = Mutable )
in
(label::labels,
label_type :: label_types,
(mapper.expr mapper val_exp :: exprs),
aliased
)
| Pcf_val (label, mutable_flag, Cfk_concrete(Override, val_exp)) ->
Location.raise_errorf ~loc "override flag not support currently"
| Pcf_val (label, mutable_flag, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtual flag not support currently"
| Pcf_method (_, _, Cfk_concrete(Override, _) ) ->
Location.raise_errorf ~loc "override flag not supported"
| Pcf_method (_, _, Cfk_virtual _ )
->
Location.raise_errorf ~loc "virtural method not supported"
| Pcf_inherit _
| Pcf_initializer _
| Pcf_attribute _
| Pcf_extension _
| Pcf_constraint _ ->
Location.raise_errorf ~loc "Only method support currently"
) in
let pval_type =
Ext_list.fold_right2 labels label_types public_obj_type
(fun label label_type acc ->
Ast_compatible.label_arrow
~loc:label.Asttypes.loc
label.Asttypes.txt
label_type acc
) in
Ast_external_mk.local_extern_cont
loc
~pval_prim:(External_process.pval_prim_of_labels labels)
(fun e ->
Ast_compatible.apply_labels ~loc e
(Ext_list.map2 labels exprs (fun l expr -> l.txt, expr) ) )
~pval_type
let record_as_js_object
loc
(self : Bs_ast_mapper.mapper)
(label_exprs : label_exprs)
: Parsetree.expression_desc =
let labels,args, arity =
Ext_list.fold_right label_exprs ([],[],0) (fun ({txt ; loc}, e) (labels,args,i) ->
match txt with
| Longident.Lident x ->
({Asttypes.loc = loc ; txt = x} :: labels, (x, self.expr self e) :: args, i + 1)
| Ldot _ | Lapply _ ->
Location.raise_errorf ~loc "invalid js label ") in
Ast_external_mk.local_external_obj loc
~pval_prim:(External_process.pval_prim_of_labels labels)
~pval_type:(Ast_core_type.from_labels ~loc arity labels)
args
let isCamlExceptionOrOpenVariant = Longident.parse "Caml_exceptions.isCamlExceptionOrOpenVariant"
let obj_magic = Longident.parse "Obj.magic"
let rec checkCases (cases : Parsetree.case list) =
List.iter check_case cases
and check_case case =
check_pat case.pc_lhs
and check_pat (pat : Parsetree.pattern) =
match pat.ppat_desc with
| Ppat_construct _ -> ()
| Ppat_or (l,r) ->
check_pat l; check_pat r
| _ -> Location.raise_errorf ~loc:pat.ppat_loc "Unsupported pattern in `bs.open`"
let convertBsErrorFunction loc (self : Bs_ast_mapper.mapper) attrs (cases : Parsetree.case list ) =
let txt = "match" in
let txt_expr = Exp.ident ~loc {txt = Lident txt; loc} in
let none = Exp.constraint_ ~loc
(Exp.construct ~loc {txt = Lident "None" ; loc} None)
(Ast_core_type.lift_option_type (Typ.any ~loc ())) in
let () = checkCases cases in
let cases = self.cases self cases in
Ast_compatible.fun_ ~attrs ~loc ( Pat.var ~loc {txt; loc })
(Exp.ifthenelse
~loc
(Ast_compatible.app1 ~loc (Exp.ident ~loc {txt = isCamlExceptionOrOpenVariant ; loc}) txt_expr )
(Exp.match_ ~loc
(Exp.constraint_ ~loc
(Ast_compatible.app1 ~loc (Exp.ident ~loc {txt = obj_magic; loc}) txt_expr)
(Ast_literal.type_exn ~loc ())
)
(Ext_list.map_append cases
[ Exp.case (Pat.any ~loc ()) none]
(fun x ->
let pc_rhs = x.pc_rhs in
let loc = pc_rhs.pexp_loc in
{
x with pc_rhs =
Exp.constraint_ ~loc
(Exp.construct ~loc {txt = Lident "Some";loc} (Some pc_rhs))
(Ast_core_type.lift_option_type (Typ.any ~loc ()) )
}
) )
)
(Some none))