let gen_load_sexp my_parse ?(strict = true) ?(buf = String.create 8192) file =
let buf_len = String.length buf in
let ic = open_in file in
let rec loop this_parse =
let len = input ic buf 0 buf_len in
if len = 0 then
failwith (sprintf "Sexplib.Sexp.gen_load_sexp: end of file: %s" file)
else
match this_parse ~pos:0 ~len buf with
| Done (sexp, ({ Parse_pos.buf_pos } as parse_pos))
when strict ->
let rec strict_loop this_parse ~pos ~len =
match this_parse ~pos ~len buf with
| Done _ | Cont (false, _) ->
failwith (
sprintf
"Sexplib.Sexp.gen_load_sexp: more than one S-expression: %s"
file)
| Cont (true, this_parse) ->
let len = input ic buf 0 buf_len in
if len = 0 then sexp
else strict_loop this_parse ~pos:0 ~len
in
let this_parse = mk_this_parse ~parse_pos my_parse in
strict_loop this_parse ~pos:buf_pos ~len:(len - buf_pos)
| Done (sexp, _) -> sexp
| Cont (_, this_parse) -> loop this_parse
in
try
let sexp = loop (mk_this_parse my_parse) in
close_in ic;
sexp
with exc -> close_in_noerr ic; raise exc