···11+## Release v0.17.0
22+33+* Add a test that `Sexp.to_string` works on large input.
44+55+* Improve error messages produced by `Sexp_conv`
66+77+* Use `[@tail_mod_cons]` in `sexp_of_list`.
88+99+* Add support for labeled tuples, a compiler extension available at:
1010+ https://github.com/ocaml-flambda/flambda-backend
1111+1212+## Release v0.16.0
1313+1414+* Added `Sexp_conv_record`. Supports improvements to `ppx_sexp_conv` for deriving
1515+ `of_sexp` on record types. Provides a GADT-based generic interface to parsing record
1616+ sexps. This avoids having to generate the same field-parsing code over and over.
1717+1818+* Added `sexp_grammar_with_tags` and `sexp_grammar_with_tag_list` to `Sexp_conv_grammar`.
+67
vendor/opam/sexplib0/CONTRIBUTING.md
···11+This repository contains open source software that is developed and
22+maintained by [Jane Street][js].
33+44+Contributions to this project are welcome and should be submitted via
55+GitHub pull requests.
66+77+Signing contributions
88+---------------------
99+1010+We require that you sign your contributions. Your signature certifies
1111+that you wrote the patch or otherwise have the right to pass it on as
1212+an open-source patch. The rules are pretty simple: if you can certify
1313+the below (from [developercertificate.org][dco]):
1414+1515+```
1616+Developer Certificate of Origin
1717+Version 1.1
1818+1919+Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
2020+1 Letterman Drive
2121+Suite D4700
2222+San Francisco, CA, 94129
2323+2424+Everyone is permitted to copy and distribute verbatim copies of this
2525+license document, but changing it is not allowed.
2626+2727+2828+Developer's Certificate of Origin 1.1
2929+3030+By making a contribution to this project, I certify that:
3131+3232+(a) The contribution was created in whole or in part by me and I
3333+ have the right to submit it under the open source license
3434+ indicated in the file; or
3535+3636+(b) The contribution is based upon previous work that, to the best
3737+ of my knowledge, is covered under an appropriate open source
3838+ license and I have the right under that license to submit that
3939+ work with modifications, whether created in whole or in part
4040+ by me, under the same open source license (unless I am
4141+ permitted to submit under a different license), as indicated
4242+ in the file; or
4343+4444+(c) The contribution was provided directly to me by some other
4545+ person who certified (a), (b) or (c) and I have not modified
4646+ it.
4747+4848+(d) I understand and agree that this project and the contribution
4949+ are public and that a record of the contribution (including all
5050+ personal information I submit with it, including my sign-off) is
5151+ maintained indefinitely and may be redistributed consistent with
5252+ this project or the open source license(s) involved.
5353+```
5454+5555+Then you just add a line to every git commit message:
5656+5757+```
5858+Signed-off-by: Joe Smith <joe.smith@email.com>
5959+```
6060+6161+Use your real name (sorry, no pseudonyms or anonymous contributions.)
6262+6363+If you set your `user.name` and `user.email` git configs, you can sign
6464+your commit automatically with git commit -s.
6565+6666+[dco]: http://developercertificate.org/
6767+[js]: https://opensource.janestreet.com/
+21
vendor/opam/sexplib0/LICENSE.md
···11+The MIT License
22+33+Copyright (c) 2005--2025 Jane Street Group, LLC <opensource-contacts@janestreet.com>
44+55+Permission is hereby granted, free of charge, to any person obtaining a copy
66+of this software and associated documentation files (the "Software"), to deal
77+in the Software without restriction, including without limitation the rights
88+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99+copies of the Software, and to permit persons to whom the Software is
1010+furnished to do so, subject to the following conditions:
1111+1212+The above copyright notice and this permission notice shall be included in all
1313+copies or substantial portions of the Software.
1414+1515+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121+SOFTWARE.
···11+"Sexplib0 - a low-dep version of Sexplib"
22+=========================================
33+44+`sexplib0` is a lightweight portion of `sexplib`, for situations where a
55+dependency on `sexplib` is problematic.
66+77+It has the type definition and the printing functions, but not parsing.
88+99+See [sexplib](https://github.com/janestreet/sexplib) for documentation.
+135
vendor/opam/sexplib0/bench/bench_record.ml
···11+open Sexplib0.Sexp_conv
22+33+type 'a or_null = 'a Basement.Or_null_shim.t
44+55+let bench_t_of_sexp ~t_of_sexp string =
66+ let sexp = Sys.opaque_identity (Parsexp.Single.parse_string_exn string) in
77+ fun () -> t_of_sexp sexp
88+;;
99+1010+type t =
1111+ { a : int
1212+ ; b : int option
1313+ ; c : bool
1414+ ; d : int array
1515+ ; e : int list
1616+ ; f : int option
1717+ ; g : int
1818+ ; h : 'a. 'a list
1919+ ; i : int or_null
2020+ }
2121+2222+let t_of_sexp =
2323+ let open struct
2424+ type poly = { h : 'a. 'a list } [@@unboxed]
2525+ end in
2626+ Sexplib0.Sexp_conv_record.record_of_sexp
2727+ ~caller:"Record.t"
2828+ ~fields:
2929+ (Field
3030+ { name = "a"
3131+ ; kind = Required
3232+ ; conv =
3333+ (fun sexp ->
3434+ let value = int_of_sexp sexp in
3535+ fun () -> value)
3636+ ; rest =
3737+ Field
3838+ { name = "b"
3939+ ; kind = Omit_nil
4040+ ; conv =
4141+ (fun sexp ->
4242+ let value = option_of_sexp int_of_sexp sexp in
4343+ fun () -> value)
4444+ ; rest =
4545+ Field
4646+ { name = "c"
4747+ ; kind = Sexp_bool
4848+ ; conv = ()
4949+ ; rest =
5050+ Field
5151+ { name = "d"
5252+ ; kind = Sexp_array
5353+ ; conv = int_of_sexp
5454+ ; rest =
5555+ Field
5656+ { name = "e"
5757+ ; kind = Sexp_list
5858+ ; conv = int_of_sexp
5959+ ; rest =
6060+ Field
6161+ { name = "f"
6262+ ; kind = Sexp_option
6363+ ; conv = int_of_sexp
6464+ ; rest =
6565+ Field
6666+ { name = "g"
6767+ ; kind = Default (fun () -> 0)
6868+ ; conv =
6969+ (fun sexp ->
7070+ let value = int_of_sexp sexp in
7171+ fun () -> value)
7272+ ; rest =
7373+ Field
7474+ { name = "h"
7575+ ; kind = Required
7676+ ; conv =
7777+ (fun sexp ->
7878+ let value =
7979+ { h =
8080+ list_of_sexp
8181+ (Sexplib0.Sexp_conv_error
8282+ .record_poly_field_value
8383+ "Record.t")
8484+ sexp
8585+ }
8686+ in
8787+ fun () -> value)
8888+ ; rest =
8989+ Field
9090+ { name = "i"
9191+ ; kind = Sexp_or_null
9292+ ; conv = int_of_sexp
9393+ ; rest = Empty
9494+ }
9595+ }
9696+ }
9797+ }
9898+ }
9999+ }
100100+ }
101101+ }
102102+ })
103103+ ~index_of_field:(function
104104+ | "a" -> 0
105105+ | "b" -> 1
106106+ | "c" -> 2
107107+ | "d" -> 3
108108+ | "e" -> 4
109109+ | "f" -> 5
110110+ | "g" -> 6
111111+ | "h" -> 7
112112+ | "i" -> 8
113113+ | _ -> -1)
114114+ ~allow_extra_fields:false
115115+ ~create:(fun (a, (b, (c, (d, (e, (f, (g, (h, (i, ()))))))))) ->
116116+ let a = a () in
117117+ let b = b () in
118118+ let g = g () in
119119+ let { h } = h () in
120120+ { a; b; c; d; e; f; g; h; i })
121121+;;
122122+123123+let%bench_fun "t_of_sexp, full, in order" =
124124+ bench_t_of_sexp
125125+ ~t_of_sexp
126126+ "((a 1) (b (2)) (c) (d (3 4)) (e (5 6)) (f 7) (g 8) (h ()) (i 9))"
127127+;;
128128+129129+let%bench_fun "t_of_sexp, full, reverse order" =
130130+ bench_t_of_sexp
131131+ ~t_of_sexp
132132+ "((i 9) (h ()) (g 8) (f 7) (e (5 6)) (d (3 4)) (c) (b (2)) (a 1))"
133133+;;
134134+135135+let%bench_fun "t_of_sexp, empty" = bench_t_of_sexp ~t_of_sexp "((a 0) (h ()))"
+1
vendor/opam/sexplib0/bench/bench_record.mli
···11+(*_ This signature is deliberately empty. *)
···11+opam-version: "2.0"
22+maintainer: "Jane Street developers"
33+authors: ["Jane Street Group, LLC"]
44+homepage: "https://github.com/janestreet/sexplib0"
55+bug-reports: "https://github.com/janestreet/sexplib0/issues"
66+dev-repo: "git+https://github.com/janestreet/sexplib0.git"
77+doc: "https://ocaml.janestreet.com/ocaml-core/latest/doc/sexplib0/index.html"
88+license: "MIT"
99+build: [
1010+ ["dune" "build" "-p" name "-j" jobs]
1111+]
1212+depends: [
1313+ "ocaml" {>= "4.14.0"}
1414+ "basement"
1515+ "dune" {>= "3.17.0"}
1616+]
1717+available: arch != "arm32" & arch != "x86_32"
1818+synopsis: "Library containing the definition of S-expressions and some base converters"
1919+description: "
2020+Part of Jane Street's Core library
2121+The Core suite of libraries is an industrial strength alternative to
2222+OCaml's standard library that was developed by Jane Street, the
2323+largest industrial user of OCaml.
2424+"
···11+open Basement
22+open StdLabels
33+open Format
44+open Stdlib_stubs
55+include Sexp_intf.Definitions
66+77+let sexp_of_t t = t
88+let sexp_of_t__stack t = t
99+let t_of_sexp t = t
1010+1111+let rec compare_list a b =
1212+ match a, b with
1313+ | [], [] -> 0
1414+ | [], _ -> -1
1515+ | _, [] -> 1
1616+ | x :: xs, y :: ys ->
1717+ let res = compare x y in
1818+ if res <> 0 then res else compare_list xs ys
1919+2020+and compare a b =
2121+ if a == b
2222+ then 0
2323+ else (
2424+ match a, b with
2525+ | Atom a, Atom b -> String.compare a b
2626+ | Atom _, _ -> -1
2727+ | _, Atom _ -> 1
2828+ | List a, List b -> compare_list a b)
2929+;;
3030+3131+let rec equal a b =
3232+ a == b
3333+ ||
3434+ match a, b with
3535+ | Atom a, Atom b -> String.equal a b
3636+ | Atom _, _ | _, Atom _ -> false
3737+ | List a, List b -> List.equal ~eq:equal a b
3838+;;
3939+4040+exception Not_found_s of t
4141+exception Of_sexp_error of exn * t
4242+4343+module Printing = struct
4444+ (** Default indentation level for human-readable conversions *)
4545+ let default_indent = Dynamic.make 1
4646+4747+ let index_of_newline str start = String.index_from_opt str start '\n'
4848+4949+ (* The maximum size of a thing on the minor heap is 256 words.
5050+ Previously, this size of the returned buffer here was 4096 bytes, which
5151+ caused the Buffer to be allocated on the *major* heap every time.
5252+5353+ According to a simple benchmark by Ron, we can improve performance for
5454+ small s-expressions by a factor of ~4 if we only allocate 1024 bytes
5555+ (128 words + some small overhead) worth of buffer initially. And one
5656+ can argue that if it's free to allocate strings smaller than 256 words,
5757+ large s-expressions requiring larger expensive buffers won't notice
5858+ the extra two doublings from 1024 bytes to 2048 and 4096. And especially
5959+ performance-sensitive applications to always pass in a larger buffer to
6060+ use. *)
6161+ let buffer () = Buffer.create 1024
6262+6363+ [@@@expand_inline
6464+ [%%template
6565+ [@@@alloc.default a @ m = (stack_local, heap_global)]
6666+6767+ let to_buffer_mach_internal ~buf sexp ~mach_maybe_esc_str =
6868+ let rec loop may_need_space = function
6969+ | Atom str ->
7070+ let str' = mach_maybe_esc_str str in
7171+ let new_may_need_space = str' == str in
7272+ if may_need_space && new_may_need_space then Buffer.add_char buf ' ';
7373+ Buffer.add_string buf str';
7474+ new_may_need_space
7575+ | List (h :: t) ->
7676+ Buffer.add_char buf '(';
7777+ let may_need_space = loop false h in
7878+ loop_rest may_need_space t;
7979+ false
8080+ | List [] ->
8181+ Buffer.add_string buf "()";
8282+ false
8383+ and loop_rest may_need_space = function
8484+ | h :: t ->
8585+ let may_need_space = loop may_need_space h in
8686+ loop_rest may_need_space t
8787+ | [] -> Buffer.add_char buf ')'
8888+ in
8989+ ignore (loop false sexp)
9090+ ;;
9191+9292+ let to_string_mach_internal t ~mach_maybe_esc_str =
9393+ match t with
9494+ | Atom str -> mach_maybe_esc_str str [@exclave_if_stack a]
9595+ | sexp ->
9696+ (let buf = buffer () in
9797+ (to_buffer_mach_internal [@alloc a]) ~buf sexp ~mach_maybe_esc_str;
9898+ let len = Buffer.length buf in
9999+ let bytes = (Bytes.create [@alloc a]) len in
100100+ Buffer.blit buf 0 bytes 0 len;
101101+ Bytes.unsafe_to_string bytes)
102102+ [@exclave_if_stack a]
103103+ ;;]]
104104+105105+ include struct
106106+ let to_buffer_mach_internal__stack ~buf sexp ~mach_maybe_esc_str =
107107+ let rec loop may_need_space = function
108108+ | Atom str ->
109109+ let str' = mach_maybe_esc_str str in
110110+ let new_may_need_space = str' == str in
111111+ if may_need_space && new_may_need_space then Buffer.add_char buf ' ';
112112+ Buffer.add_string buf str';
113113+ new_may_need_space
114114+ | List (h :: t) ->
115115+ Buffer.add_char buf '(';
116116+ let may_need_space = loop false h in
117117+ loop_rest may_need_space t;
118118+ false
119119+ | List [] ->
120120+ Buffer.add_string buf "()";
121121+ false
122122+ and loop_rest may_need_space = function
123123+ | h :: t ->
124124+ let may_need_space = loop may_need_space h in
125125+ loop_rest may_need_space t
126126+ | [] -> Buffer.add_char buf ')'
127127+ in
128128+ ignore (loop false sexp)
129129+ ;;
130130+131131+ let to_string_mach_internal__stack t ~mach_maybe_esc_str =
132132+ match t with
133133+ | Atom str -> mach_maybe_esc_str str
134134+ | sexp ->
135135+ let buf = buffer () in
136136+ to_buffer_mach_internal__stack ~buf sexp ~mach_maybe_esc_str;
137137+ let len = Buffer.length buf in
138138+ let bytes = Bytes.create__stack len in
139139+ Buffer.blit buf 0 bytes 0 len;
140140+ Bytes.unsafe_to_string bytes
141141+ ;;
142142+ end [@@ocaml.doc " @inline "]
143143+144144+ include struct
145145+ let to_buffer_mach_internal ~buf sexp ~mach_maybe_esc_str =
146146+ let rec loop may_need_space = function
147147+ | Atom str ->
148148+ let str' = mach_maybe_esc_str str in
149149+ let new_may_need_space = str' == str in
150150+ if may_need_space && new_may_need_space then Buffer.add_char buf ' ';
151151+ Buffer.add_string buf str';
152152+ new_may_need_space
153153+ | List (h :: t) ->
154154+ Buffer.add_char buf '(';
155155+ let may_need_space = loop false h in
156156+ loop_rest may_need_space t;
157157+ false
158158+ | List [] ->
159159+ Buffer.add_string buf "()";
160160+ false
161161+ and loop_rest may_need_space = function
162162+ | h :: t ->
163163+ let may_need_space = loop may_need_space h in
164164+ loop_rest may_need_space t
165165+ | [] -> Buffer.add_char buf ')'
166166+ in
167167+ ignore (loop false sexp)
168168+ ;;
169169+170170+ let to_string_mach_internal t ~mach_maybe_esc_str =
171171+ match t with
172172+ | Atom str -> mach_maybe_esc_str str
173173+ | sexp ->
174174+ let buf = buffer () in
175175+ to_buffer_mach_internal ~buf sexp ~mach_maybe_esc_str;
176176+ let len = Buffer.length buf in
177177+ let bytes = Bytes.create len in
178178+ Buffer.blit buf 0 bytes 0 len;
179179+ Bytes.unsafe_to_string bytes
180180+ ;;
181181+ end [@@ocaml.doc " @inline "]
182182+183183+ [@@@end]
184184+185185+ module Make_pretty_printing (Helpers : Pretty_printing_helpers) :
186186+ Pretty_printing with type output := string = struct
187187+ include Helpers
188188+189189+ let to_buffer_hum ~buf ?(indent = Dynamic.get default_indent) ?max_width sexp =
190190+ let ppf = Format.formatter_of_buffer buf in
191191+ let () =
192192+ match max_width with
193193+ | Some width -> Format.pp_set_margin ppf width
194194+ | None -> ()
195195+ in
196196+ Format.fprintf ppf "%a@?" (pp_hum_indent indent) sexp
197197+ ;;
198198+199199+ let to_buffer_mach ~buf sexp = to_buffer_mach_internal ~buf sexp ~mach_maybe_esc_str
200200+ let to_buffer = to_buffer_mach
201201+202202+ let to_buffer_gen ~buf ~add_char ~add_string sexp =
203203+ let rec loop may_need_space = function
204204+ | Atom str ->
205205+ let str' = mach_maybe_esc_str str in
206206+ let new_may_need_space = str' == str in
207207+ if may_need_space && new_may_need_space then add_char buf ' ';
208208+ add_string buf str';
209209+ new_may_need_space
210210+ | List (h :: t) ->
211211+ add_char buf '(';
212212+ let may_need_space = loop false h in
213213+ loop_rest may_need_space t;
214214+ false
215215+ | List [] ->
216216+ add_string buf "()";
217217+ false
218218+ and loop_rest may_need_space = function
219219+ | h :: t ->
220220+ let may_need_space = loop may_need_space h in
221221+ loop_rest may_need_space t
222222+ | [] -> add_char buf ')'
223223+ in
224224+ ignore (loop false sexp)
225225+ ;;
226226+227227+ (* String conversions *)
228228+229229+ let to_string_hum ?indent ?max_width = function
230230+ | Atom str
231231+ when match index_of_newline str 0 with
232232+ | None -> true
233233+ | Some _ -> false -> mach_maybe_esc_str str
234234+ | sexp ->
235235+ let buf = buffer () in
236236+ to_buffer_hum ~buf ?indent ?max_width sexp;
237237+ Buffer.contents buf
238238+ ;;
239239+240240+ let to_string_mach sexp = to_string_mach_internal sexp ~mach_maybe_esc_str
241241+ let to_string = to_string_mach
242242+243243+ module Pretty_printing_helpers_private = Helpers
244244+ end
245245+246246+ (* Escaping of strings used as atoms in S-expressions *)
247247+248248+ module Printing_helpers = struct
249249+ let must_escape str =
250250+ let len = String.length str in
251251+ len = 0
252252+ ||
253253+ let rec loop str ix =
254254+ match str.[ix] with
255255+ | '"' | '(' | ')' | ';' | '\\' -> true
256256+ | '|' ->
257257+ ix > 0
258258+ &&
259259+ let next = ix - 1 in
260260+ Char.equal str.[next] '#' || loop str next
261261+ | '#' ->
262262+ ix > 0
263263+ &&
264264+ let next = ix - 1 in
265265+ Char.equal str.[next] '|' || loop str next
266266+ | '\000' .. '\032' | '\127' .. '\255' -> true
267267+ | _ -> ix > 0 && loop str (ix - 1)
268268+ in
269269+ loop str (len - 1)
270270+ ;;
271271+272272+ let length_of_escaped_string s =
273273+ let n = ref 0 in
274274+ for i = 0 to String.length s - 1 do
275275+ n
276276+ := !n
277277+ +
278278+ match String.unsafe_get s i with
279279+ | '\"' | '\\' | '\n' | '\t' | '\r' | '\b' -> 2
280280+ | ' ' .. '~' -> 1
281281+ | _ -> 4
282282+ done;
283283+ !n
284284+ ;;
285285+286286+ let escaped_bytes s bytes =
287287+ let n = ref 0 in
288288+ n := 0;
289289+ for i = 0 to String.length s - 1 do
290290+ (match String.unsafe_get s i with
291291+ | ('\"' | '\\') as c ->
292292+ Bytes.unsafe_set bytes !n '\\';
293293+ incr n;
294294+ Bytes.unsafe_set bytes !n c
295295+ | '\n' ->
296296+ Bytes.unsafe_set bytes !n '\\';
297297+ incr n;
298298+ Bytes.unsafe_set bytes !n 'n'
299299+ | '\t' ->
300300+ Bytes.unsafe_set bytes !n '\\';
301301+ incr n;
302302+ Bytes.unsafe_set bytes !n 't'
303303+ | '\r' ->
304304+ Bytes.unsafe_set bytes !n '\\';
305305+ incr n;
306306+ Bytes.unsafe_set bytes !n 'r'
307307+ | '\b' ->
308308+ Bytes.unsafe_set bytes !n '\\';
309309+ incr n;
310310+ Bytes.unsafe_set bytes !n 'b'
311311+ | ' ' .. '~' as c -> Bytes.unsafe_set bytes !n c
312312+ | c ->
313313+ let a = Char.code c in
314314+ Bytes.unsafe_set bytes !n '\\';
315315+ incr n;
316316+ Bytes.unsafe_set bytes !n (Char.chr (48 + (a / 100)));
317317+ incr n;
318318+ Bytes.unsafe_set bytes !n (Char.chr (48 + (a / 10 mod 10)));
319319+ incr n;
320320+ Bytes.unsafe_set bytes !n (Char.chr (48 + (a mod 10))));
321321+ incr n
322322+ done
323323+ ;;
324324+325325+ [@@@expand_inline
326326+ [%%template
327327+ [@@@alloc.default a @ m = (heap_global, stack_local)]
328328+329329+ let escaped s =
330330+ (let length_of_escaped_string = length_of_escaped_string s in
331331+ if length_of_escaped_string = String.length s
332332+ then s
333333+ else (
334334+ let bytes = (Bytes.create [@alloc a]) length_of_escaped_string in
335335+ escaped_bytes s bytes;
336336+ Bytes.unsafe_to_string bytes))
337337+ [@exclave_if_stack a]
338338+ ;;
339339+340340+ let esc_str str =
341341+ (let estr = (escaped [@alloc a]) str in
342342+ let elen = String.length estr in
343343+ let res = (Bytes.create [@alloc a]) (elen + 2) in
344344+ Bytes.unsafe_blit_string ~src:estr ~src_pos:0 ~dst:res ~dst_pos:1 ~len:elen;
345345+ Bytes.unsafe_set res 0 '"';
346346+ Bytes.unsafe_set res (elen + 1) '"';
347347+ Bytes.unsafe_to_string res)
348348+ [@exclave_if_stack a]
349349+ ;;
350350+351351+ let mach_maybe_esc_str str =
352352+ (if must_escape str then (esc_str [@alloc a]) str else str) [@exclave_if_stack a]
353353+ ;;
354354+355355+ let to_string_mach sexp =
356356+ (to_string_mach_internal [@alloc a])
357357+ sexp
358358+ ~mach_maybe_esc_str:(mach_maybe_esc_str [@alloc a]) [@exclave_if_stack a]
359359+ ;;
360360+361361+ let to_string = (to_string_mach [@alloc a])]]
362362+363363+ include struct
364364+ let escaped s =
365365+ let length_of_escaped_string = length_of_escaped_string s in
366366+ if length_of_escaped_string = String.length s
367367+ then s
368368+ else (
369369+ let bytes = Bytes.create length_of_escaped_string in
370370+ escaped_bytes s bytes;
371371+ Bytes.unsafe_to_string bytes)
372372+ ;;
373373+374374+ let esc_str str =
375375+ let estr = escaped str in
376376+ let elen = String.length estr in
377377+ let res = Bytes.create (elen + 2) in
378378+ Bytes.unsafe_blit_string ~src:estr ~src_pos:0 ~dst:res ~dst_pos:1 ~len:elen;
379379+ Bytes.unsafe_set res 0 '"';
380380+ Bytes.unsafe_set res (elen + 1) '"';
381381+ Bytes.unsafe_to_string res
382382+ ;;
383383+384384+ let mach_maybe_esc_str str = if must_escape str then esc_str str else str
385385+ let to_string_mach sexp = to_string_mach_internal sexp ~mach_maybe_esc_str
386386+ let to_string = to_string_mach
387387+ end [@@ocaml.doc " @inline "]
388388+389389+ include struct
390390+ let escaped__stack s =
391391+ let length_of_escaped_string = length_of_escaped_string s in
392392+ if length_of_escaped_string = String.length s
393393+ then s
394394+ else (
395395+ let bytes = Bytes.create__stack length_of_escaped_string in
396396+ escaped_bytes s bytes;
397397+ Bytes.unsafe_to_string bytes)
398398+ ;;
399399+400400+ let esc_str__stack str =
401401+ let estr = escaped__stack str in
402402+ let elen = String.length estr in
403403+ let res = Bytes.create__stack (elen + 2) in
404404+ Bytes.unsafe_blit_string ~src:estr ~src_pos:0 ~dst:res ~dst_pos:1 ~len:elen;
405405+ Bytes.unsafe_set res 0 '"';
406406+ Bytes.unsafe_set res (elen + 1) '"';
407407+ Bytes.unsafe_to_string res
408408+ ;;
409409+410410+ let mach_maybe_esc_str__stack str =
411411+ if must_escape str then esc_str__stack str else str
412412+ ;;
413413+414414+ let to_string_mach__stack sexp =
415415+ to_string_mach_internal__stack sexp ~mach_maybe_esc_str:mach_maybe_esc_str__stack
416416+ ;;
417417+418418+ let to_string__stack = to_string_mach__stack
419419+ end [@@ocaml.doc " @inline "]
420420+421421+ [@@@end]
422422+423423+ let get_substring str index end_pos_opt =
424424+ let end_pos =
425425+ match end_pos_opt with
426426+ | None -> String.length str
427427+ | Some end_pos -> end_pos
428428+ in
429429+ String.sub str ~pos:index ~len:(end_pos - index)
430430+ ;;
431431+432432+ let is_one_line str =
433433+ match index_of_newline str 0 with
434434+ | None -> true
435435+ | Some index -> index + 1 = String.length str
436436+ ;;
437437+438438+ let pp_hum_maybe_esc_str ppf str =
439439+ if not (must_escape str)
440440+ then pp_print_string ppf str
441441+ else if is_one_line str
442442+ then pp_print_string ppf (esc_str str)
443443+ else (
444444+ let rec loop index =
445445+ let next_newline = index_of_newline str index in
446446+ let next_line = get_substring str index next_newline in
447447+ pp_print_string ppf (escaped next_line);
448448+ match next_newline with
449449+ | None -> ()
450450+ | Some newline_index ->
451451+ pp_print_string ppf "\\";
452452+ pp_force_newline ppf ();
453453+ pp_print_string ppf "\\n";
454454+ loop (newline_index + 1)
455455+ in
456456+ pp_open_box ppf 0;
457457+ (* the leading space is to line up the lines *)
458458+ pp_print_string ppf " \"";
459459+ loop 0;
460460+ pp_print_string ppf "\"";
461461+ pp_close_box ppf ())
462462+ ;;
463463+464464+ (* Output of S-expressions to formatters *)
465465+466466+ let rec pp_hum_indent indent ppf = function
467467+ | Atom str -> pp_hum_maybe_esc_str ppf str
468468+ | List (h :: t) ->
469469+ pp_open_box ppf indent;
470470+ pp_print_string ppf "(";
471471+ pp_hum_indent indent ppf h;
472472+ pp_hum_rest indent ppf t
473473+ | List [] -> pp_print_string ppf "()"
474474+475475+ and pp_hum_rest indent ppf = function
476476+ | h :: t ->
477477+ pp_print_space ppf ();
478478+ pp_hum_indent indent ppf h;
479479+ pp_hum_rest indent ppf t
480480+ | [] ->
481481+ pp_print_string ppf ")";
482482+ pp_close_box ppf ()
483483+ ;;
484484+485485+ let rec pp_mach_internal may_need_space ppf = function
486486+ | Atom str ->
487487+ let str' = mach_maybe_esc_str str in
488488+ let new_may_need_space = str' == str in
489489+ if may_need_space && new_may_need_space then pp_print_string ppf " ";
490490+ pp_print_string ppf str';
491491+ new_may_need_space
492492+ | List (h :: t) ->
493493+ pp_print_string ppf "(";
494494+ let may_need_space = pp_mach_internal false ppf h in
495495+ pp_mach_rest may_need_space ppf t;
496496+ false
497497+ | List [] ->
498498+ pp_print_string ppf "()";
499499+ false
500500+501501+ and pp_mach_rest may_need_space ppf = function
502502+ | h :: t ->
503503+ let may_need_space = pp_mach_internal may_need_space ppf h in
504504+ pp_mach_rest may_need_space ppf t
505505+ | [] -> pp_print_string ppf ")"
506506+ ;;
507507+508508+ let pp_hum ppf sexp = pp_hum_indent (Dynamic.get default_indent) ppf sexp
509509+ let pp_mach ppf sexp = ignore (pp_mach_internal false ppf sexp)
510510+ let pp = pp_mach
511511+ end
512512+513513+ (* Sexp size *)
514514+515515+ let rec size_loop ((v, c) as acc) = function
516516+ | Atom str -> v + 1, c + String.length str
517517+ | List lst -> List.fold_left lst ~init:acc ~f:size_loop
518518+ ;;
519519+520520+ let size sexp = size_loop (0, 0) sexp
521521+522522+ (* Buffer conversions *)
523523+524524+ include Make_pretty_printing (Printing_helpers)
525525+ include Printing_helpers
526526+end
527527+528528+include Printing
529529+530530+let of_float_style = Dynamic.make (`No_underscores : [ `Underscores | `No_underscores ])
531531+let of_int_style = Dynamic.make (`No_underscores : [ `Underscores | `No_underscores ])
532532+533533+module Private = struct
534534+ include Printing
535535+end
536536+537537+let message name fields =
538538+ let rec conv_fields = function
539539+ | [] -> []
540540+ | (fname, fsexp) :: rest ->
541541+ (match fname with
542542+ | "" -> fsexp :: conv_fields rest
543543+ | _ -> List [ Atom fname; fsexp ] :: conv_fields rest)
544544+ in
545545+ List (Atom name :: conv_fields fields)
546546+;;
···11+(* Utility Module for S-expression Conversions *)
22+33+open StdLabels
44+open MoreLabels
55+open Basement
66+77+open Blocking_sync [@@alert
88+ "-deprecated"
99+ (* Used here since sexplib0 can't depend on Await_sync *)]
1010+1111+open Printf
1212+open Sexp
1313+1414+(* Conversion of OCaml-values to S-expressions *)
1515+1616+external globalize_float : float -> float = "caml_obj_dup"
1717+external bytes_length : bytes -> int = "%bytes_length"
1818+external create_local_bytes : int -> bytes = "caml_create_bytes"
1919+2020+external unsafe_blit_bytes
2121+ : src:bytes
2222+ -> src_pos:int
2323+ -> dst:bytes
2424+ -> dst_pos:int
2525+ -> len:int
2626+ -> unit
2727+ = "caml_blit_bytes"
2828+[@@noalloc]
2929+3030+external unsafe_bytes_to_string : bytes -> string = "%bytes_to_string"
3131+3232+let bytes_to_string_local b =
3333+ let len = bytes_length b in
3434+ let s = create_local_bytes len in
3535+ unsafe_blit_bytes ~src:b ~src_pos:0 ~dst:s ~dst_pos:0 ~len;
3636+ unsafe_bytes_to_string s
3737+;;
3838+3939+external unsafe_fill_bytes
4040+ : bytes
4141+ -> pos:int
4242+ -> len:int
4343+ -> char
4444+ -> unit
4545+ = "caml_fill_bytes"
4646+[@@noalloc]
4747+4848+let string_make_local n c =
4949+ let s = create_local_bytes n in
5050+ unsafe_fill_bytes s ~pos:0 ~len:n c;
5151+ unsafe_bytes_to_string s
5252+;;
5353+5454+external format_float : string -> float -> string = "caml_format_float"
5555+external format_int32 : string -> int32 -> string = "caml_int32_format"
5656+external format_int64 : string -> int64 -> string = "caml_int64_format"
5757+external format_nativeint : string -> nativeint -> string = "caml_nativeint_format"
5858+external lazy_force : ('a lazy_t[@local_opt]) -> ('a[@local_opt]) = "%lazy_force"
5959+external array_length : _ array -> int = "%array_length"
6060+6161+external array_safe_get
6262+ : ('a array[@local_opt])
6363+ -> int
6464+ -> ('a[@local_opt])
6565+ = "%array_safe_get"
6666+6767+let string_of_int32 n = format_int32 "%d" n
6868+let string_of_int64 n = format_int64 "%d" n
6969+let string_of_nativeint n = format_nativeint "%d" n
7070+7171+(* '%.17g' is guaranteed to be round-trippable.
7272+7373+ '%.15g' will be round-trippable and not have noise at the last digit or two for a float
7474+ which was converted from a decimal (string) with <= 15 significant digits. So it's
7575+ worth trying first to avoid things like "3.1400000000000001".
7676+7777+ See comment above [to_string_round_trippable] in {!Core.Float} for
7878+ detailed explanation and examples. *)
7979+let default_string_of_float =
8080+ Dynamic.make (fun x ->
8181+ let y = format_float "%.15G" x in
8282+ if float_of_string y = x then y else format_float "%.17G" x)
8383+;;
8484+8585+let read_old_option_format = Dynamic.make true
8686+let write_old_option_format = Dynamic.make true
8787+let list_map f l = List.map l ~f
8888+8989+let list_map__stack f lst =
9090+ let rec rev lst acc =
9191+ match lst with
9292+ | [] -> acc
9393+ | hd :: tl -> rev tl (hd :: acc)
9494+ in
9595+ let rec rev_map lst acc =
9696+ match lst with
9797+ | [] -> acc
9898+ | hd :: tl -> rev_map tl (f hd :: acc)
9999+ in
100100+ rev (rev_map lst []) []
101101+;;
102102+103103+let sexp_of_unit () = List []
104104+let sexp_of_unit__stack () = List []
105105+106106+let[@zero_alloc] sexp_of_bool = function
107107+ | false -> Atom "false"
108108+ | true -> Atom "true"
109109+;;
110110+111111+let sexp_of_bool__stack = sexp_of_bool
112112+let sexp_of_string str = Atom str
113113+let sexp_of_string__stack str = Atom str
114114+let sexp_of_bytes bytes = Atom (Bytes.to_string bytes)
115115+let sexp_of_bytes__stack bytes = Atom (bytes_to_string_local bytes)
116116+let sexp_of_int n = Atom (string_of_int n)
117117+let sexp_of_int__stack n = Atom (string_of_int n)
118118+let sexp_of_float n = Atom ((Dynamic.get default_string_of_float) n)
119119+120120+let sexp_of_float__stack n =
121121+ Atom ((Dynamic.get default_string_of_float) (globalize_float n))
122122+;;
123123+124124+let sexp_of_int32 n = Atom (Int32.to_string n)
125125+let sexp_of_int32__stack n = Atom (string_of_int32 n)
126126+let sexp_of_int64 n = Atom (Int64.to_string n)
127127+let sexp_of_int64__stack n = Atom (string_of_int64 n)
128128+let sexp_of_nativeint n = Atom (Nativeint.to_string n)
129129+let sexp_of_nativeint__stack n = Atom (string_of_nativeint n)
130130+let sexp_of_ref sexp_of__a rf = sexp_of__a !rf
131131+let sexp_of_ref__stack sexp_of__a rf = sexp_of__a !rf
132132+let sexp_of_lazy_t sexp_of__a lv = sexp_of__a (Lazy.force lv)
133133+let sexp_of_lazy_t__stack sexp_of__a lv = sexp_of__a (lazy_force lv)
134134+135135+let sexp_of_option sexp_of__a option =
136136+ let write_old_option_format = Dynamic.get write_old_option_format in
137137+ match option with
138138+ | Some x when write_old_option_format -> List [ sexp_of__a x ]
139139+ | Some x -> List [ Atom "some"; sexp_of__a x ]
140140+ | None when write_old_option_format -> List []
141141+ | None -> Atom "none"
142142+;;
143143+144144+let sexp_of_option__stack sexp_of__a option =
145145+ let write_old_option_format = Dynamic.get write_old_option_format in
146146+ match option with
147147+ | Some x when write_old_option_format -> List [ sexp_of__a x ]
148148+ | Some x -> List [ Atom "some"; sexp_of__a x ]
149149+ | None when write_old_option_format -> List []
150150+ | None -> Atom "none"
151151+;;
152152+153153+let sexp_of_or_null sexp_of__a or_null =
154154+ let write_old_option_format = Dynamic.get write_old_option_format in
155155+ match or_null with
156156+ | Or_null_shim.This x when write_old_option_format -> List [ sexp_of__a x ]
157157+ | Or_null_shim.This x -> List [ Atom "this"; sexp_of__a x ]
158158+ | Null when write_old_option_format -> List []
159159+ | Null -> Atom "null"
160160+;;
161161+162162+let sexp_of_or_null__stack sexp_of__a or_null =
163163+ let write_old_option_format = Dynamic.get write_old_option_format in
164164+ match or_null with
165165+ | Or_null_shim.This x when write_old_option_format -> List [ sexp_of__a x ]
166166+ | Or_null_shim.This x -> List [ Atom "this"; sexp_of__a x ]
167167+ | Null when write_old_option_format -> List []
168168+ | Null -> Atom "null"
169169+;;
170170+171171+let sexp_of_pair sexp_of__a sexp_of__b (a, b) = List [ sexp_of__a a; sexp_of__b b ]
172172+173173+let sexp_of_triple sexp_of__a sexp_of__b sexp_of__c (a, b, c) =
174174+ List [ sexp_of__a a; sexp_of__b b; sexp_of__c c ]
175175+;;
176176+177177+let sexp_of_list sexp_of__a lst = List (List.map lst ~f:sexp_of__a)
178178+let sexp_of_list__stack sexp_of__a lst = List (list_map__stack sexp_of__a lst)
179179+180180+let sexp_of_array sexp_of__a ar =
181181+ let lst_ref = ref [] in
182182+ for i = Array.length ar - 1 downto 0 do
183183+ lst_ref := sexp_of__a ar.(i) :: !lst_ref
184184+ done;
185185+ List !lst_ref
186186+;;
187187+188188+let sexp_of_array__stack sexp_of__a ar =
189189+ let rec loop i acc =
190190+ if i < 0 then List acc else loop (i - 1) (sexp_of__a (array_safe_get ar i) :: acc)
191191+ in
192192+ loop (array_length ar - 1) []
193193+;;
194194+195195+let sexp_of_hashtbl sexp_of_key sexp_of_val htbl =
196196+ let coll ~key:k ~data:v acc = List [ sexp_of_key k; sexp_of_val v ] :: acc in
197197+ List (Hashtbl.fold htbl ~init:[] ~f:coll)
198198+;;
199199+200200+let sexp_of_opaque _ = Atom "<opaque>"
201201+let sexp_of_fun _ = Atom "<fun>"
202202+203203+(* Exception converter registration and lookup *)
204204+205205+module Exn_converter = struct
206206+ (* Fast and automatic exception registration *)
207207+208208+ module Registration = struct
209209+ type t =
210210+ { sexp_of_exn : exn -> Sexp.t
211211+ ; (* If [printexc = true] then this sexp converter is used for Printexc.to_string *)
212212+ printexc : bool
213213+ }
214214+ [@@unsafe_allow_any_mode_crossing]
215215+ end
216216+217217+ module Exn_table = Basement.Stdlib_shim.Ephemeron.K1.MakePortable (struct
218218+ type t = extension_constructor
219219+220220+ let equal = ( == )
221221+ let hash = Obj.Extension_constructor.id
222222+ end)
223223+224224+ module type The_exn_table = sig
225225+ type key
226226+227227+ val lock : key Mutex.t
228228+ end
229229+230230+ module The_exn_table : The_exn_table =
231231+ (val let (Capsule.Key.P (type key) (key : key Capsule.Key.t)) = Capsule.create () in
232232+ let lock = Mutex.create key in
233233+ (module struct
234234+ type nonrec key = key
235235+236236+ let lock = lock
237237+ end : The_exn_table))
238238+239239+ let the_exn_table : (Registration.t Exn_table.t, The_exn_table.key) Capsule.Data.t =
240240+ Capsule.Data.create (fun () -> Exn_table.create 17)
241241+ ;;
242242+243243+ (* Ephemerons are used so that [sexp_of_exn] closure don't keep the
244244+ extension_constructor live. *)
245245+ let add ?(printexc = true) ?finalise:_ extension_constructor sexp_of_exn =
246246+ let sexp_of_exn = Portability_hacks.magic_portable__needs_base_and_core sexp_of_exn in
247247+ let extension_constructor =
248248+ Portability_hacks.Cross.Portable.(cross extension_constructor) extension_constructor
249249+ in
250250+ Mutex.with_lock The_exn_table.lock ~f:(fun password ->
251251+ Capsule.Data.iter the_exn_table ~password ~f:(fun the_exn_table ->
252252+ let extension_constructor =
253253+ Portability_hacks.Cross.Contended.(cross extension_constructor)
254254+ extension_constructor
255255+ in
256256+ Exn_table.add
257257+ the_exn_table
258258+ extension_constructor
259259+ ({ sexp_of_exn; printexc } : Registration.t)))
260260+ ;;
261261+262262+ let find_auto ~for_printexc exn =
263263+ let extension_constructor = Obj.Extension_constructor.of_val exn in
264264+ let extension_constructor =
265265+ Portability_hacks.Cross.Portable.(cross extension_constructor) extension_constructor
266266+ in
267267+ match
268268+ Mutex.with_lock The_exn_table.lock ~f:(fun password ->
269269+ Capsule.Data.extract the_exn_table ~password ~f:(fun the_exn_table ->
270270+ let extension_constructor =
271271+ Portability_hacks.Cross.Contended.(cross extension_constructor)
272272+ extension_constructor
273273+ in
274274+ { Stdlib_shim.Modes.Aliased.aliased =
275275+ (Exn_table.find_opt the_exn_table extension_constructor
276276+ : Registration.t option)
277277+ })
278278+ [@nontail])
279279+ with
280280+ | { aliased = None } -> None
281281+ | { aliased = Some { sexp_of_exn; printexc } } ->
282282+ (match for_printexc, printexc with
283283+ | false, _ | _, true -> Some (sexp_of_exn exn)
284284+ | true, false -> None)
285285+ ;;
286286+287287+ module For_unit_tests_only = struct
288288+ let size () =
289289+ Mutex.with_lock The_exn_table.lock ~f:(fun password ->
290290+ Capsule.Data.extract the_exn_table ~password ~f:(fun the_exn_table ->
291291+ (Exn_table.stats_alive the_exn_table).num_bindings))
292292+ ;;
293293+ end
294294+end
295295+296296+let sexp_of_exn_opt_for_printexc exn = Exn_converter.find_auto ~for_printexc:true exn
297297+let sexp_of_exn_opt exn = Exn_converter.find_auto ~for_printexc:false exn
298298+299299+let sexp_of_exn exn =
300300+ match sexp_of_exn_opt exn with
301301+ | None -> List [ Atom (Printexc.to_string exn) ]
302302+ | Some sexp -> sexp
303303+;;
304304+305305+let exn_to_string e = Sexp.to_string_hum (sexp_of_exn e)
306306+307307+(* {[exception Blah [@@deriving sexp]]} generates a call to the function
308308+ [Exn_converter.add] defined in this file. So we are guaranted that as soon as we
309309+ mark an exception as sexpable, this module will be linked in and this printer will be
310310+ registered, which is what we want. *)
311311+let () =
312312+ (Printexc.register_printer [@alert "-unsafe_multidomain"]) (fun exn ->
313313+ match sexp_of_exn_opt_for_printexc exn with
314314+ | None -> None
315315+ | Some sexp -> Some (Sexp.to_string_hum ~indent:2 sexp))
316316+;;
317317+318318+let printexc_prefer_sexp exn =
319319+ match sexp_of_exn_opt exn with
320320+ | None -> Printexc.to_string exn
321321+ | Some sexp -> Sexp.to_string_hum ~indent:2 sexp
322322+;;
323323+324324+(* Conversion of S-expressions to OCaml-values *)
325325+326326+exception Of_sexp_error = Sexp.Of_sexp_error
327327+328328+let record_check_extra_fields = Dynamic.make true
329329+let of_sexp_error_exn exc sexp = raise (Of_sexp_error (exc, sexp))
330330+let of_sexp_error what sexp = raise (Of_sexp_error (Failure what, sexp))
331331+332332+let unit_of_sexp sexp =
333333+ match sexp with
334334+ | List [] -> ()
335335+ | Atom _ | List _ -> of_sexp_error "unit_of_sexp: empty list needed" sexp
336336+;;
337337+338338+let bool_of_sexp sexp =
339339+ match sexp with
340340+ | Atom ("true" | "True") -> true
341341+ | Atom ("false" | "False") -> false
342342+ | Atom _ -> of_sexp_error "bool_of_sexp: unknown string" sexp
343343+ | List _ -> of_sexp_error "bool_of_sexp: atom needed" sexp
344344+;;
345345+346346+let string_of_sexp sexp =
347347+ match sexp with
348348+ | Atom str -> str
349349+ | List _ -> of_sexp_error "string_of_sexp: atom needed" sexp
350350+;;
351351+352352+let bytes_of_sexp sexp =
353353+ match sexp with
354354+ | Atom str -> Bytes.of_string str
355355+ | List _ -> of_sexp_error "bytes_of_sexp: atom needed" sexp
356356+;;
357357+358358+let char_of_sexp sexp =
359359+ match sexp with
360360+ | Atom str ->
361361+ if String.length str <> 1
362362+ then of_sexp_error "char_of_sexp: atom string must contain one character only" sexp;
363363+ str.[0]
364364+ | List _ -> of_sexp_error "char_of_sexp: atom needed" sexp
365365+;;
366366+367367+let int_of_sexp sexp =
368368+ match sexp with
369369+ | Atom str ->
370370+ (try int_of_string str with
371371+ | exc -> of_sexp_error ("int_of_sexp: " ^ exn_to_string exc) sexp)
372372+ | List _ -> of_sexp_error "int_of_sexp: atom needed" sexp
373373+;;
374374+375375+let float_of_sexp sexp =
376376+ match sexp with
377377+ | Atom str ->
378378+ (try float_of_string str with
379379+ | exc -> of_sexp_error ("float_of_sexp: " ^ exn_to_string exc) sexp)
380380+ | List _ -> of_sexp_error "float_of_sexp: atom needed" sexp
381381+;;
382382+383383+let int32_of_sexp sexp =
384384+ match sexp with
385385+ | Atom str ->
386386+ (try Int32.of_string str with
387387+ | exc -> of_sexp_error ("int32_of_sexp: " ^ exn_to_string exc) sexp)
388388+ | List _ -> of_sexp_error "int32_of_sexp: atom needed" sexp
389389+;;
390390+391391+let int64_of_sexp sexp =
392392+ match sexp with
393393+ | Atom str ->
394394+ (try Int64.of_string str with
395395+ | exc -> of_sexp_error ("int64_of_sexp: " ^ exn_to_string exc) sexp)
396396+ | List _ -> of_sexp_error "int64_of_sexp: atom needed" sexp
397397+;;
398398+399399+let nativeint_of_sexp sexp =
400400+ match sexp with
401401+ | Atom str ->
402402+ (try Nativeint.of_string str with
403403+ | exc -> of_sexp_error ("nativeint_of_sexp: " ^ exn_to_string exc) sexp)
404404+ | List _ -> of_sexp_error "nativeint_of_sexp: atom needed" sexp
405405+;;
406406+407407+let ref_of_sexp a__of_sexp sexp = ref (a__of_sexp sexp)
408408+let lazy_t_of_sexp a__of_sexp sexp = Lazy.from_val (a__of_sexp sexp)
409409+410410+let option_of_sexp a__of_sexp sexp =
411411+ if Dynamic.get read_old_option_format
412412+ then (
413413+ match sexp with
414414+ | List [] | Atom ("none" | "None") -> None
415415+ | List [ el ] | List [ Atom ("some" | "Some"); el ] -> Some (a__of_sexp el)
416416+ | List _ -> of_sexp_error "option_of_sexp: list must represent optional value" sexp
417417+ | Atom _ -> of_sexp_error "option_of_sexp: only none can be atom" sexp)
418418+ else (
419419+ match sexp with
420420+ | Atom ("none" | "None") -> None
421421+ | List [ Atom ("some" | "Some"); el ] -> Some (a__of_sexp el)
422422+ | Atom _ -> of_sexp_error "option_of_sexp: only none can be atom" sexp
423423+ | List _ -> of_sexp_error "option_of_sexp: list must be (some el)" sexp)
424424+;;
425425+426426+let or_null_of_sexp a__of_sexp sexp =
427427+ if Dynamic.get read_old_option_format
428428+ then (
429429+ match sexp with
430430+ | List [] | Atom ("null" | "Null") -> Or_null_shim.Null
431431+ | List [ el ] | List [ Atom ("this" | "This"); el ] -> This (a__of_sexp el)
432432+ | List _ -> of_sexp_error "or_null_of_sexp: list must represent or_null value" sexp
433433+ | Atom _ -> of_sexp_error "or_null_of_sexp: only null can be atom" sexp)
434434+ else (
435435+ match sexp with
436436+ | Atom ("null" | "Null") -> Or_null_shim.Null
437437+ | List [ Atom ("this" | "This"); el ] -> This (a__of_sexp el)
438438+ | Atom _ -> of_sexp_error "or_null_of_sexp: only null can be atom" sexp
439439+ | List _ -> of_sexp_error "or_null_of_sexp: list must be (this el)" sexp)
440440+;;
441441+442442+let pair_of_sexp a__of_sexp b__of_sexp sexp =
443443+ match sexp with
444444+ | List [ a_sexp; b_sexp ] ->
445445+ let a = a__of_sexp a_sexp in
446446+ let b = b__of_sexp b_sexp in
447447+ a, b
448448+ | List _ ->
449449+ of_sexp_error "pair_of_sexp: list must contain exactly two elements only" sexp
450450+ | Atom _ -> of_sexp_error "pair_of_sexp: list needed" sexp
451451+;;
452452+453453+let triple_of_sexp a__of_sexp b__of_sexp c__of_sexp sexp =
454454+ match sexp with
455455+ | List [ a_sexp; b_sexp; c_sexp ] ->
456456+ let a = a__of_sexp a_sexp in
457457+ let b = b__of_sexp b_sexp in
458458+ let c = c__of_sexp c_sexp in
459459+ a, b, c
460460+ | List _ ->
461461+ of_sexp_error "triple_of_sexp: list must contain exactly three elements only" sexp
462462+ | Atom _ -> of_sexp_error "triple_of_sexp: list needed" sexp
463463+;;
464464+465465+let list_of_sexp a__of_sexp sexp =
466466+ match sexp with
467467+ | List lst -> List.map lst ~f:a__of_sexp
468468+ | Atom _ -> of_sexp_error "list_of_sexp: list needed" sexp
469469+;;
470470+471471+let array_of_sexp a__of_sexp sexp =
472472+ match sexp with
473473+ | List [] -> [||]
474474+ | List (h :: t) ->
475475+ let len = List.length t + 1 in
476476+ let res = Array.make len (a__of_sexp h) in
477477+ let rec loop i = function
478478+ | [] -> res
479479+ | h :: t ->
480480+ res.(i) <- a__of_sexp h;
481481+ loop (i + 1) t
482482+ in
483483+ loop 1 t
484484+ | Atom _ -> of_sexp_error "array_of_sexp: list needed" sexp
485485+;;
486486+487487+let hashtbl_of_sexp key_of_sexp val_of_sexp sexp =
488488+ match sexp with
489489+ | List lst ->
490490+ let htbl = Hashtbl.create 0 in
491491+ let act = function
492492+ | List [ k_sexp; v_sexp ] ->
493493+ Hashtbl.add htbl ~key:(key_of_sexp k_sexp) ~data:(val_of_sexp v_sexp)
494494+ | List _ | Atom _ -> of_sexp_error "hashtbl_of_sexp: tuple list needed" sexp
495495+ in
496496+ List.iter lst ~f:act;
497497+ htbl
498498+ | Atom _ -> of_sexp_error "hashtbl_of_sexp: list needed" sexp
499499+;;
500500+501501+let opaque_of_sexp sexp =
502502+ of_sexp_error "opaque_of_sexp: cannot convert opaque values" sexp
503503+;;
504504+505505+let fun_of_sexp sexp = of_sexp_error "fun_of_sexp: cannot convert function values" sexp
506506+507507+(* Sexp Grammars *)
508508+509509+include Sexp_conv_grammar
510510+511511+(* Registering default exception printers *)
512512+513513+let get_flc_error name (file, line, chr) = Atom (sprintf "%s %s:%d:%d" name file line chr)
514514+515515+type handler = { h : exn -> Sexp.t } [@@unboxed] [@@unsafe_allow_any_mode_crossing]
516516+517517+let () =
518518+ List.iter
519519+ ~f:(fun (extension_constructor, handler) ->
520520+ Exn_converter.add ~printexc:false ~finalise:false extension_constructor handler.h)
521521+ [ ( [%extension_constructor Assert_failure]
522522+ , { h =
523523+ (function
524524+ | Assert_failure arg -> get_flc_error "Assert_failure" arg
525525+ | _ -> assert false)
526526+ } )
527527+ ; ( [%extension_constructor Exit]
528528+ , { h =
529529+ (function
530530+ | Exit -> Atom "Exit"
531531+ | _ -> assert false)
532532+ } )
533533+ ; ( [%extension_constructor End_of_file]
534534+ , { h =
535535+ (function
536536+ | End_of_file -> Atom "End_of_file"
537537+ | _ -> assert false)
538538+ } )
539539+ ; ( [%extension_constructor Failure]
540540+ , { h =
541541+ (function
542542+ | Failure arg -> List [ Atom "Failure"; Atom arg ]
543543+ | _ -> assert false)
544544+ } )
545545+ ; ( [%extension_constructor Not_found]
546546+ , { h =
547547+ (function
548548+ | Not_found -> Atom "Not_found"
549549+ | _ -> assert false)
550550+ } )
551551+ ; ( [%extension_constructor Invalid_argument]
552552+ , { h =
553553+ (function
554554+ | Invalid_argument arg -> List [ Atom "Invalid_argument"; Atom arg ]
555555+ | _ -> assert false)
556556+ } )
557557+ ; ( [%extension_constructor Match_failure]
558558+ , { h =
559559+ (function
560560+ | Match_failure arg -> get_flc_error "Match_failure" arg
561561+ | _ -> assert false)
562562+ } )
563563+ ; ( [%extension_constructor Not_found_s]
564564+ , { h =
565565+ (function
566566+ | Not_found_s arg -> List [ Atom "Not_found_s"; arg ]
567567+ | _ -> assert false)
568568+ } )
569569+ ; ( [%extension_constructor Sys_error]
570570+ , { h =
571571+ (function
572572+ | Sys_error arg -> List [ Atom "Sys_error"; Atom arg ]
573573+ | _ -> assert false)
574574+ } )
575575+ ; ( [%extension_constructor Arg.Help]
576576+ , { h =
577577+ (function
578578+ | Arg.Help arg -> List [ Atom "Arg.Help"; Atom arg ]
579579+ | _ -> assert false)
580580+ } )
581581+ ; ( [%extension_constructor Arg.Bad]
582582+ , { h =
583583+ (function
584584+ | Arg.Bad arg -> List [ Atom "Arg.Bad"; Atom arg ]
585585+ | _ -> assert false)
586586+ } )
587587+ ; ( [%extension_constructor Lazy.Undefined]
588588+ , { h =
589589+ (function
590590+ | Lazy.Undefined -> Atom "Lazy.Undefined"
591591+ | _ -> assert false)
592592+ } )
593593+ ; ( [%extension_constructor Parsing.Parse_error]
594594+ , { h =
595595+ (function
596596+ | Parsing.Parse_error -> Atom "Parsing.Parse_error"
597597+ | _ -> assert false)
598598+ } )
599599+ ; ( [%extension_constructor Queue.Empty]
600600+ , { h =
601601+ (function
602602+ | Queue.Empty -> Atom "Queue.Empty"
603603+ | _ -> assert false)
604604+ } )
605605+ ; ( [%extension_constructor Scanf.Scan_failure]
606606+ , { h =
607607+ (function
608608+ | Scanf.Scan_failure arg -> List [ Atom "Scanf.Scan_failure"; Atom arg ]
609609+ | _ -> assert false)
610610+ } )
611611+ ; ( [%extension_constructor Stack.Empty]
612612+ , { h =
613613+ (function
614614+ | Stack.Empty -> Atom "Stack.Empty"
615615+ | _ -> assert false)
616616+ } )
617617+ ; ( [%extension_constructor Sys.Break]
618618+ , { h =
619619+ (function
620620+ | Sys.Break -> Atom "Sys.Break"
621621+ | _ -> assert false)
622622+ } )
623623+ ]
624624+;;
625625+626626+let () =
627627+ List.iter
628628+ ~f:(fun (extension_constructor, handler) ->
629629+ Exn_converter.add ~printexc:true ~finalise:false extension_constructor handler.h)
630630+ [ ( [%extension_constructor Of_sexp_error]
631631+ , { h =
632632+ (function
633633+ | Of_sexp_error (exc, sexp) ->
634634+ List [ Atom "Sexplib.Conv.Of_sexp_error"; sexp_of_exn exc; sexp ]
635635+ | _ -> assert false)
636636+ } )
637637+ ]
638638+;;
639639+640640+external ignore : 'a. ('a[@local_opt]) -> unit = "%ignore"
641641+external ( = ) : 'a. ('a[@local_opt]) -> ('a[@local_opt]) -> bool = "%equal"
642642+643643+(* The compiler generates *catastrophically* bad code if you let it inline this function.
644644+ But with that prevented, the compiler reliably optimizes this to a load from a
645645+ statically allocated array. *)
646646+let[@zero_alloc] [@inline never] [@local never] [@specialise never] sexp_of_char_statically_allocated
647647+ = function
648648+ (*$
649649+ for i = 0 to 255 do
650650+ Printf.printf "| '\\x%02x' -> Atom \"\\x%02x\"\n" i i
651651+ done
652652+ *)
653653+ | '\x00' -> Atom "\x00"
654654+ | '\x01' -> Atom "\x01"
655655+ | '\x02' -> Atom "\x02"
656656+ | '\x03' -> Atom "\x03"
657657+ | '\x04' -> Atom "\x04"
658658+ | '\x05' -> Atom "\x05"
659659+ | '\x06' -> Atom "\x06"
660660+ | '\x07' -> Atom "\x07"
661661+ | '\x08' -> Atom "\x08"
662662+ | '\x09' -> Atom "\x09"
663663+ | '\x0a' -> Atom "\x0a"
664664+ | '\x0b' -> Atom "\x0b"
665665+ | '\x0c' -> Atom "\x0c"
666666+ | '\x0d' -> Atom "\x0d"
667667+ | '\x0e' -> Atom "\x0e"
668668+ | '\x0f' -> Atom "\x0f"
669669+ | '\x10' -> Atom "\x10"
670670+ | '\x11' -> Atom "\x11"
671671+ | '\x12' -> Atom "\x12"
672672+ | '\x13' -> Atom "\x13"
673673+ | '\x14' -> Atom "\x14"
674674+ | '\x15' -> Atom "\x15"
675675+ | '\x16' -> Atom "\x16"
676676+ | '\x17' -> Atom "\x17"
677677+ | '\x18' -> Atom "\x18"
678678+ | '\x19' -> Atom "\x19"
679679+ | '\x1a' -> Atom "\x1a"
680680+ | '\x1b' -> Atom "\x1b"
681681+ | '\x1c' -> Atom "\x1c"
682682+ | '\x1d' -> Atom "\x1d"
683683+ | '\x1e' -> Atom "\x1e"
684684+ | '\x1f' -> Atom "\x1f"
685685+ | '\x20' -> Atom "\x20"
686686+ | '\x21' -> Atom "\x21"
687687+ | '\x22' -> Atom "\x22"
688688+ | '\x23' -> Atom "\x23"
689689+ | '\x24' -> Atom "\x24"
690690+ | '\x25' -> Atom "\x25"
691691+ | '\x26' -> Atom "\x26"
692692+ | '\x27' -> Atom "\x27"
693693+ | '\x28' -> Atom "\x28"
694694+ | '\x29' -> Atom "\x29"
695695+ | '\x2a' -> Atom "\x2a"
696696+ | '\x2b' -> Atom "\x2b"
697697+ | '\x2c' -> Atom "\x2c"
698698+ | '\x2d' -> Atom "\x2d"
699699+ | '\x2e' -> Atom "\x2e"
700700+ | '\x2f' -> Atom "\x2f"
701701+ | '\x30' -> Atom "\x30"
702702+ | '\x31' -> Atom "\x31"
703703+ | '\x32' -> Atom "\x32"
704704+ | '\x33' -> Atom "\x33"
705705+ | '\x34' -> Atom "\x34"
706706+ | '\x35' -> Atom "\x35"
707707+ | '\x36' -> Atom "\x36"
708708+ | '\x37' -> Atom "\x37"
709709+ | '\x38' -> Atom "\x38"
710710+ | '\x39' -> Atom "\x39"
711711+ | '\x3a' -> Atom "\x3a"
712712+ | '\x3b' -> Atom "\x3b"
713713+ | '\x3c' -> Atom "\x3c"
714714+ | '\x3d' -> Atom "\x3d"
715715+ | '\x3e' -> Atom "\x3e"
716716+ | '\x3f' -> Atom "\x3f"
717717+ | '\x40' -> Atom "\x40"
718718+ | '\x41' -> Atom "\x41"
719719+ | '\x42' -> Atom "\x42"
720720+ | '\x43' -> Atom "\x43"
721721+ | '\x44' -> Atom "\x44"
722722+ | '\x45' -> Atom "\x45"
723723+ | '\x46' -> Atom "\x46"
724724+ | '\x47' -> Atom "\x47"
725725+ | '\x48' -> Atom "\x48"
726726+ | '\x49' -> Atom "\x49"
727727+ | '\x4a' -> Atom "\x4a"
728728+ | '\x4b' -> Atom "\x4b"
729729+ | '\x4c' -> Atom "\x4c"
730730+ | '\x4d' -> Atom "\x4d"
731731+ | '\x4e' -> Atom "\x4e"
732732+ | '\x4f' -> Atom "\x4f"
733733+ | '\x50' -> Atom "\x50"
734734+ | '\x51' -> Atom "\x51"
735735+ | '\x52' -> Atom "\x52"
736736+ | '\x53' -> Atom "\x53"
737737+ | '\x54' -> Atom "\x54"
738738+ | '\x55' -> Atom "\x55"
739739+ | '\x56' -> Atom "\x56"
740740+ | '\x57' -> Atom "\x57"
741741+ | '\x58' -> Atom "\x58"
742742+ | '\x59' -> Atom "\x59"
743743+ | '\x5a' -> Atom "\x5a"
744744+ | '\x5b' -> Atom "\x5b"
745745+ | '\x5c' -> Atom "\x5c"
746746+ | '\x5d' -> Atom "\x5d"
747747+ | '\x5e' -> Atom "\x5e"
748748+ | '\x5f' -> Atom "\x5f"
749749+ | '\x60' -> Atom "\x60"
750750+ | '\x61' -> Atom "\x61"
751751+ | '\x62' -> Atom "\x62"
752752+ | '\x63' -> Atom "\x63"
753753+ | '\x64' -> Atom "\x64"
754754+ | '\x65' -> Atom "\x65"
755755+ | '\x66' -> Atom "\x66"
756756+ | '\x67' -> Atom "\x67"
757757+ | '\x68' -> Atom "\x68"
758758+ | '\x69' -> Atom "\x69"
759759+ | '\x6a' -> Atom "\x6a"
760760+ | '\x6b' -> Atom "\x6b"
761761+ | '\x6c' -> Atom "\x6c"
762762+ | '\x6d' -> Atom "\x6d"
763763+ | '\x6e' -> Atom "\x6e"
764764+ | '\x6f' -> Atom "\x6f"
765765+ | '\x70' -> Atom "\x70"
766766+ | '\x71' -> Atom "\x71"
767767+ | '\x72' -> Atom "\x72"
768768+ | '\x73' -> Atom "\x73"
769769+ | '\x74' -> Atom "\x74"
770770+ | '\x75' -> Atom "\x75"
771771+ | '\x76' -> Atom "\x76"
772772+ | '\x77' -> Atom "\x77"
773773+ | '\x78' -> Atom "\x78"
774774+ | '\x79' -> Atom "\x79"
775775+ | '\x7a' -> Atom "\x7a"
776776+ | '\x7b' -> Atom "\x7b"
777777+ | '\x7c' -> Atom "\x7c"
778778+ | '\x7d' -> Atom "\x7d"
779779+ | '\x7e' -> Atom "\x7e"
780780+ | '\x7f' -> Atom "\x7f"
781781+ | '\x80' -> Atom "\x80"
782782+ | '\x81' -> Atom "\x81"
783783+ | '\x82' -> Atom "\x82"
784784+ | '\x83' -> Atom "\x83"
785785+ | '\x84' -> Atom "\x84"
786786+ | '\x85' -> Atom "\x85"
787787+ | '\x86' -> Atom "\x86"
788788+ | '\x87' -> Atom "\x87"
789789+ | '\x88' -> Atom "\x88"
790790+ | '\x89' -> Atom "\x89"
791791+ | '\x8a' -> Atom "\x8a"
792792+ | '\x8b' -> Atom "\x8b"
793793+ | '\x8c' -> Atom "\x8c"
794794+ | '\x8d' -> Atom "\x8d"
795795+ | '\x8e' -> Atom "\x8e"
796796+ | '\x8f' -> Atom "\x8f"
797797+ | '\x90' -> Atom "\x90"
798798+ | '\x91' -> Atom "\x91"
799799+ | '\x92' -> Atom "\x92"
800800+ | '\x93' -> Atom "\x93"
801801+ | '\x94' -> Atom "\x94"
802802+ | '\x95' -> Atom "\x95"
803803+ | '\x96' -> Atom "\x96"
804804+ | '\x97' -> Atom "\x97"
805805+ | '\x98' -> Atom "\x98"
806806+ | '\x99' -> Atom "\x99"
807807+ | '\x9a' -> Atom "\x9a"
808808+ | '\x9b' -> Atom "\x9b"
809809+ | '\x9c' -> Atom "\x9c"
810810+ | '\x9d' -> Atom "\x9d"
811811+ | '\x9e' -> Atom "\x9e"
812812+ | '\x9f' -> Atom "\x9f"
813813+ | '\xa0' -> Atom "\xa0"
814814+ | '\xa1' -> Atom "\xa1"
815815+ | '\xa2' -> Atom "\xa2"
816816+ | '\xa3' -> Atom "\xa3"
817817+ | '\xa4' -> Atom "\xa4"
818818+ | '\xa5' -> Atom "\xa5"
819819+ | '\xa6' -> Atom "\xa6"
820820+ | '\xa7' -> Atom "\xa7"
821821+ | '\xa8' -> Atom "\xa8"
822822+ | '\xa9' -> Atom "\xa9"
823823+ | '\xaa' -> Atom "\xaa"
824824+ | '\xab' -> Atom "\xab"
825825+ | '\xac' -> Atom "\xac"
826826+ | '\xad' -> Atom "\xad"
827827+ | '\xae' -> Atom "\xae"
828828+ | '\xaf' -> Atom "\xaf"
829829+ | '\xb0' -> Atom "\xb0"
830830+ | '\xb1' -> Atom "\xb1"
831831+ | '\xb2' -> Atom "\xb2"
832832+ | '\xb3' -> Atom "\xb3"
833833+ | '\xb4' -> Atom "\xb4"
834834+ | '\xb5' -> Atom "\xb5"
835835+ | '\xb6' -> Atom "\xb6"
836836+ | '\xb7' -> Atom "\xb7"
837837+ | '\xb8' -> Atom "\xb8"
838838+ | '\xb9' -> Atom "\xb9"
839839+ | '\xba' -> Atom "\xba"
840840+ | '\xbb' -> Atom "\xbb"
841841+ | '\xbc' -> Atom "\xbc"
842842+ | '\xbd' -> Atom "\xbd"
843843+ | '\xbe' -> Atom "\xbe"
844844+ | '\xbf' -> Atom "\xbf"
845845+ | '\xc0' -> Atom "\xc0"
846846+ | '\xc1' -> Atom "\xc1"
847847+ | '\xc2' -> Atom "\xc2"
848848+ | '\xc3' -> Atom "\xc3"
849849+ | '\xc4' -> Atom "\xc4"
850850+ | '\xc5' -> Atom "\xc5"
851851+ | '\xc6' -> Atom "\xc6"
852852+ | '\xc7' -> Atom "\xc7"
853853+ | '\xc8' -> Atom "\xc8"
854854+ | '\xc9' -> Atom "\xc9"
855855+ | '\xca' -> Atom "\xca"
856856+ | '\xcb' -> Atom "\xcb"
857857+ | '\xcc' -> Atom "\xcc"
858858+ | '\xcd' -> Atom "\xcd"
859859+ | '\xce' -> Atom "\xce"
860860+ | '\xcf' -> Atom "\xcf"
861861+ | '\xd0' -> Atom "\xd0"
862862+ | '\xd1' -> Atom "\xd1"
863863+ | '\xd2' -> Atom "\xd2"
864864+ | '\xd3' -> Atom "\xd3"
865865+ | '\xd4' -> Atom "\xd4"
866866+ | '\xd5' -> Atom "\xd5"
867867+ | '\xd6' -> Atom "\xd6"
868868+ | '\xd7' -> Atom "\xd7"
869869+ | '\xd8' -> Atom "\xd8"
870870+ | '\xd9' -> Atom "\xd9"
871871+ | '\xda' -> Atom "\xda"
872872+ | '\xdb' -> Atom "\xdb"
873873+ | '\xdc' -> Atom "\xdc"
874874+ | '\xdd' -> Atom "\xdd"
875875+ | '\xde' -> Atom "\xde"
876876+ | '\xdf' -> Atom "\xdf"
877877+ | '\xe0' -> Atom "\xe0"
878878+ | '\xe1' -> Atom "\xe1"
879879+ | '\xe2' -> Atom "\xe2"
880880+ | '\xe3' -> Atom "\xe3"
881881+ | '\xe4' -> Atom "\xe4"
882882+ | '\xe5' -> Atom "\xe5"
883883+ | '\xe6' -> Atom "\xe6"
884884+ | '\xe7' -> Atom "\xe7"
885885+ | '\xe8' -> Atom "\xe8"
886886+ | '\xe9' -> Atom "\xe9"
887887+ | '\xea' -> Atom "\xea"
888888+ | '\xeb' -> Atom "\xeb"
889889+ | '\xec' -> Atom "\xec"
890890+ | '\xed' -> Atom "\xed"
891891+ | '\xee' -> Atom "\xee"
892892+ | '\xef' -> Atom "\xef"
893893+ | '\xf0' -> Atom "\xf0"
894894+ | '\xf1' -> Atom "\xf1"
895895+ | '\xf2' -> Atom "\xf2"
896896+ | '\xf3' -> Atom "\xf3"
897897+ | '\xf4' -> Atom "\xf4"
898898+ | '\xf5' -> Atom "\xf5"
899899+ | '\xf6' -> Atom "\xf6"
900900+ | '\xf7' -> Atom "\xf7"
901901+ | '\xf8' -> Atom "\xf8"
902902+ | '\xf9' -> Atom "\xf9"
903903+ | '\xfa' -> Atom "\xfa"
904904+ | '\xfb' -> Atom "\xfb"
905905+ | '\xfc' -> Atom "\xfc"
906906+ | '\xfd' -> Atom "\xfd"
907907+ | '\xfe' -> Atom "\xfe"
908908+ | '\xff' -> Atom "\xff"
909909+;;
910910+911911+(*$*)
912912+913913+let[@inline always] is_valid_char (char : char) : bool = Char.code char land lnot 0xff = 0
914914+915915+let[@inline never] [@local never] [@specialise never] fallback_sexp_of_char (char : char) =
916916+ Atom ((String.make [@inlined never]) 1 char)
917917+;;
918918+919919+let[@inline always] sexp_of_char (char : char) =
920920+ if is_valid_char char
921921+ then sexp_of_char_statically_allocated char [@tail]
922922+ else fallback_sexp_of_char char [@tail]
923923+;;
924924+925925+let[@inline never] [@local never] [@specialise never] fallback_sexp_of_char__stack
926926+ (char : char)
927927+ =
928928+ Atom ((string_make_local [@inlined never]) 1 char)
929929+;;
930930+931931+let[@inline always] sexp_of_char__stack (char : char) =
932932+ if is_valid_char char
933933+ then sexp_of_char_statically_allocated char
934934+ else fallback_sexp_of_char__stack char
935935+;;
+325
vendor/opam/sexplib0/src/sexp_conv.mli
···11+(** Utility Module for S-expression Conversions *)
22+33+open Basement
44+55+(** {6 Conversion of OCaml-values to S-expressions} *)
66+77+(** [default_string_of_float] reference to the default function used to convert floats to
88+ strings.
99+1010+ Initially set to [fun n -> sprintf "%.20G" n]. *)
1111+val default_string_of_float : (float -> string) Dynamic.t
1212+1313+(** [write_old_option_format] reference for the default option format used to write option
1414+ values. If set to [true], the old-style option format will be used, the new-style one
1515+ otherwise.
1616+1717+ Initially set to [true]. *)
1818+val write_old_option_format : bool Dynamic.t
1919+2020+(** [read_old_option_format] reference for the default option format used to read option
2121+ values. [Of_sexp_error] will be raised with old-style option values if this reference
2222+ is set to [false]. Reading new-style option values is always supported. Using a global
2323+ reference instead of changing the converter calling conventions is the only way to
2424+ avoid breaking old code with the standard macros.
2525+2626+ Initially set to [true]. *)
2727+val read_old_option_format : bool Dynamic.t
2828+2929+(** We re-export a tail recursive map function, because some modules override the standard
3030+ library functions (e.g. [StdLabels]) which wrecks havoc with the camlp4 extension. *)
3131+val list_map : ('a -> 'b) -> 'a list -> 'b list
3232+3333+(** As [list_map], but operating over locally-allocated values. *)
3434+val list_map__stack : ('a -> 'b) -> 'a list -> 'b list
3535+3636+(** [sexp_of_unit ()] converts a value of type [unit] to an S-expression. *)
3737+val sexp_of_unit : unit -> Sexp.t
3838+3939+(** As [sexp_of_unit], but returning a locally-allocated sexp. *)
4040+val sexp_of_unit__stack : unit -> Sexp.t
4141+4242+(** [sexp_of_bool b] converts the value [x] of type [bool] to an S-expression. *)
4343+val sexp_of_bool : bool -> Sexp.t
4444+4545+(** As [sexp_of_bool], but returning a locally-allocated sexp. *)
4646+val sexp_of_bool__stack : bool -> Sexp.t
4747+4848+(** [sexp_of_string str] converts the value [str] of type [string] to an S-expression. *)
4949+val sexp_of_string : string -> Sexp.t
5050+5151+(** As [sexp_of_string], but returning a locally-allocated sexp. *)
5252+val sexp_of_string__stack : string -> Sexp.t
5353+5454+(** [sexp_of_bytes str] converts the value [str] of type [bytes] to an S-expression. *)
5555+val sexp_of_bytes : bytes -> Sexp.t
5656+5757+(** As [sexp_of_bytes], but returning a locally-allocated sexp. *)
5858+val sexp_of_bytes__stack : bytes -> Sexp.t
5959+6060+(** [sexp_of_char c] converts the value [c] of type [char] to an S-expression. *)
6161+val sexp_of_char : char -> Sexp.t
6262+6363+(** As [sexp_of_char], but returning a locally-allocated sexp. Currently, the sexp will
6464+ contain a one-character string which is heap-allocated. *)
6565+val sexp_of_char__stack : char -> Sexp.t
6666+6767+(** [sexp_of_int n] converts the value [n] of type [int] to an S-expression. *)
6868+val sexp_of_int : int -> Sexp.t
6969+7070+(** As [sexp_of_int], but returning a locally-allocated sexp. Currently, the sexp will
7171+ contain a formatted string which is heap-allocated. *)
7272+val sexp_of_int__stack : int -> Sexp.t
7373+7474+(** [sexp_of_float n] converts the value [n] of type [float] to an S-expression. *)
7575+val sexp_of_float : float -> Sexp.t
7676+7777+(** As [sexp_of_float], but returning a locally-allocated sexp. Currently, the float will
7878+ be copied to the heap, and the sexp will contain a formatted string which is
7979+ heap-allocated. *)
8080+val sexp_of_float__stack : float -> Sexp.t
8181+8282+(** [sexp_of_int32 n] converts the value [n] of type [int32] to an S-expression. *)
8383+val sexp_of_int32 : int32 -> Sexp.t
8484+8585+(** As [sexp_of_int32], but returning a locally-allocated sexp. Currently, the sexp will
8686+ contain a formatted string which is heap-allocated. *)
8787+val sexp_of_int32__stack : int32 -> Sexp.t
8888+8989+(** [sexp_of_int64 n] converts the value [n] of type [int64] to an S-expression. *)
9090+val sexp_of_int64 : int64 -> Sexp.t
9191+9292+(** As [sexp_of_int64], but returning a locally-allocated sexp. Currently, the sexp will
9393+ contain a formatted string which is heap-allocated. *)
9494+val sexp_of_int64__stack : int64 -> Sexp.t
9595+9696+(** [sexp_of_nativeint n] converts the value [n] of type [nativeint] to an S-expression. *)
9797+val sexp_of_nativeint : nativeint -> Sexp.t
9898+9999+(** As [sexp_of_nativeint], but returning a locally-allocated sexp. Currently, the sexp
100100+ will contain a formatted string which is heap-allocated. *)
101101+val sexp_of_nativeint__stack : nativeint -> Sexp.t
102102+103103+(** [sexp_of_ref conv r] converts the value [r] of type ['a ref] to an S-expression. Uses
104104+ [conv] to convert values of type ['a] to an S-expression. *)
105105+val sexp_of_ref : 'a. ('a -> Sexp.t) -> 'a ref -> Sexp.t
106106+107107+(** As [sexp_of_ref], but returning a locally-allocated sexp. *)
108108+val sexp_of_ref__stack : 'a. ('a -> Sexp.t) -> 'a ref -> Sexp.t
109109+110110+(** [sexp_of_lazy_t conv l] converts the value [l] of type ['a lazy_t] to an S-expression.
111111+ Uses [conv] to convert values of type ['a] to an S-expression. *)
112112+val sexp_of_lazy_t : 'a. ('a -> Sexp.t) -> 'a lazy_t -> Sexp.t
113113+114114+(** As [sexp_of_lazy_t], but returning a locally-allocated sexp. *)
115115+val sexp_of_lazy_t__stack : 'a. ('a -> Sexp.t) -> 'a lazy_t -> Sexp.t
116116+117117+(** [sexp_of_option conv opt] converts the value [opt] of type ['a option] to an
118118+ S-expression. Uses [conv] to convert values of type ['a] to an S-expression. *)
119119+val sexp_of_option : 'a. ('a -> Sexp.t) -> 'a option -> Sexp.t
120120+121121+(** As [sexp_of_option], but returning a locally-allocated sexp. *)
122122+val sexp_of_option__stack : 'a. ('a -> Sexp.t) -> 'a option -> Sexp.t
123123+124124+(** [sexp_of_or_null conv orn] converts the value [orn] of type ['a or_null] to an
125125+ S-expression. Uses [conv] to convert values of type ['a] to an S-expression. *)
126126+val sexp_of_or_null : 'a. ('a -> Sexp.t) -> 'a Or_null_shim.t -> Sexp.t
127127+128128+(** As [sexp_of_or_null], but returning a locally-allocated sexp. *)
129129+val sexp_of_or_null__stack : 'a. ('a -> Sexp.t) -> 'a Or_null_shim.t -> Sexp.t
130130+131131+(** [sexp_of_pair conv1 conv2 pair] converts a pair to an S-expression. It uses its first
132132+ argument to convert the first element of the pair, and its second argument to convert
133133+ the second element of the pair. *)
134134+val sexp_of_pair : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> 'a * 'b -> Sexp.t
135135+136136+(** [sexp_of_triple conv1 conv2 conv3 triple] converts a triple to an S-expression using
137137+ [conv1], [conv2], and [conv3] to convert its elements. *)
138138+val sexp_of_triple
139139+ : 'a 'b 'c.
140140+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> 'a * 'b * 'c -> Sexp.t
141141+142142+(** [sexp_of_list conv lst] converts the value [lst] of type ['a list] to an S-expression.
143143+ Uses [conv] to convert values of type ['a] to an S-expression. *)
144144+val sexp_of_list : 'a. ('a -> Sexp.t) -> 'a list -> Sexp.t
145145+146146+(** As [sexp_of_list], but returning a locally-allocated sexp. *)
147147+val sexp_of_list__stack : 'a. ('a -> Sexp.t) -> 'a list -> Sexp.t
148148+149149+(** [sexp_of_array conv ar] converts the value [ar] of type ['a array] to an S-expression.
150150+ Uses [conv] to convert values of type ['a] to an S-expression. *)
151151+val sexp_of_array : 'a. ('a -> Sexp.t) -> 'a array -> Sexp.t
152152+153153+(** As [sexp_of_array], but returning a locally-allocated sexp. *)
154154+val sexp_of_array__stack : 'a. ('a -> Sexp.t) -> 'a array -> Sexp.t
155155+156156+(** [sexp_of_hashtbl conv_key conv_value htbl] converts the value [htbl] of type
157157+ [('a, 'b) Hashtbl.t] to an S-expression. Uses [conv_key] to convert the hashtable keys
158158+ of type ['a], and [conv_value] to convert hashtable values of type ['b] to
159159+ S-expressions. *)
160160+val sexp_of_hashtbl
161161+ : 'a 'b.
162162+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) Hashtbl.t -> Sexp.t
163163+164164+(** [sexp_of_opaque x] converts the value [x] of opaque type to an S-expression. This
165165+ means the user need not provide converters, but the result cannot be interpreted. *)
166166+val sexp_of_opaque : 'a. 'a -> Sexp.t
167167+168168+(** [sexp_of_fun f] converts the value [f] of function type to a dummy S-expression.
169169+ Functions cannot be serialized as S-expressions, but at least a placeholder can be
170170+ generated for pretty-printing. *)
171171+val sexp_of_fun : ('a -> 'b) -> Sexp.t
172172+173173+(** {6 Conversion of S-expressions to OCaml-values} *)
174174+175175+(** [Of_sexp_error (exn, sexp)] the exception raised when an S-expression could not be
176176+ successfully converted to an OCaml-value. *)
177177+exception Of_sexp_error of exn * Sexp.t
178178+179179+(** [record_check_extra_fields] checks for extra (= unknown) fields in record
180180+ S-expressions. *)
181181+val record_check_extra_fields : bool Dynamic.t
182182+183183+(** [of_sexp_error reason sexp]
184184+ @raise Of_sexp_error (Failure reason, sexp). *)
185185+val of_sexp_error : string -> Sexp.t -> 'a
186186+187187+(** [of_sexp_error exc sexp]
188188+ @raise Of_sexp_error (exc, sexp). *)
189189+val of_sexp_error_exn : exn -> Sexp.t -> 'a
190190+191191+(** [unit_of_sexp sexp] converts S-expression [sexp] to a value of type [unit]. *)
192192+val unit_of_sexp : Sexp.t -> unit
193193+194194+(** [bool_of_sexp sexp] converts S-expression [sexp] to a value of type [bool]. *)
195195+val bool_of_sexp : Sexp.t -> bool
196196+197197+(** [string_of_sexp sexp] converts S-expression [sexp] to a value of type [string]. *)
198198+val string_of_sexp : Sexp.t -> string
199199+200200+(** [bytes_of_sexp sexp] converts S-expression [sexp] to a value of type [bytes]. *)
201201+val bytes_of_sexp : Sexp.t -> bytes
202202+203203+(** [char_of_sexp sexp] converts S-expression [sexp] to a value of type [char]. *)
204204+val char_of_sexp : Sexp.t -> char
205205+206206+(** [int_of_sexp sexp] converts S-expression [sexp] to a value of type [int]. *)
207207+val int_of_sexp : Sexp.t -> int
208208+209209+(** [float_of_sexp sexp] converts S-expression [sexp] to a value of type [float]. *)
210210+val float_of_sexp : Sexp.t -> float
211211+212212+(** [int32_of_sexp sexp] converts S-expression [sexp] to a value of type [int32]. *)
213213+val int32_of_sexp : Sexp.t -> int32
214214+215215+(** [int64_of_sexp sexp] converts S-expression [sexp] to a value of type [int64]. *)
216216+val int64_of_sexp : Sexp.t -> int64
217217+218218+(** [nativeint_of_sexp sexp] converts S-expression [sexp] to a value of type [nativeint]. *)
219219+val nativeint_of_sexp : Sexp.t -> nativeint
220220+221221+(** [ref_of_sexp conv sexp] converts S-expression [sexp] to a value of type ['a ref] using
222222+ conversion function [conv], which converts an S-expression to a value of type ['a]. *)
223223+val ref_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a ref
224224+225225+(** [lazy_t_of_sexp conv sexp] converts S-expression [sexp] to a value of type ['a lazy_t]
226226+ using conversion function [conv], which converts an S-expression to a value of type
227227+ ['a]. *)
228228+val lazy_t_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a lazy_t
229229+230230+(** [option_of_sexp conv sexp] converts S-expression [sexp] to a value of type ['a option]
231231+ using conversion function [conv], which converts an S-expression to a value of type
232232+ ['a]. *)
233233+val option_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a option
234234+235235+(** [option_of_sexp conv sexp] converts S-expression [sexp] to a value of type
236236+ ['a or_null] using conversion function [conv], which converts an S-expression to a
237237+ value of type ['a]. *)
238238+val or_null_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a Or_null_shim.t
239239+240240+(** [pair_of_sexp conv1 conv2 sexp] converts S-expression [sexp] to a pair of type
241241+ ['a * 'b] using conversion functions [conv1] and [conv2], which convert S-expressions
242242+ to values of type ['a] and ['b] respectively. *)
243243+val pair_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> 'a * 'b
244244+245245+(** [triple_of_sexp conv1 conv2 conv3 sexp] converts S-expression [sexp] to a triple of
246246+ type ['a * 'b * 'c] using conversion functions [conv1], [conv2], and [conv3], which
247247+ convert S-expressions to values of type ['a], ['b], and ['c] respectively. *)
248248+val triple_of_sexp
249249+ : 'a 'b 'c.
250250+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> 'a * 'b * 'c
251251+252252+(** [list_of_sexp conv sexp] converts S-expression [sexp] to a value of type ['a list]
253253+ using conversion function [conv], which converts an S-expression to a value of type
254254+ ['a]. *)
255255+val list_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a list
256256+257257+(** [array_of_sexp conv sexp] converts S-expression [sexp] to a value of type ['a array]
258258+ using conversion function [conv], which converts an S-expression to a value of type
259259+ ['a]. *)
260260+val array_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a array
261261+262262+(** [hashtbl_of_sexp conv_key conv_value sexp] converts S-expression [sexp] to a value of
263263+ type [('a, 'b) Hashtbl.t] using conversion function [conv_key], which converts an
264264+ S-expression to hashtable key of type ['a], and function [conv_value], which converts
265265+ an S-expression to hashtable value of type ['b]. *)
266266+val hashtbl_of_sexp
267267+ : 'a 'b.
268268+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) Hashtbl.t
269269+270270+(** [opaque_of_sexp sexp]
271271+ @raise Of_sexp_error when attempting to convert an S-expression to an opaque value. *)
272272+val opaque_of_sexp : Sexp.t -> 'a
273273+274274+(** [fun_of_sexp sexp]
275275+ @raise Of_sexp_error when attempting to convert an S-expression to a function. *)
276276+val fun_of_sexp : Sexp.t -> 'a
277277+278278+(** Sexp Grammars *)
279279+280280+include module type of struct
281281+ include Sexp_conv_grammar
282282+end
283283+284284+(** Exception converters *)
285285+286286+(** [sexp_of_exn exc] converts exception [exc] to an S-expression. If no suitable
287287+ converter is found, the standard converter in [Printexc] will be used to generate an
288288+ atomic S-expression. *)
289289+val sexp_of_exn : exn -> Sexp.t
290290+291291+(** Converts an exception to a string via sexp, falling back to [Printexc.to_string] if no
292292+ sexp conversion is registered for this exception.
293293+294294+ This is different from [Printexc.to_string] in that it additionally uses the sexp
295295+ converters registered with [~printexc:false]. Another difference is that the behavior
296296+ of [Printexc] can be overridden with [Printexc.register], but here we always try sexp
297297+ conversion first. *)
298298+val printexc_prefer_sexp : exn -> string
299299+300300+(** [sexp_of_exn_opt exc] converts exception [exc] to [Some sexp]. If no suitable
301301+ converter is found, [None] is returned instead. *)
302302+val sexp_of_exn_opt : exn -> Sexp.t option
303303+304304+module Exn_converter : sig
305305+ (** [add constructor sexp_of_exn] registers exception S-expression converter
306306+ [sexp_of_exn] for exceptions with the given [constructor].
307307+308308+ NOTE: [finalise] is ignored, and provided only for backward compatibility. *)
309309+ val add
310310+ : ?printexc:bool
311311+ -> ?finalise:bool
312312+ -> extension_constructor
313313+ -> (exn -> Sexp.t)
314314+ -> unit
315315+316316+ module For_unit_tests_only : sig
317317+ val size : unit -> int
318318+ end
319319+end
320320+321321+(**/**)
322322+323323+(*_ For the syntax extension *)
324324+external ignore : 'a. ('a[@local_opt]) -> unit = "%ignore"
325325+external ( = ) : 'a. ('a[@local_opt]) -> ('a[@local_opt]) -> bool = "%equal"
+128
vendor/opam/sexplib0/src/sexp_conv_error.ml
···11+(* Conv_error: Module for Handling Errors during Automated S-expression
22+ Conversions *)
33+44+open StdLabels
55+open Printf
66+open Sexp_conv
77+88+exception Of_sexp_error = Of_sexp_error
99+1010+let error ~loc ~sexp msg = of_sexp_error (sprintf "%s_of_sexp: %s" loc msg) sexp
1111+let simple_error msg loc sexp = error ~loc ~sexp msg
1212+1313+(* Errors concerning tuples *)
1414+1515+let tuple_of_size_n_expected loc n sexp =
1616+ error ~loc ~sexp (sprintf "tuple of size %d expected" n)
1717+;;
1818+1919+let tuple_pair_expected loc name sexp =
2020+ let msg = sprintf "%s_of_sexp: expected a pair beginning with label %s" loc name in
2121+ of_sexp_error msg sexp
2222+;;
2323+2424+let tuple_incorrect_label loc name pos sexp =
2525+ let msg =
2626+ sprintf "%s_of_sexp: incorrect label for element %s at position %i" loc name pos
2727+ in
2828+ of_sexp_error msg sexp
2929+;;
3030+3131+(* Errors concerning sum types *)
3232+3333+let stag_no_args = simple_error "this constructor does not take arguments"
3434+3535+let stag_incorrect_n_args loc tag sexp =
3636+ error ~loc ~sexp (sprintf "sum tag %S has incorrect number of arguments" tag)
3737+;;
3838+3939+let stag_takes_args = simple_error "this constructor requires arguments"
4040+let nested_list_invalid_sum = simple_error "expected a variant type, saw a nested list"
4141+let empty_list_invalid_sum = simple_error "expected a variant type, saw an empty list"
4242+4343+let unexpected_stag loc expected_cnstrs sexp =
4444+ let max_cnstrs = 10 in
4545+ let expected_cnstrs =
4646+ if List.length expected_cnstrs <= max_cnstrs
4747+ then expected_cnstrs
4848+ else List.filteri expected_cnstrs ~f:(fun i _ -> i < max_cnstrs) @ [ "..." ]
4949+ in
5050+ let expected_cnstrs_string = String.concat expected_cnstrs ~sep:" " in
5151+ error
5252+ ~loc
5353+ ~sexp
5454+ (sprintf "unexpected variant constructor; expected one of %s" expected_cnstrs_string)
5555+;;
5656+5757+(* Errors concerning records *)
5858+5959+let record_sexp_bool_with_payload =
6060+ simple_error "record conversion: a [sexp.bool] field was given a payload"
6161+;;
6262+6363+let record_only_pairs_expected =
6464+ simple_error
6565+ "record conversion: only pairs expected, their first element must be an atom"
6666+;;
6767+6868+let record_invalid_fields ~what ~loc fld_names sexp =
6969+ let fld_names_str = String.concat fld_names ~sep:" " in
7070+ error ~loc ~sexp (sprintf "%s: %s" what fld_names_str)
7171+;;
7272+7373+let record_duplicate_fields loc fld_names sexp =
7474+ record_invalid_fields ~what:"duplicate fields" ~loc fld_names sexp
7575+;;
7676+7777+let record_missing_and_extra_fields loc sexp ~missing ~extras =
7878+ match missing, extras with
7979+ | [], [] -> assert false
8080+ | _ :: _, [] -> record_invalid_fields ~what:"missing fields" ~loc missing sexp
8181+ | [], _ :: _ -> record_invalid_fields ~what:"extra fields" ~loc extras sexp
8282+ | _ :: _, _ :: _ ->
8383+ let missing_fields = String.concat ~sep:" " missing in
8484+ let extra_fields = String.concat ~sep:" " extras in
8585+ error
8686+ ~loc
8787+ ~sexp
8888+ (sprintf
8989+ "extra fields found while some fields missing; extra fields: %s; missing \
9090+ fields: %s"
9191+ extra_fields
9292+ missing_fields)
9393+;;
9494+9595+let record_list_instead_atom = simple_error "list expected for record, found atom instead"
9696+9797+let record_poly_field_value =
9898+ simple_error "cannot convert values of types resulting from polymorphic record fields"
9999+;;
100100+101101+(* Errors concerning polymorphic variants *)
102102+103103+exception No_variant_match
104104+105105+let no_variant_match () = raise No_variant_match
106106+let no_matching_variant_found = simple_error "no matching variant found"
107107+let ptag_no_args = simple_error "polymorphic variant does not take arguments"
108108+109109+let ptag_incorrect_n_args loc cnstr sexp =
110110+ error
111111+ ~loc
112112+ ~sexp
113113+ (sprintf "polymorphic variant tag %S has incorrect number of arguments" cnstr)
114114+;;
115115+116116+let ptag_takes_args = simple_error "polymorphic variant tag takes an argument"
117117+118118+let nested_list_invalid_poly_var =
119119+ simple_error "a nested list is an invalid polymorphic variant"
120120+;;
121121+122122+let empty_list_invalid_poly_var =
123123+ simple_error "the empty list is an invalid polymorphic variant"
124124+;;
125125+126126+let empty_type = simple_error "trying to convert an empty type"
127127+128128+type nothing = |
···11+module Fields = struct
22+ type _ t =
33+ | Field :
44+ 'a 'b.
55+ { name : string
66+ ; conv : Sexp.t -> unit -> 'a
77+ ; rest : 'b t
88+ }
99+ -> ((unit -> 'a) * 'b) t
1010+ | Empty : unit t
1111+1212+ let rec length_loop : type a. a t -> int -> int =
1313+ fun t acc ->
1414+ match t with
1515+ | Empty -> acc
1616+ | Field field -> length_loop field.rest (acc + 1)
1717+ ;;
1818+1919+ let length t = length_loop t 0
2020+end
2121+2222+let[@tail_mod_cons] rec of_list
2323+ : type a.
2424+ caller:string
2525+ -> fields:a Fields.t
2626+ -> len:int
2727+ -> original_sexp:Sexp.t
2828+ -> pos:int
2929+ -> Sexp.t list
3030+ -> a
3131+ =
3232+ fun ~caller ~fields ~len ~original_sexp ~pos list ->
3333+ match fields with
3434+ | Empty ->
3535+ (match list with
3636+ | [] -> ()
3737+ | _ :: _ -> Sexp_conv_error.tuple_of_size_n_expected caller len original_sexp)
3838+ | Field { name; conv; rest } ->
3939+ (match list with
4040+ | [] -> Sexp_conv_error.tuple_of_size_n_expected caller len original_sexp
4141+ | sexp :: list ->
4242+ (match sexp with
4343+ | List [ Atom atom; sexp ] ->
4444+ if String.equal atom name
4545+ then
4646+ ( conv sexp
4747+ , of_list ~caller ~fields:rest ~len ~original_sexp ~pos:(pos + 1) list )
4848+ else Sexp_conv_error.tuple_incorrect_label caller name pos original_sexp
4949+ | _ -> Sexp_conv_error.tuple_pair_expected caller name sexp))
5050+;;
5151+5252+let labeled_tuple_of_sexp ~caller ~fields ~create original_sexp =
5353+ let len = Fields.length fields in
5454+ match (original_sexp : Sexp.t) with
5555+ | Atom _ -> Sexp_conv_error.tuple_of_size_n_expected caller len original_sexp
5656+ | List list -> create (of_list ~caller ~fields ~len ~original_sexp ~pos:0 list)
5757+;;
···11+(* Parses sexps for labeled tuples, a language feature currently only implemented in Jane
22+ Street's experimental branch of the compiler
33+ (https://github.com/ocaml-flambda/flambda-backend/). *)
44+55+module Fields : sig
66+ type _ t =
77+ | Field :
88+ 'a 'b.
99+ { name : string
1010+ ; conv : Sexp.t -> unit -> 'a
1111+ ; rest : 'b t
1212+ }
1313+ -> ((unit -> 'a) * 'b) t
1414+ | Empty : unit t
1515+end
1616+1717+val labeled_tuple_of_sexp
1818+ : caller:string
1919+ -> fields:'a Fields.t
2020+ -> create:('a -> 'b)
2121+ -> Sexp.t
2222+ -> 'b
+344
vendor/opam/sexplib0/src/sexp_conv_record.ml
···11+open! StdLabels
22+open Basement
33+open Sexp_conv
44+open Sexp_conv_error
55+66+module Kind = struct
77+ type (_, _) t =
88+ | Default : 'a. (unit -> 'a) -> (unit -> 'a, Sexp.t -> unit -> 'a) t
99+ | Omit_nil : 'a. (unit -> 'a, Sexp.t -> unit -> 'a) t
1010+ | Required : 'a. (unit -> 'a, Sexp.t -> unit -> 'a) t
1111+ | Sexp_array : ('a array, Sexp.t -> 'a) t
1212+ | Sexp_bool : (bool, unit) t
1313+ | Sexp_list : ('a list, Sexp.t -> 'a) t
1414+ | Sexp_option : ('a option, Sexp.t -> 'a) t
1515+ | Sexp_or_null : ('a Or_null_shim.t, Sexp.t -> 'a) t
1616+end
1717+1818+module Fields = struct
1919+ type _ t =
2020+ | Empty : unit t
2121+ | Field :
2222+ 'a 'b 'conv.
2323+ { name : string
2424+ ; kind : ('a, 'conv) Kind.t
2525+ ; conv : 'conv
2626+ ; rest : 'b t
2727+ }
2828+ -> ('a * 'b) t
2929+3030+ let length =
3131+ let rec length_loop : type a. a t -> int -> int =
3232+ fun t acc ->
3333+ match t with
3434+ | Field { rest; _ } -> length_loop rest (acc + 1)
3535+ | Empty -> acc
3636+ in
3737+ fun t -> length_loop t 0
3838+ ;;
3939+end
4040+4141+module Malformed = struct
4242+ (* Represents errors that can occur due to malformed record sexps. Accumulated as a
4343+ value so we can report multiple names at once for extra fields, duplicate fields, or
4444+ missing fields. *)
4545+ type t =
4646+ | Bool_payload
4747+ | Missing_and_extras of
4848+ { missing : string list
4949+ ; extras : string list
5050+ }
5151+ | Dups of string list
5252+ | Non_pair of Sexp.t option
5353+5454+ let missing missing = Missing_and_extras { missing; extras = [] }
5555+ let extras extras = Missing_and_extras { missing = []; extras }
5656+5757+ let combine a b =
5858+ match a, b with
5959+ (* choose the first bool-payload or non-pair error that occurs *)
6060+ | ((Bool_payload | Non_pair _) as t), _ -> t
6161+ | _, ((Bool_payload | Non_pair _) as t) -> t
6262+ (* combine lists of similar errors *)
6363+ | ( Missing_and_extras { missing = missing_a; extras = extras_a }
6464+ , Missing_and_extras { missing = missing_b; extras = extras_b } ) ->
6565+ Missing_and_extras { missing = missing_a @ missing_b; extras = extras_a @ extras_b }
6666+ | Dups a, Dups b -> Dups (a @ b)
6767+ (* otherwise, dups > extras > missing *)
6868+ | (Dups _ as t), _ | _, (Dups _ as t) -> t
6969+ ;;
7070+7171+ let raise t ~caller ~context =
7272+ match t with
7373+ | Bool_payload -> record_sexp_bool_with_payload caller context
7474+ | Missing_and_extras { missing; extras } ->
7575+ record_missing_and_extra_fields caller ~missing ~extras context
7676+ | Dups names -> record_duplicate_fields caller names context
7777+ | Non_pair maybe_context ->
7878+ let context = Option.value maybe_context ~default:context in
7979+ record_only_pairs_expected caller context
8080+ ;;
8181+end
8282+8383+exception Malformed of Malformed.t
8484+8585+module State = struct
8686+ (* Stores sexps corresponding to record fields, in the order the fields were declared.
8787+ Excludes fields already parsed in the fast path.
8888+8989+ List sexps represent a field that is present, such as (x 1) for a field named "x".
9090+ Atom sexps represent a field that is absent, or at least not yet seen. *)
9191+ type t = { state : Sexp.t array } [@@unboxed]
9292+9393+ let unsafe_get t pos = Array.unsafe_get t.state pos
9494+ let unsafe_set t pos sexp = Array.unsafe_set t.state pos sexp
9595+ let create len = { state = Array.make len (Sexp.Atom "") }
9696+end
9797+9898+(* Parsing field values from state. *)
9999+100100+let rec parse_value_malformed
101101+ : type a b. Malformed.t -> fields:(a * b) Fields.t -> state:State.t -> pos:int -> a
102102+ =
103103+ fun malformed ~fields ~state ~pos ->
104104+ let (Field field) = fields in
105105+ let malformed =
106106+ match parse_values ~fields:field.rest ~state ~pos:(pos + 1) with
107107+ | (_ : b) -> malformed
108108+ | exception Malformed other -> Malformed.combine malformed other
109109+ in
110110+ raise (Malformed malformed)
111111+112112+and[@tail_mod_cons] parse_value
113113+ : type a b. fields:(a * b) Fields.t -> state:State.t -> pos:int -> a * b
114114+ =
115115+ fun ~fields ~state ~pos ->
116116+ let (Field { name; kind; conv; rest }) = fields in
117117+ let value : a =
118118+ match kind, State.unsafe_get state pos with
119119+ (* well-formed *)
120120+ | Required, List [ _; sexp ] -> conv sexp
121121+ | Default _, List [ _; sexp ] -> conv sexp
122122+ | Omit_nil, List [ _; sexp ] -> conv sexp
123123+ | Sexp_option, List [ _; sexp ] -> Some (conv sexp)
124124+ | Sexp_or_null, List [ _; sexp ] -> This (conv sexp)
125125+ | Sexp_list, List [ _; sexp ] -> list_of_sexp conv sexp
126126+ | Sexp_array, List [ _; sexp ] -> array_of_sexp conv sexp
127127+ | Sexp_bool, List [ _ ] -> true
128128+ (* ill-formed *)
129129+ | ( ( Required
130130+ | Default _
131131+ | Omit_nil
132132+ | Sexp_option
133133+ | Sexp_or_null
134134+ | Sexp_list
135135+ | Sexp_array )
136136+ , (List (_ :: _ :: _ :: _) as sexp) ) ->
137137+ parse_value_malformed (Non_pair (Some sexp)) ~fields ~state ~pos
138138+ | ( ( Required
139139+ | Default _
140140+ | Omit_nil
141141+ | Sexp_option
142142+ | Sexp_or_null
143143+ | Sexp_list
144144+ | Sexp_array )
145145+ , List ([] | [ _ ]) ) -> parse_value_malformed (Non_pair None) ~fields ~state ~pos
146146+ | Sexp_bool, List ([] | _ :: _ :: _) ->
147147+ parse_value_malformed Bool_payload ~fields ~state ~pos
148148+ (* absent *)
149149+ | Required, Atom _ ->
150150+ parse_value_malformed (Malformed.missing [ name ]) ~fields ~state ~pos
151151+ | Default default, Atom _ -> default
152152+ | Omit_nil, Atom _ -> conv (List [])
153153+ | Sexp_option, Atom _ -> None
154154+ | Sexp_or_null, Atom _ -> Null
155155+ | Sexp_list, Atom _ -> []
156156+ | Sexp_array, Atom _ -> [||]
157157+ | Sexp_bool, Atom _ -> false
158158+ in
159159+ value, parse_values ~fields:rest ~state ~pos:(pos + 1)
160160+161161+and[@tail_mod_cons] parse_values
162162+ : type a. fields:a Fields.t -> state:State.t -> pos:int -> a
163163+ =
164164+ fun ~fields ~state ~pos ->
165165+ match fields with
166166+ | Field _ -> parse_value ~fields ~state ~pos
167167+ | Empty -> ()
168168+;;
169169+170170+(* Populating state. Handles slow path cases where there may be reordered, duplicated,
171171+ missing, or extra fields. *)
172172+173173+let rec parse_spine_malformed malformed ~index ~extra ~seen ~state ~len sexps =
174174+ let malformed =
175175+ match parse_spine_slow ~index ~extra ~seen ~state ~len sexps with
176176+ | () -> malformed
177177+ | exception Malformed other -> Malformed.combine malformed other
178178+ in
179179+ raise (Malformed malformed)
180180+181181+and parse_spine_slow ~index ~extra ~seen ~state ~len sexps =
182182+ match (sexps : Sexp.t list) with
183183+ | [] -> ()
184184+ | (List (Atom name :: _) as field) :: sexps ->
185185+ let i = index name in
186186+ (match seen <= i && i < len with
187187+ | true ->
188188+ (* valid field for slow-path parsing *)
189189+ let pos = i - seen in
190190+ (match State.unsafe_get state pos with
191191+ | Atom _ ->
192192+ (* field not seen yet *)
193193+ State.unsafe_set state pos field;
194194+ parse_spine_slow ~index ~extra ~seen ~state ~len sexps
195195+ | List _ ->
196196+ (* field already seen *)
197197+ parse_spine_malformed (Dups [ name ]) ~index ~extra ~seen ~state ~len sexps)
198198+ | false ->
199199+ (match 0 <= i && i < seen with
200200+ | true ->
201201+ (* field seen in fast path *)
202202+ parse_spine_malformed (Dups [ name ]) ~index ~extra ~seen ~state ~len sexps
203203+ | false ->
204204+ (* extra field *)
205205+ (match extra with
206206+ | true -> parse_spine_slow ~index ~extra ~seen ~state ~len sexps
207207+ | false ->
208208+ parse_spine_malformed
209209+ (Malformed.extras [ name ])
210210+ ~index
211211+ ~extra
212212+ ~seen
213213+ ~state
214214+ ~len
215215+ sexps)))
216216+ | sexp :: sexps ->
217217+ parse_spine_malformed (Non_pair (Some sexp)) ~index ~extra ~seen ~state ~len sexps
218218+;;
219219+220220+(* Slow path for record parsing. Uses state to store fields as they are discovered. *)
221221+222222+let parse_record_slow ~fields ~index ~extra ~seen sexps =
223223+ let unseen = Fields.length fields in
224224+ let state = State.create unseen in
225225+ let len = seen + unseen in
226226+ (* populate state *)
227227+ let maybe_malformed =
228228+ match parse_spine_slow ~index ~extra ~seen ~state ~len sexps with
229229+ | exception Malformed malformed -> Some malformed
230230+ | () -> None
231231+ in
232232+ (* parse values from state *)
233233+ let parsed_or_malformed =
234234+ match parse_values ~fields ~state ~pos:0 with
235235+ | values -> Ok values
236236+ | exception Malformed malformed -> Error malformed
237237+ in
238238+ match maybe_malformed, parsed_or_malformed with
239239+ | None, Ok values -> values
240240+ | Some malformed, Ok _ | None, Error malformed -> raise (Malformed malformed)
241241+ | Some malformed1, Error malformed2 ->
242242+ raise (Malformed (Malformed.combine malformed1 malformed2))
243243+;;
244244+245245+(* Fast path for record parsing. Directly parses and returns fields in the order they are
246246+ declared. Falls back on slow path if any fields are absent, reordered, or malformed. *)
247247+248248+let[@tail_mod_cons] rec parse_field_fast
249249+ : type a b.
250250+ fields:(a * b) Fields.t
251251+ -> index:(string -> int)
252252+ -> extra:bool
253253+ -> seen:int
254254+ -> Sexp.t list
255255+ -> a * b
256256+ =
257257+ fun ~fields ~index ~extra ~seen sexps ->
258258+ let (Field { name; kind; conv; rest }) = fields in
259259+ match sexps with
260260+ | List (Atom atom :: args) :: others when String.equal atom name ->
261261+ (match kind, args with
262262+ | Required, [ sexp ] ->
263263+ conv sexp, parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others
264264+ | Default _, [ sexp ] ->
265265+ conv sexp, parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others
266266+ | Omit_nil, [ sexp ] ->
267267+ conv sexp, parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others
268268+ | Sexp_option, [ sexp ] ->
269269+ ( Some (conv sexp)
270270+ , parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others )
271271+ | Sexp_or_null, [ sexp ] ->
272272+ ( This (conv sexp)
273273+ , parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others )
274274+ | Sexp_list, [ sexp ] ->
275275+ ( list_of_sexp conv sexp
276276+ , parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others )
277277+ | Sexp_array, [ sexp ] ->
278278+ ( array_of_sexp conv sexp
279279+ , parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others )
280280+ | Sexp_bool, [] ->
281281+ true, parse_spine_fast ~fields:rest ~index ~extra ~seen:(seen + 1) others
282282+ (* malformed field of some kind, dispatch to slow path *)
283283+ | _, _ -> (parse_record_slow [@tailcall false]) ~fields ~index ~extra ~seen sexps)
284284+ (* malformed or out-of-order field, dispatch to slow path *)
285285+ | _ -> (parse_record_slow [@tailcall false]) ~fields ~index ~extra ~seen sexps
286286+287287+and[@tail_mod_cons] parse_spine_fast
288288+ : type a.
289289+ fields:a Fields.t
290290+ -> index:(string -> int)
291291+ -> extra:bool
292292+ -> seen:int
293293+ -> Sexp.t list
294294+ -> a
295295+ =
296296+ fun ~fields ~index ~extra ~seen sexps ->
297297+ match fields with
298298+ | Field _ -> parse_field_fast ~fields ~index ~extra ~seen sexps
299299+ | Empty ->
300300+ (match sexps with
301301+ | [] -> ()
302302+ | _ :: _ ->
303303+ (* extra sexps, dispatch to slow path *)
304304+ (parse_record_slow [@tailcall false]) ~fields ~index ~extra ~seen sexps)
305305+;;
306306+307307+let parse_record_fast ~fields ~index ~extra sexps =
308308+ parse_spine_fast ~fields ~index ~extra ~seen:0 sexps
309309+;;
310310+311311+(* Entry points. *)
312312+313313+let record_of_sexps
314314+ ~caller
315315+ ~context
316316+ ~fields
317317+ ~index_of_field
318318+ ~allow_extra_fields
319319+ ~create
320320+ sexps
321321+ =
322322+ let allow_extra_fields =
323323+ allow_extra_fields || not (Dynamic.get Sexp_conv.record_check_extra_fields)
324324+ in
325325+ match
326326+ parse_record_fast ~fields ~index:index_of_field ~extra:allow_extra_fields sexps
327327+ with
328328+ | value -> create value
329329+ | exception Malformed malformed -> Malformed.raise malformed ~caller ~context
330330+;;
331331+332332+let record_of_sexp ~caller ~fields ~index_of_field ~allow_extra_fields ~create sexp =
333333+ match (sexp : Sexp.t) with
334334+ | Atom _ as context -> record_list_instead_atom caller context
335335+ | List sexps as context ->
336336+ record_of_sexps
337337+ ~caller
338338+ ~context
339339+ ~fields
340340+ ~index_of_field
341341+ ~allow_extra_fields
342342+ ~create
343343+ sexps
344344+;;
+56
vendor/opam/sexplib0/src/sexp_conv_record.mli
···11+module Kind : sig
22+ (** A GADT specifying how to parse a record field. See documentation for
33+ [ppx_sexp_conv]. *)
44+ type (_, _) t =
55+ | Default : 'a. (unit -> 'a) -> (unit -> 'a, Sexp.t -> unit -> 'a) t
66+ | Omit_nil : 'a. (unit -> 'a, Sexp.t -> unit -> 'a) t
77+ | Required : 'a. (unit -> 'a, Sexp.t -> unit -> 'a) t
88+ | Sexp_array : ('a array, Sexp.t -> 'a) t
99+ | Sexp_bool : (bool, unit) t
1010+ | Sexp_list : ('a list, Sexp.t -> 'a) t
1111+ | Sexp_option : ('a option, Sexp.t -> 'a) t
1212+ | Sexp_or_null : ('a Basement.Or_null_shim.t, Sexp.t -> 'a) t
1313+end
1414+1515+module Fields : sig
1616+ (** A GADT specifying record fields. *)
1717+1818+ type _ t =
1919+ | Empty : unit t
2020+ | Field :
2121+ 'a 'b 'conv.
2222+ { name : string
2323+ ; kind : ('a, 'conv) Kind.t
2424+ ; conv : 'conv
2525+ ; rest : 'b t
2626+ }
2727+ -> ('a * 'b) t
2828+end
2929+3030+(** Parses a record from a sexp that must be a list of fields.
3131+3232+ Uses [caller] as the source for error messages. Parses using the given [field]s. Uses
3333+ [index_of_field] to look up field names found in sexps. If [allow_extra_fields] is
3434+ true, extra fields are allowed and discarded without error. [create] is used to
3535+ construct the final returned value. *)
3636+val record_of_sexp
3737+ : caller:string
3838+ -> fields:'a Fields.t
3939+ -> index_of_field:(string -> int)
4040+ -> allow_extra_fields:bool
4141+ -> create:('a -> 'b)
4242+ -> Sexp.t
4343+ -> 'b
4444+4545+(** Like [record_of_sexp], but for a list of sexps with no [List] wrapper. Used, for
4646+ example, to parse arguments to a variant constructor with an inlined record argument.
4747+ Reports [context] for parse errors when no more specific sexp is applicable. *)
4848+val record_of_sexps
4949+ : caller:string
5050+ -> context:Sexp.t
5151+ -> fields:'a Fields.t
5252+ -> index_of_field:(string -> int)
5353+ -> allow_extra_fields:bool
5454+ -> create:('a -> 'b)
5555+ -> Sexp.t list
5656+ -> 'b
+14
vendor/opam/sexplib0/src/sexp_grammar.ml
···11+include Sexp_grammar_intf.Definitions
22+33+let coerce (type a b) ({ untyped = _ } as t : a t) : b t = t
44+55+let tag (type a) ({ untyped = grammar } : a t) ~key ~value : a t =
66+ { untyped = Tagged { key; value; grammar } }
77+;;
88+99+let doc_comment_tag = "sexp_grammar.doc_comment"
1010+let type_name_tag = "sexp_grammar.type_name"
1111+let assoc_tag = "sexp_grammar.assoc"
1212+let assoc_key_tag = "sexp_grammar.assoc.key"
1313+let assoc_value_tag = "sexp_grammar.assoc.value"
1414+let completion_suggested = "sexp_grammar.completion-suggested"
···11+(** Representation of S-expression grammars *)
22+33+(** This module defines a representation for s-expression grammars. Using ppx_sexp_conv
44+ and [[@@deriving sexp_grammar]] produces a grammar that is compatible with the derived
55+ [of_sexp] for a given type.
66+77+ As with other derived definitions, polymorphic types derive a function that takes a
88+ grammar for each type argument and produces a grammar for the monomorphized type.
99+1010+ Monomorphic types derive a grammar directly. To avoid top-level side effects,
1111+ [[@@deriving sexp_grammar]] wraps grammars in the [Lazy] constructor as needed.
1212+1313+ This type may change over time as our needs for expressive grammars change. We will
1414+ attempt to make changes backward-compatible, or at least provide a reasonable upgrade
1515+ path. *)
1616+1717+[@@@warning "-30"] (* allow duplicate field names *)
1818+1919+module Definitions = struct
2020+ (** Grammar of a sexp. *)
2121+ type grammar =
2222+ | Any of string (** accepts any sexp; string is a type name for human readability *)
2323+ | Bool (** accepts the atoms "true" or "false", modulo capitalization *)
2424+ | Char (** accepts any single-character atom *)
2525+ | Integer
2626+ (** accepts any atom matching ocaml integer syntax, regardless of bit width *)
2727+ | Float (** accepts any atom matching ocaml float syntax *)
2828+ | String (** accepts any atom *)
2929+ | Option of grammar
3030+ (** accepts an option, both [None] vs [Some _] and [()] vs [(_)]. *)
3131+ | List of list_grammar (** accepts a list *)
3232+ | Variant of variant (** accepts clauses keyed by a leading or sole atom *)
3333+ | Union of grammar list (** accepts a sexp if any of the listed grammars accepts it *)
3434+ | Tagged of grammar with_tag
3535+ (** annotates a grammar with a client-specific key/value pair *)
3636+ | Tyvar of string
3737+ (** Name of a type variable, e.g. [Tyvar "a"] for ['a]. Only meaningful when the body
3838+ of the innermost enclosing [defn] defines a corresponding type variable. *)
3939+ | Tycon of string * grammar list * defn list
4040+ (** Type constructor applied to arguments, and its definition.
4141+4242+ For example, writing [Tycon ("tree", [ Integer ], defns)] represents [int tree],
4343+ for whatever [tree] is defined as in [defns]. The following defines [tree] as a
4444+ binary tree with the parameter type stored at the leaves.
4545+4646+ {[
4747+ let defns =
4848+ [ { tycon = "tree"
4949+ ; tyvars = [ "a" ]
5050+ ; grammar =
5151+ Variant
5252+ { name_kind = Capitalized
5353+ ; clauses =
5454+ [ { name = "Node"
5555+ ; args = Cons (Recursive ("node", [ Tyvar "a" ]), Empty)
5656+ }
5757+ ; { name = "Leaf"
5858+ ; args = Cons (Recursive ("leaf", [ Tyvar "a" ]), Empty)
5959+ }
6060+ ]
6161+ }
6262+ }
6363+ ; { tycon = "node"
6464+ ; tyvars = [ "a" ]
6565+ ; grammar = List (Many (Recursive "tree", [ Tyvar "a" ]))
6666+ }
6767+ ; { tycon = "leaf"; tyvars = [ "a" ]; grammar = [ Tyvar "a" ] }
6868+ ]
6969+ ;;
7070+ ]}
7171+7272+ To illustrate the meaning of [Tycon] with respect to [defns], and to demonstrate
7373+ one way to access them, it is equivalent to expand the definition of "tree" one
7474+ level and move the [defns] to enclosed recursive references:
7575+7676+ {[
7777+ Tycon ("tree", [ Integer ], defns)
7878+ --> Variant
7979+ { name_kind = Capitalized
8080+ ; clauses =
8181+ [ { name = "Node"
8282+ ; args = Cons (Tycon ("node", [ Tyvar "a" ], defns), Empty)
8383+ }
8484+ ; { name = "Leaf"
8585+ ; args = Cons (Tycon ("leaf", [ Tyvar "a" ], defns), Empty)
8686+ }
8787+ ]
8888+ }
8989+ ]}
9090+9191+ This transformation exposes the structure of a grammar with recursive references,
9292+ while preserving the meaning of recursively-defined elements. *)
9393+ | Recursive of string * grammar list
9494+ (** Type constructor applied to arguments. Used to denote recursive type references.
9595+ Only meaningful when used inside the [defn]s of a [Tycon] grammar, to refer to a
9696+ type constructor in the nearest enclosing [defn] list. *)
9797+ | Lazy of grammar Basement.Portable_lazy.t
9898+ (** Lazily computed grammar. Use [Lazy] to avoid top-level side effects. To define
9999+ recursive grammars, use [Recursive] instead. *)
100100+ [@@unsafe_allow_any_mode_crossing]
101101+102102+ (** Grammar of a list of sexps. *)
103103+ and list_grammar =
104104+ | Empty (** accepts an empty list of sexps *)
105105+ | Cons of grammar * list_grammar
106106+ (** accepts a non-empty list with head and tail matching the given grammars *)
107107+ | Many of grammar (** accepts zero or more sexps, each matching the given grammar *)
108108+ | Fields of record (** accepts sexps representing fields of a record *)
109109+110110+ (** Case sensitivity options for names of variant constructors. *)
111111+ and case_sensitivity =
112112+ | Case_insensitive (** Comparison is case insensitive. Used for custom parsers. *)
113113+ | Case_sensitive (** Comparison is case sensitive. Used for polymorphic variants. *)
114114+ | Case_sensitive_except_first_character
115115+ (** Comparison is case insensitive for the first character and case sensitive
116116+ afterward. Used for regular variants. *)
117117+118118+ (** Grammar of variants. Accepts any sexp matching one of the clauses. *)
119119+ and variant =
120120+ { case_sensitivity : case_sensitivity
121121+ ; clauses : clause with_tag_list list
122122+ }
123123+ [@@unsafe_allow_any_mode_crossing]
124124+125125+ (** Grammar of a single variant clause. Accepts sexps based on the [clause_kind]. *)
126126+ and clause =
127127+ { name : string
128128+ ; clause_kind : clause_kind
129129+ }
130130+131131+ (** Grammar of a single variant clause's contents. [Atom_clause] accepts an atom
132132+ matching the clause's name. [List_clause] accepts a list whose head is an atom
133133+ matching the clause's name and whose tail matches [args]. The clause's name is
134134+ matched modulo the variant's [name_kind]. *)
135135+ and clause_kind =
136136+ | Atom_clause
137137+ | List_clause of { args : list_grammar }
138138+139139+ (** Grammar of a record. Accepts any list of sexps specifying each of the fields,
140140+ regardless of order. If [allow_extra_fields] is specified, ignores sexps with names
141141+ not found in [fields]. *)
142142+ and record =
143143+ { allow_extra_fields : bool
144144+ ; fields : field with_tag_list list
145145+ }
146146+147147+ (** Grammar of a record field. A field must show up exactly once in a record if
148148+ [required], or at most once otherwise. Accepts a list headed by [name] as an atom,
149149+ followed by sexps matching [args]. *)
150150+ and field =
151151+ { name : string
152152+ ; required : bool
153153+ ; args : list_grammar
154154+ }
155155+156156+ (** Grammar tagged with client-specific key/value pair. *)
157157+ and 'a with_tag =
158158+ { key : string
159159+ ; value : Sexp.t
160160+ ; grammar : 'a
161161+ }
162162+163163+ and 'a with_tag_list =
164164+ | Tag of 'a with_tag_list with_tag
165165+ | No_tag of 'a
166166+167167+ (** Grammar of a recursive type definition. Names the [tycon] being defined, and the
168168+ [tyvars] it takes as parameters. Specifies the [grammar] of the [tycon]. The grammar
169169+ may refer to any of the [tyvars], and to any of the [tycon]s from the same set of
170170+ [Recursive] definitions. *)
171171+ and defn =
172172+ { tycon : string
173173+ ; tyvars : string list
174174+ ; grammar : grammar
175175+ }
176176+177177+ (** Top-level grammar type. Has a phantom type parameter to associate each grammar with
178178+ the type its sexps represent. This makes it harder to apply grammars to the wrong
179179+ type, while grammars can still be easily coerced to a new type if needed. *)
180180+ type _ t = { untyped : grammar } [@@unboxed]
181181+end
182182+183183+module type Sexp_grammar = sig
184184+ include module type of struct
185185+ include Definitions
186186+ end
187187+188188+ (** Convert a sexp grammar for one type to another. *)
189189+ val coerce : 'a 'b. 'a t -> 'b t
190190+191191+ (** Add a key/value tag to a grammar. *)
192192+ val tag : 'a. 'a t -> key:string -> value:Sexp.t -> 'a t
193193+194194+ (** This reserved key is used for all tags generated from doc comments. *)
195195+ val doc_comment_tag : string
196196+197197+ (** This reserved key can be used to associate a type name with a grammar. *)
198198+ val type_name_tag : string
199199+200200+ (** This reserved key indicates that a sexp represents a key/value association. The
201201+ tag's value is ignored. *)
202202+ val assoc_tag : string
203203+204204+ (** This reserved key indicates that a sexp is a key in a key/value association. The
205205+ tag's value is ignored. *)
206206+ val assoc_key_tag : string
207207+208208+ (** This reserved key indicates that a sexp is a value in a key/value association. The
209209+ tag's value is ignored. *)
210210+ val assoc_value_tag : string
211211+212212+ (** When the key is set to [Atom "false"] for a variant clause, that clause should not
213213+ be suggested in auto-completion based on the sexp grammar. *)
214214+ val completion_suggested : string
215215+end
+186
vendor/opam/sexplib0/src/sexp_intf.ml
···11+open Basement
22+33+module Definitions = struct
44+ type t =
55+ | Atom of string
66+ | List of t list
77+88+ module type Pretty_print_to_formatter = sig
99+ (** [pp_hum ppf sexp] outputs S-expression [sexp] to formatter [ppf] in human readable
1010+ form. *)
1111+ val pp_hum : Format.formatter -> t -> unit
1212+1313+ (** [pp_hum_indent n ppf sexp] outputs S-expression [sexp] to formatter [ppf] in human
1414+ readable form and indentation level [n]. *)
1515+ val pp_hum_indent : int -> Format.formatter -> t -> unit
1616+1717+ (** [pp_mach ppf sexp] outputs S-expression [sexp] to formatter [ppf] in machine
1818+ readable (i.e. most compact) form. *)
1919+ val pp_mach : Format.formatter -> t -> unit
2020+2121+ (** Same as [pp_mach]. *)
2222+ val pp : Format.formatter -> t -> unit
2323+ end
2424+2525+ module type Pretty_printing_helpers_private = sig
2626+ (** Functions used by [Make_pretty_printing] *)
2727+2828+ val mach_maybe_esc_str : string -> string
2929+ val must_escape : string -> bool
3030+ val esc_str : string -> string
3131+ end
3232+3333+ module type Pretty_printing_helpers = sig
3434+ include Pretty_print_to_formatter (** @inline *)
3535+3636+ include Pretty_printing_helpers_private (** @inline *)
3737+ end
3838+3939+ module type Pretty_printing = sig
4040+ (*_ In [Base], this is replaced with [String.Utf8.t] *)
4141+ type output
4242+4343+ (** {1 Printing to formatters} *)
4444+4545+ include Pretty_print_to_formatter (** @inline *)
4646+4747+ (** {1 Conversion to strings} *)
4848+4949+ (** [to_string_hum ?indent ?max_width sexp] converts S-expression [sexp] to a string
5050+ in human readable form with indentation level [indent] and target maximum width
5151+ [max_width]. Note long atoms may overflow [max_width].
5252+5353+ @param indent default = [Dynamic.get default_indent]
5454+ @param max_width default = [78] *)
5555+ val to_string_hum : ?indent:int -> ?max_width:int -> t -> output
5656+5757+ (** [to_string_mach sexp] converts S-expression [sexp] to a string in machine readable
5858+ (i.e. most compact) form. *)
5959+ val to_string_mach : t -> output
6060+6161+ (** Same as [to_string_mach]. *)
6262+ val to_string : t -> output
6363+6464+ (** {1 Conversion to buffers} *)
6565+6666+ (** [to_buffer_hum ~buf ?indent ?max_width sexp] outputs the S-expression [sexp]
6767+ converted to a string in human readable form to buffer [buf] with indentation
6868+ level [indent] and target maximum width [max_width]. Note long atoms may overflow
6969+ [max_width].
7070+7171+ @param indent default = [Dynamic.get default_indent]
7272+ @param max_width default = [78] *)
7373+ val to_buffer_hum : buf:Buffer.t -> ?indent:int -> ?max_width:int -> t -> unit
7474+7575+ (** [to_buffer_mach ~buf sexp] outputs the S-expression [sexp] converted to a string
7676+ in machine readable (i.e. most compact) form to buffer [buf]. *)
7777+ val to_buffer_mach : buf:Buffer.t -> t -> unit
7878+7979+ (** [to_buffer ~buf sexp] same as {!to_buffer_mach}. *)
8080+ val to_buffer : buf:Buffer.t -> t -> unit
8181+8282+ (** [to_buffer_gen ~buf ~add_char ~add_string sexp] outputs the S-expression [sexp]
8383+ converted to a string to buffer [buf] using the output functions [add_char] and
8484+ [add_string]. *)
8585+ val to_buffer_gen
8686+ : buf:'buffer
8787+ -> add_char:('buffer -> char -> unit)
8888+ -> add_string:('buffer -> string -> unit)
8989+ -> t
9090+ -> unit
9191+9292+ (*_ See the Jane Street Style Guide for an explanation of [Private] submodules:
9393+9494+ https://opensource.janestreet.com/standards/#private-submodules *)
9595+ module Pretty_printing_helpers_private : Pretty_printing_helpers_private
9696+ end
9797+end
9898+9999+module type Sexp = sig
100100+ (*_ NOTE: We do not use the [include module type of struct] pattern here as it messes
101101+ with the compiler's short-names heuristics. This should be okay since [Definitions]
102102+ isn't exported from this library.*)
103103+ include module type of Definitions
104104+105105+ (*_ We don't use [@@deriving sexp] as this would generated references to [Sexplib],
106106+ creating a circular dependency *)
107107+ val t_of_sexp : t -> t
108108+ val sexp_of_t : t -> t
109109+ val sexp_of_t__stack : t -> t
110110+ val equal : t -> t -> bool
111111+ val compare : t -> t -> int
112112+113113+ (** [Not_found_s] is used by functions that historically raised [Not_found], to allow
114114+ them to raise an exception that contains an informative error message (as a sexp),
115115+ while still having an exception that can be distinguished from other exceptions. *)
116116+ exception Not_found_s of t
117117+118118+ (** [Of_sexp_error (exn, sexp)] the exception raised when an S-expression could not be
119119+ successfully converted to an OCaml-value. *)
120120+ exception Of_sexp_error of exn * t
121121+122122+ (** {1 Helpers} *)
123123+124124+ (** {v
125125+ Helper to build nice s-expressions for error messages. It imitates the behavior of
126126+ [[%message ...]] from the ppx_sexp_message rewriter.
127127+128128+ [message name key_values] produces a s-expression list starting with atom [name] and
129129+ followed by list of size 2 of the form [(key value)]. When the key is the empty
130130+ string, [value] is used directly instead as for [[%message]].
131131+132132+ For instance the following code:
133133+134134+ {[
135135+ Sexp.message "error"
136136+ [ "x", sexp_of_int 42
137137+ ; "" , sexp_of_exn Exit
138138+ ]
139139+ ]}
140140+141141+ produces the s-expression:
142142+143143+ {[
144144+ (error (x 42) Exit)
145145+ ]}
146146+ v} *)
147147+ val message : string -> (string * t) list -> t
148148+149149+ (** {1 Defaults} *)
150150+151151+ (** [default_indent] reference to default indentation level for human-readable
152152+ conversions.
153153+154154+ Initialisation value: 1. *)
155155+ val default_indent : int Dynamic.t
156156+157157+ (** {1 Pretty printing of S-expressions} *)
158158+159159+ module Make_pretty_printing (Helpers : Pretty_printing_helpers) :
160160+ Pretty_printing with type output := string
161161+162162+ include Pretty_printing with type output := string
163163+164164+ (** See [Pretty_printing.to_string_mach] and [to_string], respectively. *)
165165+166166+ val to_string_mach__stack : t -> string
167167+ val to_string__stack : t -> string
168168+169169+ (** {1 Styles} *)
170170+171171+ val of_float_style : [ `Underscores | `No_underscores ] Dynamic.t
172172+ val of_int_style : [ `Underscores | `No_underscores ] Dynamic.t
173173+174174+ (*_ See the Jane Street Style Guide for an explanation of [Private] submodules:
175175+176176+ https://opensource.janestreet.com/standards/#private-submodules *)
177177+ module Private : sig
178178+ (*_ Exported for sexplib *)
179179+180180+ val size : t -> int * int
181181+ val buffer : unit -> Buffer.t
182182+183183+ include Definitions.Pretty_printing_helpers_private
184184+ include Pretty_printing with type output := string
185185+ end
186186+end
+787
vendor/opam/sexplib0/src/sexpable.ml
···11+[@@@expand_inline
22+ [%%template
33+ module type Of_sexp = sig
44+ type t
55+66+ val t_of_sexp : Sexp.t -> t
77+ end
88+99+ [@@@alloc.default a @ m = (heap @ global, stack @ local)]
1010+1111+ module type Sexp_of = sig
1212+ type t
1313+1414+ val sexp_of_t : t -> Sexp.t [@@alloc a @ m = (a @ m, heap @ global)]
1515+ end
1616+1717+ module type S_any = sig
1818+ type t
1919+2020+ include Of_sexp with type t := t
2121+ include Sexp_of [@alloc a] with type t := t
2222+ end
2323+2424+ module type S = sig
2525+ type t
2626+2727+ include S_any [@alloc a] with type t := t
2828+ end
2929+3030+ [@@@kind.default ka = (value, any)]
3131+3232+ module type S_any1 = sig
3333+ type 'a t
3434+3535+ val t_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a t
3636+3737+ val sexp_of_t : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
3838+ [@@alloc a @ m = (a @ m, heap @ global)]
3939+ end
4040+4141+ module type S1 = sig
4242+ type 'a t
4343+4444+ include S_any1 [@kind ka] [@alloc a] with type 'a t := 'a t
4545+ end
4646+4747+ [@@@kind.default kb = (value, any)]
4848+4949+ module type S_any2 = sig
5050+ type ('a, 'b) t
5151+5252+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
5353+5454+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
5555+ [@@alloc a @ m = (a @ m, heap @ global)]
5656+ end
5757+5858+ module type S2 = sig
5959+ type ('a, 'b) t
6060+6161+ include S_any2 [@kind ka kb] [@alloc a] with type ('a, 'b) t := ('a, 'b) t
6262+ end
6363+6464+ [@@@kind.default kc = (value, any)]
6565+6666+ module type S_any3 = sig
6767+ type ('a, 'b, 'c) t
6868+6969+ val t_of_sexp
7070+ : 'a 'b 'c.
7171+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
7272+7373+ val sexp_of_t
7474+ : 'a 'b 'c.
7575+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
7676+ [@@alloc a @ m = (a @ m, heap @ global)]
7777+ end
7878+7979+ module type S3 = sig
8080+ type ('a, 'b, 'c) t
8181+8282+ include S_any3 [@kind ka kb kc] [@alloc a] with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
8383+ end]]
8484+8585+module type Of_sexp = sig
8686+ type t
8787+8888+ val t_of_sexp : Sexp.t -> t
8989+end
9090+9191+include struct
9292+ module type Sexp_of = sig
9393+ type t
9494+9595+ val sexp_of_t : t -> Sexp.t
9696+ end
9797+9898+ module type S_any = sig
9999+ type t
100100+101101+ include Of_sexp with type t := t
102102+ include Sexp_of with type t := t
103103+ end
104104+105105+ module type S = sig
106106+ type t
107107+108108+ include S_any with type t := t
109109+ end
110110+111111+ include struct
112112+ module type S_any1 = sig
113113+ type 'a t
114114+115115+ val t_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a t
116116+ val sexp_of_t : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
117117+ end
118118+119119+ module type S1 = sig
120120+ type 'a t
121121+122122+ include S_any1 with type 'a t := 'a t
123123+ end
124124+125125+ include struct
126126+ module type S_any2 = sig
127127+ type ('a, 'b) t
128128+129129+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
130130+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
131131+ end
132132+133133+ module type S2 = sig
134134+ type ('a, 'b) t
135135+136136+ include S_any2 with type ('a, 'b) t := ('a, 'b) t
137137+ end
138138+139139+ include struct
140140+ module type S_any3 = sig
141141+ type ('a, 'b, 'c) t
142142+143143+ val t_of_sexp
144144+ : 'a 'b 'c.
145145+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
146146+147147+ val sexp_of_t
148148+ : 'a 'b 'c.
149149+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
150150+ end
151151+152152+ module type S3 = sig
153153+ type ('a, 'b, 'c) t
154154+155155+ include S_any3 with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
156156+ end
157157+ end [@@ocaml.doc " @inline "]
158158+159159+ include struct
160160+ module type S_any3__value__value__any = sig
161161+ type ('a, 'b, 'c) t
162162+163163+ val t_of_sexp
164164+ : 'a 'b 'c.
165165+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
166166+167167+ val sexp_of_t
168168+ : 'a 'b 'c.
169169+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
170170+ end
171171+172172+ module type S3__value__value__any = sig
173173+ type ('a, 'b, 'c) t
174174+175175+ include S_any3__value__value__any with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
176176+ end
177177+ end [@@ocaml.doc " @inline "]
178178+ end [@@ocaml.doc " @inline "]
179179+180180+ include struct
181181+ module type S_any2__value__any = sig
182182+ type ('a, 'b) t
183183+184184+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
185185+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
186186+ end
187187+188188+ module type S2__value__any = sig
189189+ type ('a, 'b) t
190190+191191+ include S_any2__value__any with type ('a, 'b) t := ('a, 'b) t
192192+ end
193193+194194+ include struct
195195+ module type S_any3__value__any__value = sig
196196+ type ('a, 'b, 'c) t
197197+198198+ val t_of_sexp
199199+ : 'a 'b 'c.
200200+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
201201+202202+ val sexp_of_t
203203+ : 'a 'b 'c.
204204+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
205205+ end
206206+207207+ module type S3__value__any__value = sig
208208+ type ('a, 'b, 'c) t
209209+210210+ include S_any3__value__any__value with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
211211+ end
212212+ end [@@ocaml.doc " @inline "]
213213+214214+ include struct
215215+ module type S_any3__value__any__any = sig
216216+ type ('a, 'b, 'c) t
217217+218218+ val t_of_sexp
219219+ : 'a 'b 'c.
220220+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
221221+222222+ val sexp_of_t
223223+ : 'a 'b 'c.
224224+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
225225+ end
226226+227227+ module type S3__value__any__any = sig
228228+ type ('a, 'b, 'c) t
229229+230230+ include S_any3__value__any__any with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
231231+ end
232232+ end [@@ocaml.doc " @inline "]
233233+ end [@@ocaml.doc " @inline "]
234234+ end [@@ocaml.doc " @inline "]
235235+236236+ include struct
237237+ module type S_any1__any = sig
238238+ type 'a t
239239+240240+ val t_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a t
241241+ val sexp_of_t : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
242242+ end
243243+244244+ module type S1__any = sig
245245+ type 'a t
246246+247247+ include S_any1__any with type 'a t := 'a t
248248+ end
249249+250250+ include struct
251251+ module type S_any2__any__value = sig
252252+ type ('a, 'b) t
253253+254254+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
255255+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
256256+ end
257257+258258+ module type S2__any__value = sig
259259+ type ('a, 'b) t
260260+261261+ include S_any2__any__value with type ('a, 'b) t := ('a, 'b) t
262262+ end
263263+264264+ include struct
265265+ module type S_any3__any__value__value = sig
266266+ type ('a, 'b, 'c) t
267267+268268+ val t_of_sexp
269269+ : 'a 'b 'c.
270270+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
271271+272272+ val sexp_of_t
273273+ : 'a 'b 'c.
274274+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
275275+ end
276276+277277+ module type S3__any__value__value = sig
278278+ type ('a, 'b, 'c) t
279279+280280+ include S_any3__any__value__value with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
281281+ end
282282+ end [@@ocaml.doc " @inline "]
283283+284284+ include struct
285285+ module type S_any3__any__value__any = sig
286286+ type ('a, 'b, 'c) t
287287+288288+ val t_of_sexp
289289+ : 'a 'b 'c.
290290+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
291291+292292+ val sexp_of_t
293293+ : 'a 'b 'c.
294294+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
295295+ end
296296+297297+ module type S3__any__value__any = sig
298298+ type ('a, 'b, 'c) t
299299+300300+ include S_any3__any__value__any with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
301301+ end
302302+ end [@@ocaml.doc " @inline "]
303303+ end [@@ocaml.doc " @inline "]
304304+305305+ include struct
306306+ module type S_any2__any__any = sig
307307+ type ('a, 'b) t
308308+309309+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
310310+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
311311+ end
312312+313313+ module type S2__any__any = sig
314314+ type ('a, 'b) t
315315+316316+ include S_any2__any__any with type ('a, 'b) t := ('a, 'b) t
317317+ end
318318+319319+ include struct
320320+ module type S_any3__any__any__value = sig
321321+ type ('a, 'b, 'c) t
322322+323323+ val t_of_sexp
324324+ : 'a 'b 'c.
325325+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
326326+327327+ val sexp_of_t
328328+ : 'a 'b 'c.
329329+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
330330+ end
331331+332332+ module type S3__any__any__value = sig
333333+ type ('a, 'b, 'c) t
334334+335335+ include S_any3__any__any__value with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
336336+ end
337337+ end [@@ocaml.doc " @inline "]
338338+339339+ include struct
340340+ module type S_any3__any__any__any = sig
341341+ type ('a, 'b, 'c) t
342342+343343+ val t_of_sexp
344344+ : 'a 'b 'c.
345345+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
346346+347347+ val sexp_of_t
348348+ : 'a 'b 'c.
349349+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
350350+ end
351351+352352+ module type S3__any__any__any = sig
353353+ type ('a, 'b, 'c) t
354354+355355+ include S_any3__any__any__any with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
356356+ end
357357+ end [@@ocaml.doc " @inline "]
358358+ end [@@ocaml.doc " @inline "]
359359+ end [@@ocaml.doc " @inline "]
360360+end [@@ocaml.doc " @inline "]
361361+362362+include struct
363363+ module type Sexp_of__stack = sig
364364+ type t
365365+366366+ [@@@ocaml.text "/*"]
367367+368368+ val sexp_of_t__stack : t -> Sexp.t
369369+370370+ [@@@ocaml.text "/*"]
371371+372372+ val sexp_of_t : t -> Sexp.t
373373+ end
374374+375375+ module type S_any__stack = sig
376376+ type t
377377+378378+ include Of_sexp with type t := t
379379+ include Sexp_of__stack with type t := t
380380+ end
381381+382382+ module type S__stack = sig
383383+ type t
384384+385385+ include S_any__stack with type t := t
386386+ end
387387+388388+ include struct
389389+ module type S_any1__stack = sig
390390+ type 'a t
391391+392392+ val t_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a t
393393+394394+ [@@@ocaml.text "/*"]
395395+396396+ val sexp_of_t__stack : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
397397+398398+ [@@@ocaml.text "/*"]
399399+400400+ val sexp_of_t : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
401401+ end
402402+403403+ module type S1__stack = sig
404404+ type 'a t
405405+406406+ include S_any1__stack with type 'a t := 'a t
407407+ end
408408+409409+ include struct
410410+ module type S_any2__stack = sig
411411+ type ('a, 'b) t
412412+413413+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
414414+415415+ [@@@ocaml.text "/*"]
416416+417417+ val sexp_of_t__stack
418418+ : 'a 'b.
419419+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
420420+421421+ [@@@ocaml.text "/*"]
422422+423423+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
424424+ end
425425+426426+ module type S2__stack = sig
427427+ type ('a, 'b) t
428428+429429+ include S_any2__stack with type ('a, 'b) t := ('a, 'b) t
430430+ end
431431+432432+ include struct
433433+ module type S_any3__stack = sig
434434+ type ('a, 'b, 'c) t
435435+436436+ val t_of_sexp
437437+ : 'a 'b 'c.
438438+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
439439+440440+ [@@@ocaml.text "/*"]
441441+442442+ val sexp_of_t__stack
443443+ : 'a 'b 'c.
444444+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
445445+446446+ [@@@ocaml.text "/*"]
447447+448448+ val sexp_of_t
449449+ : 'a 'b 'c.
450450+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
451451+ end
452452+453453+ module type S3__stack = sig
454454+ type ('a, 'b, 'c) t
455455+456456+ include S_any3__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
457457+ end
458458+ end [@@ocaml.doc " @inline "]
459459+460460+ include struct
461461+ module type S_any3__value__value__any__stack = sig
462462+ type ('a, 'b, 'c) t
463463+464464+ val t_of_sexp
465465+ : 'a 'b 'c.
466466+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
467467+468468+ [@@@ocaml.text "/*"]
469469+470470+ val sexp_of_t__stack
471471+ : 'a 'b 'c.
472472+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
473473+474474+ [@@@ocaml.text "/*"]
475475+476476+ val sexp_of_t
477477+ : 'a 'b 'c.
478478+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
479479+ end
480480+481481+ module type S3__value__value__any__stack = sig
482482+ type ('a, 'b, 'c) t
483483+484484+ include
485485+ S_any3__value__value__any__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
486486+ end
487487+ end [@@ocaml.doc " @inline "]
488488+ end [@@ocaml.doc " @inline "]
489489+490490+ include struct
491491+ module type S_any2__value__any__stack = sig
492492+ type ('a, 'b) t
493493+494494+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
495495+496496+ [@@@ocaml.text "/*"]
497497+498498+ val sexp_of_t__stack
499499+ : 'a 'b.
500500+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
501501+502502+ [@@@ocaml.text "/*"]
503503+504504+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
505505+ end
506506+507507+ module type S2__value__any__stack = sig
508508+ type ('a, 'b) t
509509+510510+ include S_any2__value__any__stack with type ('a, 'b) t := ('a, 'b) t
511511+ end
512512+513513+ include struct
514514+ module type S_any3__value__any__value__stack = sig
515515+ type ('a, 'b, 'c) t
516516+517517+ val t_of_sexp
518518+ : 'a 'b 'c.
519519+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
520520+521521+ [@@@ocaml.text "/*"]
522522+523523+ val sexp_of_t__stack
524524+ : 'a 'b 'c.
525525+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
526526+527527+ [@@@ocaml.text "/*"]
528528+529529+ val sexp_of_t
530530+ : 'a 'b 'c.
531531+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
532532+ end
533533+534534+ module type S3__value__any__value__stack = sig
535535+ type ('a, 'b, 'c) t
536536+537537+ include
538538+ S_any3__value__any__value__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
539539+ end
540540+ end [@@ocaml.doc " @inline "]
541541+542542+ include struct
543543+ module type S_any3__value__any__any__stack = sig
544544+ type ('a, 'b, 'c) t
545545+546546+ val t_of_sexp
547547+ : 'a 'b 'c.
548548+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
549549+550550+ [@@@ocaml.text "/*"]
551551+552552+ val sexp_of_t__stack
553553+ : 'a 'b 'c.
554554+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
555555+556556+ [@@@ocaml.text "/*"]
557557+558558+ val sexp_of_t
559559+ : 'a 'b 'c.
560560+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
561561+ end
562562+563563+ module type S3__value__any__any__stack = sig
564564+ type ('a, 'b, 'c) t
565565+566566+ include
567567+ S_any3__value__any__any__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
568568+ end
569569+ end [@@ocaml.doc " @inline "]
570570+ end [@@ocaml.doc " @inline "]
571571+ end [@@ocaml.doc " @inline "]
572572+573573+ include struct
574574+ module type S_any1__any__stack = sig
575575+ type 'a t
576576+577577+ val t_of_sexp : 'a. (Sexp.t -> 'a) -> Sexp.t -> 'a t
578578+579579+ [@@@ocaml.text "/*"]
580580+581581+ val sexp_of_t__stack : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
582582+583583+ [@@@ocaml.text "/*"]
584584+585585+ val sexp_of_t : 'a. ('a -> Sexp.t) -> 'a t -> Sexp.t
586586+ end
587587+588588+ module type S1__any__stack = sig
589589+ type 'a t
590590+591591+ include S_any1__any__stack with type 'a t := 'a t
592592+ end
593593+594594+ include struct
595595+ module type S_any2__any__value__stack = sig
596596+ type ('a, 'b) t
597597+598598+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
599599+600600+ [@@@ocaml.text "/*"]
601601+602602+ val sexp_of_t__stack
603603+ : 'a 'b.
604604+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
605605+606606+ [@@@ocaml.text "/*"]
607607+608608+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
609609+ end
610610+611611+ module type S2__any__value__stack = sig
612612+ type ('a, 'b) t
613613+614614+ include S_any2__any__value__stack with type ('a, 'b) t := ('a, 'b) t
615615+ end
616616+617617+ include struct
618618+ module type S_any3__any__value__value__stack = sig
619619+ type ('a, 'b, 'c) t
620620+621621+ val t_of_sexp
622622+ : 'a 'b 'c.
623623+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
624624+625625+ [@@@ocaml.text "/*"]
626626+627627+ val sexp_of_t__stack
628628+ : 'a 'b 'c.
629629+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
630630+631631+ [@@@ocaml.text "/*"]
632632+633633+ val sexp_of_t
634634+ : 'a 'b 'c.
635635+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
636636+ end
637637+638638+ module type S3__any__value__value__stack = sig
639639+ type ('a, 'b, 'c) t
640640+641641+ include
642642+ S_any3__any__value__value__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
643643+ end
644644+ end [@@ocaml.doc " @inline "]
645645+646646+ include struct
647647+ module type S_any3__any__value__any__stack = sig
648648+ type ('a, 'b, 'c) t
649649+650650+ val t_of_sexp
651651+ : 'a 'b 'c.
652652+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
653653+654654+ [@@@ocaml.text "/*"]
655655+656656+ val sexp_of_t__stack
657657+ : 'a 'b 'c.
658658+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
659659+660660+ [@@@ocaml.text "/*"]
661661+662662+ val sexp_of_t
663663+ : 'a 'b 'c.
664664+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
665665+ end
666666+667667+ module type S3__any__value__any__stack = sig
668668+ type ('a, 'b, 'c) t
669669+670670+ include
671671+ S_any3__any__value__any__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
672672+ end
673673+ end [@@ocaml.doc " @inline "]
674674+ end [@@ocaml.doc " @inline "]
675675+676676+ include struct
677677+ module type S_any2__any__any__stack = sig
678678+ type ('a, 'b) t
679679+680680+ val t_of_sexp : 'a 'b. (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> Sexp.t -> ('a, 'b) t
681681+682682+ [@@@ocaml.text "/*"]
683683+684684+ val sexp_of_t__stack
685685+ : 'a 'b.
686686+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
687687+688688+ [@@@ocaml.text "/*"]
689689+690690+ val sexp_of_t : 'a 'b. ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('a, 'b) t -> Sexp.t
691691+ end
692692+693693+ module type S2__any__any__stack = sig
694694+ type ('a, 'b) t
695695+696696+ include S_any2__any__any__stack with type ('a, 'b) t := ('a, 'b) t
697697+ end
698698+699699+ include struct
700700+ module type S_any3__any__any__value__stack = sig
701701+ type ('a, 'b, 'c) t
702702+703703+ val t_of_sexp
704704+ : 'a 'b 'c.
705705+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
706706+707707+ [@@@ocaml.text "/*"]
708708+709709+ val sexp_of_t__stack
710710+ : 'a 'b 'c.
711711+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
712712+713713+ [@@@ocaml.text "/*"]
714714+715715+ val sexp_of_t
716716+ : 'a 'b 'c.
717717+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
718718+ end
719719+720720+ module type S3__any__any__value__stack = sig
721721+ type ('a, 'b, 'c) t
722722+723723+ include
724724+ S_any3__any__any__value__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
725725+ end
726726+ end [@@ocaml.doc " @inline "]
727727+728728+ include struct
729729+ module type S_any3__any__any__any__stack = sig
730730+ type ('a, 'b, 'c) t
731731+732732+ val t_of_sexp
733733+ : 'a 'b 'c.
734734+ (Sexp.t -> 'a) -> (Sexp.t -> 'b) -> (Sexp.t -> 'c) -> Sexp.t -> ('a, 'b, 'c) t
735735+736736+ [@@@ocaml.text "/*"]
737737+738738+ val sexp_of_t__stack
739739+ : 'a 'b 'c.
740740+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
741741+742742+ [@@@ocaml.text "/*"]
743743+744744+ val sexp_of_t
745745+ : 'a 'b 'c.
746746+ ('a -> Sexp.t) -> ('b -> Sexp.t) -> ('c -> Sexp.t) -> ('a, 'b, 'c) t -> Sexp.t
747747+ end
748748+749749+ module type S3__any__any__any__stack = sig
750750+ type ('a, 'b, 'c) t
751751+752752+ include S_any3__any__any__any__stack with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
753753+ end
754754+ end [@@ocaml.doc " @inline "]
755755+ end [@@ocaml.doc " @inline "]
756756+ end [@@ocaml.doc " @inline "]
757757+end [@@ocaml.doc " @inline "]
758758+759759+[@@@end]
760760+761761+module type S_with_grammar = sig
762762+ include S
763763+764764+ val t_sexp_grammar : t Sexp_grammar.t
765765+end
766766+767767+module type S1_with_grammar = sig
768768+ include S1
769769+770770+ val t_sexp_grammar : 'a Sexp_grammar.t -> 'a t Sexp_grammar.t
771771+end
772772+773773+module type S2_with_grammar = sig
774774+ include S2
775775+776776+ val t_sexp_grammar : 'a Sexp_grammar.t -> 'b Sexp_grammar.t -> ('a, 'b) t Sexp_grammar.t
777777+end
778778+779779+module type S3_with_grammar = sig
780780+ include S3
781781+782782+ val t_sexp_grammar
783783+ : 'a Sexp_grammar.t
784784+ -> 'b Sexp_grammar.t
785785+ -> 'c Sexp_grammar.t
786786+ -> ('a, 'b, 'c) t Sexp_grammar.t
787787+end
···11+open! StdLabels
22+33+module Buffer = struct
44+ include Buffer
55+66+ external magic_global : 'a -> 'b = "%identity"
77+88+ let add_string t str = Buffer.add_string (magic_global t) (magic_global str)
99+1010+ let blit src srcoff dst dstoff len =
1111+ Buffer.blit (magic_global src) srcoff (magic_global dst) dstoff len
1212+ ;;
1313+end
1414+1515+module Bytes = struct
1616+ include Bytes
1717+1818+ external create__stack : int -> bytes = "caml_create_bytes"
1919+ external unsafe_set : (bytes[@local_opt]) -> int -> char -> unit = "%bytes_unsafe_set"
2020+2121+ external unsafe_to_string
2222+ : (bytes[@local_opt])
2323+ -> (string[@local_opt])
2424+ = "%bytes_to_string"
2525+2626+ external unsafe_blit_string
2727+ : src:(string[@local_opt])
2828+ -> src_pos:int
2929+ -> dst:(bytes[@local_opt])
3030+ -> dst_pos:int
3131+ -> len:int
3232+ -> unit
3333+ = "caml_blit_string"
3434+ [@@noalloc]
3535+end
3636+3737+module String = struct
3838+ include String
3939+4040+ external length : (string[@local_opt]) -> int = "%string_length"
4141+ external get : (string[@local_opt]) -> int -> char = "%string_safe_get"
4242+ external unsafe_get : (string[@local_opt]) -> int -> char = "%string_unsafe_get"
4343+end
+44
vendor/opam/sexplib0/src/stdlib_stubs.mli
···11+open! StdLabels
22+33+module Buffer : sig
44+ include module type of struct
55+ include Buffer
66+ end
77+88+ val add_string : t -> string -> unit
99+ val blit : t -> int -> bytes -> int -> int -> unit
1010+end
1111+1212+module Bytes : sig
1313+ include module type of struct
1414+ include Bytes
1515+ end
1616+1717+ external create__stack : int -> bytes = "caml_create_bytes"
1818+ external unsafe_set : (bytes[@local_opt]) -> int -> char -> unit = "%bytes_unsafe_set"
1919+2020+ external unsafe_to_string
2121+ : (bytes[@local_opt])
2222+ -> (string[@local_opt])
2323+ = "%bytes_to_string"
2424+2525+ external unsafe_blit_string
2626+ : src:(string[@local_opt])
2727+ -> src_pos:int
2828+ -> dst:(bytes[@local_opt])
2929+ -> dst_pos:int
3030+ -> len:int
3131+ -> unit
3232+ = "caml_blit_string"
3333+ [@@noalloc]
3434+end
3535+3636+module String : sig
3737+ include module type of struct
3838+ include String
3939+ end
4040+4141+ external length : (string[@local_opt]) -> int = "%string_length"
4242+ external get : (string[@local_opt]) -> int -> char = "%string_safe_get"
4343+ external unsafe_get : (string[@local_opt]) -> int -> char = "%string_unsafe_get"
4444+end