annotate llvm/utils/unicode-case-fold.py @ 198:cef006dc7fd5

CurLexer vanish after EnterMacro finish
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 02 Jun 2021 23:09:47 +0900
parents 1d019706d866
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 #!/usr/bin/env python
anatofuz
parents:
diff changeset
2 """
anatofuz
parents:
diff changeset
3 Unicode case folding database conversion utility
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 Parses the database and generates a C++ function which implements the case
anatofuz
parents:
diff changeset
6 folding algorithm. The database entries are of the form:
anatofuz
parents:
diff changeset
7
anatofuz
parents:
diff changeset
8 <code>; <status>; <mapping>; # <name>
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 <status> can be one of four characters:
anatofuz
parents:
diff changeset
11 C - Common mappings
anatofuz
parents:
diff changeset
12 S - mappings for Simple case folding
anatofuz
parents:
diff changeset
13 F - mappings for Full case folding
anatofuz
parents:
diff changeset
14 T - special case for Turkish I characters
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 Right now this generates a function which implements simple case folding (C+S
anatofuz
parents:
diff changeset
17 entries).
anatofuz
parents:
diff changeset
18 """
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 from __future__ import print_function
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 import sys
anatofuz
parents:
diff changeset
23 import re
anatofuz
parents:
diff changeset
24 try:
anatofuz
parents:
diff changeset
25 from urllib.request import urlopen
anatofuz
parents:
diff changeset
26 except ImportError:
anatofuz
parents:
diff changeset
27 from urllib2 import urlopen
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 # This variable will body of the mappings function
anatofuz
parents:
diff changeset
31 body = ""
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 # Reads file line-by-line, extracts Common and Simple case fold mappings and
anatofuz
parents:
diff changeset
34 # returns a (from_char, to_char, from_name) tuple.
anatofuz
parents:
diff changeset
35 def mappings(f):
anatofuz
parents:
diff changeset
36 previous_from = -1
anatofuz
parents:
diff changeset
37 expr = re.compile(r'^(.*); [CS]; (.*); # (.*)')
anatofuz
parents:
diff changeset
38 for line in f:
anatofuz
parents:
diff changeset
39 m = expr.match(line)
anatofuz
parents:
diff changeset
40 if not m: continue
anatofuz
parents:
diff changeset
41 from_char = int(m.group(1), 16)
anatofuz
parents:
diff changeset
42 to_char = int(m.group(2), 16)
anatofuz
parents:
diff changeset
43 from_name = m.group(3)
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 if from_char <= previous_from:
anatofuz
parents:
diff changeset
46 raise Exception("Duplicate or unsorted characters in input")
anatofuz
parents:
diff changeset
47 yield from_char, to_char, from_name
anatofuz
parents:
diff changeset
48 previous_from = from_char
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 # Computes the shift (to_char - from_char) in a mapping.
anatofuz
parents:
diff changeset
51 def shift(mapping):
anatofuz
parents:
diff changeset
52 return mapping[1] - mapping[0]
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 # Computes the stride (from_char2 - from_char1) of two mappings.
anatofuz
parents:
diff changeset
55 def stride2(mapping1, mapping2):
anatofuz
parents:
diff changeset
56 return mapping2[0] - mapping1[0]
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 # Computes the stride of a list of mappings. The list should have at least two
anatofuz
parents:
diff changeset
59 # mappings. All mappings in the list are assumed to have the same stride.
anatofuz
parents:
diff changeset
60 def stride(block):
anatofuz
parents:
diff changeset
61 return stride2(block[0], block[1])
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 # b is a list of mappings. All the mappings are assumed to have the same
anatofuz
parents:
diff changeset
65 # shift and the stride between adjecant mappings (if any) is constant.
anatofuz
parents:
diff changeset
66 def dump_block(b):
anatofuz
parents:
diff changeset
67 global body
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 if len(b) == 1:
anatofuz
parents:
diff changeset
70 # Special case for handling blocks of length 1. We don't even need to
anatofuz
parents:
diff changeset
71 # emit the "if (C < X) return C" check below as all characters in this
anatofuz
parents:
diff changeset
72 # range will be caught by the "C < X" check emitted by the first
anatofuz
parents:
diff changeset
73 # non-trivial block.
anatofuz
parents:
diff changeset
74 body += " // {2}\n if (C == {0:#06x})\n return {1:#06x};\n".format(*b[0])
anatofuz
parents:
diff changeset
75 return
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 first = b[0][0]
anatofuz
parents:
diff changeset
78 last = first + stride(b) * (len(b)-1)
anatofuz
parents:
diff changeset
79 modulo = first % stride(b)
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 # All characters before this block map to themselves.
anatofuz
parents:
diff changeset
82 body += " if (C < {0:#06x})\n return C;\n".format(first)
anatofuz
parents:
diff changeset
83 body += " // {0} characters\n".format(len(b))
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 # Generic pattern: check upper bound (lower bound is checked by the "if"
anatofuz
parents:
diff changeset
86 # above) and modulo of C, return C+shift.
anatofuz
parents:
diff changeset
87 pattern = " if (C <= {0:#06x} && C % {1} == {2})\n return C + {3};\n"
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 if stride(b) == 2 and shift(b[0]) == 1 and modulo == 0:
anatofuz
parents:
diff changeset
90 # Special case:
anatofuz
parents:
diff changeset
91 # We can elide the modulo-check because the expression "C|1" will map
anatofuz
parents:
diff changeset
92 # the intervening characters to themselves.
anatofuz
parents:
diff changeset
93 pattern = " if (C <= {0:#06x})\n return C | 1;\n"
anatofuz
parents:
diff changeset
94 elif stride(b) == 1:
anatofuz
parents:
diff changeset
95 # Another special case: X % 1 is always zero, so don't emit the
anatofuz
parents:
diff changeset
96 # modulo-check.
anatofuz
parents:
diff changeset
97 pattern = " if (C <= {0:#06x})\n return C + {3};\n"
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 body += pattern.format(last, stride(b), modulo, shift(b[0]))
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 current_block = []
anatofuz
parents:
diff changeset
102 f = urlopen(sys.argv[1])
anatofuz
parents:
diff changeset
103 for m in mappings(f):
anatofuz
parents:
diff changeset
104 if len(current_block) == 0:
anatofuz
parents:
diff changeset
105 current_block.append(m)
anatofuz
parents:
diff changeset
106 continue
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 if shift(current_block[0]) != shift(m):
anatofuz
parents:
diff changeset
109 # Incompatible shift, start a new block.
anatofuz
parents:
diff changeset
110 dump_block(current_block)
anatofuz
parents:
diff changeset
111 current_block = [m]
anatofuz
parents:
diff changeset
112 continue
anatofuz
parents:
diff changeset
113
anatofuz
parents:
diff changeset
114 if len(current_block) == 1 or stride(current_block) == stride2(current_block[-1], m):
anatofuz
parents:
diff changeset
115 current_block.append(m)
anatofuz
parents:
diff changeset
116 continue
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 # Incompatible stride, start a new block.
anatofuz
parents:
diff changeset
119 dump_block(current_block)
anatofuz
parents:
diff changeset
120 current_block = [m]
anatofuz
parents:
diff changeset
121 f.close()
anatofuz
parents:
diff changeset
122
anatofuz
parents:
diff changeset
123 dump_block(current_block)
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 print('//===---------- Support/UnicodeCaseFold.cpp -------------------------------===//')
anatofuz
parents:
diff changeset
126 print('//')
anatofuz
parents:
diff changeset
127 print('// This file was generated by utils/unicode-case-fold.py from the Unicode')
anatofuz
parents:
diff changeset
128 print('// case folding database at')
anatofuz
parents:
diff changeset
129 print('// ', sys.argv[1])
anatofuz
parents:
diff changeset
130 print('//')
anatofuz
parents:
diff changeset
131 print('// To regenerate this file, run:')
anatofuz
parents:
diff changeset
132 print('// utils/unicode-case-fold.py \\')
anatofuz
parents:
diff changeset
133 print('// "{}" \\'.format(sys.argv[1]))
anatofuz
parents:
diff changeset
134 print('// > lib/Support/UnicodeCaseFold.cpp')
anatofuz
parents:
diff changeset
135 print('//')
anatofuz
parents:
diff changeset
136 print('//===----------------------------------------------------------------------===//')
anatofuz
parents:
diff changeset
137 print('')
anatofuz
parents:
diff changeset
138 print('#include "llvm/Support/Unicode.h"')
anatofuz
parents:
diff changeset
139 print('')
anatofuz
parents:
diff changeset
140 print("int llvm::sys::unicode::foldCharSimple(int C) {")
anatofuz
parents:
diff changeset
141 print(body)
anatofuz
parents:
diff changeset
142 print(" return C;")
anatofuz
parents:
diff changeset
143 print("}")