some changes in error reporting

This commit is contained in:
Roberto Ierusalimschy 2003-09-04 17:00:28 -03:00
parent 994a37c8e8
commit 30e51f09b9

58
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 1.123 2003/08/28 14:38:46 roberto Exp roberto $ ** $Id: llex.c,v 1.124 2003/08/29 16:48:14 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -77,45 +77,40 @@ static void luaX_error (LexState *ls, const char *s, const char *token) {
} }
void luaX_syntaxerror (LexState *ls, const char *msg) {
const char *lasttoken;
switch (ls->t.token) {
case TK_NAME:
lasttoken = getstr(ls->t.seminfo.ts);
break;
case TK_STRING:
case TK_NUMBER:
save(ls, '\0');
lasttoken = luaZ_buffer(ls->buff);
break;
default:
lasttoken = luaX_token2str(ls, ls->t.token);
break;
}
luaX_error(ls, msg, lasttoken);
}
const char *luaX_token2str (LexState *ls, int token) { const char *luaX_token2str (LexState *ls, int token) {
if (token < FIRST_RESERVED) { if (token < FIRST_RESERVED) {
lua_assert(token == (unsigned char)token); lua_assert(token == (unsigned char)token);
return luaO_pushfstring(ls->L, "%c", token); return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
luaO_pushfstring(ls->L, "%c", token);
} }
else else
return token2string[token-FIRST_RESERVED]; return token2string[token-FIRST_RESERVED];
} }
static void luaX_lexerror (LexState *ls, const char *s, int token) { static const char *txtToken (LexState *ls, int token) {
if (token == TK_EOS) switch (token) {
luaX_error(ls, s, luaX_token2str(ls, token)); case TK_NAME:
else { case TK_STRING:
save(ls, '\0'); case TK_NUMBER:
luaX_error(ls, s, luaZ_buffer(ls->buff)); save(ls, '\0');
return luaZ_buffer(ls->buff);
default:
return luaX_token2str(ls, token);
} }
} }
static void luaX_lexerror (LexState *ls, const char *msg, int token) {
luaX_error(ls, msg, txtToken(ls, token));
}
void luaX_syntaxerror (LexState *ls, const char *msg) {
luaX_lexerror(ls, msg, ls->t.token);
}
TString *luaX_newstring (LexState *ls, const char *str, size_t l) { TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
lua_State *L = ls->L; lua_State *L = ls->L;
TString *ts = luaS_newlstr(L, str, l); TString *ts = luaS_newlstr(L, str, l);
@ -406,14 +401,13 @@ int luaX_lex (LexState *ls, SemInfo *seminfo) {
luaZ_bufflen(ls->buff)); luaZ_bufflen(ls->buff));
if (ts->tsv.reserved > 0) /* reserved word? */ if (ts->tsv.reserved > 0) /* reserved word? */
return ts->tsv.reserved - 1 + FIRST_RESERVED; return ts->tsv.reserved - 1 + FIRST_RESERVED;
seminfo->ts = ts; else {
return TK_NAME; seminfo->ts = ts;
return TK_NAME;
}
} }
else { else {
int c = ls->current; int c = ls->current;
if (iscntrl(c))
luaX_error(ls, "invalid control char",
luaO_pushfstring(ls->L, "char(%d)", c));
next(ls); next(ls);
return c; /* single-char tokens (+ - / ...) */ return c; /* single-char tokens (+ - / ...) */
} }