From df5c3cf3c53f91e4135508cd9925784b4b2d842c Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Tue, 17 Mar 2020 23:58:52 -0700 Subject: token: Add TOTHER for other non-whitespace tokens This is for any non-whitespace character that doesn't match any other token categories, and could be valid if stringified, or the preprocessor is used by itself. --- cc.h | 1 + scan.c | 6 +++--- token.c | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cc.h b/cc.h index 85f739a..b9df86c 100644 --- a/cc.h +++ b/cc.h @@ -7,6 +7,7 @@ enum tokenkind { TEOF, TNEWLINE, + TOTHER, TIDENT, TNUMBER, diff --git a/scan.c b/scan.c index c9eb5ea..34e003e 100644 --- a/scan.c +++ b/scan.c @@ -407,9 +407,9 @@ again: return number(s); if (isalpha(s->chr) || s->chr == '_') return ident(s); - if (isprint(s->chr)) - error(&s->loc, "unexpected character '%c'", s->chr); - error(&s->loc, "unexpected character '\\x%02x'", s->chr); + s->usebuf = true; + nextchar(s); + return TOTHER; } } diff --git a/token.c b/token.c index dc2df02..b3673a8 100644 --- a/token.c +++ b/token.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -149,7 +150,8 @@ tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit) case TNUMBER: class = "number"; quote = true; break; case TCHARCONST: class = "character"; quote = false; break; case TSTRINGLIT: class = "string"; quote = false; break; - case TNEWLINE: class = "newline"; quote = true; break; + case TNEWLINE: class = "newline"; break; + case TOTHER: class = NULL; break; default: class = NULL; lit = kind < LEN(tokstr) ? tokstr[kind] : NULL; @@ -158,6 +160,8 @@ tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit) snprintf(buf, len, quote ? "%s '%s'" : "%s %s", class, lit); else if (class) snprintf(buf, len, "%s", class); + else if (kind == TOTHER && !isprint(lit[0])) + snprintf(buf, len, "", lit[0]); else if (lit) snprintf(buf, len, "'%s'", lit); else -- cgit v1.2.3