generate-identifier-regex.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. // Which Unicode version should be used?
  3. const version = "9.0.0";
  4. const start = require("unicode-" + version + "/Binary_Property/ID_Start/code-points.js")
  5. .filter(function(ch) { return ch > 0x7f; });
  6. let last = -1;
  7. const cont = [0x200c, 0x200d].concat(
  8. require("unicode-" + version + "/Binary_Property/ID_Continue/code-points.js")
  9. .filter(function(ch) {
  10. return ch > 0x7f && search(start, ch, last + 1) == -1;
  11. })
  12. );
  13. function search(arr, ch, starting) {
  14. for (let i = starting; arr[i] <= ch && i < arr.length; last = i++)
  15. if (arr[i] === ch)
  16. return i;
  17. return -1;
  18. }
  19. function pad(str, width) {
  20. while (str.length < width) str = "0" + str;
  21. return str;
  22. }
  23. function esc(code) {
  24. const hex = code.toString(16);
  25. if (hex.length <= 2) return "\\x" + pad(hex, 2);
  26. else return "\\u" + pad(hex, 4);
  27. }
  28. function generate(chars) {
  29. const astral = [];
  30. let re = "";
  31. for (let i = 0, at = 0x10000; i < chars.length; i++) {
  32. const from = chars[i];
  33. let to = from;
  34. while (i < chars.length - 1 && chars[i + 1] == to + 1) {
  35. i++;
  36. to++;
  37. }
  38. if (to <= 0xffff) {
  39. if (from == to) re += esc(from);
  40. else if (from + 1 == to) re += esc(from) + esc(to);
  41. else re += esc(from) + "-" + esc(to);
  42. } else {
  43. astral.push(from - at, to - from);
  44. at = to;
  45. }
  46. }
  47. return { nonASCII: re, astral: astral };
  48. }
  49. const startData = generate(start);
  50. const contData = generate(cont);
  51. console.log("let nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\";");
  52. console.log("let nonASCIIidentifierChars = \"" + contData.nonASCII + "\";");
  53. console.log("const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";");
  54. console.log("const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";");