compiler: rework diag messages a bit

This commit is contained in:
SimonFJ20 2025-02-10 17:52:22 +01:00
parent 010a3e7d19
commit c5e42844af
3 changed files with 45 additions and 34 deletions

View File

@ -18,7 +18,6 @@ export const Span = {
export type Report = { export type Report = {
severity: "fatal" | "error" | "warning" | "info"; severity: "fatal" | "error" | "warning" | "info";
origin?: string;
msg: string; msg: string;
file?: File; file?: File;
span?: Span; span?: Span;
@ -29,16 +28,17 @@ export type ReportLocation =
| { file: File; span: Span } | { file: File; span: Span }
| { file: File; pos: Pos }; | { file: File; pos: Pos };
function severityColor(severity: "fatal" | "error" | "warning" | "info") { function severityColor(
severity: "fatal" | "error" | "warning" | "info",
): string {
switch (severity) { switch (severity) {
case "fatal": case "fatal":
return "\x1b[1m\x1b[31m";
case "error": case "error":
return "\x1b[1m\x1b[31m"; return "red";
case "warning": case "warning":
return "\x1b[1m\x1b[33m"; return "yellow";
case "info": case "info":
return "\x1b[1m\x1b[34m"; return "blue";
} }
exhausted(severity); exhausted(severity);
} }
@ -52,63 +52,76 @@ export function prettyPrintReport(ctx: Ctx, rep: Report) {
}); });
} }
const { severity, msg } = rep; const { severity, msg } = rep;
const origin = rep.origin ? `\x1b[1m${rep.origin}:\x1b[0m ` : ""; const sevColor = severityColor(severity);
console.error( console.error(
`${origin}${severityColor(severity)}${severity}:\x1b[0m %c${msg}`, `%c${severity}%c: %c${msg}`,
"color: white; font-weight: bold;", `color: ${sevColor}; font-weight: bold`,
"",
"font-weight: bold;",
); );
if (!rep.file) { if (!rep.file) {
return; return;
} }
const errorLineOffset = 2;
const { absPath: path } = ctx.fileInfo(rep.file); const { absPath: path } = ctx.fileInfo(rep.file);
const { line, col } = rep.span?.begin ?? rep.pos!; const { line, col } = rep.span?.begin ?? rep.pos!;
console.error(` --> ./${path}:${line}:${col}`); console.error(
` %c--> %c./${path}:${line}:${col}`,
"color: cyan",
"",
);
if (rep.span) { if (rep.span) {
const spanLines = ctx.fileSpanText(rep.file, rep.span).split("\n"); const spanLines = ctx.fileSpanText(rep.file, rep.span).split("\n");
spanLines.pop(); spanLines.pop();
if (spanLines.length == 1) { if (spanLines.length == 1) {
const sqgPad = " ".repeat(rep.span.begin.col - 1);
console.error( console.error(
`${rep.span.begin.line.toString().padStart(4, " ")}| ${ ` %c|`,
spanLines[0] "color: cyan",
}`,
); );
console.error( console.error(
` | ${severityColor(severity)}${ `${rep.span.begin.line.toString().padStart(4, " ")}%c| %c${
" ".repeat(rep.span.begin.col - 1) spanLines[0]
}${ }`,
"color: cyan",
"",
);
console.error(
` %c| %c${sqgPad}${
"~".repeat(rep.span.end.col - rep.span.begin.col + 1) "~".repeat(rep.span.end.col - rep.span.begin.col + 1)
}\x1b[0m`, }`,
"color: cyan",
`color: ${sevColor}; font-weight: bold`,
); );
return; return;
} }
for (let i = 0; i < spanLines.length; i++) { for (let i = 0; i < spanLines.length; i++) {
console.error( console.error(
`${(rep.span.begin.line + i).toString().padStart(4, " ")}| ${ `${(rep.span.begin.line + i).toString().padStart(4, " ")}%c| ${
spanLines[i] spanLines[i]
}`, }`,
"color: cyan",
); );
if (i == 0) { if (i == 0) {
console.error( console.error(
` | ${" ".repeat(rep.span.begin.col - 1)}${ ` %c| ${" ".repeat(rep.span.begin.col - 1)}%c${
severityColor(severity)
}${
"~".repeat( "~".repeat(
spanLines[i].length - (rep.span.begin.col - 1), spanLines[i].length - (rep.span.begin.col - 1),
) )
}\x1b[0m`, }`,
"color: cyan",
`color: ${sevColor}; font-weight: bold`,
); );
} else if (i == spanLines.length - 1) { } else if (i == spanLines.length - 1) {
console.error( console.error(
` | ${severityColor(severity)}${ ` %c| %c${"~".repeat(rep.span.end.col)}\x1b[0m`,
"~".repeat(rep.span.end.col) "color: cyan",
}\x1b[0m`, `color: ${sevColor}; font-weight: bold`,
); );
} else { } else {
console.error( console.error(
` | ${severityColor(severity)}${ ` %c| %c${"~".repeat(spanLines[i].length)}\x1b[0m`,
"~".repeat(spanLines[i].length) "color: cyan",
}\x1b[0m`, `color: ${sevColor}; font-weight: bold`,
); );
} }
} }
@ -119,9 +132,9 @@ export function prettyPrintReport(ctx: Ctx, rep: Report) {
}`, }`,
); );
console.error( console.error(
` | ${severityColor(severity)}${ ` %c| %c${" ".repeat(rep.pos.col - 1)}^\x1b[0m`,
" ".repeat(rep.pos.col - 1) "color: cyan",
}^\x1b[0m`, `color: ${sevColor}; font-weight: bold`,
); );
} }
} }

View File

@ -260,7 +260,6 @@ export class Lexer implements TokenIter {
private report(msg: string, pos: Pos) { private report(msg: string, pos: Pos) {
this.ctx.report({ this.ctx.report({
severity: "error", severity: "error",
origin: "parser",
file: this.file, file: this.file,
msg, msg,
pos, pos,

View File

@ -1233,7 +1233,6 @@ export class Parser {
private report(msg: string, span = this.span()) { private report(msg: string, span = this.span()) {
this.ctx.report({ this.ctx.report({
severity: "error", severity: "error",
origin: "parser",
msg, msg,
file: this.file, file: this.file,
span, span,