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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif
extension TokenConsumer {
mutating func atStartOfExpression() -> Bool {
switch self.at(anyIn: ExpressionStart.self) {
case (.awaitTryMove, let handle)?:
var lookahead = self.lookahead()
lookahead.eat(handle)
// These can be parsed as expressions with try/await.
if lookahead.at(anyIn: SingleValueStatementExpression.self) != nil {
return true
}
// Note we currently pass `preferExpr: false` to prefer diagnosing `try then`
// as needing to be `then try`, rather than parsing `then` as an expression.
if lookahead.atStartOfDeclaration() || lookahead.atStartOfStatement(preferExpr: false) {
// If after the 'try' we are at a declaration or statement, it can't be a valid expression.
// Decide how we want to consume the 'try':
// If the declaration or statement starts at a new line, the user probably just forgot to write the expression after 'try' -> parse it as a TryExpr
// If the declaration or statement starts at the same line, the user maybe tried to use 'try' as a modifier -> parse it as unexpected text in front of that decl or stmt.
return lookahead.atStartOfLine
} else {
return true
}
case (.primaryExpressionStart(.atSign), _)?:
break
case (_, _)?:
return true
case nil:
break
}
if self.at(.atSign) || self.at(.keyword(.inout)) {
var lookahead = self.lookahead()
if lookahead.canParseType() {
return true
}
}
// 'repeat' is the start of a pack expansion expression.
if (self.at(.keyword(.repeat))) {
// FIXME: 'repeat' followed by '{' could still be a pack
// expansion, but we need to do more lookahead to figure out
// whether the '{' is the start of a closure expression or a
// brace statement for 'repeat { ... } while'
let lookahead = self.lookahead()
return lookahead.peek().rawTokenKind != .leftBrace
}
return false
}
}
extension Parser {
enum ExprFlavor {
/// Parsing a normal expression, eg. inside a function calls.
case basic
/// Parsing the condition of a `if`/`guard`/`for`/... statement or something
/// like the `where` clause after a `catch` clause.
///
/// In these cases we need to disambiguate trailing closures from the
/// statement's body.
case stmtCondition
/// Parsing the condition of a `#if` directive.
///
/// We don't allow allow newlines here.
case poundIfDirective
/// Parsing an attribute's arguments, which can contain declaration
/// references like `subscript` or `deinit`.
case attributeArguments
}
enum PatternContext {
/// There is no ambient pattern context.
case none
/// We're parsing a matching pattern that is not introduced via `let` or `var`.
///
/// In this context, identifiers are references to the enclosing scopes, not a variable binding.
///
/// ```
/// case x.y <- 'x' must refer to some 'x' defined in another scope, it cannot be e.g. an enum type.
/// ```
case matching
/// We're parsing a matching pattern that is introduced via `let`, `var`, or `inout`
///
/// ```
/// case let x.y <- 'x' must refer to the base of some member access, y must refer to some pattern-compatible identifier
/// ```
case bindingIntroducer
var admitsBinding: Bool {
switch self {
case .bindingIntroducer:
return true
case .none, .matching:
return false
}
}
}
/// Parse an expression.
mutating func parseExpression(flavor: ExprFlavor, pattern: PatternContext) -> RawExprSyntax {
// If we are parsing a refutable pattern, check to see if this is the start
// of a let/var/is pattern. If so, parse it as an UnresolvedPatternExpr and
// let pattern type checking determine its final form.
//
// Only do this if we're parsing a pattern, to improve QoI on malformed
// expressions followed by (e.g.) let/var decls.
if pattern != .none {
switch self.at(anyIn: MatchingPatternStart.self) {
case (spec: .rhs(let bindingIntroducer), handle: _)?
where self.withLookahead { $0.shouldParsePatternBinding(introducer: bindingIntroducer) }:
fallthrough
case (spec: .lhs(_), handle: _)?:
let pattern = self.parseMatchingPattern(context: .matching)
return RawExprSyntax(RawPatternExprSyntax(pattern: pattern, arena: self.arena))
// If the token can't start a pattern, or contextually isn't parsed as a
// binding introducer, handle as the start of a normal expression.
case (spec: .rhs(_), handle: _)?,
nil:
// Not a pattern. Break out to parse as a normal expression below.
break
}
}
return RawExprSyntax(self.parseSequenceExpression(flavor: flavor, pattern: pattern))
}
}
extension Parser {
/// Parse a sequence of expressions.
mutating func parseSequenceExpression(
flavor: ExprFlavor,
pattern: PatternContext = .none
) -> RawExprSyntax {
if flavor == .poundIfDirective && self.atStartOfLine {
return RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
}
// Parsed sequence elements except 'lastElement'.
var elements = [RawExprSyntax]()
// The last element parsed. we don't eagerly append to 'elements' because we
// don't want to populate the 'Array' unless the expression is actually
// sequenced.
var lastElement: RawExprSyntax
lastElement = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
var loopProgress = LoopProgressCondition()
while self.hasProgressed(&loopProgress) {
guard
!lastElement.is(RawMissingExprSyntax.self),
!(flavor == .poundIfDirective && self.atStartOfLine)
else {
break
}
// Parse the operator.
guard
let (operatorExpr, rhsExpr) =
self.parseSequenceExpressionOperator(flavor: flavor, pattern: pattern)
else {
// Not an operator. We're done.
break
}
elements.append(lastElement)
elements.append(operatorExpr)
if let rhsExpr {
// Operator parsing returned the RHS.
lastElement = rhsExpr
} else if flavor == .poundIfDirective && self.atStartOfLine {
// Don't allow RHS at a newline for `#if` conditions.
lastElement = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
break
} else {
lastElement = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
}
}
// There was no operators. Return the only element we parsed.
if elements.isEmpty {
return lastElement
}
precondition(
elements.count.isMultiple(of: 2),
"elements must have an even number of elements"
)
elements.append(lastElement)
return RawExprSyntax(
RawSequenceExprSyntax(
elements: RawExprListSyntax(elements: elements, arena: self.arena),
arena: self.arena
)
)
}
/// Parse an unresolved 'as' expression.
mutating func parseUnresolvedAsExpr(
handle: TokenConsumptionHandle
) -> (operator: RawExprSyntax, rhs: RawExprSyntax) {
let asKeyword = self.eat(handle)
let failable = self.consume(if: .postfixQuestionMark, .exclamationMark)
let op = RawUnresolvedAsExprSyntax(
asKeyword: asKeyword,
questionOrExclamationMark: failable,
arena: self.arena
)
// Parse the right type expression operand as part of the 'as' production.
let type = self.parseType()
let rhs = RawTypeExprSyntax(type: type, arena: self.arena)
return (RawExprSyntax(op), RawExprSyntax(rhs))
}
/// Parse an expression sequence operators.
///
/// Returns `nil` if the current token is not at an operator.
/// Returns a tuple of an operator expression and an optional right operand
/// expression. The right operand is only returned if it is not a common
/// sequence element.
mutating func parseSequenceExpressionOperator(
flavor: ExprFlavor,
pattern: PatternContext
) -> (operator: RawExprSyntax, rhs: RawExprSyntax?)? {
enum ExpectedTokenKind: TokenSpecSet {
case binaryOperator
case infixQuestionMark
case equal
case `is`
case `as`
case async
case arrow
case `throws`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.binaryOperator): self = .binaryOperator
case TokenSpec(.infixQuestionMark): self = .infixQuestionMark
case TokenSpec(.equal): self = .equal
case TokenSpec(.is): self = .is
case TokenSpec(.as): self = .as
case TokenSpec(.async): self = .async
case TokenSpec(.arrow): self = .arrow
case TokenSpec(.throws): self = .throws
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .binaryOperator: return .binaryOperator
case .infixQuestionMark: return .infixQuestionMark
case .equal: return .equal
case .is: return .keyword(.is)
case .as: return .keyword(.as)
case .async: return .keyword(.async)
case .arrow: return .arrow
case .throws: return .keyword(.throws)
}
}
}
switch self.at(anyIn: ExpectedTokenKind.self) {
case (.binaryOperator, let handle)?:
// Parse the operator.
let operatorToken = self.eat(handle)
let op = RawBinaryOperatorExprSyntax(operator: operatorToken, arena: arena)
return (RawExprSyntax(op), nil)
case (.infixQuestionMark, let handle)?:
// Save the '?'.
let question = self.eat(handle)
let firstChoice = self.parseSequenceExpression(flavor: flavor, pattern: pattern)
// Make sure there's a matching ':' after the middle expr.
let (unexpectedBeforeColon, colon) = self.expect(.colon)
let op = RawUnresolvedTernaryExprSyntax(
questionMark: question,
thenExpression: firstChoice,
unexpectedBeforeColon,
colon: colon,
arena: self.arena
)
let rhs: RawExprSyntax?
if colon.isMissing, self.atStartOfLine {
rhs = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
} else {
rhs = nil
}
return (RawExprSyntax(op), rhs)
case (.equal, let handle)?:
switch pattern {
case .matching, .bindingIntroducer:
return nil
case .none:
let eq = self.eat(handle)
let op = RawAssignmentExprSyntax(
equal: eq,
arena: self.arena
)
return (RawExprSyntax(op), nil)
}
case (.is, let handle)?:
let isKeyword = self.eat(handle)
let op = RawUnresolvedIsExprSyntax(
isKeyword: isKeyword,
arena: self.arena
)
// Parse the right type expression operand as part of the 'is' production.
let type = self.parseType()
let rhs = RawTypeExprSyntax(type: type, arena: self.arena)
return (RawExprSyntax(op), RawExprSyntax(rhs))
case (.as, let handle)?:
return parseUnresolvedAsExpr(handle: handle)
case (.async, _)?:
if self.peek(isAt: .arrow, .keyword(.throws)) {
fallthrough
} else {
return nil
}
case (.arrow, _)?, (.throws, _)?:
var effectSpecifiers = self.parseTypeEffectSpecifiers()
let (unexpectedBeforeArrow, arrow) = self.expect(.arrow)
let unexpectedAfterArrow = self.parseMisplacedEffectSpecifiers(&effectSpecifiers)
let op = RawArrowExprSyntax(
effectSpecifiers: effectSpecifiers,
unexpectedBeforeArrow,
arrow: arrow,
unexpectedAfterArrow,
arena: self.arena
)
return (RawExprSyntax(op), nil)
case nil:
// Not an operator.
return nil
}
}
/// Whether the current token is a valid contextual exprssion modifier like
/// `copy`, `consume`.
///
/// `copy` etc. are only contextually a keyword if they are followed by an
/// identifier or keyword on the same line. We do this to ensure that we do
/// not break any copy functions defined by users.
private mutating func atContextualExpressionModifier() -> Bool {
return self.peek(
isAt: TokenSpec(.identifier, allowAtStartOfLine: false),
TokenSpec(.dollarIdentifier, allowAtStartOfLine: false),
TokenSpec(.self, allowAtStartOfLine: false)
)
}
/// Parse an expression sequence element.
mutating func parseSequenceExpressionElement(
flavor: ExprFlavor,
pattern: PatternContext = .none
) -> RawExprSyntax {
// Try to parse '@' sign or 'inout' as an attributed typerepr.
if self.at(.atSign, .keyword(.inout)) {
var lookahead = self.lookahead()
if lookahead.canParseType() {
let type = self.parseType()
return RawExprSyntax(
RawTypeExprSyntax(
type: type,
arena: self.arena
)
)
}
}
EXPR_PREFIX: switch self.at(anyIn: ExpressionModifierKeyword.self) {
case (.await, let handle)?:
let awaitTok = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawAwaitExprSyntax(
awaitKeyword: awaitTok,
expression: sub,
arena: self.arena
)
)
case (.try, let handle)?:
let tryKeyword = self.eat(handle)
let mark = self.consume(if: .exclamationMark, .postfixQuestionMark)
let expression = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawTryExprSyntax(
tryKeyword: tryKeyword,
questionOrExclamationMark: mark,
expression: expression,
arena: self.arena
)
)
case (._move, let handle)?:
let moveKeyword = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawConsumeExprSyntax(
consumeKeyword: moveKeyword,
expression: sub,
arena: self.arena
)
)
case (._borrow, let handle)?:
let borrowTok = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawBorrowExprSyntax(
borrowKeyword: borrowTok,
expression: sub,
arena: self.arena
)
)
case (.copy, let handle)?:
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}
let copyTok = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawCopyExprSyntax(
copyKeyword: copyTok,
expression: sub,
arena: self.arena
)
)
case (.consume, let handle)?:
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}
let consumeKeyword = self.eat(handle)
let sub = self.parseSequenceExpressionElement(
flavor: flavor,
pattern: pattern
)
return RawExprSyntax(
RawConsumeExprSyntax(
consumeKeyword: consumeKeyword,
expression: sub,
arena: self.arena
)
)
case (.repeat, let handle)?:
// 'repeat' is the start of a pack expansion expression.
return RawExprSyntax(parsePackExpansionExpr(repeatHandle: handle, flavor: flavor, pattern: pattern))
case (.each, let handle)?:
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}
let each = self.eat(handle)
let pack = self.parseSequenceExpressionElement(flavor: flavor, pattern: pattern)
return RawExprSyntax(
RawPackElementExprSyntax(
eachKeyword: each,
pack: pack,
arena: self.arena
)
)
case (.any, _)?:
if !atContextualExpressionModifier() && !self.peek().isContextualPunctuator("~") {
break EXPR_PREFIX
}
// 'any' is parsed as a part of 'type'.
let type = self.parseType()
return RawExprSyntax(RawTypeExprSyntax(type: type, arena: self.arena))
case nil:
break
}
return self.parseUnaryExpression(flavor: flavor, pattern: pattern)
}
/// Parse an optional prefix operator followed by an expression.
mutating func parseUnaryExpression(
flavor: ExprFlavor,
pattern: PatternContext = .none
) -> RawExprSyntax {
// Try parse a single value statement as an expression (e.g do/if/switch).
// Note we do this here in parseUnaryExpression as we don't allow postfix
// syntax to hang off such expressions to avoid ambiguities such as postfix
// '.member', which can currently be parsed as a static dot member for a
// result builder.
switch self.at(anyIn: SingleValueStatementExpression.self) {
case (.do, let handle)?:
return RawExprSyntax(self.parseDoExpression(doHandle: .noRecovery(handle)))
case (.if, let handle)?:
return RawExprSyntax(self.parseIfExpression(ifHandle: .noRecovery(handle)))
case (.switch, let handle)?:
return RawExprSyntax(self.parseSwitchExpression(switchHandle: .noRecovery(handle)))
default:
break
}
switch self.at(anyIn: ExpressionPrefixOperator.self) {
case (.prefixAmpersand, let handle)?:
let amp = self.eat(handle)
let expr = self.parseUnaryExpression(flavor: flavor, pattern: pattern)
return RawExprSyntax(
RawInOutExprSyntax(
ampersand: amp,
expression: RawExprSyntax(expr),
arena: self.arena
)
)
case (.backslash, _)?:
return RawExprSyntax(self.parseKeyPathExpression(pattern: pattern))
case (.prefixOperator, let handle)?:
let op = self.eat(handle)
let postfix = self.parseUnaryExpression(flavor: flavor, pattern: pattern)
return RawExprSyntax(
RawPrefixOperatorExprSyntax(
operator: op,
expression: postfix,
arena: self.arena
)
)
default:
// If the next token is not an operator, just parse this as expr-postfix.
return self.parsePostfixExpression(
flavor: flavor,
pattern: pattern
)
}
}
/// Parse a postfix expression applied to another expression.
mutating func parsePostfixExpression(
flavor: ExprFlavor,
pattern: PatternContext
) -> RawExprSyntax {
let head = self.parsePrimaryExpression(pattern: pattern, flavor: flavor)
guard !head.is(RawMissingExprSyntax.self) else {
return head
}
return self.parsePostfixExpressionSuffix(
head,
flavor: flavor,
pattern: pattern
)
}
mutating func parseDottedExpressionSuffix(previousNode: (some RawSyntaxNodeProtocol)?) -> (
unexpectedPeriod: RawUnexpectedNodesSyntax?,
period: RawTokenSyntax,
declName: RawDeclReferenceExprSyntax,
generics: RawGenericArgumentClauseSyntax?
) {
precondition(self.at(.period))
let (unexpectedPeriod, period, skipMemberName) = self.consumeMemberPeriod(previousNode: previousNode)
if skipMemberName {
let missingIdentifier = missingToken(.identifier)
let declName = RawDeclReferenceExprSyntax(
baseName: missingIdentifier,
argumentNames: nil,
arena: self.arena
)
return (unexpectedPeriod, period, declName, nil)
}
// Parse the name portion.
let declName: RawDeclReferenceExprSyntax
if let indexOrSelf = self.consume(if: .integerLiteral, .keyword(.self)) {
// Handle "x.42" - a tuple index.
declName = RawDeclReferenceExprSyntax(
baseName: indexOrSelf,
argumentNames: nil,
arena: self.arena
)
} else {
// Handle an arbitrary declaration name.
declName = self.parseDeclReferenceExpr([.keywords, .compoundNames])
}
// Parse the generic arguments, if any.
let generics: RawGenericArgumentClauseSyntax?
if self.withLookahead({ $0.canParseAsGenericArgumentList() }) {
generics = self.parseGenericArguments()
} else {
generics = nil
}
return (unexpectedPeriod, period, declName, generics)
}
mutating func parseDottedExpressionSuffix(_ start: RawExprSyntax?) -> RawExprSyntax {
let (unexpectedPeriod, period, declName, generics) = parseDottedExpressionSuffix(previousNode: start)
let memberAccess = RawMemberAccessExprSyntax(
base: start,
unexpectedPeriod,
period: period,
declName: declName,
arena: self.arena
)
guard let generics = generics else {
return RawExprSyntax(memberAccess)
}
return RawExprSyntax(
RawGenericSpecializationExprSyntax(
expression: RawExprSyntax(memberAccess),
genericArgumentClause: generics,
arena: self.arena
)
)
}
mutating func parseIfConfigExpressionSuffix(
_ start: RawExprSyntax?,
flavor: ExprFlavor
) -> RawExprSyntax {
precondition(self.at(.poundIf))
let config = self.parsePoundIfDirective { (parser, isFirstElement) -> RawExprSyntax? in
if !isFirstElement {
return nil
}
let head: RawExprSyntax
if parser.at(.period) {
head = parser.parseDottedExpressionSuffix(nil)
} else if parser.at(.poundIf) {
head = parser.parseIfConfigExpressionSuffix(nil, flavor: flavor)
} else {
// TODO: diagnose and skip.
return nil
}
let result = parser.parsePostfixExpressionSuffix(
head,
flavor: flavor,
pattern: .none
)
// TODO: diagnose and skip the remaining token in the current clause.
return result
} syntax: { (parser, elements) -> RawIfConfigClauseSyntax.Elements? in
switch elements.count {
case 0: return nil
case 1: return .postfixExpression(elements.first!)
default: fatalError("Postfix #if should only have one element")
}
}
return RawExprSyntax(
RawPostfixIfConfigExprSyntax(
base: start,
config: config,
arena: self.arena
)
)
}
/// Parse the suffix of a postfix expression.
mutating func parsePostfixExpressionSuffix(
_ start: RawExprSyntax,
flavor: ExprFlavor,
pattern: PatternContext
) -> RawExprSyntax {
// Handle suffix expressions.
var leadingExpr = start
var loopProgress = LoopProgressCondition()
while self.hasProgressed(&loopProgress) {
if flavor == .poundIfDirective && self.atStartOfLine {
return leadingExpr
}
// Check for a .foo suffix.
if self.at(.period) {
leadingExpr = self.parseDottedExpressionSuffix(leadingExpr)
continue
}
// If there is an expr-call-suffix, parse it and form a call.
if let lparen = self.consume(if: TokenSpec(.leftParen, allowAtStartOfLine: false)) {
let args = self.parseArgumentListElements(pattern: pattern, flavor: flavor.callArgumentFlavor)
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)
// If we can parse trailing closures, do so.
let trailingClosure: RawClosureExprSyntax?
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
if case .basic = flavor, self.at(.leftBrace), self.withLookahead({ $0.atValidTrailingClosure(flavor: flavor) })
{
(trailingClosure, additionalTrailingClosures) = self.parseTrailingClosures(flavor: flavor)
} else {
trailingClosure = nil
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
}
leadingExpr = RawExprSyntax(
RawFunctionCallExprSyntax(
calledExpression: leadingExpr,
leftParen: lparen,
arguments: RawLabeledExprListSyntax(elements: args, arena: self.arena),
unexpectedBeforeRParen,
rightParen: rparen,
trailingClosure: trailingClosure,
additionalTrailingClosures: additionalTrailingClosures,
arena: self.arena
)
)
continue
}
// Check for a [expr] suffix.
// Note that this cannot be the start of a new line.
if let lsquare = self.consume(if: TokenSpec(.leftSquare, allowAtStartOfLine: false)) {
let args: [RawLabeledExprSyntax]
if self.at(.rightSquare) {
args = []
} else {
args = self.parseArgumentListElements(pattern: pattern)
}
let (unexpectedBeforeRSquare, rsquare) = self.expect(.rightSquare)
// If we can parse trailing closures, do so.
let trailingClosure: RawClosureExprSyntax?
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
if case .basic = flavor, self.at(.leftBrace), self.withLookahead({ $0.atValidTrailingClosure(flavor: flavor) })
{
(trailingClosure, additionalTrailingClosures) = self.parseTrailingClosures(flavor: flavor)
} else {
trailingClosure = nil
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
}
leadingExpr = RawExprSyntax(
RawSubscriptCallExprSyntax(
calledExpression: leadingExpr,
leftSquare: lsquare,
arguments: RawLabeledExprListSyntax(elements: args, arena: self.arena),
unexpectedBeforeRSquare,
rightSquare: rsquare,
trailingClosure: trailingClosure,
additionalTrailingClosures: additionalTrailingClosures,
arena: self.arena
)
)
continue
}
// Check for a trailing closure, if allowed.
if self.at(.leftBrace) && !leadingExpr.raw.kind.isLiteral
&& self.withLookahead({ $0.atValidTrailingClosure(flavor: flavor) })
{
// Add dummy blank argument list to the call expression syntax.
let list = RawLabeledExprListSyntax(elements: [], arena: self.arena)
let (first, rest) = self.parseTrailingClosures(flavor: flavor)
leadingExpr = RawExprSyntax(
RawFunctionCallExprSyntax(
calledExpression: leadingExpr,
leftParen: nil,
arguments: list,
rightParen: nil,
trailingClosure: first,
additionalTrailingClosures: rest,
arena: self.arena
)
)
// We only allow a single trailing closure on a call. This could be
// generalized in the future, but needs further design.
if self.at(.leftBrace) {
break
}
continue
}
// Check for a ? suffix.
if let question = self.consume(if: .postfixQuestionMark) {
leadingExpr = RawExprSyntax(
RawOptionalChainingExprSyntax(
expression: leadingExpr,
questionMark: question,
arena: self.arena
)
)
continue
}
// Check for a ! suffix.
if let exlaim = self.consume(if: .exclamationMark) {
leadingExpr = RawExprSyntax(
RawForceUnwrapExprSyntax(
expression: leadingExpr,
exclamationMark: exlaim,
arena: self.arena
)
)
continue
}
// Check for a postfix-operator suffix.
if let op = self.consume(if: .postfixOperator) {
leadingExpr = RawExprSyntax(
RawPostfixOperatorExprSyntax(
expression: leadingExpr,
operator: op,
arena: self.arena
)
)
continue
}
if self.at(.poundIf) {
// Check if the first '#if' body starts with '.' <identifier>, and parse
// it as a "postfix ifconfig expression".
do {
var lookahead = self.lookahead()
// Skip to the first body. We may need to skip multiple '#if' directives
// since we support nested '#if's. e.g.
// baseExpr
// #if CONDITION_1
// #if CONDITION_2
// .someMember
var loopProgress = LoopProgressCondition()
repeat {
lookahead.eat(.poundIf)
while !lookahead.at(.endOfFile) && !lookahead.currentToken.isAtStartOfLine {
lookahead.skipSingle()
}
} while lookahead.at(.poundIf) && lookahead.hasProgressed(&loopProgress)
guard lookahead.atStartOfPostfixExprSuffix() else {
break
}
}
leadingExpr = self.parseIfConfigExpressionSuffix(
leadingExpr,
flavor: flavor
)
continue
}
// Otherwise, we don't know what this token is, it must end the expression.
break
}
return leadingExpr
}
}
extension Parser {
/// Determine if this is a key path postfix operator like ".?!?".
private func getNumOptionalKeyPathPostfixComponents(
_ tokenText: SyntaxText
) -> Int? {
// Make sure every character is ".", "!", or "?", without two "."s in a row.
var numComponents = 0
var lastWasDot = false
for byte in tokenText {
if byte == UInt8(ascii: ".") {
if lastWasDot {
return nil
}
lastWasDot = true
continue
}
if byte == UInt8(ascii: "!") || byte == UInt8(ascii: "?") {
lastWasDot = false
numComponents += 1
continue
}
return nil
}
return numComponents
}
/// Consume the optional key path postfix ino a set of key path components.
private mutating func consumeOptionalKeyPathPostfix(
numComponents: Int
) -> [RawKeyPathComponentSyntax] {
var components: [RawKeyPathComponentSyntax] = []
for _ in 0..<numComponents {
// Consume a period, if there is one.
let period = self.consume(ifPrefix: ".", as: .period)
// Consume the '!' or '?'.
let questionOrExclaim =
self.consume(ifPrefix: "!", as: .exclamationMark)
?? self.expectWithoutRecovery(prefix: "?", as: .postfixQuestionMark)
components.append(
RawKeyPathComponentSyntax(
period: period,
component: .optional(
RawKeyPathOptionalComponentSyntax(
questionOrExclamationMark: questionOrExclaim,
arena: self.arena
)
),
arena: self.arena
)
)
}
return components
}
/// Parse a keypath expression.
mutating func parseKeyPathExpression(pattern: PatternContext) -> RawKeyPathExprSyntax {
// Consume '\'.
let (unexpectedBeforeBackslash, backslash) = self.expect(.backslash)
// For uniformity, \.foo is parsed as if it were MAGIC.foo, so we need to
// make sure the . is there, but parsing the ? in \.? as .? doesn't make
// sense. This is all made more complicated by .?. being considered an
// operator token. Since keypath allows '.!' '.?' and '.[', consume '.'
// the token is an operator starts with '.', or the following token is '['.
let rootType: RawTypeSyntax?
if !self.at(prefix: ".") {
rootType = self.parseSimpleType(stopAtFirstPeriod: true)
} else {
rootType = nil
}
var components: [RawKeyPathComponentSyntax] = []
var loopProgress = LoopProgressCondition()
while self.hasProgressed(&loopProgress) {
// Check for a [] or .[] suffix. The latter is only permitted when there
// are no components.
if self.at(TokenSpec(.leftSquare, allowAtStartOfLine: false))
|| (components.isEmpty && self.at(.period) && self.peek(isAt: .leftSquare))
{
// Consume the '.', if it's allowed here.
let period: RawTokenSyntax?
if !self.at(.leftSquare) {
period = self.consumeAnyToken()
} else {
period = nil
}
precondition(self.at(.leftSquare))
let lsquare = self.consumeAnyToken()
let args: [RawLabeledExprSyntax]
if self.at(.rightSquare) {
args = []
} else {
args = self.parseArgumentListElements(pattern: pattern)
}
let (unexpectedBeforeRSquare, rsquare) = self.expect(.rightSquare)
components.append(
RawKeyPathComponentSyntax(
period: period,
component: .subscript(
RawKeyPathSubscriptComponentSyntax(
leftSquare: lsquare,
arguments: RawLabeledExprListSyntax(
elements: args,
arena: self.arena
),
unexpectedBeforeRSquare,
rightSquare: rsquare,
arena: self.arena
)
),
arena: self.arena
)
)
continue
}
// Check for an operator starting with '.' that contains only
// periods, '?'s, and '!'s. Expand that into key path components.
if self.at(.prefixOperator, .binaryOperator, .postfixOperator) || self.at(.postfixQuestionMark, .exclamationMark),
let numComponents = getNumOptionalKeyPathPostfixComponents(
self.currentToken.tokenText
)
{
components.append(
contentsOf: self.consumeOptionalKeyPathPostfix(
numComponents: numComponents
)
)
continue
}
// Check for a .name or .1 suffix.
if self.at(.period) {
let (unexpectedPeriod, period, declName, generics) = parseDottedExpressionSuffix(
previousNode: components.last?.raw ?? rootType?.raw ?? backslash.raw
)
components.append(
RawKeyPathComponentSyntax(
unexpectedPeriod,
period: period,
component: .property(
RawKeyPathPropertyComponentSyntax(
declName: declName,
genericArgumentClause: generics,
arena: self.arena
)
),
arena: self.arena
)
)
continue
}
// No more postfix expressions.
break
}
return RawKeyPathExprSyntax(
unexpectedBeforeBackslash,
backslash: backslash,
root: rootType,
components: RawKeyPathComponentListSyntax(
elements: components,
arena: self.arena
),
arena: self.arena
)
}
}
extension Parser {
/// Parse a "primary expression" - these are the most basic leaves of the
/// Swift expression grammar.
mutating func parsePrimaryExpression(
pattern: PatternContext,
flavor: ExprFlavor
) -> RawExprSyntax {
switch self.at(anyIn: PrimaryExpressionStart.self) {
case (.integerLiteral, let handle)?:
let literal = self.eat(handle)
return RawExprSyntax(
RawIntegerLiteralExprSyntax(
literal: literal,
arena: self.arena
)
)
case (.floatLiteral, let handle)?:
let literal = self.eat(handle)
return RawExprSyntax(
RawFloatLiteralExprSyntax(
literal: literal,
arena: self.arena
)
)
case (.atSign, _)?:
return RawExprSyntax(self.parseStringLiteral())
case (.rawStringDelimiter, _)?, (.stringQuote, _)?, (.multilineStringQuote, _)?, (.singleQuote, _)?:
return RawExprSyntax(self.parseStringLiteral())
case (.extendedRegexDelimiter, _)?, (.regexSlash, _)?:
return RawExprSyntax(self.parseRegexLiteral())
case (.nil, let handle)?:
let nilKeyword = self.eat(handle)
return RawExprSyntax(
RawNilLiteralExprSyntax(
nilKeyword: nilKeyword,
arena: self.arena
)
)
case (.true, let handle)?,
(.false, let handle)?:
let literal = self.eat(handle)
return RawExprSyntax(
RawBooleanLiteralExprSyntax(
literal: literal,
arena: self.arena
)
)
case (.identifier, let handle)?, (.self, let handle)?, (.`init`, let handle)?, (.`deinit`, let handle)?,
(.`subscript`, let handle)?:
// If we have "case let x" followed by ".", "(", "[", or a generic
// argument list, we parse x as a normal name, not a binding, because it
// is the start of an enum or expr pattern.
if pattern.admitsBinding && self.lookahead().isInBindingPatternPosition() {
let identifier = self.eat(handle)
let pattern = RawPatternSyntax(
RawIdentifierPatternSyntax(
identifier: identifier,
arena: self.arena
)
)
return RawExprSyntax(RawPatternExprSyntax(pattern: pattern, arena: self.arena))
}
return RawExprSyntax(self.parseIdentifierExpression(flavor: flavor))
case (.Self, _)?: // Self
return RawExprSyntax(self.parseIdentifierExpression(flavor: flavor))
case (.Any, _)?: // Any
let anyType = RawTypeSyntax(self.parseAnyType())
return RawExprSyntax(RawTypeExprSyntax(type: anyType, arena: self.arena))
case (.dollarIdentifier, _)?:
return RawExprSyntax(self.parseAnonymousClosureArgument())
case (.wildcard, let handle)?: // _
let wild = self.eat(handle)
return RawExprSyntax(
RawDiscardAssignmentExprSyntax(
wildcard: wild,
arena: self.arena
)
)
case (.pound, _)?:
return RawExprSyntax(
self.parseMacroExpansionExpr(pattern: pattern, flavor: flavor)
)
case (.poundAvailable, _)?, (.poundUnavailable, _)?:
let poundAvailable = self.parsePoundAvailableConditionElement()
return RawExprSyntax(
RawDeclReferenceExprSyntax(
RawUnexpectedNodesSyntax([poundAvailable], arena: self.arena),
baseName: missingToken(.identifier),
argumentNames: nil,
arena: self.arena
)
)
case (.leftBrace, _)?: // expr-closure
return RawExprSyntax(self.parseClosureExpression())
case (.period, let handle)?: // .foo
let period = self.eat(handle)
// Special case ".<integer_literal>" like ".4". This isn't valid, but the
// developer almost certainly meant to use "0.4". Diagnose this, and
// recover as if they wrote that.
if let integerLiteral = self.consume(if: .integerLiteral) {
let text = arena.intern(
"0" + String(syntaxText: period.tokenText) + String(syntaxText: integerLiteral.tokenText)
)
return RawExprSyntax(
RawFloatLiteralExprSyntax(
literal: RawTokenSyntax(
missing: .floatLiteral,
text: text,
arena: self.arena
),
RawUnexpectedNodesSyntax(
elements: [
RawSyntax(period),
RawSyntax(integerLiteral),
],
arena: self.arena
),
arena: self.arena
)
)
}
let declName = self.parseDeclReferenceExpr([.keywords, .compoundNames])
return RawExprSyntax(
RawMemberAccessExprSyntax(
base: nil,
period: period,
declName: declName,
arena: self.arena
)
)
case (.super, _)?: // 'super'
return RawExprSyntax(self.parseSuperExpression())
case (.leftParen, _)?:
// Build a tuple expression syntax node.
// AST differentiates paren and tuple expression where the former allows
// only one element without label. However, libSyntax tree doesn't have this
// differentiation. A tuple expression node in libSyntax can have a single
// element without label.
return RawExprSyntax(self.parseTupleExpression(pattern: pattern))
case (.leftSquare, _)?:
return self.parseCollectionLiteral()
case nil:
return RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
}
}
}
extension Parser {
/// Parse an identifier as an expression.
mutating func parseIdentifierExpression(flavor: ExprFlavor) -> RawExprSyntax {
var options: DeclNameOptions = .compoundNames
switch flavor {
case .basic, .poundIfDirective, .stmtCondition: break
case .attributeArguments: options.insert(.keywordsUsingSpecialNames)
}
let declName = self.parseDeclReferenceExpr(options)
guard self.withLookahead({ $0.canParseAsGenericArgumentList() }) else {
return RawExprSyntax(declName)
}
let generics = self.parseGenericArguments()
return RawExprSyntax(
RawGenericSpecializationExprSyntax(
expression: RawExprSyntax(declName),
genericArgumentClause: generics,
arena: self.arena
)
)
}
}
extension Parser {
/// Parse a macro expansion as an expression.
mutating func parseMacroExpansionExpr(
pattern: PatternContext,
flavor: ExprFlavor
) -> RawMacroExpansionExprSyntax {
var (unexpectedBeforePound, pound) = self.expect(.pound)
if pound.trailingTriviaByteLength > 0 || currentToken.leadingTriviaByteLength > 0 {
// If there are whitespaces after '#' diagnose.
let diagnostic = TokenDiagnostic(
.extraneousTrailingWhitespaceError,
byteOffset: pound.leadingTriviaByteLength + pound.tokenText.count
)
pound = pound.tokenView.withTokenDiagnostic(tokenDiagnostic: diagnostic, arena: self.arena)
}
let unexpectedBeforeMacroName: RawUnexpectedNodesSyntax?
let macroName: RawTokenSyntax
if !self.atStartOfLine {
(unexpectedBeforeMacroName, macroName) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
} else {
unexpectedBeforeMacroName = nil
macroName = self.missingToken(.identifier)
}
// Parse the optional generic argument list.
let generics: RawGenericArgumentClauseSyntax?
if self.withLookahead({ $0.canParseAsGenericArgumentList() }) {
generics = self.parseGenericArguments()
} else {
generics = nil
}
// Parse the optional parenthesized argument list.
let leftParen = self.consume(if: TokenSpec(.leftParen, allowAtStartOfLine: false))
let args: [RawLabeledExprSyntax]
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
let rightParen: RawTokenSyntax?
if leftParen != nil {
args = parseArgumentListElements(pattern: pattern)
(unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
} else {
args = []
unexpectedBeforeRightParen = nil
rightParen = nil
}
// Parse the optional trailing closures.
let trailingClosure: RawClosureExprSyntax?
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
if case .basic = flavor, self.at(.leftBrace), self.withLookahead({ $0.atValidTrailingClosure(flavor: flavor) }) {
(trailingClosure, additionalTrailingClosures) = self.parseTrailingClosures(flavor: flavor)
} else {
trailingClosure = nil
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
}
return RawMacroExpansionExprSyntax(
unexpectedBeforePound,
pound: pound,
unexpectedBeforeMacroName,
macroName: macroName,
genericArgumentClause: generics,
leftParen: leftParen,
arguments: RawLabeledExprListSyntax(
elements: args,
arena: self.arena
),
unexpectedBeforeRightParen,
rightParen: rightParen,
trailingClosure: trailingClosure,
additionalTrailingClosures: additionalTrailingClosures,
arena: self.arena
)
}
}
extension Parser {
/// Parse a pack expansion as an expression.
mutating func parsePackExpansionExpr(
repeatHandle: TokenConsumptionHandle,
flavor: ExprFlavor,
pattern: PatternContext
) -> RawPackExpansionExprSyntax {
let repeatKeyword = self.eat(repeatHandle)
let repetitionPattern = self.parseExpression(flavor: flavor, pattern: pattern)
return RawPackExpansionExprSyntax(
repeatKeyword: repeatKeyword,
repetitionPattern: repetitionPattern,
arena: self.arena
)
}
}
extension Parser {
/// Parse a regular expression literal.
///
/// The broad structure of the regular expression is validated by the lexer.
mutating func parseRegexLiteral() -> RawRegexLiteralExprSyntax {
// See if we have an opening set of pounds.
let openingPounds = self.consume(if: .regexPoundDelimiter)
// Parse the opening slash.
let (unexpectedBeforeSlash, openingSlash) = self.expect(.regexSlash)
// If we had opening pounds, there should be no trivia for the slash.
if let openingPounds {
precondition(openingPounds.trailingTriviaByteLength == 0 && openingSlash.leadingTriviaByteLength == 0)
}
// Parse the pattern and closing slash, avoiding recovery or leading trivia
// as the lexer should provide the tokens exactly in order without trivia,
// otherwise they should be treated as missing.
let regex = self.expectWithoutRecoveryOrLeadingTrivia(.regexLiteralPattern)
let closingSlash = self.expectWithoutRecoveryOrLeadingTrivia(.regexSlash)
// Finally, parse a closing set of pounds.
let (unexpectedBeforeClosePounds, closingPounds) = parsePoundDelimiter(
.regexPoundDelimiter,
matching: openingPounds
)
return RawRegexLiteralExprSyntax(
openingPounds: openingPounds,
unexpectedBeforeSlash,
openingSlash: openingSlash,
regex: regex,
closingSlash: closingSlash,
unexpectedBeforeClosePounds,
closingPounds: closingPounds,
arena: self.arena
)
}
}
extension Parser {
/// Parse a 'super' reference to the superclass instance of a class.
mutating func parseSuperExpression() -> RawSuperExprSyntax {
// Parse the 'super' reference.
let (unexpectedBeforeSuperKeyword, superKeyword) = self.expect(.keyword(.super))
return RawSuperExprSyntax(
unexpectedBeforeSuperKeyword,
superKeyword: superKeyword,
arena: self.arena
)
}
}
extension Parser {
/// Parse a tuple expression.
mutating func parseTupleExpression(pattern: PatternContext) -> RawTupleExprSyntax {
let (unexpectedBeforeLParen, lparen) = self.expect(.leftParen)
let elements = self.parseArgumentListElements(pattern: pattern)
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)
return RawTupleExprSyntax(
unexpectedBeforeLParen,
leftParen: lparen,
elements: RawLabeledExprListSyntax(elements: elements, arena: self.arena),
unexpectedBeforeRParen,
rightParen: rparen,
arena: self.arena
)
}
}
extension Parser {
enum CollectionKind {
case dictionary(
key: RawExprSyntax,
unexpectedBeforeColon: RawUnexpectedNodesSyntax?,
colon: RawTokenSyntax,
value: RawExprSyntax
)
case array(RawExprSyntax)
}
/// Parse an element of an array or dictionary literal.
mutating func parseCollectionElement(_ existing: CollectionKind?) -> CollectionKind {
let key = self.parseExpression(flavor: .basic, pattern: .none)
switch existing {
case .array(_):
return .array(key)
case nil:
guard self.at(.colon) else {
return .array(key)
}
fallthrough
case .dictionary:
let (unexpectedBeforeColon, colon) = self.expect(.colon)
let value = self.parseExpression(flavor: .basic, pattern: .none)
return .dictionary(key: key, unexpectedBeforeColon: unexpectedBeforeColon, colon: colon, value: value)
}
}
/// Parse an array or dictionary literal.
mutating func parseCollectionLiteral() -> RawExprSyntax {
if let remainingTokens = remainingTokensIfMaximumNestingLevelReached() {
return RawExprSyntax(
RawArrayExprSyntax(
remainingTokens,
leftSquare: missingToken(.leftSquare),
elements: RawArrayElementListSyntax(elements: [], arena: self.arena),
rightSquare: missingToken(.rightSquare),
arena: self.arena
)
)
}
let (unexpectedBeforeLSquare, lsquare) = self.expect(.leftSquare)
if let rsquare = self.consume(if: .rightSquare) {
return RawExprSyntax(
RawArrayExprSyntax(
unexpectedBeforeLSquare,
leftSquare: lsquare,
elements: RawArrayElementListSyntax(elements: [], arena: self.arena),
rightSquare: rsquare,
arena: self.arena
)
)
}
if let (colon, rsquare) = self.consume(if: .colon, followedBy: .rightSquare) {
return RawExprSyntax(
RawDictionaryExprSyntax(
unexpectedBeforeLSquare,
leftSquare: lsquare,
content: .colon(colon),
rightSquare: rsquare,
arena: self.arena
)
)
}
var elementKind: CollectionKind? = nil
var elements = [RawSyntax]()
do {
var collectionProgress = LoopProgressCondition()
var keepGoing: RawTokenSyntax?
COLLECTION_LOOP: repeat {
elementKind = self.parseCollectionElement(elementKind)
/// Whether expression of an array element or the value of a dictionary
/// element is missing. If this is the case, we shouldn't recover from
/// a missing comma since most likely the closing `]` is missing.
var elementIsMissingExpression: Bool {
switch elementKind! {
case .dictionary(_, _, _, let value):
return value.is(RawMissingExprSyntax.self)
case .array(let rawExprSyntax):
return rawExprSyntax.is(RawMissingExprSyntax.self)
}
}
// Parse the ',' if exists.
if let token = self.consume(if: .comma) {
keepGoing = token
} else if !self.at(.rightSquare, .endOfFile) && !self.atStartOfLine && !elementIsMissingExpression
&& !self.atStartOfDeclaration() && !self.atStartOfStatement(preferExpr: false)
{
keepGoing = missingToken(.comma)
} else {
keepGoing = nil
}
switch elementKind! {
case .array(let el):
let element = RawArrayElementSyntax(
expression: el,
trailingComma: keepGoing,
arena: self.arena
)
if element.isEmpty {
break COLLECTION_LOOP
} else {
elements.append(RawSyntax(element))
}
case .dictionary(let key, let unexpectedBeforeColon, let colon, let value):
let element = RawDictionaryElementSyntax(
key: key,
unexpectedBeforeColon,
colon: colon,
value: value,
trailingComma: keepGoing,
arena: self.arena
)
if element.isEmpty {
break COLLECTION_LOOP
} else {
elements.append(RawSyntax(element))
}
}
} while keepGoing != nil && self.hasProgressed(&collectionProgress)
}
let (unexpectedBeforeRSquare, rsquare) = self.expect(.rightSquare)
switch elementKind! {
case .dictionary:
return RawExprSyntax(
RawDictionaryExprSyntax(
leftSquare: lsquare,
content: .elements(
RawDictionaryElementListSyntax(
elements: elements.map {
$0.as(RawDictionaryElementSyntax.self)!
},
arena: self.arena
)
),
unexpectedBeforeRSquare,
rightSquare: rsquare,
arena: self.arena
)
)
case .array:
return RawExprSyntax(
RawArrayExprSyntax(
leftSquare: lsquare,
elements: RawArrayElementListSyntax(
elements: elements.map {
$0.as(RawArrayElementSyntax.self)!
},
arena: self.arena
),
unexpectedBeforeRSquare,
rightSquare: rsquare,
arena: self.arena
)
)
}
}
}
extension Parser {
mutating func parseDefaultArgument() -> RawInitializerClauseSyntax {
let unexpectedBeforeEq: RawUnexpectedNodesSyntax?
let eq: RawTokenSyntax
if let comparison = self.consumeIfContextualPunctuator("==") {
unexpectedBeforeEq = RawUnexpectedNodesSyntax(
elements: [RawSyntax(comparison)],
arena: self.arena
)
eq = missingToken(.equal)
} else {
(unexpectedBeforeEq, eq) = self.expect(.equal)
}
let expr = self.parseExpression(flavor: .basic, pattern: .none)
return RawInitializerClauseSyntax(
unexpectedBeforeEq,
equal: eq,
value: expr,
arena: self.arena
)
}
}
extension Parser {
mutating func parseAnonymousClosureArgument() -> RawDeclReferenceExprSyntax {
let (unexpectedBeforeBaseName, baseName) = self.expect(.dollarIdentifier)
return RawDeclReferenceExprSyntax(
unexpectedBeforeBaseName,
baseName: baseName,
argumentNames: nil,
arena: self.arena
)
}
}
extension Parser {
/// Parse a closure expression.
mutating func parseClosureExpression() -> RawClosureExprSyntax {
// Parse the opening left brace.
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
// Parse the closure-signature, if present.
let signature = self.parseClosureSignatureIfPresent()
// Parse the body.
let elements = parseCodeBlockItemList(until: { $0.at(.rightBrace) })
// Parse the closing '}'.
let (unexpectedBeforeRBrace, rbrace) = self.expect(.rightBrace)
return RawClosureExprSyntax(
unexpectedBeforeLBrace,
leftBrace: lbrace,
signature: signature,
statements: elements,
unexpectedBeforeRBrace,
rightBrace: rbrace,
arena: self.arena
)
}
}
extension Parser {
/// Parse the signature of a closure, if one is present.
mutating func parseClosureSignatureIfPresent() -> RawClosureSignatureSyntax? {
// If we have a leading token that may be part of the closure signature, do a
// speculative parse to validate it and look for 'in'.
guard self.at(.atSign, .leftParen, .leftSquare) || self.at(.wildcard, .identifier) else {
// No closure signature.
return nil
}
guard self.withLookahead({ $0.canParseClosureSignature() }) else {
return nil
}
let attrs = self.parseAttributeList()
let captures: RawClosureCaptureClauseSyntax?
if let lsquare = self.consume(if: .leftSquare) {
// At this point, we know we have a closure signature. Parse the capture list
// and parameters.
var elements = [RawClosureCaptureSyntax]()
if !self.at(.rightSquare) {
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
repeat {
// Parse any specifiers on the capture like `weak` or `unowned`
let specifier = self.parseClosureCaptureSpecifiers()
// The thing being capture specified is an identifier, or as an identifier
// followed by an expression.
let unexpectedBeforeName: RawUnexpectedNodesSyntax?
let name: RawTokenSyntax?
let unexpectedBeforeEqual: RawUnexpectedNodesSyntax?
let equal: RawTokenSyntax?
let expression: RawExprSyntax
if self.peek(isAt: .equal) {
// The name is a new declaration.
(unexpectedBeforeName, name) = self.expect(
.identifier,
TokenSpec(.self, remapping: .identifier),
default: .identifier
)
(unexpectedBeforeEqual, equal) = self.expect(.equal)
expression = self.parseExpression(flavor: .basic, pattern: .none)
} else {
// This is the simple case - the identifier is both the name and
// the expression to capture.
unexpectedBeforeName = nil
name = nil
unexpectedBeforeEqual = nil
equal = nil
expression = RawExprSyntax(self.parseIdentifierExpression(flavor: .basic))
}
keepGoing = self.consume(if: .comma)
elements.append(
RawClosureCaptureSyntax(
specifier: specifier,
unexpectedBeforeName,
name: name,
unexpectedBeforeEqual,
equal: equal,
expression: expression,
trailingComma: keepGoing,
arena: self.arena
)
)
} while keepGoing != nil && self.hasProgressed(&loopProgress)
}
// We were promised a right square bracket, so we're going to get it.
var unexpectedNodes = [RawSyntax]()
while !self.at(.endOfFile) && !self.at(.rightSquare) && !self.at(.keyword(.in)) {
unexpectedNodes.append(RawSyntax(self.consumeAnyToken()))
}
let (unexpectedBeforeRSquare, rsquare) = self.expect(.rightSquare)
unexpectedNodes.append(contentsOf: unexpectedBeforeRSquare?.elements ?? [])
captures = RawClosureCaptureClauseSyntax(
leftSquare: lsquare,
items: RawClosureCaptureListSyntax(elements: elements, arena: self.arena),
RawUnexpectedNodesSyntax(unexpectedNodes, arena: self.arena),
rightSquare: rsquare,
arena: self.arena
)
} else {
captures = nil
}
var parameterClause: RawClosureSignatureSyntax.ParameterClause?
var effectSpecifiers: RawTypeEffectSpecifiersSyntax?
var returnClause: RawReturnClauseSyntax? = nil
if !self.at(.keyword(.in)) {
// If the next token is ':', then it looks like the code contained a non-shorthand closure parameter with a type annotation.
// These need to be wrapped in parentheses.
if self.at(.leftParen) || self.peek(isAt: .colon) {
// Parse the closure arguments.
let params = self.parseParameterClause(RawClosureParameterClauseSyntax.self) { parser in
parser.parseClosureParameter()
}
parameterClause = .parameterClause(params)
} else {
var params = [RawClosureShorthandParameterSyntax]()
var loopProgress = LoopProgressCondition()
do {
// Parse identifier (',' identifier)*
var keepGoing: RawTokenSyntax? = nil
repeat {
let unexpected: RawUnexpectedNodesSyntax?
let name: RawTokenSyntax
if let identifier = self.consume(if: .identifier) {
unexpected = nil
name = identifier
} else {
(unexpected, name) = self.expect(.wildcard)
}
keepGoing = consume(if: .comma)
params.append(
RawClosureShorthandParameterSyntax(
unexpected,
name: name,
trailingComma: keepGoing,
arena: self.arena
)
)
} while keepGoing != nil && self.hasProgressed(&loopProgress)
}
parameterClause = .simpleInput(RawClosureShorthandParameterListSyntax(elements: params, arena: self.arena))
}
effectSpecifiers = self.parseTypeEffectSpecifiers()
if self.at(.arrow) {
returnClause = self.parseFunctionReturnClause(
effectSpecifiers: &effectSpecifiers,
allowNamedOpaqueResultType: false
)
}
}
// Parse the 'in'.
let (unexpectedBeforeInKeyword, inKeyword) = self.expect(.keyword(.in))
return RawClosureSignatureSyntax(
attributes: attrs,
capture: captures,
parameterClause: parameterClause,
effectSpecifiers: effectSpecifiers,
returnClause: returnClause,
unexpectedBeforeInKeyword,
inKeyword: inKeyword,
arena: self.arena
)
}
mutating func parseClosureCaptureSpecifiers() -> RawClosureCaptureSpecifierSyntax? {
// Check for the strength specifier: "weak", "unowned", or
// "unowned(safe/unsafe)".
if let weakContextualKeyword = self.consume(if: .keyword(.weak)) {
return RawClosureCaptureSpecifierSyntax(
specifier: weakContextualKeyword,
leftParen: nil,
detail: nil,
rightParen: nil,
arena: self.arena
)
} else if let unownedContextualKeyword = self.consume(if: .keyword(.unowned)) {
if let lparen = self.consume(if: .leftParen) {
let (unexpectedBeforeDetail, detail) = self.expect(.keyword(.safe), .keyword(.unsafe), default: .keyword(.safe))
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)
return RawClosureCaptureSpecifierSyntax(
specifier: unownedContextualKeyword,
leftParen: lparen,
unexpectedBeforeDetail,
detail: detail,
unexpectedBeforeRParen,
rightParen: rparen,
arena: self.arena
)
} else {
return RawClosureCaptureSpecifierSyntax(
specifier: unownedContextualKeyword,
leftParen: nil,
detail: nil,
rightParen: nil,
arena: self.arena
)
}
} else {
return nil
}
}
}
extension Parser {
/// Parse the elements of an argument list.
///
/// This is currently the same as parsing a tuple expression. In the future,
/// this will be a dedicated argument list type.
mutating func parseArgumentListElements(
pattern: PatternContext,
flavor: ExprFlavor = .basic
) -> [RawLabeledExprSyntax] {
if let remainingTokens = remainingTokensIfMaximumNestingLevelReached() {
return [
RawLabeledExprSyntax(
remainingTokens,
label: nil,
colon: nil,
expression: RawExprSyntax(RawMissingExprSyntax(arena: self.arena)),
trailingComma: nil,
arena: self.arena
)
]
}
guard !self.at(.rightParen) else {
return []
}
var result = [RawLabeledExprSyntax]()
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
repeat {
let unexpectedBeforeLabel: RawUnexpectedNodesSyntax?
let label: RawTokenSyntax?
let colon: RawTokenSyntax?
if self.atArgumentLabel(allowDollarIdentifier: true) && self.peek(isAt: .colon) {
(unexpectedBeforeLabel, label) = parseArgumentLabel()
colon = consumeAnyToken()
} else if let _colon = self.consume(if: .colon) {
unexpectedBeforeLabel = nil
label = RawTokenSyntax(missing: .identifier, arena: self.arena)
colon = _colon
} else {
unexpectedBeforeLabel = nil
label = nil
colon = nil
}
// See if we have an operator decl ref '(<op>)'. The operator token in
// this case lexes as a binary operator because it neither leads nor
// follows a proper subexpression.
let expr: RawExprSyntax
if self.at(.binaryOperator) && self.peek(isAt: .comma, .rightParen, .rightSquare) {
expr = RawExprSyntax(self.parseDeclReferenceExpr(.operators))
} else {
expr = self.parseExpression(flavor: flavor, pattern: pattern)
}
keepGoing = self.consume(if: .comma)
result.append(
RawLabeledExprSyntax(
unexpectedBeforeLabel,
label: label,
colon: colon,
expression: expr,
trailingComma: keepGoing,
arena: self.arena
)
)
} while keepGoing != nil && self.hasProgressed(&loopProgress)
return result
}
}
extension Parser {
/// Parse the trailing closure(s) following a call expression.
mutating func parseTrailingClosures(
flavor: ExprFlavor
) -> (RawClosureExprSyntax, RawMultipleTrailingClosureElementListSyntax) {
// Parse the closure.
let closure = self.parseClosureExpression()
// Parse labeled trailing closures.
var elements = [RawMultipleTrailingClosureElementSyntax]()
var loopProgress = LoopProgressCondition()
while self.withLookahead({ $0.atStartOfLabelledTrailingClosure() }) && self.hasProgressed(&loopProgress) {
let (unexpectedBeforeLabel, label) = self.parseArgumentLabel()
let (unexpectedBeforeColon, colon) = self.expect(.colon)
let closure = self.parseClosureExpression()
elements.append(
RawMultipleTrailingClosureElementSyntax(
unexpectedBeforeLabel,
label: label,
unexpectedBeforeColon,
colon: colon,
closure: closure,
arena: self.arena
)
)
}
let trailing =
elements.isEmpty
? self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
: RawMultipleTrailingClosureElementListSyntax(elements: elements, arena: self.arena)
return (closure, trailing)
}
}
extension Parser.Lookahead {
mutating func atStartOfLabelledTrailingClosure() -> Bool {
// Fast path: the next two tokens must be a label and a colon.
// But 'default:' is ambiguous with switch cases and we disallow it
// (unless escaped) even outside of switches.
if !self.atArgumentLabel()
|| self.at(.keyword(.default))
|| self.peek().rawTokenKind != .colon
{
return false
}
// Do some tentative parsing to distinguish `label: { ... }` and
// `label: switch x { ... }`.
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
if lookahead.peek().rawTokenKind == .leftBrace {
return true
}
if lookahead.peek().isEditorPlaceholder {
// Editor placeholder can represent entire closures
return true
}
return false
}
/// Recover invalid uses of trailing closures in a situation
/// where the parser requires an expr-basic (which does not allow them). We
/// handle this by doing some lookahead in common situations. And later, Sema
/// will emit a diagnostic with a fixit to add wrapping parens.
mutating func atValidTrailingClosure(flavor: Parser.ExprFlavor) -> Bool {
precondition(self.at(.leftBrace), "Couldn't be a trailing closure")
// If this is the start of a get/set accessor, then it isn't a trailing
// closure.
guard !self.withLookahead({ $0.atStartOfGetSetAccessor() }) else {
return false
}
// If this is the start of a switch body, this isn't a trailing closure.
if TokenSpec(.case) ~= self.peek() {
return false
}
// If this is a normal expression (not an expr-basic) then trailing closures
// are allowed, so this is obviously one.
// TODO: We could handle try to disambiguate cases like:
// let x = foo
// {...}()
// by looking ahead for the ()'s, but this has been replaced by do{}, so this
// probably isn't worthwhile.
guard case .stmtCondition = flavor else {
return true
}
// If this is an expr-basic, then a trailing closure is not allowed. However,
// it is very common for someone to write something like:
//
// for _ in numbers.filter {$0 > 4} {
//
// and we want to recover from this very well. We need to perform arbitrary
// look-ahead to disambiguate this case, so we only do this in the case where
// the token after the { is on the same line as the {.
guard !self.peek().isAtStartOfLine else {
return false
}
// Determine if the {} goes with the expression by eating it, and looking
// to see if it is immediately followed by a token which indicates we should
// consider it part of the preceding expression
var lookahead = self.lookahead()
lookahead.eat(.leftBrace)
var loopProgress = LoopProgressCondition()
while !lookahead.at(.endOfFile, .rightBrace)
&& !lookahead.at(.poundEndif, .poundElse, .poundElseif)
&& lookahead.hasProgressed(&loopProgress)
{
lookahead.skipSingle()
}
guard lookahead.consume(if: .rightBrace) != nil else {
return false
}
switch lookahead.currentToken {
case TokenSpec(.leftBrace),
TokenSpec(.where),
TokenSpec(.comma):
return true
case TokenSpec(.leftSquare),
TokenSpec(.leftParen),
TokenSpec(.period),
TokenSpec(.is),
TokenSpec(.as),
TokenSpec(.postfixQuestionMark),
TokenSpec(.infixQuestionMark),
TokenSpec(.exclamationMark),
TokenSpec(.colon),
TokenSpec(.equal),
TokenSpec(.postfixOperator),
TokenSpec(.binaryOperator):
return !lookahead.atStartOfLine
default:
return false
}
}
}
// MARK: Do-Catch Expressions
extension Parser {
/// Parse a do expression.
mutating func parseDoExpression(doHandle: RecoveryConsumptionHandle) -> RawDoExprSyntax {
precondition(experimentalFeatures.contains(.doExpressions))
let (unexpectedBeforeDoKeyword, doKeyword) = self.eat(doHandle)
let body = self.parseCodeBlock(introducer: doKeyword)
// If the next token is 'catch', this is a 'do'/'catch'.
var elements = [RawCatchClauseSyntax]()
var loopProgress = LoopProgressCondition()
while self.at(.keyword(.catch)) && self.hasProgressed(&loopProgress) {
// Parse 'catch' clauses
elements.append(self.parseCatchClause())
}
return RawDoExprSyntax(
unexpectedBeforeDoKeyword,
doKeyword: doKeyword,
body: body,
catchClauses: RawCatchClauseListSyntax(elements: elements, arena: self.arena),
arena: self.arena
)
}
}
// MARK: Conditional Expressions
extension Parser {
/// Parse an if statement/expression.
mutating func parseIfExpression(
ifHandle: RecoveryConsumptionHandle
) -> RawIfExprSyntax {
let (unexpectedBeforeIfKeyword, ifKeyword) = self.eat(ifHandle)
let conditions: RawConditionElementListSyntax
if self.at(.leftBrace) {
conditions = RawConditionElementListSyntax(
elements: [
RawConditionElementSyntax(
condition: .expression(RawExprSyntax(RawMissingExprSyntax(arena: self.arena))),
trailingComma: nil,
arena: self.arena
)
],
arena: self.arena
)
} else {
conditions = self.parseConditionList()
}
let body = self.parseCodeBlock(introducer: ifKeyword)
// The else branch, if any, is outside of the scope of the condition.
let elseKeyword = self.consume(if: .keyword(.else))
let elseBody: RawIfExprSyntax.ElseBody?
if elseKeyword != nil {
if self.at(.keyword(.if)) {
elseBody = .ifExpr(
self.parseIfExpression(ifHandle: .constant(.keyword(.if)))
)
} else {
elseBody = .codeBlock(self.parseCodeBlock(introducer: ifKeyword))
}
} else {
elseBody = nil
}
return RawIfExprSyntax(
unexpectedBeforeIfKeyword,
ifKeyword: ifKeyword,
conditions: conditions,
body: body,
elseKeyword: elseKeyword,
elseBody: elseBody,
arena: self.arena
)
}
}
// MARK: Switch Statements/Expressions
extension Parser {
/// Parse a switch statement/expression.
mutating func parseSwitchExpression(
switchHandle: RecoveryConsumptionHandle
) -> RawSwitchExprSyntax {
let (unexpectedBeforeSwitchKeyword, switchKeyword) = self.eat(switchHandle)
// If there is no expression, like `switch { default: return false }` then left brace would parsed as
// a ``RawClosureExprSyntax`` in the condition, which is most likely not what the user meant.
// Create a missing condition instead and use the `{` for the start of the body.
let subject: RawExprSyntax
if self.at(.leftBrace) {
subject = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
} else {
subject = self.parseExpression(flavor: .stmtCondition, pattern: .none)
}
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
let cases = self.parseSwitchCases(allowStandaloneStmtRecovery: !lbrace.isMissing)
let (unexpectedBeforeRBrace, rbrace) = self.expectRightBrace(leftBrace: lbrace, introducer: switchKeyword)
return RawSwitchExprSyntax(
unexpectedBeforeSwitchKeyword,
switchKeyword: switchKeyword,
subject: subject,
unexpectedBeforeLBrace,
leftBrace: lbrace,
cases: cases,
unexpectedBeforeRBrace,
rightBrace: rbrace,
arena: self.arena
)
}
/// Parse a list of switch case clauses.
///
/// If `allowStandaloneStmtRecovery` is `true` and we discover a statement that
/// isn't covered by a case, we assume that the developer forgot to wrote the
/// `case` and synthesize it. If `allowStandaloneStmtOrDeclRecovery` is `false`,
/// this recovery is disabled.
mutating func parseSwitchCases(allowStandaloneStmtRecovery: Bool) -> RawSwitchCaseListSyntax {
var elements = [RawSwitchCaseListSyntax.Element]()
var elementsProgress = LoopProgressCondition()
while !self.at(.endOfFile, .rightBrace) && !self.at(.poundEndif, .poundElseif, .poundElse)
&& self.hasProgressed(&elementsProgress)
{
if self.withLookahead({ $0.atStartOfSwitchCase(allowRecovery: false) }) {
elements.append(.switchCase(self.parseSwitchCase()))
} else if self.canRecoverTo(.poundIf) != nil {
// '#if' in 'case' position can enclose zero or more 'case' or 'default'
// clauses.
elements.append(
.ifConfigDecl(
self.parsePoundIfDirective(
{ (parser, _) in parser.parseSwitchCases(allowStandaloneStmtRecovery: allowStandaloneStmtRecovery) },
syntax: { parser, cases in
guard cases.count == 1, let firstCase = cases.first else {
precondition(cases.isEmpty)
return .switchCases(RawSwitchCaseListSyntax(elements: [], arena: parser.arena))
}
return .switchCases(firstCase)
}
)
)
)
} else if allowStandaloneStmtRecovery
&& (self.atStartOfExpression() || self.atStartOfStatement(preferExpr: false)
|| self.atStartOfDeclaration())
{
// Synthesize a label for the statement or declaration that isn't covered by a case right now.
let statements = parseSwitchCaseBody()
if statements.isEmpty {
break
}
elements.append(
.switchCase(
RawSwitchCaseSyntax(
attribute: nil,
label: .case(
RawSwitchCaseLabelSyntax(
caseKeyword: missingToken(.case),
caseItems: RawSwitchCaseItemListSyntax(
elements: [
RawSwitchCaseItemSyntax(
pattern: RawPatternSyntax(
RawIdentifierPatternSyntax(
identifier: missingToken(.identifier),
arena: self.arena
)
),
whereClause: nil,
trailingComma: nil,
arena: self.arena
)
],
arena: self.arena
),
colon: missingToken(.colon),
arena: self.arena
)
),
statements: statements,
arena: self.arena
)
)
)
} else if self.withLookahead({ $0.atStartOfSwitchCase(allowRecovery: true) }) {
elements.append(.switchCase(self.parseSwitchCase()))
} else {
break
}
}
return RawSwitchCaseListSyntax(elements: elements, arena: self.arena)
}
mutating func parseSwitchCaseBody() -> RawCodeBlockItemListSyntax {
parseCodeBlockItemList(until: {
$0.at(.rightBrace) || $0.at(.poundEndif, .poundElseif, .poundElse)
|| $0.withLookahead({ $0.atStartOfConditionalSwitchCases() })
})
}
/// Parse a single switch case clause.
mutating func parseSwitchCase() -> RawSwitchCaseSyntax {
var unknownAttr: RawAttributeSyntax?
if let at = self.consume(if: .atSign) {
let (unexpectedBeforeIdent, ident) = self.expectIdentifier()
unknownAttr = RawAttributeSyntax(
atSign: at,
unexpectedBeforeIdent,
attributeName: RawTypeSyntax(
RawIdentifierTypeSyntax(name: ident, genericArgumentClause: nil, arena: self.arena)
),
leftParen: nil,
arguments: nil,
rightParen: nil,
arena: self.arena
)
} else {
unknownAttr = nil
}
let label: RawSwitchCaseSyntax.Label
switch self.canRecoverTo(anyIn: SwitchCaseStart.self) {
case (.case, let handle)?:
label = .case(self.parseSwitchCaseLabel(handle))
case (.default, let handle)?:
label = .default(self.parseSwitchDefaultLabel(handle))
case nil:
label = .case(
RawSwitchCaseLabelSyntax(
caseKeyword: missingToken(.keyword(.case)),
caseItems: RawSwitchCaseItemListSyntax(
elements: [
RawSwitchCaseItemSyntax(
pattern: RawPatternSyntax(
RawIdentifierPatternSyntax(identifier: missingToken(.identifier), arena: self.arena)
),
whereClause: nil,
trailingComma: nil,
arena: self.arena
)
],
arena: self.arena
),
colon: missingToken(.colon),
arena: self.arena
)
)
}
// Parse the body.
let statements = parseSwitchCaseBody()
return RawSwitchCaseSyntax(
attribute: unknownAttr,
label: label,
statements: statements,
arena: self.arena
)
}
/// Parse a switch case with a 'case' label.
mutating func parseSwitchCaseLabel(
_ handle: RecoveryConsumptionHandle
) -> RawSwitchCaseLabelSyntax {
let (unexpectedBeforeCaseKeyword, caseKeyword) = self.eat(handle)
var caseItems = [RawSwitchCaseItemSyntax]()
do {
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
repeat {
let (pattern, whereClause) = self.parseGuardedCasePattern()
keepGoing = self.consume(if: .comma)
caseItems.append(
RawSwitchCaseItemSyntax(
pattern: pattern,
whereClause: whereClause,
trailingComma: keepGoing,
arena: self.arena
)
)
} while keepGoing != nil && self.hasProgressed(&loopProgress)
}
let (unexpectedBeforeColon, colon) = self.expect(.colon)
return RawSwitchCaseLabelSyntax(
unexpectedBeforeCaseKeyword,
caseKeyword: caseKeyword,
caseItems: RawSwitchCaseItemListSyntax(elements: caseItems, arena: self.arena),
unexpectedBeforeColon,
colon: colon,
arena: self.arena
)
}
/// Parse a switch case with a 'default' label.
mutating func parseSwitchDefaultLabel(
_ handle: RecoveryConsumptionHandle
) -> RawSwitchDefaultLabelSyntax {
let (unexpectedBeforeDefaultKeyword, defaultKeyword) = self.eat(handle)
let (unexpectedBeforeColon, colon) = self.expect(.colon)
return RawSwitchDefaultLabelSyntax(
unexpectedBeforeDefaultKeyword,
defaultKeyword: defaultKeyword,
unexpectedBeforeColon,
colon: colon,
arena: self.arena
)
}
/// Parse a pattern-matching clause for a case statement,
/// including the guard expression.
mutating func parseGuardedCasePattern() -> (RawPatternSyntax, RawWhereClauseSyntax?) {
let pattern = self.parseMatchingPattern(context: .matching)
// Parse the optional 'where' guard, with this particular pattern's bound
// vars in scope.
let whereClause: RawWhereClauseSyntax?
if let whereKeyword = self.consume(if: .keyword(.where)) {
let condition = self.parseExpression(flavor: .basic, pattern: .none)
whereClause = RawWhereClauseSyntax(
whereKeyword: whereKeyword,
condition: condition,
arena: self.arena
)
} else {
whereClause = nil
}
return (pattern, whereClause)
}
}
// MARK: Lookahead
extension Parser.Lookahead {
// Consume 'async', 'throws', and 'rethrows', but in any order.
mutating func consumeEffectsSpecifiers() {
var loopProgress = LoopProgressCondition()
while let (spec, handle) = self.at(anyIn: EffectSpecifier.self),
self.hasProgressed(&loopProgress)
{
self.eat(handle)
if spec.isThrowsSpecifier, self.consume(if: .leftParen) != nil {
_ = self.canParseSimpleOrCompositionType()
self.consume(if: .rightParen)
}
}
}
mutating func canParseClosureSignature() -> Bool {
// Consume attributes.
var lookahead = self.lookahead()
var attributesProgress = LoopProgressCondition()
while let _ = lookahead.consume(if: .atSign), lookahead.hasProgressed(&attributesProgress) {
guard lookahead.at(.identifier) else {
break
}
_ = lookahead.canParseCustomAttribute()
}
// Skip by a closure capture list if present.
if lookahead.consume(if: .leftSquare) != nil {
lookahead.skipUntil(.rightSquare, .rightSquare)
if lookahead.consume(if: .rightSquare) == nil {
return false
}
}
// Parse pattern-tuple func-signature-result? 'in'.
if lookahead.at(.leftParen) { // Consume the '('.
// While we don't have '->' or ')', eat balanced tokens.
var skipProgress = LoopProgressCondition()
while !lookahead.at(.endOfFile, .rightBrace, .keyword(.in)) && !lookahead.at(.arrow)
&& lookahead.hasProgressed(&skipProgress)
{
lookahead.skipSingle()
}
} else if lookahead.at(.identifier) || lookahead.at(.wildcard) {
// Parse identifier (',' identifier)*
lookahead.consumeAnyToken()
/// If the next token is a colon, interpret is as a type annotation and consume a type after it.
/// While type annotations aren’t allowed in shorthand closure parameters, we consume them to improve recovery.
func consumeOptionalTypeAnnotation() -> Bool {
if lookahead.consume(if: .colon) != nil {
return lookahead.canParseType()
} else {
return true
}
}
var parametersProgress = LoopProgressCondition()
while consumeOptionalTypeAnnotation() && lookahead.consume(if: .comma) != nil
&& lookahead.hasProgressed(¶metersProgress)
{
if lookahead.at(.identifier) || lookahead.at(.wildcard) {
lookahead.consumeAnyToken()
continue
}
return false
}
}
// Consume the ')', if it's there.
lookahead.consume(if: .rightParen)
lookahead.consumeEffectsSpecifiers()
// Parse the func-signature-result, if present.
if lookahead.consume(if: .arrow) != nil {
guard lookahead.canParseType() else {
return false
}
lookahead.consumeEffectsSpecifiers()
}
// Parse the 'in' at the end.
guard lookahead.at(.keyword(.in)) else {
return false
}
// Okay, we have a closure signature.
return true
}
}
extension Parser.Lookahead {
// Helper function to see if we can parse member reference like suffixes
// inside '#if'.
fileprivate mutating func atStartOfPostfixExprSuffix() -> Bool {
guard self.at(.period) else {
return false
}
if self.at(.integerLiteral) {
return true
}
return self.peek().isLexerClassifiedKeyword || TokenSpec(.identifier) ~= self.peek()
}
fileprivate func isInBindingPatternPosition() -> Bool {
// Cannot form a binding pattern if a generic argument list follows, this
// is something like 'case let E<Int>.foo(x)'.
if self.peek().isContextualPunctuator("<") {
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
return !lookahead.canParseAsGenericArgumentList()
}
switch self.peek().rawTokenKind {
// A '.' indicates a member access, '(' and '[' indicate a function call or
// subscript. We can't form a binding pattern as the base of these.
case .period, .leftParen, .leftSquare:
return false
default:
return true
}
}
}
extension SyntaxKind {
fileprivate var isLiteral: Bool {
switch self {
case .arrayExpr, .booleanLiteralExpr, .dictionaryExpr, .floatLiteralExpr, .integerLiteralExpr, .nilLiteralExpr,
.regexLiteralExpr, .stringLiteralExpr:
return true
default:
return false
}
}
}
private extension Parser.ExprFlavor {
/// The expression flavor used for the argument of a call that occurs
/// within a particularly-flavored expression.
var callArgumentFlavor: Parser.ExprFlavor {
switch self {
case .basic, .poundIfDirective, .stmtCondition: return .basic
case .attributeArguments: return .attributeArguments
}
}
}
|