Binary parsing
Parseff.BE (big-endian) and Parseff.LE (little-endian) provide multi-byte integer and IEEE 754 float parsers. Single-byte readers (Parseff.any_uint8, Parseff.any_int8) and Parseff.any_varint live at the top level of Parseff.
Integer parsers
(* Top-level *)val any_uint8 : unit -> int (* 1 byte, 0..255 *)val any_int8 : unit -> int (* 1 byte, -128..127 *)val any_varint : unit -> int (* 1..9 bytes, unsigned LEB128 *)
(* Inside both Parseff.BE and Parseff.LE *)val any_int16 : unit -> int (* 2 bytes, signed *)val any_uint16 : unit -> int (* 2 bytes, unsigned *)val any_uint24 : unit -> int (* 3 bytes, 0..16777215 *)val any_int32 : unit -> int32 (* 4 bytes, signed *)val any_uint32 : unit -> int (* 4 bytes, 0..4294967295 *)val any_int64 : unit -> int64 (* 8 bytes, signed *)Parseff.any_varintreads an unsigned LEB128 varint (7 bits per byte, LSB first;"\xAC\x02"is300). Fails if longer than 9 bytes or abovemax_int(rejecting negative Protobufint32/int64). Decode zigzag values manually with(n lsr 1) lxor (-(n land 1)).- On platforms where
inthas fewer than 63 bits (such asjs_of_ocaml), large values inany_varintorany_uint32overflow silently.
let png_chunk_length () = Parseff.BE.any_int32 ()let http2_frame_length () = Parseff.BE.any_uint24 ()let zip_entry_size () = Parseff.LE.any_uint32 ()
let protobuf_bytes_field () = let len = Parseff.any_varint () in Parseff.take len
let () = assert (Parseff.parse "\xAC\x02" Parseff.any_varint = Ok 300)Floating-point parsers
Inside both Parseff.BE and Parseff.LE:
val any_float : unit -> float (* 4 bytes, IEEE 754 single precision *)val any_double : unit -> float (* 8 bytes, IEEE 754 double precision *)let coordinates () = let lat = Parseff.BE.any_double () in let lon = Parseff.BE.any_double () in (lat, lon)Exact-match parsers
Parseff.BE.int16, Parseff.BE.int32, and Parseff.BE.int64 (and their Parseff.LE counterparts) assert that the next bytes encode an exact constant. int16 accepts -32768..65535 and raises Invalid_argument outside that range.
val int16 : int -> unitval int32 : int32 -> unitval int64 : int64 -> unitlet png_magic () = Parseff.BE.int32 0x89504E47l
let wav_header () = Parseff.literal "RIFF"; let size = Parseff.LE.any_int32 () in Parseff.literal "WAVE"; sizeFixed-length reads and framed payloads
Parseff.take reads exactly n bytes as a string (fails with an Expected error if fewer than n bytes remain or n < 0). Parseff.Slice.take returns a zero-copy Parseff.Slice.t (see Zero-copy API). Parseff.within (unstable) runs an inner parser bounded to the next n bytes without copying.
val take : int -> stringval Slice.take : int -> Slice.tval within : int -> (unit -> 'a) -> 'aMixing binary and text primitives
Binary and text primitives compose freely, including backtracking with Parseff.or_:
let tlv_field () = let tag = Parseff.any_uint8 () in let len = Parseff.BE.any_uint16 () in let value = Parseff.take len in (tag, value)
let tlv_message () = Parseff.literal "TL"; let version = Parseff.or_ (fun () -> Parseff.BE.int16 0xFFFF; `Legacy) (fun () -> `Version (Parseff.BE.any_uint16 ())) in let count = Parseff.BE.any_uint16 () in let fields = Parseff.count count tlv_field in (version, fields)Errors, positions, and streaming
- Short reads: fail at the start of the field with
expected N bytes, found only K (end of input)(orfound end of inputif no bytes remain). - Exact-match mismatches: fail at the start of the field with an
Expectederror formatted in hexadecimal, e.g.expected int32be 0x01020304, found 0xDE000000, merging with other alternatives at the same offset. - Positions: always byte offsets, advancing by the field width.
- Streaming and performance: multi-byte reads use the stdlib’s
String.get_int16_be/String.get_int32_leprimitives directly on the buffer and work across chunk boundaries inParseff.Source.parseandParseff.Push.