@@ -2,21 +2,28 @@ open Import
2
2
3
3
module Config = struct
4
4
type loc_mode = [ `Short | `Full ]
5
- type t = { show_attrs : bool ; show_locs : bool ; loc_mode : loc_mode }
5
+
6
+ type t = {
7
+ show_attrs : bool ;
8
+ show_locs : bool ;
9
+ loc_mode : loc_mode ;
10
+ json : bool ;
11
+ }
6
12
7
13
module Default = struct
8
14
let show_attrs = false
9
15
let show_locs = false
10
16
let loc_mode = `Short
17
+ let json = false
11
18
end
12
19
13
20
let default =
14
21
let open Default in
15
- { show_attrs; show_locs; loc_mode }
22
+ { show_attrs; show_locs; loc_mode; json }
16
23
17
24
let make ?(show_attrs = Default. show_attrs) ?(show_locs = Default. show_locs)
18
- ?(loc_mode = Default. loc_mode) () =
19
- { show_attrs; show_locs; loc_mode }
25
+ ?(json = Default. json) ?( loc_mode = Default. loc_mode) () =
26
+ { show_attrs; show_locs; loc_mode; json }
20
27
end
21
28
22
29
let cnum (pos : Lexing.position ) = pos.pos_cnum - pos.pos_bol
@@ -38,6 +45,58 @@ type simple_val =
38
45
| List of simple_val list
39
46
| Special of string
40
47
48
+ let pp_simple_val_to_json fmt simple_val =
49
+ let rec aux indent fmt simple_val =
50
+ match simple_val with
51
+ | Unit -> Format. fprintf fmt {| " null" | }
52
+ | Int i -> Format. fprintf fmt " %d" i
53
+ | String s -> Format. fprintf fmt {| " %s" | } s
54
+ | Special s -> Format. fprintf fmt {| " %s" | } s
55
+ | Bool b -> Format. fprintf fmt " %b" b
56
+ | Char c -> Format. fprintf fmt {| " %c" | } c
57
+ | Float f -> Format. fprintf fmt " %f" f
58
+ | Int32 i32 -> Format. fprintf fmt " %ld" i32
59
+ | Int64 i64 -> Format. fprintf fmt " %Ld" i64
60
+ | Nativeint ni -> Format. fprintf fmt " %nd" ni
61
+ | Array l | Tuple l | List l ->
62
+ Format. fprintf fmt " [\n " ;
63
+ List. iteri
64
+ ~f: (fun i sv ->
65
+ if i > 0 then Format. fprintf fmt " ,\n " ;
66
+ Format. fprintf fmt " %s" (String. make (indent + 2 ) ' ' );
67
+ aux (indent + 2 ) fmt sv)
68
+ l;
69
+ Format. fprintf fmt " \n %s]" (String. make indent ' ' )
70
+ | Record fields ->
71
+ Format. fprintf fmt " {\n " ;
72
+ List. iteri
73
+ ~f: (fun i (k , v ) ->
74
+ if i > 0 then Format. fprintf fmt " ,\n " ;
75
+ Format. fprintf fmt " %s\" %s\" : " (String. make (indent + 2 ) ' ' ) k;
76
+ aux (indent + 2 ) fmt v)
77
+ fields;
78
+ Format. fprintf fmt " \n %s}" (String. make indent ' ' )
79
+ | Constr (cname , [] ) -> Format. fprintf fmt {| " %s" | } cname
80
+ | Constr (cname , [ (Constr (_ , _ :: _ ) as x ) ]) ->
81
+ Format. fprintf fmt " {\n %s\" %s\" : " (String. make (indent + 2 ) ' ' ) cname;
82
+ aux (indent + 2 ) fmt x;
83
+ Format. fprintf fmt " \n %s}" (String. make indent ' ' )
84
+ | Constr (cname , [ x ]) ->
85
+ Format. fprintf fmt " {\n %s\" %s\" : " (String. make (indent + 2 ) ' ' ) cname;
86
+ aux (indent + 2 ) fmt x;
87
+ Format. fprintf fmt " \n %s}" (String. make indent ' ' )
88
+ | Constr (cname , l ) ->
89
+ Format. fprintf fmt " {\n %s\" %s\" : [\n " (String. make (indent + 2 ) ' ' ) cname;
90
+ List. iteri
91
+ ~f: (fun i sv ->
92
+ if i > 0 then Format. fprintf fmt " ,\n " ;
93
+ Format. fprintf fmt " %s" (String. make (indent + 4 ) ' ' );
94
+ aux (indent + 4 ) fmt sv)
95
+ l;
96
+ Format. fprintf fmt " \n %s]\n %s}" (String. make (indent + 2 ) ' ' ) (String. make indent ' ' )
97
+ in
98
+ aux 0 fmt simple_val
99
+
41
100
let pp_collection ~pp_elm ~open_ ~close ~sep fmt l =
42
101
match l with
43
102
| [] -> Format. fprintf fmt " %s%s" open_ close
@@ -271,7 +330,9 @@ let with_config ~config ~f =
271
330
272
331
let pp_with_config (type a ) (lifter : a -> simple_val )
273
332
?(config = Config. default) fmt (x : a ) =
274
- with_config ~config ~f: (fun () -> pp_simple_val fmt (lifter x))
333
+ with_config ~config ~f: (fun () ->
334
+ if config.json then pp_simple_val_to_json fmt (lifter x)
335
+ else pp_simple_val fmt (lifter x))
275
336
276
337
let structure = pp_with_config lift_simple_val#structure
277
338
let structure_item = pp_with_config lift_simple_val#structure_item
0 commit comments