Module Serialk_sexp.Sexp

S-expression definitions and codec.

Meta information

type loc = Serialk_text.Tloc.t

The type for source text locations.

type a_meta

The type for meta information about atoms.

type l_meta

The type for meta information about lists.

val loc_nil : Serialk_text.Tloc.t

loc_nil is a source text location for non-parsed s-expressions.

val a_meta_nil : a_meta

a_meta_nil is parse information for non-parsed atoms.

val l_meta_nil : l_meta

l_meta_nil is parse information for non-parsed lists.

S-expressions

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

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

val atom : string -> t

atom a is `A (a, a_meta_nil).

val list : t list -> t

list l is `L (l, l_meta_nil).

Accessors

val loc : t -> loc

loc s is s's source text location.

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 get_atom : t -> string

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

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_list : t -> t list

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

val to_splice : t -> t list

to_splice s is the either the list of s if s is or or [s] if s is an atom.

Formatting

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

pp formats an s-expression.

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

pp_layout ppf l is like pp but uses layout information.

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.

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

pp_seq_layout is like pp_seq but uses layout information.

Codec

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

seq_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 from the start of the first s-expression in s to the end of the last one; note that this list does not exist syntactically in s.

If there are no s-expression in s the list is empty its location has both start and end positions at byte 0 (which may not exist).

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

val seq_to_string : t -> string

seq_to_string ~ws 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 seq_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.

S-expression indices

type index =
| Nth of int
| Key of string

The type for s-expression indexing operations.

  • Nth n, lookup zero-based element n in a list. If n is negative, counts the number of elements from the end of the list, i.e. -1 is the last list element.
  • Key k, lookup binding k in an s-expression dictionary.
val pp_index : Stdlib.Format.formatter -> index -> unit

pp_index formats indices. Keys are unbracketed.

S-expression paths

type path = index list

The type for paths, a sequence of indexing operations in reverse order.

val path_of_string : string -> (path, string) Stdlib.result

path_of_string parses a path from s according to the syntax given here.

val pp_path : Stdlib.Format.formatter -> path -> unit

pp_path is a formatter for paths.

Carets

type caret_loc =
| Before

The void before the s-expression found by the path.

| Over

The s-expression found by the path.

| After

The void after the s-expression found by the path.

The type for caret locations

type caret = caret_loc * path

The type for carets. A caret location and the path at which it applies.

val caret_of_string : string -> (caret, string) Stdlib.result

caret_of_string s parses a caret from s according to the syntax given here.

val pp_caret : Stdlib.Format.formatter -> caret -> unit

pp_caret is a formatter for carets.