All files / src/abap/2_statements combi.ts

78.35% Statements 988/1261
92.99% Branches 199/214
45.6% Functions 83/182
78.35% Lines 988/1261

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 12621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 805x 805x 1x 1x     1x 1x     1x 1x 4438234x 4438234x 4438234x 4438492x 42393x 42393x 4396099x 4438492x 282424x 282424x 4438492x 4438234x 4438234x 4438234x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 5595x 5595x 1x 1x     1x 1x     1x 1x 4202958x 4202958x 4202958x 4210008x 4210008x 237340x 237340x 237340x 4210008x 4202958x 4202958x 1x 1x     1x 1x 17x 17x 1x 1x 1105x 1105x 1x 1x 1x 1x 1x 1x 1x 1x 1748x 1748x 1748x 1x 1x     1x 1x     1x 1x 1074214x 1074214x 1074214x 1087132x 1087132x 42312x 42312x 1087132x 1074214x 1074214x 1x 1x                       1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2074x 2074x 2074x 2074x 1x 1x     1x 1x 5981914x 8904x 8904x 8904x 5973010x 5981914x 5981914x 1x 1x     1x 1x                     1x 1x     1x 1x 42x 42x 1x 1x 168498x 168498x 168498x 168498x 168498x 168498x 168498x 168498x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x     1x 1x     1x 1x 13x 11x 13x 2x 2x 13x 1x 1x             1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1094x 1094x 1094x 1x 1x     1x 1x     1x 1x 84236x 82362x 84236x 1874x 1874x 84236x 1x 1x             1x 1x     1x 1x 658x 658x 1x 1x 1x 1x 1x 1x 1x 4758x 4758x 1x 1x     1x 1x     1x 1x 672604x 672604x 672604x 711688x 711688x 5202x 711688x 676252x 706486x 30234x 30234x     711688x 672604x 672604x 672604x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 9945x 9945x 1x 1x     1x 1x     1x 1x 110124x 110124x 110124x 122804x 122804x 122804x 13480x 122804x 109323x 109323x 122804x 110123x 110123x 110123x 1x 1x     1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 4877x 4877x 1x 1x     1x 1x     1x 1x 53728x 53728x 53728x 53728x 53728x 53728x 64125x 64125x 64125x 64125x 53726x 53726x 10397x 64125x     64125x 9800x 10397x 597x 597x 64125x 53728x 2x 2x 2x     53726x 53726x 53726x 1x 1x     1x 1x 6x 6x 1x 1x     1x 1x 1x 1x 1x 1x 1x 809x 809x 1x 1x     1x 1x     1x 1x 91511x 91511x 91511x 91511x 91511x 91511x 100032x 100032x 100032x 100032x 91511x 4466x 4466x 4466x 5033x 4562x 4562x 4562x 5033x 4466x 91511x 91511x 8521x 8521x 8521x 91511x 91511x 91511x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 870x 870x 870x 1x 1x     1x 1x     1x 1x 31381x 31381x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 149x 149x 149x 1x 1x     1x 1x     1x 1x 45554x 45554x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 20831x     20831x 20831x 1x 1x             1x 1x     1x 1x 8946569x 8946569x 8946569x 8974372x 8974372x 8974372x 10336420x 10336420x 8465629x 8465629x 8465629x 10336420x 8974367x 8974372x 508738x     508738x 451976x 508738x 56762x 56762x 508738x 8974372x 8946564x 8946564x 8946564x 1x 1x       1x 1x 6x 6x 12x 12x 6x 6x 1x 1x 941x 941x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1195x 1195x 1195x 1195x 1195x 1195x 3175x 3175x 3175x 1195x 1195x 1x 1x     1x 1x     1x 1x 449514x 449514x 1x 1x     1x 1x     1x 1x 423x 423x 1x 1x 1586x 1586x 1586x 1586x 1962103x 1962103x 1962103x 1463x 1463x 1962103x 1962103x 1969387x 1969387x 1969387x 537119x 537119x 534890x 534890x 537119x 2229x 2229x 537119x 1969387x 1969387x 1962103x 1962103x 1962103x 1586x 1586x 1586x 1586x       1586x 1586x     1586x 1586x 95x 95x 1586x 1586x     1586x 1586x 7x 7x 1586x 1586x 9x 9x 1586x 1x 1x 1x 1x 1x 12183x     12183x 12183x 1x 1x             1x 1x     1x 1x 28240x 28240x 28240x 28240x 194326x 194326x 12344x 12344x 12344x 12344x 12344x 12344x 620x 12344x 11724x 11724x 12344x 194326x 28240x 28240x 1x 1x       1x 1x       1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x     1x 1x 1x 1x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x     1x 1x     1x 1x 2x 2x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1798x     1798x 1798x 1x 1x             1x 1x     1x 1x 112551x 112551x 112551x 303419x 303419x 39752x 303419x 263665x 263665x 303419x 112549x 112549x 112549x 1x 1x       1x 1x             1x 1x 28x     28x 28x 28x     28x 3x 3x 28x 2x 2x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 4977x     4977x 4977x 1x 1x             1x 1x     1x 1x 1380317x 1380317x 1380317x 10563904x 10563904x 10563904x 10563904x 452271x 418570x 452271x 33701x 33701x 452271x 452271x 10563904x 1380317x 1380317x 1380317x 1x 1x       1x 1x             1x 1x 37x 5x 5x 32x 32x 37x     37x 2x 2x 37x 2x 2x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x                         1x 1x             1x 1x 1x 76683x 76683x 76683x 76683x 76683x 76683x 108x 76683x 76575x 76575x 76683x 76683x 76683x 76683x 76683x 76683x 76683x 76683x 76683x 76683x 76683x 76683x 51014x 30553x 30553x 51014x 76683x 1x 1x 1x     46129x 46129x 46129x 1x 1x 5973010x 5973010x 1x 1x 84249x 84249x 1x 1x 5981914x 5981914x 1x 1x 1x 1x 1x 3615x 3615x 1195x 3615x 2420x 2420x 3615x 1x 805x 805x 805x 1x 1748x 1748x 1748x 1x 1x 1x 1x 1x 114787x 114787x 114787x 34227x 1355x 1355x 34227x 114787x 24817x 24817x 24817x 306x 306x 306x 24817x 80560x 55743x 55743x 114787x 18617x 18617x 18617x 18617x 18617x 1798x 1798x 1798x 1798x 1798x 4977x 4977x 4977x 4977x 4977x 9945x 9945x 9945x 4758x 4758x 4758x 459x 459x 459x 459x 459x 4007x 4007x 4007x 660x 660x 660x 870x 870x 870x 149x 149x 149x 2074x 2074x     2074x 2074x 1x 2074x 2074x 2074x 3x 3x 3x 1094x 1094x 1094x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 4754x 4754x 4754x 4754x 4754x 13x 13x 4611x 4611x 4754x 4754x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 799x 799x 799x 799x 438x 438x 799x 799x 1x 1x 1x 1x 1x 5x 5x 5x  
import * as Tokens from "../1_lexer/tokens";
import {AbstractToken as Tokens_Token} from "../1_lexer/tokens/abstract_token";
import {Position} from "../../position";
import {TokenNode, ExpressionNode, TokenNodeRegex} from "../nodes";
import {Version, LanguageVersion, ABAPRelease, Release, releaseAtLeast, versionToABAPRelease} from "../../version";
import {IStatementRunnable} from "./statement_runnable";
import {Result} from "./result";
 
 
class Regex implements IStatementRunnable {
 
  private readonly regexp: RegExp;
 
  public constructor(r: RegExp) {
    this.regexp = r;
  }
 
  public listKeywords(): string[] {
    return [];
  }
 
  public getUsing(): string[] {
    return [];
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const input of r) {
      if (input.remainingLength() === 0) {
        continue;
      }
      const token = input.peek();
      if (this.regexp.test(token.getStr()) === true) {
        result.push(input.shift(new TokenNodeRegex(token)));
      }
    }
 
    return result;
  }
 
  public railroad() {
    return "Railroad.Terminal(\"" + this.regexp.source.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\")";
  }
 
  public toStr() {
    return this.regexp.toString();
  }
 
  public first() {
    return [""];
  }
}
 
class Word implements IStatementRunnable {
 
  private readonly s: string;
 
  public constructor(s: string) {
    this.s = s.toUpperCase();
  }
 
  public listKeywords(): string[] {
    return [this.s];
  }
 
  public getUsing(): string[] {
    return [];
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const input of r) {
      if (input.remainingLength() !== 0
          && input.peek().getUpperStr() === this.s) {
//        console.log("match, " + this.s + result.length);
        result.push(input.shift(new TokenNode(input.peek())));
      }
    }
    return result;
  }
 
  public railroad() {
    return "Railroad.Terminal('\"" + this.s + "\"')";
  }
 
  public toStr() {
    return "\"" + this.s + "\"";
  }
 
  public first() {
    return [this.s];
  }
}
 
class Token implements IStatementRunnable {
 
  private readonly name: string;
  private readonly tokenType: new (p: Position, s: string) => Tokens_Token;
 
  public constructor(t: new (p: Position, s: string) => Tokens_Token) {
    this.tokenType = t;
    this.name = t.name;
  }
 
  public listKeywords(): string[] {
    return [];
  }
 
  public getUsing(): string[] {
    return [];
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const input of r) {
      if (input.remainingLength() !== 0
          && input.peek().constructor === this.tokenType) {
        result.push(input.shift(new TokenNode(input.peek())));
      }
    }
    return result;
  }
 
  public railroad() {
    let text = this.name;

    const toke: any = Tokens;
    for (const token in Tokens) {
      if (token.toUpperCase() === this.name && toke[token].railroad) {
        text = toke[token].railroad();
        break;
      }
    }
    return "Railroad.Terminal('!\"" + text + "\"')";
  }
 
  public toStr() {
    return "Token \"" + this.name + "\"";
  }
 
  public first() {
    return [""];
  }
}
 
export enum AlsoIn {
  OpenABAP = "OpenABAP",
}
 
export interface IVerOptions {
  also?: AlsoIn;
}
 
class Vers implements IStatementRunnable {
 
  private readonly release: ABAPRelease;
  private readonly runnable: IStatementRunnable;
  private readonly also: AlsoIn | undefined;
 
  public constructor(release: ABAPRelease, runnable: IStatementRunnable, also?: AlsoIn) {
    this.release = release;
    this.runnable = runnable;
    this.also = also;
  }
 
  public listKeywords(): string[] {
    return this.runnable.listKeywords();
  }
 
  public run(r: Result[]): Result[] {
    if (Combi.isOpenABAP() === true) {
      if (this.also === AlsoIn.OpenABAP) { return this.runnable.run(r); }
      return releaseAtLeast(Release.v702, this.release) ? this.runnable.run(r) : [];
    }
 
    return releaseAtLeast(Combi.getRelease(), this.release) ? this.runnable.run(r) : [];
  }
 
  public getUsing(): string[] {
    return this.runnable.getUsing();
  }
 
  public railroad() {
    let text = this.release.abap ?? "Newest";
    if (this.also) {
      text += " or " + this.also;
    }
    return "Railroad.Sequence(Railroad.Comment(\"" +
      text +
      "\", {}), " +
      this.runnable.railroad() +
      ")";
  }
 
  public toStr() {
    return "Version(" + this.runnable.toStr() + ")";
  }
 
  public first() {
    return this.runnable.first();
  }
}
 
function langVersionOrdinal(v: LanguageVersion): number {
  switch (v) {
    case LanguageVersion.KeyUser: return 0;
    case LanguageVersion.Cloud: return 1;
    case LanguageVersion.Normal: return 2;
    default: return 2;
  }
}
 
class LangVers implements IStatementRunnable {
 
  private readonly langVersion: LanguageVersion;
  private readonly runnable: IStatementRunnable;
 
  public constructor(langVersion: LanguageVersion, runnable: IStatementRunnable) {
    this.langVersion = langVersion;
    this.runnable = runnable;
  }
 
  public listKeywords(): string[] {
    return this.runnable.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.runnable.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    if (langVersionOrdinal(Combi.getLanguageVersion()) <= langVersionOrdinal(this.langVersion)) {
      return this.runnable.run(r);
    } else {
      return [];
    }
  }
 
  public railroad() {
    return "Railroad.Sequence(Railroad.Comment(\"langver=" +
      this.langVersion +
      "\", {}), " +
      this.runnable.railroad() +
      ")";
  }
 
  public toStr() {
    return "LangVersion(" + this.runnable.toStr() + ")";
  }
 
  public first() {
    return this.runnable.first();
  }
}
 
class LangVersNot implements IStatementRunnable {
 
  private readonly langVersion: LanguageVersion;
  private readonly runnable: IStatementRunnable;
 
  public constructor(langVersion: LanguageVersion, runnable: IStatementRunnable) {
    this.langVersion = langVersion;
    this.runnable = runnable;
  }
 
  public listKeywords(): string[] {
    return this.runnable.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.runnable.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    if (langVersionOrdinal(Combi.getLanguageVersion()) > langVersionOrdinal(this.langVersion)) {
      return this.runnable.run(r);
    } else {
      return [];
    }
  }
 
  public railroad() {
    return "Railroad.Sequence(Railroad.Comment(\"not langver=" +
      this.langVersion +
      "\", {}), " +
      this.runnable.railroad() +
      ")";
  }
 
  public toStr() {
    return "LanguageVersionNot(" + this.runnable.toStr() + ")";
  }
 
  public first() {
    return this.runnable.first();
  }
}
 
class OptionalPriority implements IStatementRunnable {
 
  private readonly optional: IStatementRunnable;
 
  public constructor(optional: IStatementRunnable) {
    this.optional = optional;
  }
 
  public listKeywords(): string[] {
    return this.optional.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.optional.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const input of r) {
      const res = this.optional.run([input]);
      if (res.length > 1) {
        result.push(...res);
      } else if (res.length === 0) {
        result.push(input);
      } else if (res[0].remainingLength() < input.remainingLength()) {
        result.push(...res);
      } else {
        result.push(input);
      }
    }
 
    return result;
  }
 
  public railroad() {
    return "Railroad.Optional(" + this.optional.railroad() + ")";
  }
 
  public toStr() {
    return "opt(" + this.optional.toStr() + ")";
  }
 
  public first() {
    return [""];
  }
}
 
class Optional implements IStatementRunnable {
 
  private readonly optional: IStatementRunnable;
 
  public constructor(optional: IStatementRunnable) {
    this.optional = optional;
  }
 
  public listKeywords(): string[] {
    return this.optional.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.optional.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const input of r) {
      result.push(input);
      const res = this.optional.run([input]);
      if (res.length === 1) {
        result.push(res[0]);
      } else {
        result.push(...res);
      }
    }
 
    return result;
  }
 
  public railroad() {
    return "Railroad.Optional(" + this.optional.railroad() + ")";
  }
 
  public toStr() {
    return "opt(" + this.optional.toStr() + ")";
  }
 
  public first() {
    return [""];
  }
}
 
class Star implements IStatementRunnable {
 
  private readonly sta: IStatementRunnable;
 
  public constructor(sta: IStatementRunnable) {
    this.sta = sta;
  }
 
  public listKeywords(): string[] {
    return this.sta.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.sta.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    let result = r;
 
    try {
      let res = r;
      let input: Result[] = [];
      for (;;) {
        input = res;
        res = this.sta.run(input);
 
        if (res.length === 0) {
          break;
        }
 
        if (res.length > 1000) {
          // avoid stack overflow
          result = result.concat(res);
        } else if (res.length === 1) {
          result.push(res[0]);
        } else {
          result.push(...res);
        }
      }
    } catch (err) {
      if (err instanceof FailStarError) {
        return result;
      }
      throw err;
    }
//    console.dir(result);
    return result;
  }
 
  public railroad() {
    return "Railroad.ZeroOrMore(" + this.sta.railroad() + ")";
  }
 
  public toStr() {
    return "star(" + this.sta.toStr() + ")";
  }
 
  public first() {
    return [""];
  }
}
 
class StarPriority implements IStatementRunnable {
 
  private readonly sta: IStatementRunnable;
 
  public constructor(sta: IStatementRunnable) {
    this.sta = sta;
  }
 
  public listKeywords(): string[] {
    return this.sta.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.sta.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    let result: Result[] = r;
 
    let res = r;
//    let input: Result[] = [];
    let prev: Result[] | undefined;
    for (;;) {
//      input = res;
      res = this.sta.run(res);
 
      if (res.length === 0) {
        if (prev !== undefined) {
//          console.log("star length: " + prev.length);
          let best = Number.MAX_SAFE_INTEGER;
          for (const p of prev) {
            if (p.remainingLength() < best) {
              result = [p];
              best = p.remainingLength();
            }
          }
        }
        break;
      }
 
      prev = res;
    }
 
    return result;
  }
 
  public railroad() {
    return "Railroad.ZeroOrMore(" + this.sta.railroad() + ")";
  }
 
  public toStr() {
    return "star(" + this.sta.toStr() + ")";
  }
 
  public first() {
    return [""];
  }
}
 
class Plus implements IStatementRunnable {
 
  private readonly plu: IStatementRunnable;
  private readonly sub: Sequence;
 
  public constructor(plu: IStatementRunnable) {
    this.plu = plu;
    this.sub = new Sequence([this.plu, new Star(this.plu)]);
  }
 
  public listKeywords(): string[] {
    return this.plu.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.plu.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    return this.sub.run(r);
  }
 
  public railroad() {
    return "Railroad.OneOrMore(" + this.plu.railroad() + ")";
  }
 
  public toStr() {
    return "plus(" + this.plu.toStr() + ")";
  }
 
  public first() {
    return this.plu.first();
  }
}
 
class PlusPriority implements IStatementRunnable {
 
  private readonly plu: IStatementRunnable;
  private readonly sub: Sequence;
 
  public constructor(plu: IStatementRunnable) {
    this.plu = plu;
    this.sub = new Sequence([this.plu, new StarPriority(this.plu)]);
  }
 
  public listKeywords(): string[] {
    return this.plu.listKeywords();
  }
 
  public getUsing(): string[] {
    return this.plu.getUsing();
  }
 
  public run(r: Result[]): Result[] {
    return this.sub.run(r);
  }
 
  public railroad() {
    return "Railroad.OneOrMore(" + this.plu.railroad() + ")";
  }
 
  public toStr() {
    return "plus(" + this.plu.toStr() + ")";
  }
 
  public first() {
    return this.plu.first();
  }
}
 
class Sequence implements IStatementRunnable {
  private readonly list: IStatementRunnable[];
 
  public constructor(list: IStatementRunnable[]) {
    if (list.length < 2) {
      throw new Error("Sequence, length error");
    }
    this.list = list;
  }
 
  public listKeywords(): string[] {
    const ret: string[] = [];
    for (const i of this.list) {
      ret.push(...i.listKeywords());
    }
    return ret;
  }
 
  public getUsing(): string[] {
    return this.list.reduce((a, c) => { return a.concat(c.getUsing()); }, [] as string[]);
  }
 
  public run(r: Result[]): Result[] {
    let result: Result[] = [];
 
    for (const input of r) {
      let temp = [input];
      let match = true;
      for (const sequence of this.list) {
        temp = sequence.run(temp);
        if (temp.length === 0) {
          match = false;
          break;
        }
      }
 
      if (match === true) {
        if (temp.length > 1000) {
          // avoid stack overflow
          result = result.concat(temp);
        } else if (temp.length === 1) {
          result.push(temp[0]);
        } else {
          result.push(...temp);
        }
      }
    }
 
    return result;
  }
 
  public railroad() {
    const children = this.list.map((e) => { return e.railroad(); });
    return "Railroad.Sequence(" + children.join() + ")";
  }
 
  public toStr() {
    let ret = "";
    for (const i of this.list) {
      ret = ret + i.toStr() + ",";
    }
    return "seq(" + ret + ")";
  }
 
  public first() {
    return this.list[0].first();
  }
}
 
class WordSequence implements IStatementRunnable {
 
  private readonly stri: string;
  private readonly words: IStatementRunnable[] = [];
  private readonly seq: Sequence;
 
  public constructor(stri: string) {
    this.stri = stri;
 
    const foo = this.stri.replace(/-/g, " - ");
    const split = foo.split(" ");
 
    for (const st of split) {
// todo, use Dash token
      this.words.push(new Word(st));
    }
    this.seq = new Sequence(this.words);
  }
 
  public listKeywords(): string[] {
    return [this.stri.toString()];
  }
 
  public getUsing(): string[] {
    return [];
  }
 
  public run(r: Result[]): Result[] {
    return this.seq.run(r);
  }
 
  public railroad() {
    return "Railroad.Terminal('\"" + this.stri + "\"')";
  }
 
  public toStr() {
    return "str(" + this.stri + ")";
  }
 
  public first() {
    return this.words[0].first();
  }
}
 
export abstract class Expression implements IStatementRunnable {
  private runnable: IStatementRunnable | undefined = undefined;
 
  public run(r: Result[]): Result[] {
    const results: Result[] = [];
 
    if (this.runnable === undefined) {
      this.runnable = this.getRunnable();
    }
 
    for (const input of r) {
      const temp = this.runnable.run([input]);
 
      for (const t of temp) {
        const consumed = input.remainingLength() - t.remainingLength();
        if (consumed > 0) {
          const re = new ExpressionNode(this);
          results.push(t.wrapConsumed(consumed, re));
        } else {
          results.push(t);
        }
      }
 
    }
//    console.dir(results);
    return results;
  }
 
  public abstract getRunnable(): IStatementRunnable;
 
  public listKeywords(): string[] {
// do not recurse, all Expressions are evaluated only on first level
    return [];
  }
 
  public getUsing(): string[] {
    return ["expression/" + this.getName()];
  }
 
  public getName(): string {
    return this.constructor.name;
  }
 
  public railroad() {
    return "Railroad.NonTerminal('" + this.getName() + "', {href: '#/expression/" + this.getName() + "'})";
  }
 
  public toStr() {
    return "expression(" + this.getName() + ")";
  }
 
  public first() {
    return this.getRunnable().first();
  }
}
 
class Permutation implements IStatementRunnable {
  private readonly list: IStatementRunnable[];
 
  public constructor(list: IStatementRunnable[]) {
    if (list.length < 2) {
      throw new Error("Permutation, length error, got " + list.length);
    }
    this.list = list;
  }
 
  public listKeywords(): string[] {
    const ret: string[] = [];
    for (const i of this.list) {
      ret.push(...i.listKeywords());
    }
    return ret;
  }
 
  public getUsing() {
    return this.list.reduce((a, c) => { return a.concat(c.getUsing()); }, [] as string[]);
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    const copy = this.list.slice();
    for (let index = 0; index < this.list.length; index++) {
      const temp = this.list[index].run(r);
      if (temp.length !== 0) {
// match
        result.push(...temp);
 
        const left = copy;
        left.splice(index, 1);
        if (left.length === 1) {
          result.push(...left[0].run(temp));
        } else {
          result.push(...new Permutation(left).run(temp));
        }
      }
    }
    return result;
  }
 
  public railroad() {
    const children = this.list.map((e) => { return e.railroad(); });
    return "Railroad.MultipleChoice(0, 'any'," + children.join() + ")";
  }
 
  public toStr() {
    const children = this.list.map((e) => { return e.toStr(); });
    return "per(" + children.join() + ")";
  }
 
  public first() {
    return [""];
  }
}
 
class FailCombinatorError extends Error {
 
}
 
class FailStarError extends Error {
 
}
 
class FailCombinator implements IStatementRunnable {
 
  public listKeywords(): string[] {
    return [];
  }
 
  public getUsing() {
    return [];
  }
 
  public run(_r: Result[]): Result[] {
    throw new FailCombinatorError();
  }
 
  public railroad() {
    return "Railroad.Terminal('!FailCombinator')";
  }
 
  public toStr() {
    return "fail()";
  }
 
  public first() {
    return [];
  }
}
 
// Note that Plus is implemented with Star
class FailStar implements IStatementRunnable {
 
  public listKeywords(): string[] {
    return [];
  }
 
  public getUsing() {
    return [];
  }
 
  public run(_r: Result[]): Result[] {
    throw new FailStarError();
  }
 
  public railroad() {
    return "Railroad.Terminal('!FailStar')";
  }
 
  public toStr() {
    return "fail()";
  }
 
  public first() {
    return [];
  }
}
 
class Alternative implements IStatementRunnable {
  private readonly list: IStatementRunnable[];
 
  public constructor(list: IStatementRunnable[]) {
    if (list.length < 2) {
      throw new Error("Alternative, length error");
    }
    this.list = list;
  }
 
  public listKeywords(): string[] {
    const ret: string[] = [];
    for (const i of this.list) {
      ret.push(...i.listKeywords());
    }
    return ret;
  }
 
  public getUsing() {
    return this.list.reduce((a, c) => { return a.concat(c.getUsing()); }, [] as string[]);
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const sequ of this.list) {
      const temp = sequ.run(r);
      if (temp.length === 1) {
        result.push(temp[0]);
      } else {
        result.push(...temp);
      }
    }
 
    return result;
  }
 
  public railroad() {
    const children = this.list.map((e) => { return e.railroad(); });
    return "Railroad.Choice(0, " + children.join() + ")";
  }
 
  public toStr() {
    let ret = "";
    for (const i of this.list) {
      ret = ret + i.toStr() + ",";
    }
    return "alt(" + ret + ")";
  }
 
  public first() {
    if (this.list.length !== 2) {
      return [""];
    }
    const f1 = this.list[0].first();
    const f2 = this.list[1].first();
    if (f1.length === 1 && f1[0] === "") {
      return f1;
    }
    if (f2.length === 1 && f2[0] === "") {
      return f2;
    }
    if (f1.length === 1 && f2.length === 1 && f1[0] === f2[0]) {
      return f1;
    }
    f1.push(...f2);
    return f1;
  }
}
 
// prioritized alternative, skip others if match found
class AlternativePriority implements IStatementRunnable {
  private readonly list: IStatementRunnable[];
 
  public constructor(list: IStatementRunnable[]) {
    if (list.length < 2) {
      throw new Error("Alternative, length error");
    }
    this.list = list;
  }
 
  public listKeywords(): string[] {
    const ret: string[] = [];
    for (const i of this.list) {
      ret.push(...i.listKeywords());
    }
    return ret;
  }
 
  public getUsing() {
    return this.list.reduce((a, c) => { return a.concat(c.getUsing()); }, [] as string[]);
  }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
 
    for (const sequ of this.list) {
//      console.log(seq.toStr());
      const temp = sequ.run(r);
 
      if (temp.length > 0) {
        if (temp.length === 1) {
          result.push(temp[0]);
        } else {
          result.push(...temp);
        }
        break;
      }
    }
 
    return result;
  }
 
  public railroad() {
    const children = this.list.map((e) => { return e.railroad(); });
    return "Railroad.Choice(0, " + children.join() + ")";
  }
 
  public toStr() {
    let ret = "";
    for (const i of this.list) {
      ret = ret + i.toStr() + ",";
    }
    return "alt(" + ret + ")";
  }
 
  public first() {
    if (this.list.length !== 2) {
      return [""];
    }
    const f1 = this.list[0].first();
    const f2 = this.list[1].first();
    if (f1.length === 1 && f1[0] === "") {
      return f1;
    }
    if (f2.length === 1 && f2[0] === "") {
      return f2;
    }
    if (f1.length === 1 && f2.length === 1 && f1[0] === f2[0]) {
      return f1;
    }
    f1.push(...f2);
    return f1;
  }
}
 
export class Combi {
// todo, change this class to be instantiated, constructor(runnable) ?
 
  private static release: ABAPRelease = Release.v758;
  private static langVer: LanguageVersion = LanguageVersion.Normal;
 
  public static railroad(runnable: IStatementRunnable, complex = false): string {
// todo, move method to graph.js?
    let type = "Railroad.Diagram(";
    if (complex === true) {
      type = "Railroad.ComplexDiagram(";
    }

    const result = "Railroad.Diagram.INTERNAL_ALIGNMENT = 'left';\n" +
      type +
      runnable.railroad() +
      ").toString();";
    return result;
  }
 
  public static listKeywords(runnable: IStatementRunnable): string[] {
// todo, move these walkers of the syntax tree to some abstraction?
    let res = runnable.listKeywords();
// remove duplicates
    res = res.filter((x, i, a) => { return a.indexOf(x) === i; });
    return res;
  }
 
// assumption: no pragmas supplied in tokens input
  public static run(
    runnable: IStatementRunnable,
    tokens: readonly Tokens_Token[], versionOrRelease: Version | ABAPRelease,
    languageVersion: LanguageVersion = LanguageVersion.Normal): (ExpressionNode | TokenNode)[] | undefined {
 
    this.langVer = languageVersion;
    if (typeof versionOrRelease === "string") {
      this.release = versionToABAPRelease(versionOrRelease);
    } else {
      this.release = versionOrRelease;
    }
 
    const input = new Result(tokens, 0);
    try {
      const result = runnable.run([input]);
/*
    console.log("res: " + result.length);
    for (const res of result) {
      console.dir(res.getNodes().map(n => n.get().constructor.name));
      console.dir(res.getNodes().map(n => n.concatTokens()));
    }
*/
      for (const res of result) {
        if (res.remainingLength() === 0) {
          return res.getNodes();
        }
      }
    } catch (err) {
      if (err instanceof FailCombinatorError) {
        return undefined;
      }
      throw err;
    }
 
    return undefined;
  }
 
  public static getRelease(): ABAPRelease {
    return this.release;
  }
 
  public static getLanguageVersion(): LanguageVersion {
    return this.langVer;
  }
 
  public static isOpenABAP(): boolean {
    return this.release === Release["open-abap"];
  }
 
}
 
// -----------------------------------------------------------------------------
 
export function str(s: string): IStatementRunnable {
  if (s.indexOf(" ") > 0 || s.indexOf("-") > 0) {
    return new WordSequence(s);
  } else {
    return new Word(s);
  }
}
 
export function regex(r: RegExp): IStatementRunnable {
  return new Regex(r);
}
 
export function tok(t: new (p: Position, s: string) => Tokens_Token): IStatementRunnable {
  return new Token(t);
}
 
const expressionSingletons: {[index: string]: Expression} = {};
const stringSingletons: {[index: string]: IStatementRunnable} = {};
type InputType = (new () => Expression) | string | IStatementRunnable;
 
function mapInput(s: InputType): IStatementRunnable {
  const type = typeof s;
  if (type === "string") {
    if (stringSingletons[s as string] === undefined) {
      stringSingletons[s as string] = str(s as string);
    }
    return stringSingletons[s as string];
  } else if (type === "function") {
    // @ts-ignore
    const name = s.name;
    if (expressionSingletons[name] === undefined) {
      // @ts-ignore
      expressionSingletons[name] = new s();
    }
    return expressionSingletons[name];
  } else {
    return s as IStatementRunnable;
  }
}
export function seq(first: InputType, second: InputType, ...rest: InputType[]): IStatementRunnable {
  const list = [mapInput(first), mapInput(second)];
  list.push(...rest.map(mapInput));
  return new Sequence(list);
}
export function alt(first: InputType, second: InputType, ...rest: InputType[]): IStatementRunnable {
  const list = [mapInput(first), mapInput(second)];
  list.push(...rest.map(mapInput));
  return new Alternative(list);
}
export function altPrio(first: InputType, second: InputType, ...rest: InputType[]): IStatementRunnable {
  const list = [mapInput(first), mapInput(second)];
  list.push(...rest.map(mapInput));
  return new AlternativePriority(list);
}
export function opt(first: InputType): IStatementRunnable {
  return new Optional(mapInput(first));
}
export function optPrio(first: InputType): IStatementRunnable {
  return new OptionalPriority(mapInput(first));
}
export function per(first: InputType, second: InputType, ...rest: InputType[]): IStatementRunnable {
  const list = [mapInput(first), mapInput(second)];
  list.push(...rest.map(mapInput));
  return new Permutation(list);
}
export function star(first: InputType): IStatementRunnable {
  return new Star(mapInput(first));
}
export function starPrio(first: InputType): IStatementRunnable {
  return new StarPriority(mapInput(first));
}
export function plus(first: InputType): IStatementRunnable {
  return new Plus(mapInput(first));
}
export function plusPrio(first: InputType): IStatementRunnable {
  return new PlusPriority(mapInput(first));
}
function inputToRelease(versionOrRelease: Version | ABAPRelease): ABAPRelease {
  if (typeof versionOrRelease === "string") {
    return versionToABAPRelease(versionOrRelease);
  }
  return versionOrRelease;
}
 
export function ver(versionOrRelease: Version | ABAPRelease, first: InputType, opts: IVerOptions = {}): IStatementRunnable {
  return new Vers(inputToRelease(versionOrRelease), mapInput(first), opts.also);
}
export function verLang(langVersion: LanguageVersion, first: InputType): IStatementRunnable {
  return new LangVers(langVersion, mapInput(first));
}
export function verNotLang(langVersion: LanguageVersion, first: InputType): IStatementRunnable {
  return new LangVersNot(langVersion, mapInput(first));
}
export function failCombinator(): IStatementRunnable {
  return new FailCombinator();
}
export function failStar(): IStatementRunnable {
  return new FailStar();
}
 
// Passes through results where the next two tokens do NOT match t1 followed by t2.
class StopBefore2 implements IStatementRunnable {
  private readonly t1: string;
  private readonly t2: string;
 
  public constructor(t1: string, t2: string) {
    this.t1 = t1.toUpperCase();
    this.t2 = t2.toUpperCase();
  }
 
  public listKeywords(): string[] { return []; }
  public getUsing(): string[] { return []; }
 
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
    for (const input of r) {
      const next = input.peek();
      if (next === undefined) { continue; }
      if (next.getUpperStr() === this.t1 && input.peekAt(1)?.getUpperStr() === this.t2) {
        continue;
      }
      result.push(input);
    }
    return result;
  }
 
  public railroad() { return "Railroad.Terminal('stopBefore(" + this.t1 + " " + this.t2 + ")')"; }
  public toStr() { return "stopBefore(" + this.t1 + "," + this.t2 + ")"; }
  public first() { return [""]; }
}
 
export function stopBefore(t1: string, t2: string): IStatementRunnable {
  return new StopBefore2(t1, t2);
}
 
class StopBefore1 implements IStatementRunnable {
  private readonly words: string[];
  public constructor(words: string[]) { this.words = words.map(w => w.toUpperCase()); }
  public listKeywords(): string[] { return []; }
  public getUsing(): string[] { return []; }
  public run(r: Result[]): Result[] {
    const result: Result[] = [];
    for (const input of r) {
      const next = input.peek()?.getUpperStr();
      if (next !== undefined && this.words.includes(next)) { continue; }
      result.push(input);
    }
    return result;
  }
  public railroad() { return "Railroad.Terminal('stopBefore(" + this.words.join("|") + ")')"; }
  public toStr() { return "stopBefore(" + this.words.join(",") + ")"; }
  public first() { return [""]; }
}
 
export function stopBefore1(...words: string[]): IStatementRunnable {
  return new StopBefore1(words);
}