Module B0.Sexp

module Sexp: sig .. end
S-expression decoder.


Source positions


type pos = int 
The type for byte positions.
type range = pos * pos 
The type for position ranges.
type src = 
| File of B0.Fpath.t (*
The type for input sources.
*)
type loc = src * range 
The type for locations.
val pp_loc : loc B0.Fmt.t
pp_loc ppf l prints an unspecified representation of l on ppf.

S-expressions


type t = [ `Atom of string | `List of t list ] * loc 
The type for s-expressions.
val get_atom : t -> string
get_atom s is the atom in s.
Raises Invalid_argument if s is not an atom.
val get_list : t -> t list
get_list s is the list in s.
Raises Invalid_argument if s is not a list.

Parsing s-expressions


val of_string : src:src -> string -> t B0.result
of_string ~src s inputs a top-level sequence of s-expressions from s and returns it as a list that spans the whole input.
val of_file : B0.Fpath.t -> t B0.result
of_file f inputs a top-level sequence of s-expressions from s and returns it as a list that spans the whole input.
val dump_locs : t B0.Fmt.t
dump_locs ppf se dumps the location of the s-expressions se on ppf.

Parsing key-value maps

WARNING. Except B0.Sexp.to_string_map all these functions and the keys raise Failure on error. Catch the exception in higher-level parsing functions on propagate them via Error (`Msg m).

type map = (t * loc) B0.String.Map.t * loc 
The type for key value maps. A string map that binds key name to their location and value tupled with the location of the map.
type 'a key = map -> 'a 
The type for key-value binding accessor keys. Given an s-expression map returns the key value.
val to_string_map : ?known:(string -> bool) -> t -> (map * map) B0.result
to_string_map ~known se is (known, unknown). se must be a list of key-value bindings (k v) where k is an atom and v an s-expression. The resulting binding for k is added to known or unknown tupled with the binding location depending on whether known s is true or not. known defaults to fun _ -> true.
val key : ?absent:'a -> (string -> t -> 'a) -> string -> 'a key
key ~absent parse name looks up key name, parse its value according parse (which should raise Failure with an error that includes the location in case of error). If the key is not in the key map and absent is returned, otherwise an error is raised.
val atom_key : ?absent:'a -> (string -> 'a) -> string -> 'a key
atom_key is like B0.Sexp.key but requires and parses an atom.
val list_key : ?empty:bool ->
?absent:'a list ->
(string -> t -> 'a) -> string -> 'a list key
list_key is like B0.Sexp.key but requires and parses a list of elements. If empty is false (defaults to true) an error is raised if the list is empty.
val atom_list_key : ?empty:bool ->
?absent:'a list -> (string -> 'a) -> string -> 'a list key
atom_list is like B0.Sexp.list_key but parses a list of atoms.

Value parsers


val parse_atom : string -> t -> string
parse_atom key se parses an atom from se assuming this is for a key named key (used for error reporting).
val parse_list : ?empty:bool -> string -> t -> t list
parse_list key se parses a list from se assuming this is for a key named key (used for error reporting). If empty is false (defaults to true) the parse errors if the list is empty.
val parse_atom_kind : (string -> 'a) -> string -> t -> 'a
parse_atom_kind parse key se is like B0.Sexp.parse_atom but uses parse to transform the atom. parse should raise Failure in case of error.
val parse_list_kind : ?empty:bool -> (string -> t -> 'a) -> string -> t -> 'a list
parse_list_kind parse key se is like B0.Sexp.parse_list but uses parse to transform the atom. parse should raise Failure in case of error.