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