-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
302 lines (258 loc) · 7.96 KB
/
parser.js
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
const fs = require('fs');
module.exports = function (ucdPath) {
let codePoint, cp, end, line, name, parts;
if (ucdPath == null) {
ucdPath = __dirname + '/data';
}
const codePoints = [];
const parseCodes = function (code) {
if (!code) {
return null;
}
return code.split(' ').filter((i) => i).map((i) => parseInt(i, 16));
};
// Class that represents a unicode code point
class CodePoint {
constructor(parts) {
let decimal, digit;
[this.code, this.name, this.category,
this.combiningClass, this.bidiClass,
this.decomposition, decimal, digit,
this.numeric, this.bidiMirrored, this.unicode1Name,
this.isoComment, this.uppercase, this.lowercase,
this.titlecase] = parts;
this.code = parseInt(this.code, 16);
this.combiningClass = parseInt(this.combiningClass) || 0;
this.decomposition = parseCodes(this.decomposition) || [];
this.bidiMirrored = this.bidiMirrored === 'Y';
this.uppercase = this.uppercase ? [parseInt(this.uppercase, 16)] : null;
this.lowercase = this.lowercase ? [parseInt(this.lowercase, 16)] : null;
this.titlecase = this.titlecase ? [parseInt(this.titlecase, 16)] : null;
this.folded = null;
this.caseConditions = null;
this.isCompat = false;
this.isExcluded = false;
if (this.decomposition.length && isNaN(this.decomposition[0])) {
this.isCompat = true;
this.decomposition.shift();
}
this.compositions = {}; // set later
this.combiningClassName = null;
this.block = null;
this.script = null;
this.eastAsianWidth = null;
this.joiningType = null;
this.joiningGroup = null;
this.indicSyllabicCategory = null;
this.indicPositionalCategory = null;
for (let prop of ['NFD_QC', 'NFKD_QC', 'NFC_QC', 'NFKC_QC']) {
this[prop] = 0; // Yes
}
if ((decimal && (decimal !== this.numeric)) || (digit && (digit !== this.numeric))) {
throw new Error('Decimal or digit does not match numeric value');
}
}
copy(codePoint) {
const res = new CodePoint([]);
for (let key of Object.keys(this)) {
const val = this[key];
res[key] = val;
}
res.code = codePoint;
return res;
}
}
// Parse the main unicode data file
let data = fs.readFileSync(ucdPath + '/UnicodeData.txt', 'ascii');
let rangeStart = -1;
for (line of data.split('\n')) {
if (line.length > 0) {
parts = line.split(';');
name = parts[1];
codePoint = new CodePoint(parts);
if (rangeStart >= 0) {
var i;
if (!/<.+, Last>/.test(name)) {
throw new Error('No range end found');
}
for (i = rangeStart, cp = i, end = codePoint.code; i <= end; i++ , cp = i) {
codePoints[cp] = codePoint.copy(cp);
}
rangeStart = -1;
} else if (/<.+, First>/.test(name)) {
rangeStart = codePoint.code;
} else {
codePoints[codePoint.code] = codePoint;
}
}
}
// Helper function to read a file from the unicode database
const readFile = function (filename, parse, fn) {
if (typeof parse === 'function') {
fn = parse;
parse = true;
}
data = fs.readFileSync(ucdPath + '/' + filename, 'ascii');
for (line of data.split('\n')) {
line = line.trim();
if ((line.length > 0) && (line[0] !== '#')) {
parts = line.replace(/\s*#.*$/, '').split(/\s*;\s*/);
// Parse codepoint range if requested
if (parse) {
var match = parts[0].match(/([a-z0-9]+)\.\.([a-z0-9]+)/i);
if (match) {
const start = parseInt(match[1], 16);
end = parseInt(match[2], 16);
parts[0] = [start, end];
} else {
cp = parseInt(parts[0], 16);
if (!isNaN(cp)) {
parts[0] = [cp, cp];
}
}
}
fn(parts);
}
}
};
readFile('extracted/DerivedNumericValues.txt', function (parts) {
let start;
[start, end] = parts[0];
const val = parts[3];
for (cp = start; cp <= end; cp++) {
codePoint = codePoints[cp];
if (!codePoint.numeric) {
cp.numeric = val;
}
}
});
const combiningClasses = {};
const joiningTypes = {};
readFile('PropertyValueAliases.txt', false, function (parts) {
if (parts[0] === 'ccc') {
const num = parseInt(parts[1]);
name = parts[3];
combiningClasses[num] = name;
}
if (parts[0] === 'jt') {
joiningTypes[parts[1]] = parts[2];
}
});
for (codePoint of codePoints) {
if (codePoint) {
codePoint.combiningClassName = combiningClasses[codePoint.combiningClass];
}
}
readFile('Blocks.txt', function (parts) {
let start;
[start, end] = parts[0];
for (cp = start; cp <= end; cp++) {
if (codePoints[cp]) {
codePoints[cp].block = parts[1];
}
}
});
readFile('Scripts.txt', function (parts) {
let start;
[start, end] = parts[0];
for (cp = start; cp <= end; cp++) {
if (codePoints[cp]) {
codePoints[cp].script = parts[1];
}
}
});
readFile('EastAsianWidth.txt', function (parts) {
let start;
[start, end] = parts[0];
for (cp = start; cp <= end; cp++) {
if (codePoints[cp]) {
codePoints[cp].eastAsianWidth = parts[1];
}
}
});
readFile('SpecialCasing.txt', function (parts) {
let conditions;
const code = parts[0][0];
const lower = parseCodes(parts[1]);
const title = parseCodes(parts[2]);
const upper = parseCodes(parts[3]);
if (parts[4]) {
conditions = parts[4].split(/\s+/);
}
if (!conditions) {
codePoint = codePoints[code];
codePoint.uppercase = upper;
codePoint.lowercase = lower;
codePoint.titlecase = title;
}
if (conditions) {
codePoint.caseConditions = conditions;
}
});
readFile('CaseFolding.txt', function (parts) {
const code = parts[0][0];
const type = parts[1];
const folded = parseCodes(parts[2]);
if (['C', 'F'].includes(type)) {
codePoint = codePoints[code];
if (!codePoint.lowercase || (codePoint.lowercase.join('|') !== folded.join('|'))) {
codePoint.folded = folded;
}
}
});
readFile('CompositionExclusions.txt', function (parts) {
const code = parts[0][0];
codePoints[code].isExcluded = true;
});
readFile('DerivedNormalizationProps.txt', function (parts) {
let start;
[start, end] = parts[0];
let prop = parts[1];
let val = parts[2];
if (['NFD_QC', 'NFKD_QC', 'NFC_QC', 'NFKC_QC'].includes(prop)) {
for (let code = start; code <= end; code++) {
codePoints[code][prop] = val === 'Y' ? 0 : val === 'N' ? 1 : 2;
}
}
});
readFile('ArabicShaping.txt', function (parts) {
let start;
[start, end] = parts[0];
let name = parts[1];
let joiningType = parts[2];
let joiningGroup = parts[3];
for (let code = start; code <= end; code++) {
if (codePoints[code]) {
codePoints[code].joiningType = joiningTypes[joiningType];
codePoints[code].joiningGroup = joiningGroup;
}
}
});
readFile('IndicPositionalCategory.txt', function (parts) {
let start;
[start, end] = parts[0];
let prop = parts[1];
for (let code = start; code <= end; code++) {
if (codePoints[code]) {
codePoints[code].indicPositionalCategory = prop;
}
}
});
readFile('IndicSyllabicCategory.txt', function (parts) {
let start;
[start, end] = parts[0];
let prop = parts[1];
for (let code = start; code <= end; code++) {
if (codePoints[code]) {
codePoints[code].indicSyllabicCategory = prop;
}
}
});
for (codePoint of codePoints) {
if (codePoint && (codePoint.decomposition.length > 1) && !codePoint.isCompat && !codePoint.isExcluded) {
cp = codePoints[codePoint.decomposition[1]];
cp.compositions[codePoint.decomposition[0]] = codePoint.code;
}
}
return codePoints;
};