Module Encore.Lavoisier
type state=|Partial of partial|Done|FailType of lavoisier's state.
The common way to use it is to fill a
Buffer.t when we get thePartialcase:let rec go = function | Partial { buffer; off; len; continue } -> let str = Bigstringaf.substring buffer ~off ~len in Buffer.add_string buf str ; go (continue ~committed:len) | Done -> Buffer.contents buf | Fail -> failwith "serialization" in go (emit v d)
and partial={buffer : string;off : int;len : int;continue : committed:int -> state;}type -'a tA serializer for values of type
'a.
val emit : 'a -> 'a t -> stateval emit_string : ?chunk:int -> 'a -> 'a t -> stringemit_string ?chunk v trunstwithv. The serializer allocates an internal buffer ofchunkbyte(s) (default to 4096 bytes) and enlarge it if it's needed.
val char : char tcharaccepts any character and emit it.
val string : string -> string tstring straccepts only a string which is equal tostrand emits it. Otherwise, it fails.
val pure : compare:('a -> 'a -> bool) -> 'a -> 'a tpure ~compare vaccepts only a value'aequal (seecompare) tovand emits it. Otherwise, it fails.
val choose : 'a t -> 'a t -> 'a tchoose p qrunspand emits it if succeeds. Ifpfails, then the output is reset andqwill be run instead.
val put_while0 : (char -> bool) -> string tput_while0 paccepts astringwhich respects the predicatepand emits it.
val put_while1 : (char -> bool) -> string tSame as
put_while1but the givenstringmust be not empty.
val put : (char -> bool) -> int -> string tput p naccepts astringwhich respects the predicatepand must havenbytes. Then, it emits it otherwise it fails. For example, this ABNF:DIGIT := '0' .. '9' NUMBER := 2DIGIT
can be translated to:
let number = put (function '0' .. '9' -> true | _ -> false) 2
val at_least_put : (char -> bool) -> int -> string tat_least_put p naccepts astringwhich respects the predicatepand must have, at least,nbytes. Then, it emits it otherwise it fails. For example, this ABNF:DIGIT := '0' .. '9' NUMBER := 1*DIGIT
can be translated to:
let number = at_least_put (function '0' .. '9' -> true | _ -> false) 1
val range : a:int -> b:int -> (char -> bool) -> string tval (<*) : 'a t -> unit t -> 'a tval (*>) : unit t -> 'a t -> 'a tval product : 'a t -> 'b t -> ('a * 'b) tval fail : string -> 'a tval fix : ('a t -> 'a t) -> 'a tval map : 'a t -> ('b -> 'a) -> 'b tval commit : unit tval peek : 'a t -> 'b t -> ('a, 'b) Either.t t