All files / src/bdef bdef_parser.ts

100% Statements 42/42
93.75% Branches 15/16
100% Functions 2/2
100% Lines 42/42

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 431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 1x 1x 32x 32x 32x 32x 32x 33x 10x 10x 22x 22x 1x 1x 1x 32x 32x 1297x 1295x 1295x 1295x 1297x 1x 3x 3x 1x 1297x 32x 32x 1x  
import {Comment} from "../abap/1_lexer/tokens";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {Combi} from "../abap/2_statements/combi";
import {ExpressionNode} from "../abap/nodes";
import {CDSLexer} from "../cds/cds_lexer";
import {IFile} from "../files/_ifile";
import {defaultRelease} from "../version";
import * as Expressions from "./expressions";
 
export class BDEFParser {
  public parse(file: IFile | undefined): ExpressionNode | undefined {
    if (file === undefined) {
      return undefined;
    }
 
    // BDEF has the same comments, strings and delimiters as CDS
    const tokens = this.removePragmas(CDSLexer.run(file).filter(t => !(t instanceof Comment)));
 
    const res = Combi.run(new Expressions.BDEFDefinition(), tokens, defaultRelease);
    if (res === undefined || !(res[0] instanceof ExpressionNode)) {
      return undefined;
    }
    return res[0];
  }
 
  /** pragmas can occur in many places, eg. "##NOT_ASSIGNED_TO_DETACT" or "##UNMAPPED_FIELD[SingletonID]" */
  private removePragmas(tokens: AbstractToken[]): AbstractToken[] {
    const ret: AbstractToken[] = [];
    for (let i = 0; i < tokens.length; i++) {
      if (tokens[i].getStr().startsWith("##") === false) {
        ret.push(tokens[i]);
        continue;
      }
      if (tokens[i + 1]?.getStr() === "[") {
        while (i < tokens.length - 1 && tokens[i].getStr() !== "]") {
          i++;
        }
      }
    }
    return ret;
  }
}