| 1 |
open StdLabels |
| 2 |
open Format |
| 3 |
|
| 4 |
let lsplit2 s ~on = |
| 5 |
match String.index s on with |
| 6 |
| exception Not_found -> None |
| 7 |
| i -> |
| 8 |
Some |
| 9 |
( String.sub s ~pos:0 ~len:i |
| 10 |
, String.sub s ~pos:(i + 1) ~len:(String.length s - i - 1) ) |
| 11 |
;; |
| 12 |
|
| 13 |
let () = |
| 14 |
let oc = |
| 15 |
match Sys.argv with |
| 16 |
| [| _; "-o"; fn |] -> open_out_bin fn |
| 17 |
| _ -> failwith "bad command line arguments" |
| 18 |
in |
| 19 |
try |
| 20 |
let buf = Buffer.create 512 in |
| 21 |
let pp = formatter_of_buffer buf in |
| 22 |
pp_set_margin pp max_int; |
| 23 |
|
| 24 |
Toploop.initialize_toplevel_env (); |
| 25 |
assert ( |
| 26 |
Lexing.from_string "include CamlinternalFormatBasics;;" |
| 27 |
|> !Toploop.parse_toplevel_phrase |
| 28 |
|> Toploop.execute_phrase true pp); |
| 29 |
let types = |
| 30 |
Buffer.contents buf |
| 31 |
|> Str.split (Str.regexp "\n") |
| 32 |
|> List.fold_left ~init:(false, []) ~f:(fun (in_type_group, acc) s -> |
| 33 |
match lsplit2 s ~on:' ' with |
| 34 |
| Some ("type", s) -> true, s :: acc |
| 35 |
| Some ("and", s) when in_type_group -> true, s :: acc |
| 36 |
| _ -> false, acc) |
| 37 |
|> snd |
| 38 |
|> List.rev |
| 39 |
in |
| 40 |
let s = |
| 41 |
String.concat |
| 42 |
~sep:"\n" |
| 43 |
(match types with |
| 44 |
| [] -> [] |
| 45 |
| x :: l -> |
| 46 |
(("type " ^ x) :: List.map l ~f:(( ^ ) "and ")) |
| 47 |
@ [ "[@@deriving traverse_lift]" ]) |
| 48 |
in |
| 49 |
let intf = Parse.interface (Lexing.from_string s) in |
| 50 |
let ppf = formatter_of_out_channel oc in |
| 51 |
fprintf ppf "%a@." Pprintast.signature intf; |
| 52 |
close_out oc |
| 53 |
with |
| 54 |
| exn -> |
| 55 |
Location.report_exception Format.err_formatter exn; |
| 56 |
exit 2 |
| 57 |
;; |