Convenience combinators
Ready-to-use parsers for ASCII character classes, numbers, whitespace, tokens, fixed-length slices, and framed payloads.
Character classes
val digit : unit -> charval letter : unit -> charval alphanum : unit -> charval any_char : unit -> charParseff.digit: ASCII decimal digit ('0'..'9'). UseParseff.integerto parse an integer value directly.Parseff.letter: ASCII letter ('a'..'z'or'A'..'Z').Parseff.alphanum: ASCII letter or digit.Parseff.any_char: any single byte; fails only at end of input.
let username () = let first = Parseff.letter () in let rest = Parseff.many Parseff.alphanum in String.make 1 first ^ String.of_seq (List.to_seq rest)Numbers
All number parsers stop at the first byte that does not belong to the number and work across chunk boundaries in Parseff.Source.parse.
val integer : unit -> intval number : unit -> floatval hex_integer : unit -> intParseff.integer: optional-followed by ASCII digits. Fails without a digit or onintoverflow.Parseff.number: JSON-grammar decimal float (optional-, digits, optional fraction only when a digit follows'.', optional exponent only when a digit or sign+digit follows'e'/'E'). Fails without a digit or on float overflow."-0"returns-0.(whereasintegerreturns0).Parseff.hex_integer: unsigned ASCII hex digits (no sign, no0xprefix). Fails without a digit or abovemax_int.
let point () = let x = Parseff.integer () in let _ = Parseff.char ',' in let y = Parseff.integer () in (x, y)
let color () = let _ = Parseff.char '#' in Parseff.hex_integer ()Custom number formats
integer, number, and hex_integer do not accept leading +, .5, digit separators (1_000), inf, or nan, and stop before non-matching suffixes: number on "5." returns 5. and stops at '.', and on "2em" returns 2. and stops at 'e'. Build custom formats with Parseff.take_while and Parseff.recognize:
let is_digit c = c >= '0' && c <= '9'let is_letter c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
let quantity () = let text = Parseff.recognize (fun () -> let _ = Parseff.take_while ~at_least:1 ~label:"digit" is_digit in Parseff.optional (fun () -> let _ = Parseff.char '.' in Parseff.take_while ~at_least:1 ~label:"digit" is_digit)) in let unit_ = Parseff.take_while ~at_least:1 ~label:"unit" is_letter in (float_of_string text, unit_)# Parseff.parse "2em" quantity;;- : (float * string, Parseff.error) result = Ok (2., "em")# Parseff.parse "1.5eV" quantity;;- : (float * string, Parseff.error) result = Ok (1.5, "eV")Whitespace
val is_whitespace : char -> boolval whitespace : ?at_least:int -> unit -> stringval skip_whitespace : unit -> unitParseff.is_whitespacereturnstruefor space, tab, newline, and carriage return.Parseff.whitespacereads zero or more ASCII whitespace bytes as a string (pass~at_least:1to require at least one).Parseff.skip_whitespaceskips ASCII whitespace without allocating. Prefer it whenever you do not need the matched string.
let words () = Parseff.sep_by ~at_least:1 (fun () -> Parseff.take_while ~at_least:1 ~label:"word" (fun c -> not (Parseff.is_whitespace c))) (fun () -> Parseff.whitespace ~at_least:1 ())Tokens
lexeme
Parseff.lexeme runs a parser, then ~skip (default: Parseff.skip_whitespace), and returns the parser’s result.
val lexeme : ?skip:(unit -> unit) -> (unit -> 'a) -> 'alet skip_space_and_comments () = Parseff.skip_whitespace (); while Parseff.peek_char () = Some '#' do Parseff.skip_while (fun c -> c <> '\n'); Parseff.skip_whitespace () done
let token p = Parseff.lexeme ~skip:skip_space_and_comments p
let pair () = let a = token Parseff.digit in let _ = token (fun () -> Parseff.char ',') in let b = token Parseff.digit in (a, b)keyword, is_ident_char, and identifier
Parseff.is_ident_char:truefor ASCII letters, digits, and'_'.Parseff.keyword: matches a literal only when the following byte does not satisfy~is_ident(defaultParseff.is_ident_char), sokeyword "let"rejects"letter"at offset 0. RaisesInvalid_argumenton"".Parseff.identifier: matches onestartbyte followed by zero or moreinnerbytes.~reservedrejects keywords as ordinaryExpectedfailures at offset 0 so alternatives inParseff.or_still work.
val is_ident_char : char -> boolval keyword : ?is_ident:(char -> bool) -> string -> unitval identifier : ?label:string -> ?reserved:(string -> bool) -> (char -> bool) -> (char -> bool) -> stringlet is_start c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_'
let reserved = function | "let" | "in" | "if" | "then" | "else" -> true | _ -> false
let name () = Parseff.identifier ~reserved is_start Parseff.is_ident_charIn streaming, keyword needs String.length s + 1 bytes of ~backtrack_window, and identifier needs the word length plus one byte.
Fixed-length reads and framed input
val take : int -> stringval within : int -> (unit -> 'a) -> 'aParseff.takereads exactlynbytes as a string (fails with anExpectederror if fewer thannbytes remain orn < 0).Parseff.within(unstable) runs a parser bounded to the nextnbytes, presenting EOF at the frame boundary without copying the payload. Fails if bytes remain unread inside the frame or the input ends beforenbytes.- To stream a payload larger than
~backtrack_windowto a callback, seeIter.
let frame () = let len = Parseff.BE.any_uint16 () in Parseff.within len (fun () -> let tag = Parseff.any_uint8 () in let body = Parseff.take_while (fun _ -> true) in (tag, body))