@@ -18,6 +18,30 @@ module Ast = struct
18
18
| Typ of core_type
19
19
end
20
20
21
+ let rec simple_val_to_yojson : Pp_ast.simple_val -> Yojson.Basic.t = function
22
+ | Unit -> `Null
23
+ | Int i -> `Int i
24
+ | String s -> `String s
25
+ | Special s -> `String s
26
+ | Bool b -> `Bool b
27
+ | Char c -> `String (String. make 1 c)
28
+ | Float f -> `Float f
29
+ | Int32 i32 -> `Int (Int32. to_int i32)
30
+ | Int64 i64 -> `Int (Int64. to_int i64)
31
+ | Nativeint ni -> `Int (Nativeint. to_int ni)
32
+ | Array l -> `List (List. map simple_val_to_yojson l)
33
+ | Tuple l -> `List (List. map simple_val_to_yojson l)
34
+ | List l -> `List (List. map simple_val_to_yojson l)
35
+ | Record fields ->
36
+ `Assoc (List. map (fun (k , v ) -> (k, simple_val_to_yojson v)) fields)
37
+ | Constr (cname , [] ) -> `String cname
38
+ | Constr (cname , [ x ]) -> `Assoc [ (cname, simple_val_to_yojson x) ]
39
+ | Constr (cname , l ) ->
40
+ `Assoc [ (cname, `List (List. map simple_val_to_yojson l)) ]
41
+
42
+ let json_printer fmt value =
43
+ Yojson.Basic. pp fmt (simple_val_to_yojson value)
44
+
21
45
module Input = struct
22
46
type t = Stdin | File of string | Source of string
23
47
@@ -66,13 +90,13 @@ let load_input ~kind ~input_name input =
66
90
| (Expression | Pattern | Core_type ), _ | _ , Source _ ->
67
91
parse_node ~kind ~input_name input
68
92
69
- let pp_ast ~config ast =
93
+ let pp_ast ~config ? printer ast =
70
94
match (ast : Ast.t ) with
71
- | Str str -> Pp_ast. structure ~config Format. std_formatter str
72
- | Sig sig_ -> Pp_ast. signature ~config Format. std_formatter sig_
73
- | Exp exp -> Pp_ast. expression ~config Format. std_formatter exp
74
- | Pat pat -> Pp_ast. pattern ~config Format. std_formatter pat
75
- | Typ typ -> Pp_ast. core_type ~config Format. std_formatter typ
95
+ | Str str -> Pp_ast. structure ~config ?printer Format. std_formatter str
96
+ | Sig sig_ -> Pp_ast. signature ~config ?printer Format. std_formatter sig_
97
+ | Exp exp -> Pp_ast. expression ~config ?printer Format. std_formatter exp
98
+ | Pat pat -> Pp_ast. pattern ~config ?printer Format. std_formatter pat
99
+ | Typ typ -> Pp_ast. core_type ~config ?printer Format. std_formatter typ
76
100
77
101
let named f = Cmdliner.Term. (app (const f))
78
102
@@ -97,6 +121,10 @@ let loc_mode =
97
121
in
98
122
named (fun x -> `Loc_mode x) Cmdliner.Arg. (value & vflag `Short [ full_locs ])
99
123
124
+ let json =
125
+ let doc = " Show AST as json" in
126
+ named (fun x -> `Json x) Cmdliner.Arg. (value & flag & info ~doc [ " json" ])
127
+
100
128
let kind =
101
129
let make_vflag (flag , (kind : Kind.t ), doc ) =
102
130
(Some kind, Cmdliner.Arg. info ~doc [ flag ])
@@ -126,7 +154,7 @@ let input =
126
154
let errorf fmt = Printf. ksprintf (fun s -> Error s) fmt
127
155
128
156
let run (`Show_attrs show_attrs ) (`Show_locs show_locs ) (`Loc_mode loc_mode )
129
- (`Kind kind ) (`Input input ) =
157
+ (`Json json ) (` Kind kind ) (`Input input ) =
130
158
let open Stdppx.Result in
131
159
let kind =
132
160
match kind with
@@ -148,12 +176,14 @@ let run (`Show_attrs show_attrs) (`Show_locs show_locs) (`Loc_mode loc_mode)
148
176
in
149
177
let ast = load_input ~kind ~input_name input in
150
178
let config = Pp_ast.Config. make ~show_attrs ~show_locs ~loc_mode () in
151
- pp_ast ~config ast;
179
+ let custom_printer = if json then Some json_printer else None in
180
+ pp_ast ~config ?printer:custom_printer ast;
152
181
Format. printf " %!\n " ;
153
182
Ok ()
154
183
155
184
let term =
156
- Cmdliner.Term. (const run $ show_attrs $ show_locs $ loc_mode $ kind $ input)
185
+ Cmdliner.Term. (
186
+ const run $ show_attrs $ show_locs $ loc_mode $ json $ kind $ input)
157
187
158
188
let tool_name = " ppxlib-pp-ast"
159
189
0 commit comments