Skip to content

Commit 03a46a1

Browse files
committed
Add json mapper for pp_ast
Signed-off-by: pedrobslisboa <[email protected]>
1 parent ac7fcfc commit 03a46a1

File tree

5 files changed

+572
-8
lines changed

5 files changed

+572
-8
lines changed

Diff for: bin/pp_ast.ml

+8-3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ let loc_mode =
9797
in
9898
named (fun x -> `Loc_mode x) Cmdliner.Arg.(value & vflag `Short [ full_locs ])
9999

100+
let json =
101+
let doc = "Show AST as json" in
102+
named (fun x -> `Json x) Cmdliner.Arg.(value & flag & info ~doc [ "json" ])
103+
100104
let kind =
101105
let make_vflag (flag, (kind : Kind.t), doc) =
102106
(Some kind, Cmdliner.Arg.info ~doc [ flag ])
@@ -126,7 +130,7 @@ let input =
126130
let errorf fmt = Printf.ksprintf (fun s -> Error s) fmt
127131

128132
let run (`Show_attrs show_attrs) (`Show_locs show_locs) (`Loc_mode loc_mode)
129-
(`Kind kind) (`Input input) =
133+
(`Json json) (`Kind kind) (`Input input) =
130134
let open Stdppx.Result in
131135
let kind =
132136
match kind with
@@ -147,13 +151,14 @@ let run (`Show_attrs show_attrs) (`Show_locs show_locs) (`Loc_mode loc_mode)
147151
match input with Stdin -> "<stdin>" | File fn -> fn | Source _ -> "<cli>"
148152
in
149153
let ast = load_input ~kind ~input_name input in
150-
let config = Pp_ast.Config.make ~show_attrs ~show_locs ~loc_mode () in
154+
let config = Pp_ast.Config.make ~show_attrs ~show_locs ~loc_mode ~json () in
151155
pp_ast ~config ast;
152156
Format.printf "%!\n";
153157
Ok ()
154158

155159
let term =
156-
Cmdliner.Term.(const run $ show_attrs $ show_locs $ loc_mode $ kind $ input)
160+
Cmdliner.Term.(
161+
const run $ show_attrs $ show_locs $ loc_mode $ json $ kind $ input)
157162

158163
let tool_name = "ppxlib-pp-ast"
159164

Diff for: ppxlib.opam

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ depends: [
2727
"sexplib0" {>= "v0.12"}
2828
"sexplib0" {with-test & >= "v0.15"}
2929
"stdlib-shims"
30+
"yojson"
3031
"ocamlfind" {with-test}
3132
"re" {with-test & >= "1.9.0"}
3233
"cinaps" {with-test & >= "v0.12.1"}

Diff for: src/pp_ast.ml

+66-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@ open Import
22

33
module Config = struct
44
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+
}
612

713
module Default = struct
814
let show_attrs = false
915
let show_locs = false
1016
let loc_mode = `Short
17+
let json = false
1118
end
1219

1320
let default =
1421
let open Default in
15-
{ show_attrs; show_locs; loc_mode }
22+
{ show_attrs; show_locs; loc_mode; json }
1623

1724
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 }
2027
end
2128

2229
let cnum (pos : Lexing.position) = pos.pos_cnum - pos.pos_bol
@@ -38,6 +45,58 @@ type simple_val =
3845
| List of simple_val list
3946
| Special of string
4047

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+
41100
let pp_collection ~pp_elm ~open_ ~close ~sep fmt l =
42101
match l with
43102
| [] -> Format.fprintf fmt "%s%s" open_ close
@@ -271,7 +330,9 @@ let with_config ~config ~f =
271330

272331
let pp_with_config (type a) (lifter : a -> simple_val)
273332
?(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))
275336

276337
let structure = pp_with_config lift_simple_val#structure
277338
let structure_item = pp_with_config lift_simple_val#structure_item

Diff for: src/pp_ast.mli

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Config : sig
4040
val make :
4141
?show_attrs:bool ->
4242
?show_locs:bool ->
43+
?json:bool ->
4344
?loc_mode:[ `Short | `Full ] ->
4445
unit ->
4546
t

0 commit comments

Comments
 (0)