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
open! Base
let of_sexp_error = Sexplib.Conv.of_sexp_error
type color =
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
| BrightBlack
| BrightRed
| BrightGreen
| BrightYellow
| BrightBlue
| BrightMagenta
| BrightCyan
| BrightWhite
| Default
[@@deriving sexp]
type atom_threshold = Atom_threshold of int [@@deriving sexp]
let atom_threshold_of_sexp sexp =
match atom_threshold_of_sexp sexp with
| Atom_threshold n when n < 0 ->
of_sexp_error "Atom threshold must be non_negative." sexp
| threshold -> threshold
;;
type char_threshold = Character_threshold of int [@@deriving sexp]
let char_threshold_of_sexp sexp =
match char_threshold_of_sexp sexp with
| Character_threshold n when n < 0 ->
of_sexp_error "Character threshold must be non_negative." sexp
| threshold -> threshold
;;
type depth_threshold = Depth_threshold of int [@@deriving sexp]
let depth_threshold_of_sexp sexp =
match depth_threshold_of_sexp sexp with
| Depth_threshold n when n < 1 ->
of_sexp_error "Depth threshold must be greater than 0." sexp
| threshold -> threshold
;;
type aligned_parens = Parens_alignment of bool [@@deriving sexp]
type data_alignment =
| Data_not_aligned
| Data_aligned of aligned_parens * atom_threshold * char_threshold * depth_threshold
[@@deriving sexp]
type atom_coloring =
| Color_first of int
| Color_all
| Color_none
[@@deriving sexp]
let atom_coloring_of_sexp sexp =
match atom_coloring_of_sexp sexp with
| Color_first n when n < 0 ->
of_sexp_error "The limit to color atoms must be non-negative." sexp
| coloring -> coloring
;;
[@@deriving sexp]
let sexp =
match comment_indent_of_sexp sexp with
| Indent_comment n when n < 0 -> of_sexp_error "Indentation must be non-negative." sexp
| indent -> indent
;;
[@@deriving sexp]
type atom_printing =
| Escaped
| Minimal_escaping
| Interpreted
[@@deriving sexp]
type singleton_limit = Singleton_limit of atom_threshold * char_threshold
[@@deriving sexp]
type paren_coloring = bool [@@deriving sexp]
type separator =
| No_separator
| Empty_line
[@@deriving sexp]
type parens =
| Same_line
| New_line
[@@deriving sexp]
type t =
{
indent : int [@default 2]
;
data_alignment : data_alignment
[@default
Data_aligned
( Parens_alignment false
, Atom_threshold 6
, Character_threshold 60
, Depth_threshold 3 )]
; color_scheme : color array
; atom_coloring : atom_coloring [@default Color_first 3]
; atom_printing : atom_printing [@default Escaped]
; paren_coloring : paren_coloring [@default true]
; opening_parens : parens [@default Same_line]
; closing_parens : parens [@default Same_line]
; : comments [@default Print (Indent_comment 3, Some Green, Pretty_print)]
; singleton_limit : singleton_limit
[@default Singleton_limit (Atom_threshold 3, Character_threshold 15)]
;
leading_threshold : atom_threshold * char_threshold
[@default Atom_threshold 3, Character_threshold 20]
; separator : separator [@default Empty_line]
; : sticky_comments [@default After]
}
[@@deriving sexp]
let t_of_sexp sexp =
let t = t_of_sexp sexp in
if t.indent < 0 then of_sexp_error "Indentation must be non-negative." sexp else t
;;
let default_color_scheme = [| Magenta; Yellow; Cyan; White |]
let default =
{ indent = 2
; data_alignment =
Data_aligned
( Parens_alignment false
, Atom_threshold 6
, Character_threshold 50
, Depth_threshold 3 )
; color_scheme = default_color_scheme
; atom_coloring = Color_first 3
; atom_printing = Escaped
; paren_coloring = true
; closing_parens = Same_line
; opening_parens = Same_line
; comments = Print (Indent_comment 3, Some Green, Pretty_print)
; singleton_limit = Singleton_limit (Atom_threshold 3, Character_threshold 40)
; leading_threshold = Atom_threshold 3, Character_threshold 40
; separator = Empty_line
; sticky_comments = After
}
;;
let update
?color
?interpret_atom_as_sexp
?
?new_line_separator
?custom_data_alignment
conf
=
let conf =
match color with
| None -> conf
| Some color ->
if color
then conf
else (
match conf.comments with
| Print (indent, Some _, style) ->
{ conf with
atom_coloring = Color_none
; paren_coloring = false
; comments = Print (indent, None, style)
}
| _ -> { conf with atom_coloring = Color_none; paren_coloring = false })
in
let conf =
match interpret_atom_as_sexp with
| None -> conf
| Some interpret_atom_as_sexp ->
if interpret_atom_as_sexp then { conf with atom_printing = Interpreted } else conf
in
let conf =
match drop_comments with
| None -> conf
| Some -> if drop_comments then { conf with comments = Drop } else conf
in
let conf =
match new_line_separator with
| None -> conf
| Some true -> { conf with separator = Empty_line }
| Some false -> { conf with separator = No_separator }
in
let conf =
match custom_data_alignment with
| None -> conf
| Some data_alignment -> { conf with data_alignment }
in
conf
;;
let create
?(color = false)
?(interpret_atom_as_sexp = false)
?( = false)
?(new_line_separator = false)
?custom_data_alignment
()
=
update
~color
~interpret_atom_as_sexp
~drop_comments
~new_line_separator
?custom_data_alignment
default
;;