Module Private.Sexp_helpers

Some helpers used by sexp serializers.

Originally the sexp reader in this directory were implemented using ppx meta programming via ppx_sexp_conv however we are in the process of migrating the logic to custom helpers in order to:

1. remove dependencies 2. allow customization (e.g. less parens required for variants like in dune) 3. improve error handling and reporting

This module captures some common patterns which we are gradually introducing to the code to handle to sexp handling.

module type T_of_sexp = sig ... end

Error handling

module Error_context : sig ... end

Embed context to make parsing errors more user-friendly.

Parsing utils

val parse_inline_record : (module T_of_sexp with type t = 'a) -> error_source:string -> context:Sexplib0.Sexp.t -> tag:string -> fields:Sexplib0.Sexp.t list -> 'a

When a record is embedded by a variant or polymorphic variant we'd like to support a syntax with less parens around. For example:

Suppose you have a record type M:

  module M = struct
    type t =
      { a : string
      ; b : int
      }
  end

  type t = [ `cons of M.t ]

We'd like to parse:

  cons (a hello) (b 42)

Instead of:

  cons ((a hello) (b 42))

However care must be applied for the parsing exceptions raised by use an actual sexp of the input, otherwise there would be no location. context is the sexp used to error out, and fields the record fields. M is able to parse the fields when they are wrapped by a Sexp.List constructor.

module Variant_spec : sig ... end

Helper to read variants from s-expressions. Supports nullary, unary, and variadic variants with proper error messages for each case.

val parse_variant : 'a Variant_spec.t -> error_source:string -> Sexplib0.Sexp.t -> 'a