1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
open StdLabels
type t = Sexplib0.Sexp.t =
| Atom of string
| List of t list
let list f l = List (List.map l ~f)
let option f o =
match o with
| None -> List []
| Some x -> List [ f x ]
;;
let int n = Atom (string_of_int n)
let int32 n = Atom (Int32.to_string n)
let int64 n = Atom (Int64.to_string n)
let float n = Atom (string_of_float n)
let string s = Atom s
let char c = Atom (String.make 1 c)
let bool b = Atom (string_of_bool b)
let pair f g (x, y) = List [ f x; g y ]
let record = list (fun (name, t) -> List [ Atom name; t ])
let cstr cstr args = List (Atom cstr :: args)
let cstr_record cstr fields =
List (Atom cstr :: List.map fields ~f:(fun (name, t) -> List [ Atom name; t ]))
;;
let cstr_list cstr f l = List (Atom cstr :: List.map l ~f)
let must_escape str =
let len = String.length str in
len = 0
||
let rec loop ix =
match str.[ix] with
| '"' | '(' | ')' | ';' | '\\' -> true
| '|' ->
ix > 0
&&
let next = ix - 1 in
str.[next] = '#' || loop next
| '#' ->
ix > 0
&&
let next = ix - 1 in
str.[next] = '|' || loop next
| '\000' .. '\032' | '\127' .. '\255' -> true
| _ -> ix > 0 && loop (ix - 1)
in
loop (len - 1)
;;
let sprintf = Printf.sprintf
let rec to_string = function
| Atom s -> if must_escape s then sprintf "%S" s else s
| List l -> sprintf "(%s)" (List.map l ~f:to_string |> String.concat ~sep:" ")
;;
let register_exn_converter a b = Sexplib0.Sexp_conv.Exn_converter.add a b
let exn = Sexplib0.Sexp_conv.sexp_of_exn