Streaming
Parseff provides two streaming interfaces for input that arrives incrementally:
Parseff.Source: pull-based streaming (files, channels, sockets, sequences, Eio flows).Parseff.Push: push-based streaming where your caller owns the read loop (Lwt, Async, callback APIs).
Ordinary unit -> 'a parsers work unchanged with Parseff.parse, Parseff.Source.parse, and Parseff.Push.parse.
Pull streaming: Source.parse
Parseff.Source.parse runs a parser pulling bytes from a Parseff.Source.t on demand.
val Source.parse : ?max_depth:int -> ?backtrack_window:int -> ?diagnostics:Diagnostics.t -> ?trace:(trace_event -> unit) -> ?consume:[ `All | `Partial ] -> Source.t -> (unit -> 'a) -> ('a, error) Stdlib.resultlet ic = open_in "data.json" inlet source = Parseff.Source.of_channel ic inlet result = Parseff.Source.parse source json inclose_in ic;result~consumedefaults to`All, pulling one extra byte after the parser returns to check EOF without reading the rest of the source. Pass~consume:`Partialto leave remaining bytes unread.~backtrack_windowbounds how much buffered history Parseff retains for backtracking (default:65536bytes; minimum2). Exceeding it fails withBacktrack_window_exceeded.- Error positions are byte offsets from where this parse starts in the stream.
foundtext covers only whole characters already buffered (Parseff does not pull more bytes just to lengthen a snippet), so it can be a prefix of the textParseff.parsereports andrange.stopcan be smaller.Text ""means no whole character is buffered at the failure position (it is at the end of the input read so far, or the character there is incomplete): the range is empty and the message has nofoundpart. ~diagnosticscollects diagnostics as inParseff.parse. Seediagnostics.
Streaming multiple values: Source.parse_each
Parseff.Source.parse_each parses a lazy sequence of values (such as NDJSON or log records) from one source, bounding memory by ~backtrack_window.
val Source.parse_each : ?max_depth:int -> ?backtrack_window:int -> ?diagnostics:Diagnostics.t -> ?trace:(trace_event -> unit) -> ?pending:(unit -> bool) -> Source.t -> (unit -> 'a) -> ('a, error) Stdlib.result Seq.tEach element runs the parser like Source.parse ~consume:`Partial. The parser must consume its trailing separator (a zero-width run yields an Error). The sequence stops at EOF or after the first Error, and is ephemeral (forcing an element twice raises Invalid_argument). Pass ~pending if your parser buffers lookahead tokens past its value: the run after pending returns true may consume no input, but a second zero-width run in a row at the same position yields an Error. Parseff.Push.parse_each follows the same rule.
let record () = let id = Parseff.take_while ~at_least:1 (fun c -> c >= '0' && c <= '9') in let _ = Parseff.char '\n' in int_of_string id
let () = Parseff.Source.parse_each (Parseff.Source.of_string "1\n22\n333\n") record |> Seq.iter (function | Ok id -> Printf.printf "%d\n" id | Error e -> prerr_endline (Parseff.render_error e))Source constructors
The next Source.parse or Source.parse_each on the same source starts where the last one stopped (see Parseff.Source.parse), so a message head and its body can be parsed in turn, and a bad record can be skipped. Positions restart at each parse; Parseff.Source.offset gives the absolute byte offset where the next parse starts.
Source.of_string
Wraps a string as a source (useful for testing streaming code paths).
val of_string : string -> Source.tSource.of_channel
Reads from an in_channel with buffer size ~buf_size (default 4096; raises Invalid_argument if < 1).
val of_channel : ?buf_size:int -> in_channel -> Source.tSource.of_chunks
Pulls chunks from a callback returning Some s for data (empty strings are skipped) or None for EOF. Chunks may be used without copying and must not be mutated after the callback returns (relevant only with Bytes.unsafe_to_string).
val of_chunks : (unit -> string option) -> Source.tlet source = Parseff.Source.of_chunks (fun () -> match Http.Body.read body with | `Data chunk -> Some chunk | `Eof -> None)inParseff.Source.parse source my_parserSource.of_seq
Reads chunks from a string Seq.t (same chunk-immutability rule as of_chunks).
val of_seq : string Seq.t -> Source.tlet source = Parseff.Source.of_seq (List.to_seq [ "1\n"; "22\n"; "333\n" ])Source.of_bigstring_chunks
Pulls slices Some (buf, off, len) of a bigstring (Bigstringaf.t, Cstruct.buffer, Unix.read_bigarray) or None for EOF. Copies at most 2040 bytes at a time into Parseff’s input buffer; slices must remain unchanged until the next callback invocation, which may reuse the same buffer.
type bigstring = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
val of_bigstring_chunks : (unit -> (bigstring * int * int) option) -> Source.tlet parse_bigstring buf parser = let sent = ref false in let source = Parseff.Source.of_bigstring_chunks (fun () -> if !sent then None else ( sent := true; Some (buf, 0, Bigarray.Array1.dim buf))) in Parseff.Source.parse source parser
let () = let text = "key=value" in let buf = Bigarray.Array1.init Bigarray.char Bigarray.c_layout (String.length text) (String.get text) in let pair () = let key = Parseff.take_while (fun c -> c <> '=') in let _ = Parseff.char '=' in (key, Parseff.take_while (fun _ -> true)) in match parse_bigstring buf pair with | Ok pair -> assert (pair = ("key", "value")) | Error e -> failwith (Parseff.render_error e)Source.of_function
Low-level escape hatch matching POSIX read(2): read buf off len writes up to len bytes into buf at off and returns the byte count (0 for EOF). Starts with a buffer of ~initial_buffer_size bytes (default 2040) and grows once to max initial_buffer_size 4096 after a read fills it. Prefer of_chunks or of_seq unless wrapping Unix.read, Unix.recv, or similar buffer-filling APIs.
val of_function : ?initial_buffer_size:int -> (bytes -> int -> int -> int) -> Source.tlet source = Parseff.Source.of_function ~initial_buffer_size:512 (fun buf off len -> Unix.read fd buf off len)inParseff.Source.parse source my_parserPush streaming: Push
Use Parseff.Push when your event loop pushes chunks into the parser (Lwt, Async, network callbacks).
module Push : sig type 'a t type 'a step = | Await | Done of ('a * string, error) Stdlib.result
val parse : ?max_depth:int -> ?backtrack_window:int -> ?diagnostics:Diagnostics.t -> ?trace:(trace_event -> unit) -> (unit -> 'a) -> 'a t val feed : 'a t -> string -> 'a step val finish : 'a t -> ('a * string, error) Stdlib.result val cancel : 'a t -> unit
type each_step = | Await | Stopped of string | Error of error | Ended
val parse_each : ?max_depth:int -> ?backtrack_window:int -> ?diagnostics:Diagnostics.t -> ?trace:(trace_event -> unit) -> ?pending:(unit -> bool) -> on_value:('a -> [ `Continue | `Stop ]) -> (unit -> 'a) -> (string option -> each_step) * (unit -> unit)endParseff.Push.parse, Parseff.Push.feed, and Parseff.Push.finish parse a single value and return any leftover bytes in Done (Ok (value, leftover)). End your parser with Parseff.end_of_input if you want to require full consumption. To drop an unfinished parse, for example when the client disconnects, call Parseff.Push.cancel: it releases the Parseff.Ref cells and the ~diagnostics collector that the parse holds, without giving it more input.
Parseff.Push.parse_each parses a stream of values on a single effect fiber (the push counterpart of Parseff.Source.parse_each). It returns a push function and a cancel function. Push Some chunk for data and None at EOF:
let line () = let s = Parseff.take_while (fun c -> c <> '\n') in let _ = Parseff.char '\n' in s
let push_lines chunks = let seen = ref [] in let push, _cancel = Parseff.Push.parse_each ~on_value:(fun s -> seen := s :: !seen; `Continue) line in let rec go = function | [] -> push None | chunk :: rest -> ( match push (Some chunk) with | Parseff.Push.Await -> go rest | step -> step) in match go chunks with | Parseff.Push.Ended -> Ok (List.rev !seen) | Parseff.Push.Error e -> Error e | Parseff.Push.Await | Parseff.Push.Stopped _ -> assert false
let () = assert (push_lines [ "ab"; "c\nde\n" ] = Ok [ "abc"; "de" ])Backtracking and ~backtrack_window
Parseff retains buffered bytes from the oldest active rewind point to the newest read byte. Two things pin earlier bytes:
- Open backtracking frames (
or_,one_of,look_ahead, repetition loops). - Primitives building a single value across chunks (
take,take_while,recognize), even afterParseff.commitorParseff.cut.
To stay within ~backtrack_window:
- Dispatch early with
Parseff.peek_charrather than trying long speculative branches. - Call
Parseff.committo release the nearest frame once an alternative is chosen, orParseff.cutto release every frame inside the enclosingParseff.contextonce a construct is decided. - Use
Parseff.Iterto stream items or large byte payloads past~backtrack_window(seeIter). - Raise
~backtrack_windowonly when the grammar genuinely requires longer lookahead.
let http_method () = Parseff.or_ (fun () -> Parseff.literal "GET "; Parseff.commit (); `GET) (fun () -> Parseff.literal "POST "; Parseff.commit (); `POST)Iter
Parseff.Iter passes each value or byte piece to a callback f instead of returning it. Memory stays bounded by one element or one piece, so the input can be much larger than ~backtrack_window.
module Iter : sig val many : ?at_least:int -> (unit -> 'a) -> ('a -> unit) -> unit val sep_by : ?at_least:int -> (unit -> 'a) -> (unit -> 'b) -> ('a -> unit) -> unit val take : int -> (Slice.t -> unit) -> unit val take_while : ?at_most:int -> ?set:Charset.t -> (char -> bool) -> (Slice.t -> unit) -> unitendParseff.Iter.manyandParseff.Iter.sep_bywork likeParseff.manyandParseff.sep_by, but give each element tofand do not build a list.Parseff.Iter.takeandParseff.Iter.take_whilework likeParseff.takeandParseff.take_while, but give the bytes tofas non-emptyParseff.Slice.tpieces. A piece borrows the input buffer and is valid only duringf. CallParseff.Slice.to_stringto keep it.Iter.take_while ~at_mostfails withLimit_exceededafterfhas receivedat_mostbytes, and the bytes need not fit in~backtrack_window.
let sum_ints input = let total = ref 0 in let r = Parseff.parse input (fun () -> Parseff.Iter.sep_by Parseff.integer (fun () -> Parseff.char ',') (fun n -> total := !total + n)) in Result.map (fun () -> !total) rAll four functions share these rules:
- Sealing. Before each call to
f, they seal every open backtracking frame, likeParseff.cutoutside anyParseff.context. The source can then drop earlier input. A later failure cannot rewind before a value thatfalready received.Parseff.recognizeandParseff.Slice.recognizestill keep all of their input. - Callback.
fruns outside the parser. Do not call parsers orParseff.warninsidef. An exception fromfstops the parse and propagates out of the runner. - Frames that cannot be sealed. A call inside
Parseff.look_ahead,Parseff.peek_with,Parseff.not_followed_by, aParseff.precedencelookup, orParseff.capturewithout~release:trueraisesInvalid_argument. Some other frames raise only when~diagnosticsis active, so test these parsers with~diagnostics.
See Parseff.Iter for the full contract.
examples/streaming_json_array/main.ml parses 20000 array elements through Iter.sep_by under Source.parse ~backtrack_window:4096, so the input is far larger than the window.
Iter or parse_each
| Input | Use |
|---|---|
| A top-level stream of values, such as NDJSON or log records. | Parseff.Source.parse_each or Parseff.Push.parse_each. |
| Repeated items inside a larger structure, such as the elements of a JSON array between its brackets. | Parseff.Iter.many or Parseff.Iter.sep_by inside one Parseff.Source.parse. |
A body larger than ~backtrack_window, such as a length-prefixed payload. | Parseff.Iter.take or Parseff.Iter.take_while. |
Concurrency and domains
- Multiple domains:
Parseff.parseandParseff.Source.parsestore active parse state per domain and can run concurrently across OCaml 5 domains. Do not share oneParseff.Source.torParseff.Push.tconcurrently across domains. - Cooperative fibers on one domain: multiple
Parseff.Source.parsecalls (e.g., Eio fibers) andParseff.Pushparses can interleave on one domain, provided only the source’s read function suspends (suspending inside a parser callback is not supported). - System threads: OCaml
Threads sharing a domain can preempt mid-parser; serialize parses on one domain with aMutexor run them on separate domains.