Fix error reporting position and match updated log headers

This commit is contained in:
2026-04-26 21:16:50 +02:00
parent 9449f16e02
commit c219a303ec
4 changed files with 70 additions and 28 deletions
+39 -4
View File
@@ -5,12 +5,17 @@
#include <string.h>
struct TokenStream {
const char* filename;
char* filename;
const char* code;
size_t pos;
int line;
int column;
const char* line_start;
// End of last non-EOF token
int last_line;
int last_column_end;
const char* last_line_start;
};
/**
@@ -95,12 +100,19 @@ static Token create_token(TokenStream* ts, TokenType type, const char* text, siz
t.token = type;
t.text.data = (char*)text;
t.text.length = length;
t.location.filename = (char*)ts->filename;
t.location.filename = ts->filename;
t.location.line = line;
t.location.column_start = column;
t.location.column_end = column + (int)length - 1;
t.location.line_text.data = (char*)line_start;
t.location.line_text.length = get_line_length(line_start);
if (type != TOKEN_EOF) {
ts->last_line = t.location.line;
ts->last_column_end = t.location.column_end;
ts->last_line_start = t.location.line_text.data;
}
return t;
}
@@ -112,17 +124,21 @@ TokenStream* tokenstream_open(const char* filename, const char* code) {
return NULL;
}
ts->filename = filename ? filename : "unknown";
ts->filename = strdup(filename ? filename : "unknown");
ts->code = code;
ts->pos = 0;
ts->line = 1;
ts->column = 1;
ts->line_start = code;
ts->last_line = 1;
ts->last_column_end = 0;
ts->last_line_start = code;
return ts;
}
void tokenstream_close(TokenStream* ts) {
if (ts == NULL) return;
if (ts->filename) free(ts->filename);
free(ts);
}
@@ -160,7 +176,26 @@ Token tokenstream_next(TokenStream* ts) {
}
if (peek_char(ts) == '\0') {
return create_token(ts, TOKEN_EOF, NULL, 0, ts->line, ts->column, ts->line_start);
Token t;
t.token = TOKEN_EOF;
t.text.data = NULL;
t.text.length = 0;
t.location.filename = ts->filename;
if (ts->pos > 0 && ts->code[ts->pos - 1] == '\n') {
t.location.line = ts->line;
t.location.column_start = 1;
t.location.column_end = 1;
t.location.line_text.data = (char*)ts->line_start;
t.location.line_text.length = get_line_length(ts->line_start);
} else {
t.location.line = ts->last_line;
t.location.column_start = ts->last_column_end + 1;
t.location.column_end = ts->last_column_end + 1;
t.location.line_text.data = (char*)ts->last_line_start;
t.location.line_text.length = get_line_length(ts->last_line_start);
}
return t;
}
int start_line = ts->line;