All files / src/abap/nodes statement_node.ts

97.05% Statements 362/373
94.96% Branches 132/139
100% Functions 28/28
97.05% Lines 362/373

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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 3731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 50927x 50927x 50927x 50927x 50927x 24676x 50927x 26251x 26251x 50927x 1x 1x 533106x 533106x 1x 1x 33544x 33544x 1x 1x 10091x 10091x 1x 1x 50891x     50891x 50891x 50891x 50891x 50891x 1x 1x 5808x 5808x 1x 1x 1033x 1033x 1033x 1x 1x 42444x 42444x 42444x 249111x 249111x 42444x 42444x 42444x 1x 1x 1354x 5254x 345x 345x 5254x 1009x 1009x 1x 1x 1309x 1309x 1309x 4124x 4124x 1309x 1309x 1309x 1x 1x 7152x 7152x 7152x 49397x 1x 1x 49397x 7152x 49397x 42244x 12130x 42244x 30114x 30114x 49396x 49396x 7152x 7152x 1x 1x 11x 11x 11x 40x     40x 40x 40x 11x 40x 29x 29x 13x 29x 16x 16x 40x 40x 11x 11x 1x 1x 947x 947x 947x 3637x 3637x 3637x 3637x 3637x 3637x 26x 26x 3637x 947x 3637x 2664x 1072x 2664x 1592x 1592x 3611x 3611x 947x 947x 1x 1x 1573x 1573x 1x 1x 54183x 54183x 54183x     1x 1x 38936x 38936x 38936x 38936x 38936x       1x 1x 17917x 67427x 6715x 6715x 67427x 11202x 11202x 1x 1x 2814x 2814x 12646x 1937x 1937x 12646x 2814x 2814x 1x 1x 4781x 4781x 24042x 110x 110x 24042x 4671x 4671x 1x 1x 49837x 167570x 100137x 167570x 16043x 67433x 51390x 51390x 4198x 4198x 51390x 167570x 29596x 29596x 1x 1x 38095x 38095x 129093x 94409x 129093x 841x 34684x 33843x 33843x 129093x 38095x 38095x 1x 1x 11047x 11047x 39452x 27569x 39452x 892x 892x 11883x 11883x 11047x 11047x 1x 1x 9093x 9093x 30314x 22452x 22452x 7862x 30314x 24936x 183x 183x 24936x 30314x 7797x 7797x 30314x 9093x 9093x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 94x 94x 94x 94x 94x 94x 8x 8x 94x 22x 94x 64x 64x 94x     1x 1x 2359x 2359x 2359x 9567x 9567x 9567x 9567x 9567x 307x 307x 9567x 2052x 2052x 2052x 1x 1x 14x 14x 14x 14x 155x 155x 155x 155x 155x 7x 7x 155x 14x 14x 14x 1x 1x 1x 1x 292943x 292943x 292943x 227245x 227245x 227245x 65698x 292943x 94389x 50557x 94389x 43832x 43832x 94389x 65698x 65698x 65698x 1x 1x 5490x 5490x 5490x 3137x 3137x 3137x 2353x 5490x 3170x 1804x 3170x 1366x 1366x 3170x 2353x 2353x 2353x 1x
import {Position} from "../../position";
import {VirtualPosition} from "../../virtual_position";
import {AbstractNode} from "./_abstract_node";
import {INode} from "./_inode";
import {TokenNode} from "./token_node";
import {ExpressionNode} from "./expression_node";
import {Comment} from "../1_lexer/tokens/comment";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {Pragma} from "../1_lexer/tokens/pragma";
import {StringToken} from "../1_lexer/tokens/string";
import {StringTemplateMiddle} from "../1_lexer/tokens/string_template_middle";
import {StringTemplateEnd} from "../1_lexer/tokens/string_template_end";
import {StringTemplateBegin} from "../1_lexer/tokens/string_template_begin";
import {StringTemplate} from "../1_lexer/tokens/string_template";
import {IStatement} from "../2_statements/statements/_statement";
import {IStatementRunnable} from "../2_statements/statement_runnable";
 
export class StatementNode extends AbstractNode<ExpressionNode | TokenNode> {
  private readonly statement: IStatement;
  private readonly colon: AbstractToken | undefined;
  private readonly pragmas: readonly AbstractToken[];
 
  public constructor(statement: IStatement, colon?: AbstractToken | undefined, pragmas?: readonly AbstractToken[]) {
    super();
    this.statement = statement;
    this.colon = colon;
 
    if (pragmas) {
      this.pragmas = pragmas;
    } else {
      this.pragmas = [];
    }
  }
 
  public get() {
    return this.statement;
  }
 
  public getColon(): AbstractToken | undefined {
    return this.colon;
  }
 
  public getPragmas(): readonly AbstractToken[] {
    return this.pragmas;
  }
 
  public setChildren(children: (ExpressionNode | TokenNode)[]): StatementNode {
    if (children.length === 0) {
      throw new Error("statement: zero children");
    }
 
    this.children = children;
 
    return this;
  }
 
  public getStart(): Position {
    return this.getFirstToken().getStart();
  }
 
  public getEnd(): Position {
    const last = this.getLastToken();
    return last.getEnd();
  }
 
  public getTokens(): readonly AbstractToken[] {
    const tokens: AbstractToken[] = [];
 
    for (const c of this.getChildren()) {
      tokens.push(...this.toTokens(c));
    }
 
    return tokens;
  }
 
  public includesToken(search: AbstractToken): boolean {
    for (const t of this.getTokens()) {
      if (t.getStart().equals(search.getStart())) {
        return true;
      }
    }
    return false;
  }
 
  public getTokenNodes(): readonly TokenNode[] {
    const tokens: TokenNode[] = [];
 
    for (const c of this.getChildren()) {
      tokens.push(...this.toTokenNodess(c));
    }
 
    return tokens;
  }
 
  public concatTokens(): string {
    let str = "";
    let prev: AbstractToken | undefined;
    for (const token of this.getTokens()) {
      if (token instanceof Pragma) {
        continue;
      }
      if (str === "") {
        str = token.getStr();
      } else if (prev && prev.getStr().length + prev.getCol() === token.getCol()
          && prev.getRow() === token.getRow()) {
        str = str + token.getStr();
      } else {
        str = str + " " + token.getStr();
      }
      prev = token;
    }
    return str;
  }
 
  public concatTokensVirtual(): string {
    let str = "";
    let prev: AbstractToken | undefined;
    for (const token of this.getTokens()) {
      if (token instanceof Pragma) {
        continue;
      }
      const vprev = prev?.getStart() as VirtualPosition | undefined;
      const vtoke = token?.getStart() as VirtualPosition | undefined;
      if (str === "") {
        str = token.getStr();
      } else if (prev && vprev && vtoke
          && prev.getStr().length + vprev.vcol === vtoke.vcol
          && vprev.vrow === vtoke.vrow) {
        str = str + token.getStr();
      } else {
        str = str + " " + token.getStr();
      }
      prev = token;
    }
    return str;
  }
 
  public concatTokensWithoutStringsAndComments(): string {
    let str = "";
    let prev: AbstractToken | undefined;
    for (const token of this.getTokens()) {
      if (token instanceof Comment
          || token instanceof StringToken
          || token instanceof StringTemplate
          || token instanceof StringTemplateBegin
          || token instanceof StringTemplateMiddle
          || token instanceof StringTemplateEnd) {
        continue;
      }
      if (str === "") {
        str = token.getStr();
      } else if (prev && prev.getStr().length + prev.getCol() === token.getCol()
          && prev.getRow() === token.getRow()) {
        str = str + token.getStr();
      } else {
        str = str + " " + token.getStr();
      }
      prev = token;
    }
    return str;
  }
 
  public getTerminator(): string {
    return this.getLastToken().getStr();
  }
 
  public getFirstToken(): AbstractToken {
    for (const child of this.getChildren()) {
      return child.getFirstToken();
    }
    throw new Error("StatementNode, getFirstToken, no children, " + this.get().constructor.name);
  }
 
  public getLastToken(): AbstractToken {
    const child = this.getLastChild();
 
    if (child !== undefined) {
      return child.getLastToken();
    }

    throw new Error("StatementNode, getLastToken, no children");
  }
 
  public findDirectExpression(type: new () => IStatementRunnable): ExpressionNode | undefined {
    for (const child of this.getChildren()) {
      if (child instanceof ExpressionNode && child.get() instanceof type) {
        return child;
      }
    }
    return undefined;
  }
 
  public findDirectExpressions(type: new () => IStatementRunnable): readonly ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof ExpressionNode && child.get() instanceof type) {
        ret.push(child);
      }
    }
    return ret;
  }
 
  public findDirectTokenByText(text: string): AbstractToken | undefined {
    const upper = text.toUpperCase();
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode && child.get().getStr().toUpperCase() === upper) {
        return child.get();
      }
    }
    return undefined;
  }
 
  public findFirstExpression(type: new () => IStatementRunnable): ExpressionNode | undefined {
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode) {
        continue;
      } else if (child.get() instanceof type) {
        return child;
      } else {
        const res = child.findFirstExpression(type);
        if (res) {
          return res;
        }
      }
    }
    return undefined;
  }
 
  public findAllExpressions(type: new () => IStatementRunnable): readonly ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode) {
        continue;
      } else if (child.get() instanceof type) {
        ret.push(child);
      } else {
        ret.push(...child.findAllExpressions(type));
      }
    }
    return ret;
  }
 
  public findAllExpressionsRecursive(type: new () => IStatementRunnable): readonly ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode) {
        continue;
      } else if (child.get() instanceof type) {
        ret.push(child);
      }
      ret.push(...child.findAllExpressionsRecursive(type));
    }
    return ret;
  }
 
  public findAllExpressionsMulti(type: (new () => IStatementRunnable)[], recursive = false): ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode) {
        continue;
      }
      const before = ret.length;
      for (const t of type) {
        if (child.get() instanceof t) {
          ret.push(child);
        }
      }
      if (before === ret.length || recursive === true) {
        ret.push(...child.findAllExpressionsMulti(type, recursive));
      }
    }
    return ret;
  }
 
  /**
   * Returns the Position of the first token if the sequence is found,
   * otherwise undefined. Strings and Comments are ignored in this search.
   * @param first - Text of the first Token
   * @param second - Text of the second Token
   */
  public findTokenSequencePosition(first: string, second: string): Position | undefined {
    let prev: AbstractToken | undefined;
    for (const token of this.getTokens()) {
      if (token instanceof Comment
          || token instanceof StringToken
          || token instanceof StringTemplate
          || token instanceof StringTemplateBegin
          || token instanceof StringTemplateMiddle
          || token instanceof StringTemplateEnd) {
        continue;
      }
      if (prev && token.getStr().toUpperCase() === second && prev?.getStr().toUpperCase() === first.toUpperCase()) {
        return prev.getStart();
      } else {
        prev = token;
      }
    }
    return undefined;
  }
 
  public findExpressionAfterToken(text: string): ExpressionNode | undefined {
    const children = this.getChildren();
 
    for (let i = 0; i < children.length - 1; i++) {
      const c = children[i];
      const next = children[i + 1];
      if (c instanceof TokenNode
          && c.get().getStr().toUpperCase() === text.toUpperCase()
          && next instanceof ExpressionNode) {
        return next;
      }
    }
 
    return undefined;
  }
 
  public findExpressionsAfterToken(text: string): ExpressionNode[] {
    const children = this.getChildren();
    const ret = [];
 
    for (let i = 0; i < children.length - 1; i++) {
      const c = children[i];
      const next = children[i + 1];
      if (c instanceof TokenNode
          && c.get().getStr().toUpperCase() === text.toUpperCase()
          && next instanceof ExpressionNode) {
        ret.push(next);
      }
    }
 
    return ret;
  }
 
////////////////////////////////
 
  private toTokens(b: INode): readonly AbstractToken[] {
    const tokens: AbstractToken[] = [];
 
    if (b instanceof TokenNode) {
      tokens.push(b.get());
      return tokens;
    }
 
    for (const c of b.getChildren()) {
      if (c instanceof TokenNode) {
        tokens.push(c.get());
      } else {
        tokens.push(...this.toTokens(c));
      }
    }
 
    return tokens;
  }
 
  private toTokenNodess(b: INode): readonly TokenNode[] {
    const tokens: TokenNode[] = [];
 
    if (b instanceof TokenNode) {
      tokens.push(b);
      return tokens;
    }
 
    for (const c of b.getChildren()) {
      if (c instanceof TokenNode) {
        tokens.push(c);
      } else {
        tokens.push(...this.toTokenNodess(c));
      }
    }
 
    return tokens;
  }
}