From a7076569d5935e493a5e04a0693a0180b047ad21 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 16 Jun 2023 07:38:28 +0200 Subject: prettier error --- src/main.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 4aa53da..0197bbc 100644 --- a/src/main.c +++ b/src/main.c @@ -91,7 +91,7 @@ error(const char *fmt, ...) { } static void -error_loc(const char *loc, const char *fmt, ...) { +error_loc(const char *loc, int len, const char *fmt, ...) { va_list va; va_start(va, fmt); verror(fmt, va); @@ -113,7 +113,10 @@ error_loc(const char *loc, const char *fmt, ...) { spaces += 1 + 7 * (line[i] == '\t'); fprintf(stderr, "%*s", spaces, ""); - fprintf(stderr, ANSI_RED "^" ANSI_RESET "\n"); + fprintf(stderr, ANSI_RED "^"); + for (int i = 1; i < len; i++) + fprintf(stderr, "~"); + fprintf(stderr, ANSI_RESET "\n"); exit(1); } @@ -122,7 +125,7 @@ static void expect(const Token *tok, TokenType type) { if (tok->type != type) - error_loc(tok->loc, "expected a %s, got %s '%.*s'", + error_loc(tok->loc, tok->len, "expected a %s, got %s '%.*s'", str_tok[type], str_tok[tok->type], tok->len, tok->loc); } @@ -136,7 +139,7 @@ static Token * skip(Token *tok, char *s) { if (!equal(tok, s)) - error_loc(tok->loc, "expected '%s'; got '%.*s'", s, tok->len, + error_loc(tok->loc, tok->len, "expected '%s'; got '%.*s'", s, tok->len, tok->loc); return tok->next; } @@ -232,7 +235,7 @@ tokenize(char *p) if (*p == '"') { char *end = strchr(p + 1, '"'); if (end == NULL) - error_loc(p, "unclosed double quotes"); + error_loc(p, 1, "unclosed double quotes"); cur = cur->next = new_token(TOK_STRING, p, end + 1); p = end + 1; continue; @@ -242,7 +245,7 @@ tokenize(char *p) char *q = p; const int c = escaped_char(p + 1, &p); if (*p != '\'') - error_loc(p, "unclosed single quotes"); + error_loc(p, 1, "unclosed single quotes"); p += 1; cur = cur->next = new_token(TOK_NUM, q, p); cur->val = (unsigned char)c; @@ -264,7 +267,7 @@ tokenize(char *p) continue; } - error_loc(p, "invalid token '%c'", *p); + error_loc(p, 1, "invalid token '%c'", *p); } cur->next = new_token(TOK_EOF, p, p); @@ -726,7 +729,7 @@ primary(Token **rest, Token *tok) return node; } - error_loc(tok->loc, "expected an expression"); + error_loc(tok->loc, tok->len, "expected an expression"); return NULL; } @@ -994,7 +997,7 @@ gen_expr(Node *node) } if (op == NULL) - error_loc(node->loc, "invalid expression %d", node->type); + error_loc(node->loc, node->len, "invalid expression %d", node->type); printf("\t%s\n", op); } @@ -1039,7 +1042,7 @@ get_define(Node *node) const int define_idx = node_find(defines, node); if (define_idx != -1) return const_expr(node_get(defines, define_idx)->rhs); - error_loc(node->loc, "define '%.*s' doesn't exist", node->len, + error_loc(node->loc, node->len, "define '%.*s' doesn't exist", node->len, node->loc); return 0; } @@ -1073,8 +1076,7 @@ const_expr(Node *node) default: break; } - error_loc(node->loc, "const expr doesn't support %d expression", - node->type); + error_loc(node->loc, node->len, "const expr doesn't support %d expression", node->type); return 0; } @@ -1217,7 +1219,7 @@ gen_stmt(Node *node, Node *fname, int break_lbl) break; case NOD_BREAK_STMT: if (break_lbl == -1) - error_loc(node->loc, "break statement outside of loop"); + error_loc(node->loc, node->len, "break statement outside of loop"); printf("\tJMP ,__lbl_%x\n", break_lbl); break; case NOD_SLP_STMT: @@ -1270,7 +1272,7 @@ gen_stmt(Node *node, Node *fname, int break_lbl) printf("@__lbl_%x\n", lbl + 1); break; default: - error_loc(node->loc, "unexpected %d", node->type); + error_loc(node->loc, node->len, "unexpected %d", node->type); } } @@ -1350,7 +1352,7 @@ codegen(Node *node) char *q = strings->lhs->loc + k; const int c = escaped_char(q, &q); if (c == '"') /* TODO: handle this */ - error_loc(q, "can't escape double quotes", 0); + error_loc(q - 2, 2, "can't escape double quotes", 0); printf("\t%04x\n", (unsigned)c); k = q - strings->lhs->loc; } -- cgit v1.2.3