generate-identifier-regex.js 1.7 KB

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