slige/compiler/diagnostics.ts
2025-01-22 22:40:29 +01:00

44 lines
1.1 KiB
TypeScript

import { Ctx, File } from "./ctx.ts";
export type Span = {
begin: Pos;
end: Pos;
};
export type Pos = {
idx: number;
line: number;
col: number;
};
export type Diag = {
severity: "fatal" | "error" | "warning" | "info";
origin?: string;
msg: string;
file?: File;
span?: Span;
pos?: Pos;
};
export function prettyPrintDiag(ctx: Ctx, diag: Diag) {
const { severity, msg } = diag;
const origin = diag.origin ? `${diag.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!;
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) {
}
}
}