From a003e891252a2f6b4b7d1d006b20d2b306626caa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Mar 2004 16:53:56 -0300 Subject: better error messages for some limits --- llex.c | 21 +++++++-------------- llex.h | 4 ++-- lparser.c | 24 +++++++++++++++++------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/llex.c b/llex.c index bb83571e..1d26858c 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 1.128 2003/10/20 12:24:34 roberto Exp roberto $ +** $Id: llex.c,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -61,14 +61,6 @@ void luaX_init (lua_State *L) { #define MAXSRC 80 -void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) { - if (val > limit) { - msg = luaO_pushfstring(ls->L, "too many %s (limit=%d)", msg, limit); - luaX_syntaxerror(ls, msg); - } -} - - const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { lua_assert(token == (unsigned char)token); @@ -93,11 +85,12 @@ static const char *txtToken (LexState *ls, int token) { } -static void luaX_lexerror (LexState *ls, const char *msg, int token) { +void luaX_lexerror (LexState *ls, const char *msg, int token) { char buff[MAXSRC]; luaO_chunkid(buff, getstr(ls->source), MAXSRC); - luaO_pushfstring(ls->L, "%s:%d: %s near `%s'", - buff, ls->linenumber, msg, txtToken(ls, token)); + msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); + if (token) + luaO_pushfstring(ls->L, "%s near `%s'", msg, txtToken(ls, token)); luaD_throw(ls->L, LUA_ERRSYNTAX); } @@ -123,8 +116,8 @@ static void inclinenumber (LexState *ls) { next(ls); /* skip `\n' or `\r' */ if (currIsNewline(ls) && ls->current != old) next(ls); /* skip `\n\r' or `\r\n' */ - ++ls->linenumber; - luaX_checklimit(ls, ls->linenumber, MAX_INT, "lines in a chunk"); + if (++ls->linenumber >= MAX_INT) + luaX_syntaxerror(ls, "chunk has too many lines"); } diff --git a/llex.h b/llex.h index e37c6f54..deb9e74d 100644 --- a/llex.h +++ b/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.48 2003/08/27 21:01:44 roberto Exp roberto $ +** $Id: llex.h,v 1.49 2003/10/20 12:24:34 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -67,7 +67,7 @@ void luaX_init (lua_State *L); void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source); TString *luaX_newstring (LexState *LS, const char *str, size_t l); int luaX_lex (LexState *LS, SemInfo *seminfo); -void luaX_checklimit (LexState *ls, int val, int limit, const char *msg); +void luaX_lexerror (LexState *ls, const char *msg, int token); void luaX_syntaxerror (LexState *ls, const char *s); const char *luaX_token2str (LexState *ls, int token); diff --git a/lparser.c b/lparser.c index 42fde44c..ec5ae272 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 1.222 2003/12/09 16:56:11 roberto Exp roberto $ +** $Id: lparser.c,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -28,9 +28,10 @@ #define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) +#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) luaY_errorlimit(fs,l,m) -#define enterlevel(ls) if (++(ls)->nestlevel > LUA_MAXPARSERLEVEL) \ - luaX_syntaxerror(ls, "too many syntax levels"); +#define enterlevel(ls) if (++(ls)->nestlevel > LUA_MAXPARSERLEVEL) \ + luaX_lexerror(ls, "chunk has too many syntax levels", 0) #define leavelevel(ls) ((ls)->nestlevel--) @@ -86,6 +87,15 @@ static void error_expected (LexState *ls, int token) { } +static void luaY_errorlimit (FuncState *fs, int limit, const char *what) { + const char *msg = (fs->f->lineDefined == 0) ? + luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : + luaO_pushfstring(fs->L, "function at line %d has more than %d %s", + fs->f->lineDefined, limit, what); + luaX_lexerror(fs->ls, msg, 0); +} + + static int testnext (LexState *ls, int c) { if (ls->t.token == c) { next(ls); @@ -163,7 +173,7 @@ static int luaI_registerlocalvar (LexState *ls, TString *varname) { static void new_localvar (LexState *ls, TString *name, int n) { FuncState *fs = ls->fs; - luaX_checklimit(ls, fs->nactvar+n+1, MAXVARS, "local variables"); + luaY_checklimit(fs, fs->nactvar+n+1, MAXVARS, "local variables"); fs->actvar[fs->nactvar+n] = cast(unsigned short, luaI_registerlocalvar(ls, name)); } @@ -196,7 +206,7 @@ static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { } } /* new one */ - luaX_checklimit(fs->ls, f->nups + 1, MAXUPVALUES, "upvalues"); + luaY_checklimit(fs, f->nups + 1, MAXUPVALUES, "upvalues"); luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, TString *, MAX_INT, ""); while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; @@ -441,7 +451,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { int reg = ls->fs->freereg; expdesc key, val; if (ls->t.token == TK_NAME) { - luaX_checklimit(ls, cc->nh, MAX_INT, "items in a constructor"); + luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); cc->nh++; checkname(ls, &key); } @@ -485,7 +495,7 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) { static void listfield (LexState *ls, struct ConsControl *cc) { expr(ls, &cc->v); - luaX_checklimit(ls, cc->na, MAXARG_Bx, "items in a constructor"); + luaY_checklimit(ls->fs, cc->na, MAXARG_Bx, "items in a constructor"); cc->na++; cc->tostore++; } -- cgit v1.2.1