forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregen-nonascii.js
78 lines (66 loc) · 1.86 KB
/
regen-nonascii.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
// Regenerate the table used to scan non-ASCII identifiers.
import assert from "assert";
import { writeFileSync } from "fs";
import { resolve } from "path";
import { fileURLToPath } from "url";
const disallowedProperties = [
"General_Category=Control",
"General_Category=Private_Use",
"General_Category=Surrogate",
"Noncharacter_Code_Point",
"Pattern_White_Space",
"Unassigned",
];
const disallowedCodePoints = [
0xfffd, // REPLACEMENT CHARACTER
];
const MIN_NONASCII_CODEPOINT = 0x80;
const MAX_UNICODE_CODEPOINT = 0x10ffff;
const disallowedRegex = new RegExp(
`[${disallowedProperties.map((p) => `\\p{${p}}`).join("")}]`,
"u"
);
const map = computeMap();
function isDisallowed(codePoint) {
return (
disallowedCodePoints.includes(codePoint) ||
disallowedRegex.test(String.fromCodePoint(codePoint))
);
}
function formatPairs(array) {
let s = "";
for (let i = 0; i < array.length; i += 2) {
s += ` 0x${array[i].toString(16)}, 0x${array[i + 1].toString(16)},\n`;
}
return s.slice(0, -1);
}
function computeMap() {
const map = [];
let active = false;
for (let i = MIN_NONASCII_CODEPOINT; i <= MAX_UNICODE_CODEPOINT; i++) {
const allowed = !isDisallowed(i);
if (allowed !== active) {
map.push(active ? i - 1 : i);
active = !active;
}
}
assert(!active, "MAX_UNICODE_CODEPOINT should not be allowed.");
return map;
}
const src = `//
// Generated by scripts/regen-nonascii.js
// on node ${process.version} with unicode ${process.versions.unicode}.
//
/**
* @internal
*
* Map of non-ascii characters that are valid in an identifier. Each pair of
* numbers represents an inclusive range of code points.
*/
//prettier-ignore
export const nonAsciiIdentifierMap: readonly number[] = [
${formatPairs(map)}
];
`;
const file = resolve(fileURLToPath(import.meta.url), "../../src/core/nonascii.ts");
writeFileSync(file, src);