package containers

  1. Overview
  2. Docs

Handling S-expressions

  • since 0.4
  • since 0.7

Moved the streaming parser to CCSexpStream

Basics

type t = [
  1. | `Atom of string
  2. | `List of t list
]
val equal : t -> t -> bool
val compare : t -> t -> int
val hash : t -> int
val atom : string -> t

Build an atom directly from a string

val of_int : int -> t
val of_bool : bool -> t
val of_list : t list -> t
val of_rev_list : t list -> t

Reverse the list

val of_float : float -> t

Reverse the list

val of_unit : t
val of_pair : (t * t) -> t
val of_triple : (t * t * t) -> t
val of_quad : (t * t * t * t) -> t
val of_variant : string -> t list -> t

of_variant name args is used to encode algebraic variants into a S-expr. For instance of_variant "some" [of_int 1] represents the value Some 1

val of_field : string -> t -> t

Used to represent one record field

val of_record : (string * t) list -> t

Represent a record by its named fields

Traversal of S-exp

Example: serializing 2D points

type pt = {x:int; y:int };;

let pt_of_sexp e =
  Sexp.Traverse.(
    field "x" to_int e >>= fun x ->
    field "y" to_int e >>= fun y ->
    return {x;y}
  );;

let sexp_of_pt pt = Sexp.(of_record ["x", of_int pt.x; "y", of_int pt.y]);;

let l = [{x=1;y=1}; {x=2;y=10}];;

let sexp = Sexp.(of_list (List.map sexp_of_pt l));;

Sexp.Traverse.list_all pt_of_sexp sexp;;
module Traverse : sig ... end
OCaml

Innovation. Community. Security.