commit af0a60913386396971fb0c4311370d68ae247c26
parent 3d2cb9931a7aa956b7cd04a13de1711d7dd5dd48
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 11 Jul 2014 09:26:42 +0200
Add unexpected()
This function makes a common task, saying to the user that an
unexpected token was read from the input.
Diffstat:
6 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/cc1/cc1.h b/cc1/cc1.h
@@ -15,6 +15,7 @@ struct user_opt {
extern struct user_opt options;
extern void error(const char *fmt, ...);
extern void warn(signed char flag, const char *fmt, ...);
+extern void unexpected(void);
/* definitions of types */
diff --git a/cc1/decl.c b/cc1/decl.c
@@ -70,7 +70,7 @@ directdcl(struct dcldata *dp, int8_t flags)
} else if (flags) {
if (yytoken != IDEN) {
if (flags & ID_EXPECTED)
- error("unexpected '%s'", yytext);
+ unexpected();
sym = install("", NS_IDEN);
} else {
sym = newiden();
@@ -302,7 +302,7 @@ fielddcl(Type *base)
case ';':
break;
default:
- error("declaration expected");
+ unexpected();
}
if (yytoken != ';') {
@@ -374,7 +374,7 @@ enumdcl(void)
tp->defined = 1;
while (yytoken != '}') {
if (yytoken != IDEN)
- error("identifier expected");
+ unexpected();
sym = newiden();
sym->type = inttype;
if (accept('='))
@@ -512,7 +512,7 @@ extdecl(void)
expect(';');
return;
default:
- error("declaration expected");
+ unexpected();
}
}
diff --git a/cc1/error.c b/cc1/error.c
@@ -41,3 +41,10 @@ error(const char *fmt, ...)
warn_helper(-1, fmt, va);
va_end(va);
}
+
+void
+unexpected(void)
+{
+ error("unexpected '%s'", yytext);
+}
+
diff --git a/cc1/expr.c b/cc1/expr.c
@@ -302,7 +302,7 @@ field(Node *np)
Field *fp;
if (yytoken != IDEN)
- error("unexpected '%s'", yytext);
+ unexpected();
switch (np->typeop) {
case STRUCT: case UNION:
for (fp = np->utype->u.fields; fp; fp = fp->next) {
@@ -477,7 +477,7 @@ typeunary(void)
Node *np;
if ((np = unary()) == NULL)
- error("unexpected '%s'", yytext);
+ unexpected();
tp = np->type;
/* TODO: free np */
return tp;
@@ -530,7 +530,7 @@ cast2(void)
tp = typename();
expect(')');
if ((np1 = eval(cast())) == NULL)
- error("unexpected '%s'", yytext);
+ unexpected();
if ((np2 = convert(np1, tp, 1)) == NULL)
error("bad type convertion requested");
np2->b.lvalue = np1->b.lvalue;
diff --git a/cc1/lex.c b/cc1/lex.c
@@ -412,7 +412,7 @@ void
expect(register uint8_t tok)
{
if (yytoken != tok)
- error("unexpected %s", yytext);
+ unexpected();
next();
}
diff --git a/cc1/stmt.c b/cc1/stmt.c
@@ -193,7 +193,7 @@ Goto(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
expect(GOTO);
if (yytoken != IDEN)
- error("unexpected '%s'", yytext);
+ unexpected();
emitjump(label(yytext, 0), NULL);
next();
expect(';');
@@ -211,7 +211,7 @@ Switch(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
expect(SWITCH);
expect ('(');
if ((cond = expr()) == NULL)
- error("expected expression before '%s'", yytext);
+ unexpected();
if ((cond = convert(cond, inttype, 0)) == NULL)
error("incorrect type in switch statement");
expect (')');
@@ -242,7 +242,7 @@ Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
if (!lswitch)
error("case label not within a switch statement");
if ((np = expr()) == NULL)
- error("expected expression before '%s'", yytext);
+ unexpected();
if ((np = convert(np, inttype, 0)) == NULL)
error("incorrect type in case statement");
expect(':');