slige/compiler/info.ts

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-12-11 02:11:00 +00:00
import { Pos } from "./token.ts";
export type Report = {
2025-01-03 00:15:05 +00:00
type: "error" | "warning" | "note";
2024-12-11 02:11:00 +00:00
reporter: string;
pos?: Pos;
msg: string;
};
export class Reporter {
private reports: Report[] = [];
private errorSet = false;
2024-12-29 04:39:22 +00:00
public constructor(private filePath: string) {}
public setFilePath(filePath: string) {
this.filePath = filePath;
}
2024-12-11 02:11:00 +00:00
public reportError(report: Omit<Report, "type">) {
this.reports.push({ ...report, type: "error" });
this.printReport({ ...report, type: "error" });
this.errorSet = true;
}
2025-01-03 00:15:05 +00:00
public reportWarning(report: Omit<Report, "type">) {
this.reports.push({ ...report, type: "warning" });
this.printReport({ ...report, type: "warning" });
}
2024-12-11 02:11:00 +00:00
private printReport({ reporter, type, pos, msg }: Report) {
console.error(
2024-12-13 05:09:10 +00:00
`${reporter} ${type}: ${msg}${
2024-12-29 04:39:22 +00:00
pos ? `\n at ${this.filePath}:${pos.line}:${pos.col}` : ""
2024-12-11 02:11:00 +00:00
}`,
);
}
public addNote(report: Omit<Report, "type">) {
this.reports.push({ ...report, type: "note" });
this.printReport({ ...report, type: "note" });
}
public errorOccured(): boolean {
return this.errorSet;
}
}
export function printStackTrace() {
class ReportNotAnError extends Error {
constructor() {
super("ReportNotAnError");
}
}
try {
//throw new ReportNotAnError();
2024-12-11 02:11:00 +00:00
} catch (error) {
if (!(error instanceof ReportNotAnError)) {
throw error;
}
console.log(
error.stack?.replace("Error: ReportNotAnError", "Stack trace:") ??
error,
);
}
}