All files / src/abap/2_statements expand_macros.ts

98.06% Statements 202/206
94.83% Branches 55/58
100% Functions 12/12
98.06% Lines 202/206

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 2061x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5356x 5356x 38x 38x 5356x 1x 1x 35x     35x 35x 1x 1x 37x 37x 1x 1x 26x 26x 1x 1x 389x 37x 37x 352x 352x 1x 1x 1x 1x 1x 1x 1x 5356x 5356x 5356x 1x 1x 5374x 5374x 5374x 5374x 13797x 13797x 13797x 46x 46x 46x 13797x 73x 35x 35x 73x 38x 38x 38x 73x 13797x 5374x 1x 1x 5411x 5411x 5411x 5411x 13836x 354x 354x 37x 37x 37x 37x 37x 46x 46x 37x     37x 37x 354x 317x 317x 354x 13799x 13799x 5411x 5411x 5411x 1x 1x 1x 1x 37x 37x 11x 11x 26x 26x 37x 39x 39x 1x 1x 1x 39x 39x 26x 26x 26x 37x 13x 13x 13x 13x 13x 26x 26x 26x 26x 26x 26x 26x 1x 1x 26x 26x 26x 26x 26x 19x 19x 19x 8x 8x 19x 19x 19x 19x 8x 19x 11x 11x 19x 19x 6x 19x 13x 13x 13x 13x 19x 26x 26x 26x 1x 1x 354x 354x 354x 706x 285x 706x 363x 354x 363x 9x 9x 421x 1x 58x 57x 57x 363x 363x 354x 354x 1x 1x 75x 75x 75x 258x 258x 75x 75x 75x 1x 1x
import * as Statements from "./statements";
import * as Tokens from "../1_lexer/tokens";
import {MacroContent, Comment, Unknown, MacroCall} from "./statements/_statement";
import {StatementNode} from "../nodes/statement_node";
import {Token} from "../1_lexer/tokens/_token";
import {TokenNode} from "../nodes/token_node";
import {Version} from "../../version";
import {StatementParser} from "./statement_parser";
import {MemoryFile} from "../../files/memory_file";
import {Lexer} from "../1_lexer/lexer";
import {VirtualPosition} from "../../position";
 
class Macros {
  private readonly macros: {[index: string]: StatementNode[]};
 
  public constructor(globalMacros: readonly string[]) {
    this.macros = {};
    for (const m of globalMacros) {
      this.macros[m.toUpperCase()] = [];
    }
  }
 
  public addMacro(name: string, contents: StatementNode[]): void {
    if (this.isMacro(name)) {
      return;
    }
    this.macros[name.toUpperCase()] = contents;
  }
 
  public getContents(name: string): StatementNode[] | undefined {
    return this.macros[name.toUpperCase()];
  }
 
  public listMacroNames(): string[] {
    return Object.keys(this.macros);
  }
 
  public isMacro(name: string): boolean {
    if (this.macros[name.toUpperCase()]) {
      return true;
    }
    return false;
  }
}
 
export class ExpandMacros {
  private readonly macros: Macros;
  private readonly version: Version;
 
  public constructor(globalMacros: readonly string[], version: Version) {
    this.macros = new Macros(globalMacros);
    this.version = version;
  }
 
  public find(statements: StatementNode[]) {
    let name: string | undefined = undefined;
    let contents: StatementNode[] = [];
 
    for (let i = 0; i < statements.length; i++) {
      const statement = statements[i];
 
      if (statement.get() instanceof Statements.Define) {
        // todo, will this break if first token is a pragma?
        name = statement.getTokens()[1].getStr();
        contents = [];
      } else if (name) {
        if (statement.get() instanceof Statements.EndOfDefinition) {
          this.macros.addMacro(name, contents);
          name = undefined;
        } else if (!(statement.get() instanceof Comment)) {
          statements[i] = new StatementNode(new MacroContent()).setChildren(this.tokensToNodes(statement.getTokens()));
          contents.push(statements[i]);
        }
      }
    }
  }
 
  public handleMacros(statements: readonly StatementNode[]): {statements: StatementNode[], containsUnknown: boolean} {
    const result: StatementNode[] = [];
    let containsUnknown = false;
 
    for (const statement of statements) {
      if (statement.get() instanceof Unknown || statement.get() instanceof MacroCall) {
        const macroName = this.findName(statement.getTokens());
        if (macroName && this.macros.isMacro(macroName)) {
          result.push(new StatementNode(new MacroCall()).setChildren(this.tokensToNodes(statement.getTokens())));
 
          const expanded = this.expandContents(macroName, statement);
          const handled = this.handleMacros(expanded);
          for (const e of handled.statements) {
            result.push(e);
          }
          if (handled.containsUnknown === true) {
            containsUnknown = true;
          }
 
          continue;
        } else {
          containsUnknown = true;
        }
      }
      result.push(statement);
    }
 
    return {statements: result, containsUnknown};
  }
 
  //////////////
 
  private expandContents(name: string, statement: StatementNode): readonly StatementNode[] {
    const contents = this.macros.getContents(name);
    if (contents === undefined || contents.length === 0) {
      return [];
    }
 
    let str = "";
    for (const c of contents) {
      let concat = c.concatTokens();
      if (c.getTerminator() === ",") {
        // workaround for chained statements
        concat = concat.replace(/,$/, ".");
      }
      str += concat + "\n";
    }
 
    const inputs = this.buildInput(statement);
    let i = 1;
    for (const input of inputs) {
      const search = "&" + i;
      const reg = new RegExp(search, "g");
      str = str.replace(reg, input);
      i++;
    }
 
    const file = new MemoryFile("expand_macros.abap.prog", str);
    const lexerResult = Lexer.run(file, statement.getFirstToken().getStart());
 
    const result = new StatementParser(this.version).run([lexerResult], this.macros.listMacroNames());
    return result[0].statements;
  }
 
  private buildInput(statement: StatementNode): string[] {
    const result: string[] = [];
    const tokens = statement.getTokens();
 
    let build = "";
    for (let i = 1; i < tokens.length - 1; i++) {
      const now = tokens[i];
      let next: Token | undefined = tokens[i + 1];
      if (i + 2 === tokens.length) {
        next = undefined; // dont take the punctuation
      }
 
      // argh, macros is a nightmare
      let end = now.getStart();
      if (end instanceof VirtualPosition) {
        end = new VirtualPosition(end, end.vrow, end.vcol + now.getStr().length);
      } else {
        end = now.getEnd();
      }
 
      if (next && next.getStart().equals(end)) {
        build += now.getStr();
      } else {
        build += now.getStr();
        result.push(build);
        build = "";
      }
    }
 
    return result;
  }
 
  private findName(tokens: readonly Token[]): string | undefined {
    let macroName: string | undefined = undefined;
    let previous: Token | undefined = undefined;
    for (const i of tokens) {
      if (previous && previous?.getEnd().getCol() !== i.getStart().getCol()) {
        break;
      } else if (i instanceof Tokens.Identifier || i.getStr() === "-") {
        if (macroName === undefined) {
          macroName = i.getStr();
        } else {
          macroName += i.getStr();
        }
      } else if (i instanceof Tokens.Pragma) {
        continue;
      } else {
        break;
      }
      previous = i;
    }
    return macroName;
  }
 
  private tokensToNodes(tokens: readonly Token[]): TokenNode[] {
    const ret: TokenNode[] = [];
 
    for (const t of tokens) {
      ret.push(new TokenNode(t));
    }
 
    return ret;
  }
 
}