lua-users home
lua-l archive

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


I needed this for something that never got released.  This is cut against a modified version of 5.3.4.

Share and Enjoy,
Keni


diff --git a/util/nhlsrc/lua-5.3.4/src/llex.c b/util/nhlsrc/lua-5.3.4/src/llex.c
index 7032827..8a96900 100644
--- a/util/nhlsrc/lua-5.3.4/src/llex.c
+++ b/util/nhlsrc/lua-5.3.4/src/llex.c
@@ -143,12 +143,25 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
   return ts;
 }
 
+#ifdef LUA_ATLINE
+/* XXX should be merged with save() */
+static void savebuff (LexState *ls, Mbuffer *b, int c) {
+  if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
+    size_t newsize;
+    if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
+      lexerror(ls, "@filename too long", 0);
+    newsize = luaZ_sizebuffer(b) * 2;
+    luaZ_resizebuffer(ls->L, b, newsize);
+  }
+  b->buffer[luaZ_bufflen(b)++] = cast(char, c);
+}
+#endif
 
 /*
 ** increment line number and skips newline sequence (any of
 ** \n, \r, \n\r, or \r\n)
 */
-static void inclinenumber (LexState *ls) {
+static void inclinenumber0 (LexState *ls, int domagic) {
   int old = ls->current;
   lua_assert(currIsNewline(ls));
   next(ls);  /* skip '\n' or '\r' */
@@ -156,8 +169,67 @@ static void inclinenumber (LexState *ls) {
     next(ls);  /* skip '\n\r' or '\r\n' */
   if (++ls->linenumber >= MAX_INT)
     lexerror(ls, "chunk has too many lines", 0);
+#ifdef LUA_ATLINE
+       /* XXX lparser.c:statement() saves line number around statement - is
+       this a problem? */
+  if(domagic && ls->current == '@'){
+    int n = 0;
+    do {
+      next(ls);
+    } while(ls->current == ' ' || ls->current == '\t');
+
+    if(currIsNewline(ls))
+      lexerror(ls, "@ line is empty", 0);
+
+    while(lisdigit(ls->current)){
+      n = (n*10) + ls->current - '0';
+      if(n >= MAX_INT)
+       lexerror(ls, "@ line number too large", 0);
+      next(ls);
+    };
+    ls->linenumber = n-1;
+
+    while(ls->current == ' ' || ls->current == '\t')
+      next(ls);
+
+    if(!currIsNewline(ls)){
+      Mbuffer filebuff;
+      luaZ_initbuffer(ls->L, &filebuff);
+      luaZ_resetbuffer(&filebuff);
+      luaZ_resizebuffer(ls->L, &filebuff, 100);
+      savebuff(ls, &filebuff, '@');
+
+      while(1){
+       switch (ls->current) {
+       case EOZ:
+           lexerror(ls, "unfinished filename", 0);
+           goto out;
+       case '\n':
+       case '\r':
+           goto out;
+       default:
+           savebuff(ls, &filebuff, ls->current);
+           next(ls);
+           break;
+       }
+      }
+out:
+      ls->source = luaX_newstring(ls, luaZ_buffer(&filebuff),
+          luaZ_bufflen(&filebuff));
+
+      luaZ_freebuffer(ls->L, &filebuff);
+    }
+  }
+#endif
 }
 
+static void inclinenumber (LexState *ls) {
+  inclinenumber0(ls, 1);
+}
+
+static void inclinenumber_noat (LexState *ls) {
+  inclinenumber0(ls, 0);
+}
 
 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
                     int firstchar) {
@@ -284,7 +356,7 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
       }
       case '\n': case '\r': {
         save(ls, '\n');
-        inclinenumber(ls);
+        inclinenumber_noat(ls);
         if (!seminfo) luaZ_resetbuffer(ls->buff);  /* avoid wasting space */
         break;
       }
diff --git a/util/nhlsrc/lua-5.3.4/src/luaconf.h b/util/nhlsrc/lua-5.3.4/src/luaconf.h
index f37bea0..925b3b5 100644
--- a/util/nhlsrc/lua-5.3.4/src/luaconf.h
+++ b/util/nhlsrc/lua-5.3.4/src/luaconf.h
@@ -775,9 +775,23 @@
 ** without modifying the main part of the file.
 */
 
-
-
-
+       /* accept lines of the form: @filename [lineno] ala #line.
+        * Unfortunately this only partly works: the filename isn't tracked
+        * by Lua in a way that this can ever work easily.  So it's really
+        * only useful for debugging something like yacc.
+        *
+        * If for some reason someone really wants to make this work:
+        * - add a fileinfo similar to lineinfo in struct Proto
+        * - add a table of filenames; fileinfo entries are int subscripts
+        *   to this table
+        * - compile time option for char/short/long size of entries in that tbl
+        * - optimization: if the entire table has the same value (the usual
+        *   case, write out 0 for the table size and fall back to the existing
+        *   filename
+        * - see luaK_code, DumpDebug
+        * - luac option to suppress this; pragma(?) to suppress this
+        */
+#define LUA_ATLINE
 
 #endif
 


> 
> Date: Wed, 06 Apr 2022 10:02:05 -0400
> From: binarycat <binarycat@envs.net>
> Subject: lua equivalent to the #line directive
> To: lua-l@lists.lua.org
> Message-ID: <5370DF24-4D70-4F21-BC88-99FA8CD2A022@envs.net>
> Content-Type: multipart/alternative;
> 	boundary=----C73KWKRS2FC4GOVN0UEP32TE7LZKBB
> 
> ------C73KWKRS2FC4GOVN0UEP32TE7LZKBB
> Content-Type: text/plain;
> charset=utf-8
> Content-Transfer-Encoding: quoted-printable
> 
> it seems like compiling things to lua is becoming somewhat popular, but one=
> problem with that is line numbers in error messages will then not match up=
> =2E
> 
> i was messing around with literate programming, and i decided i should add=
> line number stuff, but after some looking around, it doesn't seem like suc=
> h a thing exists=2E
> 
> is there something i'm missing? if not, is there some reason lua doesn't i=
> nclude this?
> 
> --
> binarycat
> ------C73KWKRS2FC4GOVN0UEP32TE7LZKBB
> Content-Type: text/html;
> charset=utf-8
> Content-Transfer-Encoding: quoted-printable
> 
> <!DOCTYPE html><html><body>it seems like compiling things to lua is becomin=
> g somewhat popular, but one problem with that is line numbers in error mess=
> ages will then not match up=2E<br><br>i was messing around with literate pr=
> ogramming, and i decided i should add line number stuff, but after some loo=
> king around, it doesn't seem like such a thing exists=2E<br><br>is there so=
> mething i'm missing? if not, is there some reason lua doesn't include this?=
> <br><br>--<br>binarycat</body></html>
> ------C73KWKRS2FC4GOVN0UEP32TE7LZKBB--