let grep rex ml_name = let lb = Linebuffer.create () in let annot_name = Filename.chop_extension ml_name ^ ".annot" in let line_num = ref 0 in let rec process () = Linebuffer.get_line lb (fun line -> incr line_num; if try ignore (Pcre.pcre_exec ~rex line); true with Not_found -> false then Printf.printf "%s:%d:%s\n" ml_name !line_num line; process (); ) in process (); try Annotator.merge ml_name annot_name (Linebuffer.feed lb) with Sys_error message -> Printf.eprintf "%s:%s\n" ml_name message let main () = let sources = ref [] in let re = ref None in Arg.parse [ ] (fun s -> match !re with | None -> re := Some (Pcre.regexp ~study:true s) | Some _ -> sources := s::!sources ) "Grep Objective Caml source files compiled with annotations (-dannot or -dtypes) from their annotated forms"; sources := List.rev !sources; match !re with | None -> failwith "no regular expression" | Some re -> match !sources with | [] -> failwith "no source files" | sources -> List.iter (grep re) sources let () = main ()