start implememting crate loading

This commit is contained in:
nora 2023-07-31 18:03:26 +02:00
parent a0599342ea
commit b3b0bc2e6f
12 changed files with 249 additions and 165 deletions

View file

@ -23,19 +23,19 @@ export class CompilerError extends Error {
}
}
export function withErrorHandler(input: string, f: () => void): void {
export function withErrorPrinter(input: string, filename: string, f: () => void): void {
try {
f();
} catch (e) {
if (e instanceof CompilerError) {
renderError(input, e);
renderError(input, filename, e);
} else {
throw e;
}
}
}
function renderError(input: string, e: CompilerError) {
function renderError(input: string, filename: string, e: CompilerError) {
const lineSpans = lines(input);
const line =
e.span.start === Number.MAX_SAFE_INTEGER
@ -49,6 +49,8 @@ function renderError(input: string, e: CompilerError) {
const lineIdx = lineSpans.indexOf(line);
const lineNo = lineIdx + 1;
console.error(`error: ${e.message}`);
console.error(` --> ${filename}:${lineNo}`);
console.error(`${lineNo} | ${spanToSnippet(input, line)}`);
const startRelLine =