Utility Module for S-expression Conversions
Dummy definitions for "optional" options, lists, and for opaque types
default_string_of_float
reference to the default function used
to convert floats to strings.
Initially set to fun n -> sprintf "%.20G" n
.
write_old_option_format
reference for the default option format
used to write option values. If set to true
, the old-style option
format will be used, the new-style one otherwise.
Initially set to true
.
read_old_option_format
reference for the default option format
used to read option values. Of_sexp_error
will be raised
with old-style option values if this reference is set to false
.
Reading new-style option values is always supported. Using a global
reference instead of changing the converter calling conventions is
the only way to avoid breaking old code with the standard macros.
Initially set to true
.
We re-export a tail recursive map function, because some modules
override the standard library functions (e.g. StdLabels
) which
wrecks havoc with the camlp4 extension.
sexp_of_bool b
converts the value x
of type bool
to an
S-expression.
sexp_of_bool str
converts the value str
of type string
to an
S-expression.
sexp_of_char c
converts the value c
of type char
to an
S-expression.
sexp_of_float n
converts the value n
of type float
to an
S-expression.
sexp_of_int32 n
converts the value n
of type int32
to an
S-expression.
sexp_of_int64 n
converts the value n
of type int64
to an
S-expression.
sexp_of_nativeint n
converts the value n
of type nativeint
to an
S-expression.
sexp_of_ref conv r
converts the value r
of type 'a ref
to
an S-expression. Uses conv
to convert values of type 'a
to an
S-expression.
sexp_of_hashtbl conv_key conv_value htbl
converts the value htbl
of type ('a, 'b) Hashtbl.t
to an S-expression. Uses conv_key
to convert the hashtable keys of type 'a
, and conv_value
to
convert hashtable values of type 'b
to S-expressions.
sexp_of_float32_vec vec
converts the one-dimensional bigarray
vec
of 32-bit floats in Fortran-layout to an S-expression.
sexp_of_float64_vec vec
converts the one-dimensional bigarray
vec
of 64-bit floats in Fortran-layout to an S-expression.
sexp_of_float32_mat mat
converts the two-dimensional bigarray
mat
of 32-bit floats in Fortran-layout to an S-expression.
sexp_of_float64_mat mat
converts the two-dimensional bigarray
mat
of 64-bit floats in Fortran-layout to an S-expression.
sexp_of_opaque x
converts the value x
of opaque type to an
S-expression. This means the user need not provide converters,
but the result cannot be interpreted.
sexp_of_fun f
converts the value f
of function type to a
dummy S-expression. Functions cannot be serialized as S-expressions,
but at least a placeholder can be generated for pretty-printing.
string_of__of__sexp_of conv x
converts the OCaml-value x
to
an S-expression represented as a string by using conversion function
conv
.
Of_sexp_error (exn, sexp)
the exception raised when an S-expression
could not be successfully converted to an OCaml-value.
record_check_extra_fields
checks for extra (= unknown) fields
in record S-expressions.
of_sexp_error reason sexp
Of_sexp_error
(Failure reason, sexp).
of_sexp_error exc sexp
Of_sexp_error
(exc, sexp).
unit_of_sexp sexp
converts S-expression sexp
to a value of type
unit
.
bool_of_sexp sexp
converts S-expression sexp
to a value of type
bool
.
string_of_sexp sexp
converts S-expression sexp
to a value of type
string
.
char_of_sexp sexp
converts S-expression sexp
to a value of type
char
.
float_of_sexp sexp
converts S-expression sexp
to a value of type
float
.
int32_of_sexp sexp
converts S-expression sexp
to a value of type
int32
.
int64_of_sexp sexp
converts S-expression sexp
to a value of type
int64
.
nativeint_of_sexp sexp
converts S-expression sexp
to a value
of type nativeint
.
ref_of_sexp conv sexp
converts S-expression sexp
to a value
of type 'a ref
using conversion function conv
, which converts
an S-expression to a value of type 'a
.
triple_of_sexp conv1 conv2 conv3 sexp
converts S-expression sexp
to a triple of type 'a * 'b * 'c
using conversion functions conv1
,
conv2
, and conv3
, which convert S-expressions to values of type
'a
, 'b
, and 'c
respectively.
hashtbl_of_sexp conv_key conv_value sexp
converts S-expression
sexp
to a value of type ('a, 'b) Hashtbl.t
using conversion
function conv_key
, which converts an S-expression to hashtable
key of type 'a
, and function conv_value
, which converts an
S-expression to hashtable value of type 'b
.
float32_vec_of_sexp sexp
converts S-expression sexp
to a
one-dimensional bigarray of 32-bit floats in Fortran-layout.
float64_vec_of_sexp sexp
converts S-expression sexp
to a
one-dimensional bigarray of 64-bit floats in Fortran-layout.
float32_mat_of_sexp sexp
converts S-expression sexp
to a
two-dimensional bigarray of 32-bit floats in Fortran-layout.
float64_mat_of_sexp sexp
converts S-expression sexp
to a
two-dimensional bigarray of 64-bit floats in Fortran-layout.
opaque_of_sexp sexp
Of_sexp_error
when attempting to
convert an S-expression to an opaque value.
fun_of_sexp sexp
Of_sexp_error
when attempting to
convert an S-expression to a function.
of_string__of__of_sexp conv str
converts the S-expression str
represented as a string to an OCaml-value by using conversion function
conv
.
Exception converters
sexp_of_exn exc
converts exception exc
to an S-expression.
If no suitable converter is found, the standard converter in
Printexc
will be used to generate an atomic S-expression.
sexp_of_exn_opt exc
converts exception exc
to Some sexp
.
If no suitable converter is found, None
is returned instead.
Type of handles for exception S-expression converters
add_auto ?finalise templ sexp_of_exn
registers exception S-expression
converter sexp_of_exn
for exceptions having same constructor as
template templ
.
NOTE: if the exception belongs to a transient module, e.g. local modules
(including functor instantiations), first-class modules, etc., a manually
written sexp_of_exn
must use Obj.magic
internally to avoid matching
or creating the exception, otherwise the handler can never be reclaimed
once the exception ceases to exist. If finalise
is true
, then
the exception will be automatically registered for removal with the GC
(default). Finalisation will not work with exceptions that have been
allocated outside the heap, which is the case for some standard ones
e.g. Sys_error
.
NOTE: Use with great caution, this function is primarily intended for
automated use! If unsure, use add_slow
instead.
true
add_slow sexp_of_exn
registers exception S-expression converter
sexp_of_exn
and returns a handle. Exception converters registered this
way are much slower than with add
, but this function does not require
an exception template. NOTE: if you call this function explicitly,
or the "sexp"-macro for exceptions from within local modules, you will
eventually have to unregister it manually with [root:del], otherwise there
is a space leak!