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
exception InvalidCodepoint of int
exception MalFormed
module Uchar = struct
include Uchar
let of_int x =
if Uchar.is_valid x then Uchar.unsafe_of_int x else raise MalFormed
let[@inline] utf_8_byte_length u =
let c = to_int u in
if c < 0x80 then 1
else if c < 0x800 then 2
else if c < 0x10000 then 3
else 4
let[@inline] utf_16_byte_length u = if to_int u < 0x10000 then 2 else 4
end
module Bytes = struct
include Bytes
let[@inline] unsafe_set_uint8 b i x = unsafe_set b i (Char.unsafe_chr x)
let[@inline] set_utf_8_uchar b i u =
let c = Uchar.to_int u in
let room = length b - i in
if i < 0 || room <= 0 then raise (Invalid_argument "index out of bounds")
else if c < 0x80 then begin
unsafe_set_uint8 b i c;
1
end
else if c < 0x800 then
if room < 2 then 0
else begin
unsafe_set_uint8 b i (0xC0 lor (c lsr 6));
unsafe_set_uint8 b (i + 1) (0x80 lor (c land 0x3F));
2
end
else if c < 0x10000 then
if room < 3 then 0
else begin
unsafe_set_uint8 b i (0xE0 lor (c lsr 12));
unsafe_set_uint8 b (i + 1) (0x80 lor ((c lsr 6) land 0x3F));
unsafe_set_uint8 b (i + 2) (0x80 lor (c land 0x3F));
3
end
else if room < 4 then 0
else begin
unsafe_set_uint8 b i (0xF0 lor (c lsr 18));
unsafe_set_uint8 b (i + 1) (0x80 lor ((c lsr 12) land 0x3F));
unsafe_set_uint8 b (i + 2) (0x80 lor ((c lsr 6) land 0x3F));
unsafe_set_uint8 b (i + 3) (0x80 lor (c land 0x3F));
4
end
let[@inline] unsafe_set_uint16 ~hi ~lo b i x =
unsafe_set_uint8 b (i + hi) (x lsr 8);
unsafe_set_uint8 b (i + lo) (x land 0xFF)
let[@inline] set_utf_16_uchar ~hi ~lo b i u =
let c = Uchar.to_int u in
let room = length b - i in
if i < 0 || room <= 0 then raise (Invalid_argument "index out of bounds")
else if c < 0x10000 then
if room < 2 then 0
else begin
unsafe_set_uint16 ~hi ~lo b i c;
2
end
else if room < 4 then 0
else begin
let c = c - 0x10000 in
unsafe_set_uint16 ~hi ~lo b i (0xD800 lor (c lsr 10));
unsafe_set_uint16 ~hi ~lo b (i + 2) (0xDC00 lor (c land 0x3FF));
4
end
let[@inline] set_utf_16be_uchar b i u = set_utf_16_uchar ~hi:0 ~lo:1 b i u
let[@inline] set_utf_16le_uchar b i u = set_utf_16_uchar ~hi:1 ~lo:0 b i u
end
let ( = ) (a : int) b = a = b
let ( >>| ) o f = match o with Some x -> Some (f x) | None -> None
type apos = int
type lexbuf = {
refill : Uchar.t array -> int -> int -> int;
bytes_per_char : Uchar.t -> int;
mutable buf : Uchar.t array;
mutable len : int;
mutable offset : apos;
mutable bytes_offset : apos;
mutable pos : int;
mutable bytes_pos : int;
mutable curr_bol : int;
mutable curr_bytes_bol : int;
mutable curr_line : int;
mutable start_pos : int;
mutable start_bytes_pos : int;
mutable start_bol : int;
mutable start_bytes_bol : int;
mutable start_line : int;
mutable marked_pos : int;
mutable marked_bytes_pos : int;
mutable marked_bol : int;
mutable marked_bytes_bol : int;
mutable marked_line : int;
mutable marked_val : int;
mutable filename : string;
mutable finished : bool;
mutable __private__mem : int array;
}
let chunk_size = 512
let empty_lexbuf bytes_per_char =
{
refill = (fun _ _ _ -> assert false);
bytes_per_char;
buf = [||];
len = 0;
offset = 0;
bytes_offset = 0;
pos = 0;
bytes_pos = 0;
curr_bol = 0;
curr_bytes_bol = 0;
curr_line = 1;
start_pos = 0;
start_bytes_pos = 0;
start_bol = 0;
start_bytes_bol = 0;
start_line = 0;
marked_pos = 0;
marked_bytes_pos = 0;
marked_bol = 0;
marked_bytes_bol = 0;
marked_line = 0;
marked_val = 0;
filename = "";
finished = false;
__private__mem = [||];
}
let dummy_uchar = Uchar.of_int 0
let nl_uchar = Uchar.of_int 10
let create ?(bytes_per_char = fun _ -> 1) refill =
{
(empty_lexbuf bytes_per_char) with
refill;
buf = Array.make chunk_size dummy_uchar;
}
let set_position ?bytes_position lexbuf position =
lexbuf.offset <- position.Lexing.pos_cnum - lexbuf.pos;
lexbuf.curr_bol <- position.Lexing.pos_bol;
lexbuf.curr_line <- position.Lexing.pos_lnum;
let bytes_position = Option.value ~default:position bytes_position in
lexbuf.bytes_offset <- bytes_position.Lexing.pos_cnum - lexbuf.bytes_pos;
lexbuf.curr_bytes_bol <- bytes_position.Lexing.pos_bol
let set_filename lexbuf fname = lexbuf.filename <- fname
let from_gen ?bytes_per_char gen =
let malformed = ref false in
let refill buf pos len =
let rec loop i =
if !malformed then raise MalFormed;
if i >= len then len
else (
match gen () with
| Some c ->
buf.(pos + i) <- c;
loop (i + 1)
| None -> i
| exception MalFormed when i <> 0 ->
malformed := true;
i)
in
loop 0
in
create ?bytes_per_char refill
let from_int_array ?bytes_per_char a =
from_gen ?bytes_per_char
(Gen.init ~limit:(Array.length a) (fun i -> Uchar.of_int a.(i)))
let from_uchar_array ?(bytes_per_char = fun _ -> 1) a =
let len = Array.length a in
{
(empty_lexbuf bytes_per_char) with
buf = Array.init len (fun i -> a.(i));
len;
finished = true;
}
let refill lexbuf =
if lexbuf.len + chunk_size > Array.length lexbuf.buf then begin
let s = lexbuf.start_pos in
let s_bytes = lexbuf.start_bytes_pos in
let ls = lexbuf.len - s in
if ls + chunk_size <= Array.length lexbuf.buf then
Array.blit lexbuf.buf s lexbuf.buf 0 ls
else begin
let newlen = (Array.length lexbuf.buf + chunk_size) * 2 in
let newbuf = Array.make newlen dummy_uchar in
Array.blit lexbuf.buf s newbuf 0 ls;
lexbuf.buf <- newbuf
end;
lexbuf.len <- ls;
lexbuf.offset <- lexbuf.offset + s;
lexbuf.bytes_offset <- lexbuf.bytes_offset + s_bytes;
lexbuf.pos <- lexbuf.pos - s;
lexbuf.bytes_pos <- lexbuf.bytes_pos - s_bytes;
lexbuf.marked_pos <- lexbuf.marked_pos - s;
lexbuf.marked_bytes_pos <- lexbuf.marked_bytes_pos - s_bytes;
lexbuf.start_pos <- 0;
lexbuf.start_bytes_pos <- 0;
for i = 0 to Array.length lexbuf.__private__mem - 1 do
if lexbuf.__private__mem.(i) >= 0 then
lexbuf.__private__mem.(i) <- lexbuf.__private__mem.(i) - s
done
end;
let n = lexbuf.refill lexbuf.buf lexbuf.pos chunk_size in
if n = 0 then lexbuf.finished <- true else lexbuf.len <- lexbuf.len + n
let new_line lexbuf =
lexbuf.curr_line <- lexbuf.curr_line + 1;
lexbuf.curr_bol <- lexbuf.pos + lexbuf.offset;
lexbuf.curr_bytes_bol <- lexbuf.bytes_pos + lexbuf.bytes_offset
let[@inline always] next_aux some none lexbuf =
if (not lexbuf.finished) && lexbuf.pos = lexbuf.len then refill lexbuf;
if lexbuf.finished && lexbuf.pos = lexbuf.len then none
else begin
let ret = lexbuf.buf.(lexbuf.pos) in
lexbuf.pos <- lexbuf.pos + 1;
lexbuf.bytes_pos <- lexbuf.bytes_pos + lexbuf.bytes_per_char ret;
if Uchar.equal ret nl_uchar then new_line lexbuf;
some ret
end
let next lexbuf = (next_aux [@inlined]) (fun x -> Some x) None lexbuf
let __private__next_int lexbuf = (next_aux [@inlined]) Uchar.to_int (-1) lexbuf
let mark lexbuf i =
lexbuf.marked_pos <- lexbuf.pos;
lexbuf.marked_bytes_pos <- lexbuf.bytes_pos;
lexbuf.marked_bol <- lexbuf.curr_bol;
lexbuf.marked_bytes_bol <- lexbuf.curr_bytes_bol;
lexbuf.marked_line <- lexbuf.curr_line;
lexbuf.marked_val <- i
let start lexbuf =
lexbuf.start_pos <- lexbuf.pos;
lexbuf.start_bytes_pos <- lexbuf.bytes_pos;
lexbuf.start_bol <- lexbuf.curr_bol;
lexbuf.start_bytes_bol <- lexbuf.curr_bytes_bol;
lexbuf.start_line <- lexbuf.curr_line;
mark lexbuf (-1)
let backtrack lexbuf =
lexbuf.pos <- lexbuf.marked_pos;
lexbuf.bytes_pos <- lexbuf.marked_bytes_pos;
lexbuf.curr_bol <- lexbuf.marked_bol;
lexbuf.curr_bytes_bol <- lexbuf.marked_bytes_bol;
lexbuf.curr_line <- lexbuf.marked_line;
lexbuf.marked_val
let rollback lexbuf =
lexbuf.pos <- lexbuf.start_pos;
lexbuf.bytes_pos <- lexbuf.start_bytes_pos;
lexbuf.curr_bol <- lexbuf.start_bol;
lexbuf.curr_bytes_bol <- lexbuf.start_bytes_bol;
lexbuf.curr_line <- lexbuf.start_line
let __private__ensure_mem lexbuf n =
if Array.length lexbuf.__private__mem < n then
lexbuf.__private__mem <- Array.make n (-1)
let __private__set_mem_pos lexbuf i = lexbuf.__private__mem.(i) <- lexbuf.pos
let __private__set_mem_prev_pos lexbuf i =
lexbuf.__private__mem.(i) <- lexbuf.pos - 1
let __private__set_mem_value lexbuf i v =
assert (v >= 0);
lexbuf.__private__mem.(i) <- -(v + 2)
let __private__copy_mem lexbuf dst src =
lexbuf.__private__mem.(dst) <- lexbuf.__private__mem.(src)
let __private__mem_get lexbuf i = lexbuf.__private__mem.(i)
let __private__mem_set lexbuf i v = lexbuf.__private__mem.(i) <- v
let __private__mem_pos lexbuf i = lexbuf.__private__mem.(i) - lexbuf.start_pos
let __private__mem_value lexbuf i = -(lexbuf.__private__mem.(i) + 2)
let __private__num_mem_cells lexbuf = Array.length lexbuf.__private__mem
let lexeme_start lexbuf = lexbuf.start_pos + lexbuf.offset
let lexeme_bytes_start lexbuf = lexbuf.start_bytes_pos + lexbuf.bytes_offset
let lexeme_end lexbuf = lexbuf.pos + lexbuf.offset
let lexeme_bytes_end lexbuf = lexbuf.bytes_pos + lexbuf.bytes_offset
let loc lexbuf = (lexbuf.start_pos + lexbuf.offset, lexbuf.pos + lexbuf.offset)
let bytes_loc lexbuf =
( lexbuf.start_bytes_pos + lexbuf.bytes_offset,
lexbuf.bytes_pos + lexbuf.bytes_offset )
let lexeme_length lexbuf = lexbuf.pos - lexbuf.start_pos
let lexeme_bytes_length lexbuf = lexbuf.bytes_pos - lexbuf.start_bytes_pos
let sub_lexeme_offset name lexbuf buf pos len =
let off = lexbuf.start_pos + pos in
if
pos < 0 || len < 0
|| pos > lexbuf.pos - lexbuf.start_pos - len
|| off > Array.length buf - len
then invalid_arg name;
off
let sub_lexeme lexbuf pos len =
let buf = lexbuf.buf in
let off = sub_lexeme_offset "Sedlexing.sub_lexeme" lexbuf buf pos len in
Array.sub buf off len
type submatch = { lexbuf : lexbuf; pos : int; len : int }
let lexeme_of_submatch s = sub_lexeme s.lexbuf s.pos s.len
let lexeme lexbuf =
Array.sub lexbuf.buf lexbuf.start_pos (lexbuf.pos - lexbuf.start_pos)
let lexeme_char lexbuf pos = lexbuf.buf.(lexbuf.start_pos + pos)
let lexing_position_start lexbuf =
{
Lexing.pos_fname = lexbuf.filename;
pos_lnum = lexbuf.start_line;
pos_cnum = lexbuf.start_pos + lexbuf.offset;
pos_bol = lexbuf.start_bol;
}
let lexing_position_curr lexbuf =
{
Lexing.pos_fname = lexbuf.filename;
pos_lnum = lexbuf.curr_line;
pos_cnum = lexbuf.pos + lexbuf.offset;
pos_bol = lexbuf.curr_bol;
}
let lexing_positions lexbuf =
let start_p = lexing_position_start lexbuf
and curr_p = lexing_position_curr lexbuf in
(start_p, curr_p)
let lexing_bytes_position_start lexbuf =
{
Lexing.pos_fname = lexbuf.filename;
pos_lnum = lexbuf.start_line;
pos_cnum = lexbuf.start_bytes_pos + lexbuf.bytes_offset;
pos_bol = lexbuf.start_bytes_bol;
}
let lexing_bytes_position_curr lexbuf =
{
Lexing.pos_fname = lexbuf.filename;
pos_lnum = lexbuf.curr_line;
pos_cnum = lexbuf.bytes_pos + lexbuf.bytes_offset;
pos_bol = lexbuf.curr_bytes_bol;
}
let lexing_bytes_positions lexbuf =
let start_p = lexing_bytes_position_start lexbuf
and curr_p = lexing_bytes_position_curr lexbuf in
(start_p, curr_p)
let with_tokenizer lexer' lexbuf =
let lexer () =
let token = lexer' lexbuf in
let start_p, curr_p = lexing_positions lexbuf in
(token, start_p, curr_p)
in
lexer
module Chan = struct
exception Missing_input
type t = {
b : Bytes.t;
ic : in_channel;
mutable len : int;
mutable pos : int;
}
let min_buffer_size = 64
let create ic len : t =
let len = max len min_buffer_size in
{ b = Bytes.create len; ic; len = 0; pos = 0 }
let available (t : t) = t.len - t.pos
let rec ensure_bytes_available (t : t) ~can_refill n =
if available t >= n then ()
else if can_refill then (
let len = t.len - t.pos in
if len > 0 then Bytes.blit t.b t.pos t.b 0 len;
let read = input t.ic t.b len (Bytes.length t.b - len) in
t.len <- len + read;
t.pos <- 0;
if read = 0 then raise Missing_input
else ensure_bytes_available t ~can_refill n)
else raise Missing_input
let ensure_bytes_available t ~can_refill n =
if n <= 0 || n > min_buffer_size then invalid_arg "Sedlexing.Chan.ensure";
ensure_bytes_available t ~can_refill n
let get (t : t) i = Bytes.get t.b (t.pos + i)
let advance (t : t) n =
if t.pos + n > t.len then invalid_arg "advance";
t.pos <- t.pos + n
let raw_buf (t : t) = t.b
let raw_pos (t : t) = t.pos
end
let make_from_channel ?bytes_per_char ic ~max_bytes_per_uchar
~min_bytes_per_uchar ~read_uchar =
let t = Chan.create ic (chunk_size * max_bytes_per_uchar) in
let malformed = ref false in
let refill buf pos len =
let rec loop i =
if !malformed then raise MalFormed;
if i = len then i
else (
match
let can_refill = i = 0 in
Chan.ensure_bytes_available t ~can_refill min_bytes_per_uchar;
read_uchar ~can_refill t
with
| c ->
buf.(pos + i) <- c;
loop (i + 1)
| exception MalFormed when i <> 0 ->
malformed := true;
i
| exception Chan.Missing_input ->
if i = 0 && Chan.available t > 0 then raise MalFormed;
i)
in
loop 0
in
create ?bytes_per_char refill
module Latin1 = struct
let from_gen s =
from_gen ~bytes_per_char:(fun _ -> 1) (Gen.map Uchar.of_char s)
let from_string s =
let len = String.length s in
{
(empty_lexbuf (fun _ -> 1)) with
buf = Array.init len (fun i -> Uchar.of_char s.[i]);
len;
finished = true;
}
let from_channel ic =
make_from_channel ic
~bytes_per_char:(fun _ -> 1)
~min_bytes_per_uchar:1 ~max_bytes_per_uchar:1
~read_uchar:(fun ~can_refill:_ t ->
let c = Chan.get t 0 in
Chan.advance t 1;
Uchar.of_char c)
let[@inline] to_latin1 c =
if Uchar.is_char c then Uchar.unsafe_to_char c
else raise (InvalidCodepoint (Uchar.to_int c))
let lexeme_char lexbuf pos = to_latin1 (lexeme_char lexbuf pos)
let sub_lexeme lexbuf pos len =
let buf = lexbuf.buf in
let off =
sub_lexeme_offset "Sedlexing.Latin1.sub_lexeme" lexbuf buf pos len
in
let s = Bytes.create len in
for i = 0 to len - 1 do
Bytes.unsafe_set s i (to_latin1 (Array.unsafe_get buf (off + i)))
done;
Bytes.unsafe_to_string s
let lexeme lexbuf = sub_lexeme lexbuf 0 (lexbuf.pos - lexbuf.start_pos)
let of_submatch s = sub_lexeme s.lexbuf s.pos s.len
end
module Utf8 = struct
module Helper = struct
let width = function
| '\000' .. '\127' -> 1
| '\192' .. '\223' -> 2
| '\224' .. '\239' -> 3
| '\240' .. '\247' -> 4
| _ -> raise MalFormed
let check_two n1 n2 =
if n1 < 0xc2 || 0xdf < n1 then raise MalFormed;
if n2 < 0x80 || 0xbf < n2 then raise MalFormed;
if n2 lsr 6 != 0b10 then raise MalFormed;
((n1 land 0x1f) lsl 6) lor (n2 land 0x3f)
let check_three n1 n2 n3 =
if n1 = 0xe0 then (
if n2 < 0xa0 || 0xbf < n2 then raise MalFormed;
if n3 < 0x80 || 0xbf < n3 then raise MalFormed)
else (
if n1 < 0xe1 || 0xef < n1 then raise MalFormed;
if n2 < 0x80 || 0xbf < n2 then raise MalFormed;
if n3 < 0x80 || 0xbf < n3 then raise MalFormed);
if n2 lsr 6 != 0b10 || n3 lsr 6 != 0b10 then raise MalFormed;
let p =
((n1 land 0x0f) lsl 12) lor ((n2 land 0x3f) lsl 6) lor (n3 land 0x3f)
in
if p >= 0xd800 && p <= 0xdfff then raise MalFormed;
p
let check_four n1 n2 n3 n4 =
if n1 = 0xf0 then (
if n2 < 0x90 || 0xbf < n2 then raise MalFormed;
if n3 < 0x80 || 0xbf < n3 then raise MalFormed;
if n4 < 0x80 || 0xbf < n4 then raise MalFormed)
else if n1 = 0xf4 then (
if n2 < 0x80 || 0x8f < n2 then raise MalFormed;
if n3 < 0x80 || 0xbf < n3 then raise MalFormed;
if n4 < 0x80 || 0xbf < n4 then raise MalFormed)
else (
if n1 < 0xf1 || 0xf3 < n1 then raise MalFormed;
if n2 < 0x80 || 0xbf < n2 then raise MalFormed;
if n3 < 0x80 || 0xbf < n3 then raise MalFormed;
if n4 < 0x80 || 0xbf < n4 then raise MalFormed);
if n2 lsr 6 != 0b10 || n3 lsr 6 != 0b10 || n4 lsr 6 != 0b10 then
raise MalFormed;
((n1 land 0x07) lsl 18)
lor ((n2 land 0x3f) lsl 12)
lor ((n3 land 0x3f) lsl 6)
lor (n4 land 0x3f)
let next s i =
let c1 = s.[i] in
match width c1 with
| 1 -> Char.code c1
| 2 ->
let n1 = Char.code c1 in
let n2 = Char.code s.[i + 1] in
check_two n1 n2
| 3 ->
let n1 = Char.code c1 in
let n2 = Char.code s.[i + 1] in
let n3 = Char.code s.[i + 2] in
check_three n1 n2 n3
| 4 ->
let n1 = Char.code c1 in
let n2 = Char.code s.[i + 1] in
let n3 = Char.code s.[i + 2] in
let n4 = Char.code s.[i + 3] in
check_four n1 n2 n3 n4
| _ -> assert false
let gen_from_char_gen s =
let next_or_fail () =
match Gen.next s with None -> raise MalFormed | Some x -> Char.code x
in
fun () ->
Gen.next s >>| fun c1 ->
match width c1 with
| 1 -> Uchar.of_char c1
| 2 ->
let n1 = Char.code c1 in
let n2 = next_or_fail () in
Uchar.of_int (check_two n1 n2)
| 3 ->
let n1 = Char.code c1 in
let n2 = next_or_fail () in
let n3 = next_or_fail () in
Uchar.of_int (check_three n1 n2 n3)
| 4 ->
let n1 = Char.code c1 in
let n2 = next_or_fail () in
let n3 = next_or_fail () in
let n4 = next_or_fail () in
Uchar.of_int (check_four n1 n2 n3 n4)
| _ -> raise MalFormed
end
let from_channel ic =
make_from_channel ic ~bytes_per_char:Uchar.utf_8_byte_length
~min_bytes_per_uchar:1 ~max_bytes_per_uchar:4
~read_uchar:(fun ~can_refill t ->
let w = Helper.width (Chan.get t 0) in
Chan.ensure_bytes_available t ~can_refill w;
let c =
Helper.next (Bytes.unsafe_to_string (Chan.raw_buf t)) (Chan.raw_pos t)
in
Chan.advance t w;
Uchar.of_int c)
let from_gen s =
from_gen ~bytes_per_char:Uchar.utf_8_byte_length
(Helper.gen_from_char_gen s)
let from_string s =
from_gen (Gen.init ~limit:(String.length s) (fun i -> String.get s i))
let sub_lexeme lexbuf pos len =
let buf = lexbuf.buf in
let off =
sub_lexeme_offset "Sedlexing.Utf8.sub_lexeme" lexbuf buf pos len
in
let size = ref 0 in
for i = off to off + len - 1 do
size := !size + Uchar.utf_8_byte_length (Array.unsafe_get buf i)
done;
let s = Bytes.create !size in
if !size = len then
for i = 0 to len - 1 do
Bytes.unsafe_set s i
(Uchar.unsafe_to_char (Array.unsafe_get buf (off + i)))
done
else begin
let j = ref 0 in
for i = off to off + len - 1 do
j := !j + Bytes.set_utf_8_uchar s !j (Array.unsafe_get buf i)
done;
if !j <> !size then invalid_arg "Sedlexing.Utf8.sub_lexeme"
end;
Bytes.unsafe_to_string s
let lexeme lexbuf = sub_lexeme lexbuf 0 (lexbuf.pos - lexbuf.start_pos)
let of_submatch s = sub_lexeme s.lexbuf s.pos s.len
end
module Utf16 = struct
type byte_order = Little_endian | Big_endian
module Helper = struct
let number_of_pair bo c1 c2 =
match bo with
| Little_endian -> (c2 lsl 8) + c1
| Big_endian -> (c1 lsl 8) + c2
let get_bo bo c1 c2 =
match !bo with
| Some o -> o
| None ->
let o =
match (c1, c2) with
| 0xff, 0xfe -> Little_endian
| _ -> Big_endian
in
bo := Some o;
o
let gen_from_char_gen opt_bo s =
let next_or_fail () =
match Gen.next s with None -> raise MalFormed | Some x -> Char.code x
in
let bo = ref opt_bo in
fun () ->
Gen.next s >>| fun c1 ->
let n1 = Char.code c1 in
let n2 = next_or_fail () in
let o = get_bo bo n1 n2 in
let w1 = number_of_pair o n1 n2 in
if w1 = 0xfffe then raise (InvalidCodepoint w1);
if w1 < 0xd800 || 0xdfff < w1 then Uchar.of_int w1
else if w1 <= 0xdbff then (
let n3 = next_or_fail () in
let n4 = next_or_fail () in
let w2 = number_of_pair o n3 n4 in
if w2 < 0xdc00 || w2 > 0xdfff then raise MalFormed;
let upper10 = (w1 land 0x3ff) lsl 10 and lower10 = w2 land 0x3ff in
Uchar.of_int (0x10000 + upper10 + lower10))
else raise MalFormed
end
let from_channel ic opt_bo =
let bo = ref opt_bo in
make_from_channel ic ~bytes_per_char:Uchar.utf_16_byte_length
~min_bytes_per_uchar:2 ~max_bytes_per_uchar:4
~read_uchar:(fun ~can_refill t ->
let n1 = Char.code (Chan.get t 0) in
let n2 = Char.code (Chan.get t 1) in
let o = Helper.get_bo bo n1 n2 in
let w1 = Helper.number_of_pair o n1 n2 in
if w1 = 0xfffe then raise (InvalidCodepoint w1);
if w1 < 0xd800 || 0xdfff < w1 then (
Chan.advance t 2;
Uchar.of_int w1)
else if w1 <= 0xdbff then (
Chan.ensure_bytes_available t ~can_refill 4;
let n3 = Char.code (Chan.get t 2) in
let n4 = Char.code (Chan.get t 3) in
let w2 = Helper.number_of_pair o n3 n4 in
if w2 < 0xdc00 || w2 > 0xdfff then raise MalFormed;
let upper10 = (w1 land 0x3ff) lsl 10 and lower10 = w2 land 0x3ff in
Chan.advance t 4;
Uchar.of_int (0x10000 + upper10 + lower10))
else raise MalFormed)
let from_gen s opt_bo =
from_gen ~bytes_per_char:Uchar.utf_16_byte_length
(Helper.gen_from_char_gen opt_bo s)
let from_string s =
from_gen (Gen.init ~limit:(String.length s) (fun i -> String.get s i))
let sub_lexeme lb pos len bo bom =
let buf = lb.buf in
let off = sub_lexeme_offset "Sedlexing.Utf16.sub_lexeme" lb buf pos len in
let size = ref (if bom then 2 else 0) in
for i = off to off + len - 1 do
size := !size + Uchar.utf_16_byte_length (Array.unsafe_get buf i)
done;
let s = Bytes.create !size in
let written =
match bo with
| Big_endian ->
let j =
ref (if bom then Bytes.set_utf_16be_uchar s 0 Uchar.bom else 0)
in
for i = off to off + len - 1 do
j := !j + Bytes.set_utf_16be_uchar s !j (Array.unsafe_get buf i)
done;
!j
| Little_endian ->
let j =
ref (if bom then Bytes.set_utf_16le_uchar s 0 Uchar.bom else 0)
in
for i = off to off + len - 1 do
j := !j + Bytes.set_utf_16le_uchar s !j (Array.unsafe_get buf i)
done;
!j
in
if written <> !size then invalid_arg "Sedlexing.Utf16.sub_lexeme";
Bytes.unsafe_to_string s
let lexeme lb bo bom = sub_lexeme lb 0 (lb.pos - lb.start_pos) bo bom
let of_submatch s bo bom = sub_lexeme s.lexbuf s.pos s.len bo bom
end