package melange
Toolchain to produce JS from Reason/OCaml
Install
Dune Dependency
Authors
Maintainers
Sources
melange-5.1.0-51.tbz
sha256=0c817cbd92c42ac15fea6e6d975fd3eb3aa31f22b10cca22b3687ffed0e1b092
sha512=a0b85e22b106df7e9448d12977b740f735445640d4a9368591bdf5e67a95b93d5aacf957086c524549e3402faf0916c3f501ba43a631c74d8af4a7c986842c09
doc/src/melange.js_parser/expression_parser.ml.html
Source file expression_parser.ml
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
(* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. *) module Ast = Flow_ast open Token open Parser_env open Flow_ast open Parser_common open Comment_attachment module type EXPRESSION = sig val assignment : env -> (Loc.t, Loc.t) Expression.t val assignment_cover : env -> pattern_cover val conditional : env -> (Loc.t, Loc.t) Expression.t val is_assignable_lhs : (Loc.t, Loc.t) Expression.t -> bool val left_hand_side : env -> (Loc.t, Loc.t) Expression.t val number : env -> number_type -> string -> float val bigint : env -> bigint_type -> string -> int64 option val sequence : env -> start_loc:Loc.t -> (Loc.t, Loc.t) Expression.t list -> (Loc.t, Loc.t) Expression.t val call_type_args : env -> (Loc.t, Loc.t) Expression.CallTypeArgs.t option end module Expression (Parse : PARSER) (Type : Type_parser.TYPE) (Declaration : Declaration_parser.DECLARATION) (Pattern_cover : Pattern_cover.COVER) : EXPRESSION = struct type op_precedence = | Left_assoc of int | Right_assoc of int type group_cover = | Group_expr of (Loc.t, Loc.t) Expression.t | Group_typecast of (Loc.t, Loc.t) Expression.TypeCast.t let is_tighter a b = let a_prec = match a with | Left_assoc x -> x | Right_assoc x -> x - 1 in let b_prec = match b with | Left_assoc x -> x | Right_assoc x -> x in a_prec >= b_prec let is_assignable_lhs = let open Expression in function | ( _, MetaProperty { MetaProperty.meta = (_, { Identifier.name = "new"; comments = _ }); property = (_, { Identifier.name = "target"; comments = _ }); comments = _; } ) -> false | ( _, MetaProperty { MetaProperty.meta = (_, { Identifier.name = "import"; comments = _ }); property = (_, { Identifier.name = "meta"; comments = _ }); comments = _; } ) -> false (* #sec-static-semantics-static-semantics-isvalidsimpleassignmenttarget *) | (_, Array _) | (_, Identifier _) | (_, Member _) | (_, MetaProperty _) | (_, Object _) -> true | (_, ArrowFunction _) | (_, AsExpression _) | (_, Assignment _) | (_, Binary _) | (_, Call _) | (_, Class _) | (_, Conditional _) | (_, Function _) | (_, Import _) | (_, JSXElement _) | (_, JSXFragment _) | (_, StringLiteral _) | (_, BooleanLiteral _) | (_, NullLiteral _) | (_, NumberLiteral _) | (_, BigIntLiteral _) | (_, RegExpLiteral _) | (_, ModuleRefLiteral _) | (_, Logical _) | (_, New _) | (_, OptionalCall _) | (_, OptionalMember _) | (_, Sequence _) | (_, Super _) | (_, TaggedTemplate _) | (_, TemplateLiteral _) | (_, This _) | (_, TypeCast _) | (_, TSTypeCast _) | (_, Unary _) | (_, Update _) | (_, Yield _) -> false let as_expression = Pattern_cover.as_expression let as_pattern = Pattern_cover.as_pattern (* AssignmentExpression : * [+Yield] YieldExpression * ConditionalExpression * LeftHandSideExpression = AssignmentExpression * LeftHandSideExpression AssignmentOperator AssignmentExpression * ArrowFunctionFunction * * Originally we were parsing this without backtracking, but * ArrowFunctionExpression got too tricky. Oh well. *) let rec assignment_cover = let assignment_but_not_arrow_function_cover env = let start_loc = Peek.loc env in let expr_or_pattern = conditional_cover env in match assignment_op env with | Some operator -> let expr = with_loc ~start_loc (fun env -> let left = as_pattern env expr_or_pattern in let right = assignment env in Expression.(Assignment { Assignment.operator; left; right; comments = None })) env in Cover_expr expr | _ -> expr_or_pattern in let error_callback _ = function (* Don't rollback on these errors. *) | Parse_error.StrictReservedWord -> () (* Everything else causes a rollback *) | _ -> raise Try.Rollback (* So we may or may not be parsing the first part of an arrow function * (the part before the =>). We might end up parsing that whole thing or * we might end up parsing only part of it and thinking we're done. We * need to look at the next token to figure out if we really parsed an * assignment expression or if this is just the beginning of an arrow * function *) in let try_assignment_but_not_arrow_function env = let env = env |> with_error_callback error_callback in let ret = assignment_but_not_arrow_function_cover env in match Peek.token env with | T_ARROW -> (* x => 123 *) raise Try.Rollback | T_COLON when match last_token env with | Some T_RPAREN -> true | _ -> false -> (* (x): number => 123 *) raise Try.Rollback (* async x => 123 -- and we've already parsed async as an identifier * expression *) | _ when Peek.is_identifier env -> begin match ret with | Cover_expr (_, Expression.Identifier (_, { Identifier.name = "async"; comments = _ })) when not (Peek.is_line_terminator env) -> raise Try.Rollback | _ -> ret end | _ -> ret in fun env -> let is_identifier = Peek.is_identifier env && match Peek.token env with | T_AWAIT when allow_await env -> false | T_YIELD when allow_yield env -> false | _ -> true in match (Peek.token env, is_identifier) with | (T_YIELD, _) when allow_yield env -> Cover_expr (yield env) | ((T_LPAREN as t), _) | ((T_LESS_THAN as t), _) | ((T_THIS as t), _) | (t, true) -> (* Ok, we don't know if this is going to be an arrow function or a * regular assignment expression. Let's first try to parse it as an * assignment expression. If that fails we'll try an arrow function. * Unless it begins with `async <` in which case we first try parsing * it as an arrow function, and then an assignment expression. *) let (initial, secondary) = if t = T_ASYNC && should_parse_types env && Peek.ith_token ~i:1 env = T_LESS_THAN then (try_arrow_function, try_assignment_but_not_arrow_function) else (try_assignment_but_not_arrow_function, try_arrow_function) in (match Try.to_parse env initial with | Try.ParsedSuccessfully expr -> expr | Try.FailedToParse -> (match Try.to_parse env secondary with | Try.ParsedSuccessfully expr -> expr | Try.FailedToParse -> (* Well shoot. It doesn't parse cleanly as a normal * expression or as an arrow_function. Let's treat it as a * normal assignment expression gone wrong *) assignment_but_not_arrow_function_cover env)) | _ -> assignment_but_not_arrow_function_cover env and assignment env = as_expression env (assignment_cover env) and yield env = with_loc (fun env -> if in_formal_parameters env then error env Parse_error.YieldInFormalParameters; let leading = Peek.comments env in let start_loc = Peek.loc env in Expect.token env T_YIELD; let end_loc = Peek.loc env in let (argument, delegate) = if Peek.is_implicit_semicolon env then (None, false) else let delegate = Eat.maybe env T_MULT in let has_argument = match Peek.token env with | T_SEMICOLON | T_RBRACKET | T_RCURLY | T_RPAREN | T_COLON | T_COMMA -> false | _ -> true in let argument = if delegate || has_argument then Some (assignment env) else None in (argument, delegate) in let trailing = match argument with | None -> Eat.trailing_comments env | Some _ -> [] in let open Expression in Yield Yield. { argument; delegate; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing (); result_out = Loc.btwn start_loc end_loc; }) env and is_lhs = let open Expression in function | ( _, MetaProperty { MetaProperty.meta = (_, { Identifier.name = "new"; comments = _ }); property = (_, { Identifier.name = "target"; comments = _ }); comments = _; } ) -> false | ( _, MetaProperty { MetaProperty.meta = (_, { Identifier.name = "import"; comments = _ }); property = (_, { Identifier.name = "meta"; comments = _ }); comments = _; } ) -> false (* #sec-static-semantics-static-semantics-isvalidsimpleassignmenttarget *) | (_, Identifier _) | (_, Member _) | (_, MetaProperty _) -> true | (_, Array _) | (_, ArrowFunction _) | (_, AsExpression _) | (_, Assignment _) | (_, Binary _) | (_, Call _) | (_, Class _) | (_, Conditional _) | (_, Function _) | (_, Import _) | (_, JSXElement _) | (_, JSXFragment _) | (_, StringLiteral _) | (_, BooleanLiteral _) | (_, NullLiteral _) | (_, NumberLiteral _) | (_, BigIntLiteral _) | (_, RegExpLiteral _) | (_, ModuleRefLiteral _) | (_, Logical _) | (_, New _) | (_, Object _) | (_, OptionalCall _) | (_, OptionalMember _) | (_, Sequence _) | (_, Super _) | (_, TaggedTemplate _) | (_, TemplateLiteral _) | (_, This _) | (_, TypeCast _) | (_, TSTypeCast _) | (_, Unary _) | (_, Update _) | (_, Yield _) -> false and assignment_op env = let op = let open Expression.Assignment in match Peek.token env with | T_RSHIFT3_ASSIGN -> Some (Some RShift3Assign) | T_RSHIFT_ASSIGN -> Some (Some RShiftAssign) | T_LSHIFT_ASSIGN -> Some (Some LShiftAssign) | T_BIT_XOR_ASSIGN -> Some (Some BitXorAssign) | T_BIT_OR_ASSIGN -> Some (Some BitOrAssign) | T_BIT_AND_ASSIGN -> Some (Some BitAndAssign) | T_MOD_ASSIGN -> Some (Some ModAssign) | T_DIV_ASSIGN -> Some (Some DivAssign) | T_MULT_ASSIGN -> Some (Some MultAssign) | T_EXP_ASSIGN -> Some (Some ExpAssign) | T_MINUS_ASSIGN -> Some (Some MinusAssign) | T_PLUS_ASSIGN -> Some (Some PlusAssign) | T_NULLISH_ASSIGN -> Some (Some NullishAssign) | T_AND_ASSIGN -> Some (Some AndAssign) | T_OR_ASSIGN -> Some (Some OrAssign) | T_ASSIGN -> Some None | _ -> None in if op <> None then Eat.token env; op (* ConditionalExpression : * LogicalExpression * LogicalExpression ? AssignmentExpression : AssignmentExpression *) and conditional_cover env = let start_loc = Peek.loc env in let expr = logical_cover env in if Peek.token env = T_PLING then ( Eat.token env; (* no_in is ignored for the consequent *) let env' = env |> with_no_in false in let consequent = assignment env' in Expect.token env T_COLON; let (loc, alternate) = with_loc ~start_loc assignment env in Cover_expr ( loc, let open Expression in Conditional { Conditional.test = as_expression env expr; consequent; alternate; comments = None } ) ) else expr and conditional env = as_expression env (conditional_cover env) (* * LogicalANDExpression : * BinaryExpression * LogicalANDExpression && BitwiseORExpression * * LogicalORExpression : * LogicalANDExpression * LogicalORExpression || LogicalANDExpression * LogicalORExpression ?? LogicalANDExpression * * LogicalExpression : * LogicalORExpression *) and logical_cover = let open Expression in let make_logical env left right operator loc = let left = as_expression env left in let right = as_expression env right in Cover_expr (loc, Logical { Logical.operator; left; right; comments = None }) in let rec logical_and env left lloc = match Peek.token env with | T_AND -> Eat.token env; let (rloc, right) = with_loc binary_cover env in let loc = Loc.btwn lloc rloc in let left = make_logical env left right Logical.And loc in (* `a && b ?? c` is an error, but to recover, try to parse it like `(a && b) ?? c`. *) let (loc, left) = coalesce ~allowed:false env left loc in logical_and env left loc | _ -> (lloc, left) and logical_or env left lloc = match Peek.token env with | T_OR -> Eat.token env; let (rloc, right) = with_loc binary_cover env in let (rloc, right) = logical_and env right rloc in let loc = Loc.btwn lloc rloc in let left = make_logical env left right Logical.Or loc in (* `a || b ?? c` is an error, but to recover, try to parse it like `(a || b) ?? c`. *) let (loc, left) = coalesce ~allowed:false env left loc in logical_or env left loc | _ -> (lloc, left) and coalesce ~allowed env left lloc = match Peek.token env with | T_PLING_PLING -> if not allowed then error env (Parse_error.NullishCoalescingUnexpectedLogical "??"); Expect.token env T_PLING_PLING; let (rloc, right) = with_loc binary_cover env in let (rloc, right) = match Peek.token env with | (T_AND | T_OR) as t -> (* `a ?? b || c` is an error. To recover, treat it like `a ?? (b || c)`. *) error env (Parse_error.NullishCoalescingUnexpectedLogical (Token.value_of_token t)); let (rloc, right) = logical_and env right rloc in logical_or env right rloc | _ -> (rloc, right) in let loc = Loc.btwn lloc rloc in coalesce ~allowed:true env (make_logical env left right Logical.NullishCoalesce loc) loc | _ -> (lloc, left) in fun env -> let (loc, left) = with_loc binary_cover env in let (_, left) = match Peek.token env with | T_PLING_PLING -> coalesce ~allowed:true env left loc | _ -> let (loc, left) = logical_and env left loc in logical_or env left loc in left and binary_cover = let binary_op env = let ret = let open Expression.Binary in match Peek.token env with (* Most BinaryExpression operators are left associative *) (* Lowest pri *) | T_BIT_OR -> Some (BitOr, Left_assoc 2) | T_BIT_XOR -> Some (Xor, Left_assoc 3) | T_BIT_AND -> Some (BitAnd, Left_assoc 4) | T_EQUAL -> Some (Equal, Left_assoc 5) | T_STRICT_EQUAL -> Some (StrictEqual, Left_assoc 5) | T_NOT_EQUAL -> Some (NotEqual, Left_assoc 5) | T_STRICT_NOT_EQUAL -> Some (StrictNotEqual, Left_assoc 5) | T_LESS_THAN -> Some (LessThan, Left_assoc 6) | T_LESS_THAN_EQUAL -> Some (LessThanEqual, Left_assoc 6) | T_GREATER_THAN -> Some (GreaterThan, Left_assoc 6) | T_GREATER_THAN_EQUAL -> Some (GreaterThanEqual, Left_assoc 6) | T_IN -> if no_in env then None else Some (In, Left_assoc 6) | T_INSTANCEOF -> Some (Instanceof, Left_assoc 6) | T_LSHIFT -> Some (LShift, Left_assoc 7) | T_RSHIFT -> Some (RShift, Left_assoc 7) | T_RSHIFT3 -> Some (RShift3, Left_assoc 7) | T_PLUS -> Some (Plus, Left_assoc 8) | T_MINUS -> Some (Minus, Left_assoc 8) | T_MULT -> Some (Mult, Left_assoc 9) | T_DIV -> Some (Div, Left_assoc 9) | T_MOD -> Some (Mod, Left_assoc 9) | T_EXP -> Some (Exp, Right_assoc 10) (* Highest priority *) | _ -> None in if ret <> None then Eat.token env; ret in let make_binary left right operator loc = (loc, Expression.(Binary Binary.{ operator; left; right; comments = None })) in let rec add_to_stack right (rop, rpri) rloc = function | (left, (lop, lpri), lloc) :: rest when is_tighter lpri rpri -> let loc = Loc.btwn lloc rloc in let right = make_binary left right lop loc in add_to_stack right (rop, rpri) loc rest | stack -> (right, (rop, rpri), rloc) :: stack in let rec collapse_stack right rloc = function | [] -> right | (left, (lop, _), lloc) :: rest -> let loc = Loc.btwn lloc rloc in collapse_stack (make_binary left right lop loc) loc rest in let rec helper env stack = let (expr_loc, (is_unary, expr)) = with_loc (fun env -> let is_unary = peek_unary_op env <> None in let expr = unary_cover (env |> with_no_in false) in (is_unary, expr)) env in let next = Peek.token env in ( if next = T_LESS_THAN then match expr with | Cover_expr (_, Expression.JSXElement _) -> error env Parse_error.AdjacentJSXElements | _ -> () ); let (stack, expr) = let rec loop stack expr = match Peek.token env with | T_IDENTIFIER { raw = ("as" | "satisfies") as keyword; _ } when should_parse_types env -> Eat.token env; let expr = as_expression env expr in let (stack, expr) = match stack with | (left, (lop, lpri), lloc) :: rest when is_tighter lpri (Left_assoc 6) -> let expr_loc = Loc.btwn lloc expr_loc in let expr = make_binary left expr lop expr_loc in (rest, expr) | _ -> (stack, expr) in let (expr_loc, _) = expr in let expr = if keyword = "satisfies" then let ((annot_loc, _) as annot) = Type._type env in let loc = Loc.btwn expr_loc annot_loc in Cover_expr ( loc, Expression.TSTypeCast { Expression.TSTypeCast.expression = expr; kind = Expression.TSTypeCast.Satisfies annot; comments = None; } ) else if Peek.token env = T_CONST then ( let loc = Loc.btwn expr_loc (Peek.loc env) in Eat.token env; Cover_expr ( loc, Expression.TSTypeCast { Expression.TSTypeCast.expression = expr; kind = Expression.TSTypeCast.AsConst; comments = None; } ) ) else let ((annot_loc, _) as annot) = Type._type env in let loc = Loc.btwn expr_loc annot_loc in Cover_expr ( loc, Expression.AsExpression { Expression.AsExpression.expression = expr; annot = (annot_loc, annot); comments = None; } ) in loop stack expr | _ -> (stack, expr) in loop stack expr in match (stack, binary_op env) with | ([], None) -> expr | (_, None) -> let expr = as_expression env expr in Cover_expr (collapse_stack expr expr_loc stack) | (_, Some (rop, rpri)) -> if is_unary && rop = Expression.Binary.Exp then error_at env (expr_loc, Parse_error.InvalidLHSInExponentiation); let expr = as_expression env expr in helper env (add_to_stack expr (rop, rpri) expr_loc stack) in (fun env -> helper env []) and peek_unary_op env = let open Expression.Unary in match Peek.token env with | T_NOT -> Some Not | T_BIT_NOT -> Some BitNot | T_PLUS -> Some Plus | T_MINUS -> Some Minus | T_TYPEOF -> Some Typeof | T_VOID -> Some Void | T_DELETE -> Some Delete (* If we are in a unary expression context, and within an async function, * assume that a use of "await" is intended as a keyword, not an ordinary * identifier. This is a little bit inconsistent, since it can be used as * an identifier in other contexts (such as a variable name), but it's how * Babel does it. *) | T_AWAIT when allow_await env -> if in_formal_parameters env then error env Parse_error.AwaitInAsyncFormalParameters; Some Await | _ -> None and unary_cover env = let start_loc = Peek.loc env in let leading = Peek.comments env in let op = peek_unary_op env in match op with | None -> let op = let open Expression.Update in match Peek.token env with | T_INCR -> Some Increment | T_DECR -> Some Decrement | _ -> None in (match op with | None -> postfix_cover env | Some operator -> Eat.token env; let (loc, argument) = with_loc ~start_loc unary env in if not (is_lhs argument) then error_at env (fst argument, Parse_error.InvalidLHSInAssignment); (match argument with | (_, Expression.Identifier (_, { Identifier.name; comments = _ })) when is_restricted name -> strict_error env Parse_error.StrictLHSPrefix | _ -> ()); Cover_expr ( loc, Expression.( Update { Update.operator; prefix = true; argument; comments = Flow_ast_utils.mk_comments_opt ~leading (); } ) )) | Some operator -> Eat.token env; let (loc, argument) = with_loc ~start_loc unary env in let open Expression in (match (operator, argument) with | (Unary.Delete, (_, Identifier _)) -> strict_error_at env (loc, Parse_error.StrictDelete) | (Unary.Delete, (_, Member member)) -> begin match member.Ast.Expression.Member.property with | Ast.Expression.Member.PropertyPrivateName _ -> error_at env (loc, Parse_error.PrivateDelete) | _ -> () end | _ -> ()); Cover_expr ( loc, let open Expression in Unary { Unary.operator; argument; comments = Flow_ast_utils.mk_comments_opt ~leading () } ) and unary env = as_expression env (unary_cover env) and postfix_cover env = let argument = left_hand_side_cover env in (* No line terminator allowed before operator *) if Peek.is_line_terminator env then argument else let op = let open Expression.Update in match Peek.token env with | T_INCR -> Some Increment | T_DECR -> Some Decrement | _ -> None in match op with | None -> argument | Some operator -> let argument = as_expression env argument in if not (is_lhs argument) then error_at env (fst argument, Parse_error.InvalidLHSInAssignment); (match argument with | (_, Expression.Identifier (_, { Identifier.name; comments = _ })) when is_restricted name -> strict_error env Parse_error.StrictLHSPostfix | _ -> ()); let end_loc = Peek.loc env in Eat.token env; let trailing = Eat.trailing_comments env in let loc = Loc.btwn (fst argument) end_loc in Cover_expr ( loc, Expression.( Update { Update.operator; prefix = false; argument; comments = Flow_ast_utils.mk_comments_opt ~trailing (); } ) ) and left_hand_side_cover env = let start_loc = Peek.loc env in let allow_new = not (no_new env) in let env = with_no_new false env in let expr = match Peek.token env with | T_NEW when allow_new -> Cover_expr (new_expression env) | T_IMPORT -> Cover_expr (import env) | T_SUPER -> Cover_expr (super env) | _ when Peek.is_function env -> Cover_expr (_function env) | _ -> primary_cover env in call_cover env start_loc expr and left_hand_side env = as_expression env (left_hand_side_cover env) and super env = let (allowed, call_allowed) = match allow_super env with | No_super -> (false, false) | Super_prop -> (true, false) | Super_prop_or_call -> (true, true) in let loc = Peek.loc env in let leading = Peek.comments env in Expect.token env T_SUPER; let trailing = Eat.trailing_comments env in let super = ( loc, Expression.Super { Expression.Super.comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () } ) in match Peek.token env with | T_PERIOD | T_LBRACKET -> let super = if not allowed then ( error_at env (loc, Parse_error.UnexpectedSuper); (loc, Expression.Identifier (Flow_ast_utils.ident_of_source (loc, "super"))) ) else super in call env loc super | T_LPAREN -> let super = if not call_allowed then ( error_at env (loc, Parse_error.UnexpectedSuperCall); (loc, Expression.Identifier (Flow_ast_utils.ident_of_source (loc, "super"))) ) else super in call env loc super | _ -> if not allowed then error_at env (loc, Parse_error.UnexpectedSuper) else error_unexpected ~expected:"either a call or access of `super`" env; super and import env = with_loc (fun env -> let leading = Peek.comments env in let start_loc = Peek.loc env in Expect.token env T_IMPORT; if Eat.maybe env T_PERIOD then ( (* import.meta *) let import_ident = Flow_ast_utils.ident_of_source (start_loc, "import") in let meta_loc = Peek.loc env in Expect.identifier env "meta"; let meta_ident = Flow_ast_utils.ident_of_source (meta_loc, "meta") in let trailing = Eat.trailing_comments env in Expression.MetaProperty { Expression.MetaProperty.meta = import_ident; property = meta_ident; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing (); } ) else let leading_arg = Peek.comments env in Expect.token env T_LPAREN; let argument = add_comments (assignment (with_no_in false env)) ~leading:leading_arg in Expect.token env T_RPAREN; let trailing = Eat.trailing_comments env in Expression.Import { Expression.Import.argument; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing (); }) env and call_cover ?(allow_optional_chain = true) ?(in_optional_chain = false) env start_loc left = let left = member_cover ~allow_optional_chain ~in_optional_chain env start_loc left in let optional = match last_token env with | Some T_PLING_PERIOD -> true | _ -> false in let left_to_callee env = let { remove_trailing; _ } = trailing_and_remover env in remove_trailing (as_expression env left) (fun remover left -> remover#expression left) in let arguments ?targs env callee = let (args_loc, arguments) = arguments env in let loc = Loc.btwn start_loc args_loc in let call = { Expression.Call.callee; targs; arguments = (args_loc, arguments); comments = None } in let call = if optional || in_optional_chain then let open Expression in OptionalCall { OptionalCall.call; optional; filtered_out = loc } else Expression.Call call in let in_optional_chain = in_optional_chain || optional in call_cover ~allow_optional_chain ~in_optional_chain env start_loc (Cover_expr (loc, call)) in if no_call env then left else match Peek.token env with | T_LPAREN -> arguments env (left_to_callee env) | T_LSHIFT | T_LESS_THAN when should_parse_types env -> (* If we are parsing types, then f<T>(e) is a function call with a type application. If we aren't, it's a nested binary expression. *) let error_callback _ _ = raise Try.Rollback in let env = env |> with_error_callback error_callback in (* Parameterized call syntax is ambiguous, so we fall back to standard parsing if it fails. *) Try.or_else env ~fallback:left (fun env -> let callee = left_to_callee env in let targs = call_type_args env in arguments ?targs env callee ) | _ -> left and call ?(allow_optional_chain = true) env start_loc left = as_expression env (call_cover ~allow_optional_chain env start_loc (Cover_expr left)) and new_expression env = with_loc (fun env -> let start_loc = Peek.loc env in let leading = Peek.comments env in Expect.token env T_NEW; if in_function env && Peek.token env = T_PERIOD then ( let trailing = Eat.trailing_comments env in Eat.token env; let meta = Flow_ast_utils.ident_of_source (start_loc, "new") ?comments:(Flow_ast_utils.mk_comments_opt ~leading ~trailing ()) in match Peek.token env with | T_IDENTIFIER { raw = "target"; _ } -> let property = Parse.identifier env in Expression.(MetaProperty MetaProperty.{ meta; property; comments = None }) | _ -> error_unexpected ~expected:"the identifier `target`" env; Eat.token env; (* skip unknown identifier *) Expression.Identifier meta (* return `new` identifier *) ) else let callee_loc = Peek.loc env in let expr = match Peek.token env with | T_NEW -> new_expression env | T_SUPER -> super (env |> with_no_call true) | _ when Peek.is_function env -> _function env | _ -> primary env in let callee = member ~allow_optional_chain:false (env |> with_no_call true) callee_loc expr in (* You can do something like * new raw`42` *) let callee = let callee = match Peek.token env with | T_TEMPLATE_PART part -> tagged_template env callee_loc callee part | _ -> callee in (* Remove trailing comments if the callee is followed by args or type args *) if Peek.token env = T_LPAREN || (should_parse_types env && Peek.token env = T_LESS_THAN) then let { remove_trailing; _ } = trailing_and_remover env in remove_trailing callee (fun remover callee -> remover#expression callee) else callee in let targs = (* If we are parsing types, then new C<T>(e) is a constructor with a type application. If we aren't, it's a nested binary expression. *) if should_parse_types env then (* Parameterized call syntax is ambiguous, so we fall back to standard parsing if it fails. *) let error_callback _ _ = raise Try.Rollback in let env = env |> with_error_callback error_callback in Try.or_else env ~fallback:None call_type_args else None in let arguments = match Peek.token env with | T_LPAREN -> Some (arguments env) | _ -> None in let comments = Flow_ast_utils.mk_comments_opt ~leading () in Expression.(New New.{ callee; targs; arguments; comments })) env and call_type_args = let args = let rec args_helper env acc = match Peek.token env with | T_EOF | T_GREATER_THAN -> List.rev acc | _ -> let t = match Peek.token env with | T_IDENTIFIER { value = "_"; _ } -> let loc = Peek.loc env in let leading = Peek.comments env in Expect.identifier env "_"; let trailing = Eat.trailing_comments env in Expression.CallTypeArg.Implicit ( loc, { Expression.CallTypeArg.Implicit.comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing (); } ) | _ -> Expression.CallTypeArg.Explicit (Type._type env) in let acc = t :: acc in if Peek.token env <> T_GREATER_THAN then Expect.token env T_COMMA; args_helper env acc in fun env -> let leading = Peek.comments env in Expect.token env T_LESS_THAN; let arguments = args_helper env [] in let internal = Peek.comments env in Expect.token env T_GREATER_THAN; let trailing = if Peek.token env = T_LPAREN then let { trailing; _ } = trailing_and_remover env in trailing else Eat.trailing_comments env in { Expression.CallTypeArgs.arguments; comments = Flow_ast_utils.mk_comments_with_internal_opt ~leading ~trailing ~internal (); } in fun env -> Eat.push_lex_mode env Lex_mode.TYPE; let node = if Peek.token env = T_LESS_THAN then Some (with_loc args env) else None in Eat.pop_lex_mode env; node and arguments = let spread_element env = let leading = Peek.comments env in Expect.token env T_ELLIPSIS; let argument = assignment env in Expression.SpreadElement.{ argument; comments = Flow_ast_utils.mk_comments_opt ~leading () } in let argument env = match Peek.token env with | T_ELLIPSIS -> Expression.Spread (with_loc spread_element env) | _ -> Expression.Expression (assignment env) in let rec arguments' env acc = match Peek.token env with | T_EOF | T_RPAREN -> List.rev acc | _ -> let acc = argument env :: acc in if Peek.token env <> T_RPAREN then Expect.token env T_COMMA; arguments' env acc in fun env -> with_loc (fun env -> let leading = Peek.comments env in Expect.token env T_LPAREN; let args = arguments' env [] in let internal = Peek.comments env in Expect.token env T_RPAREN; let trailing = Eat.trailing_comments env in { Expression.ArgList.arguments = args; comments = Flow_ast_utils.mk_comments_with_internal_opt ~leading ~trailing ~internal (); }) env and member_cover = let dynamic ?(allow_optional_chain = true) ?(in_optional_chain = false) ?(optional = false) env start_loc left = let expr = Parse.expression (env |> with_no_call false) in let last_loc = Peek.loc env in Expect.token env T_RBRACKET; let trailing = Eat.trailing_comments env in let loc = Loc.btwn start_loc last_loc in let member = { Expression.Member._object = as_expression env left; property = Expression.Member.PropertyExpression expr; comments = Flow_ast_utils.mk_comments_opt ~trailing (); } in let member = if in_optional_chain then let open Expression in OptionalMember { OptionalMember.member; optional; filtered_out = loc } else Expression.Member member in call_cover ~allow_optional_chain ~in_optional_chain env start_loc (Cover_expr (loc, member)) in let static ?(allow_optional_chain = true) ?(in_optional_chain = false) ?(optional = false) env start_loc left = let open Expression.Member in let (id_loc, property) = match Peek.token env with | T_POUND -> let ((id_loc, { Ast.PrivateName.name; _ }) as id) = private_identifier env in add_used_private env name id_loc; (id_loc, PropertyPrivateName id) | _ -> let ((id_loc, _) as id) = identifier_name env in (id_loc, PropertyIdentifier id) in let loc = Loc.btwn start_loc id_loc in (* super.PrivateName is a syntax error *) begin match (left, property) with | (Cover_expr (_, Ast.Expression.Super _), PropertyPrivateName _) -> error_at env (loc, Parse_error.SuperPrivate) | _ -> () end; let member = Expression.Member.{ _object = as_expression env left; property; comments = None } in let member = if in_optional_chain then let open Expression in OptionalMember { OptionalMember.member; optional; filtered_out = loc } else Expression.Member member in call_cover ~allow_optional_chain ~in_optional_chain env start_loc (Cover_expr (loc, member)) in fun ?(allow_optional_chain = true) ?(in_optional_chain = false) env start_loc left -> match Peek.token env with | T_PLING_PERIOD -> if not allow_optional_chain then error env Parse_error.OptionalChainNew; Expect.token env T_PLING_PERIOD; begin match Peek.token env with | T_TEMPLATE_PART _ -> error env Parse_error.OptionalChainTemplate; left | T_LPAREN -> left | T_LESS_THAN when should_parse_types env -> left | T_LBRACKET -> Eat.token env; dynamic ~allow_optional_chain ~in_optional_chain:true ~optional:true env start_loc left | _ -> static ~allow_optional_chain ~in_optional_chain:true ~optional:true env start_loc left end | T_LBRACKET -> Eat.token env; dynamic ~allow_optional_chain ~in_optional_chain env start_loc left | T_PERIOD -> Eat.token env; static ~allow_optional_chain ~in_optional_chain env start_loc left | T_TEMPLATE_PART part -> if in_optional_chain then error env Parse_error.OptionalChainTemplate; let expr = tagged_template env start_loc (as_expression env left) part in call_cover ~allow_optional_chain:true env start_loc (Cover_expr expr) | _ -> left and member ?(allow_optional_chain = true) env start_loc left = as_expression env (member_cover ~allow_optional_chain env start_loc (Cover_expr left)) and _function env = with_loc (fun env -> let (async, leading_async) = Declaration.async env in let (sig_loc, (id, params, generator, predicate, return, tparams, leading)) = with_loc (fun env -> let leading_function = Peek.comments env in Expect.token env T_FUNCTION; let (generator, leading_generator) = Declaration.generator env in let leading = List.concat [leading_async; leading_function; leading_generator] in (* `await` is a keyword in async functions: - proposal-async-iteration/#prod-AsyncGeneratorExpression - #prod-AsyncFunctionExpression *) let await = async in (* `yield` is a keyword in generator functions: - proposal-async-iteration/#prod-AsyncGeneratorExpression - #prod-GeneratorExpression *) let yield = generator in let (id, tparams) = if Peek.token env = T_LPAREN then (None, None) else let id = match Peek.token env with | T_LESS_THAN -> None | _ -> let env = env |> with_allow_await await |> with_allow_yield yield in let id = id_remove_trailing env (Parse.identifier ~restricted_error:Parse_error.StrictFunctionName env) in Some id in let tparams = type_params_remove_trailing env (Type.type_params env) in (id, tparams) in (* #sec-function-definitions-static-semantics-early-errors *) let env = env |> with_allow_super No_super in let params = (* await is a keyword if *this* is an async function, OR if it's already a keyword in the current scope (e.g. if this is a non-async function nested in an async function). *) let await = await || allow_await env in let params = Declaration.function_params ~await ~yield env in if Peek.token env = T_COLON then params else function_params_remove_trailing env params in let (return, predicate) = Type.function_return_annotation_and_predicate_opt env in let (return, predicate) = match predicate with | None -> (return_annotation_remove_trailing env return, predicate) | Some _ -> (return, predicate_remove_trailing env predicate) in (id, params, generator, predicate, return, tparams, leading)) env in let simple_params = is_simple_parameter_list params in let (body, contains_use_strict) = Declaration.function_body env ~async ~generator ~expression:true ~simple_params in Declaration.strict_function_post_check env ~contains_use_strict id params; Expression.Function { Function.id; params; body; generator; async; predicate; return; tparams; sig_loc; comments = Flow_ast_utils.mk_comments_opt ~leading (); }) env and number env kind raw = let value = match kind with | LEGACY_OCTAL -> strict_error env Parse_error.StrictOctalLiteral; begin try Int64.to_float (Int64.of_string ("0o" ^ raw)) with | Failure _ -> failwith ("Invalid legacy octal " ^ raw) end | LEGACY_NON_OCTAL -> strict_error env Parse_error.StrictNonOctalLiteral; begin try float_of_string raw with | Failure _ -> failwith ("Invalid number " ^ raw) end | BINARY | OCTAL -> begin try Int64.to_float (Int64.of_string raw) with | Failure _ -> failwith ("Invalid binary/octal " ^ raw) end | NORMAL -> begin try float_of_string raw with | Failure _ -> failwith ("Invalid number " ^ raw) end in Expect.token env (T_NUMBER { kind; raw }); value and bigint_strip_n raw = let size = String.length raw in let str = if size != 0 && raw.[size - 1] == 'n' then String.sub raw 0 (size - 1) else raw in str and bigint env kind raw = let postraw = bigint_strip_n raw in let value = Int64.of_string_opt postraw in Expect.token env (T_BIGINT { kind; raw }); value and primary_cover env = let loc = Peek.loc env in let leading = Peek.comments env in match Peek.token env with | T_THIS -> Eat.token env; let trailing = Eat.trailing_comments env in Cover_expr ( loc, Expression.This { Expression.This.comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () } ) | T_NUMBER { kind; raw } -> let value = number env kind raw in let trailing = Eat.trailing_comments env in let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in Cover_expr (loc, Expression.NumberLiteral { Ast.NumberLiteral.value; raw; comments }) | T_BIGINT { kind; raw } -> let value = bigint env kind raw in let trailing = Eat.trailing_comments env in let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in Cover_expr (loc, Expression.BigIntLiteral { Ast.BigIntLiteral.value; raw; comments }) | T_STRING (loc, value, raw, octal) -> if octal then strict_error env Parse_error.StrictOctalLiteral; Eat.token env; let trailing = Eat.trailing_comments env in let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in let expr = let opts = parse_options env in match (opts.module_ref_prefix, opts.module_ref_prefix_LEGACY_INTEROP) with | (Some prefix, _) when String.starts_with ~prefix value -> let prefix_len = String.length prefix in Expression.ModuleRefLiteral { Ast.ModuleRefLiteral.value; require_out = loc; prefix_len; legacy_interop = false; raw; comments; } | (_, Some prefix) when String.starts_with ~prefix value -> let prefix_len = String.length prefix in Expression.ModuleRefLiteral { Ast.ModuleRefLiteral.value; require_out = loc; prefix_len; legacy_interop = true; raw; comments; } | _ -> Expression.StringLiteral { Ast.StringLiteral.value; raw; comments } in Cover_expr (loc, expr) | (T_TRUE | T_FALSE) as token -> Eat.token env; let value = token = T_TRUE in let trailing = Eat.trailing_comments env in let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in Cover_expr (loc, Expression.BooleanLiteral { Ast.BooleanLiteral.value; comments }) | T_NULL -> Eat.token env; let trailing = Eat.trailing_comments env in let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in Cover_expr (loc, Expression.NullLiteral comments) | T_LPAREN -> Cover_expr (group env) | T_LCURLY -> let (loc, obj, errs) = Parse.object_initializer env in Cover_patt ((loc, Expression.Object obj), errs) | T_LBRACKET -> let (loc, (arr, errs)) = with_loc array_initializer env in Cover_patt ((loc, Expression.Array arr), errs) | T_DIV | T_DIV_ASSIGN -> Cover_expr (regexp env) | T_LESS_THAN -> let (loc, expression) = match Parse.jsx_element_or_fragment env with | (loc, `Element e) -> (loc, Expression.JSXElement e) | (loc, `Fragment f) -> (loc, Expression.JSXFragment f) in Cover_expr (loc, expression) | T_TEMPLATE_PART part -> let (loc, template) = template_literal env part in Cover_expr (loc, Expression.TemplateLiteral template) | T_CLASS -> Cover_expr (Parse.class_expression env) | T_IDENTIFIER { raw = "abstract"; _ } when Peek.ith_token ~i:1 env = T_CLASS -> Cover_expr (Parse.class_expression env) | _ when Peek.is_identifier env -> let id = Parse.identifier env in Cover_expr (fst id, Expression.Identifier id) | t -> error_unexpected env; (* Let's get rid of the bad token *) begin match t with | T_ERROR _ -> Eat.token env | _ -> () end; (* Really no idea how to recover from this. I suppose a null * expression is as good as anything *) let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing:[] () in Cover_expr (loc, Expression.NullLiteral comments) and primary env = as_expression env (primary_cover env) and template_literal = let rec template_parts env quasis expressions = let expr = Parse.expression env in let expressions = expr :: expressions in match Peek.token env with | T_RCURLY -> Eat.push_lex_mode env Lex_mode.TEMPLATE; let (loc, part, is_tail) = match Peek.token env with | T_TEMPLATE_PART (loc, cooked, raw, _, tail) -> let open Ast.Expression.TemplateLiteral in Eat.token env; (loc, { Element.value = { Element.cooked; raw }; tail }, tail) | _ -> assert false in Eat.pop_lex_mode env; let quasis = (loc, part) :: quasis in if is_tail then (loc, List.rev quasis, List.rev expressions) else template_parts env quasis expressions | _ -> (* Malformed template *) error_unexpected ~expected:"a template literal part" env; let imaginary_quasi = ( fst expr, { Expression.TemplateLiteral.Element.value = { Expression.TemplateLiteral.Element.raw = ""; cooked = "" }; tail = true; } ) in (fst expr, List.rev (imaginary_quasi :: quasis), List.rev expressions) in fun env ((start_loc, cooked, raw, _, is_tail) as part) -> let leading = Peek.comments env in Expect.token env (T_TEMPLATE_PART part); let (end_loc, quasis, expressions) = let head = ( start_loc, { Ast.Expression.TemplateLiteral.Element.value = { Ast.Expression.TemplateLiteral.Element.cooked; raw }; tail = is_tail; } ) in if is_tail then (start_loc, [head], []) else template_parts env [head] [] in let trailing = Eat.trailing_comments env in let loc = Loc.btwn start_loc end_loc in ( loc, { Expression.TemplateLiteral.quasis; expressions; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing (); } ) and tagged_template env start_loc tag part = let tag = expression_remove_trailing env tag in let quasi = template_literal env part in ( Loc.btwn start_loc (fst quasi), Expression.(TaggedTemplate TaggedTemplate.{ tag; quasi; comments = None }) ) and group env = let leading = Peek.comments env in let (loc, cover) = with_loc (fun env -> Expect.token env T_LPAREN; let expr_start_loc = Peek.loc env in let expression = assignment env in let ret = match Peek.token env with | T_COLON -> let annot = Type.annotation env in Group_typecast Expression.TypeCast.{ expression; annot; comments = None } | T_COMMA -> Group_expr (sequence env ~start_loc:expr_start_loc [expression]) | _ -> Group_expr expression in Expect.token env T_RPAREN; ret) env in let trailing = Eat.trailing_comments env in let ret = match cover with | Group_expr expr -> expr | Group_typecast cast -> (loc, Expression.TypeCast cast) in add_comments ret ~leading ~trailing and add_comments ?(leading = []) ?(trailing = []) (loc, expression) = let merge_comments inner = Flow_ast_utils.merge_comments ~inner ~outer:(Flow_ast_utils.mk_comments_opt ~leading ~trailing ()) in let merge_comments_with_internal inner = Flow_ast_utils.merge_comments_with_internal ~inner ~outer:(Flow_ast_utils.mk_comments_opt ~leading ~trailing ()) in let open Expression in ( loc, match expression with | Array ({ Array.comments; _ } as e) -> Array { e with Array.comments = merge_comments_with_internal comments } | ArrowFunction ({ Function.comments; _ } as e) -> ArrowFunction { e with Function.comments = merge_comments comments } | Assignment ({ Assignment.comments; _ } as e) -> Assignment { e with Assignment.comments = merge_comments comments } | Binary ({ Binary.comments; _ } as e) -> Binary { e with Binary.comments = merge_comments comments } | Call ({ Call.comments; _ } as e) -> Call { e with Call.comments = merge_comments comments } | Class ({ Class.comments; _ } as e) -> Class { e with Class.comments = merge_comments comments } | Conditional ({ Conditional.comments; _ } as e) -> Conditional { e with Conditional.comments = merge_comments comments } | Function ({ Function.comments; _ } as e) -> Function { e with Function.comments = merge_comments comments } | Identifier (loc, ({ Identifier.comments; _ } as e)) -> Identifier (loc, { e with Identifier.comments = merge_comments comments }) | Import ({ Import.comments; _ } as e) -> Import { e with Import.comments = merge_comments comments } | JSXElement ({ JSX.comments; _ } as e) -> JSXElement { e with JSX.comments = merge_comments comments } | JSXFragment ({ JSX.frag_comments; _ } as e) -> JSXFragment { e with JSX.frag_comments = merge_comments frag_comments } | StringLiteral ({ StringLiteral.comments; _ } as e) -> StringLiteral { e with StringLiteral.comments = merge_comments comments } | BooleanLiteral ({ BooleanLiteral.comments; _ } as e) -> BooleanLiteral { e with BooleanLiteral.comments = merge_comments comments } | NullLiteral comments -> NullLiteral (merge_comments comments) | NumberLiteral ({ NumberLiteral.comments; _ } as e) -> NumberLiteral { e with NumberLiteral.comments = merge_comments comments } | BigIntLiteral ({ BigIntLiteral.comments; _ } as e) -> BigIntLiteral { e with BigIntLiteral.comments = merge_comments comments } | RegExpLiteral ({ RegExpLiteral.comments; _ } as e) -> RegExpLiteral { e with RegExpLiteral.comments = merge_comments comments } | ModuleRefLiteral ({ ModuleRefLiteral.comments; _ } as e) -> ModuleRefLiteral { e with ModuleRefLiteral.comments = merge_comments comments } | Logical ({ Logical.comments; _ } as e) -> Logical { e with Logical.comments = merge_comments comments } | Member ({ Member.comments; _ } as e) -> Member { e with Member.comments = merge_comments comments } | MetaProperty ({ MetaProperty.comments; _ } as e) -> MetaProperty { e with MetaProperty.comments = merge_comments comments } | New ({ New.comments; _ } as e) -> New { e with New.comments = merge_comments comments } | Object ({ Object.comments; _ } as e) -> Object { e with Object.comments = merge_comments_with_internal comments } | OptionalCall ({ OptionalCall.call = { Call.comments; _ } as call; _ } as optional_call) -> OptionalCall { optional_call with OptionalCall.call = { call with Call.comments = merge_comments comments }; } | OptionalMember ({ OptionalMember.member = { Member.comments; _ } as member; _ } as optional_member) -> OptionalMember { optional_member with OptionalMember.member = { member with Member.comments = merge_comments comments }; } | Sequence ({ Sequence.comments; _ } as e) -> Sequence { e with Sequence.comments = merge_comments comments } | Super { Super.comments; _ } -> Super { Super.comments = merge_comments comments } | TaggedTemplate ({ TaggedTemplate.comments; _ } as e) -> TaggedTemplate { e with TaggedTemplate.comments = merge_comments comments } | TemplateLiteral ({ TemplateLiteral.comments; _ } as e) -> TemplateLiteral { e with TemplateLiteral.comments = merge_comments comments } | This { This.comments; _ } -> This { This.comments = merge_comments comments } | TypeCast ({ TypeCast.comments; _ } as e) -> TypeCast { e with TypeCast.comments = merge_comments comments } | Unary ({ Unary.comments; _ } as e) -> Unary { e with Unary.comments = merge_comments comments } | Update ({ Update.comments; _ } as e) -> Update { e with Update.comments = merge_comments comments } | Yield ({ Yield.comments; _ } as e) -> Yield { e with Yield.comments = merge_comments comments } (* TODO: Delete once all expressions support comment attachment *) | _ -> expression ) and array_initializer = let rec elements env (acc, errs) = match Peek.token env with | T_EOF | T_RBRACKET -> (List.rev acc, Pattern_cover.rev_errors errs) | T_COMMA -> let loc = Peek.loc env in Eat.token env; elements env (Expression.Array.Hole loc :: acc, errs) | T_ELLIPSIS -> let leading = Peek.comments env in let (loc, (argument, new_errs)) = with_loc (fun env -> Eat.token env; match assignment_cover env with | Cover_expr argument -> (argument, Pattern_cover.empty_errors) | Cover_patt (argument, new_errs) -> (argument, new_errs)) env in let elem = Expression.( Array.Spread ( loc, SpreadElement.{ argument; comments = Flow_ast_utils.mk_comments_opt ~leading () } ) ) in let is_last = Peek.token env = T_RBRACKET in (* if this array is interpreted as a pattern, the spread becomes an AssignmentRestElement which must be the last element. We can easily error about additional elements since they will be in the element list, but a trailing elision, like `[...x,]`, is not part of the AST. so, keep track of the error so we can raise it if this is a pattern. *) let new_errs = if (not is_last) && Peek.ith_token ~i:1 env = T_RBRACKET then let if_patt = (loc, Parse_error.ElementAfterRestElement) :: new_errs.if_patt in { new_errs with if_patt } else new_errs in if not is_last then Expect.token env T_COMMA; let acc = elem :: acc in let errs = Pattern_cover.rev_append_errors new_errs errs in elements env (acc, errs) | _ -> let (elem, new_errs) = match assignment_cover env with | Cover_expr elem -> (elem, Pattern_cover.empty_errors) | Cover_patt (elem, new_errs) -> (elem, new_errs) in if Peek.token env <> T_RBRACKET then Expect.token env T_COMMA; let acc = Expression.Array.Expression elem :: acc in let errs = Pattern_cover.rev_append_errors new_errs errs in elements env (acc, errs) in fun env -> let leading = Peek.comments env in Expect.token env T_LBRACKET; let (elems, errs) = elements env ([], Pattern_cover.empty_errors) in let internal = Peek.comments env in Expect.token env T_RBRACKET; let trailing = Eat.trailing_comments env in ( { Ast.Expression.Array.elements = elems; comments = Flow_ast_utils.mk_comments_with_internal_opt ~leading ~trailing ~internal (); }, errs ) and regexp env = Eat.push_lex_mode env Lex_mode.REGEXP; let loc = Peek.loc env in let leading = Peek.comments env in let tkn = Peek.token env in let (raw, pattern, raw_flags, trailing) = match tkn with | T_REGEXP (_, pattern, flags) -> Eat.token env; let trailing = Eat.trailing_comments env in let raw = "/" ^ pattern ^ "/" ^ flags in (raw, pattern, flags, trailing) | _ -> error_unexpected ~expected:"a regular expression" env; ("", "", "", []) in Eat.pop_lex_mode env; let filtered_flags = Buffer.create (String.length raw_flags) in String.iter (function | ('d' | 'g' | 'i' | 'm' | 's' | 'u' | 'y') as c -> Buffer.add_char filtered_flags c | _ -> ()) raw_flags; let flags = Buffer.contents filtered_flags in if flags <> raw_flags then error env (Parse_error.InvalidRegExpFlags raw_flags); let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in (loc, Expression.RegExpLiteral { Ast.RegExpLiteral.pattern; flags; raw; comments }) and try_arrow_function = (* Certain errors (almost all errors) cause a rollback *) let error_callback _ = Parse_error.( function (* Don't rollback on these errors. *) | StrictParamDupe | StrictParamName | StrictReservedWord | ParameterAfterRestParameter | NewlineBeforeArrow | AwaitAsIdentifierReference | AwaitInAsyncFormalParameters | YieldInFormalParameters | ThisParamBannedInArrowFunctions -> () (* Everything else causes a rollback *) | _ -> raise Try.Rollback ) in let concise_function_body env = match Peek.token env with | T_LCURLY -> let (body_block, contains_use_strict) = Parse.function_block_body env ~expression:true in (Function.BodyBlock body_block, contains_use_strict) | _ -> let expr = Parse.assignment env in (Function.BodyExpression expr, false) in fun env -> let env = env |> with_error_callback error_callback in let start_loc = Peek.loc env in (* a T_ASYNC could either be a parameter name or it could be indicating * that it's an async function *) let (async, leading) = if Peek.ith_token ~i:1 env <> T_ARROW then Declaration.async env else (false, []) in (* await is a keyword if this is an async function, or if we're in one already. *) let await = async || allow_await env in let env = with_allow_await await env in let yield = allow_yield env in let (sig_loc, (tparams, params, return, predicate)) = with_loc (fun env -> let tparams = type_params_remove_trailing env (Type.type_params env) in (* Disallow all fancy features for identifier => body *) if Peek.is_identifier env && tparams = None then let ((loc, _) as name) = Parse.identifier ~restricted_error:Parse_error.StrictParamName env in let param = ( loc, { Ast.Function.Param.argument = ( loc, Pattern.Identifier { Pattern.Identifier.name; annot = Ast.Type.Missing (Peek.loc_skip_lookahead env); optional = false; } ); default = None; } ) in ( tparams, ( loc, { Ast.Function.Params.params = [param]; rest = None; comments = None; this_ = None; } ), Ast.Function.ReturnAnnot.Missing Loc.{ loc with start = loc._end }, None ) else let params = Declaration.function_params ~await ~yield env in (* https://tc39.es/ecma262/#prod-ArrowFormalParameters *) Declaration.check_unique_formal_parameters env params; (* There's an ambiguity if you use a function type as the return * type for an arrow function. So we disallow anonymous function * types in arrow function return types unless the function type is * enclosed in parens *) let (return, predicate) = env |> with_no_anon_function_type true |> Type.function_return_annotation_and_predicate_opt in (tparams, params, return, predicate)) env in (* It's hard to tell if an invalid expression was intended to be an * arrow function before we see the =>. If there are no params, that * implies "()" which is only ever found in arrow params. Similarly, * rest params indicate arrow functions. Therefore, if we see a rest * param or an empty param list then we can disable the rollback and * instead generate errors as if we were parsing an arrow function *) let env = match params with | (_, { Ast.Function.Params.params = _; rest = Some _; this_ = None; comments = _ }) | (_, { Ast.Function.Params.params = []; rest = _; this_ = None; comments = _ }) -> without_error_callback env | _ -> env in (* Disallow this param annotations in arrow functions *) let params = match params with | (loc, ({ Ast.Function.Params.this_ = Some (this_loc, _); _ } as params)) -> error_at env (this_loc, Parse_error.ThisParamBannedInArrowFunctions); (loc, { params with Ast.Function.Params.this_ = None }) | _ -> params in let simple_params = is_simple_parameter_list params in if Peek.is_line_terminator env && Peek.token env = T_ARROW then error env Parse_error.NewlineBeforeArrow; Expect.token env T_ARROW; (* Now we know for sure this is an arrow function *) let env = without_error_callback env in (* arrow functions can't be generators *) let env = enter_function env ~async ~generator:false ~simple_params in let (end_loc, (body, contains_use_strict)) = with_loc concise_function_body env in Declaration.strict_function_post_check env ~contains_use_strict None params; let loc = Loc.btwn start_loc end_loc in Cover_expr ( loc, let open Expression in ArrowFunction { Function.id = None; params; body; async; generator = false; (* arrow functions cannot be generators *) predicate; return; tparams; sig_loc; comments = Flow_ast_utils.mk_comments_opt ~leading (); } ) and sequence = let rec helper acc env = match Peek.token env with | T_COMMA -> Eat.token env; let expr = assignment env in helper (expr :: acc) env | _ -> let expressions = List.rev acc in Expression.(Sequence Sequence.{ expressions; comments = None }) in (fun env ~start_loc acc -> with_loc ~start_loc (helper acc) env) end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>