Skip to content

Commit 14ccb1f

Browse files
amiraliescristianoc
authored andcommitted
POC: print patterns using rescript printer
1 parent d800a42 commit 14ccb1f

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

jscomp/common/pattern_printer.ml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
open Types
2+
open Typedtree
3+
4+
let mkpat desc = Ast_helper.Pat.mk desc
5+
6+
let untype typed =
7+
let rec loop pat =
8+
match pat.pat_desc with
9+
| Tpat_or (pa, pb, _) -> mkpat (Ppat_or (loop pa, loop pb))
10+
| Tpat_any | Tpat_var _ -> mkpat Ppat_any
11+
| Tpat_constant c -> mkpat (Ppat_constant (Untypeast.constant c))
12+
| Tpat_alias (p, _, _) -> loop p
13+
| Tpat_tuple lst -> mkpat (Ppat_tuple (List.map loop lst))
14+
| Tpat_construct (cstr_lid, cstr, lst) ->
15+
let lid = { cstr_lid with txt = Longident.Lident cstr.cstr_name } in
16+
let arg =
17+
match List.map loop lst with
18+
| [] -> None
19+
| [ p ] -> Some p
20+
| lst -> Some (mkpat (Ppat_tuple lst))
21+
in
22+
mkpat (Ppat_construct (lid, arg))
23+
| Tpat_variant (label, p_opt, _row_desc) ->
24+
let arg = Option.map loop p_opt in
25+
mkpat (Ppat_variant (label, arg))
26+
| Tpat_record (subpatterns, closed_flag) ->
27+
let fields =
28+
List.map
29+
(fun (_, lbl, p) ->
30+
(mknoloc (Longident.Lident lbl.lbl_name), loop p))
31+
subpatterns
32+
in
33+
mkpat (Ppat_record (fields, closed_flag))
34+
| Tpat_array lst -> mkpat (Ppat_array (List.map loop lst))
35+
| Tpat_lazy p -> mkpat (Ppat_lazy (loop p))
36+
in
37+
let ps = loop typed in
38+
ps
39+
40+
let print_pattern typed =
41+
let pat = untype typed in
42+
let doc = Res_printer.printPattern pat Res_comments_table.empty in
43+
Res_doc.toString ~width:80 doc

jscomp/common/pattern_printer.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val print_pattern : Typedtree.pattern -> string

jscomp/core/bs_conditional_initial.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ let setup_env () =
3434
Ctype.variant_is_subtype := Matching_polyfill.variant_is_subtype;
3535
Clflags.dump_location := false;
3636
Config.syntax_kind := `rescript;
37+
Parmatch.print_res_pat := Pattern_printer.print_pattern;
3738

3839
# 38 "core/bs_conditional_initial.pp.ml"
3940
Clflags.color := Some Always;
@@ -73,4 +74,4 @@ let setup_env () =
7374

7475

7576
let () =
76-
at_exit (fun _ -> Format.pp_print_flush Format.err_formatter ())
77+
at_exit (fun _ -> Format.pp_print_flush Format.err_formatter ())

jscomp/ml/parmatch.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ let get_type_path ty tenv =
367367
(* Values as patterns pretty printer *)
368368
(*************************************)
369369

370+
let print_res_pat: (Typedtree.pattern -> string) ref =
371+
ref (fun _ -> assert false)
372+
370373
open Format
371374
;;
372375

@@ -2090,8 +2093,7 @@ let do_check_partial ?pred exhaust loc casel pss = match pss with
20902093
let errmsg =
20912094
try
20922095
let buf = Buffer.create 16 in
2093-
let fmt = formatter_of_buffer buf in
2094-
top_pretty fmt v;
2096+
Buffer.add_string buf (!print_res_pat v);
20952097
begin match check_partial_all v casel with
20962098
| None -> ()
20972099
| Some _ ->

jscomp/ml/parmatch.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ val pretty_pat : pattern -> unit
2424
val pretty_line : pattern list -> unit
2525
val pretty_matrix : pattern list list -> unit
2626

27+
val print_res_pat: (Typedtree.pattern -> string) ref
28+
2729
val omega : pattern
2830
val omegas : int -> pattern list
2931
val omega_list : 'a list -> pattern list

0 commit comments

Comments
 (0)