diff --git a/eio/angstrom_eio.ml b/eio/angstrom_eio.ml index c001f9a..b2808a1 100644 --- a/eio/angstrom_eio.ml +++ b/eio/angstrom_eio.ml @@ -31,57 +31,47 @@ POSSIBILITY OF SUCH DAMAGE. ----------------------------------------------------------------------------*) -open Eio.Std open Angstrom.Buffered let default_buffer_size = 4096 +let default_pushback () = () + let handle_parse_result state = match state_to_unconsumed state with | None -> assert false | Some us -> us, state_to_result state -let finalize w state = +let finalize r state = (match state with | Done _ - |Fail _ -> + | Fail _ -> state | Partial feed -> feed `Eof) |> handle_parse_result - |> Promise.resolve w - -class ['a] parser_sink (parser : 'a Angstrom.t) pushback = - let p, w = Promise.create () in - object - inherit Eio.Flow.sink - - method copy src = - let buf = Cstruct.create default_buffer_size in - let rec loop = function - | (Done _ as state) - |(Fail _ as state) -> - finalize w state - | Partial feed as state -> ( - match src#read_into buf with - | 0 - |(exception End_of_file) -> - finalize w state - | len -> - let next = feed (`Bigstring (Bigstringaf.sub buf.buffer ~off:0 ~len)) in - pushback (); - loop next ) - in - loop (parse parser) - - method data = Promise.await p - end - -let default_pushback () = () + |> fun x -> r := Some x let parse ?(pushback = default_pushback) parser src = - let sink = new parser_sink parser pushback in - Eio.Flow.copy src sink; - sink#data + let r = ref None in + let buf = Cstruct.create default_buffer_size in + let rec loop = function + | (Done _ as state) + | (Fail _ as state) -> + finalize r state + | Partial feed as state -> ( + match src#read_into buf with + | 0 + | (exception End_of_file) -> + finalize r state + | len -> + let next = feed (`Bigstring (Bigstringaf.sub buf.buffer ~off:0 ~len)) in + pushback (); + loop next ) + in + loop (parse parser); + match !r with + | None -> assert false + | Some x -> x let rec buffered_state_loop pushback state src (buf : Cstruct.t) = match state with @@ -89,7 +79,7 @@ let rec buffered_state_loop pushback state src (buf : Cstruct.t) = let next = match src#read_into buf with | 0 - |(exception End_of_file) -> + | (exception End_of_file) -> k `Eof | len -> k (`Bigstring (Bigstringaf.sub buf.buffer ~off:0 ~len)) in