forked from node-unicode/node-unicode-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-blocks-scripts-properties.js
More file actions
343 lines (330 loc) · 10.5 KB
/
parse-blocks-scripts-properties.js
File metadata and controls
343 lines (330 loc) · 10.5 KB
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
'use strict';
const looseMatch = require('unicode-loose-match');
const propertyAliases = require('unicode-property-aliases');
const regenerate = require('regenerate');
const utils = require('./utils.js');
const valueAliases = require('unicode-property-value-aliases');
const categoryAliases = valueAliases.get('General_Category');
const CODEPOINT_MAX = 0x10ffff;
const findCanonicalName = function(shortName) {
const canonicalName = propertyAliases.get(shortName);
if (!canonicalName) {
// This is useful when adding newer versions, but fails for older Unicode versions.
// TODO: Fix and re-enable the exception.
//throw new Error(`Failed to find canonical name for property ${shortName}. Update \`unicode-property-aliases\`.`);
return shortName;
}
return canonicalName;
};
const initialMapForType = function(type) {
switch (type) {
case 'scripts':
return {
Unknown: regenerate().addRange(0, CODEPOINT_MAX),
};
default:
return {};
}
};
/**
* @param {'blocks' | 'bidi-mirroring' | 'derived-binary-properties' | 'derived-core-properties' | 'derived-general-category' | 'scripts'} type
* @param {string} version
* @returns
*/
const parseBlocksScriptsProperties = function(type, version) {
const map = initialMapForType(type);
const source = utils.readDataFile(version, type);
if (!source) {
return;
}
const lines = source.split('\n');
for (const line of lines) {
if (
/^#/.test(line) ||
!(
/^(?:blocks|bidi-mirroring|derived-general-category|properties)$/.test(type)
? /;\x20/.test(line)
: /\x20;\x20/.test(line)
)
) {
continue;
}
const data = line.trim().split(';');
const charRange = data[0].replace('..', '-').trim();
let item = data[1].split(
type == 'blocks' ? ';' : '#'
)[0].trim().replace(/\x20/g, '_');
if (type == 'derived-normalization-properties') {
if (item == 'FNC') {
// Old Unicode versions up to v4.0.0 use the `FNC` alias instead of
// `FC_NFKC` (for `FC_NFKC_Closure`). This is not a binary property.
continue;
} else {
const canonical = findCanonicalName(item);
if (canonical) {
if (/FC_NFKC_Closure|^NFKC_Casefold$|(?:NFC|NFD|NFKC|NFKD)_Quick_Check/.test(canonical)) {
// These are not binary properties, or their default value (in the
// file) is not `True`.
continue;
}
item = canonical;
}
}
} else if (type == 'blocks') {
// Use canonical block names. See #34
const tmp = looseMatch('Block', item).value;
if (!tmp) {
throw new Error(`Canonical block name not found for ${item}. Update \`unicode-loose-match\`.`);
}
item = tmp;
} else if (type == 'bidi-mirroring') {
item = String.fromCodePoint(parseInt(item, 16));
} else if (type == 'derived-general-category') {
item = categoryAliases.get(item) || item
}
map[item] ??= regenerate();
const rangeParts = charRange.split('-');
if (rangeParts.length == 2) {
const start = parseInt(rangeParts[0], 16), end = parseInt(rangeParts[1], 16);
map[item].addRange(start, end);
if (type == 'scripts') {
map.Unknown.removeRange(start, end);
}
} else {
const codepoint = parseInt(charRange, 16);
map[item].add(codepoint);
if (type == 'scripts') {
map.Unknown.remove(codepoint);
}
}
}
return map;
};
const parseDerivedBinaryProperties = function(version) {
if (version === '3.1.1' || version === '3.1.0' || version === '3.0.1' || version === '3.0.0' || parseInt(version.split('.')[0], 10) < 3) {
// Unicode <= 3.1.1 does not provide derived-binary-properties,
// so we should derive Bidi_Mirrored from the UnicodeData
const source = utils.readDataFile(version, 'database');
if (!source) {
return;
}
const result = [];
for (const line of source.split('\n')) {
if (line === '' || line.startsWith('#')) {
continue;
}
const data = line.split(';');
if (data[9] === 'Y') {
const codePoint = parseInt(data[0], 16);
result.push(codePoint);
}
}
return {
Bidi_Mirrored: result
}
} else {
return parseBlocksScriptsProperties('derived-binary-properties', version);
}
}
const parseDerivedGeneralCategory = function (version) {
if (
version === '3.0.1' ||
version === '3.0.0' ||
parseInt(version.split('.')[0], 10) < 3
) {
// Unicode <= 3.0.1 does not provide derived-general-category,
// so we should derive General_Category from the UnicodeData
const source = utils.readDataFile(version, 'database');
if (!source) {
return;
}
const lines = source.split('\n');
const categoryMap = {
// Note: `Any`, `ASCII`, and `Assigned` are actually properties,
// not categories. http://unicode.org/reports/tr18/#Categories
Any: regenerate().addRange(0, CODEPOINT_MAX),
ASCII: regenerate().addRange(0, 0x7f),
Assigned: regenerate(),
};
let flag = false;
let first = 0;
let lastCodePoint = -1;
// http://unicode.org/reports/tr44/#GC_Values_Table
// http://unicode.org/reports/tr18/#Categories
for (const line of lines) {
if (line === '' || line.startsWith('#')) {
continue;
}
const data = line.trim().split(';');
const codePoint = parseInt(data[0], 16);
const name = data[1];
const generalCategory = data[2];
const categories = [
categoryAliases.get(generalCategory),
categoryAliases.get(generalCategory.charAt(0)),
'Assigned',
];
if (/^(?:Ll|Lu|Lt)$/.test(generalCategory)) {
categories.push(categoryAliases.get('LC'));
}
for (const category of categories) {
categoryMap[category] ??= regenerate();
}
if (flag) {
if (/<.+, Last>/.test(name)) {
flag = false;
for (const category of categories) {
categoryMap[category].addRange(first, codePoint);
}
} else {
throw Error('Database exception');
}
} else {
// If there is a gap within UnicodeData, it must be unassigned code points
if (lastCodePoint + 1 < codePoint) {
const categories = [
categoryAliases.get('C'),
categoryAliases.get('Cn'),
];
for (const category of categories) {
categoryMap[category] ??= regenerate();
categoryMap[category].addRange(lastCodePoint + 1, codePoint - 1);
}
}
if (/<.+, First>/.test(name)) {
flag = true;
first = codePoint;
} else {
for (const category of categories) {
categoryMap[category].add(codePoint);
}
}
}
lastCodePoint = codePoint;
}
if (lastCodePoint < CODEPOINT_MAX) {
// Add the last unassigned code point range
const categories = [categoryAliases.get('C'), categoryAliases.get('Cn')];
for (const category of categories) {
categoryMap[category] ??= regenerate();
categoryMap[category].addRange(lastCodePoint + 1, CODEPOINT_MAX);
}
}
return categoryMap;
} else {
const map = parseBlocksScriptsProperties(
'derived-general-category',
version
);
const getCategory = (shortName) =>
categoryAliases.get(shortName) || shortName;
const getCategoryMap = (shortName) => map[getCategory(shortName)];
// Note: `Any`, `ASCII`, and `Assigned` are actually properties,
// not categories. http://unicode.org/reports/tr18/#Categories
map['Any'] = regenerate().addRange(0, CODEPOINT_MAX);
map['ASCII'] = regenerate().addRange(0, 0x7f);
map['Assigned'] = regenerate()
.addRange(0, CODEPOINT_MAX)
.remove(getCategoryMap('Cn'));
// https://www.unicode.org/reports/tr44/#General_Category_Values
const categoryRules = {
LC: ['Lu', 'Ll', 'Lt'],
L: ['LC', 'Lm', 'Lo'],
M: ['Mn', 'Mc', 'Me'],
N: ['Nd', 'Nl', 'No'],
P: ['Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po'],
S: ['Sm', 'Sc', 'Sk', 'So'],
Z: ['Zs', 'Zl', 'Zp'],
C: ['Cc', 'Cf', 'Cs', 'Co', 'Cn'],
};
for (const [key, values] of Object.entries(categoryRules)) {
const result = regenerate();
for (const value of values) {
result.add(getCategoryMap(value));
}
map[getCategory(key)] = result;
}
return map;
}
}
const parseBlocks = function (version) {
if (version === '3.0.1' || version === '3.0.0' || parseInt(version.split('.')[0], 10) < 3) {
const source = utils.readDataFile(version, 'blocks');
if (!source) {
return;
}
const map = {};
for (const line of source.trimEnd().split('\n')) {
if (line.startsWith('#')) {
continue;
}
const [start, end, blockName] = line.split('; ');
const canonicalBlockName = looseMatch('Block', blockName).value;
map[canonicalBlockName] = regenerate().addRange(
parseInt(start, 16),
parseInt(end, 16)
);
}
return map;
} else {
return parseBlocksScriptsProperties('blocks', version);
}
}
const parseProperties = function (version) {
if (
version === '3.0.1' ||
version === '3.0.0' ||
parseInt(version.split('.')[0], 10) < 3
) {
const source = utils.readDataFile(version, 'properties');
if (!source) {
return;
}
const map = {};
const lines = source.trimEnd().split('\n');
let currentProperty, maybeProperty, maybeRange
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if ((maybeProperty = /0x[A-F\d]+ \((.+?)\)$/.exec(line)) != null) {
currentProperty = maybeProperty[1];
if (
// ignore Bidi_Class as they have been generated from UnicodeData
currentProperty.startsWith("Bidi:") ||
// ignore Not a Character and Unassigned Code Value as they have been generated from UnicodeData
currentProperty === "Not a Character" ||
currentProperty === "Unassigned Code Value"
) {
// skip contents
i++;
while (++i < lines.length && lines[i] !== "");
continue;
}
currentProperty = currentProperty.replaceAll(' ', '_');
currentProperty = findCanonicalName(currentProperty) ?? currentProperty;
map[currentProperty] = regenerate();
} else if ((maybeRange = /^([A-F\d]{4,5})(?:..([A-F\d]{4,5}))?/.exec(line)) != null) {
if (maybeRange[2]) {
map[currentProperty].addRange(
parseInt(maybeRange[1], 16),
parseInt(maybeRange[2], 16)
);
} else {
map[currentProperty].add(parseInt(maybeRange[1], 16));
}
}
}
return map;
} else {
return parseBlocksScriptsProperties('properties', version);
}
};
module.exports = {
'parseScripts': parseBlocksScriptsProperties.bind(null, 'scripts'),
'parseProperties': parseProperties,
'parseDerivedCoreProperties': parseBlocksScriptsProperties.bind(null, 'derived-core-properties'),
'parseDerivedNormalizationProperties': parseBlocksScriptsProperties.bind(null, 'derived-normalization-properties'),
'parseBlocks': parseBlocks,
'parseMirroring': parseBlocksScriptsProperties.bind(null, 'bidi-mirroring'),
'parseDerivedBinaryProperties': parseDerivedBinaryProperties,
'parseDerivedGeneralCategory': parseDerivedGeneralCategory
};