package parseff
All checked builds passInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=dad671e98bdf1816c76a5e61da157bf38a986057ed2f9416573143bb8f1afcce
sha512=2ac03f296c73611ff6905e29c6c7df5bc3319933dbe3525a165d28f1dd56d4af4db5a0093b0d805064dc310322cef4e8b03ba1b2faf8890af2f202552e8ed2d7
doc/README.html
Parseff
Parseff is a direct-style parser combinator library for OCaml 5 where parsers are plain functions (unit -> 'a), errors carry the failure position, what was expected and a source excerpt, and the runtime handles control flow, backtracking, and streaming input. Designed for performance with zero-copy span APIs and fused operations.
Installation
$ opam install parseff -yExample
(* Add our own error case to Parseff *)
type Parseff.custom_error += Out_of_range of int
let number () =
let digits = Parseff.many ~at_least:1 Parseff.digit () in
let n = List.fold_left (fun acc d -> (acc * 10) + d) 0 digits in
if n >= 0 && n <= 255 then n
else Parseff.error (Out_of_range n)
let ip_address () =
let a = number () in
let _ = Parseff.char '.' in
let b = number () in
let _ = Parseff.char '.' in
let c = number () in
let _ = Parseff.char '.' in
let d = number () 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)A failed parse returns one Parseff.error. Parseff.render_error prints the message, the source line and a caret under the failure:
address:1:11: [error] expected digit, found end of input
1 | 192.168.1.
| ^Features
- Build parsers with direct-style and compose with
Parseffcombinators - API is designed to be expressive enough to not need monadic operators (
>>=,>>|,*>), nor binding operators (let*,let+,and+) - Typed domain errors and source excerpts: every error says what was expected at the furthest failure, with a caret under the source
- Automatic backtracking with
Parseff.or_ - Minimal dependency footprint only
refor regex support anduucpfor Unicode support - Streaming support
- Domain-safe each
Parseff.parse/Parseff.parse_sourcecall is self-contained with no global mutable state, so independent parses can run in parallel across domains - Zero-copy span APIs for low-allocation parsing
- Fused operations for hot paths
Performance
Parseff is built for throughput and low allocation: in the current benchmark suite, the generic Parseff parsers are up to ~1.5x faster than Angstrom's baselines, and its fused zero-copy paths roughly ~3x faster.
Even against Angstrom's optimized JSON parser, Parseff's optimized path is still ~1.5x faster while cutting minor allocations from ~11.1 GB to ~1.3 GB; against Angstrom's generic JSON parser, the generic / optimized Parseff paths land at ~800 MB / ~1.3 GB versus ~5.9 GB.
See the full comparison for the methodology and results, bench/bench_json.ml for the JSON benchmark, and bench/ for the full suite.
Documentation
- Quick start
- API reference
- Your first parser
- Error handling
- Making parsers fast
- Comparison with Angstrom
- A JSON parser
- Expressions with precedence
Contributing
- Open an issue to discuss proposed changes
- Write tests for new features
- Run
make fmtbefore submitting - Ensure all tests pass with
make test
License
MIT. See LICENSE for details.