11open Migrate_parsetree ;
22
3- open Ast_mapper ;
4-
5- open Parsetree ;
6-
73open Lib ;
84
95let messages = ref ([] );
106
11- let mapper = ExtractionMapper . getMapper(message => messages := [message, ...messages^ ]);
7+ let iterator = ExtractionIterator . getIterator(message => messages := [message, ...messages^ ]);
8+
9+ let extractMessages = ast => iterator. structure(iterator, Obj . magic(ast));
10+
11+ let readMessagesFromChannel = channel =>
12+ switch (Ast_io . from_channel(channel)) {
13+ | Result . Ok ((_ , Ast_io . Impl ((module Version ), ast))) =>
14+ module Convert = Convert (Version , OCaml_406 );
15+ let converted = Obj . magic(Convert . copy_structure(ast));
16+ extractMessages(converted);
17+ | Result . Ok ((_ , Ast_io . Intf (_ , _ ))) => Printf . eprintf("Interface AST not supported, only implementation AST\n " )
18+ | Result . Error (Not_a_binary_ast (_ )) => Printf . eprintf("Error: input is not a binary AST\n " )
19+ | Result . Error (Unknown_version (v )) => Printf . eprintf("Error: unknown AST version: % s \n " , v)
20+ };
21+
22+ let processFile = filename => {
23+ let channel = Unix . open_process_in("refmt -p binary " ++ filename);
24+ readMessagesFromChannel(channel);
25+ };
26+
27+ let rec processDirectory = dir =>
28+ Sys . readdir(dir)
29+ |> Array . iter(filename => {
30+ let path = Filename . concat(dir, filename);
31+ if (Sys . is_directory(path)) {
32+ processDirectory(path);
33+ } else if (Filename . extension(filename) == ".re" ) {
34+ processFile(path);
35+ };
36+ });
1237
13- let extractMessages = ast => {
14- mapper. structure(mapper, Obj . magic(ast)) |> ignore ;
38+ let outputJson = () => {
1539 let sortedJsonObjects = messages^ |> List . sort(Message . compare) |> List . map(Message . toJson);
1640 Yojson . Basic . pretty_to_channel(stdout, ` List (sortedJsonObjects));
1741 print_newline() ;
1842};
1943
20- switch (Ast_io . from_channel(stdin)) {
21- | Result . Ok ((_ , Ast_io . Impl ((module Version ), ast))) =>
22- module Convert = Convert (Version , OCaml_406 );
23- let converted = Obj . magic(Convert . copy_structure(ast));
24- extractMessages(converted);
25- | Result . Ok ((_ , Ast_io . Intf (_ , _ ))) => print_endline("Intf AST!" )
26- | Result . Error (Not_a_binary_ast (_ )) => print_endline("Error: not a binary AST: " )
27- | Result . Error (Unknown_version (v )) => print_endline("Error: unknown version: " ++ v)
44+ switch (Array . to_list(Sys . argv)) {
45+ | [ _exe , dir ] =>
46+ if (! Sys . is_directory(dir)) {
47+ Printf . eprintf("Not a directory: % s \n " , dir);
48+ exit(2 );
49+ };
50+ processDirectory(dir);
51+ outputJson() ;
52+ | [ exe , ... _params ] =>
53+ Printf . eprintf("Usage: % s <directory>\n " , exe);
54+ exit(1 );
55+ | _ => () /* cannot happen */
2856};
0 commit comments