All files / src/ddl ddl_parser.ts

97.26% Statements 71/73
64.28% Branches 9/14
100% Functions 2/2
97.26% Lines 71/73

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 731x 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 4x 4x 4x 4x 1x 1x 4x     4x 4x 1x 1x 4x 4x 4x 4x 4x 6x 6x 1x 1x 1x 1x 1x 1x 6x 5x 5x 5x 5x 5x 5x 5x 5x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x
import {Combi} from "../abap/2_statements/combi";
import {ExpressionNode} from "../abap/nodes";
import {IFile} from "../files/_ifile";
import {defaultVersion} from "../version";
import {DDLLexer} from "./ddl_lexer";
import * as Expressions from "./expressions";
 
export enum DDLKind {
  Structure = "structure",
  Table = "table",
}
 
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 {
    const tokens = DDLLexer.run(file);
 
    let res = Combi.run(new Expressions.DDLStructure(), tokens, defaultVersion);
    if (res === undefined) {
      res = Combi.run(new Expressions.DDLTable(), tokens, defaultVersion);
    }
    if (res === undefined || !(res[0] instanceof ExpressionNode)) {
      return undefined;
    }
    return this.parsedToResult(res[0]);
  }
 
  private parsedToResult(node: ExpressionNode): IDDLParserResult {
    const fields: IDDLParserResultField[] = [];
    let found = node.findDirectExpressions(Expressions.DDLStructureField);
    found = found.concat(node.findDirectExpressions(Expressions.DDLTableField));
    found = found.concat(node.findDirectExpressions(Expressions.DDLInclude));
    for (const f of found) {
      const name = f.findDirectExpression(Expressions.DDLName)?.concatTokens() || "";
      if (f.get() instanceof Expressions.DDLInclude) {
        fields.push({
          name: ".INCLUDE",
          type: name,
          key: false,
          notNull: false,
        });
      } else {
        const type = f.findDirectExpression(Expressions.DDLType)?.concatTokens() || "";
        fields.push({
          name,
          type,
          key: false,
          notNull: false,
        });
      }
    }
 
    const result: IDDLParserResult = {
      name: node.findDirectExpression(Expressions.DDLName)!.concatTokens(),
      kind: node.get() instanceof Expressions.DDLStructure ? DDLKind.Structure : DDLKind.Table,
      fields,
    };
 
    return result;
  }
}