package sexplib

  1. Overview
  2. Docs
Library for serializing OCaml values to and from S-expressions

Install

Dune Dependency

Authors

Maintainers

Sources

sexplib-v0.12.0.tar.gz
sha256=c54b11b7d30eb4d6587834f9011b37f94619b76cdc496ea22633079ced59827f
md5=a7f9f8a414aed6cc56901199cda020f6

Description

Part of Jane Street's Core library The Core suite of libraries is an industrial strength alternative to OCaml's standard library that was developed by Jane Street, the largest industrial user of OCaml.

Published: 01 Mar 2019

README

README.org

* Sexplib - S-Expressions for OCaml

=sexplib= contains functionality for parsing and pretty-printing
s-expressions.  S-expressions are defined by the following type:

#+begin_src ocaml
type sexp = Atom of string | List of sexp list
#+end_src

** Usage example

In this example we build an s-expression that corresponds to =(This
(is an) (s expression))=, serialize that into a string, and then parse
the string back into an s-expression.

#+begin_src ocaml
open Sexplib

let () =
  (* Build an Sexp from: (This (is an) (s expression)) *)
  let exp1 = Sexp.(List [
    Atom "This";
    List [Atom "is"; Atom "an"];
    List [Atom "s"; Atom "expression"]
  ]) in
  (* Serialize an Sexp object into a string *)
  print_endline (Sexp.to_string exp1);
  (* Parse a string and produce a Sexp object  *)
  let exp2 = Sexp.of_string "(This (is an) (s expression))" in
  (* Ensure we parsed what we expected. *)
  assert (Sexp.compare exp1 exp2 = 0)
#+end_src


** About

This library is often used in conjunction with =ppx_sexp_conv=, a
syntax extension which generates code from type definitions for
efficiently converting OCaml-values to s-expressions and vice versa.

Together, these two libraries make it easy to convert your OCaml
values to and from a human-readable serializable form, without the
tedium of having to write your own converters.

The library also offers functionality for extracting and replacing
sub-expressions in s-expressions.  Here, we'll only document =sexplib=
proper.  If you want to know more about the way in which OCaml types
are mapped on to s-expressions, you should look at the documentation
for [[https://github.com/janestreet/ppx_sexp_conv][=ppx_sexp_conv=]].

** Lexical conventions of s-expression

Whitespace, which consists of the space, newline, horizontal tab, and
form feed characters, is ignored unless within an OCaml-string, where
it is treated according to OCaml-conventions.  The left parenthesis
opens a new list, the right one closes it again.  Lists can be empty.

The double quote denotes the beginning and end of a string following
the lexical conventions of OCaml (see the [[http://caml.inria.fr/pub/docs/manual-ocaml/][OCaml-manual]] for
details). All characters other than double quotes, left and right
parentheses, whitespace, carriage return, and comment-introducing
characters or sequences (see next paragraph) are considered part of a
contiguous string.

** Comments

There are three kinds of comments:

- /line comments/ are introduced with =;=, and end at the newline.
- /sexp comments/ are introduced with =#;=, and end at the end of the
  following s-expression.
- /block comments/ are introduced with =#|= and end with =|#=.  These
  can be nested, and double-quotes within them must be balanced and be
  lexically correct OCaml strings.

** Grammar of s-expressions

S-expressions are either strings (= atoms) or lists.  The lists can
recursively contain further s-expressions or be empty, and must be
balanced, i.e., parentheses must match.

** Examples

#+begin_src scheme
  this_is_an_atom_123'&^%!  ; this is a comment
  "another atom in an OCaml-string \"string in a string\" \123"
  
  ; empty list follows below
  ()
  
  ; a more complex example
  (
    (
      list in a list  ; comment within a list
      (list in a list in a list)
      42 is the answer to all questions
      #; (this S-expression
           (has been commented out)
         )
      #| Block comments #| can be "nested" |# |#
    )
  )
#+end_src

** I/O and Type Conversions

There are multiple ways to perform I/O with s-expressions.  If exact
error locations are required when type conversions fail, s-expressions
need to be parsed with location annotations.  The associated parser is
slower, however, and needs more memory.

In most cases users may therefore want to use functions like
=load_sexp_conv= or =load_sexp_conv_exn=, which load s-expressions
from files and convert them.  They initially read the file without
location annotations for performance reasons.  Only if conversions
fail will the file be reparsed with location annotations.  Type errors
can then be reported accurately with file name, line number, column,
and file position.

** Custom converters

In addition to the converters provided automatically by
=ppx_sexp_conv=, it's possible to write one's own sexp-converter.  For
such converters to be available by other automatically generated
converters, it should follow the convention of being defined in the
same scope as the type, and should be named =sexp_of_[type]= and
=[type]_of_sexp=.

You must report failures by raising the =Of_sexp_error=-exception so
that then =sexplib='s tools for pinpointing the location of type
errors within an s-expression file will work properly.

Dependencies (5)

  1. num
  2. dune >= "1.5.1"
  3. sexplib0 >= "v0.12" & < "v0.13"
  4. parsexp >= "v0.12" & < "v0.13"
  5. ocaml >= "4.04.2"

Dev Dependencies

None

Used by (100)

  1. arrayjit >= "0.5.2"
  2. awa < "0.2.0"
  3. biocaml
  4. bistro
  5. bookaml >= "4.0"
  6. caldav
  7. cconv-ppx
  8. charrua < "1.6.0"
  9. charrua-server < "1.6.0"
  10. cohttp < "1.2.0"
  11. conduit < "7.0.0"
  12. conduit-async < "8.0.0"
  13. conduit-lwt < "7.0.0"
  14. conduit-mirage < "7.1.0"
  15. coq-serapi < "8.11.0+0.11.1"
  16. core < "v0.13.0"
  17. core_kernel < "v0.13.0"
  18. cstruct != "3.6.0" & < "4.0.0"
  19. cstruct-sexp
  20. csvfields < "v0.13.0"
  21. datakit-server-9p
  22. decoders-sexplib
  23. diet < "0.4"
  24. dns-forward
  25. dockerfile
  26. dockerfile-cmd >= "8.2.7"
  27. dockerfile-opam >= "8.2.7"
  28. dune-compiledb
  29. dune-deps
  30. dune-expand
  31. erlang
  32. ezjsonm < "1.2.0"
  33. ezjsonm-lwt
  34. graphql < "0.4.0"
  35. graphql_parser < "0.9.0"
  36. guile
  37. hack_parallel
  38. ipaddr < "3.1.0"
  39. jekyll-format >= "0.3.0"
  40. lazy-trie
  41. mecab
  42. mirage-conduit
  43. mirage-net-macosx
  44. nbd
  45. neural_nets_lib >= "0.5.2"
  46. nocrypto
  47. obuilder
  48. obuilder-spec
  49. ocaml-basics
  50. ocaml_plugin < "v0.13.0"
  51. odep
  52. odoc < "2.1.1"
  53. odoc-driver
  54. opam-ci-check-lint
  55. opine
  56. opium_kernel
  57. patdiff < "v0.13.0"
  58. pgocaml >= "4.2"
  59. pgocaml_ppx >= "4.2" & < "4.3.0"
  60. pgx < "1.0"
  61. ppx_cstruct >= "5.1.1"
  62. ppx_expect_nobase
  63. ppx_protocol_conv
  64. ppx_protocol_conv_json
  65. ppx_protocol_conv_jsonm
  66. ppx_protocol_conv_msgpack
  67. ppx_protocol_conv_xml_light
  68. ppx_protocol_conv_xmlm
  69. ppx_protocol_conv_yaml
  70. protocol-9p
  71. protocol-9p-unix
  72. qcow
  73. qcow-tool
  74. qcow-types
  75. rpc_parallel < "v0.13.0"
  76. safemoney >= "0.3.0"
  77. sexp < "v0.13.0"
  78. sexp_macro < "v0.13.0"
  79. sexp_pretty < "v0.13.0"
  80. shared-block-ring
  81. sihl < "0.1.0"
  82. slipshow >= "0.1.1"
  83. smtml >= "0.8.0"
  84. ssh-agent < "0.4.0"
  85. sslconf
  86. timedesc = "0.8.0"
  87. timmy
  88. tls < "0.17.0"
  89. torch < "v0.16.0"
  90. uri < "2.2.1"
  91. vchan < "6.0.2"
  92. vmnet
  93. vscoq-language-server
  94. wamp = "1.0"
  95. websocket-lwt-unix >= "2.17"
  96. x509 < "0.7.0"
  97. yaml < "3.0.0"
  98. yaml-sexp
  99. yices2_bindings
  100. zanuda

Conflicts

None

OCaml

Innovation. Community. Security.