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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14118x 14118x 1x 1x 14118x 14118x 14118x 14118x 14118x 14118x 14118x 14118x 1072x 1072x 13046x 13046x 13046x 13046x 13046x 13046x 13046x 13046x 1x 1x 13046x 13046x 13046x 5912x 5912x 20728x 20728x 20728x 20728x 20728x 5912x 5912x 5912x 5912x 5912x 5912x 5912x 5912x 13046x 13046x 13046x 1x 1x 13046x 13046x 13046x 4323x 4323x 4323x 4323x 4323x 4323x 4323x 4323x 4323x 13046x 13046x 13046x 1x 1x 13046x 13046x 13046x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 2308x 13046x 13046x 13046x 1x 1x 13046x 13046x 13046x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 1x 6034x 6033x 21x 21x 6034x 6034x 6034x 22x 6034x 6012x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 6034x 13046x 13046x 13046x 1x 1x 1x 1x 1x 1x 6034x 6034x 20048x 20048x 20048x 20048x 3x 3x 20048x 6031x 6031x 1x 1x 8342x 8342x 2089x 2089x 2089x 5x 5x 5x 5x 2089x 2089x 2089x 2089x 1x 1x 1x 1x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 2089x 8342x 8342x 1x 1x 20410x 14256x 14256x 6154x 6154x 20410x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 6154x 6154x 6154x 1x 1x 20410x 14256x 14256x 6154x 6154x 20410x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 2050x 6154x 6154x 6154x 1x 1x 20410x 14256x 14256x 6154x 6154x 20410x 115x 115x 6039x 6039x 6039x 20410x 9x 9x 9x 9x 9x 20410x 10145x 10145x 10145x 10145x 10145x 10145x 10145x 10145x 10145x 6039x 20410x 9x 9x 20410x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 20410x 2035x 2035x 2035x 2035x 2035x 2035x 2035x 2035x 2035x 6039x 6039x 6039x 1x 1x 10239x 10239x 10239x 10239x 10239x 1x 1x 20410x 14256x 14256x 6154x 6154x 20410x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 20945x 6154x 6154x 1x 1x 1x 20945x 20945x 20945x 20945x 13239x 21492x 21492x 21492x 21492x 21492x 21492x 21492x 21492x 21492x 13239x 20945x 20945x 20945x 2227x 2249x 2249x 2249x 2249x 2249x 2249x 2249x 2249x 2249x 2227x 20945x 20945x 20945x 4129x 4136x 4136x 4136x 4136x 4136x 4136x 4136x 4136x 4136x 4129x 20945x 20945x 20945x 9202x 9202x 9202x 9202x 9202x 9202x 9202x 9202x 9202x 20945x 20945x 20945x 1x 1x | import * as Structures from "../3_structures/structures";
import * as Expressions from "../2_statements/expressions";
import * as Statements from "../2_statements/statements";
import {StructureNode, StatementNode, ExpressionNode} from "../nodes";
import {Identifier} from "./_identifier";
import * as Tokens from "../1_lexer/tokens";
import {Visibility} from "./visibility";
import {ParsedFileInformation} from "./parsed_file_information";
import {InfoClassImplementation, InfoClassDefinition, InfoMethodDefinition, InfoInterfaceDefinition, InfoAttribute, InfoAlias, AttributeLevel, InfoMethodParameter, MethodParameterDirection, InfoFormDefinition, InfoImplementing, InfoConstant, Duration, RiskLevel} from "./_abap_file_information";
export class ABAPFileInformationParser {
private readonly filename: string;
public constructor(filename: string) {
this.filename = filename;
}
public parse(structure: StructureNode | undefined): ParsedFileInformation {
const result: ParsedFileInformation = {
interfaces: [],
classes: [],
forms: [],
implementations: [],
};
if (structure === undefined) {
return result;
}
result.classes = this.parseClasses(structure);
result.interfaces = this.parseInterfaces(structure);
result.implementations = this.parseClassImplementations(structure);
result.forms = this.parseForms(structure);
return result;
}
private parseClassImplementations(structure: StructureNode): InfoClassImplementation[] {
const implementations: InfoClassImplementation[] = [];
for (const found of structure.findAllStructures(Structures.ClassImplementation)) {
const methods = [];
for (const method of found.findAllStructures(Structures.Method)) {
const methodName = method.findFirstExpression(Expressions.MethodName)?.getFirstToken();
if (methodName) {
methods.push(new Identifier(methodName, this.filename));
}
}
const name = found.findFirstStatement(Statements.ClassImplementation)!.findFirstExpression(Expressions.ClassName)!.getFirstToken();
implementations.push({
name: name.getStr(),
identifier: new Identifier(name, this.filename),
methods,
});
}
return implementations;
}
private parseForms(structure: StructureNode): InfoFormDefinition[] {
const forms: InfoFormDefinition[] = [];
for (const statement of structure.findAllStructures(Structures.Form)) {
// FORMs can contain a dash in the name
const pos = statement.findFirstExpression(Expressions.FormName)!.getFirstToken().getStart();
const name = statement.findFirstExpression(Expressions.FormName)!.concatTokens();
const nameToken = new Tokens.Identifier(pos, name);
forms.push({
name: nameToken.getStr(),
identifier: new Identifier(nameToken, this.filename),
});
}
return forms;
}
private parseInterfaces(structure: StructureNode): InfoInterfaceDefinition[] {
const interfaces: InfoInterfaceDefinition[] = [];
for (const found of structure.findDirectStructures(Structures.Interface)) {
const i = found.findFirstStatement(Statements.Interface);
if (i === undefined) {
throw new Error("Interface expected, parseInterfaces");
}
const interfaceName = i.findDirectExpression(Expressions.InterfaceName)!.getFirstToken();
const methods = this.parseMethodDefinition(found, Visibility.Public);
const attributes = this.parseAttributes(found, Visibility.Public);
const aliases = this.parseAliases(found, Visibility.Public);
const constants = this.parseConstants(found, Visibility.Public);
const g = i.findDirectExpression(Expressions.ClassGlobal);
interfaces.push({
name: interfaceName.getStr(),
identifier: new Identifier(interfaceName, this.filename),
isLocal: g === undefined,
isGlobal: g !== undefined,
interfaces: this.getImplementing(found),
aliases,
methods,
constants,
attributes,
});
}
return interfaces;
}
private parseClasses(structure: StructureNode): InfoClassDefinition[] {
const classes: InfoClassDefinition[] = [];
for (const found of structure.findAllStructures(Structures.ClassDefinition)) {
const className = found.findFirstStatement(Statements.ClassDefinition)!.findFirstExpression(Expressions.ClassName)!.getFirstToken();
const methods = this.parseMethodDefinition(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
methods.push(...this.parseMethodDefinition(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
methods.push(...this.parseMethodDefinition(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
const attributes = this.parseAttributes(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
attributes.push(...this.parseAttributes(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
attributes.push(...this.parseAttributes(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
const aliases = this.parseAliases(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
aliases.push(...this.parseAliases(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
aliases.push(...this.parseAliases(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
const constants = this.parseConstants(found.findFirstStructure(Structures.PublicSection), Visibility.Public);
constants.push(...this.parseConstants(found.findFirstStructure(Structures.ProtectedSection), Visibility.Protected));
constants.push(...this.parseConstants(found.findFirstStructure(Structures.PrivateSection), Visibility.Private));
const superClassName = found.findFirstExpression(Expressions.SuperClassName)?.getFirstToken().getStr();
const containsGlobal = found.findFirstExpression(Expressions.ClassGlobal);
const cdef = found.findFirstStatement(Statements.ClassDefinition);
const concat = cdef?.concatTokens().toUpperCase() || "";
let riskLevel: RiskLevel | undefined;
if (concat.includes("RISK LEVEL CRITICAL")) {
riskLevel = RiskLevel.critical;
} else if (concat.includes("RISK LEVEL DANGEROUS")) {
riskLevel = RiskLevel.dangerous;
} else if (concat.includes("RISK LEVEL HARMLESS")) {
riskLevel = RiskLevel.harmless;
}
let duration: Duration | undefined;
if (concat.includes("DURATION SHORT")) {
duration = Duration.short;
} else if (concat.includes("DURATION LONG")) {
duration = Duration.long;
} else if (concat.includes("DURATION MEDIUM")) {
duration = Duration.medium;
}
classes.push({
name: className.getStr(),
identifier: new Identifier(className, this.filename),
isLocal: containsGlobal === undefined,
isGlobal: containsGlobal !== undefined,
methods,
superClassName,
interfaces: this.getImplementing(found),
isForTesting: concat.includes(" FOR TESTING"),
duration,
riskLevel,
isAbstract: cdef?.findDirectTokenByText("ABSTRACT") !== undefined,
isSharedMemory: concat.includes(" SHARED MEMORY ENABLED"),
behaviorDefinitionName: this.findBehaviorDefinitionName(cdef),
isFinal: found.findFirstExpression(Expressions.ClassFinal) !== undefined,
aliases,
attributes,
constants,
});
}
return classes;
}
///////////////////
/** "FOR BEHAVIOR OF" and "FOR EVENTS OF" both use the BehaviorDefinitionName expression,
* so look at the token two positions before the expression to tell them apart */
private findBehaviorDefinitionName(cdef: StatementNode | undefined): string | undefined {
const children = cdef?.getChildren() || [];
for (let i = 2; i < children.length; i++) {
const child = children[i];
if (child instanceof ExpressionNode
&& child.get() instanceof Expressions.BehaviorDefinitionName
&& children[i - 2].concatTokens().toUpperCase() === "BEHAVIOR") {
return child.concatTokens();
}
}
return undefined;
}
private getImplementing(input: StructureNode): InfoImplementing[] {
const ret: InfoImplementing[] = [];
for (const node of input.findAllStatements(Statements.InterfaceDef)) {
const abstract = node.findDirectExpression(Expressions.AbstractMethods);
const abstractMethods: string[] = [];
if (abstract) {
for (const m of abstract.findDirectExpressions(Expressions.MethodName)) {
abstractMethods.push(m.concatTokens().toUpperCase());
}
}
const final = node.findDirectExpression(Expressions.FinalMethods);
const finalMethods: string[] = [];
if (final) {
for (const m of final.findDirectExpressions(Expressions.MethodName)) {
finalMethods.push(m.concatTokens().toUpperCase());
}
}
const concat = node.concatTokens().toUpperCase();
const allAbstract = concat.includes(" ALL METHODS ABSTRACT");
const partial = concat.includes(" PARTIALLY IMPLEMENTED");
const name = node.findFirstExpression(Expressions.InterfaceName)!.getFirstToken().getStr().toUpperCase();
ret.push({
name,
partial,
allAbstract,
abstractMethods,
finalMethods,
});
}
return ret;
}
private parseAliases(node: StructureNode | undefined, visibility: Visibility): InfoAlias[] {
if (node === undefined) {
return [];
}
const ret: InfoAlias[] = [];
for (const a of node.findAllStatements(Statements.Aliases)) {
const name = a.findFirstExpression(Expressions.SimpleName)!.getFirstToken();
const comp = a.findFirstExpression(Expressions.Field)!.getFirstToken();
ret.push({
name: name.getStr(),
identifier: new Identifier(name, this.filename),
visibility,
component: comp.getStr(),
});
}
return ret;
}
private parseConstants(node: StructureNode | undefined, visibility: Visibility): InfoConstant[] {
if (node === undefined) {
return [];
}
const results: InfoConstant[] = [];
for (const constant of node.findAllStatements(Statements.Constant)) {
const name = constant.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
const typeName = constant.findFirstExpression(Expressions.TypeName);
// VALUE `const_value` -> `const_value`
const literal = constant.findFirstExpression(Expressions.Value)?.getTokens()[1].getStr() ?? "``";
// `const_value` -> const_value
const value = literal.slice(1, literal?.length - 1);
results.push({
name: name!.getStr(),
typeName: typeName ? typeName.getFirstToken().getStr() : "",
value: value,
identifier: new Identifier(name, this.filename),
visibility,
});
}
return results;
}
private parseAttributes(node: StructureNode | undefined, visibility: Visibility): InfoAttribute[] {
if (node === undefined) {
return [];
}
const contents = node.findFirstStructure(Structures.SectionContents);
if (contents === undefined) {
return [];
}
const ret: InfoAttribute[] = [];
const data = contents.findDirectStatements(Statements.Data);
for (const structure of contents.findDirectStructures(Structures.Data)) {
const begin = structure.findDirectStatement(Statements.DataBegin)!;
if (begin.findDirectExpression(Expressions.DefinitionName)) {
data.push(begin);
}
}
for (const d of data) {
const name = d.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
ret.push({
name: name.getStr(),
identifier: new Identifier(name, this.filename),
level: AttributeLevel.Instance,
readOnly: this.isReadOnly(d),
visibility,
});
}
const classData = contents.findDirectStatements(Statements.ClassData);
for (const structure of contents.findDirectStructures(Structures.ClassData)) {
classData.push(structure.findDirectStatement(Statements.ClassDataBegin)!);
}
for (const d of classData) {
const name = (d.findFirstExpression(Expressions.DefinitionName)
?? d.findDirectExpression(Expressions.NamespaceSimpleName))!.getFirstToken();
ret.push({
name: name.getStr(),
identifier: new Identifier(name, this.filename),
level: AttributeLevel.Static,
readOnly: this.isReadOnly(d),
visibility,
});
}
for (const d of contents.findDirectStatements(Statements.Constant)) {
const name = d.findFirstExpression(Expressions.DefinitionName)!.getFirstToken();
ret.push({
name: name.getStr(),
identifier: new Identifier(name, this.filename),
level: AttributeLevel.Constant,
readOnly: true,
visibility,
});
}
return ret;
}
private isReadOnly(statement: StatementNode): boolean {
const definition = statement.findDirectExpression(Expressions.DataDefinition);
const nodes = definition ? [definition, ...definition.findAllExpressions(Expressions.TypeTableKey)] : [statement];
// In these grammar nodes, a direct READ token only occurs in the READ-ONLY modifier.
return nodes.some(node => node.findDirectTokenByText("READ") !== undefined);
}
private parseMethodDefinition(node: StructureNode | undefined, visibility: Visibility): InfoMethodDefinition[] {
if (node === undefined) {
return [];
}
const methods: InfoMethodDefinition[] = [];
for (const def of node.findAllStatements(Statements.MethodDef)) {
const methodName = def.findDirectExpression(Expressions.MethodName)?.getFirstToken();
if (methodName === undefined) {
continue;
}
const parameters = this.parseMethodParameters(def);
const concatenated = def.concatTokens().toUpperCase();
methods.push({
name: methodName.getStr(),
identifier: new Identifier(methodName, this.filename),
isRedefinition: def.findDirectExpression(Expressions.Redefinition) !== undefined,
isForTesting: concatenated.includes(" FOR TESTING"),
isFinal: concatenated.includes(" FINAL"),
isDefaultIgnore: concatenated.includes(" DEFAULT IGNORE"),
isDefaultFail: concatenated.includes(" DEFAULT FAIL"),
isAbstract: def.findDirectExpression(Expressions.Abstract) !== undefined,
isEventHandler: def.findDirectExpression(Expressions.EventHandler) !== undefined,
visibility,
parameters,
exceptions: [], // todo
});
}
return methods;
}
// todo, refactor this method, it is too long
private parseMethodParameters(node: StatementNode): InfoMethodParameter[] {
const ret: InfoMethodParameter[] = [];
const importing = node.findFirstExpression(Expressions.MethodDefImporting);
if (importing) {
for (const param of importing.findAllExpressions(Expressions.MethodParam)) {
const name = param.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
if (name) {
ret.push({
name: name.getStr().replace("!", ""),
identifier: new Identifier(name, this.filename),
direction: MethodParameterDirection.Importing,
});
}
}
}
const exporting = node.findFirstExpression(Expressions.MethodDefExporting);
if (exporting) {
for (const param of exporting.findAllExpressions(Expressions.MethodParam)) {
const name = param.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
if (name) {
ret.push({
name: name.getStr().replace("!", ""),
identifier: new Identifier(name, this.filename),
direction: MethodParameterDirection.Exporting,
});
}
}
}
const changing = node.findFirstExpression(Expressions.MethodDefChanging);
if (changing) {
for (const param of changing.findAllExpressions(Expressions.MethodParam)) {
const name = param.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
if (name) {
ret.push({
name: name.getStr().replace("!", ""),
identifier: new Identifier(name, this.filename),
direction: MethodParameterDirection.Changing,
});
}
}
}
const returning = node.findFirstExpression(Expressions.MethodDefReturning);
if (returning) {
const name = returning.findDirectExpression(Expressions.MethodParamName)?.getFirstToken();
if (name) {
ret.push({
name: name.getStr().replace("!", ""),
identifier: new Identifier(name, this.filename),
direction: MethodParameterDirection.Returning,
});
}
}
return ret;
}
}
|