All files / lib/core/expression parser.js

88.7% Statements 872/983
86.33% Branches 240/278
93.75% Functions 30/32
88.7% Lines 872/983

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 63x 63x 63x 63x 63x 63x 63x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 9357x 9357x 9357x 504x 504x 504x 504x 504x 504x 26060x 26060x 26060x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 2419x 2419x 2419x 2419x 12340x 12340x 12340x 2206x 2206x 2206x 10134x 10134x 12340x       12340x         10134x 12340x 348x 348x 348x 348x 348x 2135x 2135x         2135x 347x 347x 347x 347x 1788x 1788x 1788x 348x 1x 1x 347x 347x 347x 9786x 12340x 10x 10x 10x 10x 10x 10x 9776x 12340x 419x 419x 476x 476x       476x 476x 419x 419x 419x     419x 419x 419x 9357x 12328x 4489x 4489x 4489x 4489x 4489x 4868x 4868x 4868x 4868x 4868x 4868x 4868x       2418x 2418x 2418x 2419x 504x 504x 504x 504x 504x 504x 504x                 504x 504x 504x 504x 504x 504x 504x                                           504x 504x 504x 504x 504x 504x 504x 504x 10x 10x 10x 52x 52x       52x 10x 10x 52x 6x 6x 6x 35x 35x 29x               35x 35x 6x 6x 36x 36x   10x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 713x 713x 713x 713x 1035x     1035x 771x 1022x 440x 440x 704x 704x 704x 713x 504x 504x 504x 504x 504x 504x 504x 504x 1706x 1706x 1706x 1706x 1706x 504x 504x 504x 504x 504x 504x 504x 504x 504x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 504x 504x 504x 504x 504x 504x 62303x 62303x 504x 504x 504x 504x 504x 504x 6169x 6169x 504x 504x 504x 504x 504x 504x 504x 35567x 35567x 35567x 504x 504x 504x 504x 504x 504x 504x 15x 15x 15x 504x 504x 504x 504x 504x 504x 504x 18769x 3565x 3565x 3565x 15204x 18769x 504x 504x 504x 504x 504x 504x 504x 1597x 2x 2x 2x 2x 2x 2x 2x 1597x 504x 504x 504x 504x 504x 504x 2406x 2406x 50x 50x 2406x 504x 504x 504x 504x 504x 504x 2674x 2674x 2662x 2662x           2674x 504x 504x 504x 504x 504x 504x 3669x 3669x 31x 31x 3632x 3632x 3632x 3669x 289x     289x 289x 289x 289x 3327x 3669x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 3669x 3669x 3669x 3669x 3669x 2766x 2766x 20x 20x 20x 20x 2766x 3649x 3649x 3669x 123x 123x 123x 123x 114x 118x 118x 37x 37x 37x 81x 81x 116x 77x 77x 114x 123x 17x 17x 17x 106x 106x 3632x 3632x 3669x 504x 504x 504x 504x 504x 504x 7x 7x 7x 2x 2x 2x     2x 2x 2x 2x 2x 7x 7x 7x 504x 504x 504x 504x 504x 504x 504x 504x 504x 504x 44x 15x 15x 3x 3x 3x 3x 3x 3x 14x 6x 6x 6x 6x 6x 6x 6x 6x 6x 29x 44x 504x 504x 504x 504x 504x 504x 3632x 3632x 62x 62x 62x 62x 62x 3554x 3632x 504x 504x 504x 504x 504x 504x 504x 3888x 3888x 3888x 4124x 4124x 4124x 4124x 4124x 4124x 4124x 3868x 3868x 256x 256x 4124x 4124x 4124x 4124x 3868x 3868x 3888x 504x 504x 504x 504x 504x 504x 3965x 3965x 3965x 3x 3x 3x     3x 3x 3962x 3965x 74x 74x 74x 3888x 3888x 3965x 504x 504x 504x 504x 504x 504x 3888x 3888x 3888x 77x     77x 77x 77x 3795x 3888x 504x 504x 504x 504x 504x 504x 13x 13x 13x                           13x           13x 13x 13x 13x 504x 504x 504x 504x 504x 504x 3888x 3888x 3888x 5781x 1217x 1217x     1217x 1217x 1217x 4564x 5781x 14x 14x 3x 3x 3x 14x             11x 14x     11x 11x 11x 4550x 5781x 77x 77x 77x 77x 77x 4473x 5781x 601x 601x 601x 3872x 3872x 3872x 3872x 3872x 3888x 504x 504x 504x 504x 504x 504x 617x 617x 617x 334x 468x   468x 468x 468x 466x 324x 324x 334x 607x 607x 617x 504x 504x 504x 504x 504x 504x 3901x 3901x 3901x 725x 725x 725x 3176x 3901x 9x 9x 9x 3167x 3631x 2952x 76x 76x 76x 2952x 7x 7x 7x 7x 7x 7x 7x     7x 7x 7x 2869x 2952x 13x 13x 13x 13x 13x 13x 13x 13x 13x 2856x 2856x 2856x 215x 1606x 121x 121x 121x 121x 121x 94x 1563x 22x 22x 22x 13x 30x 30x 4x 30x 26x 26x 30x 13x 13x 13x 22x 22x 22x 72x 295x 68x 68x 68x 48x 60x 60x 4x 4x 3x 3x 56x 56x 42x 60x   14x 14x 14x 14x 14x 14x 14x 14x 60x 45x 45x 48x 68x 68x 68x 4x 4x 3901x 3901x 3901x 3901x 3901x 504x 504x 504x 504x 504x 504x 56x 6x 6x 6x 6x 50x 56x 50x 50x   56x 504x 504x 504x 504x 504x 504x 504x 9x 9x 9x 9x 9x 9x 34x         34x 5x 5x 5x 29x 29x 29x 24x 24x 5x 5x 5x 5x 5x 5x 29x 29x 29x 9x 9x 9x 9x 504x  
/**
 * @file parser.js
 * @description A JavaScript expression parser for Avenx template expressions.
 *
 * ## Why Avenx needs its own
 *
 * Template expressions used to be handed straight to the engine:
 *
 * ```js
 * new Function(`with(this) { return (${expression}) }`)
 * ```
 *
 * Two consequences followed from that line, and neither could be fixed while
 * it stood.
 *
 * **`unsafe-eval` was structural.** Any Content-Security-Policy without
 * `'unsafe-eval'` stopped the framework working, which excludes it from
 * regulated environments, browser extensions, and a good many default
 * enterprise policies.
 *
 * **The sandbox was not a boundary.** `AvenxSandbox` wrapped values that
 * reached an expression *through the scope* and blocked the literal identifiers
 * `constructor`, `__proto__` and `prototype` with a regular expression over the
 * source text. An object created *inside* the expression never passed through
 * the scope, so it was never wrapped, and the text check fell to string
 * concatenation:
 *
 * ```js
 * ({})['const'+'ructor']['const'+'ructor']('return 1')()   // executed
 * ```
 *
 * There is no version of a source-text blacklist that closes that. The value
 * being guarded is produced by the engine, inside code the guard has already
 * handed over.
 *
 * ## What parsing changes
 *
 * Once Avenx holds the AST, it decides what every operation means. A member
 * access is a call into {@link module:lib/core/expression/evaluator}, with the
 * property key already resolved — so `x['const'+'ructor']` and `x.constructor`
 * arrive at the same check, because by then they are the same string. Nothing
 * is handed to the engine as code, so nothing needs `unsafe-eval`.
 *
 * ## Scope
 *
 * This parses the expression language Avenx templates use — everything up to
 * and including arrow functions, which templates need for `items.filter(i =>
 * i.done)`. It is not a full ECMAScript parser and does not try to be:
 * statements, classes, generators, `async`/`await`, destructuring patterns and
 * regular-expression literals are out of scope. An expression it cannot parse
 * raises {@link ExpressionParseError} rather than guessing, and the caller
 * decides what to do about it.
 * @module lib/core/expression/parser
 */
 
/**
 * Raised when an expression is outside the supported language.
 */
export class ExpressionParseError extends Error {
  /**
   * @param {string} message - What went wrong.
   * @param {number} position - Character offset in the expression.
   * @param {string} source - The expression being parsed.
   */
  constructor(message, position, source) {
    super(message);
    this.name = 'ExpressionParseError';
    /** @type {number} */
    this.position = position;
    /** @type {string} */
    this.source = source;
  }
}
 
/**
 * Binary operator precedence, higher binds tighter.
 * @type {Object<string, number>}
 */
const BINARY_PRECEDENCE = {
  '??': 1,
  '||': 2,
  '&&': 3,
  '|': 4,
  '^': 5,
  '&': 6,
  '==': 7, '!=': 7, '===': 7, '!==': 7,
  '<': 8, '>': 8, '<=': 8, '>=': 8, 'in': 8, 'instanceof': 8,
  '<<': 9, '>>': 9, '>>>': 9,
  '+': 10, '-': 10,
  '*': 11, '/': 11, '%': 11,
  '**': 12,
};
 
/**
 * Operators sorted longest-first, so `>>>=` is matched before `>>>`.
 * @type {string[]}
 */
const PUNCTUATORS = [
  '>>>=', '===', '!==', '**=', '<<=', '>>=', '&&=', '||=', '??=', '...', '>>>',
  '=>', '==', '!=', '<=', '>=', '&&', '||', '??', '?.', '++', '--', '**',
  '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<', '>>',
  '{', '}', '(', ')', '[', ']', ';', ',', '<', '>', '+', '-', '*', '/', '%',
  '&', '|', '^', '!', '~', '?', ':', '=', '.',
];
 
/**
 * Assignment operators.
 * @type {Set<string>}
 */
const ASSIGNMENT_OPERATORS = new Set([
  '=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '&&=', '||=', '??=',
]);
 
/**
 * Keywords that are values rather than identifiers.
 * @type {Object<string, any>}
 */
const LITERAL_KEYWORDS = {
  true: true,
  false: false,
  null: null,
  undefined: undefined,
};
 
/**
 * Prefix operators.
 * @type {Set<string>}
 */
const UNARY_OPERATORS = new Set(['!', '-', '+', '~', 'typeof', 'void']);
 
/**
 * Whether a character can start an identifier.
 * @param {string} ch - A single character.
 * @returns {boolean} True when it can begin an identifier.
 */
function isIdentStart(ch) {
  return !!ch && /[A-Za-z_$]/.test(ch);
}
 
/**
 * Whether a character can continue an identifier.
 * @param {string} ch - A single character.
 * @returns {boolean} True when it can continue an identifier.
 */
function isIdentPart(ch) {
  return !!ch && /[\w$]/.test(ch);
}
 
/**
 * Turns an expression into tokens.
 *
 * Template literals are tokenised whole and re-parsed by the parser, because
 * their `${}` holes contain expressions and nesting them in the tokenizer would
 * require a second stack for no benefit.
 * @param {string} source - The expression source.
 * @returns {Array<object>} The token stream, terminated by an `eof` token.
 * @throws {ExpressionParseError} On an unterminated string or unknown character.
 */
export function tokenize(source) {
  const tokens = [];
  let i = 0;
 
  while (i < source.length) {
    const ch = source[i];
 
    if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
      i++;
      continue;
    }
 
    // Comments are legal inside an action body and harmless in an expression.
    if (ch === '/' && source[i + 1] === '/') {
      while (i < source.length && source[i] !== '\n') i++;
      continue;
    }
    if (ch === '/' && source[i + 1] === '*') {
      const end = source.indexOf('*/', i + 2);
      i = end === -1 ? source.length : end + 2;
      continue;
    }
 
    if (ch === '"' || ch === "'") {
      const start = i;
      i++;
      let value = '';
      let closed = false;
      while (i < source.length) {
        const c = source[i];
        if (c === '\\') {
          value += unescapeChar(source, i);
          i += escapeLength(source, i);
          continue;
        }
        if (c === ch) {
          i++;
          closed = true;
          break;
        }
        value += c;
        i++;
      }
      if (!closed) {
        throw new ExpressionParseError('Unterminated string literal', start, source);
      }
      tokens.push({ type: 'string', value, start });
      continue;
    }
 
    if (ch === '`') {
      const start = i;
      const raw = readTemplateLiteral(source, i);
      i = raw.end;
      tokens.push({ type: 'template', value: raw.text, start });
      continue;
    }
 
    if (/[0-9]/.test(ch) || (ch === '.' && /[0-9]/.test(source[i + 1]))) {
      const start = i;
      while (i < source.length && /[0-9a-fA-FxXoObBeE._]/.test(source[i])) {
        // An exponent sign is part of the number; a following `-` otherwise is not.
        if ((source[i] === 'e' || source[i] === 'E') && (source[i + 1] === '+' || source[i + 1] === '-')) {
          i += 2;
          continue;
        }
        i++;
      }
      const text = source.slice(start, i).replace(/_/g, '');
      const value = Number(text);
      if (Number.isNaN(value) && text.toLowerCase() !== 'nan') {
        throw new ExpressionParseError(`Invalid number "${text}"`, start, source);
      }
      tokens.push({ type: 'number', value, start });
      continue;
    }
 
    if (isIdentStart(ch)) {
      const start = i;
      while (i < source.length && isIdentPart(source[i])) i++;
      tokens.push({ type: 'name', value: source.slice(start, i), start });
      continue;
    }
 
    const punctuator = PUNCTUATORS.find((p) => source.startsWith(p, i));
    if (punctuator) {
      tokens.push({ type: 'punct', value: punctuator, start: i });
      i += punctuator.length;
      continue;
    }

    throw new ExpressionParseError(`Unexpected character "${ch}"`, i, source);
  }
 
  tokens.push({ type: 'eof', value: null, start: source.length });
  return tokens;
}
 
/**
 * How many source characters an escape sequence occupies.
 * @param {string} source - The source.
 * @param {number} i - Offset of the backslash.
 * @returns {number} The sequence length including the backslash.
 */
function escapeLength(source, i) {
  const next = source[i + 1];
  if (next === 'u') {
    return source[i + 2] === '{' ? source.indexOf('}', i) - i + 1 : 6;
  }
  if (next === 'x') return 4;
  return 2;
}
 
/**
 * Resolves an escape sequence to the character it denotes.
 * @param {string} source - The source.
 * @param {number} i - Offset of the backslash.
 * @returns {string} The unescaped character.
 */
function unescapeChar(source, i) {
  const next = source[i + 1];
  switch (next) {
    case 'n': return '\n';
    case 't': return '\t';
    case 'r': return '\r';
    case 'b': return '\b';
    case 'f': return '\f';
    case 'v': return '\v';
    case '0': return '\0';
    case 'x': return String.fromCharCode(parseInt(source.slice(i + 2, i + 4), 16));
    case 'u': {
      if (source[i + 2] === '{') {
        const close = source.indexOf('}', i);
        return String.fromCodePoint(parseInt(source.slice(i + 3, close), 16));
      }
      return String.fromCharCode(parseInt(source.slice(i + 2, i + 6), 16));
    }
    default: return next;
  }
}
 
/**
 * Reads a template literal, tracking nested braces and strings inside holes.
 * @param {string} source - The source.
 * @param {number} start - Offset of the opening backtick.
 * @returns {{text: string, end: number}} The literal text and the offset after it.
 * @throws {ExpressionParseError} When the literal is unterminated.
 */
function readTemplateLiteral(source, start) {
  let i = start + 1;
  while (i < source.length) {
    const ch = source[i];
    if (ch === '\\') {
      i += 2;
      continue;
    }
    if (ch === '`') {
      return { text: source.slice(start + 1, i), end: i + 1 };
    }
    if (ch === '$' && source[i + 1] === '{') {
      let depth = 1;
      i += 2;
      while (i < source.length && depth > 0) {
        if (source[i] === '{') depth++;
        else if (source[i] === '}') depth--;
        else if (source[i] === '"' || source[i] === "'" || source[i] === '`') {
          const quote = source[i];
          i++;
          while (i < source.length && source[i] !== quote) {
            if (source[i] === '\\') i++;
            i++;
          }
        }
        i++;
      }
      continue;
    }
    i++;
  }
  throw new ExpressionParseError('Unterminated template literal', start, source);
}
 
/**
 * Parses a semicolon-separated run of expression statements.
 *
 * Most inline handlers and short action bodies are one or more expressions:
 * `count++`, `busy = true; count++`, `state.text = event.target.value`. Those
 * take the AST path and need no eval. A body with real statement syntax --
 * `if`, `for`, `return`, a declaration -- is not an expression program and
 * raises, so the caller falls back.
 * @param {string} source - The statement source.
 * @returns {object} A Program node.
 * @throws {ExpressionParseError} When the source is not a run of expressions.
 */
export function parseExpressionProgram(source) {
  const parser = new Parser(source);
  const body = [];
 
  for (;;) {
    while (parser.eat(';')) {
      // Empty statement.
    }
    if (parser.peek().type === 'eof') break;
    body.push(parser.parseExpressionStatement());
    if (parser.eat(';')) continue;
    break;
  }
 
  parser.expectEnd();
  return { type: 'Program', body };
}
 
/**
 * Parses an expression into an AST.
 * @param {string} source - The expression source.
 * @returns {object} The root AST node.
 * @throws {ExpressionParseError} When the source is outside the supported language.
 */
export function parseExpression(source) {
  const parser = new Parser(source);
  const node = parser.parseExpressionStatement();
  parser.expectEnd();
  return node;
}
 
/**
 * A recursive-descent parser over a token stream.
 */
class Parser {
  /**
   * @param {string} source - The expression source.
   */
  constructor(source) {
    /** @type {string} */
    this.source = source;
    /** @type {Array<object>} */
    this.tokens = tokenize(source);
    /** @type {number} */
    this.index = 0;
  }
 
  /**
   * The token at the cursor.
   * @returns {object} The current token.
   */
  peek() {
    return this.tokens[this.index];
  }
 
  /**
   * Advances past the current token.
   * @returns {object} The token consumed.
   */
  next() {
    return this.tokens[this.index++];
  }
 
  /**
   * Whether the current token is a given punctuator.
   * @param {string} value - The punctuator.
   * @returns {boolean} True when it matches.
   */
  atPunct(value) {
    const token = this.peek();
    return token.type === 'punct' && token.value === value;
  }
 
  /**
   * Whether the current token is a given keyword.
   * @param {string} value - The keyword.
   * @returns {boolean} True when it matches.
   */
  atName(value) {
    const token = this.peek();
    return token.type === 'name' && token.value === value;
  }
 
  /**
   * Consumes a punctuator if present.
   * @param {string} value - The punctuator.
   * @returns {boolean} Whether it was consumed.
   */
  eat(value) {
    if (this.atPunct(value)) {
      this.index++;
      return true;
    }
    return false;
  }
 
  /**
   * Consumes a punctuator, or fails.
   * @param {string} value - The punctuator.
   * @throws {ExpressionParseError} When the token does not match.
   */
  expect(value) {
    if (!this.eat(value)) {
      const token = this.peek();
      throw new ExpressionParseError(
        `Expected "${value}" but found ${token.type === 'eof' ? 'end of expression' : `"${token.value}"`}`,
        token.start,
        this.source,
      );
    }
  }
 
  /**
   * Asserts the token stream is exhausted.
   * @throws {ExpressionParseError} When tokens remain.
   */
  expectEnd() {
    const token = this.peek();
    if (token.type !== 'eof') {
      throw new ExpressionParseError(`Unexpected "${token.value}"`, token.start, this.source);
    }
  }
 
  /**
   * Parses a full expression, including the comma operator.
   * @returns {object} The AST node.
   */
  parseExpressionStatement() {
    const first = this.parseAssignment();
    if (!this.atPunct(',')) {
      return first;
    }
    const expressions = [first];
    while (this.eat(',')) {
      expressions.push(this.parseAssignment());
    }
    return { type: 'Sequence', expressions };
  }
 
  /**
   * Parses an assignment, arrow function or conditional.
   * @returns {object} The AST node.
   */
  parseAssignment() {
    const arrow = this.tryParseArrow();
    if (arrow) {
      return arrow;
    }
 
    const left = this.parseConditional();
    const token = this.peek();
    if (token.type === 'punct' && ASSIGNMENT_OPERATORS.has(token.value)) {
      if (left.type !== 'Identifier' && left.type !== 'Member') {
        throw new ExpressionParseError('Invalid assignment target', token.start, this.source);
      }
      this.next();
      const right = this.parseAssignment();
      return { type: 'Assignment', operator: token.value, target: left, value: right };
    }
    return left;
  }
 
  /**
   * Attempts to parse an arrow function at the cursor.
   *
   * Speculative because `(a, b)` is ambiguous until `=>` is or is not found.
   * The cursor is restored when the guess is wrong, which is cheap: the token
   * stream is already materialised.
   * @returns {object|null} The arrow node, or null when this is not one.
   */
  tryParseArrow() {
    const start = this.index;
 
    // `x => …`
    const token = this.peek();
    if (token.type === 'name' && !Object.prototype.hasOwnProperty.call(LITERAL_KEYWORDS, token.value)) {
      const after = this.tokens[this.index + 1];
      if (after && after.type === 'punct' && after.value === '=>') {
        this.next();
        this.next();
        return { type: 'Arrow', params: [token.value], body: this.parseArrowBody() };
      }
    }
 
    // `(a, b) => …`
    if (this.atPunct('(')) {
      const params = [];
      this.next();
      let valid = true;
      if (!this.atPunct(')')) {
        for (;;) {
          const param = this.peek();
          if (param.type !== 'name') {
            valid = false;
            break;
          }
          params.push(param.value);
          this.next();
          if (this.eat(',')) continue;
          break;
        }
      }
      if (valid && this.eat(')') && this.atPunct('=>')) {
        this.next();
        return { type: 'Arrow', params, body: this.parseArrowBody() };
      }
      this.index = start;
    }
 
    return null;
  }
 
  /**
   * Parses a parenthesised parameter name list.
   * @returns {string[]} The parameter names.
   */
  parseParameterList() {
    this.expect('(');
    const params = [];
    if (!this.atPunct(')')) {
      for (;;) {
        const param = this.next();
        if (param.type !== 'name') {
          throw new ExpressionParseError('Expected a parameter name', param.start, this.source);
        }
        params.push(param.value);
        if (this.eat(',')) continue;
        break;
      }
    }
    this.expect(')');
    return params;
  }
 
  /**
   * Parses an arrow function's body.
   *
   * A braced body is supported only when it is a single `return`; anything more
   * is a statement list, which belongs to the statement evaluator rather than
   * here.
   * @returns {object} The body expression.
   */
  parseArrowBody() {
    if (this.atPunct('{')) {
      this.next();
      if (this.atName('return')) {
        this.next();
        const value = this.parseAssignment();
        this.eat(';');
        this.expect('}');
        return value;
      }
      if (this.eat('}')) {
        return { type: 'Literal', value: undefined };
      }
      const token = this.peek();
      throw new ExpressionParseError(
        'An arrow function body must be an expression or a single return statement',
        token.start,
        this.source,
      );
    }
    return this.parseAssignment();
  }
 
  /**
   * Parses a conditional expression.
   * @returns {object} The AST node.
   */
  parseConditional() {
    const test = this.parseBinary(0);
    if (this.eat('?')) {
      const consequent = this.parseAssignment();
      this.expect(':');
      const alternate = this.parseAssignment();
      return { type: 'Conditional', test, consequent, alternate };
    }
    return test;
  }
 
  /**
   * Parses a binary expression using precedence climbing.
   * @param {number} minPrecedence - The lowest precedence to accept.
   * @returns {object} The AST node.
   */
  parseBinary(minPrecedence) {
    let left = this.parseUnary();
 
    for (;;) {
      const token = this.peek();
      const operator =
        token.type === 'punct' || (token.type === 'name' && (token.value === 'in' || token.value === 'instanceof'))
          ? token.value
          : null;
      const precedence = operator ? BINARY_PRECEDENCE[operator] : undefined;
      if (precedence === undefined || precedence < minPrecedence) {
        break;
      }
      this.next();
      // `**` is right-associative; everything else is left-associative.
      const right = this.parseBinary(operator === '**' ? precedence : precedence + 1);
      const type = operator === '&&' || operator === '||' || operator === '??' ? 'Logical' : 'Binary';
      left = { type, operator, left, right };
    }
 
    return left;
  }
 
  /**
   * Parses a prefix expression.
   * @returns {object} The AST node.
   */
  parseUnary() {
    const token = this.peek();
 
    if (token.type === 'punct' && (token.value === '++' || token.value === '--')) {
      this.next();
      const argument = this.parseUnary();
      if (argument.type !== 'Identifier' && argument.type !== 'Member') {
        throw new ExpressionParseError('Invalid update target', token.start, this.source);
      }
      return { type: 'Update', operator: token.value, prefix: true, argument };
    }
 
    if ((token.type === 'punct' || token.type === 'name') && UNARY_OPERATORS.has(token.value)) {
      this.next();
      return { type: 'Unary', operator: token.value, argument: this.parseUnary() };
    }
 
    return this.parsePostfix();
  }
 
  /**
   * Parses a postfix update.
   * @returns {object} The AST node.
   */
  parsePostfix() {
    const argument = this.parseCallMember();
    const token = this.peek();
    if (token.type === 'punct' && (token.value === '++' || token.value === '--')) {
      if (argument.type !== 'Identifier' && argument.type !== 'Member') {
        throw new ExpressionParseError('Invalid update target', token.start, this.source);
      }
      this.next();
      return { type: 'Update', operator: token.value, prefix: false, argument };
    }
    return argument;
  }
 
  /**
   * Parses the constructor of a `new` expression: member access only, no calls.
   * @returns {object} The AST node.
   */
  parseNewCallee() {
    let node = this.parsePrimary();
    for (;;) {
      if (this.eat('.')) {
        const name = this.next();
        if (name.type !== 'name') {
          throw new ExpressionParseError('Expected a property name after "."', name.start, this.source);
        }
        node = {
          type: 'Member',
          object: node,
          property: { type: 'Literal', value: name.value },
          computed: false,
          optional: false,
        };
        continue;
      }
      if (this.eat('[')) {
        const property = this.parseExpressionStatement();
        this.expect(']');
        node = { type: 'Member', object: node, property, computed: true, optional: false };
        continue;
      }
      break;
    }
    return node;
  }
 
  /**
   * Parses member access and calls, left to right.
   * @returns {object} The AST node.
   */
  parseCallMember() {
    let node = this.parsePrimary();
 
    for (;;) {
      if (this.eat('.')) {
        const name = this.next();
        if (name.type !== 'name') {
          throw new ExpressionParseError('Expected a property name after "."', name.start, this.source);
        }
        node = { type: 'Member', object: node, property: { type: 'Literal', value: name.value }, computed: false, optional: false };
        continue;
      }
 
      if (this.atPunct('?.')) {
        this.next();
        if (this.atPunct('(')) {
          node = { type: 'Call', callee: node, args: this.parseArguments(), optional: true };
          continue;
        }
        if (this.atPunct('[')) {
          this.next();
          const property = this.parseExpressionStatement();
          this.expect(']');
          node = { type: 'Member', object: node, property, computed: true, optional: true };
          continue;
        }
        const name = this.next();
        if (name.type !== 'name') {
          throw new ExpressionParseError('Expected a property name after "?."', name.start, this.source);
        }
        node = { type: 'Member', object: node, property: { type: 'Literal', value: name.value }, computed: false, optional: true };
        continue;
      }
 
      if (this.eat('[')) {
        const property = this.parseExpressionStatement();
        this.expect(']');
        node = { type: 'Member', object: node, property, computed: true, optional: false };
        continue;
      }
 
      if (this.atPunct('(')) {
        node = { type: 'Call', callee: node, args: this.parseArguments(), optional: false };
        continue;
      }
 
      break;
    }
 
    return node;
  }
 
  /**
   * Parses a parenthesised argument list.
   * @returns {Array<object>} The argument nodes.
   */
  parseArguments() {
    this.expect('(');
    const args = [];
    if (!this.atPunct(')')) {
      for (;;) {
        if (this.eat('...')) {
          args.push({ type: 'Spread', argument: this.parseAssignment() });
        } else {
          args.push(this.parseAssignment());
        }
        if (this.eat(',')) continue;
        break;
      }
    }
    this.expect(')');
    return args;
  }
 
  /**
   * Parses a primary expression.
   * @returns {object} The AST node.
   */
  parsePrimary() {
    const token = this.peek();
 
    if (token.type === 'number' || token.type === 'string') {
      this.next();
      return { type: 'Literal', value: token.value };
    }
 
    if (token.type === 'template') {
      this.next();
      return this.parseTemplateParts(token.value);
    }
 
    if (token.type === 'name') {
      if (Object.prototype.hasOwnProperty.call(LITERAL_KEYWORDS, token.value)) {
        this.next();
        return { type: 'Literal', value: LITERAL_KEYWORDS[token.value] };
      }
      if (token.value === 'function') {
        // A function expression used as a callback -- `items.map(function (i) {
        // return i.label; })` -- is ordinary template code, and refusing it
        // would push the expression onto a path that no longer exists. The body
        // is parsed on the same terms as an arrow's: an expression, or a single
        // return.
        this.next();
        if (this.peek().type === 'name') {
          this.next();
        }
        const params = this.parseParameterList();
        return { type: 'Arrow', params, body: this.parseArrowBody(), isFunction: true };
      }
 
      if (token.value === 'new') {
        this.next();
        // The constructor is a member expression *without* calls: in
        // `new Date().getTime()` the `new` binds to `Date`, and `.getTime()`
        // applies to the instance. Parsing the whole call chain first and then
        // unwrapping it gets that backwards.
        const callee = this.parseNewCallee();
        const args = this.atPunct('(') ? this.parseArguments() : [];
        return { type: 'New', callee, args };
      }
      this.next();
      return { type: 'Identifier', name: token.value, start: token.start };
    }
 
    if (this.atPunct('(')) {
      this.next();
      const inner = this.parseExpressionStatement();
      this.expect(')');
      return inner;
    }
 
    if (this.atPunct('[')) {
      this.next();
      const elements = [];
      if (!this.atPunct(']')) {
        for (;;) {
          if (this.atPunct(']')) break;
          if (this.eat('...')) {
            elements.push({ type: 'Spread', argument: this.parseAssignment() });
          } else {
            elements.push(this.parseAssignment());
          }
          if (this.eat(',')) continue;
          break;
        }
      }
      this.expect(']');
      return { type: 'ArrayLiteral', elements };
    }
 
    if (this.atPunct('{')) {
      this.next();
      const properties = [];
      if (!this.atPunct('}')) {
        for (;;) {
          if (this.atPunct('}')) break;
          if (this.eat('...')) {
            properties.push({ kind: 'spread', value: this.parseAssignment() });
            if (this.eat(',')) continue;
            break;
          }
          const key = this.parseObjectKey();
          if (this.eat(':')) {
            properties.push({ kind: 'init', key, value: this.parseAssignment() });
          } else if (key.computed) {
            throw new ExpressionParseError('A computed key needs a value', this.peek().start, this.source);
          } else {
            // Shorthand: `{ count }`.
            properties.push({
              kind: 'init',
              key,
              value: { type: 'Identifier', name: key.node.value },
            });
          }
          if (this.eat(',')) continue;
          break;
        }
      }
      this.expect('}');
      return { type: 'ObjectLiteral', properties };
    }
 
    throw new ExpressionParseError(
      token.type === 'eof' ? 'Unexpected end of expression' : `Unexpected "${token.value}"`,
      token.start,
      this.source,
    );
  }
 
  /**
   * Parses an object literal key.
   * @returns {{computed: boolean, node: object}} The key description.
   */
  parseObjectKey() {
    if (this.eat('[')) {
      const node = this.parseAssignment();
      this.expect(']');
      return { computed: true, node };
    }
    const token = this.next();
    if (token.type === 'name' || token.type === 'string' || token.type === 'number') {
      return { computed: false, node: { type: 'Literal', value: String(token.value) } };
    }
    throw new ExpressionParseError('Expected a property name', token.start, this.source);
  }
 
  /**
   * Splits a template literal into its static and interpolated parts.
   * @param {string} raw - The text between the backticks.
   * @returns {object} A TemplateLiteral node.
   */
  parseTemplateParts(raw) {
    const quasis = [];
    const expressions = [];
    let text = '';
    let i = 0;
 
    while (i < raw.length) {
      if (raw[i] === '\\') {
        text += unescapeChar(raw, i);
        i += escapeLength(raw, i);
        continue;
      }
      if (raw[i] === '$' && raw[i + 1] === '{') {
        let depth = 1;
        let j = i + 2;
        while (j < raw.length && depth > 0) {
          if (raw[j] === '{') depth++;
          else if (raw[j] === '}') depth--;
          if (depth === 0) break;
          j++;
        }
        quasis.push(text);
        text = '';
        expressions.push(parseExpression(raw.slice(i + 2, j)));
        i = j + 1;
        continue;
      }
      text += raw[i];
      i++;
    }
 
    quasis.push(text);
    return { type: 'TemplateLiteral', quasis, expressions };
  }
}