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

This commit is contained in:
Roberto Ierusalimschy
1995-09-15 17:48:26 -03:00
parent 457bac94ce
commit 367139c6d9

47
lex.c
View File

@@ -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> #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 <stdlib.h>
#include <string.h> #include <string.h>
#include "mem.h"
#include "tree.h" #include "tree.h"
#include "table.h" #include "table.h"
#include "opcode.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 "parser.h"
#include "ugly.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 lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
#define next() { current = input(); } #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(); } #define save_and_next() { save(current); next(); }
static int current; static int current;
static char yytext[3000]; static char *yytext = NULL;
static int textsize = 0;
static char *yytextLast; static char *yytextLast;
static Input input; static Input input;
@@ -30,6 +34,11 @@ void lua_setinput (Input fn)
{ {
current = ' '; current = ' ';
input = fn; input = fn;
if (yytext == NULL)
{
textsize = MINBUFF;
yytext = newvector(textsize, char);
}
} }
char *lua_lasttext (void) 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) static int read_long_string (void)
{ {
int cont = 0; int cont = 0;
int spaceleft = textsize - (yytextLast - yytext);
while (1) while (1)
{ {
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
{
growtext();
spaceleft = textsize - (yytextLast - yytext);
}
switch (current) switch (current)
{ {
case EOF: case EOF:
case 0: case 0:
return WRONGTOKEN; return WRONGTOKEN;
case '[': case '[':
save_and_next(); save_and_next(); spaceleft--;
if (current == '[') if (current == '[')
{ {
cont++; cont++;
save_and_next(); save_and_next(); spaceleft--;
} }
continue; continue;
case ']': case ']':
save_and_next(); save_and_next(); spaceleft--;
if (current == ']') if (current == ']')
{ {
if (cont == 0) return STRING; if (cont == 0) return STRING;
cont--; cont--;
save_and_next(); save_and_next(); spaceleft--;
} }
continue; continue;
case '\n':
lua_linenumber++; /* goes through */
default: default:
save_and_next(); save_and_next(); spaceleft--;
} }
} }
} }
@@ -200,9 +226,16 @@ int yylex (void)
case '\'': case '\'':
{ {
int del = current; int del = current;
int spaceleft = textsize - (yytextLast - yytext);
next(); /* skip the delimiter */ next(); /* skip the delimiter */
while (current != del) while (current != del)
{ {
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
{
growtext();
spaceleft = textsize - (yytextLast - yytext);
}
spaceleft--;
switch (current) switch (current)
{ {
case EOF: case EOF: