Browse Source

better error messages for some limits

Roberto Ierusalimschy 21 years ago
parent
commit
a003e89125
3 changed files with 26 additions and 23 deletions
  1. 7 14
      llex.c
  2. 2 2
      llex.h
  3. 17 7
      lparser.c

+ 7 - 14
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");
 }
 
 

+ 2 - 2
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);
 

+ 17 - 7
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++;
 }