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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20405x 20405x 115x 115x 115x 115x 115x 20405x 1x 1x 139x 139x 139x 139x 139x 139x 1x 1x 114x 114x 1x 1x 93x 93x 1x 1x 1937x 116x 116x 1821x 1821x 1x 1x 116x 116x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20405x 20405x 20405x 20405x 20405x 20405x 20405x 1x 1x 5x 5x 1x 1x 20577x 20577x 20577x 20577x 20577x 20577x 20500x 20500x 20577x 20577x 264959x 264959x 264959x 264959x 150x 150x 150x 150x 264959x 147x 147x 147x 147x 99x 99x 99x 99x 72x 72x 72x 72x 72x 99x 264809x 288x 139x 139x 139x 139x 288x 148x 148x 148x 288x 264959x 20577x 1x 1x 20614x 20614x 20614x 20614x 20614x 20614x 20614x 264838x 264838x 1798x 1798x 116x 116x 116x 101x 82x 82x 101x 101x 116x 116x 116x 116x 114x 114x 114x 114x 120x 120x 114x 116x 2x 2x 2x 116x 116x 1798x 1682x 1682x 1798x 264722x 264722x 20614x 20614x 20614x 1x 1x 1x 1x 114x 114x 26x 26x 88x 88x 114x 106x 106x 1x 1x 1x 106x 106x 88x 88x 88x 114x 65x 65x 65x 65x 65x 65x 88x 88x 88x 88x 88x 88x 88x 1x 1x 88x 88x 88x 88x 88x 71x 71x 71x 51x 51x 71x 71x 71x 71x 9x 71x 62x 62x 71x 71x 6x 71x 65x 65x 65x 65x 71x 88x 88x 88x 1x 1x 1803x 1803x 1803x 3711x 1571x 3711x 1926x 1803x 1926x 123x 123x 2140x 1x 214x 213x 213x 1926x 1926x 1803x 1803x 1x 1x 266x 266x 266x 932x 932x 266x 266x 266x 1x 1x | import * as Statements from "./statements";
import * as Expressions from "./expressions";
import * as Tokens from "../1_lexer/tokens";
import {MacroContent, Comment, Unknown, MacroCall, MacroRecursion} from "./statements/_statement";
import {StatementNode} from "../nodes/statement_node";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {TokenNode} from "../nodes/token_node";
import {ABAPRelease, LanguageVersion} from "../../version";
import {StatementParser} from "./statement_parser";
import {MemoryFile} from "../../files/memory_file";
import {Lexer} from "../1_lexer/lexer";
import {VirtualPosition} from "../../virtual_position";
import {IRegistry} from "../../_iregistry";
import {Program} from "../../objects/program";
import {IFile} from "../../files/_ifile";
import {Position} from "../../position";
class Macros {
private readonly macros: {[index: string]: {
statements: StatementNode[],
filename: string | undefined,
}};
public constructor(globalMacros: readonly string[]) {
this.macros = {};
for (const m of globalMacros) {
this.macros[m.toUpperCase()] = {
statements: [],
filename: undefined,
};
}
}
public addMacro(name: string, contents: StatementNode[], filename: string): void {
if (this.isMacro(name)) {
return;
}
this.macros[name.toUpperCase()] = {
statements: contents,
filename: filename,
};
}
public getContents(name: string): StatementNode[] | undefined {
return this.macros[name.toUpperCase()].statements;
}
public listMacroNames(): string[] {
return Object.keys(this.macros);
}
public isMacro(name: string): boolean {
if (this.macros[name.toUpperCase()]) {
return true;
}
return false;
}
public getMacroFilename(name: string): string | undefined {
return this.macros[name.toUpperCase()].filename;
}
}
export class ExpandMacros {
private readonly macros: Macros;
private readonly globalMacros: readonly string[];
private readonly release: ABAPRelease;
private readonly languageVersion: LanguageVersion;
private readonly reg?: IRegistry;
// "reg" must be supplied if there are cross object macros via INCLUDE
public constructor(globalMacros: readonly string[], release: ABAPRelease, reg?: IRegistry,
languageVersion: LanguageVersion = LanguageVersion.Normal) {
this.macros = new Macros(globalMacros);
this.release = release;
this.languageVersion = languageVersion;
this.globalMacros = globalMacros;
this.reg = reg;
}
public listMacroNames(): string[] {
return this.macros.listMacroNames();
}
public find(statements: StatementNode[], file: IFile, clear = true, includePath: Set<string> = new Set()) {
let nameToken: AbstractToken | undefined = undefined;
let start: Position | undefined = undefined;
let contents: StatementNode[] = [];
const macroReferences = this.reg?.getMacroReferences();
if (clear) {
macroReferences?.clear(file.getFilename());
}
for (let i = 0; i < statements.length; i++) {
const statement = statements[i];
const type = statement.get();
if (type instanceof Statements.Define) {
// todo, will this break if first token is a pragma?
nameToken = statement.getTokens()[1];
start = statement.getFirstToken().getStart();
contents = [];
} else if (type instanceof Statements.Include) {
const includeName = statement.findDirectExpression(Expressions.IncludeName)?.concatTokens();
// todo, this does not take function module includes into account
const prog = this.reg?.getObject("PROG", includeName) as Program | undefined;
if (prog) {
prog.parse(this.release, this.globalMacros, this.reg, this.languageVersion);
const includeMainFile = prog.getMainABAPFile();
// an include that is already on the current include path is not walked again
if (includeMainFile && includePath.has(includeMainFile.getFilename()) === false) {
includePath.add(includeMainFile.getFilename());
// slow, this copies everything,
this.find([...includeMainFile.getStatements()], includeMainFile, false, includePath);
includePath.delete(includeMainFile.getFilename());
}
}
} else if (nameToken) {
if (type instanceof Statements.EndOfDefinition) {
this.macros.addMacro(nameToken.getStr(), contents, file.getFilename());
macroReferences?.addDefinition({filename: file.getFilename(), token: nameToken}, start!, statement.getLastToken().getEnd());
nameToken = undefined;
} else if (!(type instanceof Comment)) {
statements[i] = new StatementNode(new MacroContent()).setChildren(this.tokensToNodes(statement.getTokens()));
contents.push(statements[i]);
}
}
}
}
public handleMacros(statements: readonly StatementNode[], file: IFile,
seen: ReadonlySet<string> = new Set()): {statements: StatementNode[], containsUnknown: boolean} {
const result: StatementNode[] = [];
let containsUnknown = false;
const macroReferences = this.reg?.getMacroReferences();
for (const statement of statements) {
const type = statement.get();
if (type instanceof Unknown || type instanceof MacroCall) {
const macroName = ExpandMacros.findName(statement.getTokens());
if (macroName && this.macros.isMacro(macroName)) {
const filename = this.macros.getMacroFilename(macroName);
if (filename) {
macroReferences?.addReference({
filename: filename,
token: statement.getFirstToken(),
});
}
result.push(new StatementNode(new MacroCall(), statement.getColon()).setChildren(this.tokensToNodes(statement.getTokens())));
if (!seen.has(macroName.toUpperCase())) {
const expanded = this.expandContents(macroName, statement);
const nextSeen = new Set(seen).add(macroName.toUpperCase());
const handled = this.handleMacros(expanded, file, nextSeen);
for (const e of handled.statements) {
result.push(e);
}
if (handled.containsUnknown === true) {
containsUnknown = true;
}
} else {
const node = new StatementNode(new MacroRecursion(), statement.getColon());
result.push(node.setChildren(this.tokensToNodes(statement.getTokens())));
}
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");
// a function replacement: "$`" and "$'" in the argument are text, not patterns
str = str.replace(reg, () => input);
i++;
}
const file = new MemoryFile("expand_macros.abap.prog", str);
const lexerResult = new Lexer().run(file, statement.getFirstToken().getStart());
const result = new StatementParser(this.release, this.reg, this.languageVersion).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: AbstractToken | 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;
}
public static findName(tokens: readonly AbstractToken[]): string | undefined {
let macroName: string | undefined = undefined;
let previous: AbstractToken | 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 AbstractToken[]): TokenNode[] {
const ret: TokenNode[] = [];
for (const t of tokens) {
ret.push(new TokenNode(t));
}
return ret;
}
} |