@@ -18,6 +18,29 @@ 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 = Yojson.Basic. pp fmt (simple_val_to_yojson value)
43
+
21
44
module Input = struct
22
45
type t = Stdin | File of string | Source of string
23
46
@@ -66,13 +89,13 @@ let load_input ~kind ~input_name input =
66
89
| (Expression | Pattern | Core_type ), _ | _ , Source _ ->
67
90
parse_node ~kind ~input_name input
68
91
69
- let pp_ast ~config ast =
92
+ let pp_ast ~config ? printer ast =
70
93
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
94
+ | Str str -> Pp_ast. structure ~config ?printer Format. std_formatter str
95
+ | Sig sig_ -> Pp_ast. signature ~config ?printer Format. std_formatter sig_
96
+ | Exp exp -> Pp_ast. expression ~config ?printer Format. std_formatter exp
97
+ | Pat pat -> Pp_ast. pattern ~config ?printer Format. std_formatter pat
98
+ | Typ typ -> Pp_ast. core_type ~config ?printer Format. std_formatter typ
76
99
77
100
let named f = Cmdliner.Term. (app (const f))
78
101
@@ -97,6 +120,10 @@ let loc_mode =
97
120
in
98
121
named (fun x -> `Loc_mode x) Cmdliner.Arg. (value & vflag `Short [ full_locs ])
99
122
123
+ let json =
124
+ let doc = " Show AST as json" in
125
+ named (fun x -> `Json x) Cmdliner.Arg. (value & flag & info ~doc [ " json" ])
126
+
100
127
let kind =
101
128
let make_vflag (flag , (kind : Kind.t ), doc ) =
102
129
(Some kind, Cmdliner.Arg. info ~doc [ flag ])
@@ -126,7 +153,7 @@ let input =
126
153
let errorf fmt = Printf. ksprintf (fun s -> Error s) fmt
127
154
128
155
let run (`Show_attrs show_attrs ) (`Show_locs show_locs ) (`Loc_mode loc_mode )
129
- (`Kind kind ) (`Input input ) =
156
+ (`Json json ) (` Kind kind ) (`Input input ) =
130
157
let open Stdppx.Result in
131
158
let kind =
132
159
match kind with
@@ -148,12 +175,14 @@ let run (`Show_attrs show_attrs) (`Show_locs show_locs) (`Loc_mode loc_mode)
148
175
in
149
176
let ast = load_input ~kind ~input_name input in
150
177
let config = Pp_ast.Config. make ~show_attrs ~show_locs ~loc_mode () in
151
- pp_ast ~config ast;
178
+ let custom_printer = if json then Some json_printer else None in
179
+ pp_ast ~config ?printer:custom_printer ast;
152
180
Format. printf " %!\n " ;
153
181
Ok ()
154
182
155
183
let term =
156
- Cmdliner.Term. (const run $ show_attrs $ show_locs $ loc_mode $ kind $ input)
184
+ Cmdliner.Term. (
185
+ const run $ show_attrs $ show_locs $ loc_mode $ json $ kind $ input)
157
186
158
187
let tool_name = " ppxlib-pp-ast"
159
188
0 commit comments