aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cc.h2
-rw-r--r--pp.c9
-rw-r--r--token.c15
3 files changed, 17 insertions, 9 deletions
diff --git a/cc.h b/cc.h
index 96dde5e..05459e1 100644
--- 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
index e62845f..4831cd6 100644
--- 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
index 2397756..6d4484d 100644
--- 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;