Debian Codegraph @

ide/rocqide/utf8_convert.mll

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 {
12 open Lexing
13 let b = Buffer.create 127
14
15 }
16
17 (* Replace all occurrences of \x{iiii} and \x{iiiiiiii} by UTF-8 valid chars *)
18
19 let digit = ['0'-'9''A'-'Z''a'-'z']
20 let short = digit digit digit digit
21 let long = short short
22
23 rule entry = parse
24 | "\\x{" (short | long ) '}'
25 { let s = lexeme lexbuf in
26 let n = String.length s in
27 let code =
28 try Glib.Utf8.from_unichar
29 (int_of_string ("0x"^(String.sub s 3 (n - 4))))
30 with _ -> s
31 in
32 let c = if Glib.Utf8.validate code then code else s in
33 Buffer.add_string b c;
34 entry lexbuf
35 }
36 | _
37 { let s = lexeme lexbuf in
38 Buffer.add_string b s;
39 entry lexbuf}
40 | eof
41 {
42 let s = Buffer.contents b in Buffer.reset b ; s
43 }
44
45
46 {
47 let f s =
48 let lb = from_string s in
49 Buffer.reset b;
50 entry lb
51 }