@@ -4,14 +4,14 @@ open Format
4
4
5
5
type entry = {
6
6
fields : string String.Map .t
7
- } [@@ deriving compare , sexp ]
7
+ } [@@ deriving bin_io , compare , sexp ]
8
8
9
9
type row = {row : entry array } [@@ deriving sexp ]
10
10
type 'a seq = 'a Sequence .t
11
11
12
12
module Type = struct
13
13
type typ = Int | Str | Bool | Float
14
- [@@ deriving compare ,enumerate,sexp]
14
+ [@@ deriving bin_io , compare ,enumerate,sexp]
15
15
type 'a t = {
16
16
parse : string -> 'a option ;
17
17
pack : 'a -> string ;
@@ -26,9 +26,9 @@ module Type = struct
26
26
type header = {
27
27
fname : string ;
28
28
ftype : typ ;
29
- } [@@ deriving compare , sexp ]
29
+ } [@@ deriving bin_io , compare , sexp ]
30
30
31
- type signature = header list [@@ deriving compare , sexp ]
31
+ type signature = header list [@@ deriving bin_io , compare , sexp ]
32
32
33
33
type ('f,'k) scheme = {
34
34
read : entry -> 'a -> 'b option ;
@@ -169,19 +169,22 @@ let declare = Attribute.declare
169
169
170
170
171
171
module Doc = struct
172
+ module Problem = Error
172
173
module Error = Monad.Result. Error
173
174
open Error.Syntax
174
175
175
176
type t = {
176
177
scheme : Type .signature String.Map .t ;
177
178
entries : entry list String.Map .t ;
178
- } [@@ deriving compare ]
179
+ } [@@ deriving bin_io , compare ]
179
180
180
181
let empty = {
181
182
scheme = String.Map. empty;
182
183
entries = String.Map. empty;
183
184
}
184
185
186
+ let is_empty {scheme} = Map. is_empty scheme
187
+
185
188
let errorf fmt = Or_error. errorf fmt
186
189
187
190
let declarations {scheme} = Map. length scheme
@@ -395,6 +398,24 @@ module Doc = struct
395
398
Sexp. scan_sexps (String. strip str |> Lexing. from_string) |>
396
399
of_sexps)
397
400
401
+ let sexps_of_t {scheme; entries} =
402
+ let attrs =
403
+ Map. fold entries ~init: [] ~f: (fun ~key :n ~data :vs sexps ->
404
+ List. fold vs ~init: sexps ~f: (fun sexps v ->
405
+ sexp_of_attr scheme n v :: sexps)) in
406
+ Map. fold scheme ~init: attrs ~f: (fun ~key :n ~data :s xs ->
407
+ sexp_of_decl (n,s) :: xs)
408
+
409
+ let sexp_of_t doc = Sexp. List (sexps_of_t doc)
410
+
411
+ let t_of_sexp = function
412
+ | Sexp. Atom _ -> invalid_arg " Ogre.Doc.t_of_sexp: expects a list"
413
+ | List exp -> match of_sexps exp with
414
+ | Ok x -> x
415
+ | Error err ->
416
+ invalid_argf " Ogre.Doc.t_of_sexp: ill-formed document: %s"
417
+ (Problem. to_string_hum err) ()
418
+
398
419
let to_string x = asprintf " %a" pp x
399
420
end
400
421
0 commit comments