All files / src/abap/5_syntax/structures type_enum.ts

90.48% Statements 38/42
80.77% Branches 21/26
100% Functions 1/1
90.48% Lines 38/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 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 691x 1x 1x     1x 1x 1x   1x   7x       7x 7x       7x 7x 7x 7x     7x   7x   7x 3x 3x     3x   3x     7x 7x 7x 2x 2x 1x       7x 7x 6x   7x 7x 7x 10x         7x 7x 7x     7x    
import * as Expressions from "../../2_statements/expressions";
import * as Statements from "../../2_statements/statements";
import * as Structures from "../../3_structures/structures";
import {StructureNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {IntegerType, IStructureComponent, StructureType} from "../../types/basic";
import {TypedIdentifier} from "../../types/_typed_identifier";
import {ReferenceType} from "../_reference";
 
export class TypeEnum {
  public runSyntax(node: StructureNode, scope: CurrentScope, filename: string): TypedIdentifier[] {
    Iif (!(node.get() instanceof Structures.TypeEnum)) {
      throw new Error("TypeEnum, unexpected type");
    }
 
    const begin = node.findDirectStatement(Statements.TypeEnumBegin);
    Iif (begin === undefined) {
      throw new Error("TypeEnum, unexpected type, begin");
    }
 
    const ret: TypedIdentifier[] = [];
    for (const type of node.findDirectStatements(Statements.Type)) {
      const expr = type.findFirstExpression(Expressions.NamespaceSimpleName);
      Iif (expr === undefined) {
        continue;
      }
      const token = expr.getFirstToken();
      // integer is default if BASE TYPE is not specified
      ret.push(new TypedIdentifier(token, filename, new IntegerType()));
    }
    for (const type of node.findDirectStatements(Statements.TypeEnum)) {
      const expr = type.findFirstExpression(Expressions.NamespaceSimpleName);
      Iif (expr === undefined) {
        continue;
      }
      const token = expr.getFirstToken();
      // integer is default if BASE TYPE is not specified
      ret.push(new TypedIdentifier(token, filename, new IntegerType()));
    }
 
    const baseType = begin.findExpressionAfterToken("TYPE")?.getFirstToken();
    const baseName = baseType?.getStr();
    if (baseType && baseName) {
      const found = scope.findType(baseName);
      if (found) {
        scope.addReference(baseType, found, ReferenceType.TypeReference, filename);
      }
    }
 
    let name = begin.findExpressionAfterToken("STRUCTURE");
    if (name === undefined) {
      name = begin.findFirstExpression(Expressions.NamespaceSimpleName);
    }
    Eif (name) {
      const components: IStructureComponent[] = [];
      for (const r of ret) {
        components.push({
          name: r.getName(),
          type: r.getType(),
        });
      }
      const id = new TypedIdentifier(name.getFirstToken(), filename, new StructureType(components));
      scope.addType(id);
      ret.push(id);
    }
 
    return ret;
  }
}