You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.5 KiB

4 years ago
  1. /*
  2. Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. (function () {
  22. 'use strict';
  23. var code = require('./code');
  24. function isStrictModeReservedWordES6(id) {
  25. switch (id) {
  26. case 'implements':
  27. case 'interface':
  28. case 'package':
  29. case 'private':
  30. case 'protected':
  31. case 'public':
  32. case 'static':
  33. case 'let':
  34. return true;
  35. default:
  36. return false;
  37. }
  38. }
  39. function isKeywordES5(id, strict) {
  40. // yield should not be treated as keyword under non-strict mode.
  41. if (!strict && id === 'yield') {
  42. return false;
  43. }
  44. return isKeywordES6(id, strict);
  45. }
  46. function isKeywordES6(id, strict) {
  47. if (strict && isStrictModeReservedWordES6(id)) {
  48. return true;
  49. }
  50. switch (id.length) {
  51. case 2:
  52. return (id === 'if') || (id === 'in') || (id === 'do');
  53. case 3:
  54. return (id === 'var') || (id === 'for') || (id === 'new') || (id === 'try');
  55. case 4:
  56. return (id === 'this') || (id === 'else') || (id === 'case') ||
  57. (id === 'void') || (id === 'with') || (id === 'enum');
  58. case 5:
  59. return (id === 'while') || (id === 'break') || (id === 'catch') ||
  60. (id === 'throw') || (id === 'const') || (id === 'yield') ||
  61. (id === 'class') || (id === 'super');
  62. case 6:
  63. return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
  64. (id === 'switch') || (id === 'export') || (id === 'import');
  65. case 7:
  66. return (id === 'default') || (id === 'finally') || (id === 'extends');
  67. case 8:
  68. return (id === 'function') || (id === 'continue') || (id === 'debugger');
  69. case 10:
  70. return (id === 'instanceof');
  71. default:
  72. return false;
  73. }
  74. }
  75. function isReservedWordES5(id, strict) {
  76. return id === 'null' || id === 'true' || id === 'false' || isKeywordES5(id, strict);
  77. }
  78. function isReservedWordES6(id, strict) {
  79. return id === 'null' || id === 'true' || id === 'false' || isKeywordES6(id, strict);
  80. }
  81. function isRestrictedWord(id) {
  82. return id === 'eval' || id === 'arguments';
  83. }
  84. function isIdentifierNameES5(id) {
  85. var i, iz, ch;
  86. if (id.length === 0) { return false; }
  87. ch = id.charCodeAt(0);
  88. if (!code.isIdentifierStartES5(ch)) {
  89. return false;
  90. }
  91. for (i = 1, iz = id.length; i < iz; ++i) {
  92. ch = id.charCodeAt(i);
  93. if (!code.isIdentifierPartES5(ch)) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. function decodeUtf16(lead, trail) {
  100. return (lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000;
  101. }
  102. function isIdentifierNameES6(id) {
  103. var i, iz, ch, lowCh, check;
  104. if (id.length === 0) { return false; }
  105. check = code.isIdentifierStartES6;
  106. for (i = 0, iz = id.length; i < iz; ++i) {
  107. ch = id.charCodeAt(i);
  108. if (0xD800 <= ch && ch <= 0xDBFF) {
  109. ++i;
  110. if (i >= iz) { return false; }
  111. lowCh = id.charCodeAt(i);
  112. if (!(0xDC00 <= lowCh && lowCh <= 0xDFFF)) {
  113. return false;
  114. }
  115. ch = decodeUtf16(ch, lowCh);
  116. }
  117. if (!check(ch)) {
  118. return false;
  119. }
  120. check = code.isIdentifierPartES6;
  121. }
  122. return true;
  123. }
  124. function isIdentifierES5(id, strict) {
  125. return isIdentifierNameES5(id) && !isReservedWordES5(id, strict);
  126. }
  127. function isIdentifierES6(id, strict) {
  128. return isIdentifierNameES6(id) && !isReservedWordES6(id, strict);
  129. }
  130. module.exports = {
  131. isKeywordES5: isKeywordES5,
  132. isKeywordES6: isKeywordES6,
  133. isReservedWordES5: isReservedWordES5,
  134. isReservedWordES6: isReservedWordES6,
  135. isRestrictedWord: isRestrictedWord,
  136. isIdentifierNameES5: isIdentifierNameES5,
  137. isIdentifierNameES6: isIdentifierNameES6,
  138. isIdentifierES5: isIdentifierES5,
  139. isIdentifierES6: isIdentifierES6
  140. };
  141. }());
  142. /* vim: set sw=4 ts=4 et tw=80 : */