From c5e42844af4a51fe5c47ec5ae8e2063d9e6213bc Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Mon, 10 Feb 2025 17:52:22 +0100 Subject: [PATCH] compiler: rework diag messages a bit --- slige/compiler/common/diagnostics.ts | 77 ++++++++++++++++------------ slige/compiler/parse/lexer.ts | 1 - slige/compiler/parse/parser.ts | 1 - 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/slige/compiler/common/diagnostics.ts b/slige/compiler/common/diagnostics.ts index 8f0fc18..bacf7b9 100644 --- a/slige/compiler/common/diagnostics.ts +++ b/slige/compiler/common/diagnostics.ts @@ -18,7 +18,6 @@ export const Span = { export type Report = { severity: "fatal" | "error" | "warning" | "info"; - origin?: string; msg: string; file?: File; span?: Span; @@ -29,16 +28,17 @@ export type ReportLocation = | { file: File; span: Span } | { file: File; pos: Pos }; -function severityColor(severity: "fatal" | "error" | "warning" | "info") { +function severityColor( + severity: "fatal" | "error" | "warning" | "info", +): string { switch (severity) { case "fatal": - return "\x1b[1m\x1b[31m"; case "error": - return "\x1b[1m\x1b[31m"; + return "red"; case "warning": - return "\x1b[1m\x1b[33m"; + return "yellow"; case "info": - return "\x1b[1m\x1b[34m"; + return "blue"; } exhausted(severity); } @@ -52,63 +52,76 @@ export function prettyPrintReport(ctx: Ctx, rep: Report) { }); } const { severity, msg } = rep; - const origin = rep.origin ? `\x1b[1m${rep.origin}:\x1b[0m ` : ""; + const sevColor = severityColor(severity); console.error( - `${origin}${severityColor(severity)}${severity}:\x1b[0m %c${msg}`, - "color: white; font-weight: bold;", + `%c${severity}%c: %c${msg}`, + `color: ${sevColor}; font-weight: bold`, + "", + "font-weight: bold;", ); if (!rep.file) { return; } - const errorLineOffset = 2; const { absPath: path } = ctx.fileInfo(rep.file); 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) { const spanLines = ctx.fileSpanText(rep.file, rep.span).split("\n"); spanLines.pop(); if (spanLines.length == 1) { + const sqgPad = " ".repeat(rep.span.begin.col - 1); console.error( - `${rep.span.begin.line.toString().padStart(4, " ")}| ${ - spanLines[0] - }`, + ` %c|`, + "color: cyan", ); console.error( - ` | ${severityColor(severity)}${ - " ".repeat(rep.span.begin.col - 1) - }${ + `${rep.span.begin.line.toString().padStart(4, " ")}%c| %c${ + spanLines[0] + }`, + "color: cyan", + "", + ); + console.error( + ` %c| %c${sqgPad}${ "~".repeat(rep.span.end.col - rep.span.begin.col + 1) - }\x1b[0m`, + }`, + "color: cyan", + `color: ${sevColor}; font-weight: bold`, ); return; } for (let i = 0; i < spanLines.length; i++) { console.error( - `${(rep.span.begin.line + i).toString().padStart(4, " ")}| ${ + `${(rep.span.begin.line + i).toString().padStart(4, " ")}%c| ${ spanLines[i] }`, + "color: cyan", ); if (i == 0) { console.error( - ` | ${" ".repeat(rep.span.begin.col - 1)}${ - severityColor(severity) - }${ + ` %c| ${" ".repeat(rep.span.begin.col - 1)}%c${ "~".repeat( spanLines[i].length - (rep.span.begin.col - 1), ) - }\x1b[0m`, + }`, + "color: cyan", + `color: ${sevColor}; font-weight: bold`, ); } else if (i == spanLines.length - 1) { console.error( - ` | ${severityColor(severity)}${ - "~".repeat(rep.span.end.col) - }\x1b[0m`, + ` %c| %c${"~".repeat(rep.span.end.col)}\x1b[0m`, + "color: cyan", + `color: ${sevColor}; font-weight: bold`, ); } else { console.error( - ` | ${severityColor(severity)}${ - "~".repeat(spanLines[i].length) - }\x1b[0m`, + ` %c| %c${"~".repeat(spanLines[i].length)}\x1b[0m`, + "color: cyan", + `color: ${sevColor}; font-weight: bold`, ); } } @@ -119,9 +132,9 @@ export function prettyPrintReport(ctx: Ctx, rep: Report) { }`, ); console.error( - ` | ${severityColor(severity)}${ - " ".repeat(rep.pos.col - 1) - }^\x1b[0m`, + ` %c| %c${" ".repeat(rep.pos.col - 1)}^\x1b[0m`, + "color: cyan", + `color: ${sevColor}; font-weight: bold`, ); } } diff --git a/slige/compiler/parse/lexer.ts b/slige/compiler/parse/lexer.ts index d8bbe55..b27ae40 100644 --- a/slige/compiler/parse/lexer.ts +++ b/slige/compiler/parse/lexer.ts @@ -260,7 +260,6 @@ export class Lexer implements TokenIter { private report(msg: string, pos: Pos) { this.ctx.report({ severity: "error", - origin: "parser", file: this.file, msg, pos, diff --git a/slige/compiler/parse/parser.ts b/slige/compiler/parse/parser.ts index 2e855d8..5faa099 100644 --- a/slige/compiler/parse/parser.ts +++ b/slige/compiler/parse/parser.ts @@ -1233,7 +1233,6 @@ export class Parser { private report(msg: string, span = this.span()) { this.ctx.report({ severity: "error", - origin: "parser", msg, file: this.file, span,