generate-identifier-regex.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. // Always use the latest available version of Unicode!
  3. // https://tc39.github.io/ecma262/#sec-conformance
  4. const version = "14.0.0";
  5. const start = require("@unicode/unicode-" +
  6. version +
  7. "/Binary_Property/ID_Start/code-points.js").filter(function (ch) {
  8. return ch > 0x7f;
  9. });
  10. let last = -1;
  11. const cont = [0x200c, 0x200d].concat(
  12. require("@unicode/unicode-" +
  13. version +
  14. "/Binary_Property/ID_Continue/code-points.js").filter(function (ch) {
  15. return ch > 0x7f && search(start, ch, last + 1) == -1;
  16. })
  17. );
  18. function search(arr, ch, starting) {
  19. for (let i = starting; arr[i] <= ch && i < arr.length; last = i++) {
  20. if (arr[i] === ch) return i;
  21. }
  22. return -1;
  23. }
  24. function pad(str, width) {
  25. while (str.length < width) str = "0" + str;
  26. return str;
  27. }
  28. function esc(code) {
  29. const hex = code.toString(16);
  30. if (hex.length <= 2) return "\\x" + pad(hex, 2);
  31. else return "\\u" + pad(hex, 4);
  32. }
  33. function generate(chars) {
  34. const astral = [];
  35. let re = "";
  36. for (let i = 0, at = 0x10000; i < chars.length; i++) {
  37. const from = chars[i];
  38. let to = from;
  39. while (i < chars.length - 1 && chars[i + 1] == to + 1) {
  40. i++;
  41. to++;
  42. }
  43. if (to <= 0xffff) {
  44. if (from == to) re += esc(from);
  45. else if (from + 1 == to) re += esc(from) + esc(to);
  46. else re += esc(from) + "-" + esc(to);
  47. } else {
  48. astral.push(from - at, to - from);
  49. at = to;
  50. }
  51. }
  52. return { nonASCII: re, astral: astral };
  53. }
  54. const startData = generate(start);
  55. const contData = generate(cont);
  56. console.log("/* prettier-ignore */");
  57. console.log('let nonASCIIidentifierStartChars = "' + startData.nonASCII + '";');
  58. console.log("/* prettier-ignore */");
  59. console.log('let nonASCIIidentifierChars = "' + contData.nonASCII + '";');
  60. console.log("/* prettier-ignore */");
  61. console.log(
  62. "const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";"
  63. );
  64. console.log("/* prettier-ignore */");
  65. console.log(
  66. "const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";"
  67. );