| 317 | } |
| 318 | |
| 319 | private parseBasicString(multiline: boolean): string { |
| 320 | this.expect("\"") |
| 321 | if (multiline) { |
| 322 | this.expect("\"") |
| 323 | this.expect("\"") |
| 324 | this.advance(this.input.startsWith("\r\n", this.index) ? 2 : this.peek() === "\n" ? 1 : 0) |
| 325 | } |
| 326 | let output = "" |
| 327 | while (!this.done) { |
| 328 | if (multiline && this.input.startsWith("\"\"\"", this.index)) { |
| 329 | this.advance(3) |
| 330 | return output + this.closingQuotes("\"") |
| 331 | } |
| 332 | const character = this.peek() |
| 333 | if (!multiline && character === "\"") { |
| 334 | this.advance() |
| 335 | return output |
| 336 | } |
| 337 | if (!multiline && (character === "\n" || character === "\r")) { |
| 338 | this.fail("Basic strings cannot contain newlines") |
| 339 | } |
| 340 | if (character !== "\\") { |
| 341 | output += character |
| 342 | this.advance() |
| 343 | continue |
| 344 | } |
| 345 | |
| 346 | this.advance() |
| 347 | if (multiline && /[ \t\r\n]/.test(this.peek())) { |
| 348 | while (/[ \t]/.test(this.peek())) this.advance() |
| 349 | if (this.peek() !== "\n" && this.peek() !== "\r") { |
| 350 | this.fail("Invalid multiline string continuation") |
| 351 | } |
| 352 | while (/[ \t\r\n]/.test(this.peek())) this.advance() |
| 353 | continue |
| 354 | } |
| 355 | const escape = this.peek() |
| 356 | this.advance() |
| 357 | const escapes: Record<string, string> = { |
| 358 | b: "\b", |
| 359 | t: "\t", |
| 360 | n: "\n", |
| 361 | f: "\f", |
| 362 | r: "\r", |
| 363 | e: "\u001B", |
| 364 | "\"": "\"", |
| 365 | "\\": "\\" |
| 366 | } |
| 367 | if (hasOwn.call(escapes, escape)) { |
| 368 | output += escapes[escape] |
| 369 | } else if (escape === "x" || escape === "u" || escape === "U") { |
| 370 | const length = escape === "x" ? 2 : escape === "u" ? 4 : 8 |
| 371 | const hex = this.input.slice(this.index, this.index + length) |
| 372 | const codePoint = Number.parseInt(hex, 16) |
| 373 | if ( |
| 374 | !new RegExp(`^[0-9A-Fa-f]{${length}}$`).test(hex) || |
| 375 | (codePoint >= 0xD800 && codePoint <= 0xDFFF) || |
| 376 | codePoint > 0x10FFFF |