Debian Codegraph @

plugins/extraction/json.ml

raw symbols SCIP
1 open Pp
2 open Util
3 open Names
4 open Table
5 open Miniml
6 open Mlutil
7 open Common
8
9 let json_str s =
10 qs s
11
12 let json_int i =
13 int i
14
15 let json_bool b =
16 if b then str "true" else str "false"
17
18 let json_global table typ ref =
19 if is_custom ref then
20 json_str (find_custom ref)
21 else
22 json_str (Common.pp_global table typ ref)
23
24 let json_id id =
25 json_str (Id.to_string id)
26
27 let json_dict_one (k, v) =
28 json_str k ++ str (": ") ++ v
29
30 let json_dict_open l =
31 str ("{") ++ fnl () ++
32 str (" ") ++ hov 0 (prlist_with_sep pr_comma json_dict_one l)
33
34 let json_dict l =
35 json_dict_open l ++ fnl () ++
36 str ("}")
37
38 let json_list l =
39 str ("[") ++ fnl () ++
40 str (" ") ++ hov 0 (prlist_with_sep pr_comma (fun x -> x) l) ++ fnl () ++
41 str ("]")
42
43 let json_listarr a =
44 str ("[") ++ fnl () ++
45 str (" ") ++ hov 0 (prvect_with_sep pr_comma (fun x -> x) a) ++ fnl () ++
46 str ("]")
47
48
49 let preamble table mod_name comment used_modules usf =
50 (match comment with
51 | None -> mt ()
52 | Some s -> str "/* " ++ hov 0 s ++ str " */" ++ fnl ()) ++
53 json_dict_open [
54 ("what", json_str "module");
55 ("name", json_id mod_name);
56 ("need_magic", json_bool (usf.magic));
57 ("need_dummy", json_bool (usf.mldummy));
58 ("used_modules", json_list
59 (List.map (fun mf -> json_str (file_of_modfile (State.get_table table) mf)) (DirPath.Set.elements used_modules)))
60 ]
61
62
63 (*s Pretty-printing of types. *)
64
65 let rec json_type table vl = function
66 | Tmeta _ | Tvar' _ -> assert false
67 | Tvar i -> (try
68 let varid = List.nth vl (pred i) in json_dict [
69 ("what", json_str "type:var");
70 ("name", json_id varid)
71 ]
72 with Failure _ -> json_dict [
73 ("what", json_str "type:varidx");
74 ("name", json_int i)
75 ])
76 | Tglob (r, l) -> json_dict [
77 ("what", json_str "type:glob");
78 ("name", json_global table Type r);
79 ("args", json_list (List.map (json_type table vl) l))
80 ]
81 | Tarr (t1,t2) -> json_dict [
82 ("what", json_str "type:arrow");
83 ("left", json_type table vl t1);
84 ("right", json_type table vl t2)
85 ]
86 | Tdummy _ -> json_dict [("what", json_str "type:dummy")]
87 | Tunknown -> json_dict [("what", json_str "type:unknown")]
88 | Taxiom -> json_dict [("what", json_str "type:axiom")]
89
90
91 (*s Pretty-printing of expressions. *)
92
93 let rec json_expr table env = function
94 | MLrel n -> json_dict [
95 ("what", json_str "expr:rel");
96 ("name", json_id (get_db_name n env))
97 ]
98 | MLapp (f, args) -> json_dict [
99 ("what", json_str "expr:apply");
100 ("func", json_expr table env f);
101 ("args", json_list (List.map (json_expr table env) args))
102 ]
103 | MLlam _ as a ->
104 let fl, a' = collect_lams a in
105 let fl, env' = push_vars (List.map id_of_mlid fl) env in
106 json_dict [
107 ("what", json_str "expr:lambda");
108 ("argnames", json_list (List.map json_id (List.rev fl)));
109 ("body", json_expr table env' a')
110 ]
111 | MLletin (id, a1, a2) ->
112 let i, env' = push_vars [id_of_mlid id] env in
113 json_dict [
114 ("what", json_str "expr:let");
115 ("name", json_id (List.hd i));
116 ("nameval", json_expr table env a1);
117 ("body", json_expr table env' a2)
118 ]
119 | MLglob r -> json_dict [
120 ("what", json_str "expr:global");
121 ("name", json_global table Term r)
122 ]
123 | MLcons (_, r, a) -> json_dict [
124 ("what", json_str "expr:constructor");
125 ("name", json_global table Cons r);
126 ("args", json_list (List.map (json_expr table env) a))
127 ]
128 | MLtuple l -> json_dict [
129 ("what", json_str "expr:tuple");
130 ("items", json_list (List.map (json_expr table env) l))
131 ]
132 | MLcase (typ, t, pv) -> json_dict [
133 ("what", json_str "expr:case");
134 ("expr", json_expr table env t);
135 ("cases", json_listarr (Array.map (fun x -> json_one_pat table env x) pv))
136 ]
137 | MLfix (i, ids, defs) ->
138 let ids', env' = push_vars (List.rev (Array.to_list ids)) env in
139 let ids' = Array.of_list (List.rev ids') in
140 json_dict [
141 ("what", json_str "expr:fix");
142 ("funcs", json_listarr (Array.map (fun (fi, ti) ->
143 json_dict [
144 ("what", json_str "fix:item");
145 ("name", json_id fi);
146 ("body", json_function table env' ti)
147 ]) (Array.map2 (fun a b -> a,b) ids' defs)));
148 ("for", json_int i);
149 ]
150 | MLexn s -> json_dict [
151 ("what", json_str "expr:exception");
152 ("msg", json_str s)
153 ]
154 | MLdummy _ -> json_dict [("what", json_str "expr:dummy")]
155 | MLmagic a -> json_dict [
156 ("what", json_str "expr:coerce");
157 ("value", json_expr table env a)
158 ]
159 | MLaxiom _ -> json_dict [("what", json_str "expr:axiom")]
160 | MLuint i -> json_dict [
161 ("what", json_str "expr:int");
162 ("int", json_str (Uint63.to_string i))
163 ]
164 | MLfloat f -> json_dict [
165 ("what", json_str "expr:float");
166 ("float", json_str (Float64.to_string f))
167 ]
168 | MLstring s -> json_dict [
169 ("what", json_str "expr:string");
170 ("string", json_str (Pstring.to_string s))
171 ]
172 | MLparray(t,def) -> json_dict [
173 ("what", json_str "expr:array");
174 ("elems", json_listarr (Array.map (json_expr table env) t));
175 ("default", json_expr table env def)
176 ]
177
178 and json_one_pat table env (ids,p,t) =
179 let ids', env' = push_vars (List.rev_map id_of_mlid ids) env in json_dict [
180 ("what", json_str "case");
181 ("pat", json_gen_pat table (List.rev ids') env' p);
182 ("body", json_expr table env' t)
183 ]
184
185 and json_gen_pat table ids env = function
186 | Pcons (r, l) -> json_cons_pat table r (List.map (json_gen_pat table ids env) l)
187 | Pusual r -> json_cons_pat table r (List.map json_id ids)
188 | Ptuple l -> json_dict [
189 ("what", json_str "pat:tuple");
190 ("items", json_list (List.map (json_gen_pat table ids env) l))
191 ]
192 | Pwild -> json_dict [("what", json_str "pat:wild")]
193 | Prel n -> json_dict [
194 ("what", json_str "pat:rel");
195 ("name", json_id (get_db_name n env))
196 ]
197
198 and json_cons_pat table r ppl = json_dict [
199 ("what", json_str "pat:constructor");
200 ("name", json_global table Cons r);
201 ("argnames", json_list ppl)
202 ]
203
204 and json_function table env t =
205 let bl, t' = collect_lams t in
206 let bl, env' = push_vars (List.map id_of_mlid bl) env in
207 json_dict [
208 ("what", json_str "expr:lambda");
209 ("argnames", json_list (List.map json_id (List.rev bl)));
210 ("body", json_expr table env' t')
211 ]
212
213
214 (*s Pretty-printing of inductive types declaration. *)
215
216 let json_ind table ip pl cv = json_dict [
217 ("what", json_str "decl:ind");
218 ("name", json_global table Type ip.ip_typename_ref);
219 ("argnames", json_list (List.map json_id pl));
220 ("constructors", json_listarr (Array.mapi (fun idx c -> json_dict [
221 ("name", json_global table Cons ip.ip_consnames_ref.(idx));
222 ("argtypes", json_list (List.map (json_type table pl) c))
223 ]) cv))
224 ]
225
226
227 (*s Pretty-printing of a declaration. *)
228
229 let pp_decl table = function
230 | Dind defs -> prvecti_with_sep pr_comma
231 (fun i p -> if p.ip_logical then str ""
232 else json_ind table p p.ip_vars p.ip_types) defs.ind_packets
233 | Dtype (r, l, t) -> json_dict [
234 ("what", json_str "decl:type");
235 ("name", json_global table Type r);
236 ("argnames", json_list (List.map json_id l));
237 ("value", json_type table l t)
238 ]
239 | Dfix (rv, defs, typs) -> json_dict [
240 ("what", json_str "decl:fixgroup");
241 ("fixlist", json_listarr (Array.mapi (fun i r ->
242 json_dict [
243 ("what", json_str "fixgroup:item");
244 ("name", json_global table Term rv.(i));
245 ("type", json_type table [] typs.(i));
246 ("value", json_function table (empty_env table ()) defs.(i))
247 ]) rv))
248 ]
249 | Dterm (r, a, t) -> json_dict [
250 ("what", json_str "decl:term");
251 ("name", json_global table Term r);
252 ("type", json_type table [] t);
253 ("value", json_function table (empty_env table ()) a)
254 ]
255
256 let rec pp_structure_elem table = function
257 | (l,SEdecl d) -> [ pp_decl table d ]
258 | (l,SEmodule m) -> pp_module_expr table m.ml_mod_expr
259 | (l,SEmodtype m) -> []
260 (* for the moment we simply discard module type *)
261
262 and pp_module_expr table = function
263 | MEstruct (mp,sel) -> List.concat (List.map (pp_structure_elem table) sel)
264 | MEfunctor _ -> []
265 (* for the moment we simply discard unapplied functors *)
266 | MEident _ | MEapply _ -> assert false
267 (* should be expansed in extract_env *)
268
269 let pp_struct table mls =
270 let pp_sel (mp,sel) = State.with_visibility table mp [] begin fun table ->
271 let p = prlist_with_sep pr_comma identity
272 (List.concat (List.map (pp_structure_elem table) sel)) in
273 p
274 end in
275 str "," ++ fnl () ++
276 str " " ++ qs "declarations" ++ str ": [" ++ fnl () ++
277 str " " ++ hov 0 (prlist_with_sep pr_comma pp_sel mls) ++ fnl () ++
278 str " ]" ++ fnl () ++
279 str "}" ++ fnl ()
280
281 let file_naming state mp = file_of_modfile (State.get_table state) mp
282
283 let json_descr = {
284 keywords = Id.Set.empty;
285 file_suffix = ".json";
286 file_naming = file_naming;
287 preamble = preamble;
288 pp_struct = pp_struct;
289 sig_suffix = None;
290 sig_preamble = (fun _ _ _ _ _ -> mt ());
291 pp_sig = (fun _ _ -> mt ());
292 pp_decl = pp_decl;
293 }