All files / src/ddl ddl_parser.ts

96.74% Statements 119/123
85.41% Branches 41/48
100% Functions 6/6
96.74% Lines 119/123

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 1241x 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 30x 30x 30x 30x 30x 25x 25x 30x 9x 9x 30x 5x 5x 30x 3x 3x 27x 27x 1x 1x 27x 27x 196x 86x 86x 196x 196x 196x 196x 51x 51x 51x 196x 4x 4x 196x 4x 4x 4x 4x 47x 43x 43x 43x 43x 196x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1x 1x 125x     125x 125x 1x 1x 51x 54x 54x 54x 51x 54x 26x 54x     1x 1x 51x 51x 211x 211x 211x 211x 26x 26x 1x 1x 27x 27x 27x 27x 16x 16x 1x  
import {Comment} from "../abap/1_lexer/tokens";
import {Combi} from "../abap/2_statements/combi";
import {ExpressionNode} from "../abap/nodes";
import {IFile} from "../files/_ifile";
import {defaultRelease} from "../version";
import {CDSLexer} from "../cds/cds_lexer";
import * as Expressions from "./expressions";
 
export enum DDLKind {
  Structure = "structure",
  Table = "table",
  Aspect = "aspect",
  ExtendType = "extend-type",
}
 
export interface IDDLParserResultField {
  key: boolean,
  name: string,
  type: string,
  notNull: boolean,
}
 
export interface IDDLParserResult {
  name: string,
  kind: DDLKind,
  fields: IDDLParserResultField[];
}
 
export class DDLParser {
  public parse(file: IFile): IDDLParserResult | undefined {
    let tokens = CDSLexer.run(file);
    tokens = tokens.filter(t => !(t instanceof Comment));
 
    let res = Combi.run(new Expressions.DDLStructure(), tokens, defaultRelease);
    if (res === undefined) {
      res = Combi.run(new Expressions.DDLTable(), tokens, defaultRelease);
    }
    if (res === undefined) {
      res = Combi.run(new Expressions.DDLAspect(), tokens, defaultRelease);
    }
    if (res === undefined) {
      res = Combi.run(new Expressions.DDLExtendType(), tokens, defaultRelease);
    }
    if (res === undefined || !(res[0] instanceof ExpressionNode)) {
      return undefined;
    }
    return this.parsedToResult(res[0]);
  }
 
  private parsedToResult(node: ExpressionNode): IDDLParserResult {
    const fields: IDDLParserResultField[] = [];
    for (const child of node.getChildren()) {
      if (!(child instanceof ExpressionNode)) { continue; }
      const expr = child.get();
      const isField = expr instanceof Expressions.DDLStructureField
        || expr instanceof Expressions.DDLTableField
        || expr instanceof Expressions.DDLInclude
        || expr instanceof Expressions.DDLNamedInclude;
      if (!isField) { continue; }
 
      const key = this.hasKey(child);
      const notNull = this.hasNotNull(child);
      if (expr instanceof Expressions.DDLInclude) {
        const target = this.compactTokens(child.findDirectExpression(Expressions.DDLName));
        fields.push({name: ".INCLUDE", type: target, key, notNull});
      } else if (expr instanceof Expressions.DDLNamedInclude) {
        const names = child.findDirectExpressions(Expressions.DDLName);
        const alias = this.compactTokens(names[0]);
        const target = this.compactTokens(names[1]);
        fields.push({name: alias, type: target, key, notNull});
      } else {
        const name = this.compactTokens(child.findDirectExpression(Expressions.DDLName));
        const type = this.compactTokens(child.findDirectExpression(Expressions.DDLType));
        fields.push({name, type, key, notNull});
      }
    }
 
    const result: IDDLParserResult = {
      name: this.compactTokens(node.findDirectExpression(Expressions.DDLName)),
      kind: this.kindOf(node),
      fields,
    };
 
    return result;
  }
 
  private compactTokens(node: ExpressionNode | undefined): string {
    if (node === undefined) {
      return "";
    }
    return node.concatTokens().replace(/\s+/g, "");
  }
 
  private hasKey(node: ExpressionNode): boolean {
    for (const c of node.getChildren()) {
      const expr = (c as any).get?.();
      const ctor = expr?.constructor?.name;
      if (ctor === "CDSAnnotation") { continue; }
      const tok = c.concatTokens().toUpperCase().trim();
      if (tok === "KEY") { return true; }
      if (tok !== "") { return false; }
    }
    return false;
  }
 
  private hasNotNull(node: ExpressionNode): boolean {
    const children = node.getChildren();
    for (let i = 0; i < children.length - 1; i++) {
      const a = (children[i] as any).concatTokens?.().toUpperCase().trim();
      const b = (children[i + 1] as any).concatTokens?.().toUpperCase().trim();
      if (a === "NOT" && b === "NULL") { return true; }
    }
    return false;
  }
 
  private kindOf(node: ExpressionNode): DDLKind {
    const expr = node.get();
    if (expr instanceof Expressions.DDLStructure) { return DDLKind.Structure; }
    if (expr instanceof Expressions.DDLAspect) { return DDLKind.Aspect; }
    if (expr instanceof Expressions.DDLExtendType) { return DDLKind.ExtendType; }
    return DDLKind.Table;
  }
}