All files / src/abap/5_syntax/expressions method_parameters.ts

79.31% Statements 92/116
68.09% Branches 64/94
100% Functions 10/10
78.76% Lines 89/113

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235  1x 1x   1x 1x 1x     1x                                 1x   8x       8x   8x 9x 9x         3x 3x   2x 2x   4x 2x                           4x 4x   4x       4x 4x 4x 1x 3x 3x           3x 3x 3x 1x   2x 2x     2x     3x 3x 1x 2x   2x             2x 2x 2x 1x   1x 1x     1x     2x   2x               15x 20x 20x 5x   21x 15x 1x   14x       19x                       15x   15x       15x   15x 21x       21x 21x       21x 21x       21x 21x 16x 47x 15x       21x   20x 1x 1x           20x     14x               5x   5x       5x   5x 5x       5x 5x       5x 5x       5x   5x     5x      
import {CurrentScope} from "../_current_scope";
import {VoidType} from "../../types/basic";
import * as Expressions from "../../2_statements/expressions";
import {IMethodDefinition} from "../../types/_method_definition";
import {ExpressionNode} from "../../nodes";
import {InlineData} from "./inline_data";
import {Target} from "./target";
import {AbstractType} from "../../types/basic/_abstract_type";
import {INode} from "../../nodes/_inode";
import {Source} from "./source";
 
// todo, checking that all mandatory parameters are filled
// todo, checking that types are compatible
 
interface IListItemT {
  name: string;
  target: ExpressionNode;
  targetType: AbstractType | undefined;
}
 
interface IListItemS {
  name: string;
  source: ExpressionNode;
  sourceType: AbstractType | undefined;
}
 
export class MethodParameters {
  public runSyntax(node: INode, scope: CurrentScope, method: IMethodDefinition | VoidType, filename: string): void {
    Iif (!(node.get() instanceof Expressions.MethodParameters)) {
      throw new Error("MethodParameters, unexpected input");
    }
 
    const children = node.getChildren().slice();
 
    while (children.length > 0) {
      const name = children.shift()?.getFirstToken().getStr().toUpperCase();
      switch (name) {
        case "EXPORTING":
          this.checkExporting(children.shift(), scope, method, filename);
          break;
        case "IMPORTING":
          this.checkImporting(children.shift(), scope, method, filename);
          break;
        case "CHANGING":
          this.checkChanging(children.shift(), scope, method, filename);
          break;
        case "RECEIVING":
          this.checkReceiving(children.shift(), scope, method, filename);
          break;
        case "EXCEPTIONS":
          children.shift(); // todo, old style exceptions
          break;
        default:
          throw new Error("MethodParameters, unexpected token, " + name);
      }
    }
  }
 
///////////////////////
 
  private checkReceiving(node: INode | undefined, scope: CurrentScope, method: IMethodDefinition | VoidType, filename: string) {
 
    const type = method instanceof VoidType ? method : method.getParameters().getReturning()?.getType();
    Iif (type === undefined) {
      throw new Error("Method does not have a returning parameter");
    } else Iif (!(node instanceof ExpressionNode)) {
      throw new Error("checkReceiving, not an expression node");
    }
 
    const target = node.findDirectExpression(Expressions.Target);
    const inline = target?.findDirectExpression(Expressions.InlineData);
    if (inline) {
      new InlineData().runSyntax(inline, scope, filename, type);
    } else Eif (target) {
      new Target().runSyntax(target, scope, filename);
    }
 
  }
 
  private checkImporting(node: INode | undefined, scope: CurrentScope, method: IMethodDefinition | VoidType, filename: string) {
    for (const item of this.parameterListT(node, scope, filename)) {
      let parameterType: AbstractType | undefined = undefined;
      if (method instanceof VoidType) {
        parameterType = method;
      } else {
        const parameter = method.getParameters().getExporting().find(p => p.getName().toUpperCase() === item.name);
        Iif (parameter === undefined) {
          throw new Error("Method exporting parameter \"" + item.name + "\" does not exist");
        }
        parameterType = parameter.getType();
      }
 
      const inline = item.target.findDirectExpression(Expressions.InlineData);
      if (inline) {
        new InlineData().runSyntax(inline, scope, filename, parameterType);
      } else Iif (item.targetType === undefined) {
        throw new Error("Could not determine target type");
      } else Eif (item.targetType) {
// todo, check that targetType and parameterType are compatible
      }
    }
  }
 
  private checkChanging(node: INode | undefined, scope: CurrentScope, method: IMethodDefinition | VoidType, filename: string) {
    for (const item of this.parameterListT(node, scope, filename)) {
      let parameterType: AbstractType | undefined = undefined;
      if (method instanceof VoidType) {
        parameterType = method;
      } else {
        const parameter = method.getParameters().getChanging().find(p => p.getName().toUpperCase() === item.name);
        Iif (parameter === undefined) {
          throw new Error("Method changing parameter \"" + item.name + "\" does not exist");
        }
        parameterType = parameter.getType();
      }
 
      Eif (item.targetType) {
// todo, check that targetType and parameterType are compatible
        Iif (0) {
          console.log(parameterType); // todo
        }
      }
    }
  }
 
  public checkExporting(node: INode | undefined, scope: CurrentScope, method: IMethodDefinition | VoidType, filename: string) {
    for (const item of this.parameterListS(node, scope, filename, method)) {
      let parameterType: AbstractType | undefined = undefined;
      if (method instanceof VoidType) {
        parameterType = method;
      } else {
        const parameter = method.getParameters().getImporting().find(p => p.getName().toUpperCase() === item.name);
        if (parameter === undefined) {
          throw new Error("Method importing parameter \"" + item.name + "\" does not exist");
        }
        parameterType = parameter.getType();
      }
 
      // todo, check that targetType and parameterType are compatible
      Iif (0) {
        console.log(parameterType); // todo
      }
    }
  }
 
  private parameterListS(
    node: INode | undefined,
    scope: CurrentScope,
    filename: string,
    method: IMethodDefinition | VoidType): IListItemS[] {
 
    Iif (node === undefined) {
      return [];
    } else Iif (!(node.get() instanceof Expressions.ParameterListS)) {
      throw new Error("parameterListS, unexpected node");
    }
 
    const ret: IListItemS[] = [];
 
    for (const c of node.getChildren()) {
      Iif (!(c.get() instanceof Expressions.ParameterS) || !(c instanceof ExpressionNode)) {
        throw new Error("parameterListS, unexpected node, child");
      }
 
      const name = c.findDirectExpression(Expressions.ParameterName)?.getFirstToken().getStr().toUpperCase();
      Iif (name === undefined) {
        throw new Error("parameterListS, no name determined");
      }
 
      const source = c.findDirectExpression(Expressions.Source);
      Iif (source === undefined) {
        throw new Error("parameterListS, no source found");
      }
 
      let targetType: AbstractType | undefined = undefined;
      if (!(method instanceof VoidType)) {
        for (const i of method.getParameters().getImporting()) {
          if (i.getName().toUpperCase() === name) {
            targetType = i.getType();
          }
        }
      }
      let sourceType = new Source().runSyntax(source, scope, filename, targetType);
 
      if (sourceType === undefined) {
        Eif (method instanceof VoidType) {
          sourceType = method;
        } else {
          throw new Error("No source type determined for parameter " + name + " input");
        }
      }
 
      ret.push({name, source, sourceType});
    }
 
    return ret;
  }
 
  private parameterListT(
    node: INode | undefined,
    scope: CurrentScope,
    filename: string): IListItemT[] {
 
    Iif (node === undefined) {
      return [];
    } else Iif (!(node.get() instanceof Expressions.ParameterListT)) {
      throw new Error("parameterListT, unexpected node");
    }
 
    const ret: IListItemT[] = [];
 
    for (const c of node.getChildren()) {
      Iif (!(c.get() instanceof Expressions.ParameterT) || !(c instanceof ExpressionNode)) {
        throw new Error("parameterListT, unexpected node, child");
      }
 
      const name = c.findDirectExpression(Expressions.ParameterName)?.getFirstToken().getStr().toUpperCase();
      Iif (name === undefined) {
        throw new Error("parameterListT, no name determined");
      }
 
      const target = c.findDirectExpression(Expressions.Target);
      Iif (target === undefined) {
        throw new Error("parameterListT, no target found");
      }
 
      const targetType = new Target().runSyntax(target, scope, filename);
 
      ret.push({name, target, targetType});
    }
 
    return ret;
  }
 
}