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
let max_escape_length = 256
let max_osc_length = 8192
type control =
| CUU of int
| CUD of int
| CUF of int
| CUB of int
| CNL of int
| CPL of int
| CHA of int
| VPA of int
| CUP of int * int
| ED of int
| EL of int
| IL of int
| DL of int
| DCH of int
| ICH of int
| OSC of int * string
| Hyperlink of ((string * string) list * string) option
| Reset
| DECSC
| DECRC
| Unknown of string
type sgr_attr =
[ `Reset
| `Bold
| `Dim
| `Italic
| `Underline
| `Double_underline
| `Blink
| `Inverse
| `Hidden
| `Strikethrough
| `Overline
| `Framed
| `Encircled
| `No_bold
| `No_dim
| `No_italic
| `No_underline
| `No_blink
| `No_inverse
| `No_hidden
| `No_strikethrough
| `No_overline
| `No_framed
| `No_encircled
| `Fg of Color.t
| `Bg of Color.t ]
type token = Text of string | SGR of sgr_attr list | Control of control
let is_final_byte c =
let code = Char.code c in
code >= 0x40 && code <= 0x7e
let parse_sgr_params params len =
let rec loop i acc =
if i >= len then List.rev acc
else
let param = params.(i) in
match param with
| 0 -> loop (i + 1) (`Reset :: acc)
| 1 -> loop (i + 1) (`Bold :: acc)
| 2 -> loop (i + 1) (`Dim :: acc)
| 3 -> loop (i + 1) (`Italic :: acc)
| 4 -> loop (i + 1) (`Underline :: acc)
| 21 -> loop (i + 1) (`Double_underline :: acc)
| 5 -> loop (i + 1) (`Blink :: acc)
| 7 -> loop (i + 1) (`Inverse :: acc)
| 8 -> loop (i + 1) (`Hidden :: acc)
| 9 -> loop (i + 1) (`Strikethrough :: acc)
| 53 -> loop (i + 1) (`Overline :: acc)
| 51 -> loop (i + 1) (`Framed :: acc)
| 52 -> loop (i + 1) (`Encircled :: acc)
| 22 -> loop (i + 1) (`No_dim :: `No_bold :: acc)
| 23 -> loop (i + 1) (`No_italic :: acc)
| 24 -> loop (i + 1) (`No_underline :: acc)
| 25 -> loop (i + 1) (`No_blink :: acc)
| 27 -> loop (i + 1) (`No_inverse :: acc)
| 28 -> loop (i + 1) (`No_hidden :: acc)
| 29 -> loop (i + 1) (`No_strikethrough :: acc)
| 54 -> loop (i + 1) (`No_encircled :: `No_framed :: acc)
| 55 -> loop (i + 1) (`No_overline :: acc)
| 39 -> loop (i + 1) (`Fg Color.default :: acc)
| 49 -> loop (i + 1) (`Bg Color.default :: acc)
| n when 30 <= n && n <= 37 ->
loop (i + 1) (`Fg (Color.of_palette_index (n - 30)) :: acc)
| n when 90 <= n && n <= 97 ->
loop (i + 1) (`Fg (Color.of_palette_index (n - 90 + 8)) :: acc)
| n when 40 <= n && n <= 47 ->
loop (i + 1) (`Bg (Color.of_palette_index (n - 40)) :: acc)
| n when 100 <= n && n <= 107 ->
loop (i + 1) (`Bg (Color.of_palette_index (n - 100 + 8)) :: acc)
| 38 when i + 2 < len && params.(i + 1) = 5 ->
let idx = params.(i + 2) in
loop (i + 3) (`Fg (Color.of_palette_index idx) :: acc)
| 38 when i + 4 < len && params.(i + 1) = 2 ->
let r = params.(i + 2) in
let g = params.(i + 3) in
let b = params.(i + 4) in
loop (i + 5) (`Fg (Color.of_rgb r g b) :: acc)
| 48 when i + 2 < len && params.(i + 1) = 5 ->
let idx = params.(i + 2) in
loop (i + 3) (`Bg (Color.of_palette_index idx) :: acc)
| 48 when i + 4 < len && params.(i + 1) = 2 ->
let r = params.(i + 2) in
let g = params.(i + 3) in
let b = params.(i + 4) in
loop (i + 5) (`Bg (Color.of_rgb r g b) :: acc)
| _ -> loop (i + 1) acc
in
loop 0 []
let max_csi_params = 32
let parse_csi_params body body_len params max_params =
let rec parse i current count =
if i >= body_len then (
if count < max_params then params.(count) <- current;
min (count + 1) max_params)
else
match body.[i] with
| ';' ->
if count < max_params then params.(count) <- current;
parse (i + 1) 0 (count + 1)
| '0' .. '9' as c ->
parse (i + 1) ((current * 10) + Char.code c - 48) count
| _ -> parse (i + 1) current count
in
if body_len > 0 then parse 0 0 0 else 0
let parse_csi ~params body final : token option =
let body_len = String.length body in
let param_count = parse_csi_params body body_len params max_csi_params in
let get n default = if n < param_count then params.(n) else default in
match final with
| 'A' -> Some (Control (CUU (get 0 1)))
| 'B' -> Some (Control (CUD (get 0 1)))
| 'C' -> Some (Control (CUF (get 0 1)))
| 'D' -> Some (Control (CUB (get 0 1)))
| 'E' -> Some (Control (CNL (get 0 1)))
| 'F' -> Some (Control (CPL (get 0 1)))
| 'G' -> Some (Control (CHA (get 0 1)))
| 'd' -> Some (Control (VPA (get 0 1)))
| 'H' | 'f' -> Some (Control (CUP (get 0 1, get 1 1)))
| 'J' -> Some (Control (ED (get 0 0)))
| 'K' -> Some (Control (EL (get 0 0)))
| 'L' -> Some (Control (IL (get 0 1)))
| 'M' -> Some (Control (DL (get 0 1)))
| 'P' -> Some (Control (DCH (get 0 1)))
| '@' -> Some (Control (ICH (get 0 1)))
| 'm' ->
let attrs =
if param_count = 0 then [ `Reset ]
else parse_sgr_params params param_count
in
Some (SGR attrs)
| 's' -> Some (Control DECSC)
| 'u' -> Some (Control DECRC)
| _ -> Some (Control (Unknown (Printf.sprintf "CSI[%s%c" body final)))
let parse_hyperlink data =
let len = String.length data in
let rec find_semi i =
if i >= len then -1 else if data.[i] = ';' then i else find_semi (i + 1)
in
match find_semi 0 with
| -1 -> None
| semi1 ->
let params_str = String.sub data 0 semi1 in
let link = String.sub data (semi1 + 1) (len - semi1 - 1) in
if params_str = "" && link = "" then Some (Hyperlink None)
else if params_str = "" then Some (Hyperlink (Some ([], link)))
else
let rec parse_kv i acc =
if i >= String.length params_str then List.rev acc
else
let rec find_delim j =
if j >= String.length params_str then j
else if params_str.[j] = ':' then j
else find_delim (j + 1)
in
let next_colon = find_delim i in
let segment = String.sub params_str i (next_colon - i) in
let rec find_eq k =
if k >= String.length segment then -1
else if segment.[k] = '=' then k
else find_eq (k + 1)
in
let acc' =
match find_eq 0 with
| -1 -> acc
| eq_pos ->
let key = String.sub segment 0 eq_pos in
let value =
String.sub segment (eq_pos + 1)
(String.length segment - eq_pos - 1)
in
(key, value) :: acc
in
parse_kv (next_colon + 1) acc'
in
let kv_pairs = parse_kv 0 [] in
Some (Hyperlink (Some (kv_pairs, link)))
let parse_osc body : token =
match String.index_opt body ';' with
| None -> Control (Unknown ("OSC " ^ body))
| Some semi -> (
let code_str = String.sub body 0 semi in
let data = String.sub body (semi + 1) (String.length body - semi - 1) in
match int_of_string_opt code_str with
| Some 8 -> (
match parse_hyperlink data with
| Some c -> Control c
| None -> Control (Unknown ("OSC8 " ^ data)))
| Some code -> Control (OSC (code, data))
| None -> Control (Unknown ("OSC " ^ body)))
type state = Normal | Escape | Csi | Osc | Osc_escape
type t = {
mutable pending_buf : bytes;
mutable pending_len : int;
mutable state : state;
csi_buf : Buffer.t;
osc_buf : Buffer.t;
text_buf : Buffer.t;
utf8_buf : bytes;
mutable utf8_len : int;
csi_params : int array;
}
let create () =
{
pending_buf = Bytes.create 256;
pending_len = 0;
state = Normal;
csi_buf = Buffer.create 32;
osc_buf = Buffer.create 64;
text_buf = Buffer.create 128;
utf8_buf = Bytes.create 4;
utf8_len = 0;
csi_params = Array.make max_csi_params 0;
}
let reset p =
p.pending_len <- 0;
p.state <- Normal;
Buffer.clear p.csi_buf;
Buffer.clear p.osc_buf;
Buffer.clear p.text_buf;
p.utf8_len <- 0
let has_pending p = p.pending_len > 0 || p.state <> Normal || p.utf8_len > 0
let pending p = Bytes.sub p.pending_buf 0 p.pending_len
let decode_utf8 p src off len =
if len <= 0 then ()
else begin
let buf = p.text_buf in
let end_pos = off + len in
let i = ref off in
if p.utf8_len > 0 then begin
while p.utf8_len < 4 && !i < end_pos do
Bytes.set p.utf8_buf p.utf8_len (Bytes.get src !i);
p.utf8_len <- p.utf8_len + 1;
incr i;
let d = Bytes.get_utf_8_uchar p.utf8_buf 0 in
if Uchar.utf_decode_is_valid d then begin
Buffer.add_utf_8_uchar buf (Uchar.utf_decode_uchar d);
p.utf8_len <- 0
end
done;
if p.utf8_len = 4 then begin
Buffer.add_utf_8_uchar buf Uchar.rep;
p.utf8_len <- 0
end
end;
while !i < end_pos do
let d = Bytes.get_utf_8_uchar src !i in
if Uchar.utf_decode_is_valid d then begin
Buffer.add_utf_8_uchar buf (Uchar.utf_decode_uchar d);
i := !i + Uchar.utf_decode_length d
end
else begin
let remaining = end_pos - !i in
let b = Bytes.get_uint8 src !i in
let is_valid_lead = b >= 0xC2 && b <= 0xF4 in
if remaining < 4 && is_valid_lead then begin
Bytes.blit src !i p.utf8_buf 0 remaining;
p.utf8_len <- remaining;
i := end_pos
end
else begin
Buffer.add_utf_8_uchar buf Uchar.rep;
incr i
end
end
done
end
let flush_utf8 p =
if p.utf8_len > 0 then begin
Buffer.add_utf_8_uchar p.text_buf Uchar.rep;
p.utf8_len <- 0
end
let[@inline] emit_text_cb p emit =
if Buffer.length p.text_buf > 0 then (
emit (Text (Buffer.contents p.text_buf));
Buffer.clear p.text_buf)
let feed p src ~off ~len emit =
let eof = len = 0 in
let input, input_off, input_len =
if p.pending_len = 0 then (src, off, len)
else
let total = p.pending_len + len in
if total > Bytes.length p.pending_buf then (
let new_size = max total (Bytes.length p.pending_buf * 2) in
let new_buf = Bytes.create new_size in
Bytes.blit p.pending_buf 0 new_buf 0 p.pending_len;
p.pending_buf <- new_buf);
Bytes.blit src off p.pending_buf p.pending_len len;
p.pending_len <- p.pending_len + len;
(p.pending_buf, 0, p.pending_len)
in
let rec loop pos =
if pos >= input_len then pos
else
match p.state with
| Normal ->
let rec scan_text start i =
if i >= input_len then (
if i > start then
decode_utf8 p input (input_off + start) (i - start);
i)
else
let c = Bytes.get input (input_off + i) in
if c = '\x1b' then (
if i > start then
decode_utf8 p input (input_off + start) (i - start);
emit_text_cb p emit;
p.state <- Escape;
loop (i + 1))
else scan_text start (i + 1)
in
scan_text pos pos
| Escape -> (
let c = Bytes.get input (input_off + pos) in
match c with
| '[' ->
Buffer.clear p.csi_buf;
p.state <- Csi;
loop (pos + 1)
| ']' ->
Buffer.clear p.osc_buf;
p.state <- Osc;
loop (pos + 1)
| 'c' ->
p.state <- Normal;
emit (Control Reset);
loop (pos + 1)
| '7' ->
p.state <- Normal;
emit (Control DECSC);
loop (pos + 1)
| '8' ->
p.state <- Normal;
emit (Control DECRC);
loop (pos + 1)
| '%' | '(' | ')' | '*' | '+' ->
if pos + 1 < input_len then (
p.state <- Normal;
loop (pos + 2))
else (
p.state <- Normal;
pos - 1)
| _ ->
Buffer.add_char p.text_buf '\x1b';
p.state <- Normal;
loop pos)
| Csi ->
let c = Bytes.get input (input_off + pos) in
if Buffer.length p.csi_buf >= max_escape_length then (
p.state <- Normal;
Buffer.clear p.csi_buf;
emit (Control (Unknown "CSI_TOO_LONG"));
loop (pos + 1))
else if is_final_byte c then (
let body = Buffer.contents p.csi_buf in
let tok =
match parse_csi ~params:p.csi_params body c with
| Some t -> t
| None -> Control (Unknown "")
in
p.state <- Normal;
emit tok;
loop (pos + 1))
else (
Buffer.add_char p.csi_buf c;
loop (pos + 1))
| Osc ->
let c = Bytes.get input (input_off + pos) in
if c = '\x07' then (
let body = Buffer.contents p.osc_buf in
let tok = parse_osc body in
p.state <- Normal;
emit tok;
loop (pos + 1))
else if c = '\x1b' then (
p.state <- Osc_escape;
loop (pos + 1))
else if Buffer.length p.osc_buf >= max_osc_length then (
p.state <- Normal;
Buffer.clear p.osc_buf;
emit (Control (Unknown "OSC_TOO_LONG"));
loop (pos + 1))
else (
Buffer.add_char p.osc_buf c;
loop (pos + 1))
| Osc_escape ->
let c = Bytes.get input (input_off + pos) in
if c = '\\' then (
let body = Buffer.contents p.osc_buf in
let tok = parse_osc body in
p.state <- Normal;
emit tok;
loop (pos + 1))
else (
Buffer.add_char p.osc_buf '\x1b';
p.state <- Osc;
loop pos)
in
let consumed = loop 0 in
let remaining = input_len - consumed in
if remaining > 0 then (
if input == p.pending_buf then (
if
consumed > 0
then Bytes.blit p.pending_buf consumed p.pending_buf 0 remaining)
else (
if remaining > Bytes.length p.pending_buf then
p.pending_buf <- Bytes.create (max remaining 256);
Bytes.blit input (input_off + consumed) p.pending_buf 0 remaining);
p.pending_len <- remaining)
else p.pending_len <- 0;
if eof then flush_utf8 p;
emit_text_cb p emit
let parse s =
let p = create () in
let acc = ref [] in
let collect tok = acc := tok :: !acc in
feed p (Bytes.unsafe_of_string s) ~off:0 ~len:(String.length s) collect;
feed p Bytes.empty ~off:0 ~len:0 collect;
List.rev !acc