All files / src/abap/3_structures/structures _combi.ts

91.77% Statements 357/389
97.43% Branches 76/78
67.5% Functions 27/40
91.77% Lines 357/389

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 3891x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 185x     185x 185x 1x 1x       1x 1x     1x 1x 159x 159x 1x 1x 16468x 16468x 16468x 37511x 37511x 7510x 7510x 7510x 7510x 7510x 7510x 7510x 7510x 30001x 37511x 29995x 37511x 6x 6x 6x 6x 30001x 30001x 30001x 8958x 8958x 8958x 8958x 8958x 8958x 8958x 8958x 1x 1x 1x 1x 1x 1x 1x 1x 1x 61x     61x 61x 1x 1x 33858x 33858x 60x 60x 671x 697x 251x 697x 446x 446x 697x 671x 60x 33858x 1x 1x 16x 16x 1x 1x       1x 1x     1x 1x 33858x 33858x 33858x 33858x 33858x 1x 1x 1x 1x 1x 1x 1x 1x 33857x 33857x 33858x 28134x 28134x 15122x 15122x 28134x 378x 378x 378x 28134x 18735x 33858x 22572x 22572x 11705x 11705x 22572x 280x 280x 280x 22572x 7030x 33858x 6372x 6372x 6372x 6372x 6372x 6372x 6372x 33858x 658x 658x 658x 658x 658x 658x 658x 658x 33858x 1x 1x 1x 1x 1x 1x 131x 131x 1x 1x     1x 1x     1x 1x 9890x 9890x 9890x 9890x 1x 1x     1x 1x 1x 1x 1x 1x 124x 124x 1x 1x     1x 1x     1x 1x 16820x 16820x 16820x 34662x 7303x 7303x 7303x 7303x 7303x 7303x 7303x 7303x 27359x 27359x 27359x 34662x 9517x 378x 378x 378x 378x 378x 378x 378x 9517x 9139x 9139x 9139x 9139x 9139x 9139x 9139x 9139x 9517x 17842x 34662x 17838x 34662x 4x 4x 4x 4x 17842x 17842x 17842x 16820x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 52x 52x 1x 1x     1x 1x     1x 1x 166x 166x 166x 1x 1x 32103x 52x 52x 52x 52x 32103x 1x 1x 31937x 31937x 31937x 31937x 12120x 31937x 19817x 19817x 31937x 31937x 1x 1x 1x 1x 1x 1x 841x 841x 1x 1x 651x 651x 15x 15x 636x 636x 1x 1x     1x 1x     1x 1x 31224x 31224x 1x 1x 56397x 590x 590x 590x 590x 590x 590x 590x 56397x 25173x 25173x 25173x 25173x 25173x 25173x 25173x 25173x 55807x 30634x 30634x 30634x 30634x 30634x 30634x 30634x 30634x 56397x 1x 1x 63x 63x 63x 1x 61x 61x 61x 1x 122x 122x 122x 1x 131x 131x 131x 1x 124x 124x 124x 1x 841x 841x 841x 1x 1x 343x 343x 52x 52x 343x 343x
import {IStructure} from "./_structure";
import {StructureNode, StatementNode} from "../../nodes";
import {INode} from "../../nodes/_inode";
import {IStatement, MacroCall, NativeSQL} from "../../2_statements/statements/_statement";
import {IStructureRunnable} from "./_structure_runnable";
import {IMatch} from "./_match";
 
class Sequence implements IStructureRunnable {
  private readonly list: IStructureRunnable[];
 
  public constructor(list: IStructureRunnable[]) {
    if (list.length < 2) {
      throw new Error("Sequence, length error");
    }
    this.list = list;
  }
 
  public toRailroad() {
    const children = this.list.map((e) => { return e.toRailroad(); });
    return "Railroad.Sequence(" + children.join() + ")";
  }
 
  public getUsing(): string[] {
    return this.list.reduce((a, c) => { return a.concat(c.getUsing()); }, [] as string[]);
  }
 
  public first() {
    return this.list[0].first();
  }
 
  public run(statements: StatementNode[], parent: INode): IMatch {
    let inn = statements;
    let out: StatementNode[] = [];
    for (const i of this.list) {
      const match = i.run(inn, parent);
      if (match.error) {
        return {
          matched: [],
          unmatched: statements,
          error: true,
          errorDescription: match.errorDescription,
          errorMatched: out.length,
        };
      }
 
      if (match.matched.length < 100) {
        out.push(...match.matched);
      } else {
        // avoid using the spread operator, it might trigger "Maximum call stack size exceeded"
        // when the number of matched elements is very large
        out = out.concat(match.matched);
      }
 
      inn = match.unmatched;
    }
    return {
      matched: out,
      unmatched: inn,
      error: false,
      errorDescription: "",
      errorMatched: 0,
    };
  }
}
 
// Note that the Alternative does not nessesarily return the first in the alternative
// as a map is used for better performance
class Alternative implements IStructureRunnable {
  private readonly list: IStructureRunnable[];
  private map: {[index: string]: IStructureRunnable[]};
 
  public constructor(list: IStructureRunnable[]) {
    if (list.length < 2) {
      throw new Error("Alternative, length error");
    }
    this.list = list;
  }
 
  private setupMap() {
    // dont call from constructor, it will cause infinite loop
    if (this.map === undefined) {
      this.map = {};
      for (const i of this.list) {
        for (const first of i.first()) {
          if (this.map[first]) {
            this.map[first].push(i);
          } else {
            this.map[first] = [i];
          }
        }
      }
    }
  }
 
  public first() {
    return [""];
  }
 
  public toRailroad() {
    const children = this.list.map((e) => { return e.toRailroad(); });
    return "Railroad.Choice(0, " + children.join() + ")";
  }
 
  public getUsing() {
    return this.list.reduce((a, c) => { return a.concat(c.getUsing()); }, [] as string[]);
  }
 
  public run(statements: StatementNode[], parent: INode): IMatch {
    this.setupMap();
    let count = 0;
    let countError = "";
 
    if (statements.length === 0) {
      return {
        matched: [],
        unmatched: statements,
        error: true,
        errorDescription: countError,
        errorMatched: count,
      };
    }
 
    const token = statements[0].getFirstToken().getStr().toUpperCase();
    for (const i of this.map[token] || []) {
      const match = i.run(statements, parent);
      if (match.error === false) {
        return match;
      }
      if (match.errorMatched > count) {
        countError = match.errorDescription;
        count = match.errorMatched;
      }
    }
 
    for (const i of this.map[""] || []) {
      const match = i.run(statements, parent);
      if (match.error === false) {
        return match;
      }
      if (match.errorMatched > count) {
        countError = match.errorDescription;
        count = match.errorMatched;
      }
    }
 
    if (count === 0) {
      return {
        matched: [],
        unmatched: statements,
        error: true,
        errorDescription: "Unexpected code structure",
        errorMatched: count,
      };
    } else {
      return {
        matched: [],
        unmatched: statements,
        error: true,
        errorDescription: countError,
        errorMatched: count,
      };
    }
  }
}
 
class Optional implements IStructureRunnable {
  private readonly obj: IStructureRunnable;
 
  public constructor(obj: IStructureRunnable) {
    this.obj = obj;
  }
 
  public toRailroad() {
    return "Railroad.Optional(" + this.obj.toRailroad() + ")";
  }
 
  public getUsing() {
    return this.obj.getUsing();
  }
 
  public run(statements: StatementNode[], parent: INode): IMatch {
    const ret = this.obj.run(statements, parent);
    ret.error = false;
    return ret;
  }
 
  public first() {
    return [""];
  }
}
 
class Star implements IStructureRunnable {
  private readonly obj: IStructureRunnable;
 
  public constructor(obj: IStructureRunnable) {
    this.obj = obj;
  }
 
  public toRailroad() {
    return "Railroad.ZeroOrMore(" + this.obj.toRailroad() + ")";
  }
 
  public getUsing() {
    return this.obj.getUsing();
  }
 
  public run(statements: StatementNode[], parent: INode): IMatch {
    let inn = statements;
    let out: StatementNode[] = [];
    while (true) {
      if (inn.length === 0) {
        return {
          matched: out,
          unmatched: inn,
          error: false,
          errorDescription: "",
          errorMatched: 0,
        };
      }
 
      const match = this.obj.run(inn, parent);
 
      if (match.error === true) {
        if (match.errorMatched > 0) {
          return {
            matched: out,
            unmatched: inn,
            error: true,
            errorDescription: match.errorDescription,
            errorMatched: match.errorMatched,
          };
        } else {
          return {
            matched: out,
            unmatched: inn,
            error: false,
            errorDescription: "",
            errorMatched: 0,
          };
        }
      }
 
      if (match.matched.length < 100) {
        out.push(...match.matched);
      } else {
        // avoid using the spread operator, it might trigger "Maximum call stack size exceeded"
        // when the number of matched elements is very large
        out = out.concat(match.matched);
      }
 
      inn = match.unmatched;
    }
  }
 
  public first() {
    return [""];
  }
}
 
class SubStructure implements IStructureRunnable {
  private readonly s: IStructure;
  private matcher: IStructureRunnable;
 
  public constructor(s: IStructure) {
    this.s = s;
  }
 
  public toRailroad() {
    return "Railroad.NonTerminal('" + this.s.constructor.name + "', {href: '#/structure/" + this.s.constructor.name + "'})";
  }
 
  public getUsing() {
    return ["structure/" + this.s.constructor.name];
  }
 
  public first() {
    this.setupMatcher();
    return this.matcher.first();
  }
 
  private setupMatcher() {
    if (this.matcher === undefined) {
      // SubStructures are singletons, so the getMatcher can be saved, its expensive to create
      // dont move this to the constructor, as it might trigger infinite recursion
      this.matcher = this.s.getMatcher();
    }
  }
 
  public run(statements: StatementNode[], parent: INode): IMatch {
    const nparent = new StructureNode(this.s);
    this.setupMatcher();
    const ret = this.matcher.run(statements, nparent);
    if (ret.matched.length === 0) {
      ret.error = true;
    } else {
      parent.addChild(nparent);
    }
    return ret;
  }
}
 
class SubStatement implements IStructureRunnable {
  private readonly obj: new () => IStatement;
 
  public constructor(obj: new () => IStatement) {
    this.obj = obj;
  }
 
  public first() {
    const o = new this.obj();
    if (o instanceof MacroCall || o instanceof NativeSQL) {
      return [""];
    }
    return o.getMatcher().first();
  }
 
  public toRailroad() {
    return "Railroad.Terminal('" + this.className() + "', {href: '#/statement/" + this.className() + "'})";
  }
 
  public getUsing() {
    return ["statement/" + this.className()];
  }
 
  private className() {
    return this.obj.name;
  }
 
  public run(statements: StatementNode[], parent: INode): IMatch {
    if (statements.length === 0) {
      return {
        matched: [],
        unmatched: [],
        error: true,
        errorDescription: "Expected " + this.className().toUpperCase(),
        errorMatched: 0,
      };
    } else if (statements[0].get() instanceof this.obj) {
      parent.addChild(statements[0]);
      return {
        matched: [statements[0]],
        unmatched: statements.splice(1),
        error: false,
        errorDescription: "",
        errorMatched: 0,
      };
    } else {
      return {
        matched: [],
        unmatched: statements,
        error: true,
        errorDescription: "Expected " + this.className().toUpperCase(),
        errorMatched: 0,
      };
    }
  }
}
 
export function seq(first: IStructureRunnable, ...rest: IStructureRunnable[]): IStructureRunnable {
  return new Sequence([first].concat(rest));
}
 
export function alt(first: IStructureRunnable, ...rest: IStructureRunnable[]): IStructureRunnable {
  return new Alternative([first].concat(rest));
}
 
export function beginEnd(begin: IStructureRunnable, body: IStructureRunnable, end: IStructureRunnable): IStructureRunnable {
  return new Sequence([begin, body, end]);
}
 
export function opt(o: IStructureRunnable): IStructureRunnable {
  return new Optional(o);
}
 
export function star(s: IStructureRunnable): IStructureRunnable {
  return new Star(s);
}
 
export function sta(s: new () => IStatement): IStructureRunnable {
  return new SubStatement(s);
}
 
const singletons: {[index: string]: SubStructure} = {};
export function sub(s: new () => IStructure): IStructureRunnable {
  if (singletons[s.name] === undefined) {
    singletons[s.name] = new SubStructure(new s());
  }
  return singletons[s.name];
}