vc3/asm/report.c
2025-04-02 22:38:03 +02:00

51 lines
1.4 KiB
C

#include "report.h"
#include <stdio.h>
void print_report_loc(
const char* filename, const char* text, size_t text_len, Loc loc)
{
const char* displacement_spaces
= " "
" "
" "
" ";
size_t line_start = loc.idx;
while (line_start > 0 && text[line_start] != '\n') {
line_start -= 1;
}
if (text[line_start] == '\n') {
line_start += 1;
}
size_t line_end = loc.idx + 1;
while (line_end < text_len && text[line_end] != '\n') {
line_end += 1;
}
const char* line = &text[line_start];
int line_len = (int)line_end - (int)line_start;
fprintf(stderr,
" \x1b[96m--> ./%s:%d:%d\n "
"\x1b[37m|\n\x1b[96m%5d\x1b[37m|\x1b[0m%.*s\n "
"\x1b[37m|%.*s\x1b[1;91m^\x1b[0m\n",
filename,
loc.line,
loc.col,
loc.line,
line_len,
line,
loc.col - 1,
displacement_spaces);
}
void reporter_print_loc(Reporter* rep, Loc loc)
{
print_report_loc(rep->filename, rep->text, rep->text_len, loc);
}
void reporter_error_with_loc(Reporter* rep, const char* msg, Loc loc)
{
REPORTF_ERROR("%s", msg);
reporter_print_loc(rep, loc);
}