-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-unicode.js
284 lines (217 loc) · 6.74 KB
/
generate-unicode.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
/*
=== Unicode Test Generator for ES6 Parsers ===
When parsing ES6, there are three Unicode character classifications
that we are interested in:
- Whitespace: In addition to the whitespace characters explicitly
defined in the ES6 specification, we also need to recognize any
character with a Unicode general category of 'Zs'. The predefined
whitespace characters are:
- U+0009 <TAB> [\t]
- U+000B <VT> [\v]
- U+000C <FF> [\f]
- U+0020 <SP> [ ]
- U+00A0 <NBSP>
- U+FEFF <BOM>
In order to find the other whitespace characters, we download the
text file located at http://www.unicode.org/Public/UNIDATA/PropList.txt
and find all of the character ranges with the 'White_Space' property
and whose general category is 'Zs'.
- IdentifierStart and IdentifierPart: In addition to the identifier
characters explicitly defined in the ES6 specification, we also need
to recognize characters with a Unicode derived property of 'ID_Start'
or 'ID_Continue'. The predefined identifier characters are:
- U+0024 [$] IdentifierStart
- U+005F [_] IdentifierStart
- U+200C <ZWNJ> IdentifierPart
- U+200D <ZWJ> IdentifierPart
In order to find the other identifier characters, we download the
text file located at http://www.unicode.org/Public/UNIDATA/DerivedCoreProperties.txt
and find all of the character ranges with a Unicode derived property
of 'ID_Start' or 'ID_Continue' which can be represented in UTF-16 as
a single code unit.
After obtaining the list of valid code points for whitespace and identifiers,
we build a list of ranges and output code
*/
const Path = require('path');
const HTTP = require('http');
const FS = require('fs');
function log(msg) {
console.log('...' + msg);
}
function downloadDatabase(url, filePath, callback) {
let filename = Path.basename(filePath);
if (FS.existsSync(filePath)) {
log(filename + ' database already downloaded');
return callback();
}
log('Downloading ' + filename + ' database');
HTTP.get(url, response => {
let out = FS.createWriteStream(filePath);
response.pipe(out);
response.on('end', callback);
}).on('error', fail);
}
function getUnicodeVersion(data) {
let version = /^#[^\n\d]*(\d+(?:\.\d+)*)/;
let date = /\n#\s*Date:\s*(.+)/;
let match;
if (match = version.exec(data)) {
unicodeVersion = match[1];
}
if (match = date.exec(data)) {
unicodeDate = match[1];
}
}
function foldRanges(map) {
let ranges = [];
let range;
Object.keys(map).sort((a, b) => +(a) - b).forEach(val => {
// Convert val to a number
val = +val;
if (range && val === range.to + 1 && map[val] === range.type) {
range.to = val;
} else {
range = {
from: val,
to: val,
type: map[val],
};
ranges.push(range);
}
});
return ranges;
}
let unicodeVersion = '';
let unicodeDate = '';
let wsRanges;
let idRanges;
const BASE_URL = 'http://www.unicode.org/Public/UNIDATA';
const PROP_LIST_URL = `${ BASE_URL }/PropList.txt`;
const DERIVED_PROPS_URL = `${ BASE_URL }/DerivedCoreProperties.txt`;
const WORK_FOLDER = Path.resolve(__dirname, '_unicode');
const PROP_LIST_PATH = Path.resolve(WORK_FOLDER, 'PropList.txt');
const DERIVED_PROPS_PATH = Path.resolve(WORK_FOLDER, 'DerivedCoreProperties.txt');
const JS_OUT_PATH = Path.resolve(WORK_FOLDER, 'UnicodeData.js');
// Whitespace
const CP_TAB = 0x09;
const CP_VT = 0x0b;
const CP_FF = 0x0c;
const CP_SP = 0x20;
const CP_NBSP = 0xa0;
const CP_BOM = 0xfeff;
// Identifiers
const CP_DOLLAR = 0x24;
const CP_UNDERSCORE = 0x5f;
const CP_ZWNJ = 0x200c;
const CP_ZWJ = 0x200d;
// Code Point Types
const WHITE_SPACE = 1;
const ID_START = 2;
const ID_CONT = 3;
const Tasks = {
start() {
log('Unicode Test Generator for ES6 Parsers');
nextTask();
},
createWorkFolder() {
if (!FS.existsSync(WORK_FOLDER)) {
log('Creating work folder');
FS.mkdirSync(WORK_FOLDER);
}
nextTask();
},
getPropList() {
downloadDatabase(PROP_LIST_URL, PROP_LIST_PATH, nextTask);
},
getDerivedProps() {
downloadDatabase(DERIVED_PROPS_URL, DERIVED_PROPS_PATH, nextTask);
},
loadWhiteSpaceChars() {
let wsMap = {};
// Load pre-defined whitespace code points
wsMap[CP_TAB] =
wsMap[CP_VT] =
wsMap[CP_FF] =
wsMap[CP_SP] =
wsMap[CP_NBSP] =
wsMap[CP_BOM] = WHITE_SPACE;
log('Opening PropList.txt database');
let propList = FS.readFileSync(PROP_LIST_PATH, {
encoding: 'utf8'
});
let pattern = /\n([a-fA-F0-9\.]+)\s+;\s*White_Space\s*#\s*Zs/g;
let match;
getUnicodeVersion(propList);
log('Unicode ' + unicodeVersion + ' | ' + unicodeDate);
log('Scanning PropList.txt for White_Space (Zs)');
while (match = pattern.exec(propList)) {
let range = match[1].split('..').map(val => parseInt(val, 16));
if (range.length === 1) {
range[1] = range[0];
}
for (let code = range[0]; code <= range[1]; ++code) {
wsMap[code] = WHITE_SPACE;
}
}
wsRanges = foldRanges(wsMap);
nextTask();
},
loadIdentChars() {
let idMap = {};
// Load pre-defined identifier code points
idMap[CP_DOLLAR] = ID_START;
idMap[CP_UNDERSCORE] = ID_START;
idMap[CP_ZWNJ] = ID_CONT;
idMap[CP_ZWJ] = ID_CONT;
log('Opening DerivedCoreProperties.txt database');
let propList = FS.readFileSync(DERIVED_PROPS_PATH, {
encoding: 'utf8'
});
let pattern = /\n([a-fA-F0-9\.]+)\s+;\s*ID_(Start|Continue)\s+/g;
let match;
log('Scanning DerivedCoreProperties.txt for ID_Start and ID_Continue');
while (match = pattern.exec(propList)) {
let range = match[1].split('..').map(val => parseInt(val, 16));
if (range.length === 1) {
range[1] = range[0];
}
for (let code = range[0]; code <= range[1]; ++code) {
if (idMap[code] !== ID_START) {
idMap[code] = match[2] === 'Start' ? ID_START : ID_CONT;
}
}
}
idRanges = foldRanges(idMap);
nextTask();
},
exportJS() {
log('Generating javascript code');
function rangesToArrayString(ranges) {
return '[\n' +
ranges.map(range => {
return ` ${ range.from }, ${ range.to - range.from }, ${ range.type },\n`;
}).join('') +
']';
}
let out = '// Unicode ' + unicodeVersion + ' | ' + unicodeDate + '\n\n' +
'export const IDENTIFIER = ' + rangesToArrayString(idRanges) + ';\n\n' +
'export const WHITESPACE = ' + rangesToArrayString(wsRanges) + ';\n';
FS.writeFileSync(JS_OUT_PATH, out);
nextTask();
},
};
Tasks.list = Object.keys(Tasks);
function nextTask() {
if (Tasks.list.length === 0) {
return complete();
}
let next = Tasks.list.shift();
Tasks[next]();
}
function fail(err) {
log('Oops!', err);
}
function complete() {
log('Finished.');
}
nextTask();