All files / src/abap/nodes expression_node.ts

96.39% Statements 294/305
96.42% Branches 108/112
95.65% Functions 22/23
96.39% Lines 294/305

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 3061x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2254798x 2254798x 2254798x 1x 1x     1x 1x 17704519x 17704519x 1x 1x 2254798x 2254798x 2254798x 1x 1x 1x 84647x 84647x 94346x 94346x 84647x 84647x 1x 1x 896629x 896629x 896629x     1x 1x 195x 195x 195x 715x     715x 195x 715x 520x 82x 520x 438x 10x 438x 428x 428x 438x 715x 715x 195x 195x 1x 1x 630945x 630945x 630945x 1090148x     1090148x 630945x 1090148x 459203x 18869x 459203x 440334x 440334x 1090148x 1090148x 630945x 630945x 1x 1x 19x 19x 19x 35x 35x 35x 35x 35x 35x 1x 1x 35x 18x 35x 16x 9x 16x 7x 7x 34x 34x 19x 19x 1x 1x 1x 634450x 634450x 634450x 1024141x 1024141x 634450x 634450x 634450x 1x 1x 1243376x 1243376x 1243376x 684315x 684315x 684315x 559061x 1243376x 634252x 415017x 634252x 219235x 219235x 634252x 559061x 559061x 559061x 1x 1x 58172x 58172x 58172x 58172x 58172x       1x 1x 4571x 4571x 4571x 6375x 2830x 6375x 3545x 3545x 6375x 4571x 4571x 4571x 1x 1x 322x 322x 322x 854x 481x 481x 854x 322x 322x 322x 1x 1x 758654x 1598737x 287654x 287654x 1598737x 471000x 471000x 1x 1x 659x 659x 659x 1881x 1881x 1881x 1881x 1881x 181x 181x 1881x 478x 478x 478x 1x 1x 26426x 26426x 80386x 14228x 14228x 80386x 26426x 26426x 1x 1x 943x 943x 2831x 1917x 5789x 716x 716x 716x 5789x 1917x 2831x 943x 943x 1x 1x 211643x 211643x 403592x 45042x 45042x 403592x 166601x 166601x 1x 1x 259408x 259408x 418153x 212998x 418153x 10836x 10836x 205155x 205155x 259408x 259408x 1x 1x 417964x 417964x 767471x 404253x 767471x 102539x 363218x 260679x 260679x 767471x 417964x 417964x 1x 1x 33398x 33398x 50272x 28083x 28083x 22189x 50272x 46780x 253x 253x 46780x 50272x 22005x 22005x 50272x 33398x 33398x 1x 1x 7174333x 49248x 49248x 7125085x 7174333x 11425290x 6063262x 11425290x 351926x 5362028x 5010102x 5010102x 111598x 111598x 5010102x 11425290x 6661561x 6661561x 1x  
import {TokenNode} from "./token_node";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {INode} from "./_inode";
import {Pragma, StringToken, StringTemplate, StringTemplateBegin, StringTemplateMiddle, StringTemplateEnd, Comment} from "../1_lexer/tokens";
import {IStatementRunnable} from "../2_statements/statement_runnable";
import {AbstractNode} from "./_abstract_node";
 
export class ExpressionNode extends AbstractNode<ExpressionNode | TokenNode> {
  private readonly expression: IStatementRunnable;
 
  public constructor(expression: IStatementRunnable) {
    super();
    this.expression = expression;
  }
 
  public addChild(_n: ExpressionNode | TokenNode): void {
    throw new Error("ExpressionNode: addChild() must not be called, nodes are immutable after setChildren()");
  }
 
  public get(): IStatementRunnable {
    return this.expression;
  }
 
  public setChildren(children: (ExpressionNode | TokenNode)[]): ExpressionNode {
    super.setChildren(children);
    return this;
  }
 
  // not stored on the node, saves a field per ExpressionNode, the statement parser tracks counts itself
  public countTokens(): number {
    let count = 0;
    for (const child of this.getChildren()) {
      count = count + child.countTokens();
    }
    return count;
  }
 
  public getFirstToken(): AbstractToken {
    for (const child of this.getChildren()) {
      return child.getFirstToken();
    }
    throw new Error("ExpressionNode, getFirstToken, no children");
  }
 
  public concatTokensWithLinebreaks(): 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 {
        if (prev && prev.getRow() !== token.getRow()) {
          str = str + "\n" + token.getStr();
        } else {
          str = str + " " + token.getStr();
        }
      }
      prev = token;
    }
    return str;
  }
 
  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 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;
  }
 
  // todo: delete this method?, its slow
  public getTokens(): readonly AbstractToken[] {
    const tokens: AbstractToken[] = [];
 
    for (const c of this.getChildren()) {
      tokens.push(...this.toTokens(c));
    }
 
    return tokens;
  }
 
  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;
  }
 
  public getLastToken(): AbstractToken {
    const child = this.getLastChild();
 
    if (child) {
      return child.getLastToken();
    }

    throw new Error("ExpressionNode, getLastToken, no children");
  }
 
  public getAllTokens(): AbstractToken[] {
    const ret: AbstractToken[] = [];
 
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode) {
        ret.push(child.get());
      } else {
        ret.push(...child.getAllTokens());
      }
    }
 
    return ret;
  }
 
  public getDirectTokens(): readonly AbstractToken[] {
    const ret: AbstractToken[] = [];
 
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode) {
        ret.push(child.get());
      }
    }
 
    return ret;
  }
 
  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 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 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 findDirectExpressionsMulti(type: (new () => IStatementRunnable)[]): ExpressionNode[] {
    const ret: ExpressionNode[] = [];
    for (const child of this.getChildren()) {
      if (child instanceof ExpressionNode) {
        for (const t of type) {
          if (child.get() instanceof t) {
            ret.push(child);
            break;
          }
        }
      }
    }
    return ret;
  }
 
  public findDirectTokenByText(text: string): AbstractToken | undefined {
    const search = text.toUpperCase();
    for (const child of this.getChildren()) {
      if (child instanceof TokenNode && child.get().getStr().toUpperCase() === search) {
        return child.get();
      }
    }
    return undefined;
  }
 
  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 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 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;
  }
 
  public findFirstExpression(type: new () => IStatementRunnable): ExpressionNode | undefined {
    if (this.get() instanceof type) {
      return this;
    }
 
    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;
  }
}