Browse Source

buffer for literals now grows dynamically, allowing big programs between [[ and ]].

Roberto Ierusalimschy 30 years ago
parent
commit
367139c6d9
1 changed files with 40 additions and 7 deletions
  1. 40 7
      lex.c

+ 40 - 7
lex.c

@@ -1,4 +1,4 @@
-char *rcs_lex = "$Id: lex.c,v 2.14 1994/12/27 20:50:38 celes Exp $";
+char *rcs_lex = "$Id: lex.c,v 2.15 1995/07/06 17:47:08 roberto Exp roberto $";
  
 
 #include <ctype.h>
@@ -7,6 +7,7 @@ char *rcs_lex = "$Id: lex.c,v 2.14 1994/12/27 20:50:38 celes Exp $";
 #include <stdlib.h>
 #include <string.h>
 
+#include "mem.h"
 #include "tree.h"
 #include "table.h"
 #include "opcode.h"
@@ -14,6 +15,8 @@ char *rcs_lex = "$Id: lex.c,v 2.14 1994/12/27 20:50:38 celes Exp $";
 #include "parser.h"
 #include "ugly.h"
 
+#define MINBUFF 260
+
 #define lua_strcmp(a,b)	(a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
 
 #define next() { current = input(); }
@@ -21,7 +24,8 @@ char *rcs_lex = "$Id: lex.c,v 2.14 1994/12/27 20:50:38 celes Exp $";
 #define save_and_next()  { save(current); next(); }
 
 static int current;
-static char yytext[3000];
+static char *yytext = NULL;
+static int textsize = 0;
 static char *yytextLast;
 
 static Input input;
@@ -30,6 +34,11 @@ void lua_setinput (Input fn)
 {
   current = ' ';
   input = fn;
+  if (yytext == NULL)
+  {
+    textsize = MINBUFF;
+    yytext = newvector(textsize, char);
+  }
 }
 
 char *lua_lasttext (void)
@@ -85,35 +94,52 @@ static int findReserved (char *name)
 }
 
 
+static void growtext (void)
+{
+  int size = yytextLast - yytext;
+  textsize *= 2;
+  yytext = growvector(yytext, textsize, char);
+  yytextLast = yytext + size;
+}
+
+
 static int read_long_string (void)
 {
   int cont = 0;
+  int spaceleft = textsize - (yytextLast - yytext);
   while (1)
   {
+    if (spaceleft <= 2)  /* may read more than 1 char in one cicle */
+    {
+      growtext();
+      spaceleft = textsize - (yytextLast - yytext);
+    }
     switch (current)
     {
       case EOF:
       case 0:
         return WRONGTOKEN;
       case '[':
-        save_and_next();
+        save_and_next(); spaceleft--;
         if (current == '[')
         {
           cont++;
-          save_and_next();
+          save_and_next(); spaceleft--;
         }
         continue; 
       case ']':
-        save_and_next();
+        save_and_next(); spaceleft--;
         if (current == ']')
         {
           if (cont == 0) return STRING;
           cont--;
-          save_and_next();
+          save_and_next(); spaceleft--;
         }
         continue; 
+      case '\n':
+        lua_linenumber++;  /* goes through */
       default:
-        save_and_next();
+        save_and_next(); spaceleft--;
     }
   }
 }
@@ -200,9 +226,16 @@ int yylex (void)
       case '\'':
       {
         int del = current;
+        int spaceleft = textsize - (yytextLast - yytext);
         next();  /* skip the delimiter */
         while (current != del)
         {
+          if (spaceleft <= 2) /* may read more than 1 char in one cicle */
+          {
+            growtext();
+            spaceleft = textsize - (yytextLast - yytext);
+          }
+          spaceleft--;
           switch (current)
           {
             case EOF: