rename diags to reports

This commit is contained in:
SimonFJ20 2025-01-23 10:18:33 +01:00
parent 93dd4c32c8
commit cd087392b9
3 changed files with 45 additions and 49 deletions

View File

@ -1,11 +1,11 @@
import * as ast from "./ast/mod.ts"; import * as ast from "./ast/mod.ts";
import { Diag } from "./diagnostics.ts"; import { prettyPrintReport, printStackTrace, Report } from "./diagnostics.ts";
export class Ctx { export class Ctx {
private fileIds = new Ids(); private fileIds = new Ids();
private files = new Map<Id<File>, FileInfo>(); private files = new Map<Id<File>, FileInfo>();
private diags: Diag[] = []; private reports: Report[] = [];
public fileHasChildWithIdent(file: File, childIdent: string): boolean { public fileHasChildWithIdent(file: File, childIdent: string): boolean {
return this.files.get(id(file))! return this.files.get(id(file))!
@ -40,43 +40,23 @@ export class Ctx {
} }
public fileInfo(file: File): FileInfo { public fileInfo(file: File): FileInfo {
this.files.get(id(file))!; return this.files.get(id(file))!;
} }
public reportFatal(file: File, msg: string) { public report(rep: Report) {
console.error(`fatal: ${msg}`); this.reports.push(rep);
this.reportImmediately(file, msg); this.reportImmediately(rep);
} }
public enableReportImmediately = false; public enableReportImmediately = false;
public enableStacktrace = false; public enableStacktrace = false;
private reportImmediately(file: File, msg: string) { private reportImmediately(rep: Report) {
if (!this.enableReportImmediately) { if (this.enableReportImmediately) {
return; prettyPrintReport(this, rep);
} if (this.enableStacktrace) {
printStackTrace();
if (!this.enableStacktrace) {
return;
}
class StackTracer extends Error {
constructor() {
super("StackTracer");
} }
} }
try {
//throw new ReportNotAnError();
} catch (error) {
if (!(error instanceof StackTracer)) {
throw error;
}
console.log(
error.stack?.replace(
"Error: StackTracer",
"Stack trace:",
) ??
error,
);
}
} }
} }

View File

@ -11,7 +11,7 @@ export type Pos = {
col: number; col: number;
}; };
export type Diag = { export type Report = {
severity: "fatal" | "error" | "warning" | "info"; severity: "fatal" | "error" | "warning" | "info";
origin?: string; origin?: string;
msg: string; msg: string;
@ -20,24 +20,35 @@ export type Diag = {
pos?: Pos; pos?: Pos;
}; };
export function prettyPrintDiag(ctx: Ctx, diag: Diag) { export function prettyPrintReport(ctx: Ctx, rep: Report) {
const { severity, msg } = diag; const { severity, msg } = rep;
const origin = diag.origin ? `${diag.origin}: ` : ""; const origin = rep.origin ? `${rep.origin}: ` : "";
console.error(`${origin}${severity}: ${msg}`); console.error(`${origin}${severity}: ${msg}`);
if (diag.file && (diag.span || diag.pos)) { if (rep.file && (rep.span || rep.pos)) {
const { absPath: path, text } = ctx.fileInfo(diag.file); const { absPath: path } = ctx.fileInfo(rep.file);
const { line, col } = diag.span?.begin ?? diag.pos!; const { line, col } = rep.span?.begin ?? rep.pos!;
console.error(` at ./${path}:${line}:${col}`); console.error(` at ./${path}:${line}:${col}`);
if (diag.span) { }
let begin = diag.span.begin.idx; }
while (begin >= 0 && text[begin - 1] != "\n") {
begin -= 1; export function printStackTrace() {
} class StackTracer extends Error {
let end = diag.span.end.idx; constructor() {
while (end < text.length && text[end + 1] != "\n") { super("StackTracer");
end += 1; }
} }
} else if (diag.pos) { try {
} throw new StackTracer();
} catch (error) {
if (!(error instanceof StackTracer)) {
throw error;
}
console.log(
error.stack?.replace(
"Error: StackTracer",
"Stack trace:",
) ??
error,
);
} }
} }

View File

@ -61,6 +61,11 @@ export class FileTreeCollector implements ast.Visitor<[_P]> {
this.subFilePromise = this.subFilePromise this.subFilePromise = this.subFilePromise
.then(() => { .then(() => {
if (this.ctx.fileHasChildWithIdent(file, ident)) { if (this.ctx.fileHasChildWithIdent(file, ident)) {
this.ctx.report({
severity: "fatal",
msg: `module '${ident}' already declared`,
file,
});
} }
return new FileTreeCollector( return new FileTreeCollector(
this.ctx, this.ctx,