package alba

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file pretty_printer.ml

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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
open Common


module type PRINTER =
  sig
    type t
    val empty: t
    val (<+>): t -> t -> t
    val char: char -> t
    val substring: string -> int -> int -> t
    val fill: int -> char -> t
  end





module type SIG =
  sig
    type t
    val empty: t
    val substring: string -> int -> int -> t
    val string: string -> t
    val char: char -> t
    val fill: int -> char -> t
    val line: string -> t
    val cut: t
    val space: t
    val nest: int -> t -> t
    val nest_list: int -> t list -> t
    val nest_relative: int -> t -> t
    val group: t -> t
    val group_list: t list -> t
    val wrap_words: string -> t
    val fill_paragraph: string -> t
    val (<+>): t -> t -> t
    val chain: t list -> t
    val chain_separated: t list -> t -> t
    val list_separated: t -> t list -> t
  end




module Text =
  struct
    type t =
      | String of string * int * int
      | Fill of int * char
      | Char of char
    let string s i l =
      assert (0 <= i);
      assert (0 <= l);
      assert (i + l <= String.length s);
      String (s,i,l)
    let char c = Char c
    let fill n c = Fill (n,c)
    let length = function
      | String (_,_,l) -> l
      | Fill (n,_) -> n
      | Char _ -> 1
    let apply
          (f1:string -> int -> int -> 'a)
          (f2:int -> char -> 'a)
          (f3:char -> 'a)
        : t -> 'a =
      function
      | String (s,i,l) -> f1 s i l
      | Fill (n,c) -> f2 n c
      | Char c -> f3 c
  end




module Line =
  struct
    type t = {s:string; i:int}
    let make s i = {s;i}
    let text (l:t): string = l.s
    let length l = String.length l.s
    let indent l = l.i
  end






(* Gammar

   d ::= t* g* c*                   -- document

   g ::= [| g* c* |]                -- group, at least one LB, either direct or
                                    -- indirect

   c ::= l t* g*                    -- chunk
*)



type chunk = {line: Line.t;
              texts:  Text.t list;
              cgroups: group list}

and group = {len:int;
             groups: group list;
             chunks: chunk list}

module Chunk =
  struct
    let line (c:chunk): Line.t = c.line
    let groups (c:chunk): group list = c.cgroups
    let texts (c:chunk): Text.t list = c.texts
    let make (line:Line.t): chunk =
      {line; texts = []; cgroups = []}
    let add_text (t:Text.t) (c:chunk): chunk =
      assert (c.cgroups = []);
      {c with texts = t :: c.texts}
    let add_group (g:group) (c:chunk): chunk =
      {c with cgroups = g :: c.cgroups}
  end





module Group =
  struct
    let length (g:group): int =  g.len
    let empty = {len = 0; groups = []; chunks = []}
    let groups (g:group): group list = g.groups
    let chunks (g:group): chunk list = g.chunks
    let add_text (t:Text.t) (g:group): group =
      match g.chunks with
      | [] ->
         assert false (* Illegal call *)
      | c :: tl ->
         {g with len = g.len + Text.length t; chunks = Chunk.add_text t c :: tl}
    let add_line (l:Line.t) (g:group): group =
      {g with
        len = g.len + Line.length l;
        chunks = Chunk.make l :: g.chunks}
    let add_group (gi:group) (go:group): group =
      let len = go.len + gi.len
      in
      match go.chunks with
      | [] ->
         {go with len; groups = gi :: go.groups}
      | c :: cs ->
         {go with len; chunks = Chunk.add_group gi c :: cs}
  end




module Buffer =
  struct
    type t = {gs: group list;
              l:  int;  (* length *)
              o:  int   (* open groups*) }

    let is_empty (b:t): bool = (b.o = 0)
    let length (b:t): int = b.l
    let count (b:t): int = b.o
    let groups (b:t): group list = b.gs
    let empty: t =
      {gs = []; l = 0; o = 0;}

    let push (g:group) (b:t): t =
      {gs = g :: b.gs; l = Group.length g + b.l; o = b.o + 1}

    let add_text (t:Text.t) (b:t): t =
      match b.gs with
      | [] ->
         assert false (* Illegal call *)
      | g :: tl ->
         {b with
           gs = Group.add_text t g :: tl;
           l  = b.l + Text.length t}

    let add_line (l:Line.t) (b:t): t =
      match b.gs with
      | [] ->
         assert false (* Illegal call *)
      | g :: tl ->
         {b with
           gs = Group.add_line l g :: tl;
           l  = b.l + Line.length l}

    let open_groups (n:int) (b:t): t =
      assert (0 <= n);
      let rec ogs n =
        if n = 0 then
          b.gs
        else
          Group.empty :: ogs (n-1)
      in
      {b with o = b.o + n; gs = ogs n}

    let close_groups (n:int) (b:t): t =
      assert (0 <= n);
      assert (n < b.o);
      let rec close n gs =
        if n = 0 then
          gs
        else
          match gs with
          | gi :: go :: tl ->
             close
               (n-1)
               (Group.add_group gi go :: tl)
          | _ ->
             assert false (* Illegal call: cannot close group unless there is
                             one group to which it can be added. *)
      in
      {b with o = b.o - n; gs = close n b.gs}
  end


module State =
  struct
    type indent = {
        line_indent:int;     (* Indent of the current line *)
        current_indent:int;  (* Current indentation level *)
        pos: int             (* Position on the current line *)
      }
    type groups = {
        oe: int;  (* open effective groups *)
        oa: int;  (* open active groups *)
        o_r: int; (* open groups to the right of the last open
                     group in buffer *)
      }
    type params = {
        width: int;        (* desired maximal line width *)
        ribbon: int;       (* desired maximal ribbon width *)
      }
    type t = {
        params:params;
        indent:indent;
        groups:groups;
        buffer:Buffer.t
      }

    let init  (i:int) (width:int) (ribbon:int): t =
      {params = {width;ribbon};
       indent = {line_indent = i; current_indent = i; pos = i};
       groups = {oe = 0; oa = 0; o_r = 0};
       buffer = Buffer.empty}

    let normal (st:t): bool =
      Buffer.is_empty st.buffer

    let buffering (st:t): bool =
      not (Buffer.is_empty st.buffer)

    let position (st:t): int =
      st.indent.pos

    let current_indent (st:t): int =
      st.indent.current_indent

    let relative_position(st:t): int =
      st.indent.pos - st.indent.current_indent

    let fits_pos (p:int) (st:t): bool =
      (* Is position [p] allowed, i.e. is it within the line width and the
         ribbon width? *)
      p <= st.params.width
      && p - st.indent.line_indent <= st.params.ribbon

    let fits (len:int) (st:t): bool =
      (* Do [len] more characters after the buffer still fit on the line? *)
      fits_pos (st.indent.pos + (Buffer.length st.buffer) + len) st

    let buffer_fits (st:t): bool =
      (* Does the buffer fit on the line? *)
      fits 0 st

    let increment_position (i:int) (st:t): t =
      {st with indent = {st.indent with pos = st.indent.pos + i}}

    let add_text (t:Text.t) (st:t): t =
      {st with buffer = Buffer.add_text t st.buffer}

    let add_line (alternative_text:string) (st:t): t =
      assert (buffering st);
      assert (0 < st.groups.oa);
      let o = Buffer.count st.buffer in
      let buffer =
        if st.groups.oa <= o then
          Buffer.close_groups (o - st.groups.oa) st.buffer
        else
          st.buffer
      in
      {st with
        buffer =
          Buffer.open_groups
            (if o < st.groups.oa then
               st.groups.oa - o
             else
               st.groups.o_r)
            buffer
          |> (Buffer.add_line
              @@ Line.make alternative_text st.indent.current_indent)}


    let newline (indent:int) (st:t): t =
      assert (normal st);
      {st with
        indent = {
          st.indent with
          pos = indent;
          line_indent = indent
        }}

    let active_to_effective (st:t): t =
      (* Make all active groups effective *)
      assert (normal st);
      {st with
        groups = {
          st.groups with
          oa = 0;
          oe = st.groups.oe + st.groups.oa
      }}

    let one_active_to_effective (st:t): t =
      (* Make one active group effective *)
      assert (0 < st.groups.oa);
      {st with
        groups = {
          st.groups with
          oa = st.groups.oa - 1;
          oe = st.groups.oe + 1
      }}


    let right_to_active (st:t): t =
      {st with
        groups = {
          st.groups with
          oa = st.groups.oa + st.groups.o_r;
          o_r = 0}}


    let start_buffering (s:string) (st:t): t =
      assert (normal st);
      {st with
        buffer = Buffer.(
          open_groups st.groups.oa st.buffer
          |> add_line (Line.make s st.indent.current_indent));
        groups = {st.groups with o_r = 0}}

    let clear_buffer (st:t): t =
      {st with buffer = Buffer.empty}

    let push (g:group) (st:t): t =
      {st with buffer = Buffer.push g st.buffer}


    let open_group (st:t): t =
      {st with
          groups =
            if st.groups.oa < Buffer.count st.buffer then
              {st.groups with o_r = st.groups.o_r + 1}
            else
              {st.groups with oa = st.groups.oa + 1}}


    let close_group (st:t): t =
      {st with
        groups =
          if 0 < st.groups.o_r then
            (assert (st.groups.oa < Buffer.count st.buffer);
             {st.groups with
               o_r = st.groups.o_r - 1})
          else if 0 < st.groups.oa then
            {st.groups with
              oa = st.groups.oa - 1}
          else
            (assert (0 < st.groups.oe);
             {st.groups with
               oe = st.groups.oe - 1})}

    let increment_indent (i:int) (st:t) =
      let current_indent = st.indent.current_indent + i in
      assert (0 <= current_indent);
      {st with
         indent = {st.indent with current_indent}}
  end








module Pretty (P:PRINTER) =
  struct

    type state = State.t

    type loop_state =
      | More of (unit -> loop_state)
      | Done of P.t


    let loop: loop_state -> P.t =
        let rec do_loop i = function
        | Done p ->
            p
        | More f ->
            do_loop (i + 1) (f ())
        in
        do_loop 0

    type 'a cont =
      (* A continuation function takes a value, a state and the cumulated
         printing command and returns the remainder of the loop. *)
      'a -> state -> P.t -> loop_state

    module M =
      struct
        type 'a t =
          state
          -> P.t      (* printed up to now *)
          -> 'a cont  (* continuation *)
          -> loop_state

        let return (a:'a): 'a t =
          fun st p k -> k a st p

        let (>>=) (m:'a t) (f:'a -> 'b t): 'b t =
          fun st p k ->
          m
            st
            p
            (fun a st p -> More (fun _ -> f a st p k))
      end


    type t = unit M.t


    let empty: t =
      M.return ()

    let (<+>) (m1:t) (m2:t): t =
      M.(m1 >>= fun _ -> m2)


    let state: state M.t =
      fun st p k -> k st st p

    let put (st:state): unit M.t =
      fun _ p k -> k () st p


    let update (f:state->state): t =
      fun st p k -> k () (f st) p


    let get_and_update (f:state->state): state M.t =
      fun st p k -> k st (f st) p


    let relative_position:  int M.t =
      M.(state >>= fun st -> return @@ State.relative_position st)


    let fill_indent (state: state) (p: P.t): state * P.t =
        let pos = State.position state
        and ind = State.current_indent state
        in
        if pos = 0 then
            State.increment_position (ind - pos) state,
            P.(p <+> fill (ind - pos) ' ')
        else
            state, p


    let out_string (s:string) (start:int) (len:int): t =
      fun st p k ->
      More
        (fun _ ->
            let st, p = fill_indent st p in
            k
                ()
                (State.increment_position len st)
                P.(p <+> substring s start len))


    let out_char (c:char): t =
      fun st p k ->
      More
        (fun _ ->
            let st, p = fill_indent st p in
            k
                ()
                (State.increment_position 1 st)
                P.(p <+> char c))


    let out_fill (n:int) (c:char): t =
      assert (0 <= n);
      fun st p k ->
          More
              (fun _ ->
               let st, p = fill_indent st p in
               k
                   ()
                   (State.increment_position n st)
                   P.(p <+> fill n c))


    let out_text (t:Text.t): t =
      Text.apply out_string out_fill out_char t

    let out_line (indent:int): t =
      fun st p k ->
      More
        (fun _ ->
          k
            ()
            (State.newline indent st)
            P.(p <+> char '\n' <+> fill indent ' '))


    let print_list (l:'a list) (f:'a -> t): t =
      M.(List.fold_right
           (fun a pr -> pr >>= fun _ -> f a)
           l (return ()))

    let out_texts (l:Text.t list): t =
      print_list
        l
        out_text

    let out_alternative_text (l:Line.t): t =
      let s = Line.text l in
      out_string s 0 (String.length s)

    let line_normal (s:string): t =
      M.(state >>= fun st ->
         if 0 < st.groups.oa && State.fits (String.length s) st then
           put (State.start_buffering s st)
         else
           put (State.active_to_effective st) >>= fun _ ->
           out_line st.indent.current_indent)



    let rec flush_flatten_group (g:group): t =
      (* Print the group [g] flattened. *)
      let open Group in
      M.(flush_flatten_groups (groups g) >>= fun _ ->
         flush_flatten_chunks (chunks g))

    and flush_flatten_groups (gs:group list): t =
      (* Print all groups in the list [gs] flattened. *)
      print_list
        gs
        flush_flatten_group

    and flush_flatten_chunks (cs:chunk list): t =
      (* Print all chunks in the list [cs] flattened. *)
      print_list
        cs
        flush_flatten_chunk

    and flush_flatten_chunk (c:chunk): t =
      (* Print the chunk [c] flattened. *)
      M.(out_alternative_text (Chunk.line c)
         >>= fun _ ->
         out_texts (Chunk.texts c)
         >>= fun _ ->
         flush_flatten_groups (Chunk.groups c))


    let flush_flatten: t =
      (* flush the complete buffer flattening it i.e. print all line breaks
         with their corresponding alternative text. *)
      let open M in
      state >>= fun st ->
      print_list
        (Buffer.groups st.buffer)
        flush_flatten_group
      >>= fun _ ->
      update State.clear_buffer


    let rec flush_group (g:group): t =
      (* Flush the complete group [g]. If it fits on the line, then flush it
         flattened. Otherwise print its break hints as line breaks. *)
      let open M in
      state >>= fun st ->
      if State.fits (Group.length g) st then
        flush_flatten_group g
      else
        flush_groups g.groups >>= fun _ ->
        flush_chunks g.chunks

    and flush_chunk (c:chunk): t =
      let open M in
      out_line (Line.indent (Chunk.line c))
      >>= fun _ ->
      out_texts c.texts
      >>= fun _ ->
      flush_groups c.cgroups

    and flush_groups (gs:group list): t =
      print_list gs flush_group

    and flush_chunks (cs:chunk list): t =
      print_list cs flush_chunk


    let flush_incomplete (is_last:bool) (g:group): t =
      (* Flush an incomplete group from the buffer *)
      let open M in
      update
        (fun st ->
          if 0 < st.groups.oa then
            State.one_active_to_effective st
          else if is_last then
            State.right_to_active st
          else
            st)
      >>= fun _ ->
      flush_groups g.groups
      >>= fun _ ->
      flush_chunks g.chunks

    let flush_effective: t =
      (* Flush open groups until buffer fits or is empty. *)
      let open M in
      let rec flush (remaining_len:int) (is_last:bool) = function
        | [] -> assert false (* Illegal call! *)
        | [g] ->
           flush_incomplete is_last g
        | g :: gs ->
           let len = Group.length g + remaining_len
           in
           flush len false gs >>= fun _ ->
           state >>= fun st ->
           if State.fits len st then
             put (State.push g st)
           else
             flush_incomplete is_last g
      in
      state >>= fun st ->
      put (State.clear_buffer st) >>= fun _ ->
      flush 0 true (Buffer.groups st.buffer)


    let text (t:Text.t): t =
      let open M in
      state >>= fun st ->
      if State.normal st then
        out_text t
      else
        let st = State.add_text t st in
        if State.buffer_fits st then
          put st
        else
          put st >>= fun _ -> flush_effective


    let substring (s:string) (start:int) (len:int): t =
      assert (0 <= start);
      assert (start+len <= String.length s);
      text (Text.string s start len)


    let string (s:string): t =
      substring s 0 (String.length s)

    let char (c:char): t =
      text (Text.char c)

    let fill (n:int) (c:char): t =
      assert (0 <= n);
      text (Text.fill n c)


    let rec line (alternative_text:string): t =
      let open M in
      state >>= fun st ->
      if State.normal st then
        line_normal alternative_text
      else if 0 < st.groups.oa then
        (* Still inside the active group *)
        let st = State.(right_to_active @@ add_line alternative_text st)
        in
        if State.buffer_fits st then
          put st
        else
          put st >>= fun _ -> flush_effective
      else
        (* Outside the active group. *)
        put (State.right_to_active st) >>= fun _ ->
        flush_flatten >>= fun _ ->
        line alternative_text


    let cut: t =
      line ""

    let space: t =
      line " "


    let rec chain (l: t list): t =
      let open M in
      match l with
      | [] ->
         return ()
      | hd :: tl ->
         hd >>= fun _ -> chain tl


    let list_separated (sep: t) (lst: t list): t =
      let rec chn = function
        | [] ->
           empty
        | [p] ->
           p
        | p :: tl ->
           p <+> sep <+> chn tl
      in
      chn lst


    let chain_separated (lst:t list) (sep:t): t =
       list_separated sep lst


    let group (m:t): t =
      let open M in
      update State.open_group >>= fun _ ->
      m >>= fun a ->
      update State.close_group >>= fun _ ->
      return a

    let nest (i:int) (m:t): 't =
      let open M in
      get_and_update (State.increment_indent i) >>= fun _ ->
      (*let pos,ind = State.position st, State.current_indent st in
      let m =
        if pos = 0 (*pos < ind + i*) then
          fill (ind + i - pos) ' ' >>= fun _ -> m
        else
          m
      in*)
      m >>= fun a ->
      update (State.increment_indent (-i)) >>= fun _ ->
      return a

    let nest_list (i:int) (lst:t list): t =
      nest i (chain lst)

    let nest_relative (i:int) (m:t): t =
      let open M in
      relative_position >>= fun p ->
      nest (i+p) m


    let group_list (lst: t list): t =
      group @@ chain lst

    let wrap_words (s:string): t =
      let open M in
      let is_blank c = c = ' '
      and is_not_blank c = c <> ' '
      in
      let word_start i = String.find is_not_blank i s
      and word_end   i = String.find is_blank i s
      and len = String.length s
      in
      let rec fill p =
        let i = word_start p in
        (if p < i then
           group space
         else
           return ())
        >>= fun _ ->
        if i < len then
          let j = word_end i in
          substring s i (j-i) >>= fun _ ->
          fill j
        else
          return ()
      in
      fill 0

    let fill_paragraph = wrap_words

    let run (indent:int) (width:int) (ribbon:int) (p:unit M.t): P.t =
      loop
      @@ M.(p >>= fun _ -> flush_flatten)
           (State.init indent width ribbon)
           P.empty
           (fun () _ p -> Done p)
  end





















(*
  ------------
  Module Tests
  ------------
 *)

module PP = Pretty (Readable_printer)

include PP

let run (indent:int) (width:int) (ribbon:int) (m:t): Readable_printer.R.t =
  PP.run
    indent width ribbon
    m
  |> Readable_printer.readable


module R = Readable_printer.R

let compare (r:R.t) (s:string): bool =
  let len = String.length s in
  let rec comp r i =
    if i = len then
      true
    else
      let more = R.has_more r in
      if not more then
        false
      else
        s.[i] = R.peek r
        && comp (R.advance r) (i+1)
  in
  comp r 0

let print_readable (r:R.t): unit =
  let open Printf in
  let rec print r =
    if R.has_more r then
      (printf "%c" (R.peek r);
       print (R.advance r))
    else
      ()
  in
  print r;
  printf "\n"



let test (w:int) (pflag:bool) (pp:t) (expected:string): bool =
  let res = compare (run 0 w w pp) expected in
  if pflag then
    print_readable (run 0 w w pp);
  res


let%test _ =
  let str = "01234 6789 0 2 4 6 8 01 34 6789 0"
  and res = "01234 6789\n\
             0 2 4 6 8\n\
             01 34 6789\n\
             0"
  in
  test 10 false (fill_paragraph str) res


let%test _ =
  test
    10 false
    (nest_list 4 [string "0123"; cut; string "456"]
     <+> cut <+> string "0123")
    "    0123\n\
    \    456\n\
     0123"


(*
let%test _ =
    test
        70 true
        (
            string "line1"
            <+> cut
            <+> nest 4 (string "indented" <+> cut)
            <+> string "line2"
        )
        "line1\n\
        \    indented\n\
        \    line2"  (* WRONG: shall not be indented! *)
*)


let%test _ =
  compare
    (run 0 20 20
       (group_list
          [
            (group_list
               [string "class";
                nest_list 4 [space; string "Natural"];
                space; string "create"]);
            nest_list 4
              [space;
               (group_list
                  [string "0"; line "; "; string "succ(Natural)"])
              ];
            chain [space; string "end"]
          ]
       ))
       "class Natural create\n\
        \    0; succ(Natural)\n\
        end"



let maybe =
  group (group (string "class"
                <+> nest 4 (space <+> string "Maybe(A)")
                <+> space
                <+> string "create")
         <+> nest 4 (space
                     <+> group
                           (string "nothing"
                            <+> line "; "
                            <+> string "just(A)"))
         <+> space
         <+> string "end")

let%test _ =
  compare
    (run 0 70 70 maybe)
    "class Maybe(A) create nothing; just(A) end"


let%test _ =
  compare
    (run 0 20 20 maybe)
    "class\
     \n    Maybe(A)\n\
     create\
     \n    nothing; just(A)\n\
     end"



let%test _ =
  compare
    (run 0 15 15 maybe)
    "class\
     \n    Maybe(A)\n\
     create\
     \n    nothing\
     \n    just(A)\n\
     end"





let plus =
  let gns p =
    group (nest 2 (space <+> p))
  in
  let insp =
    group (string "inspect"
           <+> nest
                 2
                 (space
                  <+> group (
                          string "a" <+> line "; "
                          <+> string "(_:Natural) := Natural"))
           <+> space <+> string "case"
      )
  and cases =
    group (
        string "0 :="
        <+> gns (string "b")
        <+> line "; "
        <+> string "n.successor :="
        <+> gns (string "n + b.successor")
      )
  in
  string "(+)(a:Natural,b:Natural): Natural :="
  <+> gns (
          group (
              insp
              <+> nest
                    2
                    (space <+> cases)
              <+> space <+> string "end")
        )



let%test _ =
  compare
    (run 0 40 40 plus)
    "(+)(a:Natural,b:Natural): Natural :=\
     \n  inspect a; (_:Natural) := Natural case\
     \n    0 := b\n    n.successor := n + b.successor\
     \n  end"



let%test _ =
  compare
    (run 0 39 39 plus)
    "(+)(a:Natural,b:Natural): Natural :=\
     \n  inspect\
     \n    a; (_:Natural) := Natural\
     \n  case\
     \n    0 := b\
     \n    n.successor := n + b.successor\
     \n  end"


let%test _ =
  compare
    (run 0 33 33 plus)
    "(+)(a:Natural,b:Natural): Natural :=\
     \n  inspect\n    a; (_:Natural) := Natural\
     \n  case\
     \n    0 := b\
     \n    n.successor :=\
     \n      n + b.successor\
     \n  end"
OCaml

Innovation. Community. Security.