Source file box_debug.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
open Format_

let css =
  {|
  .box {
    border: 2px solid black;
    display: inline-block;
    font-family: courier;
    margin: 0;
    padding: 4px;
  }
  .box:hover {
    border-color: red;
  }
  .name {
    font-family: arial;
    font-style: italic;
    font-weight: lighter;
    font-size: 10px;
    padding: 1px;
    margin: 0 0 4px 0;
  }
  .break {
    background-color: black;
    color: white;
    display: inline-block;
    font-size: 10px;
    padding: 2px;
  }
  .cbreak {
    background-color: purple;
  }
  .keyword {
    background-color: yellow;
  }
  .if_newline {
    background-color: green;
  }
  .break_unless_newline {
    background-color: blue;
  }
  .fits_or_breaks {
    background-color: red;
  }
  .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;
    position: absolute;
    z-index: 1;
  }
  .break:hover .tooltiptext {
    visibility: visible;
  }
|}

let debug = ref false

let with_box k fs =
  let g = !debug in
  debug := true ;
  fprintf fs
    {|
<html>
  <head>
    <style>
      %s
    </style>
  </head>
  <body>
    %t
  </body>
</html>
|}
    css k ;
  debug := g

let box_open ?name box_kind n fs =
  if !debug then (
    let name =
      match name with
      | Some s -> sprintf "%s:%s" box_kind s
      | None -> box_kind
    in
    let name = if n = 0 then name else sprintf "%s(%d)" name n in
    open_vbox 0 ;
    fprintf fs "<div class=\"box\">" ;
    pp_print_break fs 1 2 ;
    fprintf fs "<p class=\"name\">%s</p>" name ;
    pp_print_break fs 1 2 )

let box_close fs =
  if !debug then (
    pp_close_box fs () ; pp_print_break fs 0 0 ; fprintf fs "</div>" )

let break fs n o =
  if !debug then
    fprintf fs
      "<div class=\"break\">(%i,%i)<span class=\"tooltiptext\">break %i \
       %i</span></div>"
      n o n o

let pp_keyword fs s = fprintf fs "<span class=\"keyword\">%s</span>" s

let _pp_format_lit fs =
  let open CamlinternalFormatBasics in
  function
  | Close_box -> pp_keyword fs "@]"
  | Close_tag -> pp_keyword fs "@}"
  | Break (str, _, _) -> pp_keyword fs str
  | FFlush -> pp_keyword fs "@?"
  | Force_newline -> pp_keyword fs "@\\n"
  | Flush_newline -> pp_keyword fs "@."
  | Magic_size (str, _) -> pp_keyword fs str
  | Escaped_at -> fprintf fs "@@"
  | Escaped_percent -> fprintf fs "@@%%"
  | Scan_indic c -> pp_keyword fs ("@" ^ String.make 1 c)

let rec _format_string :
    type a b c d e f.
    _ -> (a, b, c, d, e, f) CamlinternalFormatBasics.fmt -> unit =
  let open CamlinternalFormatBasics in
  fun fs -> function
    | String_literal (s, tl) -> fprintf fs "%s%a" s _format_string tl
    | Char_literal (c, tl) -> fprintf fs "%c%a" c _format_string tl
    | Formatting_lit (lit, tl) ->
        fprintf fs "%a%a" _pp_format_lit lit _format_string tl
    | Formatting_gen (Open_box (Format (n, _)), tl) ->
        pp_keyword fs "@[" ; _format_string fs n ; _format_string fs tl
    | Formatting_gen (Open_tag (Format (n, _)), tl) ->
        pp_keyword fs "@{" ; _format_string fs n ; _format_string fs tl
    | End_of_format -> ()
    | _ -> pp_keyword fs "??"

(** Returns a boolean to signal the caller not to render the format string
    again. *)
let fmt fs f =
  let open CamlinternalFormatBasics in
  if !debug then (
    let (Format (fmt, _)) = f in
    _format_string fs fmt ; true )
  else false

let cbreak fs ~fits:(s1, i, s2) ~breaks:(s3, j, s4) =
  if !debug then
    fprintf fs
      "<div class=\"break cbreak\">(%s,%i,%s) (%s,%i,%s)<span \
       class=\"tooltiptext\">cbreak ~fits:(%S, %i, %S) ~breaks:(%S, %i, \
       %S)</span></div>"
      s1 i s2 s3 j s4 s1 i s2 s3 j s4

let if_newline fs s =
  if !debug then
    fprintf fs
      "<div class=\"break if_newline\">(%s)<span \
       class=\"tooltiptext\">if_newline %S</span></div>"
      s s

let break_unless_newline fs n o =
  if !debug then
    fprintf fs
      "<div class=\"break break_unless_newline\">(%i,%i)<span \
       class=\"tooltiptext\">break_unless_newline %i %i</span></div>"
      n o n o

let fits_or_breaks fs fits n o breaks =
  if !debug then
    fprintf fs
      "<div class=\"break fits_or_breaks\">(%s,%i,%i,%s)<span \
       class=\"tooltiptext\">fits_or_breaks %S %i %i %S</span></div>"
      fits n o breaks fits n o breaks