package encore

  1. Overview
  2. Docs

Module Encore.LavoisierSource

Sourcetype state =
  1. | Partial of partial
  2. | Done
  3. | Fail

Type of lavoisier's state.

The common way to use it is to fill a Buffer.t when we get the Partial 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)
Sourceand partial = {
  1. buffer : string;
  2. off : int;
  3. len : int;
  4. continue : committed:int -> state;
}
Sourcetype -'a t

A serializer for values of type 'a.

Sourceval emit : 'a -> 'a t -> state
Sourceval emit_string : ?chunk:int -> 'a -> 'a t -> string

emit_string ?chunk v t runs t with v. The serializer allocates an internal buffer of chunk byte(s) (default to 4096 bytes) and enlarge it if it's needed.

Sourceval char : char t

char accepts any character and emit it.

Sourceval string : string -> string t

string str accepts only a string which is equal to str and emits it. Otherwise, it fails.

Sourceval pure : compare:('a -> 'a -> bool) -> 'a -> 'a t

pure ~compare v accepts only a value 'a equal (see compare) to v and emits it. Otherwise, it fails.

Sourceval choose : 'a t -> 'a t -> 'a t

choose p q runs p and emits it if succeeds. If p fails, then the output is reset and q will be run instead.

Sourceval put_while0 : (char -> bool) -> string t

put_while0 p accepts a string which respects the predicate p and emits it.

Sourceval put_while1 : (char -> bool) -> string t

Same as put_while1 but the given string must be not empty.

Sourceval put : (char -> bool) -> int -> string t

put p n accepts a string which respects the predicate p and must have n 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 
Sourceval at_least_put : (char -> bool) -> int -> string t

at_least_put p n accepts a string which respects the predicate p 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
Sourceval range : a:int -> b:int -> (char -> bool) -> string t
Sourceval (<*) : 'a t -> unit t -> 'a t
Sourceval (*>) : unit t -> 'a t -> 'a t
Sourceval product : 'a t -> 'b t -> ('a * 'b) t
Sourceval fail : string -> 'a t
Sourceval fix : ('a t -> 'a t) -> 'a t
Sourceval map : 'a t -> ('b -> 'a) -> 'b t
Sourceval commit : unit t
Sourceval peek : 'a t -> 'b t -> ('a, 'b) Either.t t
OCaml

Innovation. Community. Security.