All files index.js

100% Statements 240/240
94.74% Branches 18/19
100% Functions 126/126
100% Lines 236/236

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 11721x 1x 1x 1x   1x 124x 1435x 1435x 1435x 2870x 2816x 2804x   1435x             1435x     1x   1x                                       1x 1x               1x 1x                   1x             45x 45x                       25x 25x           81x 81x           5x 5x 5x 5x         36x 36x                   26x 26x 1x 1x 1x 1x 1x 1x 1x   25x                           36x                                     29x           36x                     1x           36x                 4x 4x       36x 36x                     21x         21x               26x         44x 44x     26x 26x           24x 24x 24x 24x         2x 2x 2x             3x 3x     1x 1x                           1x     3x     2x     27x     6x     2x     6x             41x                                       19x                           20x                               1x                               4x                           5x                         1x                           8x                     9x                   2x                   11x 11x           6x                 5x 5x                 5x   11x                   6x 6x     5x 5x 5x             5x 5x 5x           5x 5x           3x                         2x 2x                             3x                   3x 3x                                 16x       1x               22x 22x       16x 16x 16x               8x               2x                       8x 8x             8x       1x                   1x 1x 1x     2x 2x 2x                 6x 6x             6x       1x                     7x 7x 7x     6x 6x 4x 4x   2x 2x 2x             2x               13x         13x                   4x                 22x   2x       1x       22x     6x           22x   4x 4x 4x       2x         1x         22x   6x                 1x 1x 1x         1x                           2x       1x               4x 4x       2x 2x 2x                     26x       2x   2x         26x 26x 26x               10x         10x 10x             24x 24x       10x 10x                 9x       1x   1x         9x 9x 9x               7x             7x 7x 7x               79x   8x                                     8x 8x 8x           2x         4x                 17x               14x                                                         4x               1x                             4x       4x             5x 5x       4x 4x 4x         4x 4x 4x         4x         4x 4x 4x               4x             4x 4x 4x                 50x     97x 97x       7x   90x                                                                         140x 1x 146x     140x     140x 140x 140x                                                       1x                          
const { rule, bean, map, mapfn, ignore, merge } = require("../../index");
const { lPluck } = require("../../index");
const lex = require("./lexer");
const types = require("./types");
 
const wrap = (fn, priority) =>
  rule((lhs, rhs) => {
    const result = fn(lhs, rhs);
    result.lambda = result.lambda || [];
    for (const it of [lhs, rhs]) {
      if (it.type == "wrapped") continue;
      if (it.type == "parameter") result.lambda.push(it);
      else if (it.lambda?.length) result.lambda.push(...it.lambda);
    }
    result.meta = {
      location: rhs.meta?.location || {
        file: rhs.file,
        line: rhs.line,
        column: rhs.column,
      },
    };
    return result;
  }, priority);
 
const ranges = ["range", "rangeFull", "rangeBy", "byRange", "ranger"];
 
const values = [
  "symbol",
  "parameter",
  "number",
  "array",
  "string",
  "formattedString",
  "hashmap",
  "propertyAccess",
  ...ranges,
  "slice",
  "set",
  "wrapped",
  "awaited",
  "parallelFn",
  "null",
  "true",
  "false",
  "inCheck",
];
const arrayLike = ["array", "symbol", "propertyAccess", "slice"];
const expressions = [
  "math",
  "comparison",
  "logical",
  "logicalNot",
  "call",
  "arrowAssignment",
];
const controls = ["conditional", "fullConditional"];
const topLevels = [
  ...controls,
  "function",
  ...values,
  "assignment",
  "arrowAssignment",
  ...expressions,
  "imported",
];
 
const rules = merge(
  {
    // Calls
    ...map(["symbol", "parallelFn", "propertyAccess", "method", "wrapped"], {
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          lhs.isFn = true;
          return {
            type: "callOpen",
            fn: lhs,
            args: [rhs],
          };
        }, 3)
      ),
    }),
    callOpen: {
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          lhs.args.push(rhs);
          return lhs;
        }, 4)
      ),
      ...map(
        ["lineBreak", "ender"],
        wrap((lhs) => {
          lhs.type = "call";
          return lhs;
        }, 1)
      ),
    },
    pipeOpen: {
      call: wrap((lhs, rhs) => {
        rhs.args.unshift(lhs.data);
        rhs.isMap = lhs.isMap;
        rhs.isFlow = true;
        return rhs;
      }),
      ...map(
        ["symbol", "parallelFn", "method", "propertyAccess", "wrapped"],
        wrap((lhs, rhs) => {
          rhs.isFn = true;
          return {
            type: "callOpen",
            fn: rhs,
            args: [lhs.data],
            isMap: lhs.isMap,
            isFlow: true,
          };
        }, 1)
      ),
      awaited: wrap((lhs, rhs) => {
        const { data, isMap } = lhs;
        if (rhs.value.type.startsWith("call")) {
          rhs.value.args.unshift(data);
          rhs.value.isMap = isMap;
          rhs.value.awaited = true;
          rhs.value.await = rhs.await;
          rhs.value.all = rhs.all;
          rhs.value.isFlow = true;
          return rhs.value;
        }
        return {
          type: "call",
          fn: rhs.value,
          args: [data],
          isMap,
          awaited: true,
          all: rhs.all,
          await: rhs.await,
          isFlow: true,
        };
      }),
    },
    arrow: {
      mulOp: wrap((lhs, rhs) => {
        return { type: "mapArrow", arrow: lhs, map: rhs };
      }, 4),
    },
  },
  /* {
    // Calls
    ...map(values, {
      ender: wrap((lhs) => {
        return { type: "data", content: lhs };
      }, 0.1),
    }),
    call: {
      ender: wrap((lhs) => lhs, 99),
    },
  }, */
  {
    // Calls
    ...map([...values, ...expressions, "data"], {
      arrow: wrap((lhs) => {
        return {
          type: "pipeOpen",
          data: lhs,
        };
      }, 0.31),
      mapArrow: wrap((lhs) => {
        return {
          type: "pipeOpen",
          data: lhs,
          isMap: true,
        };
      }, 0.31),
    }),
  },
  {
    // Blocks
    lineBreak: {
      indent: wrap((_, rhs) => rhs, 99),
    },
    indent: {
      ...map(
        topLevels,
        wrap((_, rhs) => {
          return { type: "blockOpen", content: [rhs] };
        }, 0)
      ),
    },
    blockOpen: {
      ...ignore("lineBreak"),
      ...map(
        topLevels,
        wrap((lhs, rhs) => {
          lhs.content.push(rhs);
          return lhs;
        }, 0)
      ),
      outdent: wrap((lhs) => {
        lhs.type = "block";
        return lhs;
      }),
    },
    /* lineBreak: {
      block: wrap((_, rhs) => rhs),
    }, */
  },
  {
    // Parallel functions
    pike: {
      symbol: wrap((pike, rhs) => {
        return { type: "pikeOpen", fn: types.get(rhs), start: pike };
      }),
    },
    pikeOpen: {
      pike: wrap((lhs) => {
        return { type: "parallelFn", fn: lhs.fn, start: lhs.start };
      }),
    },
  },
  {
    // Functions
    fn: {
      symbol: wrap((lhs, rhs) => {
        return { start: lhs, type: "fnOpen", params: [], name: types.get(rhs) };
      }, 10),
    },
    fnOpen: {
      symbol: wrap((fn, rhs) => {
        fn.params.push(types.get(rhs));
        return fn;
      }, 10),
      colon: wrap((fn) => {
        fn.type = "fnTail";
        return fn;
      }),
    },
    fnTail: {
      ...ignore("lineBreak"),
      block: wrap((lhs, rhs) => {
        lhs.type = "function";
        rhs.type = "return";
        lhs.body = types.get(rhs);
        return lhs;
      }, 10),
      ...map(
        [...expressions, ...values],
        wrap((lhs, rhs) => {
          lhs.type = "function";
          lhs.body = types.get({ type: "return", content: [rhs] });
          return lhs;
        }, 0)
      ),
    },
    comment: {
      ...ignore("lineBreak"),
      function: wrap((lhs, rhs) => {
        rhs.man = lhs;
        return rhs;
      }, 2),
      exportedFunction: wrap((lhs, rhs) => {
        rhs.value.man = lhs;
        return rhs;
      }, 2),
    },
  },
  {
    // Math
    /*
    Even though we're doing a 1:1 translation to JavaScript
    and we do not care about op priorities, I decided to
    implement them anyways because that's the correct way
    of doing it.
  */
    ...map([...values, "math"], {
      powOp: wrap((lhs, op) => {
        return { type: "powLhs", lhs: types.get(lhs), op };
      }, 9),
      mulOp: wrap((lhs, op) => {
        return { type: "mulLhs", lhs: types.get(lhs), op };
      }, 8),
      divOp: wrap((lhs, op) => {
        return { type: "divLhs", lhs: types.get(lhs), op };
      }, 8),
      addOp: wrap((lhs, op) => {
        return { type: "addLhs", lhs: types.get(lhs), op };
      }, 7),
      subOp: wrap((lhs, op) => {
        return { type: "subLhs", lhs: types.get(lhs), op };
      }, 7),
      modOp: wrap((lhs, op) => {
        return { type: "modLhs", lhs: types.get(lhs), op };
      }, 7),
    }),
    ...mapfn(["add", "sub", "mul", "div", "pow", "mod"], (op) => [
      `${op}Lhs`,
      {
        ...ignore("lineBreak"),
        ...map(
          values,
          wrap((lhs, rhs) => {
            return {
              type: "math",
              value: types.get({
                type: op,
                op: lhs.op,
                lhs: lhs.lhs,
                rhs: types.get(rhs),
              }),
            };
          }, 10)
        ),
      },
    ]),
  },
  {
    // Comparisons
    ...map([...values, ...expressions], {
      ...map(
        ["eq", "gt", "lt", "gte", "lte"],
        wrap((lhs, op) => {
          return {
            type: "comparisonOpen",
            lhs: types.get(lhs),
            op,
            comparisons: [],
          };
        }, 5)
      ),
    }),
    comparisonOpen: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          return {
            type: "comparison",
            lhs: lhs.lhs,
            comparisons: [
              ...lhs.comparisons,
              { op: lhs.op, rhs: types.get(rhs) },
            ],
          };
        }, 6)
      ),
    },
    comparison: {
      ...ignore("lineBreak"),
      ...map(
        ["eq", "gt", "lt", "gte", "lte"],
        wrap((lhs, op) => {
          return {
            type: "comparisonOpen",
            lhs: lhs.lhs,
            op,
            comparisons: lhs.comparisons,
          };
        })
      ),
    },
  },
  {
    // Logical
    ...map([...values, ...expressions], {
      ...map(
        ["and", "or"],
        wrap((lhs, op) => {
          return {
            type: "logicalOpen",
            lhs: types.get(lhs),
            op,
            logicals: [],
          };
        }, 2)
      ),
    }),
    logicalOpen: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          return {
            type: "logical",
            lhs: lhs.lhs,
            logicals: [...lhs.logicals, { op: lhs.op, rhs: types.get(rhs) }],
          };
        }, 3)
      ),
    },
    logical: {
      ...ignore("lineBreak"),
      ...map(
        ["and", "or"],
        wrap((lhs, op) => {
          return {
            type: "logicalOpen",
            lhs: lhs.lhs,
            op,
            logicals: lhs.logicals,
          };
        })
      ),
    },
    not: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((op, rhs) => {
          return { type: "logicalNot", op, rhs: types.get(rhs) };
        }, 3.5)
      ),
    },
  },
  {
    // if else
    if: {
      ...map(
        expressions,
        wrap((lhs, rhs) => {
          return {
            type: "ifOpen",
            start: lhs,
            condition: types.get(rhs),
          };
        }, 0.05)
      ),
      ...map(
        values,
        wrap((lhs, rhs) => {
          return {
            type: "ifOpen",
            start: lhs,
            condition: types.get(rhs),
          };
        }, 0)
      ),
    },
    ifOpen: {
      colon: wrap((lhs) => {
        lhs.type = "ifTail";
        return lhs;
      }),
    },
    ifTail: {
      ...ignore("lineBreak"),
      block: wrap((lhs, rhs) => {
        return {
          type: "conditional",
          elseIfs: [],
          if: { ...lhs, type: "ifBlock", body: rhs },
        };
      }),
      ...map(
        [...expressions, ...values],
        wrap((lhs, rhs) => {
          const body = { type: "block", content: [rhs] };
          return {
            type: "conditional",
            elseIfs: [],
            if: { ...lhs, type: "ifBlock", body },
          };
        }, 0)
      ),
    },
    conditional: {
      lineBreak: wrap((lhs) => lhs, 100),
      else: wrap((lhs, rhs) => {
        return {
          type: "ifElseOpen",
          if: lhs.if,
          elseIfs: lhs.elseIfs,
          open: { start: rhs },
        };
      }),
    },
    ifElseOpen: {
      colon: wrap((lhs) => {
        lhs.type = "ifElseTail";
        return lhs;
      }),
      if: wrap((lhs, rhs) => {
        lhs.open.if = rhs;
        lhs.type = "ifElseIfNoCondition";
        return lhs;
      }),
    },
    ifElseIfNoCondition: {
      ...map(
        [...expressions, ...values],
        wrap((lhs, rhs) => {
          lhs.type = "ifElseIfOpen";
          lhs.open.condition = types.get(rhs);
          return lhs;
        }, 0)
      ),
    },
    ifElseIfOpen: {
      colon: wrap((lhs) => {
        lhs.type = "ifElseIfTail";
        return lhs;
      }),
    },
    ifElseIfTail: {
      ...ignore("lineBreak"),
      block: wrap((lhs, rhs) => {
        return {
          type: "conditional",
          if: lhs.if,
          elseIfs: [
            ...lhs.elseIfs,
            { ...lhs.open, type: "elseIfBlock", body: rhs },
          ],
          else: lhs.else,
        };
      }),
      ...map(
        [...expressions, ...values],
        wrap((lhs, rhs) => {
          const body = { type: "block", content: [rhs] };
          return {
            type: "conditional",
            if: lhs.if,
            elseIfs: [
              ...lhs.elseIfs,
              { ...lhs.open, type: "elseIfBlock", body },
            ],
            else: lhs.else,
          };
        }, 0)
      ),
    },
    ifElseTail: {
      ...ignore("lineBreak"),
      block: wrap((lhs, rhs) => {
        return {
          type: "fullConditional",
          if: lhs.if,
          elseIfs: [...lhs.elseIfs],
          else: { ...lhs.open, type: "elseBlock", body: rhs },
        };
      }),
      ...map(
        [...expressions, ...values],
        wrap((lhs, rhs) => {
          const body = { type: "block", content: [rhs] };
          return {
            type: "fullConditional",
            if: lhs.if,
            elseIfs: [...lhs.elseIfs],
            else: { ...lhs.open, type: "elseBlock", body },
          };
        }, 0)
      ),
    },
  },
  {
    // Arrays
    lSquare: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          return { type: "arrayOpen", start: lhs, items: [types.get(rhs)] };
        }, 0.1)
      ),
      rSquare: wrap((lhs, rhs) => {
        return { type: "array", start: lhs, end: rhs, items: [] };
      }),
    },
    arrayOpen: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          lhs.items.push(types.get(rhs));
          return lhs;
        }, 0.1)
      ),
      rSquare: wrap((lhs, rhs) => {
        lhs.type = "array";
        lhs.end = rhs;
        return lhs;
      }),
    },
  },
  // Hashmaps
  {
    hash: {
      symbol: wrap((lhs, rhs) => {
        return {
          type: "hashOpen",
          key: types.get(rhs),
          start: lhs,
          keyValues: [],
        };
      }, 3),
      indent: wrap((lhs) => {
        return {
          type: "hashmapIndent",
          start: lhs,
          keyValues: [],
          isTopLevel: true,
        };
      }),
    },
    hashOpen: {
      ...map(
        values,
        wrap((lhs, rhs) => {
          lhs.type = "hashmap";
          lhs.keyValues.push(
            types.get({
              type: "keyValue",
              key: lhs.key,
              value: types.get(rhs),
            })
          );
          return lhs;
        }, 3)
      ),
      indent: wrap((lhs, rhs) => {
        return {
          type: "hashmapIndent",
          parent: lhs,
          keyValues: [],
          start: rhs,
        };
      }),
    },
    hashmap: {
      symbol: wrap((lhs, rhs) => {
        lhs.key = types.get(rhs);
        lhs.type = "hashOpen";
        return lhs;
      }, 3),
      indent: wrap((lhs) => {
        lhs.type = "hashmapIndent";
        lhs.isTopLevel = true;
        return lhs;
      }),
    },
    // Nested Hashmap
    hashIndentOpen: {
      ...map(["lineBreak"], wrap(lPluck, 3)),
      ...map(
        values,
        wrap((lhs, rhs) => {
          lhs.type = "hashmapIndent";
          lhs.keyValues.push(
            types.get({
              type: "keyValue",
              key: lhs.key,
              value: types.get(rhs),
            })
          );
          return lhs;
        }, 3)
      ),
      indent: wrap((lhs, rhs) => {
        return {
          type: "hashmapIndent",
          parent: lhs,
          start: rhs,
          keyValues: [],
        };
      }),
    },
    hashmapIndent: {
      ...map(["lineBreak"], wrap(lPluck, 3)),
      symbol: wrap((lhs, rhs) => {
        lhs.key = types.get(rhs);
        lhs.type = "hashIndentOpen";
        return lhs;
      }, 3),
      outdent: wrap((lhs) => {
        const { parent, isTopLevel } = lhs;
        if (isTopLevel) {
          lhs.type = "hashmap";
          return lhs;
        }
        lhs.type = "hashmap";
        parent.type = parent.type == "hashOpen" ? "hashmap" : "hashmapIndent";
        parent.keyValues.push(
          types.get({
            type: "keyValue",
            key: parent.key,
            value: types.get(lhs),
          })
        );
        return parent;
      }),
    },
  },
  // Property Access
  {
    ...map(["symbol", "propertyAccess", "slice", "parameter"], {
      dot: wrap((lhs, dot) => {
        return { type: "propertyAccessOpen", lhs, dot };
      }, 12),
    }),
    propertyAccessOpen: {
      symbol: wrap((lhs, rhs) => {
        return { ...lhs, type: "propertyAccess", rhs };
      }, 13),
    },
  },
  {
    // Method
    dot: {
      ...map(
        ["symbol", "propertyAccess", "slice"],
        wrap((dot, rhs) => {
          return { type: "method", dot, property: rhs };
        }, 11)
      ),
    },
  },
  // Range
  {
    ranger: {
      ...map(
        values.filter((item) => !ranges.includes(item)),
        wrap((lhs, rhs) => {
          return { type: "range", location: lhs, end: types.get(rhs) };
        }, 43)
      ),
      byRange: wrap((lhs, rhs) => {
        return { location: lhs, step: rhs.step, type: "rangeBy" };
      }),
    },
    ...map(
      values.filter((item) => !ranges.includes(item)),
      {
        ranger: wrap((lhs, rhs) => {
          return { type: "range", start: types.get(lhs), location: rhs };
        }, 44),
      }
    ),
    range: {
      ...map(
        values.filter((item) => !ranges.includes(item)),
        wrap((lhs, rhs) => {
          lhs.type = "rangeFull";
          lhs.end = types.get(rhs);
          return lhs;
        }, 45)
      ),
      byRange: wrap((lhs, rhs) => {
        return { ...lhs, step: rhs.step, type: "rangeBy" };
      }),
    },
    rangeFull: {
      byRange: wrap((lhs, rhs) => {
        return { ...lhs, step: rhs.step, type: "rangeBy" };
      }),
    },
    by: {
      ...map(
        values.filter((item) => !ranges.includes(item)),
        wrap((lhs, rhs) => {
          return { type: "byRange", step: types.get(rhs), location: lhs };
        }, 47)
      ),
    },
  },
  {
    // Slice
    sliceOpen: {
      array: wrap((lhs, rhs) => {
        lhs.type = "slice";
        lhs.slicer = types.get(rhs);
        return lhs;
      }),
    },
    ...map(arrayLike, {
      slicer: wrap((lhs) => {
        return {
          type: "sliceOpen",
          slicee: types.get(lhs),
        };
      }, 99),
    }),
  },
  {
    // Sets
    lCurly: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          return { type: "setOpen", start: lhs, items: [types.get(rhs)] };
        })
      ),
      rCurly: wrap((lhs, rhs) => {
        return { type: "set", start: lhs, end: rhs, items: [] };
      }),
    },
    setOpen: {
      ...ignore("lineBreak"),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          lhs.items.push(types.get(rhs));
          return lhs;
        })
      ),
      rCurly: wrap((lhs, rhs) => {
        lhs.type = "set";
        lhs.end = rhs;
        return lhs;
      }),
    },
  },
  {
    // Wrapped
    lParen: {
      ...map(["lineBreak"], rule(lPluck, 10)),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          return { type: "wrappedOpen", start: lhs, content: rhs };
        }, 0.1)
      ),
      rParen: wrap(() => {
        return { type: "wrapped", content: null };
      }),
      ender: wrap((lhs) => lhs, 100),
    },
    wrappedOpen: {
      ...ignore("lineBreak"),
      rParen: wrap((lhs, rhs) => {
        lhs.type = "wrapped";
        lhs.end = rhs;
        return lhs;
      }),
    },
  },
  {
    // Formatted Strings
    ...map(["symbol", "propertyAccess", "slice"], {
      format: wrap((lhs) => {
        return { type: "fmtStrFn", fn: lhs, args: [] };
      }, 99),
    }),
    fmtStrFn: {
      fmtStart: wrap((lhs) => {
        lhs.type = "fmtOpen";
        return lhs;
      }, 99),
    },
    fmtOpen: {
      ...map(
        ["fmtExpr", "strEscape", "fmtStr"],
        wrap((lhs, rhs) => {
          lhs.args.push(rhs);
          return lhs;
        }, 99)
      ),
      fmtEnd: wrap((lhs) => {
        lhs.type = "formattedString";
        return lhs;
      }),
    },
    // fmtExpr
    fmtExprStart: {
      ...map(["lineBreak"], rule(lPluck, 10)),
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          return { type: "fmtExprOpen", start: lhs, content: types.get(rhs) };
        }, 0.11)
      ),
      fmtExprEnd: wrap(() => {
        return { type: "fmtExpr", content: null };
      }),
      ender: wrap((lhs) => lhs, 100),
    },
    fmtExprOpen: {
      ...ignore("lineBreak"),
      fmtExprEnd: wrap((lhs, rhs) => {
        lhs.type = "fmtExpr";
        lhs.end = rhs;
        return lhs;
      }),
    },
  },
  {
    // Assignment
    symbol: {
      assign: wrap((lhs, rhs) => {
        return { type: "assignOpen", name: types.get(lhs), assign: rhs };
      }),
    },
    assignOpen: {
      ...map(
        [...values, ...expressions],
        wrap((lhs, rhs) => {
          lhs.type = "assignment";
          lhs.value = types.get(rhs);
          return lhs;
        }, 0.001)
      ),
    },
  },
  {
    // Fat arrow assignment
    ...map([...values, ...expressions], {
      ender: rule((lhs) => lhs, 0.5),
      fatArrow: wrap((lhs, rhs) => {
        return {
          type: "fatArrowOpen",
          arrow: rhs,
          value: types.checkLambda(lhs, types.get(lhs), true),
        };
      }, 0.5),
    }),
    /* callOpen: {
      fatArrow: wrap((lhs, rhs) => {
        lhs.type = "call";
        return {
          type: "fatArrowOpen",
          arrow: rhs,
          value: types.checkLambda(lhs, types.get(lhs), true),
        };
      }, 0.5),
    }, */
    fatArrowOpen: {
      symbol: wrap((lhs, rhs) => {
        lhs.type = "arrowAssignment";
        lhs.name = types.get(rhs);
        return lhs;
      }, 0.5),
    },
    arrowAssignment: {
      ...ignore("lineBreak"),
      arrow: wrap((lhs) => {
        return {
          type: "pipeOpen",
          data: lhs,
        };
      }),
      ender: wrap((lhs) => lhs),
    },
  },
  {
    // Await
    await: {
      ...map(
        [...values, "call", "parallelFn", "method"],
        wrap((lhs, rhs) => {
          return { type: "awaited", await: lhs, value: rhs };
        }, 0.4)
      ),
    },
    awaitAll: {
      ...map(
        [...values, "call", "parallelFn"],
        wrap((lhs, rhs) => {
          return {
            type: "awaited",
            all: true,
            await: lhs,
            value: rhs,
          };
        }, 0.4)
      ),
    },
  },
  {
    ...map(["symbol", "propertyAccess", "wrapped", "slice"], {
      ...map(
        ["lParen", "lCurly", "lSquare", "hash"],
        wrap(
          /* istanbul ignore next */
          () => {
            /* This never matches, but boosts wrapped, array,
             hashmap, set vs block rule matching */
          },
          0.01
        )
      ),
    }),
  },
  {
    // Export
    export: {
      function: wrap((lhs, rhs) => {
        return {
          type: "exportedFunction",
          name: rhs.name,
          value: rhs,
          export: lhs,
        };
      }),
      assignment: wrap((lhs, rhs) => {
        return {
          type: "exported",
          name: rhs.name,
          value: rhs,
          export: lhs,
        };
      }),
    },
  },
  {
    // Import
    import: {
      ...map(
        ["asClause", "symbol"],
        wrap((lhs, rhs) => {
          return { type: "importOpen", items: [rhs], import: lhs };
        }, 17)
      ),
      string: wrap((lhs, rhs) => {
        return { type: "imported", import: lhs, path: rhs };
      }),
    },
    importOpen: {
      ...map(
        ["asClause", "symbol"],
        wrap((lhs, rhs) => {
          lhs.items.push(rhs);
          return lhs;
        }, 18)
      ),
      from: wrap((lhs, rhs) => {
        lhs.from = rhs;
        lhs.type = "importFromOpen";
        return lhs;
      }),
    },
    importFromOpen: {
      string: wrap((lhs, rhs) => {
        lhs.path = rhs;
        lhs.type = "imported";
        return lhs;
      }),
    },
    ...map(["symbol", "mulOp"], {
      as: wrap((lhs, rhs) => {
        return { type: "asOpen", lhs, as: rhs };
      }, 32),
    }),
    asOpen: {
      symbol: wrap((lhs, rhs) => {
        lhs.rhs = rhs;
        lhs.type = "asClause";
        return lhs;
      }, 33),
    },
  },
  {
    // In check
    ...map([...expressions, ...values], {
      in: wrap((lhs, rhs) => {
        return { type: "inCheckOpen", lhs: lhs, start: rhs };
      }, 0.1),
    }),
    inCheckOpen: {
      ...map(
        [...expressions, ...values],
        wrap((lhs, rhs) => {
          lhs.type = "inCheck";
          lhs.rhs = rhs;
          return lhs;
        })
      ),
    },
  },
  {
    // Top level rule
    ...map([...topLevels, "exported", "exportedFunction"], {
      eof: wrap((lhs) => {
        return { type: "clio", content: [types.get(lhs)] };
      }, 0),
      clio: wrap((lhs, rhs) => {
        rhs.content.unshift(types.get(lhs));
        return rhs;
      }, 0),
    }),
    lineBreak: {
      clio: wrap((_, rhs) => rhs, 0),
      eof: wrap(() => {
        return { type: "clio", content: [] };
      }),
    },
  }
);
 
/* istanbul ignore next */
const skips = ["blockOpen", "ifTail"];
 
/* istanbul ignore next */
const parsingError = (source, file, tokens) => {
  let first = tokens.first;
  let next = tokens.first.next;
  while (true) {
    const isValid = rules[first.item.type][next.item.type];
    if (isValid || skips.includes(next.item.type)) {
      first = next;
      next = first.next;
    } else {
      break;
    }
  }
  first = first.item;
  next = next.item;
  const expecting = Object.keys(rules[first.type] || {}).join(", ");
  const start = Math.max(0, next.line - 3);
  const location = next.meta?.location || next;
  const { line, column } = location;
  const message = [
    `Parsing error at ${file}[${line}:${column}]\n`,
    source.split("\n").slice(start, line).join("\n"),
    " ".repeat(column) + "^",
    `\nExpecting one of ${expecting} but encountered ${next?.type}`,
  ].join("\n");
  return new Error(message);
};
 
const parse = (tokens) => bean(tokens, rules);
const compile = (source, file, debug = false) => {
  const tokens = lex(source, file);
  /* istanbul ignore next */
  if (debug) console.dir(tokens.current, { depth: null });
  const result = parse(tokens);
  /* istanbul ignore next */
  if (debug) console.dir(result, { depth: null });
  Eif (result.first.item.type == "clio") {
    const code = types.get(result.current.item).toString();
    return code;
  } else {
    /* istanbul ignore next */
    throw parsingError(source, file, result);
  }
};
 
/* istanbul ignore next */
let i = 1;
/* istanbul ignore next */
const testStr = (name, input, output, debug = false) => {
  const result = compile(input, "<mem>", debug);
  const passes = result == output;
  console.log(i.toString().padStart(2, " "), [passes], name);
  if (!passes) {
    console.log();
    console.log("Error compiling:");
    console.log(input);
    console.log();
    console.log(`Expecting:`);
    console.log(output);
    console.log();
    console.log(`Got:`);
    console.log(result);
  }
  i++;
};
 
module.exports.compile = compile;
 
/*
  Math has the highest priority
  Comparison comes second
  Logical is third
  Hashmaps, arrays, sets, wraps are next
  Function calls are next
  Blocks are next
  Export is next
 
  Quick functions are messy, they can be improved
*/