diff options
-rw-r--r-- | cc.h | 2 | ||||
-rw-r--r-- | pp.c | 9 | ||||
-rw-r--r-- | token.c | 15 |
3 files changed, 17 insertions, 9 deletions
@@ -371,7 +371,7 @@ extern struct token tok; extern const char *tokstr[]; void tokenprint(const struct token *); -void tokendesc(char *, size_t, enum tokenkind, const char *); +char *tokencheck(const struct token *, enum tokenkind, const char *); _Noreturn void error(const struct location *, const char *, ...); /* scan */ @@ -133,14 +133,9 @@ peek(int kind) char * expect(enum tokenkind kind, const char *msg) { - char *lit, want[64], got[64]; + char *lit; - if (tok.kind != kind) { - tokendesc(want, sizeof(want), kind, NULL); - tokendesc(got, sizeof(got), tok.kind, tok.lit); - error(&tok.loc, "expected %s %s, saw %s", want, msg, got); - } - lit = tok.lit; + lit = tokencheck(&tok, kind, msg); next(); return lit; @@ -136,7 +136,7 @@ tokenprint(const struct token *t) fputs(str, stdout); } -void +static void tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit) { const char *class; @@ -162,6 +162,19 @@ tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit) snprintf(buf, len, "<unknown>"); } +char * +tokencheck(const struct token *t, enum tokenkind kind, const char *msg) +{ + char want[64], got[64]; + + if (t->kind != kind) { + tokendesc(want, sizeof(want), kind, NULL); + tokendesc(got, sizeof(got), t->kind, t->lit); + error(&t->loc, "expected %s %s, saw %s", want, msg, got); + } + return t->lit; +} + _Noreturn void error(const struct location *loc, const char *fmt, ...) { va_list ap; |