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 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 | 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 196912x 196912x 196912x 196912x 196912x 196912x 196912x 196912x 196912x 196912x 196912x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 41448x 548x 548x 548x 548x 548x 548x 196912x 196912x 548x 548x 548x 548x 548x 157836x 157836x 548x 548x 548x 548x 548x 157315x 157315x 548x 548x 548x 548x 548x 548x 548x 164017x 164016x 164016x 164017x 548x 548x 548x 548x 548x 548x 548x 548x 157315x 157315x 157315x 157315x 83093x 5x 5x 83088x 83093x 83088x 83088x 74222x 149205x 54094x 54094x 54094x 20128x 20128x 20128x 136049x 24359x 24359x 24359x 2x 2x 24359x 24357x 24359x 24359x 24359x 157315x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 51097x 51097x 548x 548x 548x 548x 548x 548x 19978x 19978x 548x 548x 548x 548x 548x 3925084x 3925084x 548x 548x 548x 548x 548x 548x 10629398x 10629398x 548x 548x 548x 548x 548x 548x 1792566x 1792566x 548x 548x 548x 548x 548x 548x 548x 3195207x 1684747x 1684747x 3195207x 548x 548x 548x 548x 548x 548x 548x 86538x 32697x 32697x 86538x 548x 548x 548x 548x 548x 548x 900768x 900768x 548x 548x 548x 548x 548x 548x 22061x 22061x 548x 548x 548x 548x 548x 72760x 72760x 548x 548x 548x 548x 548x 258457x 222052x 258457x 548x 548x 548x 548x 548x 548x 2243777x 2243777x 548x 548x 548x 548x 548x 548x 548x 2900097x 2900097x 2900097x 2900097x 2900097x 548x 548x 548x 548x 548x 548x 548x 548x 2767945x 2767945x 548x 548x 548x 548x 548x 548x 548x 548x 548x 2785311x 2785311x 2785311x 2785311x 2785311x 2785311x 2785311x 548x 548x 548x 548x 548x 548x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 48698x 548x 548x 548x 548x 548x 548x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 27123x 575x 575x 575x 27123x 27123x 548x 548x 548x 548x 548x 548x 576408x 576408x 576408x 576408x 576408x 548x 548x 548x 548x 548x 548x 548x 548x 41084x 41084x 41084x 41084x 41084x 56456x 56456x 16643x 59x 59x 16643x 16584x 16584x 16643x 29342x 29342x 13970x 13970x 13970x 13970x 41084x 548x 548x 548x 548x 548x 14010x 2x 2x 14010x 548x 548x 548x 548x 548x 548x 548x 69870x 69870x 69870x 180x 180x 69870x 69870x 69870x 82x 69870x 69870x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 415196x 63415x 63415x 53203x 63415x 1464x 1464x 1464x 1464x 63415x 8748x 63415x 63415x 360529x 415175x 350512x 350512x 10710x 10710x 10710x 350512x 47143x 47043x 350512x 8154x 8154x 8154x 239x 350512x 9919x 9919x 9919x 9919x 700x 700x 9219x 9919x 41x 41x 9178x 350512x 3099x 2712x 350512x 48938x 350512x 13784x 350512x 3121x 350512x 1569x 350512x 1733x 350512x 5917x 350512x 12x 350512x 56167x 350512x 3805x 350512x 350512x 6002x 350512x 1510x 1510x 1510x 1510x 1510x 350512x 3609x 3609x 3609x 3602x 3602x 3566x 3566x 7x 7x 350512x 2489x 2489x 2303x 350512x 2456x 2456x 1793x 1538x 1538x 663x 663x 350512x 120375x 350512x 350512x 131301x 131301x 131301x 131301x 415196x 95225x 95225x 1857x 1857x 95225x 129002x 129002x 129002x 129002x 129002x 415196x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 10718x 10718x 2867x 10x 10718x 548x 548x 548x 548x 548x 548x 548x 548x 65339x 65339x 65339x 65319x 65339x 121481x 121266x 121266x 65339x 60914x 65339x 548x 548x 548x 548x 548x 548x 548x 65951x 65951x 65951x 65951x 65951x 65951x 548x 548x 548x 548x 548x 548x 548x 548x 78157x 78157x 78157x 78417x 78417x 78417x 78417x 132x 132x 78417x 2x 2x 77377x 78411x 78417x 78157x 548x 548x 548x 548x 548x 548x 548x 11907x 11907x 11907x 11907x 11907x 11907x 11907x 11907x 11907x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 23609x 23609x 23609x 23609x 23609x 23609x 23609x 23609x 23609x 23609x 548x 548x 548x 548x 548x 548x 548x 548x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 75103x 548x 548x 548x 548x 548x 548x 72177x 72177x 72177x 548x 548x 548x 548x 548x 548x 57304x 57304x 57304x 57304x 57304x 57304x 157060x 157015x 157060x 53227x 23x 23x 53227x 53204x 53204x 53227x 155087x 155087x 55326x 55326x 57304x 548x 548x 548x 548x 548x 548x 96912x 96912x 96912x 96912x 96912x 90664x 27803x 90664x 90664x 581x 581x 581x 581x 581x 581x 581x 581x 81269x 81269x 71146x 96912x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 70226x 70226x 70226x 64878x 64803x 64878x 54676x 54676x 64878x 1537x 2029x 2029x 1537x 64878x 203x 203x 64878x 7831x 7831x 64878x 556x 556x 64878x 64878x 70226x 70226x 70226x 70226x 70226x 70226x 70226x 60946x 60946x 50139x 49848x 49848x 60655x 70226x 70226x 548x 548x 548x 548x 548x 548x 48938x 48938x 48938x 48938x 48938x 48938x 48938x 548x 548x 548x 548x 548x 548x 54823x 54823x 54823x 54823x 54823x 548x 548x 548x 548x 548x 548x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 1516x 13783x 12238x 12238x 2728x 12238x 12206x 12206x 12206x 12206x 12206x 12238x 32x 32x 32x 13743x 13784x 9552x 1x 1x 9551x 9552x 9551x 9551x 9552x 9552x 9552x 9011x 9011x 9552x 9552x 4191x 13782x 13784x 2657x 2658x 2658x 2658x 2657x 4191x 4191x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 13784x 548x 548x 548x 548x 548x 548x 548x 9551x 9549x 9549x 9549x 9549x 2x 9551x 548x 548x 548x 548x 548x 548x 17675x 17675x 17675x 17675x 17675x 17675x 17675x 548x 548x 548x 548x 548x 548x 3121x 3121x 3121x 3121x 3121x 3121x 548x 548x 548x 548x 548x 548x 1569x 1569x 1569x 1569x 1569x 1569x 1569x 1569x 1569x 548x 548x 548x 548x 548x 548x 1733x 1733x 1733x 1733x 1733x 1733x 1733x 1733x 1733x 1733x 1733x 8644x 8644x 3737x 3737x 3737x 3737x 3737x 3737x 3737x 2657x 3736x 1080x 1078x 1078x 1078x 3555x 8644x 4907x 4907x 4907x 8644x 901x 901x 901x 901x 901x 901x 901x 1733x 548x 548x 548x 548x 548x 548x 5917x 5917x 5917x 5917x 5917x 4347x 4347x 4347x 2528x 2528x 2528x 2528x 2528x 2528x 4347x 1819x 1819x 1819x 4347x 4347x 4347x 4347x 5917x 5917x 4465x 5917x 548x 548x 548x 548x 548x 548x 12x 7x 7x 7x 7x 7x 12x 548x 548x 548x 548x 548x 548x 56167x 55705x 55705x 56167x 56167x 56167x 56167x 548x 548x 548x 548x 548x 548x 3805x 3805x 3805x 3803x 3803x 3803x 3805x 548x 548x 548x 548x 548x 548x 548x 6002x 6002x 6002x 4530x 6001x 1472x 1472x 1467x 1467x 1472x 4535x 4537x 4530x 4530x 4530x 6002x 6002x 548x 548x 548x 548x 548x 548x 548x 548x 1857x 1857x 1857x 2x 2x 1855x 1855x 1855x 1855x 1855x 1855x 1857x 1857x 1857x 1857x 1857x 548x 548x 548x 548x 548x 548x 548x 548x 3566x 3566x 3566x 3566x 67x 3566x 3499x 3499x 3499x 3479x 3479x 3546x 3546x 3546x 3566x 548x 548x 548x 548x 548x 548x 3499x 3499x 1215x 1215x 1215x 1215x 1215x 3499x 25x 25x 25x 25x 25x 25x 25x 2275x 2275x 3499x 3630x 1355x 3630x 3563x 3563x 3630x 23x 3630x 3540x 3540x 3540x 3563x 3563x 2270x 3499x 548x 548x 548x 548x 548x 548x 4600x 4598x 4600x 548x 548x 548x 548x 548x 548x 3968x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3968x 548x 548x 548x 548x 548x 548x 2303x 2303x 2303x 2303x 32x 32x 32x 27x 27x 27x 27x 27x 2271x 2303x 94x 4x 4x 4x 4x 4x 4x 4x 94x 10x 90x 80x 80x 80x 89x 89x 2177x 2292x 533x 533x 533x 533x 533x 961x 429x 961x 873x 873x 961x 961x 961x 533x 395x 395x 395x 533x 43x 43x 43x 76x 76x 76x 76x 76x 5x 5x 71x 71x 38x 433x 433x 433x 1644x 1644x 1644x 1644x 1644x 1644x 2303x 548x 548x 548x 548x 548x 548x 1644x 283x 283x 1642x 1644x 185x 5x 1644x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 5565x 5565x 5565x 5565x 5565x 5565x 2586x 2586x 2586x 2565x 2565x 2565x 2565x 2565x 2565x 5565x 312x 2979x 2667x 2667x 5251x 5565x 5565x 5565x 5565x 5565x 548x 548x 548x 548x 548x 548x 548x 5216x 5216x 5216x 5216x 5216x 22618x 22618x 22618x 22618x 7986x 7986x 22034x 22034x 4627x 4627x 5216x 548x 548x 548x 548x 548x 548x 548x 14853x 14853x 52x 52x 52x 14801x 14853x 548x 548x 548x 548x 548x 548x 548x 548x 548x 7986x 7986x 7986x 7986x 7986x 7986x 7986x 2x 2x 7986x 7986x 7986x 548x 548x 548x 548x 548x 548x 548x 548x 548x 4627x 4627x 4627x 14801x 12234x 2x 14801x 4627x 548x 548x 548x 548x 548x 548x 548x 22618x 22618x 22618x 22618x 3890x 3890x 3890x 3890x 3890x 1124x 3890x 3890x 1101x 1101x 1101x 1101x 1101x 1101x 1101x 1101x 1101x 1101x 911x 911x 911x 911x 2789x 2789x 2789x 2789x 3890x 21517x 21517x 21517x 21517x 21517x 22618x 29x 29x 29x 29x 29x 29x 29x 22618x 1x 1x 1x 22618x 1004x 1004x 1004x 861x 861x 861x 1004x 21517x 21517x 21517x 22618x 17596x 2357x 2357x 2357x 17596x 17596x 17596x 17596x 3921x 22618x 3921x 15587x 3776x 3776x 3776x 3776x 3776x 22608x 145x 145x 3676x 3676x 22618x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 24606x 24606x 24606x 24606x 24606x 24606x 24606x 6x 6x 24606x 102x 102x 102x 24598x 24598x 24598x 24598x 24598x 24598x 24598x 24598x 24598x 24598x 24598x 24606x 548x 548x 548x 548x 548x 548x 102653x 7691x 7691x 7691x 7691x 7691x 94962x 102653x 3157x 3157x 3157x 102651x 8231x 8231x 8231x 8231x 8231x 8231x 83574x 102653x 548x 548x 548x 548x 548x 548x 548x 548x 548x 292917x 292917x 292917x 1805x 1805x 253571x 2573x 2573x 1325x 292917x 548x 548x 548x 548x 548x 548x 548x 827681x 825319x 825319x 827681x 803460x 803460x 803460x 827681x 37808x 37808x 37808x 37808x 37808x 37808x 37808x 749715x 827681x 548x 548x 548x 548x 548x 548x 548x 8540x 122x 122x 8418x 8540x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 825319x 825319x 441647x 441647x 214x 214x 214x 214x 441363x 441363x 383672x 383672x 825315x 7794x 7794x 7794x 1x 1x 1x 1x 1x 1x 1x 7794x 3452x 3452x 4341x 4341x 375878x 825315x 330633x 825319x 548x 548x 548x 548x 548x 548x 548x 48697x 48697x 48697x 48697x 48697x 48697x 22074x 48684x 27123x 27123x 27123x 27123x 48697x 548x 548x 548x 548x 548x 548x 548x 548x 548x 22289x 22289x 22289x 22289x 22289x 22289x 22289x 22289x 22289x 22289x 22289x 9498x 9498x 22286x 12631x 12631x 12631x 21787x 21787x 22289x 548x 548x 548x 548x 548x 548x 548x 2362x 2362x 2362x 1086x 1086x 1086x 2362x 1276x 1276x 1276x 1276x 1276x 1992x 2362x 548x 548x 548x 548x 548x 548x 548x 803460x 803460x 803460x 14162x 14162x 14162x 14162x 14162x 14162x 14162x 803460x 548x 548x 548x 548x 548x 548x 548x 803460x 803460x 803460x 803460x 548x 548x 548x 548x 548x 548x 548x 993718x 982094x 982094x 860864x 860864x 914458x 10260x 10260x 10260x 8663x 993718x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 891252x 993718x 993718x 103502x 993718x 993718x 2x 2x 103500x 993718x 993718x 993718x 61x 993718x 103500x 103500x 103500x 103500x 103500x 993718x 993718x 993718x 993718x 993718x 993718x 993718x 993718x 52x 993718x 2x 2x 102466x 102466x 102466x 102466x 102466x 993718x 993718x 891252x 548x 548x 548x 548x 548x 548x 944039x 944039x 944039x 3640x 3640x 3640x 3640x 3640x 3493x 3493x 940399x 944039x 18315x 18315x 18315x 18315x 18315x 18315x 18315x 76x 76x 76x 18069x 18315x 18067x 18067x 922084x 944039x 15284x 15284x 15284x 15284x 15284x 15284x 55x 55x 15160x 15284x 15160x 15160x 906800x 906800x 906800x 944017x 944039x 4383x 944039x 4382x 4382x 4382x 4382x 4382x 4382x 4382x 891319x 944039x 548x 548x 548x 548x 548x 548x 908544x 908544x 908544x 908544x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 908718x 908718x 908718x 1336816x 246264x 246264x 246264x 246264x 246264x 246264x 246264x 246264x 246264x 1336816x 17607x 17607x 17607x 17607x 17607x 17607x 17607x 17607x 17607x 17607x 1336816x 150955x 150955x 150955x 150955x 150955x 150955x 150955x 1336816x 11650x 11650x 11650x 11648x 11648x 11650x 3410x 3410x 3410x 3410x 3410x 3410x 3410x 8238x 8238x 11650x 3213x 3213x 3213x 11648x 5025x 5025x 5025x 8163x 8163x 8163x 8163x 1336816x 3637x 3637x 3637x 3637x 3637x 3637x 3637x 906703x 906703x 906703x 908718x 11249x 11249x 11249x 11249x 895454x 908718x 548x 548x 548x 548x 548x 548x 251289x 14850x 14850x 14850x 14850x 14850x 14850x 236439x 251289x 548x 548x 548x 548x 548x 548x 168010x 168010x 168010x 168010x 229527x 86204x 229527x 229527x 3107x 3107x 3107x 3107x 229527x 221855x 221855x 229527x 167066x 168010x 548x 548x 548x 548x 548x 548x 922324x 922324x 922324x 922324x 174344x 922324x 922324x 14550x 922324x 3x 3x 3x 3x 3x 3x 1x 1x 922324x 733427x 922324x 733427x 922297x 90885x 90885x 35533x 35533x 90885x 15961x 15961x 15961x 15961x 15961x 90885x 28529x 90885x 90885x 6537x 6537x 6537x 6537x 90885x 4325x 90885x 90885x 642542x 922275x 642401x 642401x 922275x 58134x 58134x 58134x 58134x 922324x 1748x 1748x 1746x 1748x 1748x 1746x 1746x 922324x 922324x 922324x 31963x 31963x 31963x 31963x 31963x 31963x 922324x 11967x 11967x 11967x 11967x 11967x 11967x 922324x 2678x 922324x 13856x 922324x 152x 922324x 5492x 5492x 28x 28x 28x 28x 28x 28x 28x 5464x 5464x 922324x 516411x 516201x 922324x 922324x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 35533x 35533x 35523x 35523x 35523x 35533x 40150x 4627x 40150x 39275x 39275x 32129x 32129x 35533x 32129x 32129x 35533x 3398x 3398x 3398x 35533x 28731x 28731x 32129x 32129x 32129x 32129x 35533x 548x 548x 548x 548x 548x 548x 548x 548x 15931x 15931x 15931x 41946x 31349x 41946x 41946x 3085x 3085x 3085x 41946x 4851x 4851x 4851x 4851x 4851x 4851x 32753x 32753x 15459x 15931x 548x 548x 548x 548x 548x 548x 28529x 28529x 28529x 28529x 28529x 74136x 55262x 74136x 68628x 68628x 27764x 28529x 548x 548x 548x 548x 548x 548x 68628x 4771x 4771x 4771x 4771x 4771x 63857x 63857x 63857x 63857x 63857x 68628x 499x 499x 499x 8x 8x 8x 499x 68628x 2x 2x 2x 68628x 3929x 3929x 3929x 2827x 2827x 2827x 2827x 2827x 2827x 2827x 2827x 2827x 3929x 61030x 61030x 61030x 68628x 4183x 4183x 4183x 4183x 4183x 4183x 68628x 56718x 56718x 56718x 56718x 68627x 47919x 47919x 47919x 47919x 8799x 68628x 68531x 8789x 8789x 68531x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 68531x 8617x 8617x 8629x 68628x 548x 548x 548x 548x 548x 548x 548x 548x 20061x 20061x 20061x 20061x 20061x 548x 548x 548x 548x 548x 548x 13856x 13856x 13856x 76x 76x 76x 76x 74x 74x 74x 74x 13780x 13780x 13856x 13856x 13856x 548x 548x 548x 548x 548x 548x 548x 548x 177x 177x 177x 167x 167x 177x 177x 548x 548x 548x 548x 548x 548x 152x 152x 152x 105x 105x 105x 105x 103x 103x 103x 103x 145x 47x 47x 152x 152x 152x 152x 152x 548x 548x 548x 548x 548x 548x 187780x 187780x 187780x 187780x 187780x 187780x 187780x 5x 5x 187780x 6306x 6306x 187780x 2002x 2002x 2002x 187775x 187775x 187780x 548x 548x 548x 548x 548x 548x 548x 2002x 2002x 2002x 2002x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 6306x 6306x 6306x 30x 30x 6306x 548x 548x 548x 548x 548x 548x 18187x 18187x 18187x 18187x 18187x 18187x 18187x 24770x 24770x 24770x 24415x 24415x 24415x 24415x 24415x 17812x 17812x 18187x 548x 548x 548x 548x 548x 548x 548x 548x 42582x 42582x 42582x 42582x 42582x 42582x 42582x 42582x 42582x 37273x 37273x 42582x 42582x 548x 548x 548x 548x 548x 548x 703414x 703414x 688683x 688683x 703414x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 709060x 4094x 4094x 709060x 3x 3x 709060x 709060x 548x 548x 548x 548x 548x 548x 1009626x 1009060x 1009060x 1009060x 1009060x 1009626x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 161084x 161084x 161084x 1x 1x 161084x 161084x 161084x 161084x 548x 548x 548x 548x 548x 548x 548x 162440x 162440x 162440x 162440x 162440x 162440x 548x 548x 548x 548x 548x 548x 548x 175213x 3399x 3399x 3399x 3399x 3399x 175213x 10755x 10755x 10755x 10755x 10755x 161059x 175213x 548x 548x 548x 548x 548x 548x 548x 548x 3399x 3399x 3399x 8494x 5095x 8494x 8494x 1798x 1798x 1798x 8494x 1655x 1655x 1655x 1655x 1655x 1655x 1655x 5041x 5041x 1505x 3399x 548x 548x 548x 548x 548x 548x 548x 10755x 10755x 10755x 19528x 8779x 19528x 19528x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 17279x 17279x 17279x 17279x 19528x 5722x 5722x 19528x 11517x 11517x 11517x 11517x 11517x 11517x 439x 439x 439x 439x 439x 11514x 10993x 10993x 11517x 14729x 14729x 6005x 10755x 548x 548x 548x 548x 548x 548x 548x 92032x 92032x 92032x 9404x 9404x 9404x 9404x 9404x 92032x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 33781x 33781x 33781x 33691x 33691x 33781x 33781x 33781x 33781x 33781x 23x 23x 23x 26x 26x 6x 6x 6x 2x 2x 4x 4x 26x 26x 10x 10x 10x 10x 20x 20x 19x 19x 33781x 17x 17x 17x 21x 21x 21x 1x 1x 1x 1x 1x 20x 20x 7x 7x 33781x 12x 12x 12x 12x 12x 12x 33781x 38x 12x 12x 33781x 33781x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 41404x 41404x 41404x 41404x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 41128x 41128x 41128x 41128x 27120x 27120x 27120x 41128x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 548x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 548x 548x | /**
* @file parser.js
* @description The compiler's own ECMAScript parser, producing ESTree.
*
* ## Why the compiler owns a JavaScript parser
*
* Four places in the compiler have to understand real JavaScript rather than
* the small expression language templates use: the action compiler rewrites
* free identifiers in an `<action>` body, the `csp.strict` check proves an
* emitted bundle contains no `eval`, the component reader decides whether a
* `.component.js` file is markup or a module, and the declaration reader
* evaluates a constant initialiser. All four used `acorn`, which made a parser
* the published package's only runtime dependency.
*
* That dependency is now gone. `acorn` stays in `devDependencies`, where
* {@link ../../../../test/unit/jsParserDifferential.test.js} runs it against
* this parser over the whole repository and over generated sources: it is the
* oracle that keeps this file honest, and it is never on a build path.
*
* ## What it supports, and what it refuses
*
* Everything through ES2024 that appears in real source: modules, classes with
* private names and static blocks, generators, `async`/`await` including
* top-level await, optional chaining, destructuring with defaults and rest,
* template literals with nesting, regular-expression literals, labels, `for
* await`, BigInt, numeric separators, dynamic `import()`, `import.meta` and
* `new.target`.
*
* What it does not recognise, it refuses with a position. There is no
* permissive branch and no recovery: the compiler's rule is compile-or-refuse,
* and a parser that guessed at syntax it did not model would turn a build error
* into a silent miscompile.
*
* ## Shape
*
* Nodes are ESTree with acorn's conventions, because that is what the code
* reading them already expects: `start`/`end` offsets on every node, `loc`
* under `locations: true`, `ChainExpression` around an optional chain,
* `directive` on a directive-prologue statement, `attributes` on an import.
* The differential test compares property-for-property, so the two cannot
* drift.
* @module lib/compiler/parser/js/parser
*/
import { Tokenizer, JsParseError, RESERVED_WORDS } from './tokenizer.js';
/**
* Thrown internally to abandon a speculative parse. Never escapes.
* @type {object}
*/
const BAIL = { bail: true };
/**
* 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,
};
/**
* Assignment operators.
* @type {Set<string>}
*/
const ASSIGN_OPERATORS = new Set([
'=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=', '|=', '^=', '&=',
'**=', '&&=', '||=', '??=',
]);
/**
* Unary operators that take a prefix argument.
* @type {Set<string>}
*/
const UNARY_OPERATORS = new Set(['-', '+', '!', '~']);
/**
* Words that are reserved only in strict mode.
* @type {Set<string>}
*/
const STRICT_RESERVED = new Set([
'implements', 'interface', 'let', 'package', 'private', 'protected',
'public', 'static', 'yield',
]);
/** A scope that `var` reaches: the top level or a function body. @type {number} */
const SCOPE_VAR = 1;
/** A scope that only holds lexical declarations. @type {number} */
const SCOPE_BLOCK = 2;
/** A `catch (e)` scope, whose single simple binding `var` may redeclare. @type {number} */
const SCOPE_SIMPLE_CATCH = 4;
/**
* One binding scope.
*
* Duplicate-declaration errors are early errors rather than parse errors, but
* they are errors all the same, and a compiler whose parser accepted
* `const a = 1; const a = 2;` would hand the code generator a program that no
* engine will run. Tracking names is what lets this parser refuse them with a
* position, as acorn does.
*/
class BindingScope {
/**
* @param {number} flags - A combination of the SCOPE_ constants.
*/
constructor(flags) {
/** @type {number} */
this.flags = flags;
/** @type {Set<string>} Names declared with `var`, or as parameters. */
this.var = new Set();
/** @type {Set<string>} Names declared with `let`, `const` or `class`. */
this.lexical = new Set();
/** @type {Set<string>} Function declarations, which sloppy code may repeat. */
this.functions = new Set();
/** @type {string|null} The name a simple `catch` clause binds. */
this.simpleCatchName = null;
}
}
/**
* A recursive-descent ECMAScript parser.
*/
class Parser {
/**
* @param {string} source - The source text.
* @param {object} [options] - Parse options; see {@link parse}.
*/
constructor(source, options = {}) {
/** @type {string} */
this.input = source;
/** @type {object} */
this.options = options;
/** @type {boolean} */
this.locations = !!options.locations;
/** @type {'script'|'module'} */
this.sourceType = options.sourceType === 'module' ? 'module' : 'script';
/** @type {Tokenizer} */
this.tz = new Tokenizer(source);
if (options.allowHashBang !== false) this.tz.skipHashBang();
/** @type {import('./tokenizer.js').Token} */
this.tok = this.tz.nextToken();
/** @type {boolean} Whether `return` is currently legal. */
this.inFunction = !!options.allowReturnOutsideFunction;
/** @type {boolean} Whether `await` is currently an operator. */
this.inAsync = this.sourceType === 'module' || !!options.allowAwaitOutsideFunction;
/** @type {boolean} Whether `yield` is currently an operator. */
this.inGenerator = false;
/** @type {boolean} Whether `super.x` is currently legal. */
this.inMethod = !!options.allowSuperOutsideMethod;
/** @type {boolean} Whether `super()` is currently legal. */
this.inDerivedConstructor = !!options.allowSuperOutsideMethod;
/** @type {boolean} Whether `new.target` is currently legal. */
this.inNewTargetScope = !!options.allowNewTargetOutsideFunction;
/** @type {boolean} Whether strict-mode-only rules apply. */
this.strict = this.sourceType === 'module' || !!options.strict;
/** @type {string[]} Labels in scope, for `break`/`continue`. */
this.labels = [];
/** @type {number} Loop and switch nesting, for unlabelled `break`. */
this.breakDepth = 0;
/** @type {number} Loop nesting, for unlabelled `continue`. */
this.continueDepth = 0;
/** @type {BindingScope[]} The scope chain. */
this.scopeStack = [];
/**
* Every binding added, so a speculative parse can be undone exactly.
* Cloning the scope chain on each guess would cost a pass over every name
* in scope; an undo log costs one entry per binding.
* @type {Array<{set: Set<string>, name: string}>}
*/
this.bindingLog = [];
/**
* One frame per class body being parsed: the private names it declares,
* and the ones used inside it that no inner class declared. A use that
* reaches the bottom of the stack unresolved is an error, which is what
* makes `this.#gone` a build failure rather than a runtime one.
* @type {Array<{declared: Set<string>, used: Array<{name: string, pos: number}>,
* roles: Map<string, string>}>}
*/
this.privateNameStack = [];
/**
* Shorthand properties with a default, `{ a = 1 }`, which are legal only
* once the object turns out to be a destructuring pattern. Each is cleared
* by {@link Parser#toAssignable}; whatever is left when the parse ends was
* never a pattern.
* @type {Array<{start: number}>}
*/
this.pendingShorthandDefaults = [];
}
/**
* Pushes a scope.
* @param {number} flags - A combination of the SCOPE_ constants.
*/
enterScope(flags) {
this.scopeStack.push(new BindingScope(flags));
}
/**
* Pops the current scope.
*/
exitScope() {
this.scopeStack.pop();
}
/**
* @returns {BindingScope} The innermost scope.
*/
currentScope() {
return this.scopeStack[this.scopeStack.length - 1];
}
/**
* Records a name in a set, so the undo log can remove it again.
* @param {Set<string>} set - The set to add to.
* @param {string} name - The name.
*/
addBinding(set, name) {
if (set.has(name)) return;
set.add(name);
this.bindingLog.push({ set, name });
}
/**
* Declares a name, refusing a redeclaration the language forbids.
* @param {string} name - The declared name.
* @param {'var'|'lexical'|'function'|'param'|'simple-catch'} kind - How it is declared.
* @param {number} pos - Where to report a clash.
*/
declareName(name, kind, pos) {
const scope = this.currentScope();
if (!scope) return;
if (kind === 'lexical' || kind === 'simple-catch' || (kind === 'function' && this.strict)) {
if (scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name)) {
this.raise(`Identifier '${name}' has already been declared`, pos);
}
this.addBinding(scope.lexical, name);
if (kind === 'simple-catch') scope.simpleCatchName = name;
return;
}
if (kind === 'param') {
this.addBinding(scope.var, name);
return;
}
// `var` and a sloppy function declaration reach the nearest var scope, and
// may not pass a lexical binding of the same name on the way.
for (let i = this.scopeStack.length - 1; i >= 0; i -= 1) {
const current = this.scopeStack[i];
const shadowedByCatch = (current.flags & SCOPE_SIMPLE_CATCH) !== 0 && current.simpleCatchName === name;
if (current.lexical.has(name) && !shadowedByCatch) {
this.raise(`Identifier '${name}' has already been declared`, pos);
}
if (kind === 'function' && current.functions.has(name) && (current.flags & SCOPE_VAR) === 0) {
// Two function declarations in one block are allowed in sloppy code.
break;
}
this.addBinding(current.var, name);
if (kind === 'function') this.addBinding(current.functions, name);
if ((current.flags & SCOPE_VAR) !== 0) break;
}
}
// ---------------------------------------------------------------- utilities
/**
* Raises a positioned error.
* @param {string} message - What went wrong.
* @param {number} [pos] - Where.
* @throws {import('./tokenizer.js').JsParseError} Always.
*/
raise(message, pos = this.tok.start) {
this.tz.raise(message, pos);
}
/**
* Reports the current token as unexpected.
* @throws {import('./tokenizer.js').JsParseError} Always.
*/
unexpected() {
this.raise('Unexpected token');
}
/**
* Advances to the next token.
*/
next() {
this.tok = this.tz.nextToken();
}
/**
* @param {string} value - A punctuator.
* @returns {boolean} Whether the current token is it.
*/
isPunct(value) {
return this.tok.type === 'punct' && this.tok.value === value;
}
/**
* @param {string} value - A word.
* @returns {boolean} Whether the current token is that identifier or keyword.
*/
isName(value) {
return this.tok.type === 'name' && this.tok.value === value;
}
/**
* Consumes the current token when it is the given punctuator.
* @param {string} value - A punctuator.
* @returns {boolean} Whether it was consumed.
*/
eatPunct(value) {
if (!this.isPunct(value)) return false;
this.next();
return true;
}
/**
* Consumes the current token when it is the given word.
* @param {string} value - A word.
* @returns {boolean} Whether it was consumed.
*/
eatName(value) {
if (!this.isName(value)) return false;
this.next();
return true;
}
/**
* Consumes the given punctuator or fails.
* @param {string} value - A punctuator.
*/
expectPunct(value) {
if (!this.eatPunct(value)) this.raise(`Expected '${value}'`);
}
/**
* Consumes the given word or fails.
* @param {string} value - A word.
*/
expectName(value) {
if (!this.eatName(value)) this.raise(`Expected '${value}'`);
}
/**
* @returns {boolean} Whether a semicolon may be inserted here.
*/
canInsertSemicolon() {
return this.tok.type === 'eof' || this.isPunct('}') || this.tok.nlBefore;
}
/**
* Consumes a statement-ending semicolon, real or inserted.
*/
semicolon() {
if (this.eatPunct(';')) return;
if (!this.canInsertSemicolon()) this.raise('Expected a semicolon');
}
/**
* Starts a node at the current token.
* @returns {object} A bare node.
*/
startNode() {
return this.startNodeAt(this.tok.start);
}
/**
* Starts a node at a given offset.
* @param {number} pos - The offset.
* @returns {object} A bare node.
*/
startNodeAt(pos) {
/** @type {any} */
const node = { type: '', start: pos, end: 0 };
if (this.locations) node.loc = { start: this.tz.locate(pos), end: null };
return node;
}
/**
* Finishes a node at the end of the previous token.
* @param {object} node - The node.
* @param {string} type - Its ESTree type.
* @returns {object} The node.
*/
finishNode(node, type) {
return this.finishNodeAt(node, type, this.tz.lastEnd);
}
/**
* Finishes a node at an explicit offset.
* @param {object} node - The node.
* @param {string} type - Its ESTree type.
* @param {number} end - The end offset.
* @returns {object} The node.
*/
finishNodeAt(node, type, end) {
/** @type {any} */
const target = node;
target.type = type;
target.end = end;
if (this.locations) target.loc.end = this.tz.locate(end);
return target;
}
/**
* Captures parser and tokenizer state for a speculative parse.
* @returns {object} The state.
*/
saveState() {
return {
tz: this.tz.save(),
tok: this.tok,
inFunction: this.inFunction,
inAsync: this.inAsync,
inGenerator: this.inGenerator,
inMethod: this.inMethod,
inDerivedConstructor: this.inDerivedConstructor,
inNewTargetScope: this.inNewTargetScope,
strict: this.strict,
labels: this.labels.slice(),
breakDepth: this.breakDepth,
continueDepth: this.continueDepth,
scopeDepth: this.scopeStack.length,
privateDepth: this.privateNameStack.length,
bindings: this.bindingLog.length,
shorthandDefaults: this.pendingShorthandDefaults.length,
};
}
/**
* Restores state captured by {@link Parser#saveState}.
* @param {object} state - The state.
*/
restoreState(state) {
this.tz.restore(state.tz);
this.tok = state.tok;
this.inFunction = state.inFunction;
this.inAsync = state.inAsync;
this.inGenerator = state.inGenerator;
this.inMethod = state.inMethod;
this.inDerivedConstructor = state.inDerivedConstructor;
this.inNewTargetScope = state.inNewTargetScope;
this.strict = state.strict;
this.labels = state.labels;
this.breakDepth = state.breakDepth;
this.continueDepth = state.continueDepth;
this.scopeStack.length = state.scopeDepth;
this.privateNameStack.length = state.privateDepth;
while (this.bindingLog.length > state.bindings) {
const entry = this.bindingLog.pop();
entry.set.delete(entry.name);
}
this.pendingShorthandDefaults.length = state.shorthandDefaults;
}
/**
* Peeks at the token after the current one.
* @returns {import('./tokenizer.js').Token} The next token.
*/
peek() {
const state = this.tz.save();
const token = this.tz.nextToken();
this.tz.restore(state);
return token;
}
// --------------------------------------------------------------- statements
/**
* Parses the whole input.
* @returns {object} A Program node.
*/
parseTopLevel() {
const node = this.startNodeAt(0);
node.body = [];
this.enterScope(SCOPE_VAR);
let prologue = true;
while (this.tok.type !== 'eof') {
const statement = this.parseStatement(true);
if (prologue) {
if (this.isDirective(statement)) {
statement.directive = statement.expression.raw.slice(1, -1);
if (statement.directive === 'use strict') this.strict = true;
} else {
prologue = false;
}
}
node.body.push(statement);
}
this.flushShorthandDefaults();
this.exitScope();
node.sourceType = this.sourceType;
return this.finishNodeAt(node, 'Program', this.input.length);
}
/**
* Reports any `{ a = 1 }` that never became a destructuring pattern.
*/
flushShorthandDefaults() {
if (this.pendingShorthandDefaults.length === 0) return;
const first = this.pendingShorthandDefaults.reduce((a, b) => (a.start <= b.start ? a : b));
this.raise('A shorthand property default is only valid in a destructuring pattern', first.start);
}
/**
* Whether a statement is a directive-prologue entry.
* @param {object} statement - The statement.
* @returns {boolean} True when it is.
*/
isDirective(statement) {
return (
statement.type === 'ExpressionStatement' &&
statement.expression.type === 'Literal' &&
// `("use strict");` is an expression, not a directive, so the literal has
// to begin where the statement does.
statement.expression.start === statement.start &&
typeof statement.expression.value === 'string' &&
typeof statement.expression.raw === 'string' &&
(statement.expression.raw[0] === '"' || statement.expression.raw[0] === "'")
);
}
/**
* Parses one statement.
* @param {boolean} [topLevel] - Whether `import`/`export` are legal here.
* @param {boolean} [declarations] - Whether a lexical declaration is legal here,
* which it is not as the body of an `if` or a loop.
* @param {'list'|'single'|'none'} [functions] - Where a function declaration
* may stand: anywhere in a statement list, directly as a sloppy `if`
* branch, or nowhere.
* @returns {object} The statement node.
*/
parseStatement(topLevel = false, declarations = true, functions = 'list') {
if (this.tok.type === 'punct') {
switch (this.tok.value) {
case '{':
return this.parseBlock();
case ';': {
const node = this.startNode();
this.next();
return this.finishNode(node, 'EmptyStatement');
}
default:
break;
}
}
if (this.tok.type === 'name') {
switch (this.tok.value) {
case 'var':
// `var` is a statement, so `if (a) var b = 1;` is legal; only a
// lexical declaration is refused in a statement position.
return this.parseVarStatement('var');
case 'const':
if (!declarations) this.raise('A lexical declaration cannot be the body of a statement');
return this.parseVarStatement('const');
case 'let':
// Where a declaration is not allowed, `let` can only be a reference,
// which is how `for (a of b) let x = 1` fails at `x` rather than here.
if (declarations && this.isLetDeclaration()) return this.parseVarStatement('let');
break;
case 'function':
// A statement list always allows one. Directly as an `if` branch,
// sloppy code allows it too, which Annex B grandfathered in.
// Everywhere else -- a loop body, a label inside one -- it is an error.
if (functions === 'none' || (functions === 'single' && this.strict)) {
this.raise('A function declaration cannot appear here');
}
// Annex B grandfathered a plain function, never a generator.
if (functions !== 'list' && this.peek().value === '*') {
this.raise('A generator declaration cannot appear here');
}
return this.parseFunctionStatement(false);
case 'class':
if (!declarations) this.raise('A lexical declaration cannot be the body of a statement');
return this.parseClass(this.startNode(), true);
case 'if':
return this.parseIf();
case 'for':
return this.parseFor();
case 'while':
return this.parseWhile();
case 'do':
return this.parseDoWhile();
case 'switch':
return this.parseSwitch();
case 'try':
return this.parseTry();
case 'with':
return this.parseWith();
case 'return':
return this.parseReturn();
case 'throw':
return this.parseThrow();
case 'break':
case 'continue':
return this.parseBreakContinue(this.tok.value);
case 'debugger': {
const node = this.startNode();
this.next();
this.semicolon();
return this.finishNode(node, 'DebuggerStatement');
}
case 'import': {
const after = this.peek();
const isCall = after.type === 'punct' && (after.value === '(' || after.value === '.');
if (!isCall) {
if (this.sourceType !== 'module') this.raise("'import' is only allowed in a module");
if (!topLevel) this.raise("'import' may only appear at the top level");
return this.parseImport();
}
break;
}
case 'export':
if (this.sourceType !== 'module') this.raise("'export' is only allowed in a module");
if (!topLevel) this.raise("'export' may only appear at the top level");
return this.parseExport();
case 'async': {
const after = this.peek();
if (after.type === 'name' && after.value === 'function' && !after.nlBefore) {
if (functions !== 'list') this.raise('A function declaration cannot appear here');
return this.parseFunctionStatement(true);
}
break;
}
default:
break;
}
}
// A label is the one place a statement starts with an identifier followed
// by a colon, and it has to be told apart from an expression before the
// expression is parsed.
if (this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value)) {
const after = this.peek();
if (after.type === 'punct' && after.value === ':') {
return this.parseLabeledStatement(functions);
}
}
const node = this.startNode();
node.expression = this.parseExpression();
this.semicolon();
return this.finishNode(node, 'ExpressionStatement');
}
/**
* Whether the `let` at the current token opens a declaration.
*
* `let` is not a reserved word, so `let` alone, `let.x` and `let(0)` are all
* references to a variable called `let`. A declaration is `let` followed by a
* binding: an identifier, `[` or `{`.
* @returns {boolean} True when it declares.
*/
isLetDeclaration() {
const after = this.peek();
if (after.type === 'name') return !RESERVED_WORDS.has(after.value) || after.value === 'let';
if (after.type === 'punct') return after.value === '[' || after.value === '{';
return false;
}
/**
* Parses `{ … }`.
* @param {boolean} [ownScope] - Whether the block introduces a scope of its
* own. A `catch` body shares the clause's scope, so its block does not.
* @returns {object} A BlockStatement.
*/
parseBlock(ownScope = true) {
const node = this.startNode();
this.expectPunct('{');
if (ownScope) this.enterScope(SCOPE_BLOCK);
node.body = [];
while (!this.eatPunct('}')) {
if (this.tok.type === 'eof') this.raise("Expected '}'");
node.body.push(this.parseStatement());
}
if (ownScope) this.exitScope();
return this.finishNode(node, 'BlockStatement');
}
/**
* Parses a `var`, `let` or `const` statement.
* @param {string} kind - The declaration keyword.
* @returns {object} A VariableDeclaration.
*/
parseVarStatement(kind) {
const node = this.startNode();
this.next();
this.parseVarDeclarations(node, kind, false);
this.semicolon();
return this.finishNode(node, 'VariableDeclaration');
}
/**
* Parses the declarator list of a variable declaration.
* @param {object} node - The declaration node.
* @param {string} kind - The declaration keyword.
* @param {boolean} noIn - Whether `in` is an operator here.
*/
parseVarDeclarations(node, kind, noIn) {
node.declarations = [];
node.kind = kind;
for (;;) {
const declarator = this.startNode();
declarator.id = this.parseBindingAtom(kind === 'var' ? 'var' : 'lexical');
declarator.init = this.eatPunct('=') ? this.parseMaybeAssign(noIn) : null;
if (declarator.init === null && kind === 'const' && !noIn) {
this.raise('A const declaration must have an initialiser', declarator.start);
}
if (declarator.init === null && declarator.id.type !== 'Identifier' && !noIn) {
this.raise('A destructuring declaration must have an initialiser', declarator.start);
}
node.declarations.push(this.finishNode(declarator, 'VariableDeclarator'));
if (!this.eatPunct(',')) break;
}
}
/**
* Parses a function declaration.
* @param {boolean} isAsync - Whether it is `async function`.
* @returns {object} A FunctionDeclaration.
*/
parseFunctionStatement(isAsync) {
const node = this.startNode();
if (isAsync) this.next();
this.expectName('function');
const isGenerator = this.eatPunct('*');
const namePos = this.tok.start;
node.id = this.parseIdentifier();
this.declareName(node.id.name, 'function', namePos);
return this.parseFunctionRest(node, 'FunctionDeclaration', isAsync, isGenerator);
}
/**
* Parses the parameters and body of a function.
* @param {object} node - The started node.
* @param {string} type - FunctionDeclaration or FunctionExpression.
* @param {boolean} isAsync - Whether it is async.
* @param {boolean} isGenerator - Whether it is a generator.
* @returns {object} The finished node.
*/
parseFunctionRest(node, type, isAsync, isGenerator) {
node.expression = false;
node.generator = isGenerator;
node.async = isAsync;
node.params = this.parseParams();
const outer = this.enterFunction(isAsync, isGenerator);
this.declareParams(node.params);
node.body = this.parseFunctionBody();
this.exitFunction(outer);
return this.finishNode(node, type);
}
/**
* Enters a function scope.
* @param {boolean} isAsync - Whether it is async.
* @param {boolean} isGenerator - Whether it is a generator.
* @returns {object} The state to restore.
*/
enterFunction(isAsync, isGenerator) {
const outer = {
inFunction: this.inFunction,
inAsync: this.inAsync,
inGenerator: this.inGenerator,
inNewTargetScope: this.inNewTargetScope,
labels: this.labels,
breakDepth: this.breakDepth,
continueDepth: this.continueDepth,
};
this.inFunction = true;
this.inAsync = isAsync;
this.inGenerator = isGenerator;
this.inNewTargetScope = true;
this.labels = [];
this.breakDepth = 0;
this.continueDepth = 0;
this.enterScope(SCOPE_VAR);
return outer;
}
/**
* Leaves a function scope.
* @param {object} outer - The state from {@link Parser#enterFunction}.
*/
exitFunction(outer) {
this.exitScope();
Object.assign(this, outer);
}
/**
* Parses a function body, honouring a `'use strict'` directive in it.
* @returns {object} A BlockStatement.
*/
parseFunctionBody() {
const node = this.startNode();
this.expectPunct('{');
node.body = [];
const outerStrict = this.strict;
let prologue = true;
while (!this.eatPunct('}')) {
if (this.tok.type === 'eof') this.raise("Expected '}'");
const statement = this.parseStatement();
if (prologue) {
if (this.isDirective(statement)) {
statement.directive = statement.expression.raw.slice(1, -1);
if (statement.directive === 'use strict') this.strict = true;
} else {
prologue = false;
}
}
node.body.push(statement);
}
this.strict = outerStrict;
return this.finishNode(node, 'BlockStatement');
}
/**
* Parses a parenthesised parameter list.
* @returns {object[]} The parameter patterns.
*/
parseParams() {
this.expectPunct('(');
/** @type {object[]} */
const params = [];
let first = true;
while (!this.eatPunct(')')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct(')')) break;
if (this.isPunct('...')) {
const rest = this.startNode();
this.next();
rest.argument = this.parseBindingAtom();
params.push(this.finishNode(rest, 'RestElement'));
// A rest parameter must be last and cannot be followed by a comma.
this.expectPunct(')');
return params;
}
params.push(this.parseMaybeDefault());
}
return params;
}
/**
* Declares a function's parameters into the scope it has just entered.
*
* Parameters are parsed before the scope exists, because a default value is
* an ordinary expression in the enclosing context; the names are recorded
* once the body's scope is open.
* @param {object[]} params - The parameter patterns.
*/
declareParams(params) {
/** @type {string[]} */
const names = [];
const collect = (node) => {
if (!node) return;
switch (node.type) {
case 'Identifier':
names.push(node.name);
return;
case 'ObjectPattern':
for (const property of node.properties) {
collect(property.type === 'RestElement' ? property.argument : property.value);
}
return;
case 'ArrayPattern':
for (const element of node.elements) collect(element);
return;
case 'AssignmentPattern':
collect(node.left);
return;
case 'RestElement':
collect(node.argument);
return;
default:
}
};
for (const param of params) collect(param);
// A duplicate parameter name is only legal in a sloppy function whose
// parameters are all plain identifiers.
const simple = params.every((param) => param.type === 'Identifier');
if (this.strict || !simple) {
const seen = new Set();
for (const name of names) {
if (seen.has(name)) this.raise(`Duplicate parameter name '${name}'`);
seen.add(name);
}
}
for (const name of names) this.declareName(name, 'param', this.tok.start);
}
/**
* Parses `if`.
* @returns {object} An IfStatement.
*/
parseIf() {
const node = this.startNode();
this.next();
node.test = this.parseParenExpression();
node.consequent = this.parseStatement(false, false, 'single');
node.alternate = this.eatName('else') ? this.parseStatement(false, false, 'single') : null;
return this.finishNode(node, 'IfStatement');
}
/**
* Parses a parenthesised expression, as a control structure's head.
* @returns {object} The expression.
*/
parseParenExpression() {
this.expectPunct('(');
const expression = this.parseExpression();
this.expectPunct(')');
return expression;
}
/**
* Parses `for`, `for…in`, `for…of` and `for await…of`.
* @returns {object} The statement.
*/
parseFor() {
const node = this.startNode();
this.next();
// The whole head shares one block scope with the body, so `for (let i …)`
// declares `i` once per loop rather than in the enclosing scope.
this.enterScope(SCOPE_BLOCK);
const isAwait = this.isName('await') && this.inAsync;
if (isAwait) this.next();
this.expectPunct('(');
let init = null;
let startsWithLet = false;
if (this.isPunct(';')) {
// No initialiser: `for (; test; update)`.
} else if (
this.isName('var') ||
this.isName('const') ||
(this.isName('let') && this.isLetDeclaration())
) {
const declaration = this.startNode();
const kind = this.tok.value;
this.next();
this.parseVarDeclarations(declaration, kind, true);
init = this.finishNode(declaration, 'VariableDeclaration');
} else {
startsWithLet = this.isName('let');
init = this.parseExpression(true);
}
if (this.isName('of') || this.isName('in')) {
if (startsWithLet) {
this.raise("The left-hand side of a for-in/of loop may not start with 'let'", init.start);
}
const isOf = this.tok.value === 'of';
if (isAwait && !isOf) this.raise("'for await' requires 'of'");
this.next();
node.left = this.toForTarget(init);
node.right = isOf ? this.parseMaybeAssign() : this.parseExpression();
this.expectPunct(')');
if (isOf) node.await = isAwait;
node.body = this.parseLoopBody();
this.exitScope();
return this.finishNode(node, isOf ? 'ForOfStatement' : 'ForInStatement');
}
if (isAwait) this.raise("'for await' requires 'of'");
if (init && init.type === 'VariableDeclaration') {
for (const declarator of init.declarations) {
if (declarator.init === null && declarator.id.type !== 'Identifier') {
this.raise('A destructuring declaration must have an initialiser', declarator.start);
}
if (declarator.init === null && init.kind === 'const') {
this.raise('A const declaration must have an initialiser', declarator.start);
}
}
}
node.init = init;
this.expectPunct(';');
node.test = this.isPunct(';') ? null : this.parseExpression();
this.expectPunct(';');
node.update = this.isPunct(')') ? null : this.parseExpression();
this.expectPunct(')');
node.body = this.parseLoopBody();
this.exitScope();
return this.finishNode(node, 'ForStatement');
}
/**
* Converts a `for` head into an assignment target.
* @param {object} node - The parsed head.
* @returns {object} A declaration or a pattern.
*/
toForTarget(node) {
if (node.type === 'VariableDeclaration') {
if (node.declarations.length !== 1) {
this.raise('A for-in/of head may declare only one binding', node.start);
}
if (node.declarations[0].init !== null) {
this.raise('A for-in/of binding may not have an initialiser', node.start);
}
return node;
}
return this.toAssignable(node, true);
}
/**
* Parses a loop body, tracking that `break` and `continue` are legal in it.
* @returns {object} The body statement.
*/
parseLoopBody() {
this.breakDepth += 1;
this.continueDepth += 1;
const body = this.parseStatement(false, false, 'none');
this.breakDepth -= 1;
this.continueDepth -= 1;
return body;
}
/**
* Parses `while`.
* @returns {object} A WhileStatement.
*/
parseWhile() {
const node = this.startNode();
this.next();
node.test = this.parseParenExpression();
node.body = this.parseLoopBody();
return this.finishNode(node, 'WhileStatement');
}
/**
* Parses `do … while`.
* @returns {object} A DoWhileStatement.
*/
parseDoWhile() {
const node = this.startNode();
this.next();
node.body = this.parseLoopBody();
this.expectName('while');
node.test = this.parseParenExpression();
// The semicolon after `do {} while ()` is optional even without a newline.
this.eatPunct(';');
return this.finishNode(node, 'DoWhileStatement');
}
/**
* Parses `switch`.
* @returns {object} A SwitchStatement.
*/
parseSwitch() {
const node = this.startNode();
this.next();
node.discriminant = this.parseParenExpression();
node.cases = [];
this.expectPunct('{');
// Every clause of a switch shares one block scope.
this.enterScope(SCOPE_BLOCK);
this.breakDepth += 1;
let current = null;
let sawDefault = false;
while (!this.isPunct('}')) {
if (this.tok.type === 'eof') this.raise("Expected '}'");
if (this.isName('case') || this.isName('default')) {
const isCase = this.tok.value === 'case';
if (current) this.finishNode(current, 'SwitchCase');
current = this.startNode();
node.cases.push(current);
current.consequent = [];
this.next();
if (isCase) {
current.test = this.parseExpression();
} else {
if (sawDefault) this.raise('Duplicate default clause', current.start);
sawDefault = true;
current.test = null;
}
this.expectPunct(':');
} else {
if (!current) this.unexpected();
current.consequent.push(this.parseStatement());
}
}
// A case ends where its last statement ends, not where the switch's brace
// is, so the open case is closed before the brace is consumed.
if (current) this.finishNode(current, 'SwitchCase');
this.expectPunct('}');
this.exitScope();
this.breakDepth -= 1;
return this.finishNode(node, 'SwitchStatement');
}
/**
* Parses `try`.
* @returns {object} A TryStatement.
*/
parseTry() {
const node = this.startNode();
this.next();
node.block = this.parseBlock();
node.handler = null;
if (this.isName('catch')) {
const clause = this.startNode();
this.next();
if (this.eatPunct('(')) {
// A single simple binding may be redeclared with `var` in the body,
// which is why the parameter's own scope is the body's scope too.
const simple = this.isName('(') === false && this.tok.type === 'name';
this.enterScope(SCOPE_BLOCK | (simple ? SCOPE_SIMPLE_CATCH : 0));
clause.param = this.parseBindingAtom(simple ? 'simple-catch' : 'lexical');
this.expectPunct(')');
} else {
clause.param = null;
this.enterScope(SCOPE_BLOCK);
}
clause.body = this.parseBlock(false);
this.exitScope();
node.handler = this.finishNode(clause, 'CatchClause');
}
node.finalizer = this.eatName('finally') ? this.parseBlock() : null;
if (!node.handler && !node.finalizer) this.raise('Missing catch or finally clause', node.start);
return this.finishNode(node, 'TryStatement');
}
/**
* Parses `with`, which strict mode forbids.
* @returns {object} A WithStatement.
*/
parseWith() {
if (this.strict) this.raise("'with' is not allowed in strict mode");
const node = this.startNode();
this.next();
node.object = this.parseParenExpression();
node.body = this.parseStatement(false, false, 'none');
return this.finishNode(node, 'WithStatement');
}
/**
* Parses `return`.
* @returns {object} A ReturnStatement.
*/
parseReturn() {
if (!this.inFunction) this.raise("'return' outside of function");
const node = this.startNode();
this.next();
node.argument = this.isPunct(';') || this.canInsertSemicolon() ? null : this.parseExpression();
this.semicolon();
return this.finishNode(node, 'ReturnStatement');
}
/**
* Parses `throw`.
* @returns {object} A ThrowStatement.
*/
parseThrow() {
const node = this.startNode();
this.next();
if (this.tok.nlBefore) this.raise('Illegal newline after throw');
node.argument = this.parseExpression();
this.semicolon();
return this.finishNode(node, 'ThrowStatement');
}
/**
* Parses `break` or `continue`.
* @param {string} keyword - Which one.
* @returns {object} The statement.
*/
parseBreakContinue(keyword) {
const node = this.startNode();
this.next();
if (this.isPunct(';') || this.canInsertSemicolon()) {
node.label = null;
} else {
node.label = this.parseIdentifier();
if (!this.labels.includes(node.label.name)) {
this.raise(`Unknown label '${node.label.name}'`, node.label.start);
}
}
this.semicolon();
if (node.label === null) {
const depth = keyword === 'break' ? this.breakDepth : this.continueDepth;
if (depth === 0) this.raise(`Unsyntactic ${keyword}`, node.start);
}
return this.finishNode(node, keyword === 'break' ? 'BreakStatement' : 'ContinueStatement');
}
/**
* Parses `label: statement`.
* @param {'list'|'single'|'none'} [functions] - The label's own position, which
* it passes to its body rather than widening.
* @returns {object} A LabeledStatement.
*/
parseLabeledStatement(functions = 'list') {
const node = this.startNode();
node.label = this.parseIdentifier();
if (this.labels.includes(node.label.name)) {
this.raise(`Label '${node.label.name}' is already declared`, node.label.start);
}
this.expectPunct(':');
this.labels.push(node.label.name);
// A labelled statement may be broken out of even when it is not a loop.
this.breakDepth += 1;
// `l: function f(){}` is legal in a sloppy statement list and nowhere else,
// so the label passes its own position down rather than widening it.
node.body = this.parseStatement(false, false, functions === 'list' ? 'single' : 'none');
this.breakDepth -= 1;
this.labels.pop();
return this.finishNode(node, 'LabeledStatement');
}
// ------------------------------------------------------------------ modules
/**
* Parses an `import` declaration.
* @returns {object} An ImportDeclaration.
*/
parseImport() {
const node = this.startNode();
this.next();
node.specifiers = [];
if (this.tok.type === 'string') {
node.source = this.parseLiteral();
} else {
node.specifiers = this.parseImportSpecifiers();
this.expectName('from');
if (this.tok.type !== 'string') this.raise('Expected a module specifier');
node.source = this.parseLiteral();
}
node.attributes = this.parseImportAttributes();
this.semicolon();
return this.finishNode(node, 'ImportDeclaration');
}
/**
* Parses the specifier list of an `import`.
* @returns {object[]} The specifiers.
*/
parseImportSpecifiers() {
const specifiers = [];
if (this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value)) {
const node = this.startNode();
node.local = this.parseIdentifier();
specifiers.push(this.finishNode(node, 'ImportDefaultSpecifier'));
if (!this.eatPunct(',')) return specifiers;
}
if (this.isPunct('*')) {
const node = this.startNode();
this.next();
this.expectName('as');
node.local = this.parseIdentifier();
specifiers.push(this.finishNode(node, 'ImportNamespaceSpecifier'));
return specifiers;
}
this.expectPunct('{');
let first = true;
while (!this.eatPunct('}')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct('}')) break;
const node = this.startNode();
node.imported = this.parseModuleExportName();
if (this.eatName('as')) {
node.local = this.parseIdentifier();
} else {
if (node.imported.type !== 'Identifier') this.raise('A string import name needs `as`', node.start);
node.local = node.imported;
}
specifiers.push(this.finishNode(node, 'ImportSpecifier'));
}
return specifiers;
}
/**
* Parses an export name, which may be a string since ES2022.
* @returns {object} An Identifier or a string Literal.
*/
parseModuleExportName() {
if (this.tok.type === 'string') return this.parseLiteral();
return this.parseIdentifierName();
}
/**
* Parses a `with { … }` import attributes clause.
* @returns {object[]} The attributes.
*/
parseImportAttributes() {
if (!this.isName('with') || this.tok.nlBefore) return [];
this.next();
this.expectPunct('{');
const attributes = [];
let first = true;
while (!this.eatPunct('}')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct('}')) break;
const node = this.startNode();
node.key = this.tok.type === 'string' ? this.parseLiteral() : this.parseIdentifierName();
this.expectPunct(':');
if (this.tok.type !== 'string') this.raise('An import attribute value must be a string');
node.value = this.parseLiteral();
attributes.push(this.finishNode(node, 'ImportAttribute'));
}
return attributes;
}
/**
* Parses an `export` declaration in all its forms.
* @returns {object} The export node.
*/
parseExport() {
const node = this.startNode();
this.next();
if (this.eatPunct('*')) {
node.exported = this.eatName('as') ? this.parseModuleExportName() : null;
this.expectName('from');
if (this.tok.type !== 'string') this.raise('Expected a module specifier');
node.source = this.parseLiteral();
node.attributes = this.parseImportAttributes();
this.semicolon();
return this.finishNode(node, 'ExportAllDeclaration');
}
if (this.eatName('default')) {
if (this.isName('function') || (this.isName('async') && this.peek().value === 'function')) {
// The declaration node starts at `async`, not at `function`.
const inner = this.startNode();
const isAsync = this.eatName('async');
this.expectName('function');
const isGenerator = this.eatPunct('*');
inner.id = this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value) ? this.parseIdentifier() : null;
node.declaration = this.parseFunctionRest(inner, 'FunctionDeclaration', isAsync, isGenerator);
} else if (this.isName('class')) {
node.declaration = this.parseClass(this.startNode(), 'nullable');
} else {
node.declaration = this.parseMaybeAssign();
this.semicolon();
}
return this.finishNode(node, 'ExportDefaultDeclaration');
}
if (this.isPunct('{')) {
node.declaration = null;
node.specifiers = [];
this.next();
let first = true;
while (!this.eatPunct('}')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct('}')) break;
const specifier = this.startNode();
specifier.local = this.parseModuleExportName();
specifier.exported = this.eatName('as') ? this.parseModuleExportName() : specifier.local;
node.specifiers.push(this.finishNode(specifier, 'ExportSpecifier'));
}
if (this.eatName('from')) {
if (this.tok.type !== 'string') this.raise('Expected a module specifier');
node.source = this.parseLiteral();
node.attributes = this.parseImportAttributes();
} else {
node.source = null;
node.attributes = [];
for (const specifier of node.specifiers) {
if (specifier.local.type !== 'Identifier') {
this.raise('A string export name needs a module specifier', specifier.start);
}
// Without `from`, the local half names a binding in this module, so
// it has to be a usable identifier: `export { default as x }` is only
// meaningful when re-exporting.
if (RESERVED_WORDS.has(specifier.local.name)) {
this.raise(`Unexpected keyword '${specifier.local.name}'`, specifier.local.start);
}
this.checkReference(specifier.local.name, specifier.local.start);
}
}
this.semicolon();
return this.finishNode(node, 'ExportNamedDeclaration');
}
node.declaration = this.parseExportedDeclaration();
node.specifiers = [];
node.source = null;
node.attributes = [];
return this.finishNode(node, 'ExportNamedDeclaration');
}
/**
* Parses the declaration after `export`.
* @returns {object} The declaration.
*/
parseExportedDeclaration() {
if (this.isName('var') || this.isName('const') || (this.isName('let') && this.isLetDeclaration())) {
return this.parseVarStatement(this.tok.value);
}
if (this.isName('function')) return this.parseFunctionStatement(false);
if (this.isName('async') && this.peek().value === 'function') return this.parseFunctionStatement(true);
if (this.isName('class')) return this.parseClass(this.startNode(), true);
return this.unexpected();
}
// ------------------------------------------------------------------ classes
/**
* Parses a class declaration or expression.
* @param {object} node - The started node.
* @param {boolean|'nullable'} isStatement - Whether it is a declaration, and
* whether its name may be omitted.
* @returns {object} The class node.
*/
parseClass(node, isStatement) {
this.expectName('class');
const outerStrict = this.strict;
// A class body is always strict code.
this.strict = true;
if (this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value) && !this.isName('extends')) {
const namePos = this.tok.start;
node.id = this.parseIdentifier();
if (isStatement === true) {
// The name is lexical in the enclosing scope, but the class body is
// already strict, so the reserved-word check uses the outer strictness.
this.strict = outerStrict;
this.checkBindingName(node.id.name, 'lexical', namePos);
this.strict = true;
}
} else if (isStatement === true) {
this.raise('A class declaration needs a name');
} else {
node.id = null;
}
node.superClass = this.eatName('extends') ? this.parseExprSubscripts() : null;
node.body = this.parseClassBody(node.superClass !== null);
this.strict = outerStrict;
return this.finishNode(node, isStatement ? 'ClassDeclaration' : 'ClassExpression');
}
/**
* Parses a class body.
* @param {boolean} derived - Whether the class extends another.
* @returns {object} A ClassBody.
*/
parseClassBody(derived) {
const node = this.startNode();
this.expectPunct('{');
this.privateNameStack.push({ declared: new Set(), used: [], roles: new Map() });
node.body = [];
while (!this.eatPunct('}')) {
if (this.tok.type === 'eof') this.raise("Expected '}'");
if (this.eatPunct(';')) continue;
const element = this.parseClassElement(derived);
if (element.key && element.key.type === 'PrivateIdentifier') {
this.declarePrivateName(element);
}
node.body.push(element);
}
this.resolvePrivateNames();
return this.finishNode(node, 'ClassBody');
}
/**
* Records a use of a private name, to be resolved when the class body ends.
* @param {string} name - The name without its `#`.
* @param {number} pos - Where it is written.
*/
usePrivateName(name, pos) {
const frame = this.privateNameStack[this.privateNameStack.length - 1];
if (!frame) {
this.raise(`Private field '#${name}' must be declared in an enclosing class`, pos);
return;
}
frame.used.push({ name, pos });
}
/**
* Declares the private name a class element defines.
*
* A name may appear twice only as a getter and its setter, which is the one
* case where two elements describe one field.
* @param {object} element - The parsed class element.
*/
declarePrivateName(element) {
const frame = this.privateNameStack[this.privateNameStack.length - 1];
const name = element.key.name;
const role = element.kind === 'get' || element.kind === 'set' ? element.kind : 'field';
const previous = frame.roles ? frame.roles.get(name) : undefined;
if (!frame.roles) frame.roles = new Map();
const pairs = previous !== undefined && previous !== role && previous !== 'field' && role !== 'field';
if (previous !== undefined && !pairs) {
this.raise(`Identifier '#${name}' has already been declared`, element.key.start);
}
frame.roles.set(name, pairs ? 'field' : role);
frame.declared.add(name);
}
/**
* Resolves the private names used in a class body that has just ended.
*
* A name the body declares is resolved; anything left belongs to an
* enclosing class, so it is handed outwards. At the outermost class, an
* unresolved name is an error with its position.
*/
resolvePrivateNames() {
const frame = this.privateNameStack.pop();
const outer = this.privateNameStack[this.privateNameStack.length - 1];
for (const use of frame.used) {
if (frame.declared.has(use.name)) continue;
if (outer) outer.used.push(use);
else this.raise(`Private field '#${use.name}' must be declared in an enclosing class`, use.pos);
}
}
/**
* Parses one class element: a method, a field or a static block.
* @param {boolean} derived - Whether the class extends another.
* @returns {object} The element node.
*/
parseClassElement(derived) {
const node = this.startNode();
node.static = false;
if (this.isName('static')) {
const after = this.peek();
// `static` is a modifier unless what follows shows it is the member's
// own name: `static() {}`, `static = 1`, `static;` or `static}`.
const isModifier = !(
after.type === 'punct' &&
(after.value === '(' || after.value === '=' || after.value === ';' || after.value === '}')
);
if (after.type === 'punct' && after.value === '{') {
// A static initialisation block.
this.next();
node.body = [];
this.expectPunct('{');
const outer = this.enterFunction(false, false);
this.inFunction = false;
while (!this.eatPunct('}')) {
if (this.tok.type === 'eof') this.raise("Expected '}'");
node.body.push(this.parseStatement());
}
this.exitFunction(outer);
delete node.static;
return this.finishNode(node, 'StaticBlock');
}
if (isModifier) {
this.next();
node.static = true;
}
}
let isAsync = false;
let isGenerator = false;
let kind = 'method';
if (this.isName('async')) {
const after = this.peek();
const stops = after.type === 'punct' && (after.value === '(' || after.value === '=' || after.value === ';' || after.value === '}');
if (!stops && !after.nlBefore) {
this.next();
isAsync = true;
}
}
if (this.isPunct('*')) {
this.next();
isGenerator = true;
}
if ((this.isName('get') || this.isName('set')) && !isAsync && !isGenerator) {
const after = this.peek();
const stops = after.type === 'punct' && (after.value === '(' || after.value === '=' || after.value === ';' || after.value === '}');
if (!stops) {
kind = this.tok.value;
this.next();
}
}
this.parsePropertyName(node);
if (this.isPunct('(')) {
if (kind === 'method' && !node.computed && node.key.type === 'Identifier' && node.key.name === 'constructor') {
kind = 'constructor';
if (isAsync || isGenerator) this.raise('A constructor may not be async or a generator', node.key.start);
}
node.kind = kind;
node.value = this.parseMethod(isAsync, isGenerator, kind === 'constructor' && derived, kind);
return this.finishNode(node, 'MethodDefinition');
}
if (kind !== 'method' || isAsync || isGenerator) this.unexpected();
if (this.eatPunct('=')) {
const outer = this.enterFunction(false, false);
this.inFunction = false;
this.inMethod = true;
node.value = this.parseMaybeAssign();
this.exitFunction(outer);
} else {
node.value = null;
}
this.semicolon();
return this.finishNode(node, 'PropertyDefinition');
}
/**
* Parses a method's parameters and body.
* @param {boolean} isAsync - Whether it is async.
* @param {boolean} isGenerator - Whether it is a generator.
* @param {boolean} derivedConstructor - Whether `super()` is legal in it.
* @param {string} [kind] - `get` or `set` when it is an accessor, so its
* parameter count can be checked.
* @returns {object} A FunctionExpression.
*/
parseMethod(isAsync, isGenerator, derivedConstructor, kind = 'method') {
const node = this.startNode();
node.id = null;
node.expression = false;
node.generator = isGenerator;
node.async = isAsync;
node.params = this.parseParams();
if (kind === 'get' && node.params.length !== 0) {
this.raise('A getter takes no parameters', node.start);
}
if (kind === 'set') {
if (node.params.length !== 1) this.raise('A setter takes exactly one parameter', node.start);
if (node.params[0].type === 'RestElement') this.raise('A setter cannot take a rest parameter', node.params[0].start);
}
const outer = this.enterFunction(isAsync, isGenerator);
this.declareParams(node.params);
const outerMethod = this.inMethod;
const outerDerived = this.inDerivedConstructor;
this.inMethod = true;
this.inDerivedConstructor = !!derivedConstructor;
node.body = this.parseFunctionBody();
this.inMethod = outerMethod;
this.inDerivedConstructor = outerDerived;
this.exitFunction(outer);
return this.finishNode(node, 'FunctionExpression');
}
/**
* Reads a property name into `node.key` and `node.computed`.
* @param {object} node - The node to fill.
*/
parsePropertyName(node) {
if (this.eatPunct('[')) {
node.computed = true;
node.key = this.parseMaybeAssign();
this.expectPunct(']');
return;
}
node.computed = false;
if (this.tok.type === 'num' || this.tok.type === 'string' || this.tok.type === 'bigint') {
node.key = this.parseLiteral();
return;
}
if (this.tok.type === 'privatename') {
const key = this.startNode();
key.name = this.tok.value;
this.next();
node.key = this.finishNode(key, 'PrivateIdentifier');
return;
}
node.key = this.parseIdentifierName();
}
// -------------------------------------------------------------- expressions
/**
* Parses a comma-separated expression.
* @param {boolean} [noIn] - Whether `in` is an operator here.
* @returns {object} The expression.
*/
parseExpression(noIn = false) {
const start = this.tok.start;
const first = this.parseMaybeAssign(noIn);
if (!this.isPunct(',')) return first;
const node = this.startNodeAt(start);
node.expressions = [first];
while (this.eatPunct(',')) {
node.expressions.push(this.parseMaybeAssign(noIn));
}
return this.finishNode(node, 'SequenceExpression');
}
/**
* Parses an assignment, an arrow function, or anything tighter.
* @param {boolean} [noIn] - Whether `in` is an operator here.
* @returns {object} The expression.
*/
parseMaybeAssign(noIn = false) {
if (this.inGenerator && this.isName('yield')) return this.parseYield(noIn);
const arrow = this.tryParseArrow();
if (arrow) return arrow;
const start = this.tok.start;
const left = this.parseMaybeConditional(noIn);
if (this.tok.type === 'punct' && ASSIGN_OPERATORS.has(this.tok.value)) {
const node = this.startNodeAt(start);
node.operator = this.tok.value;
node.left = node.operator === '=' ? this.toAssignable(left, true) : this.checkSimpleTarget(left);
this.next();
node.right = this.parseMaybeAssign(noIn);
return this.finishNode(node, 'AssignmentExpression');
}
return left;
}
/**
* Checks that a compound assignment's target is a simple reference.
* @param {object} node - The parsed left side.
* @returns {object} The same node.
*/
checkSimpleTarget(node) {
if (node.type !== 'Identifier' && node.type !== 'MemberExpression') {
this.raise('Invalid assignment target', node.start);
}
return node;
}
/**
* Attempts to parse an arrow function at the current position.
*
* Three shapes start one: `x => …`, `async x => …` and `(…) => …`, with
* `async (…) => …`. Only the parenthesised forms need speculation, because
* `(a, b)` is a valid expression as well as a valid parameter list; the parse
* is attempted as parameters and re-wound when no `=>` follows.
* @returns {object|null} The arrow function, or null when there is none.
*/
tryParseArrow() {
// `x => …`
if (this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value) && this.tok.value !== 'async') {
const after = this.peek();
if (after.type === 'punct' && after.value === '=>' && !after.nlBefore) {
const node = this.startNode();
const param = this.parseIdentifier();
return this.parseArrowBody(node, [param], false);
}
return null;
}
// `async x => …` and `async (…) => …`
if (this.isName('async')) {
const after = this.peek();
if (after.nlBefore) return null;
if (after.type === 'name' && !RESERVED_WORDS.has(after.value)) {
const node = this.startNode();
const state = this.saveState();
this.next();
const param = this.parseIdentifier();
if (this.isPunct('=>') && !this.tok.nlBefore) {
return this.parseArrowBody(node, [param], true);
}
this.restoreState(state);
return null;
}
if (after.type === 'punct' && after.value === '(') {
return this.speculateParenArrow(true);
}
return null;
}
if (this.isPunct('(')) return this.speculateParenArrow(false);
return null;
}
/**
* Speculatively parses `(…) =>` as an arrow function.
* @param {boolean} isAsync - Whether `async` precedes the parentheses.
* @returns {object|null} The arrow function, or null.
*/
speculateParenArrow(isAsync) {
const state = this.saveState();
const node = this.startNode();
try {
if (isAsync) this.next();
const params = this.parseParams();
if (!this.isPunct('=>') || this.tok.nlBefore) throw BAIL;
return this.parseArrowBody(node, params, isAsync);
} catch (error) {
if (error !== BAIL && !(error instanceof JsParseError)) throw error;
this.restoreState(state);
return null;
}
}
/**
* Parses an arrow function's body, the parameters already in hand.
* @param {object} node - The started node.
* @param {object[]} params - The parameters.
* @param {boolean} isAsync - Whether it is async.
* @returns {object} An ArrowFunctionExpression.
*/
parseArrowBody(node, params, isAsync) {
this.expectPunct('=>');
node.id = null;
node.params = params;
node.async = isAsync;
node.generator = false;
const outer = this.enterFunction(isAsync, false);
this.declareParams(params);
// An arrow keeps the enclosing `new.target`, and has no `this` of its own,
// so the surrounding method context stays in force.
this.inNewTargetScope = outer.inNewTargetScope;
if (this.isPunct('{')) {
node.body = this.parseFunctionBody();
node.expression = false;
} else {
node.body = this.parseMaybeAssign();
node.expression = true;
}
this.exitFunction(outer);
return this.finishNode(node, 'ArrowFunctionExpression');
}
/**
* Parses `yield` and `yield*`.
* @param {boolean} noIn - Whether `in` is an operator here.
* @returns {object} A YieldExpression.
*/
parseYield(noIn) {
const node = this.startNode();
this.next();
if (this.isPunct('*')) {
this.next();
node.delegate = true;
node.argument = this.parseMaybeAssign(noIn);
} else {
node.delegate = false;
const ends = this.canInsertSemicolon() ||
(this.tok.type === 'punct' && [')', ']', '}', ',', ';', ':'].includes(this.tok.value));
node.argument = ends ? null : this.parseMaybeAssign(noIn);
}
return this.finishNode(node, 'YieldExpression');
}
/**
* Parses a conditional expression.
* @param {boolean} noIn - Whether `in` is an operator here.
* @returns {object} The expression.
*/
parseMaybeConditional(noIn) {
const start = this.tok.start;
const test = this.parseExprOps(noIn);
if (!this.isPunct('?')) return test;
const node = this.startNodeAt(start);
this.next();
node.test = test;
node.consequent = this.parseMaybeAssign();
this.expectPunct(':');
node.alternate = this.parseMaybeAssign(noIn);
return this.finishNode(node, 'ConditionalExpression');
}
/**
* Parses a run of binary and logical operators.
* @param {boolean} noIn - Whether `in` is an operator here.
* @returns {object} The expression.
*/
parseExprOps(noIn) {
const start = this.tok.start;
const left = this.parseMaybeUnary();
return this.parseExprOp(left, start, 0, noIn);
}
/**
* The precedence of the current token as a binary operator.
* @param {boolean} noIn - Whether `in` is an operator here.
* @returns {{operator: string, precedence: number}|null} The operator, or null.
*/
currentBinaryOperator(noIn) {
if (this.tok.type === 'punct') {
const precedence = BINARY_PRECEDENCE[this.tok.value];
if (precedence) return { operator: this.tok.value, precedence };
return null;
}
if (this.tok.type === 'name') {
if (this.tok.value === 'instanceof') return { operator: 'instanceof', precedence: 8 };
if (this.tok.value === 'in' && !noIn) return { operator: 'in', precedence: 8 };
}
return null;
}
/**
* Climbs precedence from a parsed left operand.
* @param {object} left - The left operand.
* @param {number} start - Where the expression began.
* @param {number} minPrecedence - The lowest precedence to bind here.
* @param {boolean} noIn - Whether `in` is an operator here.
* @returns {object} The expression.
*/
parseExprOp(left, start, minPrecedence, noIn) {
for (;;) {
const op = this.currentBinaryOperator(noIn);
if (!op || op.precedence <= minPrecedence) return left;
const isLogical = op.operator === '||' || op.operator === '&&' || op.operator === '??';
if (op.operator === '??' && left.type === 'LogicalExpression' && left.operator !== '??' && !left.parenthesised) {
this.raise("'??' cannot be mixed with '||' or '&&' without parentheses");
}
if (
(op.operator === '||' || op.operator === '&&') &&
left.type === 'LogicalExpression' &&
left.operator === '??' &&
!left.parenthesised
) {
this.raise("'??' cannot be mixed with '||' or '&&' without parentheses");
}
this.next();
const rightStart = this.tok.start;
let right = this.parseMaybeUnary();
// `**` is right-associative, so it binds its own precedence again.
right = this.parseExprOp(right, rightStart, op.operator === '**' ? op.precedence - 1 : op.precedence, noIn);
// `a ?? b || c` is an error rather than a grouping, and `||` binds
// tighter, so the offending operand arrives here as the right side.
if (
op.operator === '??' &&
right.type === 'LogicalExpression' &&
right.operator !== '??' &&
!right.parenthesised
) {
this.raise("'??' cannot be mixed with '||' or '&&' without parentheses", right.start);
}
const node = this.startNodeAt(start);
node.left = left;
node.operator = op.operator;
node.right = right;
left = this.finishNode(node, isLogical ? 'LogicalExpression' : 'BinaryExpression');
}
}
/**
* Parses a unary or update expression, or anything tighter.
* @returns {object} The expression.
*/
parseMaybeUnary() {
const start = this.tok.start;
if (this.isName('await') && this.inAsync) {
const node = this.startNode();
this.next();
node.argument = this.parseMaybeUnary();
const finished = this.finishNode(node, 'AwaitExpression');
if (this.isPunct('**')) this.raise('Unary operand cannot be the left side of **');
return finished;
}
if (this.tok.type === 'punct' && (UNARY_OPERATORS.has(this.tok.value) || this.tok.value === '++' || this.tok.value === '--')) {
const isUpdate = this.tok.value === '++' || this.tok.value === '--';
const node = this.startNode();
node.operator = this.tok.value;
node.prefix = true;
this.next();
node.argument = this.parseMaybeUnary();
if (isUpdate) {
this.checkSimpleTarget(node.argument);
return this.finishNode(node, 'UpdateExpression');
}
const finished = this.finishNode(node, 'UnaryExpression');
if (this.isPunct('**')) this.raise('Unary operand cannot be the left side of **');
return finished;
}
if (this.tok.type === 'name' && (this.tok.value === 'typeof' || this.tok.value === 'void' || this.tok.value === 'delete')) {
const node = this.startNode();
node.operator = this.tok.value;
node.prefix = true;
this.next();
node.argument = this.parseMaybeUnary();
if (node.operator === 'delete' && this.strict && node.argument.type === 'Identifier') {
this.raise('Deleting a local variable is not allowed in strict mode', node.start);
}
const finished = this.finishNode(node, 'UnaryExpression');
if (this.isPunct('**')) this.raise('Unary operand cannot be the left side of **');
return finished;
}
let expression = this.parseExprSubscripts();
while (
this.tok.type === 'punct' &&
(this.tok.value === '++' || this.tok.value === '--') &&
!this.tok.nlBefore
) {
const node = this.startNodeAt(start);
node.operator = this.tok.value;
node.prefix = false;
node.argument = this.checkSimpleTarget(expression);
this.next();
expression = this.finishNode(node, 'UpdateExpression');
}
return expression;
}
/**
* Parses an atom and every subscript applied to it.
* @returns {object} The expression.
*/
parseExprSubscripts() {
const start = this.tok.start;
const base = this.parseExprAtom();
return this.parseSubscripts(base, start, false);
}
/**
* Applies member accesses, calls and tagged templates to a base expression.
* @param {object} base - The base expression.
* @param {number} start - Where the whole expression began.
* @param {boolean} noCalls - Whether a call would belong to an enclosing `new`.
* @returns {object} The expression, wrapped in a ChainExpression when any
* link was optional.
*/
parseSubscripts(base, start, noCalls) {
let optionalChained = false;
let current = base;
for (;;) {
if (this.isPunct('.')) {
this.next();
const node = this.startNodeAt(start);
node.object = current;
node.property = this.parseMemberProperty();
node.computed = false;
node.optional = false;
current = this.finishNode(node, 'MemberExpression');
continue;
}
if (this.isPunct('[')) {
this.next();
const node = this.startNodeAt(start);
node.object = current;
node.property = this.parseExpression();
node.computed = true;
node.optional = false;
this.expectPunct(']');
current = this.finishNode(node, 'MemberExpression');
continue;
}
if (this.isPunct('(') && !noCalls) {
const node = this.startNodeAt(start);
node.callee = current;
node.arguments = this.parseArguments();
node.optional = false;
current = this.finishNode(node, 'CallExpression');
continue;
}
if (this.isPunct('?.')) {
// `new a?.b()` is an error, whatever follows the `?.`: an optional
// chain can never be the callee of a `new` expression.
if (noCalls) this.raise('Optional chaining cannot appear in the callee of new expressions');
optionalChained = true;
this.next();
if (this.isPunct('(')) {
const node = this.startNodeAt(start);
node.callee = current;
node.arguments = this.parseArguments();
node.optional = true;
current = this.finishNode(node, 'CallExpression');
continue;
}
const node = this.startNodeAt(start);
node.object = current;
if (this.eatPunct('[')) {
node.property = this.parseExpression();
node.computed = true;
this.expectPunct(']');
} else {
node.property = this.parseMemberProperty();
node.computed = false;
}
node.optional = true;
current = this.finishNode(node, 'MemberExpression');
continue;
}
if (this.tok.type === 'template') {
if (optionalChained) this.raise('A tagged template cannot appear in an optional chain');
const node = this.startNodeAt(start);
node.tag = current;
node.quasi = this.parseTemplate();
current = this.finishNode(node, 'TaggedTemplateExpression');
continue;
}
break;
}
if (optionalChained) {
const node = this.startNodeAt(start);
node.expression = current;
return this.finishNode(node, 'ChainExpression');
}
return current;
}
/**
* Parses the property after a `.`, which may be a private name.
* @returns {object} An Identifier or PrivateIdentifier.
*/
parseMemberProperty() {
if (this.tok.type === 'privatename') {
const node = this.startNode();
node.name = this.tok.value;
this.usePrivateName(node.name, node.start);
this.next();
return this.finishNode(node, 'PrivateIdentifier');
}
return this.parseIdentifierName();
}
/**
* Parses a call's argument list.
* @returns {object[]} The arguments.
*/
parseArguments() {
this.expectPunct('(');
const args = [];
let first = true;
while (!this.eatPunct(')')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct(')')) break;
if (this.isPunct('...')) {
const spread = this.startNode();
this.next();
spread.argument = this.parseMaybeAssign();
args.push(this.finishNode(spread, 'SpreadElement'));
} else {
args.push(this.parseMaybeAssign());
}
}
return args;
}
/**
* Parses a primary expression.
* @returns {object} The expression.
*/
parseExprAtom() {
switch (this.tok.type) {
case 'num':
case 'bigint':
case 'string':
return this.parseLiteral();
case 'regexp':
return this.parseLiteral();
case 'template':
return this.parseTemplate();
case 'privatename': {
// `#field in obj` is the only place a private name stands alone.
const node = this.startNode();
node.name = this.tok.value;
this.usePrivateName(node.name, node.start);
this.next();
if (!this.isName('in')) this.raise('A private name is only allowed in a member access or `in`', node.start);
return this.finishNode(node, 'PrivateIdentifier');
}
default:
break;
}
if (this.tok.type === 'punct') {
switch (this.tok.value) {
case '(': {
return this.parseParenExpressionAtom();
}
case '[': {
const node = this.startNode();
this.next();
node.elements = this.parseExpressionList(']', true);
return this.finishNode(node, 'ArrayExpression');
}
case '{':
return this.parseObject();
case '/':
case '/=': {
this.tz.reScanAsRegExp();
this.tok = this.tz.curr;
return this.parseLiteral();
}
default:
return this.unexpected();
}
}
if (this.tok.type !== 'name') return this.unexpected();
switch (this.tok.value) {
case 'this': {
const node = this.startNode();
this.next();
return this.finishNode(node, 'ThisExpression');
}
case 'super': {
const node = this.startNode();
if (!this.inMethod) this.raise("'super' is only allowed in a method");
this.next();
if (this.isPunct('(') && !this.inDerivedConstructor && !this.options.allowSuperOutsideMethod) {
this.raise("'super' call is only allowed in a derived constructor", node.start);
}
if (!this.isPunct('(') && !this.isPunct('.') && !this.isPunct('[')) {
this.raise("'super' must be called or accessed", node.start);
}
return this.finishNode(node, 'Super');
}
case 'null':
case 'true':
case 'false': {
const node = this.startNode();
node.value = this.tok.value === 'null' ? null : this.tok.value === 'true';
node.raw = this.tok.value;
this.next();
return this.finishNode(node, 'Literal');
}
case 'function': {
const node = this.startNode();
this.next();
const isGenerator = this.eatPunct('*');
node.id = this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value) ? this.parseIdentifier() : null;
return this.parseFunctionRest(node, 'FunctionExpression', false, isGenerator);
}
case 'class':
return this.parseClass(this.startNode(), false);
case 'new':
return this.parseNew();
case 'import':
return this.parseImportExpression();
case 'async': {
const after = this.peek();
if (after.type === 'name' && after.value === 'function' && !after.nlBefore) {
const node = this.startNode();
this.next();
this.next();
const isGenerator = this.eatPunct('*');
node.id = this.tok.type === 'name' && !RESERVED_WORDS.has(this.tok.value) ? this.parseIdentifier() : null;
return this.parseFunctionRest(node, 'FunctionExpression', true, isGenerator);
}
return this.parseIdentifier();
}
default:
if (RESERVED_WORDS.has(this.tok.value)) return this.unexpected();
return this.parseIdentifier();
}
}
/**
* Parses a parenthesised expression.
*
* Parentheses are not kept as nodes, matching ESTree, but the fact that an
* expression was parenthesised is remembered on it: `(a ?? b) || c` is legal
* where `a ?? b || c` is not.
* @returns {object} The inner expression.
*/
parseParenExpressionAtom() {
this.next();
if (this.isPunct(')')) this.raise('Expected an expression');
const innerStart = this.tok.start;
const expressions = [];
let first = true;
while (!this.isPunct(')')) {
if (first) first = false;
else this.expectPunct(',');
if (this.isPunct('...')) this.raise('A rest element needs an arrow function');
expressions.push(this.parseMaybeAssign());
}
const innerEnd = this.tz.lastEnd;
this.expectPunct(')');
if (expressions.length === 0) this.raise('Expected an expression', innerStart);
let value;
if (expressions.length > 1) {
const node = this.startNodeAt(innerStart);
node.expressions = expressions;
value = this.finishNodeAt(node, 'SequenceExpression', innerEnd);
} else {
value = expressions[0];
}
// Not enumerable, so a consumer walking own keys — the CSP scan, the action
// analyser — never sees it, and the differential test compares equal.
Object.defineProperty(value, 'parenthesised', { value: true, enumerable: false, configurable: true });
return value;
}
/**
* Parses a comma-separated list up to a closing punctuator.
* @param {string} close - The closing punctuator.
* @param {boolean} allowHoles - Whether elisions produce null elements.
* @returns {Array<object|null>} The elements.
*/
parseExpressionList(close, allowHoles) {
const elements = [];
let first = true;
while (!this.eatPunct(close)) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct(close)) break;
if (allowHoles && this.isPunct(',')) {
elements.push(null);
continue;
}
if (this.isPunct('...')) {
const spread = this.startNode();
this.next();
spread.argument = this.parseMaybeAssign();
elements.push(this.finishNode(spread, 'SpreadElement'));
continue;
}
elements.push(this.parseMaybeAssign());
}
return elements;
}
/**
* Parses an object literal.
* @returns {object} An ObjectExpression.
*/
parseObject() {
const node = this.startNode();
this.expectPunct('{');
node.properties = [];
let first = true;
while (!this.eatPunct('}')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct('}')) break;
node.properties.push(this.parseObjectMember());
}
return this.finishNode(node, 'ObjectExpression');
}
/**
* Parses one object literal member.
* @returns {object} A Property or SpreadElement.
*/
parseObjectMember() {
if (this.isPunct('...')) {
const spread = this.startNode();
this.next();
spread.argument = this.parseMaybeAssign();
return this.finishNode(spread, 'SpreadElement');
}
const node = this.startNode();
let isAsync = false;
let isGenerator = false;
if (this.isName('async')) {
const after = this.peek();
const stops = after.type === 'punct' && (after.value === ':' || after.value === '(' || after.value === ',' || after.value === '}' || after.value === '=');
if (!stops && !after.nlBefore) {
this.next();
isAsync = true;
}
}
if (this.isPunct('*')) {
this.next();
isGenerator = true;
}
if ((this.isName('get') || this.isName('set')) && !isAsync && !isGenerator) {
const after = this.peek();
const stops = after.type === 'punct' && (after.value === ':' || after.value === '(' || after.value === ',' || after.value === '}' || after.value === '=');
if (!stops) {
const kind = this.tok.value;
this.next();
this.parsePropertyName(node);
node.kind = kind;
node.method = false;
node.shorthand = false;
node.value = this.parseMethod(false, false, false, kind);
return this.finishNode(node, 'Property');
}
}
this.parsePropertyName(node);
if (this.isPunct('(')) {
node.kind = 'init';
node.method = true;
node.shorthand = false;
node.value = this.parseMethod(isAsync, isGenerator, false);
return this.finishNode(node, 'Property');
}
if (isAsync || isGenerator) this.unexpected();
node.kind = 'init';
node.method = false;
if (this.eatPunct(':')) {
node.shorthand = false;
node.value = this.parseMaybeAssign();
return this.finishNode(node, 'Property');
}
if (node.computed || node.key.type !== 'Identifier') this.unexpected();
if (RESERVED_WORDS.has(node.key.name)) this.raise(`'${node.key.name}' cannot be a shorthand property`, node.key.start);
this.checkReference(node.key.name, node.key.start);
node.shorthand = true;
if (this.isPunct('=')) {
// `{ a = 1 }` is only legal as a destructuring pattern. It is parsed here
// and rejected by toAssignable if the object never becomes one.
const assignment = this.startNodeAt(node.key.start);
assignment.operator = '=';
assignment.left = this.cloneIdentifier(node.key);
this.next();
assignment.right = this.parseMaybeAssign();
node.value = this.finishNode(assignment, 'AssignmentExpression');
const pending = { start: node.key.start };
this.pendingShorthandDefaults.push(pending);
// Not enumerable, so the node's shape stays exactly ESTree's.
Object.defineProperty(node, 'shorthandDefault', { value: pending, enumerable: false, configurable: true });
} else {
node.value = this.cloneIdentifier(node.key);
}
return this.finishNode(node, 'Property');
}
/**
* Copies an identifier node, so a shorthand property's key and value are
* distinct nodes with the same range, as acorn produces them.
* @param {object} node - The identifier.
* @returns {object} A copy.
*/
cloneIdentifier(node) {
/** @type {any} */
const copy = { type: 'Identifier', start: node.start, end: node.end, name: node.name };
if (this.locations) copy.loc = { start: { ...node.loc.start }, end: { ...node.loc.end } };
return copy;
}
/**
* Parses `new X(…)`, `new X` and `new.target`.
* @returns {object} A NewExpression or MetaProperty.
*/
parseNew() {
const node = this.startNode();
this.next();
if (this.isPunct('.')) {
this.next();
const property = this.parseIdentifierName();
if (property.name !== 'target') this.raise("The only valid meta property for new is 'new.target'", property.start);
if (!this.inNewTargetScope) this.raise("'new.target' can only be used in functions and class static block", node.start);
node.meta = this.metaIdentifier(node.start, 'new');
node.property = property;
return this.finishNode(node, 'MetaProperty');
}
const calleeStart = this.tok.start;
node.callee = this.parseSubscripts(this.parseExprAtom(), calleeStart, true);
node.arguments = this.isPunct('(') ? this.parseArguments() : [];
return this.finishNode(node, 'NewExpression');
}
/**
* Builds the identifier half of a meta property.
* @param {number} start - Where the keyword begins.
* @param {string} name - The keyword.
* @returns {object} An Identifier.
*/
metaIdentifier(start, name) {
/** @type {any} */
const node = { type: 'Identifier', start, end: start + name.length, name };
if (this.locations) {
node.loc = { start: this.tz.locate(start), end: this.tz.locate(start + name.length) };
}
return node;
}
/**
* Parses `import(…)` and `import.meta`.
* @returns {object} An ImportExpression or MetaProperty.
*/
parseImportExpression() {
const node = this.startNode();
this.next();
if (this.isPunct('.')) {
this.next();
const property = this.parseIdentifierName();
if (property.name !== 'meta') this.raise("The only valid meta property for import is 'import.meta'", property.start);
if (this.sourceType !== 'module' && !this.options.allowImportExpressionEverywhere) {
this.raise("'import.meta' is only allowed in a module", node.start);
}
node.meta = this.metaIdentifier(node.start, 'import');
node.property = property;
return this.finishNode(node, 'MetaProperty');
}
if (!this.isPunct('(')) this.unexpected();
this.next();
node.source = this.parseMaybeAssign();
node.options = this.eatPunct(',') && !this.isPunct(')') ? this.parseMaybeAssign() : null;
this.eatPunct(',');
this.expectPunct(')');
return this.finishNode(node, 'ImportExpression');
}
/**
* Parses a literal token into a Literal node.
* @returns {object} A Literal.
*/
parseLiteral() {
const node = this.startNode();
const token = this.tok;
node.value = token.type === 'bigint' ? this.bigIntValue(token.value) : token.value;
node.raw = token.raw !== undefined ? token.raw : this.input.slice(token.start, token.end);
// `0755` and `09` are legacy octal or leading-zero decimal literals, which
// strict code forbids. The prefixed forms (`0x`, `0o`, `0b`) never match.
if (token.type === 'num' && this.strict && /^0[0-9]/.test(node.raw)) {
this.raise('Legacy octal literals are not allowed in strict mode', token.start);
}
if (token.type === 'regexp') {
node.value = this.regExpValue(token.value);
node.regex = { pattern: token.value.pattern, flags: token.value.flags };
} else if (token.type === 'bigint') {
// The written digits may be hexadecimal; the node carries the value.
node.bigint = node.value === null ? token.value : String(node.value);
}
this.next();
return this.finishNode(node, 'Literal');
}
/**
* Builds a BigInt for a literal, when the host supports it.
* @param {string} digits - The digits without the `n`.
* @returns {bigint|null} The value, or null.
*/
bigIntValue(digits) {
try {
return BigInt(digits);
} catch {
return null;
}
}
/**
* Builds a RegExp for a literal, or null when the host rejects the pattern.
*
* A pattern this engine cannot compile is still a syntactically valid literal
* in source the compiler only has to copy through, so the node is produced
* with a null value rather than refused.
* @param {{pattern: string, flags: string}} regex - The literal's parts.
* @returns {RegExp|null} The value.
*/
regExpValue(regex) {
try {
return new RegExp(regex.pattern, regex.flags);
} catch {
return null;
}
}
/**
* Parses a template literal, including its substitutions.
* @returns {object} A TemplateLiteral.
*/
parseTemplate() {
const node = this.startNode();
node.quasis = [];
node.expressions = [];
let chunk = this.tok.value;
node.quasis.push(this.templateElement(chunk));
while (!chunk.tail) {
this.next();
node.expressions.push(this.parseExpression());
if (!this.isPunct('}')) this.raise("Expected '}' to close a template substitution");
this.tz.reScanTemplateContinue();
this.tok = this.tz.curr;
chunk = this.tok.value;
node.quasis.push(this.templateElement(chunk));
}
this.next();
return this.finishNode(node, 'TemplateLiteral');
}
/**
* Builds one TemplateElement.
* @param {{cooked: (string|null), raw: string, tail: boolean, rawStart: number,
* rawEnd: number}} chunk - The lexed chunk.
* @returns {object} A TemplateElement.
*/
templateElement(chunk) {
/** @type {any} */
const node = {
type: 'TemplateElement',
start: chunk.rawStart,
end: chunk.rawEnd,
value: { raw: chunk.raw, cooked: chunk.cooked },
tail: chunk.tail,
};
if (this.locations) {
node.loc = { start: this.tz.locate(chunk.rawStart), end: this.tz.locate(chunk.rawEnd) };
}
return node;
}
/**
* Parses an identifier that must not be a reserved word.
* @returns {object} An Identifier.
*/
parseIdentifier() {
if (this.tok.type !== 'name') this.unexpected();
if (RESERVED_WORDS.has(this.tok.value)) this.unexpected();
this.checkReference(this.tok.value, this.tok.start);
return this.parseIdentifierName();
}
/**
* Refuses a word that cannot name a variable in the current mode.
*
* Strict code reserves nine further words, and a module is always strict, so
* `let`, `static` and `yield` are references only in sloppy scripts. A
* property name is not a reference and goes through
* {@link Parser#parseIdentifierName}, which does not check: `obj.let` and
* `{ let: 1 }` are fine everywhere.
* @param {string} name - The word.
* @param {number} pos - Where it is written.
*/
checkReference(name, pos) {
if (this.strict && STRICT_RESERVED.has(name)) {
this.raise(`The keyword '${name}' is reserved`, pos);
}
if (name === 'await' && (this.inAsync || this.sourceType === 'module')) {
this.raise("'await' is reserved here", pos);
}
if (name === 'yield' && this.inGenerator) {
this.raise("'yield' is reserved in a generator", pos);
}
}
/**
* Parses any word as an identifier, reserved or not. Used for property names.
* @returns {object} An Identifier.
*/
parseIdentifierName() {
if (this.tok.type !== 'name') this.unexpected();
const node = this.startNode();
node.name = this.tok.value;
this.next();
return this.finishNode(node, 'Identifier');
}
// ----------------------------------------------------------------- patterns
/**
* Validates and records a declared name.
* @param {string} name - The name.
* @param {string} kind - How it is declared; `'none'` only validates.
* @param {number} pos - Where it is written.
*/
checkBindingName(name, kind, pos) {
if (this.strict && STRICT_RESERVED.has(name)) {
this.raise(`The keyword '${name}' is reserved`, pos);
}
if (this.strict && (name === 'eval' || name === 'arguments')) {
this.raise(`Binding ${name} in strict mode`, pos);
}
if (name === 'let' && kind === 'lexical') {
this.raise("'let' is disallowed as a lexically bound name", pos);
}
if (name === 'yield' && this.inGenerator) {
this.raise("'yield' cannot be a binding name in a generator", pos);
}
if (name === 'await' && (this.inAsync || this.sourceType === 'module')) {
this.raise("'await' cannot be a binding name here", pos);
}
if (kind !== 'none') this.declareName(name, kind, pos);
}
/**
* Parses an identifier in a binding position.
* @param {string} kind - How it is being declared.
* @returns {object} An Identifier.
*/
parseBindingIdentifier(kind) {
const pos = this.tok.start;
const name = this.tok.type === 'name' ? this.tok.value : '';
const node = this.parseIdentifier();
this.checkBindingName(name, kind, pos);
return node;
}
/**
* Parses a binding target: an identifier or a destructuring pattern.
* @param {string} [kind] - How the names in it are being declared.
* @returns {object} The pattern.
*/
parseBindingAtom(kind = 'none') {
if (this.isPunct('[')) {
const node = this.startNode();
this.next();
node.elements = this.parseBindingList(']', kind);
return this.finishNode(node, 'ArrayPattern');
}
if (this.isPunct('{')) {
const node = this.startNode();
this.next();
node.properties = this.parseObjectPatternProperties(kind);
return this.finishNode(node, 'ObjectPattern');
}
return this.parseBindingIdentifier(kind);
}
/**
* Parses an array pattern's elements.
* @param {string} close - The closing punctuator.
* @param {string} [kind] - How the names in it are being declared.
* @returns {Array<object|null>} The elements.
*/
parseBindingList(close, kind = 'none') {
const elements = [];
let first = true;
while (!this.eatPunct(close)) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct(close)) break;
if (this.isPunct(',')) {
elements.push(null);
continue;
}
if (this.isPunct('...')) {
const rest = this.startNode();
this.next();
rest.argument = this.parseBindingAtom(kind);
elements.push(this.finishNode(rest, 'RestElement'));
this.expectPunct(close);
return elements;
}
elements.push(this.parseMaybeDefault(kind));
}
return elements;
}
/**
* Parses an object pattern's properties.
* @param {string} [kind] - How the names in it are being declared.
* @returns {object[]} The properties.
*/
parseObjectPatternProperties(kind = 'none') {
const properties = [];
let first = true;
while (!this.eatPunct('}')) {
if (first) first = false;
else this.expectPunct(',');
if (this.eatPunct('}')) break;
if (this.isPunct('...')) {
const rest = this.startNode();
this.next();
rest.argument = this.parseBindingIdentifier(kind);
properties.push(this.finishNode(rest, 'RestElement'));
this.expectPunct('}');
return properties;
}
const node = this.startNode();
this.parsePropertyName(node);
node.kind = 'init';
node.method = false;
if (this.eatPunct(':')) {
node.shorthand = false;
node.value = this.parseMaybeDefault(kind);
} else {
if (node.computed || node.key.type !== 'Identifier') this.unexpected();
if (RESERVED_WORDS.has(node.key.name)) this.raise(`'${node.key.name}' cannot be a binding name`, node.key.start);
node.shorthand = true;
this.checkReference(node.key.name, node.key.start);
this.checkBindingName(node.key.name, kind, node.key.start);
if (this.isPunct('=')) {
const assignment = this.startNodeAt(node.key.start);
assignment.left = this.cloneIdentifier(node.key);
this.next();
assignment.right = this.parseMaybeAssign();
node.value = this.finishNode(assignment, 'AssignmentPattern');
} else {
node.value = this.cloneIdentifier(node.key);
}
}
properties.push(this.finishNode(node, 'Property'));
}
return properties;
}
/**
* Parses a binding with an optional default value.
* @param {string} [kind] - How the names in it are being declared.
* @returns {object} The pattern.
*/
parseMaybeDefault(kind = 'none') {
const start = this.tok.start;
const left = this.parseBindingAtom(kind);
if (!this.isPunct('=')) return left;
const node = this.startNodeAt(start);
this.next();
node.left = left;
node.right = this.parseMaybeAssign();
return this.finishNode(node, 'AssignmentPattern');
}
/**
* Re-reads an expression as an assignment target.
*
* `[a, b] = c` is parsed as an ArrayExpression and only then known to be a
* pattern, which is the one place ECMAScript genuinely needs this. The
* conversion is in place, so the node's range is unchanged.
* @param {object} node - The parsed expression.
* @param {boolean} top - Whether this is the outermost target.
* @returns {object} The pattern.
*/
toAssignable(node, top = false) {
switch (node.type) {
case 'Identifier':
case 'MemberExpression':
if (node.type === 'MemberExpression' && node.optional) {
this.raise('An optional member access is not a valid assignment target', node.start);
}
return node;
case 'ObjectPattern':
case 'ArrayPattern':
case 'AssignmentPattern':
case 'RestElement':
return node;
case 'ObjectExpression': {
node.type = 'ObjectPattern';
const properties = node.properties;
for (let i = 0; i < properties.length; i += 1) {
const property = properties[i];
if (property.type === 'SpreadElement') {
if (i !== properties.length - 1) this.raise('A rest element must be last', property.start);
property.type = 'RestElement';
if (property.argument.type !== 'Identifier' && property.argument.type !== 'MemberExpression') {
this.raise('An object rest target must be a simple reference', property.argument.start);
}
continue;
}
if (property.kind !== 'init' || property.method) {
this.raise('A method is not a valid assignment target', property.start);
}
if (property.shorthandDefault) {
const index = this.pendingShorthandDefaults.indexOf(property.shorthandDefault);
if (index !== -1) this.pendingShorthandDefaults.splice(index, 1);
delete property.shorthandDefault;
}
property.value = this.toAssignable(property.value);
}
return node;
}
case 'ArrayExpression': {
node.type = 'ArrayPattern';
const elements = node.elements;
for (let i = 0; i < elements.length; i += 1) {
const element = elements[i];
if (element === null) continue;
if (element.type === 'SpreadElement') {
if (i !== elements.length - 1) this.raise('A rest element must be last', element.start);
element.type = 'RestElement';
element.argument = this.toAssignable(element.argument);
continue;
}
elements[i] = this.toAssignable(element);
}
return node;
}
case 'AssignmentExpression': {
if (node.operator !== '=') this.raise('Invalid assignment target', node.start);
node.type = 'AssignmentPattern';
delete node.operator;
node.left = this.toAssignable(node.left);
return node;
}
default:
if (top) this.raise('Invalid assignment target', node.start);
this.raise('Invalid destructuring target', node.start);
return node;
}
}
}
/**
* Parses a complete program.
* @param {string} source - The source text.
* @param {object} [options] - Parse options. `sourceType` (`'script'` by
* default), `locations`, `allowReturnOutsideFunction`,
* `allowAwaitOutsideFunction`, `allowSuperOutsideMethod`,
* `allowNewTargetOutsideFunction` and `allowHashBang` behave as acorn's do.
* `ecmaVersion` is accepted and ignored: this parser implements one version
* of the language, the current one.
* @returns {object} An ESTree Program node.
* @throws {JsParseError} When the source is not valid JavaScript.
*/
export function parse(source, options = {}) {
if (typeof source !== 'string') throw new TypeError('parse() expects a source string');
const parser = new Parser(source, options);
return runGuarded(parser, () => parser.parseTopLevel());
}
/**
* Runs a parse, turning a stack overflow into an ordinary parse error.
*
* The parser is recursive, so source nested a few thousand levels deep — which
* only ever arrives from a generator — exhausts the stack. A `RangeError`
* escaping into the build would surface as an internal crash rather than as a
* diagnostic, so it is reported like any other thing the parser cannot read:
* with a position, and as a reason the build can print.
* @template T
* @param {Parser} parser - The parser, for its position.
* @param {() => T} run - The parse to attempt.
* @returns {T} Whatever the parse produced.
* @throws {JsParseError} When the source nests too deeply to parse.
*/
function runGuarded(parser, run) {
try {
return run();
} catch (error) {
if (error instanceof RangeError) {
parser.raise('Not enough stack space to parse input');
}
throw error;
}
}
/**
* Parses a single expression starting at an offset.
*
* Stops as soon as the expression ends, leaving whatever follows unread, which
* is how the declaration reader tells `{ a: 1 }` from `{ a: 1 } and more`: it
* compares the node's `end` against the length of what it handed in.
* @param {string} source - The source text.
* @param {number} [offset] - Where to start.
* @param {object} [options] - Parse options, as {@link parse} takes them.
* @returns {object} The expression node.
* @throws {JsParseError} When no expression can be read there.
*/
export function parseExpressionAt(source, offset = 0, options = {}) {
if (typeof source !== 'string') throw new TypeError('parseExpressionAt() expects a source string');
const parser = new Parser(source, { ...options, allowHashBang: false });
parser.enterScope(SCOPE_VAR);
parser.tz.pos = offset;
parser.tok = parser.tz.nextToken();
return runGuarded(parser, () => {
const expression = parser.parseExpression();
parser.flushShorthandDefaults();
return expression;
});
}
export { JsParseError };
|