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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 164x 164x 165x 165x 165x 165x 174x 174x 174x 174x 155x 155x 19x 174x 6x 6x 6x 6x 174x 165x 167x 167x 159x 158x 1x 1x 19x 19x 19x 35x 35x 9x 35x 6x 6x 20x 20x 20x 4x 4x 1x | import * as Expressions from "../../2_statements/expressions";
import * as Statements from "../../2_statements/statements";
import * as Structures from "../../3_structures/structures";
import {StructureNode} from "../../nodes";
import {SyntaxInput, syntaxIssue} from "../_syntax_input";
// "special exceptions must be caught before general exceptions", a CATCH of a class that inherits
// from a class named in an earlier CATCH of the same TRY can never be reached, this is a syntax error
// only reported when the lineage of the later class is known up to the earlier class, void classes are skipped
export class Try {
public static runSyntax(node: StructureNode, input: SyntaxInput): void {
const caught = new Set<string>();
for (const catchStructure of node.findDirectStructures(Structures.Catch)) {
const statement = catchStructure.findDirectStatement(Statements.Catch);
if (statement === undefined) {
continue;
}
const names: string[] = [];
for (const c of statement.findDirectExpressions(Expressions.ClassName)) {
const token = c.getFirstToken();
const className = token.getStr().toUpperCase();
names.push(className);
if (caught.size === 0) {
continue;
}
const superclass = this.findCaughtSuperclass(className, caught, input);
if (superclass !== undefined) {
const message = `CATCH of ${className} after the CATCH of its superclass ${superclass}`;
input.issues.push(syntaxIssue(input, token, message));
return;
}
}
for (const name of names) {
caught.add(name);
}
}
}
private static findCaughtSuperclass(className: string, caught: Set<string>, input: SyntaxInput): string | undefined {
const visited = new Set<string>([className]);
let current = input.scope.findClassDefinition(className);
while (current !== undefined) {
const superclass = current.getSuperClass()?.toUpperCase();
if (superclass === undefined || visited.has(superclass)) {
return undefined;
} else if (caught.has(superclass)) {
return superclass;
}
visited.add(superclass);
current = input.scope.findClassDefinition(superclass);
}
return undefined;
}
}
|