Module Encore.Lavoisier
type state
=
|
Partial of partial
|
Done
|
Fail
Type of lavoisier's state.
The common way to use it is to fill a
Buffer
.t when we get thePartial
case: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 t
A serializer for values of type
'a
.
val emit : 'a -> 'a t -> state
val emit_string : ?chunk:int -> 'a -> 'a t -> string
emit_string ?chunk v t
runst
withv
. The serializer allocates an internal buffer ofchunk
byte(s) (default to 4096 bytes) and enlarge it if it's needed.
val char : char t
char
accepts any character and emit it.
val string : string -> string t
string str
accepts only a string which is equal tostr
and emits it. Otherwise, it fails.
val pure : compare:('a -> 'a -> bool) -> 'a -> 'a t
pure ~compare v
accepts only a value'a
equal (seecompare
) tov
and emits it. Otherwise, it fails.
val choose : 'a t -> 'a t -> 'a t
choose p q
runsp
and emits it if succeeds. Ifp
fails, then the output is reset andq
will be run instead.
val put_while0 : (char -> bool) -> string t
put_while0 p
accepts astring
which respects the predicatep
and emits it.
val put_while1 : (char -> bool) -> string t
Same as
put_while1
but the givenstring
must be not empty.
val put : (char -> bool) -> int -> string t
put p n
accepts astring
which respects the predicatep
and must haven
bytes. 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 t
at_least_put p n
accepts astring
which respects the predicatep
and must have, at least,n
bytes. 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 t
val (<*) : 'a t -> unit t -> 'a t
val (*>) : unit t -> 'a t -> 'a t
val product : 'a t -> 'b t -> ('a * 'b) t
val fail : string -> 'a t
val fix : ('a t -> 'a t) -> 'a t
val map : 'a t -> ('b -> 'a) -> 'b t
val commit : unit t
val peek : 'a t -> 'b t -> ('a, 'b) Either.t t