diff options
author | Marvin Borner | 2021-08-07 23:39:21 +0200 |
---|---|---|
committer | Marvin Borner | 2021-08-07 23:39:21 +0200 |
commit | 51c4defc436c0d119941eb6d5b953d27b5b8e6f7 (patch) | |
tree | c90b6329b81a4c4715f7860cb3a6a7b1d3174e37 /src/tokenize.c | |
parent | 55e5ec54eaef97e87efefc2294a5afaf8935566b (diff) |
Better error logging
Diffstat (limited to 'src/tokenize.c')
-rw-r--r-- | src/tokenize.c | 32 |
1 files changed, 16 insertions, 16 deletions
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); |