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 -> 'afunctions 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.
recoverrecords the error, skips to a sync point, and returns a placeholder;listrecovers per element;capturereturns a failure as aresult. - Automatic backtracking:
Parseff.or_andParseff.one_of, withcommitandcutto seal branches. - Pull and push streaming:
Source.parse,Source.parse_each, andPushwith 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, andsep_by_takefor low-allocation hot paths. - Minimal dependencies: depends only on
uucpfor 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
opam install parseff -yDune package management
Add parseff to your dune-project:
(package (name my_project) (depends (ocaml (>= 5.3)) parseff))Lock and build:
dune pkg lockdune buildFrom source
git clone https://github.com/davesnx/parseff.gitcd parseffmake initmake builddune installAdd 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
| Section | Key APIs |
|---|---|
| Primitives | literal, char, satisfy, peek_char, Charset, take_while, skip_while, take_until, skip_until, end_of_input, position, with_range, location, recognize |
| Combinators | or_, one_of, one_of_labeled, optional, look_ahead, peek_with, skip_to, not_followed_by, expect, context, commit, cut, rec_, parse ~trace |
| Repetition and separation | many, count, sep_by, end_by, between, fold_left, fold_right, expression, precedence |
| Convenience | digit, letter, alphanum, any_char, integer, number, hex_integer, whitespace, skip_whitespace, lexeme, keyword, identifier, take, within |
| Errors | fail, error, expect, context, cut, render_error, error_message |
| Error recovery | capture, recover, require, list, skip_nested |
| Diagnostics | warn, Diagnostics, Ref |
| Zero-copy API | Slice.take_while, Slice.take, Slice.recognize, Slice.sep_by_take, sep_by_take |
| Streaming | Source.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 parsing | any_uint8, any_int8, any_varint, BE.*, LE.* |
| UTF-8 parsing | Utf8.satisfy, Utf8.char, Utf8.any_char, Utf8.take_while, Utf8.letter, Utf8.whitespace, Utf8.Slice.take_while |