lexical analiser may use luaI_buffer, instead of waste space with

a separate buffer.
This commit is contained in:
Roberto Ierusalimschy
1996-05-30 11:04:07 -03:00
parent 9863223fbf
commit 5cddb264d4

135
lex.c
View File

@@ -1,4 +1,4 @@
char *rcs_lex = "$Id: lex.c,v 2.32 1996/03/21 16:33:47 roberto Exp roberto $"; char *rcs_lex = "$Id: lex.c,v 2.33 1996/05/28 21:07:32 roberto Exp roberto $";
#include <ctype.h> #include <ctype.h>
@@ -14,32 +14,24 @@ char *rcs_lex = "$Id: lex.c,v 2.32 1996/03/21 16:33:47 roberto Exp roberto $";
#define MINBUFF 260 #define MINBUFF 260
#define next() { current = input(); } #define next() (current = input())
#define save(x) { *yytextLast++ = (x); } #define save(x) (yytext[tokensize++] = (x))
#define save_and_next() { save(current); next(); } #define save_and_next() (save(current), next())
static int current;
static char *yytext = NULL;
static int textsize = 0;
static char *yytextLast;
static Input input; static int current; /* look ahead character */
static Input input; /* input function */
void lua_setinput (Input fn) 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)
{ {
*yytextLast = 0; return luaI_buffer(1);
return yytext;
} }
@@ -80,70 +72,69 @@ void luaI_addReserved (void)
} }
static void growtext (void) static int read_long_string (char *yytext, int buffsize)
{
int size = yytextLast - yytext;
textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
yytextLast = yytext + size;
}
static int read_long_string (void)
{ {
int cont = 0; int cont = 0;
int spaceleft = textsize - (yytextLast - yytext); int tokensize = 2; /* '[[' already stored */
while (1) while (1)
{ {
if (spaceleft <= 2) /* may read more than 1 char in one cicle */ if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
{ yytext = luaI_buffer(buffsize *= 2);
growtext();
spaceleft = textsize - (yytextLast - yytext);
}
switch (current) switch (current)
{ {
case EOF: case EOF:
case 0: case 0:
save(0);
return WRONGTOKEN; return WRONGTOKEN;
case '[': case '[':
save_and_next(); spaceleft--; save_and_next();
if (current == '[') if (current == '[')
{ {
cont++; cont++;
save_and_next(); spaceleft--; save_and_next();
} }
continue; continue;
case ']': case ']':
save_and_next(); spaceleft--; save_and_next();
if (current == ']') if (current == ']')
{ {
if (cont == 0) return STRING; if (cont == 0) goto endloop;
cont--; cont--;
save_and_next(); spaceleft--; save_and_next();
} }
continue; continue;
case '\n': case '\n':
lua_linenumber++; /* goes through */ lua_linenumber++; /* goes through */
default: default:
save_and_next(); spaceleft--; save_and_next();
} }
} } endloop:
save_and_next(); /* pass the second ']' */
yytext[tokensize-2] = 0; /* erases ']]' */
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
yytext[tokensize-2] = ']'; /* restores ']]' */
save(0);
return STRING;
} }
int luaY_lex (void) int luaY_lex (void)
{ {
double a;
static int linelasttoken = 0; static int linelasttoken = 0;
double a;
int buffsize = MINBUFF;
char *yytext = luaI_buffer(buffsize);
yytext[1] = yytext[2] = yytext[3] = 0;
if (lua_debug) if (lua_debug)
luaI_codedebugline(linelasttoken); luaI_codedebugline(linelasttoken);
linelasttoken = lua_linenumber; linelasttoken = lua_linenumber;
while (1) while (1)
{ {
yytextLast = yytext; int tokensize = 0;
switch (current) switch (current)
{ {
case EOF: case EOF:
case 0: case 0:
save(0);
return 0; return 0;
case '\n': linelasttoken = ++lua_linenumber; case '\n': linelasttoken = ++lua_linenumber;
case ' ': case ' ':
@@ -153,16 +144,16 @@ int luaY_lex (void)
continue; continue;
case '$': case '$':
next(); save_and_next();
while (isalnum(current) || current == '_') while (isalnum(current) || current == '_')
save_and_next(); save_and_next();
*yytextLast = 0; save(0);
if (strcmp(yytext, "debug") == 0) if (strcmp(yytext+1, "debug") == 0)
{ {
luaY_lval.vInt = 1; luaY_lval.vInt = 1;
return DEBUG; return DEBUG;
} }
else if (strcmp(yytext, "nodebug") == 0) else if (strcmp(yytext+1, "nodebug") == 0)
{ {
luaY_lval.vInt = 0; luaY_lval.vInt = 0;
return DEBUG; return DEBUG;
@@ -181,12 +172,7 @@ int luaY_lex (void)
else else
{ {
save_and_next(); /* pass the second '[' */ save_and_next(); /* pass the second '[' */
if (read_long_string() == WRONGTOKEN) return read_long_string(yytext, buffsize);
return WRONGTOKEN;
save_and_next(); /* pass the second ']' */
*(yytextLast-2) = 0; /* erases ']]' */
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
return STRING;
} }
case '=': case '=':
@@ -213,21 +199,17 @@ int luaY_lex (void)
case '\'': case '\'':
{ {
int del = current; int del = current;
int spaceleft = textsize - (yytextLast - yytext); save_and_next();
next(); /* skip the delimiter */
while (current != del) while (current != del)
{ {
if (spaceleft <= 2) /* may read more than 1 char in one cicle */ if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
{ yytext = luaI_buffer(buffsize *= 2);
growtext();
spaceleft = textsize - (yytextLast - yytext);
}
spaceleft--;
switch (current) switch (current)
{ {
case EOF: case EOF:
case 0: case 0:
case '\n': case '\n':
save(0);
return WRONGTOKEN; return WRONGTOKEN;
case '\\': case '\\':
next(); /* do not save the '\' */ next(); /* do not save the '\' */
@@ -244,9 +226,11 @@ int luaY_lex (void)
save_and_next(); save_and_next();
} }
} }
next(); /* skip the delimiter */ next(); /* skip delimiter */
*yytextLast = 0; save(0);
luaY_lval.vWord = luaI_findconstantbyname(yytext); luaY_lval.vWord = luaI_findconstantbyname(yytext+1);
tokensize--;
save(del); save(0); /* restore delimiter */
return STRING; return STRING;
} }
@@ -266,7 +250,7 @@ int luaY_lex (void)
{ {
TaggedString *ts; TaggedString *ts;
do { save_and_next(); } while (isalnum(current) || current == '_'); do { save_and_next(); } while (isalnum(current) || current == '_');
*yytextLast = 0; save(0);
ts = lua_createstring(yytext); ts = lua_createstring(yytext);
if (ts->marked > 2) if (ts->marked > 2)
return ts->marked; /* reserved word */ return ts->marked; /* reserved word */
@@ -285,8 +269,7 @@ int luaY_lex (void)
save_and_next(); save_and_next();
return DOTS; /* ... */ return DOTS; /* ... */
} }
else else return CONC; /* .. */
return CONC; /* .. */
} }
else if (!isdigit(current)) return '.'; else if (!isdigit(current)) return '.';
/* current is a digit: goes through to number */ /* current is a digit: goes through to number */
@@ -296,12 +279,19 @@ int luaY_lex (void)
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
a=0.0; a=0.0;
do { a=10.0*a+(current-'0'); save_and_next(); } while (isdigit(current)); do {
a=10.0*a+(current-'0');
save_and_next();
} while (isdigit(current));
if (current == '.') save_and_next(); if (current == '.') save_and_next();
fraction: fraction:
{ double da=0.1; { double da=0.1;
while (isdigit(current)) while (isdigit(current))
{a+=(current-'0')*da; da/=10.0; save_and_next()}; {
a+=(current-'0')*da;
da/=10.0;
save_and_next();
}
if (current == 'e' || current == 'E') if (current == 'e' || current == 'E')
{ {
int e=0; int e=0;
@@ -310,8 +300,11 @@ fraction:
save_and_next(); save_and_next();
neg=(current=='-'); neg=(current=='-');
if (current == '+' || current == '-') save_and_next(); if (current == '+' || current == '-') save_and_next();
if (!isdigit(current)) return WRONGTOKEN; if (!isdigit(current)) { save(0); return WRONGTOKEN; }
do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current)); do {
e=10.0*e+(current-'0');
save_and_next();
} while (isdigit(current));
for (ea=neg?0.1:10.0; e>0; e>>=1) for (ea=neg?0.1:10.0; e>0; e>>=1)
{ {
if (e & 1) a*=ea; if (e & 1) a*=ea;
@@ -319,6 +312,7 @@ fraction:
} }
} }
luaY_lval.vFloat = a; luaY_lval.vFloat = a;
save(0);
return NUMBER; return NUMBER;
} }
@@ -330,3 +324,4 @@ fraction:
} }
} }
} }