Module Serialk_sexp.Sexp

S-expression definitions and codec.

S-expressions

type loc = Serialk_tlex.Tloc.t

The type for text locations.

val loc_nil : loc

loc_nil is an invalid input location.

type t = [
| `A of string * loc
| `L of t list * loc
]

The type for generic s-expression representations. Either an atom or a list.

val loc : t -> loc

loc s is s's input location.

Constructors

val atom : string -> t

atom a is `A (a, loc_nil).

val list : t list -> t

list l is `L (l, loc_nil).

Accessors

val to_atom : t -> (string, string) Stdlib.result

to_atom s extracts an atom from s. If s is a list an error with the location formatted according to Tloc.pp is returned.

val to_list : t -> (t list, string) Stdlib.result

to_list s extracts a list from s. If s is an atom an error with the location formatted according to Tloc.pp is returned.

val get_atom : t -> string

get_atom s is like to_atom but raises Invalid_argument if s is not an atom.

val get_list : t -> t list

get_atom s is like to_list but raises Invalid_argument if s is not an list.

Formatting

val pp : Stdlib.Format.formatter -> t -> unit

pp formats an s-expression.

Warning. Assumes all OCaml strings in the formatted value are UTF-8 encoded.

val pp_seq : Stdlib.Format.formatter -> t -> unit

pp_seq formats an s-expression but if it is a list the outer list separators are not formatted in the output.

Warning. Assumes all OCaml strings in the formatted value are UTF-8 encoded.

Codec

val of_string : ?⁠file:Serialk_tlex.Tloc.fpath -> string -> (t, string) Stdlib.result

of_string ?file s parses a sequence of s-expressions from s. file is the file for locations, defaults to "-". The sequence is returned as a fake s-expression list that spans the whole string; note that this list does not exist syntactically in s.

Note. All OCaml strings returned by this function are UTF-8 encoded.

val to_string : t -> string

to_string s encodes s to a sequence of s-expressions. If s is an s-expression list this wrapping list is not syntactically represented in the output (see also of_string), use to_string (list [l]) if you want to output l as a list.

Warning. Assumes all OCaml strings in s are UTF-8 encoded.