File: simple_example.ml

package info (click to toggle)
easy-format 1.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: ml: 1,223; makefile: 15
file content (330 lines) | stat: -rw-r--r-- 9,711 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
(*
  A fairly complete demonstration of the features provided
  by Easy-format.
*)


open Easy_format


let list =
  { list with
      list_style = Some "list";
      opening_style = Some "op";
      body_style = Some "body";
      separator_style = Some "sep";
      closing_style = Some "cl"
  }
let atom = { atom_style = Some "atom" }
let label = { label with label_style = Some "label" }



let tuple_param =
  { list with
      space_after_opening = false;
      space_before_closing = false;
      align_closing = false
  }

let operator_param =
  { list with
      space_after_opening = false;
      space_before_closing = false;
      separators_stick_left = false;
      space_before_separator = true;
      space_after_separator = true;
      align_closing = true
  }


let html_escape_string s =
  let buf = Buffer.create (2 * String.length s) in
  for i = 0 to String.length s - 1 do
    match s.[i] with
	'&' -> Buffer.add_string buf "&"
      | '<' -> Buffer.add_string buf "&lt;"
      | '>' -> Buffer.add_string buf "&gt;"
      | c -> Buffer.add_char buf c
  done;
  Buffer.contents buf

let html_escape = `Escape_string html_escape_string
let html_style = [
  "atom", { tag_open = "<a>"; tag_close = "</a>" };
  "body", { tag_open = "<lb>"; tag_close = "</lb>" };
  "list", { tag_open = "<l>"; tag_close = "</l>" };
  "op", { tag_open = "<op>"; tag_close = "</op>" };
  "cl", { tag_open = "<cl>"; tag_close = "</cl>" };
  "sep", { tag_open = "<sep>"; tag_close = "</sep>" };
  "label", { tag_open = "<la>"; tag_close = "</la>" };
]



let format_tuple f l =
  List (("(", ",", ")", tuple_param), List.map f l)

let format_int x =
  Atom (string_of_int x, atom)

let format_float x =
  Atom (Printf.sprintf "%.5f" x, atom)

let format_sum ?(wrap = `Wrap_atoms) l =
  List (("(", "+", ")", { operator_param with wrap_body = wrap }),
	List.map format_int l)

let format_array ~align_closing ~wrap f a =
  let l = Array.to_list (Array.map f a) in
  List (("[|", ";", "|]",
	 { list with
	     align_closing = align_closing;
	     wrap_body = wrap }),
	l)

let format_matrix
    ?(align_closing1 = true)
    ?(align_closing2 = true)
    ?(wrap1 = `Wrap_atoms)
    ?(wrap2 = `Wrap_atoms)
    m =
  format_array ~align_closing: align_closing1 ~wrap: wrap1
    (format_array ~align_closing: align_closing2 ~wrap: wrap2 format_float) m


let _format_record f l0 =
  let l =
    List.map
      (fun (s, x) -> Label ((Atom (s ^ ":", atom), label), f x))
      l0 in
  List (("{", ";", "}", list), l)

let begin_style =
  { label with indent_after_label = 0 },
  ("begin", ";", "end",
   { list with stick_to_label = false })

let curly_style =
  label,
  ("{", ";", "}", list)

let format_function_definition (body_label, body_param) name param body =
  Label (
    (
      Label (
	(Atom ("function " ^ name, atom), label),
	List (("(", ",", ")", tuple_param),
	      List.map (fun s -> Atom (s, atom)) param)
      ),
      body_label
    ),
    List (body_param, List.map (fun s -> Atom (s, atom)) body)
  )

(*
   Illustrate the difference between `Force_break and `Force_breaks_rec on
   labels.
*)
let label_one_atom = Atom ("reallyLongLabelOne", atom)
let label_two_atom = Atom ("reallyLongLabelTwo", atom)
let label_three_atom = Atom ("reallyLongLabelABC", atom)
let make_list_in_labels (wrap) =
  Label (
    (label_one_atom, label),
    (
      Label (
        (label_two_atom, label),
        (
          Label (
            (label_three_atom, label),
            List (
              ("[", ",", "]", { list with wrap_body = wrap }),
              [
                Atom ("1.23456", atom);
                Atom ("9.87654", atom);
                Atom ("9.87654", atom);
                Atom ("9.87654", atom);
              ]
            )
          )
        )
      )
    )
  )

(*
   Illustrate the difference between `Force_break and `Force_breaks_rec
*)
let make_heterogenous_list (container_wrap, wrap) =
  List (
    ("[", ",", "]", { list with wrap_body = container_wrap }),
    [
      Atom ("0", atom);
      List (
        ("[", ",", "]", { list with wrap_body = wrap }),
        [
          Atom ("1.23456", atom);
          Atom ("9.87654", atom);
        ]
      );
      Atom ("1", atom);
      Atom ("2", atom);
      Atom ("3", atom);
    ]
  )

let print_margin fmt () =
  let margin = Format.pp_get_margin fmt () in
  for _ = 1 to margin do
    print_char '+'
  done;
  print_newline ()


let with_margin ?(html = false) margin f x =
  let fmt = Format.formatter_of_out_channel stdout in
  Format.pp_set_margin fmt margin;
  if html then
    Pretty.define_styles fmt html_escape html_style;
  print_margin fmt ();
  f fmt x;
  Format.pp_print_flush fmt ();
  print_newline ()

let print s =
  Printf.printf "\n*** %s ***\n%!" s

let print_tuple fmt l =
  Pretty.to_formatter fmt (format_tuple format_int l)

let print_heterogenous_list fmt wrap =
  Pretty.to_formatter fmt (make_heterogenous_list wrap)

let print_list_in_labels fmt wrap =
  Pretty.to_formatter fmt (make_list_in_labels wrap)

let print_sum ?wrap fmt l =
  Pretty.to_formatter fmt (format_sum ?wrap l)

let print_matrix ?align_closing1 ?align_closing2 ?wrap1 ?wrap2 m fmt () =
  Pretty.to_formatter fmt
    (format_matrix ?align_closing1 ?align_closing2 ?wrap1 ?wrap2 m)

let print_function_definition style name param fmt body =
  Pretty.to_formatter fmt (format_function_definition style name param body)

let () =
  let ints = Array.to_list (Array.init 10 (fun i -> i)) in

  (* A simple tuple that fits on one line *)
  with_margin 80 print_tuple ints;
  with_margin 20 print_tuple ints;

  (* Printed as a sum *)
  with_margin 80 print_sum ints;
  with_margin ~html:true 80 print_sum ints;
  with_margin 20 (print_sum ~wrap:`Always_wrap) ints;
  with_margin 20 (print_sum ~wrap:`Never_wrap) ints;

  (* Heterogenous list *)
  print "wrappable outer list, inner list using `Force_breaks";
  with_margin 80 print_heterogenous_list (`Always_wrap, `Force_breaks);
  with_margin 20 print_heterogenous_list (`Always_wrap, `Force_breaks);

  print "wrappable outer list, inner list using `Force_breaks_rec";
  with_margin 80 print_heterogenous_list (`Always_wrap, `Force_breaks_rec);
  with_margin 20 print_heterogenous_list (`Always_wrap, `Force_breaks_rec);

  print "never_wrap outer list, inner list using `Force_breaks";
  with_margin 80 print_heterogenous_list (`Never_wrap, `Force_breaks);
  with_margin 20 print_heterogenous_list (`Never_wrap, `Force_breaks);

  print "never_wrap outer list, inner list using `Force_breaks_rec";
  with_margin 80 print_heterogenous_list (`Never_wrap, `Force_breaks_rec);
  with_margin 20 print_heterogenous_list (`Never_wrap, `Force_breaks_rec);

  print "no breaks outer list, inner list using `Force_breaks";
  with_margin 80 print_heterogenous_list (`No_breaks, `Force_breaks);
  with_margin 20 print_heterogenous_list (`No_breaks, `Force_breaks);

  print "no breaks outer list, inner list using `Force_breaks_rec";
  with_margin 80 print_heterogenous_list (`No_breaks, `Force_breaks_rec);
  with_margin 20 print_heterogenous_list (`No_breaks, `Force_breaks_rec);

  print "label with inner list using `Force_breaks_rec";
  with_margin 80 print_list_in_labels (`Force_breaks_rec);
  with_margin 70 print_list_in_labels (`Force_breaks_rec);
  with_margin 20 print_list_in_labels (`Force_breaks_rec);

  print "label with inner list using `Force_breaks";
  with_margin 80 print_list_in_labels (`Force_breaks);
  with_margin 70 print_list_in_labels (`Force_breaks);
  with_margin 20 print_list_in_labels (`Force_breaks);

  print "label with inner list using `Never_wrap";
  with_margin 80 print_list_in_labels (`Never_wrap);
  with_margin 70 print_list_in_labels (`Never_wrap);
  with_margin 20 print_list_in_labels (`Never_wrap);

  (* Triangular array of arrays showing wrapping of lists of atoms *)
  let m = Array.init 20 (fun i -> Array.init i (fun i -> sqrt (float i))) in

  (* Default style *)
  print "default style";
  with_margin 80 (print_matrix m) ();

  (* Other styles *)
  print "style 1";
  with_margin 80 (print_matrix
		    ~align_closing1: false ~align_closing2: false m) ();
  print "style 2";
  with_margin 80 (print_matrix
		    ~align_closing1: false ~align_closing2: false
		    ~wrap2: `Never_wrap m) ();
  print "style 3";
  with_margin 80 (print_matrix
		    ~align_closing1: false ~align_closing2: false
		    ~wrap2: `Always_wrap m) ();
  print "style 4";
  with_margin 80 (print_matrix
		    ~align_closing2: false
		    ~wrap1: `Always_wrap ~wrap2: `Always_wrap m) ();
  print "style 5";
  with_margin 80 (print_matrix
		    ~align_closing1: false
		    ~wrap1: `Always_wrap ~wrap2: `Always_wrap m) ();
  print "style 6";
  with_margin 80 (print_matrix ~wrap2: `Force_breaks m) ();
  print "style 7";
  with_margin 80 (print_matrix ~wrap1: `Always_wrap ~wrap2: `No_breaks m) ();
  print "style 8";
  with_margin 80 (print_matrix ~wrap2: `No_breaks m) ();
  print "style 9";
  with_margin 80 (print_matrix ~wrap1: `No_breaks m) ();
  print "style 10";
  with_margin 80 (print_matrix ~wrap1: `No_breaks ~wrap2: `Force_breaks m) ();
  print "style 11";
  with_margin 80 (print_matrix ~wrap2: `Never_wrap m) ();


  (* A function definition, showed with different right-margin settings
     and either begin-end or { } around the function body. *)
  let program html margin style =
    with_margin ~html margin
      (print_function_definition
	 style
	 "hello" ["arg1";"arg2";"arg3"])
      [
	"print \"hello\"";
	"return (1 < 2)"
      ]
  in
  List.iter (
    fun style ->
      List.iter (
	fun margin ->
	  program false margin style;
	  program true margin style
      ) [ 10; 20; 30; 40; 80 ]
  ) [ curly_style; begin_style ]