Skip to content

Commit

Permalink
Fix Kappa-Dev#644: Use Buffer.t instead of Bi_outbuf.t
Browse files Browse the repository at this point in the history
This should make Kappa compatible with the latest version of yojson.

Since ocaml-community/yojson#74, yojson does
not rely on biniou buffers.

The main drawback seems to be that `JsonUtil.write_to_channel` now
writes everything to the memory (in a `Buffer.t`) before dumping the
buffer to the channel.
  • Loading branch information
thierry-martinez committed Oct 19, 2022
1 parent b4fd715 commit 90a9290
Show file tree
Hide file tree
Showing 26 changed files with 116 additions and 114 deletions.
6 changes: 3 additions & 3 deletions core/KaSa_rep/frontend/ckappa_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ let rule_id_of_json json =
raise (Yojson.Basic.Util.Type_error (JsonUtil.build_msg "rule id",json))

let write_c_rule_id ob f =
Yojson.Basic.to_outbuf ob (rule_id_to_json f)
Yojson.Basic.to_buffer ob (rule_id_to_json f)

let string_of_c_rule_id ?(len = 1024) x =
let ob = Bi_outbuf.create len in
let ob = Buffer.create len in
write_c_rule_id ob x;
Bi_outbuf.contents ob
Buffer.contents ob

let read_c_rule_id p lb =
rule_id_of_json (Yojson.Basic.from_lexbuf ~stream:true p lb)
Expand Down
2 changes: 1 addition & 1 deletion core/KaSa_rep/frontend/ckappa_sig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type c_counter_name

val rule_id_to_json : c_rule_id -> Yojson.Basic.t
val rule_id_of_json : Yojson.Basic.t -> c_rule_id
val write_c_rule_id : Bi_outbuf.t -> c_rule_id -> unit
val write_c_rule_id : Buffer.t -> c_rule_id -> unit
val string_of_c_rule_id : ?len:int -> c_rule_id -> string
val read_c_rule_id : Yojson.Safe.lexer_state -> Lexing.lexbuf -> c_rule_id
val c_rule_id_of_string : string -> c_rule_id
Expand Down
30 changes: 16 additions & 14 deletions core/dataStructures/jsonUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
(* |_|\_\ * GNU Lesser General Public License Version 3 *)
(******************************************************************************)

let initial_buffer_size = 0x1000

let write_to_channel f d x =
let b = Bi_outbuf.create_channel_writer d in
let b = Buffer.create initial_buffer_size in
let () = f b x in
Bi_outbuf.flush_channel_writer b
Buffer.output_buffer d b

let string_of_write f ?(len = 1024) x =
let ob = Bi_outbuf.create len in
let ob = Buffer.create len in
let () = f ob x in
Bi_outbuf.contents ob
Buffer.contents ob

let read_of_string f x =
let lex_st = Yojson.Basic.init_lexer () in
Expand Down Expand Up @@ -89,18 +91,18 @@ let to_list ?error_msg:(error_msg=build_msg "list") of_json = function
| `Null -> []
| x -> raise (Yojson.Basic.Util.Type_error (error_msg,x))

let write_comma ob = Bi_outbuf.add_char ob ','
let write_comma ob = Buffer.add_char ob ','

let rec iter2 f_elt x = function
| [] -> ()
| y :: l -> write_comma x; f_elt x y; iter2 f_elt x l

let write_list f ob l =
let () = Bi_outbuf.add_char ob '[' in
let () = Buffer.add_char ob '[' in
let () = match l with
| [] -> ()
| y :: l -> f ob y; iter2 f ob l in
Bi_outbuf.add_char ob ']'
Buffer.add_char ob ']'

let of_array to_json a =
`List (Array.fold_right (fun x acc -> to_json x::acc) a [])
Expand All @@ -111,12 +113,12 @@ let to_array ?error_msg:(error_msg=build_msg "array") of_json = function
| x -> raise (Yojson.Basic.Util.Type_error (error_msg,x))

let write_array f ob l =
let () = Bi_outbuf.add_char ob '[' in
let () = Buffer.add_char ob '[' in
let () = if Array.length l > 0 then f ob l.(0) in
let () = Tools.iteri
(fun i -> let () = write_comma ob in f ob l.(succ i))
(pred (Array.length l)) in
Bi_outbuf.add_char ob ']'
Buffer.add_char ob ']'

let rec iter_seq ob = function
| [] -> ()
Expand All @@ -126,11 +128,11 @@ let rec iter_seq ob = function
iter_seq ob q

let write_sequence ob l =
let () = Bi_outbuf.add_char ob '[' in
let () = Buffer.add_char ob '[' in
let () = match l with
| [] -> ()
| f::q -> let () = f ob in iter_seq ob q in
Bi_outbuf.add_char ob ']'
Buffer.add_char ob ']'

let read_variant read_id read st b =
let () = Yojson.Basic.read_lbr st b in
Expand Down Expand Up @@ -164,7 +166,7 @@ let to_assoc

let write_field na f ob x =
let () = Yojson.Basic.write_string ob na in
let () = Bi_outbuf.add_char ob ':' in
let () = Buffer.add_char ob ':' in
f ob x

let of_pair ?(lab1="first") ?(lab2="second") to_json1 to_json2 (a,b) =
Expand Down Expand Up @@ -206,11 +208,11 @@ let to_pair ?lab1:(lab1="first") ?lab2:(lab2="second")
raise (Yojson.Basic.Util.Type_error (error_msg,x))

let write_compact_pair f g ob (x,y) =
let () = Bi_outbuf.add_char ob '[' in
let () = Buffer.add_char ob '[' in
let () = f ob x in
let () = write_comma ob in
let () = g ob y in
Bi_outbuf.add_char ob ']'
Buffer.add_char ob ']'

let read_compact_pair f g st b =
let () = Yojson.Basic.read_lbr st b in
Expand Down
20 changes: 10 additions & 10 deletions core/dataStructures/jsonUtil.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

(** Parsing utils *)

val write_to_channel: (Bi_outbuf.t -> 'a -> unit) -> out_channel -> 'a -> unit
val string_of_write: (Bi_outbuf.t -> 'a -> unit) -> ?len:int -> 'a -> string
val write_to_channel: (Buffer.t -> 'a -> unit) -> out_channel -> 'a -> unit
val string_of_write: (Buffer.t -> 'a -> unit) -> ?len:int -> 'a -> string

val read_of_string:
(Yojson.Basic.lexer_state -> Lexing.lexbuf -> 'a) -> string -> 'a
Expand All @@ -22,7 +22,7 @@ val read_next_item :
(Yojson.Basic.lexer_state -> Lexing.lexbuf -> 'a) ->
(Yojson.Basic.lexer_state -> Lexing.lexbuf -> 'a)

val write_comma: Bi_outbuf.t -> unit
val write_comma: Buffer.t -> unit

(** Jsonify simple types *)

Expand All @@ -46,7 +46,7 @@ val to_option: (Yojson.Basic.t -> 'a) -> Yojson.Basic.t -> 'a option
(** Beware: `Null is reserved for None *)

val write_option:
(Bi_outbuf.t -> 'a -> unit) -> Bi_outbuf.t -> 'a option -> unit
(Buffer.t -> 'a -> unit) -> Buffer.t -> 'a option -> unit

val read_option:
(Yojson.Basic.lexer_state -> Lexing.lexbuf -> 'a) ->
Expand All @@ -57,16 +57,16 @@ val of_list: ('a -> Yojson.Basic.t) -> 'a list -> Yojson.Basic.t
val to_list:
?error_msg:string -> (Yojson.Basic.t -> 'a) -> Yojson.Basic.t -> 'a list

val write_list: (Bi_outbuf.t -> 'a -> unit) -> Bi_outbuf.t -> 'a list -> unit
val write_list: (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a list -> unit

val of_array: ('a -> Yojson.Basic.t) -> 'a array -> Yojson.Basic.t

val to_array:
?error_msg:string -> (Yojson.Basic.t -> 'a) -> Yojson.Basic.t -> 'a array

val write_array: (Bi_outbuf.t -> 'a -> unit) -> Bi_outbuf.t -> 'a array -> unit
val write_array: (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a array -> unit

val write_sequence: Bi_outbuf.t -> (Bi_outbuf.t -> unit) list -> unit
val write_sequence: Buffer.t -> (Buffer.t -> unit) list -> unit

val read_variant:
(Yojson.Basic.lexer_state -> Lexing.lexbuf -> 'a) ->
Expand All @@ -84,7 +84,7 @@ val to_assoc:
Yojson.Basic.t -> 'a list

val write_field:
string -> (Bi_outbuf.t -> 'a -> unit) -> Bi_outbuf.t -> 'a -> unit
string -> (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a -> unit

val of_pair:
?lab1:string -> ?lab2:string ->
Expand All @@ -97,8 +97,8 @@ val to_pair:
Yojson.Basic.t -> 'a * 'b

val write_compact_pair:
(Bi_outbuf.t -> 'a -> unit) -> (Bi_outbuf.t -> 'b -> unit) ->
Bi_outbuf.t -> 'a * 'b -> unit
(Buffer.t -> 'a -> unit) -> (Buffer.t -> 'b -> unit) ->
Buffer.t -> 'a * 'b -> unit

val read_compact_pair:
(Yojson.Basic.lexer_state -> Lexing.lexbuf -> 'a) ->
Expand Down
6 changes: 3 additions & 3 deletions core/dataStructures/locality.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ let annot_of_yojson ?filenames f = function
| `Assoc [ "val", x ] -> (f x, dummy)
| x -> raise (Yojson.Basic.Util.Type_error ("Invalid location",x))

let write_range ob f = Yojson.Basic.to_outbuf ob (to_compact_yojson None f)
let write_range ob f = Yojson.Basic.to_buffer ob (to_compact_yojson None f)

let string_of_range ?(len = 1024) x =
let ob = Bi_outbuf.create len in
let ob = Buffer.create len in
write_range ob x;
Bi_outbuf.contents ob
Buffer.contents ob

let read_range p lb =
of_compact_yojson
Expand Down
4 changes: 2 additions & 2 deletions core/dataStructures/locality.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ val annot_to_yojson :
?filenames : int Mods.StringMap.t ->
('a -> Yojson.Basic.t) -> 'a annot -> Yojson.Basic.t

val write_position : Bi_outbuf.t -> position -> unit
val write_position : Buffer.t -> position -> unit

val read_position :
Yojson.Safe.lexer_state -> Lexing.lexbuf -> position

val write_range : Bi_outbuf.t -> t -> unit
val write_range : Buffer.t -> t -> unit
(** Output a JSON value of type {!t}. *)

val string_of_range : ?len:int -> t -> string
Expand Down
6 changes: 3 additions & 3 deletions core/dataStructures/nbr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ let of_yojson = function
| x -> raise (Yojson.Basic.Util.Type_error ("Not an Nbr",x))

let write_t ob f =
Yojson.Basic.to_outbuf ob (to_yojson f)
Yojson.Basic.to_buffer ob (to_yojson f)

let string_of_t ?(len = 1024) x =
let ob = Bi_outbuf.create len in
let ob = Buffer.create len in
write_t ob x;
Bi_outbuf.contents ob
Buffer.contents ob

let read_t p lb =
of_yojson (Yojson.Basic.from_lexbuf ~stream:true p lb)
Expand Down
2 changes: 1 addition & 1 deletion core/dataStructures/nbr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ val to_yojson : t -> Yojson.Basic.t
val of_yojson : Yojson.Basic.t -> t
(** @raise Yojson.Basic.Util.Type_error if incorrect *)

val write_t : Bi_outbuf.t -> t -> unit
val write_t : Buffer.t -> t -> unit
(** Output a JSON value of type {!t}. *)

val string_of_t : ?len:int -> t -> string
Expand Down
44 changes: 22 additions & 22 deletions core/dataStructures/result_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type ('a,'b) t = {
}

let write_severity ob x =
let () = Bi_outbuf.add_char ob '"' in
let () = Bi_outbuf.add_string ob (Logs.level_to_string (Some x)) in
Bi_outbuf.add_char ob '"'
let () = Buffer.add_char ob '"' in
let () = Buffer.add_string ob (Logs.level_to_string (Some x)) in
Buffer.add_char ob '"'

let read_severity p lb =
match Logs.level_of_string (Yojson.Basic.read_string p lb) with
Expand All @@ -36,13 +36,13 @@ let read_severity p lb =
raise (Yojson.Json_error ("While reading severity: "^x))

let write_status ob = function
| `OK -> Bi_outbuf.add_string ob "200"
| `Accepted -> Bi_outbuf.add_string ob "202"
| `Created -> Bi_outbuf.add_string ob "201"
| `Bad_request -> Bi_outbuf.add_string ob "400"
| `Conflict -> Bi_outbuf.add_string ob "409"
| `Not_found -> Bi_outbuf.add_string ob "404"
| `Request_timeout -> Bi_outbuf.add_string ob "408"
| `OK -> Buffer.add_string ob "200"
| `Accepted -> Buffer.add_string ob "202"
| `Created -> Buffer.add_string ob "201"
| `Bad_request -> Buffer.add_string ob "400"
| `Conflict -> Buffer.add_string ob "409"
| `Not_found -> Buffer.add_string ob "404"
| `Request_timeout -> Buffer.add_string ob "408"

let read_status p lb =
match Yojson.Basic.read_int p lb with
Expand All @@ -57,7 +57,7 @@ let read_status p lb =
("Status "^string_of_int x^" is out of the scope of Kappa"))

let write_message ob { severity; text; range } =
let () = Bi_outbuf.add_char ob '{' in
let () = Buffer.add_char ob '{' in
let () = JsonUtil.write_field "severity" write_severity ob severity in
let () = JsonUtil.write_comma ob in
let () = JsonUtil.write_field "text" Yojson.Basic.write_string ob text in
Expand All @@ -66,7 +66,7 @@ let write_message ob { severity; text; range } =
| Some r ->
let () = JsonUtil.write_comma ob in
JsonUtil.write_field "range" Locality.write_range ob r in
Bi_outbuf.add_char ob '}'
Buffer.add_char ob '}'

let read_message p lb =
let (severity,text,range) =
Expand All @@ -87,25 +87,25 @@ let print_message f { range; text; _ } =

let write_t write__ok write__error = fun ob -> function
| { value = Result.Ok x; status; messages } ->
Bi_outbuf.add_string ob "[\"Ok\",";
Buffer.add_string ob "[\"Ok\",";
write__ok ob x;
Bi_outbuf.add_char ob ',';
Buffer.add_char ob ',';
write_status ob status;
Bi_outbuf.add_char ob ',';
Buffer.add_char ob ',';
JsonUtil.write_list write_message ob messages;
Bi_outbuf.add_char ob ']'
Buffer.add_char ob ']'
| { value = Result.Error x; status; messages } ->
Bi_outbuf.add_string ob "[\"Error\",";
Buffer.add_string ob "[\"Error\",";
write__error ob x;
Bi_outbuf.add_char ob ',';
Buffer.add_char ob ',';
write_status ob status;
Bi_outbuf.add_char ob ',';
Buffer.add_char ob ',';
JsonUtil.write_list write_message ob messages;
Bi_outbuf.add_char ob ']'
Buffer.add_char ob ']'
let string_of_t write__ok write__error ?(len = 1024) x =
let ob = Bi_outbuf.create len in
let ob = Buffer.create len in
write_t write__ok write__error ob x;
Bi_outbuf.contents ob
Buffer.contents ob

let read_t_content f p lb =
let v = f p lb in
Expand Down
12 changes: 6 additions & 6 deletions core/dataStructures/result_util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ type ('a,'b) t = {
messages : message list;
}

val write_message : Bi_outbuf.t -> message -> unit
val write_message : Buffer.t -> message -> unit

val read_message : Yojson.Safe.lexer_state -> Lexing.lexbuf -> message

val print_message : Format.formatter -> message -> unit

val write_t :
(Bi_outbuf.t -> 'ok -> unit) ->
(Bi_outbuf.t -> 'error -> unit) ->
Bi_outbuf.t -> ('ok, 'error) t -> unit
(Buffer.t -> 'ok -> unit) ->
(Buffer.t -> 'error -> unit) ->
Buffer.t -> ('ok, 'error) t -> unit
(** Output a JSON value of type {!t}. *)

val string_of_t :
(Bi_outbuf.t -> 'ok -> unit) ->
(Bi_outbuf.t -> 'error -> unit) ->
(Buffer.t -> 'ok -> unit) ->
(Buffer.t -> 'error -> unit) ->
?len:int -> ('ok, 'error) t -> string
(** Serialize a value of type {!t} into a JSON string. @param len
specifies the initial length of the buffer used internally.
Expand Down
2 changes: 1 addition & 1 deletion core/grammar/ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ val to_created_mixture : mixture -> mixture
val compil_of_json : Yojson.Basic.t -> parsing_compil
val compil_to_json : parsing_compil -> Yojson.Basic.t

val write_parsing_compil : Bi_outbuf.t -> parsing_compil -> unit
val write_parsing_compil : Buffer.t -> parsing_compil -> unit
val read_parsing_compil : Yojson.lexer_state -> Lexing.lexbuf -> parsing_compil
4 changes: 2 additions & 2 deletions core/grammar/kfiles.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type catalog_item = {
}

let write_catalog_item ob { position; id } =
let () = Bi_outbuf.add_char ob '{' in
let () = Buffer.add_char ob '{' in
let () = JsonUtil.write_field "id" Yojson.Basic.write_string ob id in
let () = JsonUtil.write_comma ob in
let () = JsonUtil.write_field "position" Yojson.Basic.write_int ob position in
Bi_outbuf.add_char ob '}'
Buffer.add_char ob '}'

let read_catalog_item p lb =
let (position,id,count) =
Expand Down
2 changes: 1 addition & 1 deletion core/grammar/kfiles.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type catalog_item = {
id : string;
}

val write_catalog_item : Bi_outbuf.t -> catalog_item -> unit
val write_catalog_item : Buffer.t -> catalog_item -> unit
val read_catalog_item : Yojson.lexer_state -> Lexing.lexbuf -> catalog_item

val create : unit -> catalog
Expand Down
Loading

0 comments on commit 90a9290

Please sign in to comment.