Add rendering of compiler panics in code snippets

This commit is contained in:
Araozu 2024-07-28 19:54:15 -05:00
parent 27bfca8880
commit bb6478b474
3 changed files with 14 additions and 5 deletions

View File

@ -12,6 +12,6 @@ const [native_html, error_type, error_message] = await native_highlighter(thpcod
</span></pre> </span></pre>
{ {
error_message !== null && ( error_message !== null && (
<CodeError error_type={error_type}>{error_message}</CodeError> <CodeError error_type={error_type} error_message={error_message}></CodeError>
) )
} }

View File

@ -1,8 +1,8 @@
--- ---
const { error_type = "Unknown" } = Astro.props; const { error_type = "Unknown", error_message } = Astro.props;
--- ---
<div class="px-4 py-2 rounded bg-red-200 dark:bg-red-950"> <div class="px-4 py-2 rounded bg-red-200 dark:bg-red-950">
<span class="inline-block font-bold">{error_type} error:</span> <span class="inline-block font-bold">{error_type} error:</span>
<slot /> <span class="inline-block whitespace-pre-wrap">{error_message}</span>
</div> </div>

View File

@ -56,7 +56,12 @@ export interface TokenizeResult {
export async function native_highlighter(code: string): Promise<[string, string, string | null]> { export async function native_highlighter(code: string): Promise<[string, string, string | null]> {
let formatted_code = leftTrimDedent(code).join("\n"); let formatted_code = leftTrimDedent(code).join("\n");
const result = await native_lex(formatted_code); let result: TokenizeResult;
try {
result = await native_lex(formatted_code);
} catch (error) {
return compiler_error(formatted_code, error as Error);
}
if (result.Err) { if (result.Err) {
return lex_error_highlighter(formatted_code, result.Err!.Lex!); return lex_error_highlighter(formatted_code, result.Err!.Lex!);
@ -101,6 +106,10 @@ function syntax_error_highlighter(code: string, tokens: Array<Token>, error: Syn
return [highlighted, "Syntax", error_message]; return [highlighted, "Syntax", error_message];
} }
function compiler_error(code: string, error: Error): [string, string, string] {
return [code, "Fatal Compiler", error.message];
}
function highlight_tokens(input: string, tokens: Array<Token>): string { function highlight_tokens(input: string, tokens: Array<Token>): string {
const input_chars = input.split(""); const input_chars = input.split("");
let output = ""; let output = "";
@ -192,7 +201,7 @@ const native_lex = (code: string) => new Promise<TokenizeResult>((resolve, rejec
if (code === 0) { if (code === 0) {
resolve(JSON.parse(response)); resolve(JSON.parse(response));
} else { } else {
reject(error); reject(new Error(error));
} }
}); });
}) })