Error handling
Parseff exposes four main APIs for errors:
Parseff.fail: fail with a string messageParseff.error: fail with a typedParseff.custom_errorvalueParseff.expect: relabelExpectedfailures at a parser’s startParseff.contextandParseff.cut: name constructs and seal backtracking
Built-in and custom errors
Built-in errors come from Parseff itself: primitive mismatches (Expected), messages from Parseff.fail (Failed), or limit checks (Limit_exceeded, Depth_limit_exceeded, Backtrack_window_exceeded). Parseff.render_error formats all of them out of the box.
Custom errors are constructors you add to the extensible variant Parseff.custom_error, raised with Parseff.error and returned in kind as Custom. Use them when callers need to match on domain-specific validation failures in code. Pass ~custom to Parseff.render_error to format them.
type Parseff.custom_error += Out_of_range of int
let rating () = let n = Char.code (Parseff.digit ()) - Char.code '0' in if n < 1 || n > 5 then Parseff.error (Out_of_range n) else n
let () = match Parseff.parse "7" rating with | Ok n -> Printf.printf "Got %d\n" n | Error { kind = Custom (Out_of_range n); _ } -> Printf.printf "%d is out of range\n" n | Error e -> prerr_endline (Parseff.render_error e)fail and error
Both Parseff.fail and Parseff.error fail at the current position and participate in normal backtracking: Parseff.or_, Parseff.many, Parseff.optional, and other combinators absorb them and try the next alternative. Call Parseff.cut first to make a failure fatal.
val fail : ?from:int -> ?upto:int -> ?notes:note list -> ?help:string -> string -> 'a
val error : ?from:int -> ?upto:int -> ?notes:note list -> ?help:string -> custom_error -> 'a~fromand~upto(both default to the current position) set the underlined byte range.~notesattaches annotations: a note withat = Some rangeunderlines that range in the excerpt;at = Noneprints a* note:line.~helpprints a* help:line after any notes.
let octet () = let start = Parseff.position () in let n = int_of_string (Parseff.take_while ~at_least:1 ~label:"digit" (fun c -> c >= '0' && c <= '9')) in if n <= 255 then n else ( Parseff.cut (); Parseff.fail ~from:start ~notes:[ { Parseff.at = None; text = "octets go from 0 to 255" } ] "octet out of range")
let () = match Parseff.parse "10.300" (fun () -> let a = octet () in let _ = Parseff.char '.' in (a, octet ())) with | Error e -> print_endline (Parseff.render_error ~name:"address" e) | Ok _ -> ()Output:
address:1:4: [error] octet out of range1 | 10.300 | ^^^ * note: octets go from 0 to 255Parseff.expected_token fails with an Expected item instead, for a token that a separate lexer found or any range the parser scanned: found and the range cover the bytes up to ~upto, the item merges with other expected items at the same position, and Parseff.expect relabels it. It takes ~help and ~notes like fail.
expect
Parseff.expect replaces Expected failures at the parser’s starting position with expected <description>. Failures past the starting position, as well as fail and error, are preserved.
val expect : string -> (unit -> 'a) -> 'alet dot () = Parseff.expect "a dot separator" (fun () -> Parseff.char '.')context and cut
Parseff.context names a grammar construct so failures inside it report * while parsing <name>. Parseff.cut seals backtracking frames inside the enclosing context so a later failure becomes the parse error instead of retrying outer alternatives. See combinators.
To collect multiple errors and keep parsing, see the Error recovery guide.
Furthest-failure selection
When a parse fails, Parseff reports the failure at the highest byte offset reached across all branches, including branches abandoned by or_, many, sep_by, optional, or fold_*:
- Multiple primitive mismatches at that position merge into one sorted, deduplicated
Expectedlist:expected "else", "if", or "while", found 'x'. - At the same position,
failanderrorwin overExpected; between twofail/errorcalls, the first wins. literalfails at the first differing byte and expects the remaining suffix.- End-of-input failures preserve what was expected:
"[1, 2"] reports [expected ',' or ']', found end of input. Parseff.commitdiscards failures at or beyond its position so a committed branch reports only its own expectations.
The error value
type error = private { severity : severity; kind : error_kind; range : range; location : location option; contexts : context list; notes : note list; help : string option; excerpt : excerpt;}error and its nested types are private (readable and matchable, not constructible), except Parseff.range and Parseff.note.
kind cases:
Expected { items; found; more }: primitive mismatch.itemsholds up to 64 sorted, unique expectations (Char,Literal,Label,End_of_input);morecounts overflow items.foundisText(input snippet),End(EOF),Short n(short fixed-width read), orValue(mismatched binary integer).Failed msg: raised byParseff.fail, or"no alternative matched"forone_of [].Custom e: raised byParseff.error. Match with a fallback_case.Limit_exceeded { limit }: exceeded~at_mostontake_while,skip_while,Slice.take_while,take_until,skip_until,Utf8.take_while,Utf8.skip_while,Utf8.Slice.take_while,many,sep_by, orend_by. Fatal likecut (); fail: no enclosing choice tries another option, up to the nearestParseff.look_ahead,Parseff.peek_withorParseff.capture. Usecaptureto recover; with~release:trueit resumes after the item that crossed the limit.Depth_limit_exceeded { limit }: exceeded~max_depthonParseff.rec_or stack depth inParseff.fold_right.Backtrack_window_exceeded { limit; oldest }: exceeded~backtrack_windowin a streaming parse.
Rendering errors
Parseff.render_error formats an error with its source excerpt, carets, notes, help, and open constructs; Parseff.error_message returns the one-line message alone.
input.json:4:5: [error] expected ',' or '}', found '"'2 | "config": { | ╭─────────────╯3 | │ "a": 14 | │ "b": 2 | ╰─────^ * while parsing object, from 2:13 * while parsing object, from 1:1Formatting details:
- Header:
name:line:col: [error] message(orline:colwithout~name;byte Nwith no excerpt when the line is unknown in streaming). Lists up to 8 expected items before, or N more alternatives. - Columns and carets: header and context columns count Unicode code points, while
location.byte_colcounts bytes. Carets and note underlines count terminal cells (wide characters like名take two, combining marks take zero), preserve tabs, and cover escaped control/invalid UTF-8 bytes (\x1B,\u{00A0}). - Streaming dropped prefixes: when streaming dropped the start of the error line and
~sourceis omitted, the header still reports the code-point column unless the error starts inside the dropped prefix (in which case it falls back tolocation.byte_col, and contexts in that prefix showfrom byte N). The excerpt prefixes...and places carets only under retained bytes. Pass~sourceif you have the full input. - Sources: line and column restart at each
Parseff.Source.parse, so for its errors pass as~sourcethe input fromParseff.Source.offset, read before the parse, onward. - Contexts: prints up to 5 innermost
* while parsinglines (plus one more rather than hiding a single line). Delimited constructs spanning lines render the╭─╯/│/╰──^bar, collapsing multi-line gaps to.... - Notes and help: ranged notes underline with
-; unranged notes print as* note:;~help(and built-in hints for depth/window limits) prints as* help:. - Custom errors: pass
~custom; without it,Customprintscustom error (no ~custom printer given).