aboutsummaryrefslogblamecommitdiffstats
path: root/lib/envelope.ml
blob: d8404d9a1d01bb22153ae7d8a3de1bde118784ba (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                                         
(** Envelope parser for tagged S-expressions.

    Parses commands of the form (tag payload...) where the tag selects
    which sublanguage handles the payload. The tag set is open — future
    sublanguages (tcl, ppl, acl) just add new tags. *)

type tagged = { tag : string; payload : Sexplib.Sexp.t }

let parse (input : string) : (tagged, string) Result.t =
  match Sexplib.Sexp.of_string input with
  | Sexplib.Sexp.List (Sexplib.Sexp.Atom tag :: rest) ->
    let payload = match rest with
      | [single] -> single
      | many -> Sexplib.Sexp.List many
    in
    Ok { tag; payload }
  | _ -> Error "expected (tag payload...)"
  | exception exn -> Error (Printexc.to_string exn)