Debian Codegraph @

src/3rdparty/masm/yarr/generateYarrCanonicalizeUnicode

raw
1 #! /usr/bin/env python
2
3 # Copyright (C) 2016 Apple Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 # This tool processes the Unicode Character Database file CaseFolding.txt to create
27 # canonicalization table as decribed in ECMAScript 6 standard in section
28 # "21.2.2.8.2 Runtime Semantics: Canonicalize()", step 2.
29
30 import optparse
31 import os
32 import re
33 import sys
34
35 header = """/*
36 * Copyright (C) 2016 Apple Inc. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
49 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
52 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60 // DO NO EDIT! - This file was generated by generateYarrCanonicalizeUnicode
61
62 #include "config.h"
63 #include "YarrCanonicalize.h"
64
65 namespace JSC { namespace Yarr {
66
67 """
68
69 footer = """} } // JSC::Yarr
70 """
71
72 MaxUnicode = 0x10ffff
73 commonAndSimpleLinesRE = re.compile(r"(?P<code>[0-9A-F]+)\s*;\s*[CS]\s*;\s*(?P<mapping>[0-9A-F]+)", re.IGNORECASE)
74
75 def openOrExit(path, mode):
76 try:
77 dirname = os.path.dirname(path)
78 if not os.path.isdir(dirname):
79 os.makedirs(dirname)
80 if sys.version_info.major >= 3:
81 return open(path, mode, encoding="UTF-8")
82 else:
83 return open(path, mode)
84 except IOError as e:
85 print("I/O error opening {0}, ({1}): {2}".format(path, e.errno, e.strerror))
86 exit(1)
87
88 class Canonicalize:
89 def __init__(self):
90 self.canonicalGroups = {};
91
92 def addMapping(self, code, mapping):
93 if mapping not in self.canonicalGroups:
94 self.canonicalGroups[mapping] = []
95 self.canonicalGroups[mapping].append(code)
96
97 def readCaseFolding(self, file):
98 codesSeen = set()
99 for line in file:
100 line = line.split('#', 1)[0]
101 line = line.rstrip()
102 if (not len(line)):
103 continue
104
105 fields = commonAndSimpleLinesRE.match(line)
106 if (not fields):
107 continue
108
109 code = int(fields.group('code'), 16)
110 mapping = int(fields.group('mapping'), 16)
111
112 codesSeen.add(code)
113 self.addMapping(code, mapping)
114
115 for i in range(MaxUnicode + 1):
116 if i in codesSeen:
117 continue;
118
119 self.addMapping(i, i)
120
121 def createTables(self, file):
122 typeInfo = [""] * (MaxUnicode + 1)
123 characterSets = []
124
125 for mapping in sorted(self.canonicalGroups.keys()):
126 characters = self.canonicalGroups[mapping]
127 if len(characters) == 1:
128 typeInfo[characters[0]] = "CanonicalizeUnique:0"
129 else:
130 characters.sort()
131 if len(characters) > 2:
132 for ch in characters:
133 typeInfo[ch] = "CanonicalizeSet:%d" % len(characterSets)
134 characterSets.append(characters)
135 else:
136 low = characters[0]
137 high = characters[1]
138 delta = high - low
139 if delta == 1:
140 type = "CanonicalizeAlternatingUnaligned:0" if low & 1 else "CanonicalizeAlternatingAligned:0"
141 typeInfo[low] = type
142 typeInfo[high] = type
143 else:
144 typeInfo[low] = "CanonicalizeRangeLo:%d" % delta
145 typeInfo[high] = "CanonicalizeRangeHi:%d" % delta
146
147 rangeInfo = []
148 end = 0
149 while end <= MaxUnicode:
150 begin = end
151 type = typeInfo[end]
152 while end < MaxUnicode and typeInfo[end + 1] == type:
153 end = end + 1
154 rangeInfo.append({"begin": begin, "end": end, "type": type})
155 end = end + 1
156
157 for i in range(len(characterSets)):
158 characters = ""
159 cur_set = characterSets[i]
160 for ch in cur_set:
161 characters = characters + "0x{character:04x}, ".format(character=ch)
162 file.write("const UChar32 unicodeCharacterSet{index:d}[] = {{ {characters}0 }};\n".format(index=i, characters=characters))
163
164 file.write("\n")
165 file.write("static const size_t UNICODE_CANONICALIZATION_SETS = {setCount:d};\n".format(setCount=len(characterSets)))
166 file.write("const UChar32* const unicodeCharacterSetInfo[UNICODE_CANONICALIZATION_SETS] = {\n")
167
168 for i in range(len(characterSets)):
169 file.write(" unicodeCharacterSet{setNumber:d},\n".format(setNumber=i))
170
171 file.write("};\n")
172 file.write("\n")
173 file.write("const size_t UNICODE_CANONICALIZATION_RANGES = {rangeCount:d};\n".format(rangeCount=len(rangeInfo)))
174 file.write("const CanonicalizationRange unicodeRangeInfo[UNICODE_CANONICALIZATION_RANGES] = {\n")
175
176 for info in rangeInfo:
177 typeAndValue = info["type"].split(":")
178 file.write(" {{ 0x{begin:04x}, 0x{end:04x}, 0x{value:04x}, {type} }},\n".format(begin=info["begin"], end=info["end"], value=int(typeAndValue[1]), type=typeAndValue[0]))
179
180 file.write("};\n")
181 file.write("\n")
182
183
184 if __name__ == "__main__":
185 parser = optparse.OptionParser(usage = "usage: %prog <CaseFolding.txt> <YarrCanonicalizeUnicode.h>")
186 (options, args) = parser.parse_args()
187
188 if len(args) != 2:
189 parser.error("<CaseFolding.txt> <YarrCanonicalizeUnicode.h>")
190
191 caseFoldingTxtPath = args[0]
192 canonicalizeHPath = args[1]
193 caseFoldingTxtFile = openOrExit(caseFoldingTxtPath, "r")
194 canonicalizeHFile = openOrExit(canonicalizeHPath, "w")
195
196 canonicalize = Canonicalize()
197 canonicalize.readCaseFolding(caseFoldingTxtFile)
198
199 canonicalizeHFile.write(header);
200 canonicalize.createTables(canonicalizeHFile)
201 canonicalizeHFile.write(footer);
202
203 caseFoldingTxtFile.close()
204 canonicalizeHFile.close()
205
206 exit(0)