UNPKG

1.23 kBJavaScriptView Raw
1import {charCodes} from "./charcodes";
2import {WHITESPACE_CHARS} from "./whitespace";
3
4function computeIsIdentifierChar(code) {
5 if (code < 48) return code === 36;
6 if (code < 58) return true;
7 if (code < 65) return false;
8 if (code < 91) return true;
9 if (code < 97) return code === 95;
10 if (code < 123) return true;
11 if (code < 128) return false;
12 throw new Error("Should not be called with non-ASCII char code.");
13}
14
15export const IS_IDENTIFIER_CHAR = new Uint8Array(65536);
16for (let i = 0; i < 128; i++) {
17 IS_IDENTIFIER_CHAR[i] = computeIsIdentifierChar(i) ? 1 : 0;
18}
19for (let i = 128; i < 65536; i++) {
20 IS_IDENTIFIER_CHAR[i] = 1;
21}
22// Aside from whitespace and newlines, all characters outside the ASCII space are either
23// identifier characters or invalid. Since we're not performing code validation, we can just
24// treat all invalid characters as identifier characters.
25for (const whitespaceChar of WHITESPACE_CHARS) {
26 IS_IDENTIFIER_CHAR[whitespaceChar] = 0;
27}
28IS_IDENTIFIER_CHAR[0x2028] = 0;
29IS_IDENTIFIER_CHAR[0x2029] = 0;
30
31export const IS_IDENTIFIER_START = IS_IDENTIFIER_CHAR.slice();
32for (let numChar = charCodes.digit0; numChar <= charCodes.digit9; numChar++) {
33 IS_IDENTIFIER_START[numChar] = 0;
34}