Skip to content

Commit 82c8550

Browse files
committed
[ generalities ] Remove Format-based pretty printing
1 parent e026774 commit 82c8550

File tree

6 files changed

+63
-83
lines changed

6 files changed

+63
-83
lines changed

coursework1-marking/main.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,23 @@ let print_err =
277277
| `Not_enough_solutions expected_json ->
278278
[p [txt "Your code does not produce enough solutions. The \
279279
following solution is not generated by your constraints:"]
280-
; code_bl (Generalities.Json.Printing.to_string expected_json)
280+
; code_bl (Generalities.Json.to_string expected_json)
281281
]
282282
| `Too_many_solutions unwanted_json ->
283283
[p [txt "Your code produces too many solutions. The following \
284284
solution is generated by your constraints, but is not \
285285
required by the solution:"]
286-
; code_bl (Generalities.Json.Printing.to_string unwanted_json)
286+
; code_bl (Generalities.Json.to_string unwanted_json)
287287
]
288288
| `Solution_mismatch (expected, submitted) ->
289289
[ p [txt "Your code produces solutions that are not required, and \
290290
misses solutions that are required. The following is an \
291291
example of a solution that your code should produce but \
292292
does not:"]
293-
; code_bl (Generalities.Json.Printing.to_string expected)
293+
; code_bl (Generalities.Json.to_string expected)
294294
; p [txt "This is an example of a solution that you code produces \
295295
but should not:"]
296-
; code_bl (Generalities.Json.Printing.to_string submitted)
296+
; code_bl (Generalities.Json.to_string submitted)
297297
]
298298

299299
let seq_head seq = match seq () with
@@ -335,7 +335,7 @@ let mark_question submission (question_id, question_title, marks, solution) =
335335
None, Error
336336
[ p [txt "The solutions generated by this code are:"]
337337
; code_bl (String.concat "\n"
338-
(List.map Generalities.Json.Printing.to_string jsons))
338+
(List.map Generalities.Json.to_string jsons))
339339
]
340340
| Error err ->
341341
Some 0, Error (print_err err)

generalities/json.ml

+13-63
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module P = struct
4545
|> Seq_ext.intersperse (comma ^^ break)
4646
|> concat
4747
in
48-
(* maybe_flat *) (nest 2 (break ^^ body) ^^ break)
48+
maybe_flat (nest 2 (break ^^ body) ^^ break)
4949

5050
let rec to_document = function
5151
| JNull ->
@@ -67,65 +67,15 @@ module P = struct
6767
fields)
6868
end
6969

70-
module Printing = struct
71-
let pp_comma fmt () = Format.pp_print_string fmt ","
72-
73-
let rec to_string = function
74-
| JString str -> Printf.sprintf "%S" str
75-
| JBool b -> Printf.sprintf "%b" b
76-
| JInt i -> string_of_int i
77-
| JArray jsons ->
78-
"[" ^ String.concat ", " (List.map to_string jsons) ^ "]"
79-
| JNull -> "null"
80-
| JObject obj ->
81-
let field_to_string (nm, json) =
82-
Printf.sprintf "%S: %s" nm (to_string json)
83-
in
84-
"{" ^ String.concat ", " (List.map field_to_string obj) ^ "}"
85-
86-
(* FIXME: move to a Utf8_string module *)
87-
let json_escape fmt s =
88-
let s = Utf8_string.of_string_unsafe s in
89-
(* FIXME *)
90-
let b = Buffer.create (Utf8_string.byte_length s + 4) in
91-
let escape_char c =
92-
if c = Uchar.of_char '"' then Buffer.add_char b '"'
93-
else if c = Uchar.of_char '\\' then Buffer.add_char b '\\'
94-
else if c = Uchar.of_char '\n' then Buffer.add_string b "\\n"
95-
else if c = Uchar.of_char '\x0c' then Buffer.add_string b "\\f"
96-
else if c = Uchar.of_char '\t' then Buffer.add_string b "\\t"
97-
else if c = Uchar.of_char '\r' then Buffer.add_string b "\\r"
98-
else if c = Uchar.of_char '\b' then Buffer.add_string b "\\b"
99-
else Buffer.add_utf_8_uchar b c
100-
in
101-
Utf8_string.iter escape_char s;
102-
Format.pp_print_string fmt (Buffer.contents b)
103-
104-
let pp_string fmt s =
105-
Format.fprintf fmt "\"%a\"" json_escape s
106-
let pp_delim =
107-
Format.pp_print_string
108-
109-
let rec pp fmt = function
110-
| JString s -> pp_string fmt s
111-
| JBool true -> Format.pp_print_string fmt "true"
112-
| JBool false -> Format.pp_print_string fmt "false"
113-
| JInt i -> Format.pp_print_int fmt i
114-
(* | Float f -> Format.pp_print_float fmt f (* FIXME: proper format *) *)
115-
| JNull -> Format.pp_print_string fmt "null"
116-
| JArray [] ->
117-
Format.pp_print_string fmt "[]"
118-
| JArray elems ->
119-
Format.fprintf fmt "%a@,@[<v2> %a@]@,%a"
120-
pp_delim "["
121-
(Format.pp_print_list ~pp_sep:pp_comma pp) elems
122-
pp_delim "]"
123-
| JObject [] ->
124-
Format.pp_print_string fmt "{}"
125-
| JObject fields ->
126-
Format.fprintf fmt "%a@,@[<v2> %a@]@,%a" pp_delim "{"
127-
(Format.pp_print_list ~pp_sep:pp_comma pp_field)
128-
fields pp_delim "}"
129-
130-
and pp_field fmt (nm, json) = Format.fprintf fmt "%a: %a" pp_string nm pp json
131-
end
70+
let rec to_string = function
71+
| JString str -> Printf.sprintf "%S" str
72+
| JBool b -> Printf.sprintf "%b" b
73+
| JInt i -> string_of_int i
74+
| JArray jsons ->
75+
"[" ^ String.concat ", " (List.map to_string jsons) ^ "]"
76+
| JNull -> "null"
77+
| JObject obj ->
78+
let field_to_string (nm, json) =
79+
Printf.sprintf "%S: %s" nm (to_string json)
80+
in
81+
"{" ^ String.concat ", " (List.map field_to_string obj) ^ "}"

generalities/pretty.ml

+33-8
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,26 @@ let group doc =
9393
(******************************************************************************)
9494
(* Output of documents *)
9595

96-
let emit =
97-
print_string
98-
let emit_newline =
99-
print_newline
100-
let emit_spaces n =
101-
print_string (String.make n ' ')
102-
103-
let output ?(width=80) document =
96+
type output =
97+
{ emit : string -> unit
98+
; emit_newline : unit -> unit
99+
; emit_spaces : int -> unit
100+
}
101+
102+
let output_of_channel ch =
103+
{ emit = Out_channel.output_string ch
104+
; emit_newline = (fun () -> Out_channel.output_char ch '\n')
105+
; emit_spaces = (fun n -> Out_channel.output_string ch (String.make n ' '))
106+
}
107+
108+
let output_of_buffer b =
109+
{ emit = Buffer.add_string b
110+
; emit_newline = (fun () -> Buffer.add_string b "\n")
111+
; emit_spaces = (fun n -> Buffer.add_string b (String.make n ' '))
112+
}
113+
114+
let output ?(width=80) output document =
115+
let { emit; emit_newline; emit_spaces } = output in
104116
let rec flat = function
105117
| Empty | AlignSpaces _ ->
106118
()
@@ -139,3 +151,16 @@ let output ?(width=80) document =
139151
in
140152
let _ = process 0 0 0 document.doc in
141153
()
154+
155+
let print ?width document =
156+
let o = output_of_channel Out_channel.stdout in
157+
output ?width o document
158+
159+
let to_buffer ?width b document =
160+
let o = output_of_buffer b in
161+
output ?width o document
162+
163+
let to_string ?width document =
164+
let b = Buffer.create 8192 in
165+
to_buffer ?width b document;
166+
Buffer.contents b

generalities/pretty.mli

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ val break : document
2121
3. flat output
2222
4. typed document constructor combinators
2323
*)
24-
val output : ?width:int -> document -> unit
24+
val print : ?width:int -> document -> unit
25+
26+
val to_string : ?width:int -> document -> string
27+
28+
val to_buffer : ?width:int -> Buffer.t -> document -> unit

slakemoth/bin/main.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let handle_errors = function
66
| Ok () ->
77
exit 0
88
| Error (`Parse err) ->
9-
Format.eprintf "ERROR: %s\n" (Parser_util.Driver.string_of_error err);
9+
Printf.eprintf "ERROR: %s\n" (Parser_util.Driver.string_of_error err);
1010
exit 1
1111
| Error (`Type_error (location, msg)) ->
1212
let msg = Printf.sprintf "Problem at %a: %s"
@@ -26,7 +26,8 @@ let execute filename =
2626
commands
2727
|> List.to_seq
2828
|> Seq.concat_map Evaluator.execute_command
29-
|> Seq.iter (Format.printf "@[<v0>%a@]@\n" Json.Printing.pp);
29+
|> Seq.iter (fun json -> Pretty.print (Json.P.to_document json);
30+
print_newline ());
3031
Result.ok ()
3132

3233
let pretty_print filename =

slakemoth/widget/slakemoth_widget.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let component configuration =
44

55
module Ast = Slakemoth.Ast
66
open Slakemoth.Environment
7+
open Generalities
78

89
type state = {
910
input : string;
@@ -150,13 +151,12 @@ let component configuration =
150151
(match state.parse_result with
151152
| Ok commands ->
152153
let b = Buffer.create 8192 in
153-
let fmt = Format.formatter_of_buffer b in
154154
commands
155155
|> List.to_seq
156156
|> Seq.concat_map Slakemoth.Evaluator.execute_command
157-
|> Seq.iter (Format.fprintf fmt "@[<v0>%a@]@\n"
158-
Generalities.Json.Printing.pp);
159-
Format.pp_print_flush fmt ();
157+
|> Seq.iter (fun json ->
158+
Pretty.to_buffer ~width:50 b (Json.P.to_document json);
159+
Buffer.add_string b "\n");
160160
{ state with fresh = true; output = `String (Buffer.contents b) }
161161
| Error _ ->
162162
(* Button should be disabled to prevent this *)

0 commit comments

Comments
 (0)