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 { Diag } from "./diagnostics.ts";
import { prettyPrintReport, printStackTrace, Report } from "./diagnostics.ts";
export class Ctx {
private fileIds = new Ids();
private files = new Map<Id<File>, FileInfo>();
private diags: Diag[] = [];
private reports: Report[] = [];
public fileHasChildWithIdent(file: File, childIdent: string): boolean {
return this.files.get(id(file))!
@ -40,43 +40,23 @@ export class Ctx {
}
public fileInfo(file: File): FileInfo {
this.files.get(id(file))!;
return this.files.get(id(file))!;
}
public reportFatal(file: File, msg: string) {
console.error(`fatal: ${msg}`);
this.reportImmediately(file, msg);
public report(rep: Report) {
this.reports.push(rep);
this.reportImmediately(rep);
}
public enableReportImmediately = false;
public enableStacktrace = false;
private reportImmediately(file: File, msg: string) {
if (!this.enableReportImmediately) {
return;
}
if (!this.enableStacktrace) {
return;
}
class StackTracer extends Error {
constructor() {
super("StackTracer");
private reportImmediately(rep: Report) {
if (this.enableReportImmediately) {
prettyPrintReport(this, rep);
if (this.enableStacktrace) {
printStackTrace();
}
}
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;
};
export type Diag = {
export type Report = {
severity: "fatal" | "error" | "warning" | "info";
origin?: string;
msg: string;
@ -20,24 +20,35 @@ export type Diag = {
pos?: Pos;
};
export function prettyPrintDiag(ctx: Ctx, diag: Diag) {
const { severity, msg } = diag;
const origin = diag.origin ? `${diag.origin}: ` : "";
export function prettyPrintReport(ctx: Ctx, rep: Report) {
const { severity, msg } = rep;
const origin = rep.origin ? `${rep.origin}: ` : "";
console.error(`${origin}${severity}: ${msg}`);
if (diag.file && (diag.span || diag.pos)) {
const { absPath: path, text } = ctx.fileInfo(diag.file);
const { line, col } = diag.span?.begin ?? diag.pos!;
if (rep.file && (rep.span || rep.pos)) {
const { absPath: path } = ctx.fileInfo(rep.file);
const { line, col } = rep.span?.begin ?? rep.pos!;
console.error(` at ./${path}:${line}:${col}`);
if (diag.span) {
let begin = diag.span.begin.idx;
while (begin >= 0 && text[begin - 1] != "\n") {
begin -= 1;
}
let end = diag.span.end.idx;
while (end < text.length && text[end + 1] != "\n") {
end += 1;
}
} else if (diag.pos) {
}
}
}
export function printStackTrace() {
class StackTracer extends Error {
constructor() {
super("StackTracer");
}
}
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
.then(() => {
if (this.ctx.fileHasChildWithIdent(file, ident)) {
this.ctx.report({
severity: "fatal",
msg: `module '${ident}' already declared`,
file,
});
}
return new FileTreeCollector(
this.ctx,