package parseff

  1. Overview
  2. Docs
Direct-style parser combinator library for OCaml 5 powered by algebraic effects

Install

dune-project
 Dependency

Authors

Maintainers

Sources

parseff-0.3.0.tbz
sha256=133646bafed8921551767b27ab01aed3106b5c1eaadc86133357901c16ebc5e2
sha512=6da046fdb320999fbc244ea01e02e54c451d926f8bc7cf6a8036b0e576a622d35780712f59aae1e4d6f386289b0040f588123e2ac8290a0e505caf66ff9a94eb

doc/parseff/Parseff/Source/index.html

Module Parseff.SourceSource

Input sources for incremental parsing. A source wraps a readable byte stream — a channel, file descriptor, or custom reader — behind a uniform interface. The parser pulls data on demand through the effect handler; existing parser code works unchanged.

Sourcetype t
Sourceval of_string : string -> t

of_string s creates a source from a complete string. Useful for testing streaming code paths with known input.

Sourceval of_channel : ?buf_size:int -> in_channel -> t

of_channel ?buf_size ic creates a source that reads from ic. buf_size controls the internal read buffer (default 4096).

Sourceval of_function : (bytes -> int -> int -> int) -> t

of_function read creates a source that calls read buf off len to obtain up to len bytes starting at offset off in buf. Must return the number of bytes actually read; return 0 to signal EOF.

This is the low-level escape hatch for byte sources that fill a pre-allocated buffer, such as Unix.read or Unix.recv. For most use cases, prefer of_chunks or of_seq.

Sourceval of_chunks : (unit -> string option) -> t

of_chunks read creates a source that calls read () to obtain the next chunk of input. Return Some s with a non-empty string for data, or None to signal EOF. Empty strings (Some "") are silently skipped.

This is the recommended way to wrap effectful or callback-based data sources:

(* Eio promise *)
let fetched = ref false in
let source = Source.of_chunks (fun () ->
    if !fetched then None
    else begin
      fetched := true;
      Some (Promise.await_exn promise)
    end)
Sourceval of_seq : string Seq.t -> t

of_seq seq creates a source from a lazy sequence of string chunks. Each element is a chunk of input; the sequence ending (Seq.Nil) signals EOF.

let source = Source.of_seq (List.to_seq [ "chunk1"; "chunk2"; "chunk3" ])