package parseff
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
Direct-style parser combinator library for OCaml 5 powered by algebraic effects
Install
dune-project
Dependency
Authors
Maintainers
Sources
parseff-0.1.0.tbz
sha256=097c71a38b39ab5925518e16c0efdf3b77a6b3b2185c82f168e0f1f4cb0772bf
sha512=811fbd770148bf3004ffc764dc08fa1a3ded9b4613f5749a6d2841c1af868de7afff4dd6b808b38254d28592433e23613573707159dfa171687839c520e93bb3
doc/errors.html
Error handling
Parseff exposes three main APIs for error handling:
Parseff.fail: fail with a string messageParseff.error: fail with a typed custom valueParseff.expect: relabel failures from another parser
fail
Abort parsing with a message. The message is returned as Error { error = `Expected msg; ... }.
val fail : string -> 'alet byte () =
let n =
int_of_string
(Parseff.take_while1
(fun c -> c >= '0' && c <= '9')
~label:"digit")
in
if n >= 0 && n <= 255 then n
else Parseff.fail "number must be between 0 and 255"
let () =
match Parseff.parse "300" byte with
| Error { pos; error = `Expected msg } ->
(* pos = 0, msg = "number must be between 0 and 255" *)
Printf.printf "Error at %d: %s\n" pos msg
| Ok n -> Printf.printf "Got: %d\n" n
| Error _ -> ()Use this when a human-readable string is enough.
error
Abort parsing with a typed custom error value.
val error : 'e -> 'alet number () =
let s =
Parseff.take_while1 (fun c -> c >= '0' && c <= '9') ~label:"digit"
in
let n = int_of_string s in
if n > 255 then Parseff.error (`Too_large n)
else if n < 0 then Parseff.error (`Negative n)
else n
let () =
match Parseff.parse "300" number with
| Error { error = `Too_large n; pos } ->
Printf.printf "Number %d too large at position %d\n" n pos
| Error { error = `Negative n; pos } ->
Printf.printf "Negative number %d at position %d\n" n pos
| Error { error = `Expected msg; _ } ->
Printf.printf "Parse error: %s\n" msg
| Error _ -> Printf.printf "Other error\n"
| Ok n -> Printf.printf "Got %d\n" nUse this when callers need to pattern match on specific failure cases.
Polymorphic variants work great with error for quick error types:
let number_quick () =
let s =
Parseff.take_while1 (fun c -> c >= '0' && c <= '9') ~label:"digit"
in
let n = int_of_string s in
if n > 255 then Parseff.error `Too_large
else if n < 0 then Parseff.error `Negative
else nexpect
Run a parser and replace its failure message with a clearer description.
val expect : string -> (unit -> 'a) -> 'alet dot () =
Parseff.expect "a dot separator" (fun () -> Parseff.char '.')
let number () =
let s =
Parseff.take_while1 (fun c -> c >= '0' && c <= '9') ~label:"digit"
in
int_of_string s
let ip_address () =
let a = number () in
let _ = dot () in
let b = number () in
let _ = dot () in
let c = number () in
let _ = dot () in
let d = number () in
Parseff.end_of_input ();
(a, b, c, d)Without expect, a failed char '.' reports expected '.'. With expect, it reports expected a dot separator.
Built-in error variants
Parseff adds three built-in error variants to every parse result:
`Expected of string: The parser encountered the wrong input at a given position. Produced byParseff.fail, primitive mismatches (e.g.Parseff.charseeing the wrong character), andParseff.expectrelabeling.`Unexpected_end_of_input: The input ended before the parser could finish. Produced when primitives likeParseff.char,Parseff.consume, orParseff.satisfyneed more input but have reached the end.`Depth_limit_exceeded of string: AParseff.rec_call exceeded the~max_depthlimit. The message contains the depth that was exceeded.
Your own error types from Parseff.error are merged with these via polymorphic variant row extension.
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page