extract_ocaml_code.ml 1.09 KB
Newer Older
1 2
(*

3
Simple utility to extract a portion of the standard input
4

5
  extract_ocaml_code <id>
6

7
outputs the part of the standard input between anchors BEGIN{id} and END{id}
8 9 10

*)

11 12 13 14 15 16 17 18
let section =
  match Sys.argv with
  | [| _; section |] -> section
  | _ ->
    Format.eprintf "Usage: %s <id>@\n\
Output the part of the standard input between anchors BEGIN{id} and END{id}@."
      Sys.argv.(0);
    exit 2
19 20 21 22 23 24

let begin_re = Str.regexp_string ("BEGIN{" ^ section ^ "}")

let search_begin () =
  try
    while true do
25
      let l = read_line () in
26 27 28 29 30
      try
        let _ = Str.search_forward begin_re l 0 in raise Exit
      with Not_found -> ()
    done
  with
31 32
  | End_of_file ->
    Format.eprintf "Error: opening anchor BEGIN{%s} not found@." section;
33 34 35 36 37 38 39 40 41
    exit 1
  | Exit -> ()


let end_re = Str.regexp_string ("END{" ^ section ^ "}")

let search_end () =
  try
    while true do
42
      let l = read_line () in
43 44 45
      try
        let _ = Str.search_forward end_re l 0 in raise Exit
      with Not_found ->
46 47
        print_string l;
        print_char '\n'
48
    done
49
  with Exit -> ()
50 51 52

let () =
  search_begin (); search_end ()