cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit ca7d50a3fef8d4f2bfa199a0e334217978fd6483
parent d05ebf8bed8ac45b50263c85ce1c0d2327d66d55
Author: Michael Forney <mforney@mforney.org>
Date:   Mon,  9 Mar 2020 02:02:58 -0700

token: Add tokencheck utility function

This function is like expect(), but operates on a specific token
and does not read the following token.

Diffstat:
Mcc.h | 2+-
Mpp.c | 9++-------
Mtoken.c | 15++++++++++++++-
3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/cc.h b/cc.h @@ -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 */ diff --git a/pp.c b/pp.c @@ -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; diff --git a/token.c b/token.c @@ -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;