All files / src/abap/nodes structure_node.ts

94.48% Statements 257/272
94% Branches 94/100
100% Functions 22/22
94.48% Lines 257/272

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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 2721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39330x 39330x 39330x 1x 1x 344157x 344157x 1x 1x 1x 354x 716x 71x 716x 362x 645x 283x 283x 247x 247x 283x 716x 36x 36x 1x 1x 45x 45x 63x 63x 45x 45x 1x 1x 210x 302x 178x 178x 302x 32x 32x 1x 1x 3673x 3673x 5585x 622x 622x 5585x 3673x 3673x 1x 1x 8141x 8141x 14108x 718x 718x 14108x 8141x 8141x 1x 1x 6259x 6871x 5558x 6871x 740x 1313x 573x 573x 75x 75x 573x 6871x 626x 626x 1x 1x 11004x 20668x 20668x 3009x 3009x 20668x 7995x 7995x 1x 1x 1819x 1819x 1414x 1414x 405x 405x     1x 1x 411x 411x 411x 411x 411x       1x 1x 1435x 1435x 1435x 1435x 1435x       1x 1x 22569x 22569x 43582x 43582x 22569x 22569x 1x 1x 8159x 8159x 8159x 15462x 9042x 15462x 6420x 6420x 15462x 8159x 8159x 1x 1x 5498x 5498x 10468x 10468x 5498x 5498x 1x 1x 79355x 79355x 154390x 58328x 154390x 2705x 2705x 154390x 79355x 79355x 1x 1x 2311x 2311x 4527x 2808x 4527x 1719x 1719x 4527x 2311x 2311x 1x 1x 30281x 30281x 30281x 58325x 33984x 58325x 239x 239x 24341x 24341x 30281x 30281x 1x 1x 2296x 2296x 5565x 183x 183x 5565x 2296x 4174x 2451x 2451x 1723x 1723x 4174x 4373x       4373x 1723x 1723x 1723x 4174x 2113x 2113x 1x 1x 117894x 117894x     117894x 202897x 112822x 202897x 3920x 90075x 86155x 86155x 202897x 117894x 117894x 1x 1x 11594x 2826x 2826x 11594x 21846x 2057x 2057x 21846x 6711x 6711x 1x 1x 29949x     29949x 64211x 44415x 64211x 5368x 19796x 14428x 14428x 14x 14x 14428x 64211x 24567x 24567x 1x 1x
import {AbstractNode} from "./_abstract_node";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {IStatement} from "../2_statements/statements/_statement";
import {IStructure} from "../3_structures/structures/_structure";
import {IStatementRunnable} from "../2_statements/statement_runnable";
import {StatementNode} from "./statement_node";
import {ExpressionNode} from "./expression_node";
 
export class StructureNode extends AbstractNode<StructureNode | StatementNode> {
  private readonly structure: IStructure;
 
  public constructor(structure: IStructure) {
    super();
    this.structure = structure;
  }
 
  public get() {
    return this.structure;
  }
 
  // todo, remove this method, the logic should never go up in the tree
  public findParent(node: StatementNode): StructureNode | undefined {
    for (const child of this.getChildren()) {
      if (child === node) {
        return this;
      } else if (child instanceof StatementNode) {
        continue;
      } else {
        const res = child.findParent(node);
        if (res) {
          return res;
        }
      }
    }
    return undefined;
  }
 
  public concatTokens(): string {
    let concat = "";
    for (const child of this.getChildren()) {
      concat = concat + child.concatTokens();
    }
    return concat;
  }
 
  public findDirectStatement(type: new () => IStatement): StatementNode | undefined {
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode && child.get() instanceof type) {
        return child;
      }
    }
    return undefined;
  }
 
  public findDirectStatements(type: new () => IStatement): StatementNode[] {
    const ret: StatementNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode && child.get() instanceof type) {
        ret.push(child);
      }
    }
    return ret;
  }
 
  public findDirectStructures(type: new () => IStructure): StructureNode[] {
    const ret: StructureNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof StructureNode && child.get() instanceof type) {
        ret.push(child);
      }
    }
    return ret;
  }
 
  public findFirstStatement(type: new () => IStatement): StatementNode | undefined {
    for (const child of this.getChildren()) {
      if (child.get() instanceof type) {
        return child as StatementNode;
      } else if (child instanceof StatementNode) {
        continue;
      } else {
        const res = child.findFirstStatement(type);
        if (res) {
          return res;
        }
      }
    }
    return undefined;
  }
 
  public findFirstExpression(type: new () => IStatementRunnable): ExpressionNode | undefined {
    for (const child of this.getChildren()) {
      const res = child.findFirstExpression(type);
      if (res) {
        return res;
      }
    }
    return undefined;
  }
 
  public getFirstStatement(): StatementNode | undefined {
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        return child;
      }
      return child.getFirstStatement();
    }
    return undefined;
  }
 
  public getFirstToken(): AbstractToken {
    const child = this.getFirstChild();
 
    if (child !== undefined) {
      return child.getFirstToken();
    }

    throw new Error("StructureNode, getFirstToken, unexpected type");
  }
 
  public getLastToken(): AbstractToken {
    const child = this.getLastChild();
 
    if (child !== undefined) {
      return child.getLastToken();
    }

    throw new Error("StructureNode, getLastToken, unexpected type");
  }
 
  public findAllExpressions(type: new () => IStatementRunnable): ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      ret.push(...child.findAllExpressions(type));
    }
    return ret;
  }
 
  public findAllExpressionsRecursive(type: new () => IStatementRunnable): ExpressionNode[] {
    const ret: ExpressionNode[] = [];
 
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        ret.push(...child.findAllExpressionsRecursive(type));
      } else {
        ret.push(...child.findAllExpressionsRecursive(type));
      }
    }
    return ret;
  }
 
  public findAllExpressionsMulti(type: (new () => IStatementRunnable)[]): ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      ret.push(...child.findAllExpressionsMulti(type));
    }
    return ret;
  }
 
  public findAllStatements(type: new () => IStatement): StatementNode[] {
    const ret: StatementNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof StructureNode) {
        ret.push(...child.findAllStatements(type));
      } else if (child.get() instanceof type) {
        ret.push(child);
      }
    }
    return ret;
  }
 
  public findAllStatementNodes(): StatementNode[] {
    const ret: StatementNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        ret.push(child);
      } else {
        ret.push(...child.findAllStatementNodes());
      }
    }
    return ret;
  }
 
  public findAllStructuresRecursive(type: new () => IStructure): StructureNode[] {
    const ret: StructureNode[] = [];
 
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        continue;
      } else if (child.get() instanceof type) {
        ret.push(child);
      }
      ret.push(...child.findAllStructuresRecursive(type));
    }
    return ret;
  }
 
  public findAllStructuresMulti(type: (new () => IStructure)[]): StructureNode[] {
    const ret: StructureNode[] = [];
    for (const t of type) {
      if (this.get() instanceof t) {
        return [this];
      }
    }
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        continue;
      }
 
      let found = false;
      for (const t of type) {
        if (this.get() instanceof t) {
          ret.push(child);
          found = true;
        }
      }
      if (found === false) {
        ret.push(...child.findAllStructuresMulti(type));
      }
    }
    return ret;
  }
 
  public findAllStructures(type: new () => IStructure): StructureNode[] {
    const ret: StructureNode[] = [];
    if (this.get() instanceof type) {
      return [this];
    }
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        continue;
      } else if (child.get() instanceof type) {
        ret.push(child);
      } else {
        ret.push(...child.findAllStructures(type));
      }
    }
    return ret;
  }
 
  public findDirectStructure(type: new () => IStructure): StructureNode | undefined {
    if (this.get() instanceof type) {
      return this;
    }
    for (const child of this.getChildren()) {
      if (child.get() instanceof type) {
        return child as StructureNode;
      }
    }
    return undefined;
  }
 
  public findFirstStructure(type: new () => IStructure): StructureNode | undefined {
    if (this.get() instanceof type) {
      return this;
    }
    for (const child of this.getChildren()) {
      if (child instanceof StatementNode) {
        continue;
      } else if (child.get() instanceof type) {
        return child;
      } else {
        const res = child.findFirstStructure(type);
        if (res) {
          return res;
        }
      }
    }
    return undefined;
  }
 
}