From 51c4defc436c0d119941eb6d5b953d27b5b8e6f7 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 7 Aug 2021 23:39:21 +0200 Subject: Better error logging --- src/tokenize.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/tokenize.c') diff --git a/src/tokenize.c b/src/tokenize.c index 02059a8..2a0eea6 100644 --- a/src/tokenize.c +++ b/src/tokenize.c @@ -13,7 +13,7 @@ static char next_non_alnum(struct ctx *ctx, size_t start) if (!isalnum(ctx->data[i])) return ctx->data[i]; - errln(ctx, "Unexpected end of buffer"); + errln(&ctx->location, "Unexpected end of buffer"); } static bool peek_to_is_alnum(struct ctx *ctx, size_t start, char ch) @@ -28,7 +28,7 @@ static bool peek_to_is_alnum(struct ctx *ctx, size_t start, char ch) return false; } - errln(ctx, "Unexpected end of buffer"); + errln(&ctx->location, "Unexpected end of buffer"); } static size_t peek_alnum_to(struct ctx *ctx, size_t start, char ch) @@ -40,10 +40,10 @@ static size_t peek_alnum_to(struct ctx *ctx, size_t start, char ch) return i; if (!isalnum(cur)) - errln(ctx, "'%c' is not alpha-numeric", cur); + errln(&ctx->location, "'%c' is not alpha-numeric", cur); } - errln(ctx, "Unexpected end of buffer"); + errln(&ctx->location, "Unexpected end of buffer"); } static size_t peek_identifier(struct ctx *ctx, size_t start, char ch) @@ -55,10 +55,10 @@ static size_t peek_identifier(struct ctx *ctx, size_t start, char ch) return i; if (!isalnum(cur) && (cur < '!' || cur > '~')) - errln(ctx, "'%c' is not an identifier", cur); + errln(&ctx->location, "'%c' is not an identifier", cur); } - errln(ctx, "Unexpected end of buffer"); + errln(&ctx->location, "Unexpected end of buffer"); } static size_t peek_to(struct ctx *ctx, size_t start, char ch) @@ -70,7 +70,7 @@ static size_t peek_to(struct ctx *ctx, size_t start, char ch) return i; } - errln(ctx, "Unexpected end of buffer"); + errln(&ctx->location, "Unexpected end of buffer"); } static void token_add(struct ctx *ctx, enum token_type type, size_t start, size_t end) @@ -79,15 +79,16 @@ static void token_add(struct ctx *ctx, enum token_type type, size_t start, size_ token.type = type; token.string.start = start; token.string.end = end; + token.location = ctx->location; assert(++ctx->token_count < TOKENS_MAX); ctx->tokens[ctx->token_count] = token; if (type == NEWLINE) { - ctx->line++; - ctx->column = 0; + ctx->location.line++; + ctx->location.column = 0; } else { - ctx->column += end - start; + ctx->location.column += end - start; } } @@ -95,15 +96,14 @@ void token_print(struct ctx *ctx, struct token *token) { assert(token->type != UNKNOWN); - printf("[token type=%d] '", token->type); + printf("[token type=%d] ", token->type); if (token->type == NEWLINE || token->type == END) { printf("' (Unprintable)\n"); return; } - for (size_t i = token->string.start; i < token->string.end; i++) - printf("%c", ctx->data[i]); - printf("'\n"); + printf("'%.*s'\n", (int)(token->string.end - token->string.start), + ctx->data + token->string.start); } void tokenize(struct ctx *ctx) @@ -131,7 +131,7 @@ void tokenize(struct ctx *ctx) token_add(ctx, EQUAL, i, i + 1); continue; case ' ': - ctx->column++; + ctx->location.column++; continue; default: break; @@ -145,7 +145,7 @@ void tokenize(struct ctx *ctx) } else { // Unnamed identifier ('_') end_param = peek_to(ctx, start_param, ' '); if (end_param - start_param != 1 || ctx->data[start_param] != '_') - errln(ctx, "Invalid param identifier"); + errln(&ctx->location, "Invalid param identifier"); } token_add(ctx, TYPE, i, start_param - 1); -- cgit v1.2.3