Commit 54d6b937 authored by Guillaume Melquiond's avatar Guillaume Melquiond

Simplify extract_ocaml_code.

parent f091aad9
...@@ -1873,19 +1873,19 @@ doc/extract_ocaml_code: doc/extract_ocaml_code.ml ...@@ -1873,19 +1873,19 @@ doc/extract_ocaml_code: doc/extract_ocaml_code.ml
$(OCAMLC) str.cma -o $@ $< $(OCAMLC) str.cma -o $@ $<
doc/logic__%.ml: examples/use_api/logic.ml doc/extract_ocaml_code doc/logic__%.ml: examples/use_api/logic.ml doc/extract_ocaml_code
doc/extract_ocaml_code examples/use_api/logic.ml $* doc doc/extract_ocaml_code $* < $< > $@
doc/whyconf__%.ml: src/driver/whyconf.ml doc/extract_ocaml_code doc/whyconf__%.ml: src/driver/whyconf.ml doc/extract_ocaml_code
doc/extract_ocaml_code src/driver/whyconf.ml $* doc doc/extract_ocaml_code $* < $< > $@
doc/call_provers__%.ml: src/driver/call_provers.ml doc/extract_ocaml_code doc/call_provers__%.ml: src/driver/call_provers.ml doc/extract_ocaml_code
doc/extract_ocaml_code src/driver/call_provers.ml $* doc doc/extract_ocaml_code $* < $< > $@
doc/mlw_tree__%.ml: examples/use_api/mlw_tree.ml doc/extract_ocaml_code doc/mlw_tree__%.ml: examples/use_api/mlw_tree.ml doc/extract_ocaml_code
doc/extract_ocaml_code examples/use_api/mlw_tree.ml $* doc doc/extract_ocaml_code $* < $< > $@
doc/transform__%.ml: examples/use_api/transform.ml doc/extract_ocaml_code doc/transform__%.ml: examples/use_api/transform.ml doc/extract_ocaml_code
doc/extract_ocaml_code examples/use_api/transform.ml $* doc doc/extract_ocaml_code $* < $< > $@
OCAMLCODE_LOGIC = opening printformula declarepropvars declarepropatoms \ OCAMLCODE_LOGIC = opening printformula declarepropvars declarepropatoms \
buildtask printtask buildtask2 \ buildtask printtask buildtask2 \
......
(* (*
Simple utility to extract a portion of a file Simple utility to extract a portion of the standard input
extract_ocaml_code <path/file.ext> <id> <dir> extract_ocaml_code <id>
extracts the part of path/file.ext between lines containing anchors BEGIN{id} and END{id} outputs the part of the standard input between anchors BEGIN{id} and END{id}
and puts the result in the file dir/file_id.ext
*) *)
open Format let section =
match Sys.argv with
let filename,section,output_dir = | [| _; section |] -> section
if Array.length Sys.argv = 4 then | _ ->
Sys.argv.(1), Sys.argv.(2), Sys.argv.(3) Format.eprintf "Usage: %s <id>@\n\
else Output the part of the standard input between anchors BEGIN{id} and END{id}@."
begin Sys.argv.(0);
eprintf "Usage: %s <path/file.ext> <id> <dir>@\n\ exit 2
Extract the part of path/file.ext between lines containing anchors BEGIN{id} and END{id}@\n\
and puts the result in the file dir/file_id.ext@."
Sys.argv.(0);
exit 2
end
let ch_in =
try
open_in filename
with
exn ->
eprintf "Cannot open %s for reading: %s@." filename (Printexc.to_string exn);
exit 1
let basename = Filename.basename filename
let ext = Filename.extension basename
let basename = Filename.remove_extension basename
let begin_re = Str.regexp_string ("BEGIN{" ^ section ^ "}") let begin_re = Str.regexp_string ("BEGIN{" ^ section ^ "}")
let search_begin () = let search_begin () =
try try
while true do while true do
let l = input_line ch_in in let l = read_line () in
try try
let _ = Str.search_forward begin_re l 0 in raise Exit let _ = Str.search_forward begin_re l 0 in raise Exit
with Not_found -> () with Not_found -> ()
done done
with with
End_of_file -> | End_of_file ->
eprintf "Error: opening anchor BEGIN{%s} not found in file %s@." section filename; Format.eprintf "Error: opening anchor BEGIN{%s} not found@." section;
close_in ch_in;
exit 1 exit 1
| Exit -> () | Exit -> ()
let end_re = Str.regexp_string ("END{" ^ section ^ "}") let end_re = Str.regexp_string ("END{" ^ section ^ "}")
let file_out = Filename.concat output_dir (basename ^ "__" ^ section ^ ext)
let ch_out =
try
open_out file_out
with
exn ->
eprintf "Cannot open %s for writing: %s@." file_out (Printexc.to_string exn);
close_in ch_in;
exit 1
let search_end () = let search_end () =
try try
while true do while true do
let l = input_line ch_in in let l = read_line () in
try try
let _ = Str.search_forward end_re l 0 in raise Exit let _ = Str.search_forward end_re l 0 in raise Exit
with Not_found -> with Not_found ->
output_string ch_out l; print_string l;
output_char ch_out '\n' print_char '\n'
done done
with with Exit -> ()
End_of_file ->
eprintf "Error: ending anchor END{%s} not found in file %s@." section filename;
close_in ch_in;
close_out ch_out;
exit 1
| Exit ->
close_in ch_in;
close_out ch_out
let () = let () =
search_begin (); search_end () search_begin (); search_end ()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment