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

62.22% Statements 28/45
56.82% Branches 25/44
100% Functions 1/1
62.22% Lines 28/45

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 711x 1x 1x 1x   1x 1x   1x     1x   1x 1x   1x 1x 3x 3x         3x         3x 1x     2x   1x 1x 1x               1x             1x     1x     1x 85x         1x     1x      
import * as Expressions from "../../2_statements/expressions";
import * as Statements from "../../2_statements/statements";
import * as Structures from "../../3_structures/structures";
import {StatementNode, StructureNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {TypedIdentifier} from "../../types/_typed_identifier";
import * as Basic from "../../types/basic";
import {IStructureComponent} from "../../types/basic";
import {Static} from "../statements/static";
 
// todo, this is much like DATA, refactor?
export class Statics {
  public runSyntax(node: StructureNode, scope: CurrentScope, filename: string): TypedIdentifier | undefined {
    const name = node.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
    let table: boolean = false;
 
    const components: IStructureComponent[] = [];
    for (const c of node.getChildren()) {
      const ctyp = c.get();
      Iif (c instanceof StatementNode && ctyp instanceof Statements.Static) {
        const found = new Static().runSyntax(c, scope, filename);
        if (found) {
          components.push({name: found.getName(), type: found.getType()});
        }
      } else Iif (c instanceof StructureNode && ctyp instanceof Structures.Statics) {
        const found = new Statics().runSyntax(c, scope, filename);
        if (found) {
          components.push({name: found.getName(), type: found.getType()});
        }
      } else if (c instanceof StatementNode && ctyp instanceof Statements.StaticBegin) {
        Iif (c.findDirectTokenByText("OCCURS")) {
          table = true;
        }
      } else if (c instanceof StatementNode && ctyp instanceof Statements.IncludeType) {
        // INCLUDES
        const typeName = c.findFirstExpression(Expressions.TypeName)?.getFirstToken().getStr();
        let found = scope.findType(typeName)?.getType();
        Iif (found === undefined) {
          const f = scope.getDDIC().lookupTableOrView(typeName);
          if (f instanceof TypedIdentifier) {
            found = f.getType();
          } else {
            found = f;
          }
        }
        Iif (found instanceof Basic.VoidType) {
          if (table === true) {
            return new TypedIdentifier(name, filename, new Basic.TableType(found, {withHeader: true}));
          } else {
            return new TypedIdentifier(name, filename, found);
          }
        }
        Iif (found instanceof Basic.UnknownType) {
          return new TypedIdentifier(name, filename, new Basic.UnknownType("unknown type, " + typeName));
        }
        Iif (!(found instanceof Basic.StructureType)) {
          throw new Error("not structured, " + typeName);
        }
        for (const c of found.getComponents()) {
          components.push(c);
        }
      }
    }
 
    Iif (table === true) {
      return new TypedIdentifier(name, filename, new Basic.TableType(new Basic.StructureType(components), {withHeader: true}));
    } else {
      return new TypedIdentifier(name, filename, new Basic.StructureType(components));
    }
  }
}