index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.readCodePoint = readCodePoint;
  6. exports.readInt = readInt;
  7. exports.readStringContents = readStringContents;
  8. var _isDigit = function isDigit(code) {
  9. return code >= 48 && code <= 57;
  10. };
  11. const forbiddenNumericSeparatorSiblings = {
  12. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  13. hex: new Set([46, 88, 95, 120])
  14. };
  15. const isAllowedNumericSeparatorSibling = {
  16. bin: ch => ch === 48 || ch === 49,
  17. oct: ch => ch >= 48 && ch <= 55,
  18. dec: ch => ch >= 48 && ch <= 57,
  19. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  20. };
  21. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  22. const initialPos = pos;
  23. const initialLineStart = lineStart;
  24. const initialCurLine = curLine;
  25. let out = "";
  26. let containsInvalid = false;
  27. let chunkStart = pos;
  28. const {
  29. length
  30. } = input;
  31. for (;;) {
  32. if (pos >= length) {
  33. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  34. out += input.slice(chunkStart, pos);
  35. break;
  36. }
  37. const ch = input.charCodeAt(pos);
  38. if (isStringEnd(type, ch, input, pos)) {
  39. out += input.slice(chunkStart, pos);
  40. break;
  41. }
  42. if (ch === 92) {
  43. out += input.slice(chunkStart, pos);
  44. let escaped;
  45. ({
  46. ch: escaped,
  47. pos,
  48. lineStart,
  49. curLine
  50. } = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors));
  51. if (escaped === null) {
  52. containsInvalid = true;
  53. } else {
  54. out += escaped;
  55. }
  56. chunkStart = pos;
  57. } else if (ch === 8232 || ch === 8233) {
  58. ++pos;
  59. ++curLine;
  60. lineStart = pos;
  61. } else if (ch === 10 || ch === 13) {
  62. if (type === "template") {
  63. out += input.slice(chunkStart, pos) + "\n";
  64. ++pos;
  65. if (ch === 13 && input.charCodeAt(pos) === 10) {
  66. ++pos;
  67. }
  68. ++curLine;
  69. chunkStart = lineStart = pos;
  70. } else {
  71. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  72. }
  73. } else {
  74. ++pos;
  75. }
  76. }
  77. return {
  78. pos,
  79. str: out,
  80. containsInvalid,
  81. lineStart,
  82. curLine
  83. };
  84. }
  85. function isStringEnd(type, ch, input, pos) {
  86. if (type === "template") {
  87. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  88. }
  89. return ch === (type === "double" ? 34 : 39);
  90. }
  91. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  92. const throwOnInvalid = !inTemplate;
  93. pos++;
  94. const res = ch => ({
  95. pos,
  96. ch,
  97. lineStart,
  98. curLine
  99. });
  100. const ch = input.charCodeAt(pos++);
  101. switch (ch) {
  102. case 110:
  103. return res("\n");
  104. case 114:
  105. return res("\r");
  106. case 120:
  107. {
  108. let code;
  109. ({
  110. code,
  111. pos
  112. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  113. return res(code === null ? null : String.fromCharCode(code));
  114. }
  115. case 117:
  116. {
  117. let code;
  118. ({
  119. code,
  120. pos
  121. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  122. return res(code === null ? null : String.fromCodePoint(code));
  123. }
  124. case 116:
  125. return res("\t");
  126. case 98:
  127. return res("\b");
  128. case 118:
  129. return res("\u000b");
  130. case 102:
  131. return res("\f");
  132. case 13:
  133. if (input.charCodeAt(pos) === 10) {
  134. ++pos;
  135. }
  136. case 10:
  137. lineStart = pos;
  138. ++curLine;
  139. case 8232:
  140. case 8233:
  141. return res("");
  142. case 56:
  143. case 57:
  144. if (inTemplate) {
  145. return res(null);
  146. } else {
  147. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  148. }
  149. default:
  150. if (ch >= 48 && ch <= 55) {
  151. const startPos = pos - 1;
  152. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  153. let octalStr = match[0];
  154. let octal = parseInt(octalStr, 8);
  155. if (octal > 255) {
  156. octalStr = octalStr.slice(0, -1);
  157. octal = parseInt(octalStr, 8);
  158. }
  159. pos += octalStr.length - 1;
  160. const next = input.charCodeAt(pos);
  161. if (octalStr !== "0" || next === 56 || next === 57) {
  162. if (inTemplate) {
  163. return res(null);
  164. } else {
  165. errors.strictNumericEscape(startPos, lineStart, curLine);
  166. }
  167. }
  168. return res(String.fromCharCode(octal));
  169. }
  170. return res(String.fromCharCode(ch));
  171. }
  172. }
  173. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  174. const initialPos = pos;
  175. let n;
  176. ({
  177. n,
  178. pos
  179. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors));
  180. if (n === null) {
  181. if (throwOnInvalid) {
  182. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  183. } else {
  184. pos = initialPos - 1;
  185. }
  186. }
  187. return {
  188. code: n,
  189. pos
  190. };
  191. }
  192. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors) {
  193. const start = pos;
  194. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  195. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  196. let invalid = false;
  197. let total = 0;
  198. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  199. const code = input.charCodeAt(pos);
  200. let val;
  201. if (code === 95 && allowNumSeparator !== "bail") {
  202. const prev = input.charCodeAt(pos - 1);
  203. const next = input.charCodeAt(pos + 1);
  204. if (!allowNumSeparator) {
  205. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  206. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  207. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  208. }
  209. ++pos;
  210. continue;
  211. }
  212. if (code >= 97) {
  213. val = code - 97 + 10;
  214. } else if (code >= 65) {
  215. val = code - 65 + 10;
  216. } else if (_isDigit(code)) {
  217. val = code - 48;
  218. } else {
  219. val = Infinity;
  220. }
  221. if (val >= radix) {
  222. if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  223. val = 0;
  224. } else if (forceLen) {
  225. val = 0;
  226. invalid = true;
  227. } else {
  228. break;
  229. }
  230. }
  231. ++pos;
  232. total = total * radix + val;
  233. }
  234. if (pos === start || len != null && pos - start !== len || invalid) {
  235. return {
  236. n: null,
  237. pos
  238. };
  239. }
  240. return {
  241. n: total,
  242. pos
  243. };
  244. }
  245. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  246. const ch = input.charCodeAt(pos);
  247. let code;
  248. if (ch === 123) {
  249. ++pos;
  250. ({
  251. code,
  252. pos
  253. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  254. ++pos;
  255. if (code !== null && code > 0x10ffff) {
  256. if (throwOnInvalid) {
  257. errors.invalidCodePoint(pos, lineStart, curLine);
  258. } else {
  259. return {
  260. code: null,
  261. pos
  262. };
  263. }
  264. }
  265. } else {
  266. ({
  267. code,
  268. pos
  269. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  270. }
  271. return {
  272. code,
  273. pos
  274. };
  275. }