Skip to content

Quick start

Parseff is a direct-style parser combinator library for OCaml 5. Parsers are plain functions (unit -> 'a); the runtime handles backtracking, streaming input, and error reporting with source excerpts. Designed for performance with zero-copy slice APIs and fused operations.

Features

  • Direct-style parsers: compose plain unit -> 'a functions without monadic (>>=, >>|, *>) or binding (let*, let+, and+) operators.
  • Structured errors and source excerpts: furthest-failure reporting with merged expectations, typed custom errors, and construct contexts.
  • Error recovery: keep parsing after an error and report every error in one run. recover records the error, skips to a sync point, and returns a placeholder; list recovers per element; capture returns a failure as a result.
  • Automatic backtracking: Parseff.or_ and Parseff.one_of, with commit and cut to seal branches.
  • Pull and push streaming: Source.parse, Source.parse_each, and Push with bounded backtracking.
  • Domain-safe: parse state lives per domain so independent parses run in parallel across OCaml 5 domains.
  • Zero-copy slices and fused scanners: Slice.t, Charset, and sep_by_take for low-allocation hot paths.
  • Minimal dependencies: depends only on uucp for Unicode character properties.

Performance

Parseff is built for throughput and low allocation: in the benchmark suite, Parseff’s generic parsers are up to ~1.5x faster than Angstrom’s baselines, and its fused zero-copy paths are ~3x faster. Against Angstrom’s optimized JSON parser, Parseff’s optimized path is ~1.5x faster while reducing minor allocations from ~11.1 GB to ~1.3 GB.

See the comparison with Angstrom, bench/bench_json.ml, and bench/.

Installation

opam

Terminal window
opam install parseff -y

Dune package management

Add parseff to your dune-project:

(package
(name my_project)
(depends
(ocaml (>= 5.3))
parseff))

Lock and build:

Terminal window
dune pkg lock
dune build

From source

Terminal window
git clone https://github.com/davesnx/parseff.git
cd parseff
make init
make build
dune install

Add to your dune file

(executable
(name my_parser)
(libraries parseff))

Example

An IPv4 address parser validating octets in 0–255 (see the IP address guide):

type Parseff.custom_error += Out_of_range of int
let octet () =
let digits = Parseff.many ~at_least:1 Parseff.digit in
let value d = Char.code d - Char.code '0' in
let n = List.fold_left (fun acc d -> (acc * 10) + value d) 0 digits in
if n >= 0 && n <= 255 then n
else Parseff.error (Out_of_range n)
let ip_address () =
let a = octet () in
let _ = Parseff.char '.' in
let b = octet () in
let _ = Parseff.char '.' in
let c = octet () in
let _ = Parseff.char '.' in
let d = octet () in
(a, b, c, d)
let () =
match Parseff.parse "192.168.1.1" ip_address with
| Ok (a, b, c, d) ->
Printf.printf "Parsed: %d.%d.%d.%d\n" a b c d
| Error { kind = Custom (Out_of_range n); range; _ } ->
Printf.printf "Error at %d: %d out of range (0-255)\n" range.start n
| Error e ->
prerr_endline (Parseff.render_error ~name:"address" e)

API organization

SectionKey APIs
Primitivesliteral, char, satisfy, peek_char, Charset, take_while, skip_while, take_until, skip_until, end_of_input, position, with_range, location, recognize
Combinatorsor_, one_of, one_of_labeled, optional, look_ahead, peek_with, skip_to, not_followed_by, expect, context, commit, cut, rec_, parse ~trace
Repetition and separationmany, count, sep_by, end_by, between, fold_left, fold_right, expression, precedence
Conveniencedigit, letter, alphanum, any_char, integer, number, hex_integer, whitespace, skip_whitespace, lexeme, keyword, identifier, take, within
Errorsfail, error, expect, context, cut, render_error, error_message
Error recoverycapture, recover, require, list, skip_nested
Diagnosticswarn, Diagnostics, Ref
Zero-copy APISlice.take_while, Slice.take, Slice.recognize, Slice.sep_by_take, sep_by_take
StreamingSource.of_*, Source.parse, Source.parse_each, Push.parse, Push.feed, Push.finish, Push.cancel, Push.parse_each, Iter.many, Iter.sep_by, Iter.take, Iter.take_while
Binary parsingany_uint8, any_int8, any_varint, BE.*, LE.*
UTF-8 parsingUtf8.satisfy, Utf8.char, Utf8.any_char, Utf8.take_while, Utf8.letter, Utf8.whitespace, Utf8.Slice.take_while