lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi All,

in our use of Lua we have stumbled over a small issue regarding the readability
of parser error messages. This issue arises due to the fact that we often have
Lua scripts embedded in other files, so the Lua parser believes to start in line
1, when it actually is somewhere else in the file.

We would like to suggest the inclusion of the attached patch against
lua-5.3.0-work2, which adds a new macro named luaL_loadbuffern. It behaves just
like the - kept for compatibility - luaL_loadbuffer, except that it takes an
additional integer argument with which the line number counter in the parser is
initialised.

Thank you and best regards,
Colin
diff -urw lua-5.3.0-work2/src/lapi.c lua-5.3.0-initline/src/lapi.c
--- lua-5.3.0-work2/src/lapi.c	2014-03-12 21:57:40.000000000 +0100
+++ lua-5.3.0-initline/src/lapi.c	2014-05-15 21:06:10.000000000 +0200
@@ -992,13 +992,14 @@
 
 
 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
-                      const char *chunkname, const char *mode) {
+                                    const char *chunkname, const char *mode,
+                                    unsigned initline) {
   ZIO z;
   int status;
   lua_lock(L);
   if (!chunkname) chunkname = "?";
   luaZ_init(L, &z, reader, data);
-  status = luaD_protectedparser(L, &z, chunkname, mode);
+  status = luaD_protectedparser(L, &z, chunkname, mode, initline);
   if (status == LUA_OK) {  /* no errors? */
     LClosure *f = clLvalue(L->top - 1);  /* get newly created function */
     if (f->nupvalues == 1) {  /* does it have one upvalue? */
diff -urw lua-5.3.0-work2/src/lauxlib.c lua-5.3.0-initline/src/lauxlib.c
--- lua-5.3.0-work2/src/lauxlib.c	2014-03-12 21:57:40.000000000 +0100
+++ lua-5.3.0-initline/src/lauxlib.c	2014-05-15 21:11:51.000000000 +0200
@@ -669,7 +669,7 @@
   }
   if (c != EOF)
     lf.buff[lf.n++] = c;  /* 'c' is the first character of the stream */
-  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
+  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode, 1);
   readstatus = ferror(lf.f);
   if (filename) fclose(lf.f);  /* close file (even in case of errors) */
   if (readstatus) {
@@ -698,11 +698,12 @@
 
 
 LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
-                                 const char *name, const char *mode) {
+                                 const char *name, const char *mode,
+                                 unsigned initline) {
   LoadS ls;
   ls.s = buff;
   ls.size = size;
-  return lua_load(L, getS, &ls, name, mode);
+  return lua_load(L, getS, &ls, name, mode, initline);
 }
 
 
diff -urw lua-5.3.0-work2/src/lauxlib.h lua-5.3.0-initline/src/lauxlib.h
--- lua-5.3.0-work2/src/lauxlib.h	2014-01-05 15:04:46.000000000 +0100
+++ lua-5.3.0-initline/src/lauxlib.h	2014-05-15 21:11:26.000000000 +0200
@@ -81,7 +81,8 @@
 #define luaL_loadfile(L,f)	luaL_loadfilex(L,f,NULL)
 
 LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
-                                   const char *name, const char *mode);
+                                   const char *name, const char *mode,
+                                   unsigned initline);
 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
 
 LUALIB_API lua_State *(luaL_newstate) (void);
@@ -135,7 +136,8 @@
 
 #define luaL_opt(L,f,n,d)	(lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
 
-#define luaL_loadbuffer(L,s,sz,n)	luaL_loadbufferx(L,s,sz,n,NULL)
+#define luaL_loadbuffer(L,s,sz,n)	luaL_loadbufferx(L,s,sz,n,NULL,1)
+#define luaL_loadbuffern(L,s,sz,n,i)	luaL_loadbufferx(L,s,sz,n,NULL,i)
 
 
 /*
diff -urw lua-5.3.0-work2/src/lbaselib.c lua-5.3.0-initline/src/lbaselib.c
--- lua-5.3.0-work2/src/lbaselib.c	2014-03-12 21:57:40.000000000 +0100
+++ lua-5.3.0-initline/src/lbaselib.c	2014-05-15 21:12:25.000000000 +0200
@@ -327,13 +327,13 @@
   int env = (!lua_isnone(L, 4) ? 4 : 0);  /* 'env' index or 0 if no 'env' */
   if (s != NULL) {  /* loading a string? */
     const char *chunkname = luaL_optstring(L, 2, s);
-    status = luaL_loadbufferx(L, s, l, chunkname, mode);
+    status = luaL_loadbufferx(L, s, l, chunkname, mode, 1);
   }
   else {  /* loading from a reader function */
     const char *chunkname = luaL_optstring(L, 2, "=(load)");
     luaL_checktype(L, 1, LUA_TFUNCTION);
     lua_settop(L, RESERVEDSLOT);  /* create reserved slot */
-    status = lua_load(L, generic_reader, NULL, chunkname, mode);
+    status = lua_load(L, generic_reader, NULL, chunkname, mode, 1);
   }
   return load_aux(L, status, env);
 }
diff -urw lua-5.3.0-work2/src/ldo.c lua-5.3.0-initline/src/ldo.c
--- lua-5.3.0-work2/src/ldo.c	2014-03-21 14:52:33.000000000 +0100
+++ lua-5.3.0-initline/src/ldo.c	2014-05-15 21:06:58.000000000 +0200
@@ -632,6 +632,7 @@
   Dyndata dyd;  /* dynamic structures used by the parser */
   const char *mode;
   const char *name;
+  unsigned initline;
 };
 
 
@@ -654,7 +655,7 @@
   }
   else {
     checkmode(L, p->mode, "text");
-    cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
+    cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c, p->initline);
   }
   lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
   luaF_initupvals(L, &cl->l);
@@ -662,11 +663,11 @@
 
 
 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
-                                        const char *mode) {
+                          const char *mode, unsigned initline) {
   struct SParser p;
   int status;
   L->nny++;  /* cannot yield during parsing */
-  p.z = z; p.name = name; p.mode = mode;
+  p.z = z; p.name = name; p.mode = mode; p.initline = initline;
   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
diff -urw lua-5.3.0-work2/src/ldo.h lua-5.3.0-initline/src/ldo.h
--- lua-5.3.0-work2/src/ldo.h	2011-11-29 16:55:08.000000000 +0100
+++ lua-5.3.0-initline/src/ldo.h	2014-05-15 21:04:30.000000000 +0200
@@ -27,7 +27,7 @@
 typedef void (*Pfunc) (lua_State *L, void *ud);
 
 LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
-                                                  const char *mode);
+                                    const char *mode, unsigned initline);
 LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
 LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
 LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults,
diff -urw lua-5.3.0-work2/src/llex.c lua-5.3.0-initline/src/llex.c
--- lua-5.3.0-work2/src/llex.c	2014-02-14 16:23:51.000000000 +0100
+++ lua-5.3.0-initline/src/llex.c	2014-05-15 20:59:17.000000000 +0200
@@ -160,15 +160,15 @@
 
 
 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
-                    int firstchar) {
+                    int firstchar, unsigned initline) {
   ls->decpoint = '.';
   ls->L = L;
   ls->current = firstchar;
   ls->lookahead.token = TK_EOS;  /* no look-ahead token */
   ls->z = z;
   ls->fs = NULL;
-  ls->linenumber = 1;
-  ls->lastline = 1;
+  ls->linenumber = initline;
+  ls->lastline = initline;
   ls->source = source;
   ls->envn = luaS_new(L, LUA_ENV);  /* get env name */
   luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER);  /* initialize buffer */
diff -urw lua-5.3.0-work2/src/llex.h lua-5.3.0-initline/src/llex.h
--- lua-5.3.0-work2/src/llex.h	2013-12-30 21:47:58.000000000 +0100
+++ lua-5.3.0-initline/src/llex.h	2014-05-15 20:59:40.000000000 +0200
@@ -71,7 +71,8 @@
 
 LUAI_FUNC void luaX_init (lua_State *L);
 LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
-                              TString *source, int firstchar);
+                              TString *source, int firstchar,
+                              unsigned initline);
 LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
 LUAI_FUNC void luaX_next (LexState *ls);
 LUAI_FUNC int luaX_lookahead (LexState *ls);
diff -urw lua-5.3.0-work2/src/lparser.c lua-5.3.0-initline/src/lparser.c
--- lua-5.3.0-work2/src/lparser.c	2013-12-30 21:47:58.000000000 +0100
+++ lua-5.3.0-initline/src/lparser.c	2014-05-15 21:00:21.000000000 +0200
@@ -1619,7 +1619,8 @@
 
 
 Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
-                      Dyndata *dyd, const char *name, int firstchar) {
+                      Dyndata *dyd, const char *name, int firstchar,
+                      unsigned initline) {
   LexState lexstate;
   FuncState funcstate;
   Closure *cl = luaF_newLclosure(L, 1);  /* create main closure */
@@ -1634,7 +1635,7 @@
   lexstate.buff = buff;
   lexstate.dyd = dyd;
   dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
-  luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
+  luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar, initline);
   mainfunc(&lexstate, &funcstate);
   lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
   /* all scopes should be correctly finished */
diff -urw lua-5.3.0-work2/src/lparser.h lua-5.3.0-initline/src/lparser.h
--- lua-5.3.0-work2/src/lparser.h	2013-08-30 18:01:37.000000000 +0200
+++ lua-5.3.0-initline/src/lparser.h	2014-05-15 21:00:33.000000000 +0200
@@ -114,7 +114,8 @@
 
 
 LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
-                                Dyndata *dyd, const char *name, int firstchar);
+                                Dyndata *dyd, const char *name, int firstchar,
+                                unsigned initline);
 
 
 #endif
diff -urw lua-5.3.0-work2/src/lua.h lua-5.3.0-initline/src/lua.h
--- lua-5.3.0-work2/src/lua.h	2014-03-21 21:34:25.000000000 +0100
+++ lua-5.3.0-initline/src/lua.h	2014-05-15 21:06:25.000000000 +0200
@@ -269,7 +269,8 @@
 
 LUA_API int   (lua_load) (lua_State *L, lua_Reader reader, void *dt,
                                         const char *chunkname,
-                                        const char *mode);
+                                        const char *mode,
+                                        unsigned initline);
 
 LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
 
diff -urw lua-5.3.0-work2/src/luac.c lua-5.3.0-initline/src/luac.c
--- lua-5.3.0-work2/src/luac.c	2011-11-29 18:46:33.000000000 +0100
+++ lua-5.3.0-initline/src/luac.c	2014-05-15 21:12:51.000000000 +0200
@@ -141,7 +141,7 @@
  {
   Proto* f;
   int i=n;
-  if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
+  if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL,1)!=LUA_OK) fatal(lua_tostring(L,-1));
   f=toproto(L,-1);
   for (i=0; i<n; i++)
   {