Skip to content

Commit 7ddeba0

Browse files
committed
implements bin_io and sexp protocol for OGRE docs
The sexp is using the OGRE syntax not just a deriviation from the representation.
1 parent 6bcf3c4 commit 7ddeba0

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/ogre/ogre.ml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ open Format
44

55
type entry = {
66
fields : string String.Map.t
7-
} [@@deriving compare, sexp]
7+
} [@@deriving bin_io, compare, sexp]
88

99
type row = {row : entry array} [@@deriving sexp]
1010
type 'a seq = 'a Sequence.t
1111

1212
module Type = struct
1313
type typ = Int | Str | Bool | Float
14-
[@@deriving compare,enumerate,sexp]
14+
[@@deriving bin_io, compare,enumerate,sexp]
1515
type 'a t = {
1616
parse : string -> 'a option;
1717
pack : 'a -> string;
@@ -26,9 +26,9 @@ module Type = struct
2626
type header = {
2727
fname : string;
2828
ftype : typ;
29-
} [@@deriving compare, sexp]
29+
} [@@deriving bin_io, compare, sexp]
3030

31-
type signature = header list [@@deriving compare, sexp]
31+
type signature = header list [@@deriving bin_io, compare, sexp]
3232

3333
type ('f,'k) scheme = {
3434
read : entry -> 'a -> 'b option;
@@ -169,19 +169,22 @@ let declare = Attribute.declare
169169

170170

171171
module Doc = struct
172+
module Problem = Error
172173
module Error = Monad.Result.Error
173174
open Error.Syntax
174175

175176
type t = {
176177
scheme : Type.signature String.Map.t;
177178
entries : entry list String.Map.t;
178-
} [@@deriving compare]
179+
} [@@deriving bin_io, compare]
179180

180181
let empty = {
181182
scheme = String.Map.empty;
182183
entries = String.Map.empty;
183184
}
184185

186+
let is_empty {scheme}= Map.is_empty scheme
187+
185188
let errorf fmt = Or_error.errorf fmt
186189

187190
let declarations {scheme} = Map.length scheme
@@ -395,6 +398,24 @@ module Doc = struct
395398
Sexp.scan_sexps (String.strip str |> Lexing.from_string) |>
396399
of_sexps)
397400

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+
398419
let to_string x = asprintf "%a" pp x
399420
end
400421

lib/ogre/ogre.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,18 @@ end
510510
511511
*)
512512
module Doc : sig
513-
type t = doc [@@deriving compare]
513+
type t = doc [@@deriving bin_io, compare, sexp]
514514

515515

516516
(** [empty] creates an empty document *)
517517
val empty : doc
518518

519519

520+
(** [is_empty x] is true iff [x] is [empty].
521+
@since 2.2.0 *)
522+
val is_empty : doc -> bool
523+
524+
520525
(** [merge d1 d2] merges two documents in one. Returns an error,
521526
if documents contain inconsistent declarations.*)
522527
val merge : doc -> doc -> doc Or_error.t

0 commit comments

Comments
 (0)