jon.recoil.org

Module Core.SexpSource

Code for managing s-expressions.

include Bin_prot.Binable.S__local with type t := Core.Sexp.t

This function only needs implementation if t exposed to be a polymorphic variant. Despite what the type reads, this does *not* produce a function after reading; instead it takes the constructor tag (int) before reading and reads the rest of the variant t afterwards.

Sourceval bin_shape_t : Bin_prot.Shape.t
val globalize : Core.Sexp.t @ local -> Core.Sexp.t
Sourcemodule O : sig ... end
include Core.Comparable.S__local with type t := Core.Sexp.t and type comparator_witness = Base.Sexp.comparator_witness
val (>=) : Core.Sexp.t -> Core.Sexp.t -> bool
val (<=) : Core.Sexp.t -> Core.Sexp.t -> bool
val (=) : Core.Sexp.t -> Core.Sexp.t -> bool
val (>) : Core.Sexp.t -> Core.Sexp.t -> bool
val (<) : Core.Sexp.t -> Core.Sexp.t -> bool
val (<>) : Core.Sexp.t -> Core.Sexp.t -> bool
val equal__local : Core.Sexp.t @ local -> Core.Sexp.t @ local -> bool
val compare__local : Core.Sexp.t @ local -> Core.Sexp.t @ local -> int
val ascending : Core.Sexp.t -> Core.Sexp.t -> int
val descending : Core.Sexp.t -> Core.Sexp.t -> int
val between : Core.Sexp.t -> low:Core.Sexp.t -> high:Core.Sexp.t -> bool
val clamp_exn : Core.Sexp.t -> min:Core.Sexp.t -> max:Core.Sexp.t -> Core.Sexp.t
type comparator_witness = Base.Sexp.comparator_witness
val comparator : (Core.Sexp.t, Core.Sexp.comparator_witness) Base__Comparator.t
Sourcemodule Map : sig ... end
Sourcemodule Set : sig ... end
include Base.Stringable.S with type t := Core.Sexp.t
include module type of struct include Sexplib.Sexp end with type t := Core.Sexp.t
include Sexplib.Sexp_intf.S with type t := Core.Sexp.t
Sourceval compare : Core.Sexp.t -> Core.Sexp.t -> int @@ portable
Sourceval equal : Core.Sexp.t -> Core.Sexp.t -> bool @@ portable
Defaults
val default_indent : int Basement.Dynamic.t @@ portable

default_indent reference to default indentation level for human-readable conversions. Initialisation value: 2.

S-expression size
val size : Core.Sexp.t -> int * int @@ portable

size sexp

  • returns

    (n_atoms, n_chars), where n_atoms is the number of atoms in S-expression sexp, and n_chars is the number of characters in the atoms of the S-expression.

Scan functions

scan_sexp ?buf lexbuf scans an S-expression from lex buffer lexbuf using the optional string buffer buf for storing intermediate strings.

val scan_sexps : ?buf:Stdlib.Buffer.t -> Stdlib.Lexing.lexbuf -> Core.Sexp.t list

scan_sexps ?buf lexbuf reads a list of S-expressions from lex buffer lexbuf using the optional string buffer buf for storing intermediate strings.

val scan_rev_sexps : ?buf:Stdlib.Buffer.t -> Stdlib.Lexing.lexbuf -> Core.Sexp.t list

scan_rev_sexps ?buf lexbuf same as scan_sexps, but returns the reversed list and is slightly more efficient.

val scan_sexp_opt : ?buf:Stdlib.Buffer.t -> Stdlib.Lexing.lexbuf -> Core.Sexp.t option

scan_sexp_opt ?buf lexbuf is equivalent to scan_sexp ?buf lexbuf except that it returns None when the eof is reached.

val scan_iter_sexps : ?buf:Stdlib.Buffer.t -> f:(Core.Sexp.t -> unit) -> Stdlib.Lexing.lexbuf -> unit

scan_iter_sexps ?buf ~f lexbuf iterates over all S-expressions scanned from lex buffer lexbuf using function f, and the optional string buffer buf for storing intermediate strings.

val scan_fold_sexps : ?buf:Stdlib.Buffer.t -> f:('a -> Core.Sexp.t -> 'a) -> init:'a -> Stdlib.Lexing.lexbuf -> 'a

scan_fold_sexps ?buf ~f ~init lexbuf folds over all S-expressions scanned from lex buffer lexbuf using function f, initial state init, and the optional string buffer buf for storing intermediate strings.

val scan_sexps_conv : ?buf:Stdlib.Buffer.t -> f:(Core.Sexp.t -> 'a) -> Stdlib.Lexing.lexbuf -> 'a list

scan_sexps_conv ?buf ~f lexbuf maps all S-expressions scanned from lex buffer lexbuf to some list using function f, and the optional string buffer buf for storing intermediate strings.

Type and exception definitions for (partial) parsing
Sourcemodule Parse_pos = Sexplib.Sexp.Parse_pos
Sourcemodule Cont_state = Sexplib.Sexp.Cont_state
type ('a, 't) parse_result = ('a, 't) Sexplib.Pre_sexp.parse_result =
  1. | Done of 't * Parse_pos.t
    (*

    Done (t, parse_pos) finished parsing an S-expression. Current parse position is parse_pos.

    *)
  2. | Cont of Cont_state.t * ('a, 't) Core.Sexp.parse_fun
    (*

    Cont (cont_state, parse_fun) met the end of input before completely parsing an S-expression. The user has to call parse_fun to continue parsing the S-expression in another buffer. cont_state is the current parsing state of the continuation. NOTE: the continuation may only be called once and will raise Failure otherwise!

    *)

Type of result from calling Sexp.parse.

and ('a, 't) parse_fun = pos:int -> len:int -> 'a -> ('a, 't) Core.Sexp.parse_result

Type of parsing functions with given offsets and lengths.

Sourcemodule Annotated = Sexplib.Sexp.Annotated

Module for parsing S-expressions annotated with location information

type 't parse_state = private 't Sexplib.Pre_sexp.parse_state = {
  1. parse_pos : Parse_pos.t;
    (*

    Current parse position

    *)
}

Type of state maintained during parsing

type parse_error = Sexplib.Pre_sexp.parse_error = {
  1. err_msg : string;
    (*

    Reason why parsing failed

    *)
  2. parse_state : [ `Sexp of Core.Sexp.t list list Core.Sexp.parse_state | `Annot of Annotated.stack Core.Sexp.parse_state ];
    (*

    State of parser

    *)
}

Type of parse errors

exception Parse_error of Core.Sexp.parse_error

Exception raised during partial parsing

Unannotated (partial) parsing
val parse : ?parse_pos:Parse_pos.t -> ?len:int -> string -> (string, Core.Sexp.t) Core.Sexp.parse_result @@ portable

parse ?parse_pos ?len str (partially) parses an S-expression in string buffer str starting out with position information provided in parse_pos and reading at most len characters. To parse a single atom that is not delimited by whitespace it is necessary to call this function a second time with the returned continuation, and a dummy buffer that contains whitespace.

parse starts parsing str at position parse_pos.buf_pos. Each subsequent parse_fun from a Cont uses the buf and pos that is supplied to it. The final parse_fun that returns Done mutates the buf_pos in the originally supplied parse_pos, and then returns it.

  • parameter parse_pos

    default = Parse_pos.create ()

  • parameter len

    default = String.length str - parse_pos.Parse_pos.buf_pos

val parse_bigstring : ?parse_pos:Parse_pos.t -> ?len:int -> Core.Sexp.bigstring -> (Core.Sexp.bigstring, Core.Sexp.t) Core.Sexp.parse_result @@ portable

parse_bigstring ?parse_pos ?len str same as parse, but operates on bigstrings.

val input_sexp : ?parse_pos:Parse_pos.t -> Stdlib.in_channel -> Core.Sexp.t @@ portable

input_sexp ?parse_pos ic parses an S-expression from input channel ic using initial position information in parse_pos. NOTE: this function is not as fast on files as Sexp.load_sexp, and is also slightly slower than the scan-functions. But it is guaranteed that input_sexp is only going to read data parseable as an S-expression. Thus, subsequent input functions will see the data immediately following it.

  • parameter parse_pos

    default = Parse_pos.create ()

val input_sexps : ?parse_pos:Parse_pos.t -> ?buf:bytes -> Stdlib.in_channel -> Core.Sexp.t list @@ portable

input_sexps ?parse_pos ?buf ic parses S-expressions from input channel ic until EOF is reached. Faster than the scan-functions.

  • parameter parse_pos

    default = Parse_pos.create ()

val input_rev_sexps : ?parse_pos:Parse_pos.t -> ?buf:bytes -> Stdlib.in_channel -> Core.Sexp.t list @@ portable

input_rev_sexps ?parse_pos ?buf ic same as Sexp.input_sexps, but returns a reversed list of S-expressions, which is slightly more efficient.

Loading of (converted) S-expressions
val load_sexp : ?strict:bool -> ?buf:bytes -> string -> Core.Sexp.t @@ portable

load_sexp ?strict ?buf file reads one S-expression from file using buffer buf for storing intermediate data. Faster than the scan-functions.

  • raises Failure

    if parsing reached the end of file before one S-expression could be read.

  • raises Failure

    if strict is true and there is more than one S-expression in the file.

  • parameter strict

    default = true

val load_sexps : ?buf:bytes -> string -> Core.Sexp.t list @@ portable

load_sexps ?buf file reads a list of S-expressions from file using buffer buf for storing intermediate data. Faster than the scan-functions.

  • raises Parse_error

    if there is unparseable data in the file.

  • raises Failure

    if parsing reached the end of file before the last S-expression could be fully read.

val load_rev_sexps : ?buf:bytes -> string -> Core.Sexp.t list @@ portable

load_rev_sexps ?buf file same as Sexp.load_sexps, but returns a reversed list of S-expressions, which is slightly more efficient.

val load_sexp_conv : ?strict:bool -> ?buf:bytes -> string -> (Core.Sexp.t -> 'a) -> 'a Annotated.conv @@ portable

load_sexp_conv ?strict ?buf file f like Sexp.load_sexp, but performs a conversion on the fly using f. Performance is equivalent to executing Sexp.load_sexp and performing conversion when there are no errors. In contrast to the plain S-expression loader, this function not only performs the conversion, it will give exact error ranges for conversion errors.

  • raises Parse_error

    if there is unparseable data in the file.

  • raises Failure

    if parsing reached the end of file before the last S-expression could be fully read.

val load_sexp_conv_exn : ?strict:bool -> ?buf:bytes -> string -> (Core.Sexp.t -> 'a) -> 'a @@ portable

load_sexp_conv_exn ?strict ?buf file f like load_sexp_conv, but returns the converted value or raises Of_sexp_error with exact location information in the case of a conversion error.

val load_sexps_conv : ?buf:bytes -> string -> (Core.Sexp.t -> 'a) -> 'a Annotated.conv list @@ portable

load_sexps_conv ?buf file f like Sexp.load_sexps, but performs a conversion on the fly using f. Performance is equivalent to executing Sexp.load_sexps and performing conversion when there are no errors. In contrast to the plain S-expression loader, this function not only performs the conversion, it will give exact error ranges for conversion errors.

  • raises Parse_error

    if there is unparseable data in the file.

  • raises Failure

    if parsing reached the end of file before the last S-expression could be fully read.

val load_sexps_conv_exn : ?buf:bytes -> string -> (Core.Sexp.t -> 'a) -> 'a list @@ portable

load_sexps_conv_exn ?buf file f like load_sexps_conv, but returns the converted value or raises Of_sexp_error with exact location information in the case of a conversion error.

Output of S-expressions to I/O-channels

NOTE: for performance reasons these output functions may need to allocate large strings to write out huge S-expressions. This may cause problems on 32-bit platforms. If you think that you may need to write huge S-expressions on such platforms, you might want to use the pretty-printers that write to formatters instead (see further below).

val output_hum : Stdlib.out_channel -> Core.Sexp.t -> unit @@ portable

output_hum oc sexp outputs S-expression sexp to output channel oc in human readable form.

val output_hum_indent : int -> Stdlib.out_channel -> Core.Sexp.t -> unit @@ portable

output_hum_indent indent oc sexp outputs S-expression sexp to output channel oc in human readable form using indentation level indent.

val output_mach : Stdlib.out_channel -> Core.Sexp.t -> unit @@ portable

output_mach oc sexp outputs S-expression sexp to output channel oc in machine readable (i.e. most compact) form.

val output : Stdlib.out_channel -> Core.Sexp.t -> unit @@ portable

output oc sexp same as output_mach.

Output of S-expressions to file

All save-functions write to a temporary file before moving it into place to avoid intermittent garbling of existing files, which may cause problems for other processes that try to read.

val save_hum : ?perm:int -> string -> Core.Sexp.t -> unit @@ portable

save_hum ?perm file sexp outputs S-expression sexp to file in human readable form.

  • parameter perm

    default = umask

val save_mach : ?perm:int -> string -> Core.Sexp.t -> unit @@ portable

save_mach ?perm file sexp outputs S-expression sexp to file in machine readable (i.e. most compact) form.

  • parameter perm

    default = umask

val save : ?perm:int -> string -> Core.Sexp.t -> unit @@ portable

save ?perm file sexp same as save_mach.

val save_sexps_hum : ?perm:int -> string -> Core.Sexp.t list -> unit @@ portable

save_sexps_hum ?perm file sexps outputs S-expression list sexps to file in human readable form, each sexp being followed by a newline.

  • parameter perm

    default = umask

val save_sexps_mach : ?perm:int -> string -> Core.Sexp.t list -> unit @@ portable

save_sexps_mach ?perm file sexps outputs S-expression list sexps to file in machine readable form, each sexp being followed by a newline.

  • parameter perm

    default = umask

val save_sexps : ?perm:int -> string -> Core.Sexp.t list -> unit @@ portable

save_sexps ?perm file sexp same as save_sexps_mach.

String and bigstring conversions
Sourcemodule Of_string_conv_exn = Sexplib.Sexp.Of_string_conv_exn

Module encapsulating the exception raised by string converters when type conversions fail.

val of_string : string -> Core.Sexp.t @@ portable

of_string str parses a string str as an S-expression.

val of_string_many : string -> Core.Sexp.t list @@ portable

of_string_many parses a string containing zero or more S-expressions.

Unlike many other functions in this module, on parse failure it raises Parsexp.Parse_error rather than a native Sexplib.Sexp.Parse_error.

val of_string_conv : string -> (Core.Sexp.t -> 'a) -> 'a Annotated.conv @@ portable

of_string_conv str conv like of_string, but performs type conversion with conv.

  • returns

    conversion result.

val of_string_many_conv_exn : string -> (Core.Sexp.t -> 'a) -> 'a list @@ portable

of_string_many_conv_exn str conv like of_string_many, but performs type conversion with conv. Raises if type conversion fails.

Unlike many other functions in this module, on parse failure it raises Parsexp.Parse_error rather than a native Sexplib.Sexp.Parse_error.

It still raises Sexplib.Sexp.Of_string_conv_exn on sexp conversion errors.

  • returns

    conversion result.

val of_string_conv_exn : string -> (Core.Sexp.t -> 'a) -> 'a @@ portable

of_string_conv_exn str conv like of_string_conv, but raises Of_string_conv_exn.E if type conversion fails.

  • returns

    converted value.

val of_bigstring : Core.Sexp.bigstring -> Core.Sexp.t

of_bigstring bstr same as of_string, but operates on bigstrings.

val of_bigstring_conv : Core.Sexp.bigstring -> (Core.Sexp.t -> 'a) -> 'a Annotated.conv

of_bigstring_conv bstr conv like of_bigstring, but performs type conversion with conv.

  • returns

    conversion result.

val of_bigstring_conv_exn : Core.Sexp.bigstring -> (Core.Sexp.t -> 'a) -> 'a

of_bigstring_conv_exn bstr conv like of_bigstring_conv, but raises Of_string_conv_exn.E if type conversion fails.

  • returns

    converted value.

Pretty printing
Sourcemodule Make_pretty_printing = Sexplib.Sexp.Make_pretty_printing

Printing to formatters

Conversion to strings

Conversion to buffers

See Pretty_printing.to_string_hum, to_string_mach, and to_string, respectively.

val to_string_hum__stack : ?indent:int -> ?max_width:int -> Core.Sexp.t @ local -> string @ local @@ portable

WARNING to_string_hum__stack globalizes t if it is allocated on the stack.

val to_string_mach__stack : Core.Sexp.t @ local -> string @ local @@ portable
val to_string__stack : Core.Sexp.t @ local -> string @ local @@ portable
Utilities for automated type conversions
val unit : Core.Sexp.t @@ portable

unit the unit-value as expressed by an S-expression.

val is_unit : Core.Sexp.t -> bool @@ portable
val sexp_of_t : Core.Sexp.t -> Core.Sexp.t @@ portable

sexp_of_t sexp maps S-expressions which are part of a type with automated S-expression conversion to themselves.

val sexp_of_t__stack : Core.Sexp.t @ local -> Core.Sexp.t @ local @@ portable
val t_of_sexp : Core.Sexp.t -> Core.Sexp.t @@ portable

t_of_sexp sexp maps S-expressions which are part of a type with automated S-expression conversion to themselves.

val t_sexp_grammar : Core.Sexp.t Sexplib0.Sexp_grammar.t @@ portable
Utilities for conversion error handling
type found = [
  1. | `Found
  2. | `Pos of int * Core.Sexp.found
]

Type of successful search results. `Found means that an S-expression was found at the immediate position, and `Pos (pos, found) indicates that it was found at position pos within a structure (= S-expression list) where found describes recursively where it was found in that structure.

type search_result = [
  1. | `Not_found
  2. | Core.Sexp.found
]

Type of search results. `Not_found means that an S-expression was not found within another S-expression.

val search_physical : Core.Sexp.t -> contained:Core.Sexp.t -> Core.Sexp.search_result @@ portable

search_physical sexp ~contained

  • returns

    the search result indicating whether, and if, where the S-expression contained was found within S-expression sexp.

val subst_found : Core.Sexp.t -> subst:Core.Sexp.t -> Core.Sexp.found -> Core.Sexp.t @@ portable

subst_found sexp ~subst found

  • returns

    the S-expression that results from substituting subst within S-expression sexp at the location described by found.

Sourcemodule With_layout = Sexplib.Sexp.With_layout

S-expressions annotated with relative source positions and comments

exception Of_sexp_error of Base.Exn.t * Core.Sexp.t

A witness that Sexp.t can safely cross portability.

A witness that Sexp.t can safely cross contention.

Pretty printing

module type Pretty_printing = Base.Sexp.Pretty_printing
module type Pretty_printing_to_string = Base.Sexp.Pretty_printing_to_string
Sourcemodule Utf8 = Base.Sexp.Utf8
Sourcemodule Utf8_as_string = Base.Sexp.Utf8_as_string
include Core.Sexp.Pretty_printing_to_string

Printing to formatters

include Sexplib0.Sexp.Pretty_print_to_formatter
val pp_hum : Stdlib.Format.formatter -> Sexplib0.Sexp.t -> unit @@ portable

pp_hum ppf sexp outputs S-expression sexp to formatter ppf in human readable form.

val pp_hum_indent : int -> Stdlib.Format.formatter -> Sexplib0.Sexp.t -> unit @@ portable

pp_hum_indent n ppf sexp outputs S-expression sexp to formatter ppf in human readable form and indentation level n.

val pp_mach : Stdlib.Format.formatter -> Sexplib0.Sexp.t -> unit @@ portable

pp_mach ppf sexp outputs S-expression sexp to formatter ppf in machine readable (i.e. most compact) form.

val pp : Stdlib.Format.formatter -> Sexplib0.Sexp.t -> unit @@ portable

Same as pp_mach.

Conversion to strings

val to_string_hum : ?indent:int -> ?max_width:int -> Sexplib0.Sexp.t -> string @@ portable

to_string_hum ?indent ?max_width sexp converts S-expression sexp to a string in human readable form with indentation level indent and target maximum width max_width. Note long atoms may overflow max_width.

  • parameter indent

    default = Dynamic.get default_indent

  • parameter max_width

    default = 78

val to_string_mach : Sexplib0.Sexp.t -> string @@ portable

to_string_mach sexp converts S-expression sexp to a string in machine readable (i.e. most compact) form.

val to_string : Sexplib0.Sexp.t -> string @@ portable

Same as to_string_mach.

Conversion to buffers

val to_buffer_hum : buf:Stdlib.Buffer.t -> ?indent:int -> ?max_width:int -> Sexplib0.Sexp.t -> unit @@ portable

to_buffer_hum ~buf ?indent ?max_width sexp outputs the S-expression sexp converted to a string in human readable form to buffer buf with indentation level indent and target maximum width max_width. Note long atoms may overflow max_width.

  • parameter indent

    default = Dynamic.get default_indent

  • parameter max_width

    default = 78

val to_buffer_mach : buf:Stdlib.Buffer.t -> Sexplib0.Sexp.t -> unit @@ portable

to_buffer_mach ~buf sexp outputs the S-expression sexp converted to a string in machine readable (i.e. most compact) form to buffer buf.

val to_buffer : buf:Stdlib.Buffer.t -> Sexplib0.Sexp.t -> unit @@ portable

to_buffer ~buf sexp same as to_buffer_mach.

val to_buffer_gen : buf:'buffer -> add_char:('buffer -> char -> unit) -> add_string:('buffer -> string -> unit) -> Sexplib0.Sexp.t -> unit @@ portable

to_buffer_gen ~buf ~add_char ~add_string sexp outputs the S-expression sexp converted to a string to buffer buf using the output functions add_char and add_string.

Printing configuration

val of_float_style : [ `Underscores | `No_underscores ] Base.Dynamic.t @@ portable
val of_int_style : [ `Underscores | `No_underscores ] Base.Dynamic.t @@ portable
Sourcetype 'a no_raise = 'a

no_raise is the identity, but by using 'a no_raise in a sexpable type, the resulting use sexp_of_no_raise protects the conversion of 'a to a sexp so that if it fails, one gets a sexp with an error message about the failure, rather than an exception being raised.

WARNING: The resulting no_raise_of_sexp can still raise.

Sourcemodule Sexp_maybe : sig ... end

If sexp_of_t fails, it returns Error rather than raising. You can convert values of this type to and from sexp in processes that can or cannot parse the underlying sexp in any combination and still recover the original value. Also, the Error case contains a human-readable description of the error.

Sourcemodule With_text : sig ... end

A With_text.t is a value paired with the full textual representation of its sexp. This is useful for dealing with the case where you want to keep track of a value along with the format of the s-expression it was generated from, which allows you to maintain formatting details, comments, etc.

Sourceval of_sexp_allow_extra_fields_recursively : (Base.Sexp.t -> 'a) -> Base.Sexp.t -> 'a @@ portable

of_sexp_allow_extra_fields_recursively of_sexp sexp uses of_sexp to convert sexp to a value, but will not fail if there are any extra fields in a record (even deeply nested records).

The implementation uses global state, so it is not thread safe.

Sourcemodule Stable : sig ... end