UTF-8 parsing
Parseff.Utf8 provides primitives operating on Unicode code points (Uchar.t) decoded from UTF-8 string input, backed by uucp for Unicode character properties.
Primitives operate at the code-point level (a base character plus combining mark is two code points). For grapheme clusters, compose them with a library such as uuseg.
Primitives
val satisfy : ?label:string -> (Uchar.t -> bool) -> Uchar.tval char : Uchar.t -> Uchar.tval any_char : unit -> Uchar.tval take_while : ?at_least:int -> ?at_most:int -> ?label:string -> (Uchar.t -> bool) -> stringval skip_while : ?at_least:int -> ?at_most:int -> ?label:string -> (Uchar.t -> bool) -> unit
module Slice : sig val take_while : ?at_least:int -> ?at_most:int -> ?label:string -> (Uchar.t -> bool) -> Slice.tendParseff.Utf8.satisfydecodes the next code point (1–4 bytes) and tests the predicate, failing withexpected <label>(default"matching character").Parseff.Utf8.charmatches an exactUchar.t.Parseff.Utf8.any_chardecodes and returns the next code point, failing at EOF or on invalid UTF-8.Parseff.Utf8.take_whileconsumes code points while the predicate holds and returns the matched UTF-8 string.~at_leastand~at_mostcount code points, not bytes.Parseff.Utf8.skip_whileskips matching code points without allocating.Parseff.Utf8.Slice.take_whilereturns a zero-copyParseff.Slice.t.- All three take
~at_least,~at_most, and~label, with the same failure. A(at_most + 1)-th matching code point fails withLimit_exceeded { limit = at_most }covering those code points.
Mixing with byte-level primitives
UTF-8 and byte-level primitives mix freely in the same parser: use byte-level primitives for ASCII syntax and Parseff.Utf8 for text content.
let field () = let key = Parseff.take_while ~at_least:1 ~label:"key" (fun c -> c >= 'a' && c <= 'z') in Parseff.literal ":"; Parseff.Utf8.skip_whitespace (); let value = Parseff.Utf8.take_while ~at_least:1 ~label:"value" (fun u -> Uchar.to_int u <> 0x0A) in (key, value)
let lambda () = Parseff.Utf8.char (Uchar.of_int 0x03BB)Convenience combinators
val letter : unit -> Uchar.tval alphanum : unit -> Uchar.tval is_whitespace : Uchar.t -> boolval whitespace : ?at_least:int -> unit -> stringval skip_whitespace : unit -> unitParseff.Utf8.letter: matches Unicode alphabetic code points (Uucp.Alpha.is_alphabeticacross Latin, Greek, Cyrillic, CJK, Arabic, Devanagari, etc.).Parseff.Utf8.alphanum: matches a Unicode alphabetic code point or ASCII digit.Parseff.Utf8.is_whitespace,Parseff.Utf8.whitespace, andParseff.Utf8.skip_whitespace: match the UnicodeWhite_Spaceproperty (Uucp.White.is_white_space, includingU+00A0,U+2002,U+2003,U+3000).~at_leastonwhitespacecounts code points.
Invalid UTF-8
All Utf8 primitives fail with expected valid UTF-8 on bare continuation bytes (0x80–0xBF), invalid lead bytes (0xFE, 0xFF), overlong encodings, and truncated sequences, pointing at the first invalid byte (expected valid UTF-8, found '\xFF'). Invalid UTF-8 always reports valid UTF-8 regardless of ~label.
Positions, columns, and streaming
- Positions:
Parseff.positionandlocation.byte_colalways count bytes (a singleUtf8.satisfyadvances by 1–4 bytes). - Error columns and carets:
Parseff.render_errorheader columns count code points, while carets count terminal cells (wide characters like名take two, combining marks take zero). Seeerrorsfor streaming dropped-prefix behavior.
input.json:1:6: [error] expected ',' or ']', found '"'1 | ["é" "b"] | ^- Streaming: all
Utf8primitives work across chunk boundaries inParseff.Source.parseandParseff.Push.