identifier.js 1.2 KB

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