1997-03-18 18:30:50 +03:00
|
|
|
char *rcs_lex = "$Id: lex.c,v 2.42 1997/02/07 13:49:46 roberto Exp roberto $";
|
1996-05-30 18:04:07 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
1993-12-28 19:42:29 +03:00
|
|
|
#include <string.h>
|
1993-12-23 00:15:16 +03:00
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
#include "auxlib.h"
|
1995-09-16 00:48:26 +04:00
|
|
|
#include "mem.h"
|
1994-11-15 00:40:14 +03:00
|
|
|
#include "tree.h"
|
|
|
|
#include "table.h"
|
1996-02-13 20:30:39 +03:00
|
|
|
#include "lex.h"
|
1993-12-23 00:15:16 +03:00
|
|
|
#include "inout.h"
|
1996-02-07 17:14:40 +03:00
|
|
|
#include "luadebug.h"
|
1994-12-27 23:50:38 +03:00
|
|
|
#include "parser.h"
|
1993-12-23 00:15:16 +03:00
|
|
|
|
1997-02-07 16:49:46 +03:00
|
|
|
#define MINBUFF 250
|
1995-09-16 00:48:26 +04:00
|
|
|
|
1996-05-30 18:04:07 +04:00
|
|
|
#define next() (current = input())
|
|
|
|
#define save(x) (yytext[tokensize++] = (x))
|
|
|
|
#define save_and_next() (save(current), next())
|
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
|
1996-05-30 18:04:07 +04:00
|
|
|
static int current; /* look ahead character */
|
|
|
|
static Input input; /* input function */
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
void lua_setinput (Input fn)
|
|
|
|
{
|
1996-09-26 01:52:00 +04:00
|
|
|
current = '\n';
|
|
|
|
lua_linenumber = 0;
|
1993-12-23 00:15:16 +03:00
|
|
|
input = fn;
|
|
|
|
}
|
|
|
|
|
1997-02-07 16:49:46 +03:00
|
|
|
static void luaI_auxsyntaxerror (char *s, char *token)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
1997-03-18 18:30:50 +03:00
|
|
|
luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
|
1996-11-08 15:49:35 +03:00
|
|
|
s, token, lua_linenumber, lua_parsedfile);
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
|
1997-02-07 16:49:46 +03:00
|
|
|
void luaI_syntaxerror (char *s)
|
|
|
|
{
|
|
|
|
char *token = luaI_buffer(1);
|
|
|
|
if (token[0] == 0)
|
|
|
|
token = "<eof>";
|
|
|
|
luaI_auxsyntaxerror(s, token);
|
|
|
|
}
|
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
|
1994-09-22 16:44:00 +04:00
|
|
|
static struct
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
int token;
|
|
|
|
} reserved [] = {
|
|
|
|
{"and", AND},
|
|
|
|
{"do", DO},
|
|
|
|
{"else", ELSE},
|
|
|
|
{"elseif", ELSEIF},
|
|
|
|
{"end", END},
|
|
|
|
{"function", FUNCTION},
|
|
|
|
{"if", IF},
|
|
|
|
{"local", LOCAL},
|
|
|
|
{"nil", NIL},
|
|
|
|
{"not", NOT},
|
|
|
|
{"or", OR},
|
|
|
|
{"repeat", REPEAT},
|
|
|
|
{"return", RETURN},
|
|
|
|
{"then", THEN},
|
|
|
|
{"until", UNTIL},
|
|
|
|
{"while", WHILE} };
|
|
|
|
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
|
|
|
|
|
|
|
|
1996-02-14 16:35:51 +03:00
|
|
|
void luaI_addReserved (void)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
1996-02-14 16:35:51 +03:00
|
|
|
int i;
|
|
|
|
for (i=0; i<RESERVEDSIZE; i++)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
1996-02-14 16:35:51 +03:00
|
|
|
TaggedString *ts = lua_createstring(reserved[i].name);
|
|
|
|
ts->marked = reserved[i].token; /* reserved word (always > 255) */
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-06 23:24:29 +03:00
|
|
|
static int inclinenumber (int pragma_allowed)
|
1996-09-26 01:52:00 +04:00
|
|
|
{
|
1996-11-08 22:08:30 +03:00
|
|
|
++lua_linenumber;
|
1996-11-06 23:24:29 +03:00
|
|
|
if (pragma_allowed && current == '$') { /* is a pragma? */
|
1997-02-07 16:49:46 +03:00
|
|
|
char buff[MINBUFF+1];
|
1996-09-26 01:52:00 +04:00
|
|
|
int i = 0;
|
|
|
|
next(); /* skip $ */
|
1996-11-22 16:08:02 +03:00
|
|
|
while (isalnum((unsigned char)current)) {
|
1996-11-08 15:49:35 +03:00
|
|
|
if (i >= MINBUFF) luaI_syntaxerror("pragma too long");
|
1996-09-26 01:52:00 +04:00
|
|
|
buff[i++] = current;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
buff[i] = 0;
|
|
|
|
if (strcmp(buff, "debug") == 0)
|
|
|
|
lua_debug = 1;
|
|
|
|
else if (strcmp(buff, "nodebug") == 0)
|
|
|
|
lua_debug = 0;
|
1997-02-07 16:49:46 +03:00
|
|
|
else luaI_auxsyntaxerror("invalid pragma", buff);
|
1996-09-26 01:52:00 +04:00
|
|
|
}
|
1996-11-08 22:08:30 +03:00
|
|
|
return lua_linenumber;
|
1996-09-26 01:52:00 +04:00
|
|
|
}
|
1993-12-23 00:15:16 +03:00
|
|
|
|
1996-05-30 18:04:07 +04:00
|
|
|
static int read_long_string (char *yytext, int buffsize)
|
1995-07-06 21:47:08 +04:00
|
|
|
{
|
|
|
|
int cont = 0;
|
1996-05-30 18:04:07 +04:00
|
|
|
int tokensize = 2; /* '[[' already stored */
|
1995-07-06 21:47:08 +04:00
|
|
|
while (1)
|
|
|
|
{
|
1996-05-30 18:04:07 +04:00
|
|
|
if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
|
|
|
|
yytext = luaI_buffer(buffsize *= 2);
|
1995-07-06 21:47:08 +04:00
|
|
|
switch (current)
|
|
|
|
{
|
|
|
|
case 0:
|
1996-05-30 18:04:07 +04:00
|
|
|
save(0);
|
1995-07-06 21:47:08 +04:00
|
|
|
return WRONGTOKEN;
|
|
|
|
case '[':
|
1996-05-30 18:04:07 +04:00
|
|
|
save_and_next();
|
1995-07-06 21:47:08 +04:00
|
|
|
if (current == '[')
|
|
|
|
{
|
|
|
|
cont++;
|
1996-05-30 18:04:07 +04:00
|
|
|
save_and_next();
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
continue;
|
1995-07-06 21:47:08 +04:00
|
|
|
case ']':
|
1996-05-30 18:04:07 +04:00
|
|
|
save_and_next();
|
1995-07-06 21:47:08 +04:00
|
|
|
if (current == ']')
|
|
|
|
{
|
1996-05-30 18:04:07 +04:00
|
|
|
if (cont == 0) goto endloop;
|
1995-07-06 21:47:08 +04:00
|
|
|
cont--;
|
1996-05-30 18:04:07 +04:00
|
|
|
save_and_next();
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
continue;
|
1995-09-16 00:48:26 +04:00
|
|
|
case '\n':
|
1996-09-26 01:52:00 +04:00
|
|
|
save_and_next();
|
1996-11-06 23:24:29 +03:00
|
|
|
inclinenumber(0);
|
1996-09-26 01:52:00 +04:00
|
|
|
continue;
|
1995-07-06 21:47:08 +04:00
|
|
|
default:
|
1996-05-30 18:04:07 +04:00
|
|
|
save_and_next();
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
} 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;
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
|
|
|
|
1995-12-21 19:14:04 +03:00
|
|
|
int luaY_lex (void)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
1995-10-25 16:05:51 +03:00
|
|
|
static int linelasttoken = 0;
|
1996-05-30 18:04:07 +04:00
|
|
|
double a;
|
|
|
|
int buffsize = MINBUFF;
|
|
|
|
char *yytext = luaI_buffer(buffsize);
|
|
|
|
yytext[1] = yytext[2] = yytext[3] = 0;
|
1995-10-25 16:05:51 +03:00
|
|
|
if (lua_debug)
|
|
|
|
luaI_codedebugline(linelasttoken);
|
|
|
|
linelasttoken = lua_linenumber;
|
1993-12-23 00:15:16 +03:00
|
|
|
while (1)
|
|
|
|
{
|
1996-05-30 18:04:07 +04:00
|
|
|
int tokensize = 0;
|
1993-12-23 00:15:16 +03:00
|
|
|
switch (current)
|
|
|
|
{
|
1996-09-26 01:52:00 +04:00
|
|
|
case '\n':
|
1993-12-23 00:39:15 +03:00
|
|
|
next();
|
1996-11-06 23:24:29 +03:00
|
|
|
linelasttoken = inclinenumber(1);
|
1993-12-23 00:15:16 +03:00
|
|
|
continue;
|
1993-12-23 00:39:15 +03:00
|
|
|
|
1996-09-26 01:52:00 +04:00
|
|
|
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
|
|
|
|
next();
|
|
|
|
continue;
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
case '-':
|
|
|
|
save_and_next();
|
1996-03-14 18:17:28 +03:00
|
|
|
if (current != '-') return '-';
|
1993-12-23 00:39:15 +03:00
|
|
|
do { next(); } while (current != '\n' && current != 0);
|
1993-12-23 00:15:16 +03:00
|
|
|
continue;
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1995-07-06 21:47:08 +04:00
|
|
|
case '[':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '[') return '[';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
save_and_next(); /* pass the second '[' */
|
1996-05-30 18:04:07 +04:00
|
|
|
return read_long_string(yytext, buffsize);
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
|
|
|
|
1994-09-26 19:21:52 +03:00
|
|
|
case '=':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '=';
|
|
|
|
else { save_and_next(); return EQ; }
|
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
case '<':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '<';
|
|
|
|
else { save_and_next(); return LE; }
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
case '>':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '>';
|
|
|
|
else { save_and_next(); return GE; }
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
case '~':
|
|
|
|
save_and_next();
|
|
|
|
if (current != '=') return '~';
|
|
|
|
else { save_and_next(); return NE; }
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
{
|
|
|
|
int del = current;
|
1996-05-30 18:04:07 +04:00
|
|
|
save_and_next();
|
1994-09-22 16:44:00 +04:00
|
|
|
while (current != del)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
1996-05-30 18:04:07 +04:00
|
|
|
if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
|
|
|
|
yytext = luaI_buffer(buffsize *= 2);
|
1993-12-23 00:15:16 +03:00
|
|
|
switch (current)
|
|
|
|
{
|
1994-09-22 16:44:00 +04:00
|
|
|
case 0:
|
|
|
|
case '\n':
|
1996-05-30 18:04:07 +04:00
|
|
|
save(0);
|
1993-12-23 00:15:16 +03:00
|
|
|
return WRONGTOKEN;
|
|
|
|
case '\\':
|
|
|
|
next(); /* do not save the '\' */
|
|
|
|
switch (current)
|
|
|
|
{
|
|
|
|
case 'n': save('\n'); next(); break;
|
|
|
|
case 't': save('\t'); next(); break;
|
|
|
|
case 'r': save('\r'); next(); break;
|
1996-11-06 23:24:29 +03:00
|
|
|
case '\n': save_and_next(); inclinenumber(0); break;
|
1996-09-26 01:52:00 +04:00
|
|
|
default : save_and_next(); break;
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
break;
|
1994-09-22 16:44:00 +04:00
|
|
|
default:
|
1993-12-23 00:15:16 +03:00
|
|
|
save_and_next();
|
|
|
|
}
|
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
next(); /* skip delimiter */
|
|
|
|
save(0);
|
|
|
|
luaY_lval.vWord = luaI_findconstantbyname(yytext+1);
|
|
|
|
tokensize--;
|
|
|
|
save(del); save(0); /* restore delimiter */
|
1993-12-23 00:15:16 +03:00
|
|
|
return STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e':
|
|
|
|
case 'f': case 'g': case 'h': case 'i': case 'j':
|
|
|
|
case 'k': case 'l': case 'm': case 'n': case 'o':
|
|
|
|
case 'p': case 'q': case 'r': case 's': case 't':
|
|
|
|
case 'u': case 'v': case 'w': case 'x': case 'y':
|
|
|
|
case 'z':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
|
|
case 'F': case 'G': case 'H': case 'I': case 'J':
|
|
|
|
case 'K': case 'L': case 'M': case 'N': case 'O':
|
|
|
|
case 'P': case 'Q': case 'R': case 'S': case 'T':
|
|
|
|
case 'U': case 'V': case 'W': case 'X': case 'Y':
|
|
|
|
case 'Z':
|
|
|
|
case '_':
|
|
|
|
{
|
1996-02-14 16:35:51 +03:00
|
|
|
TaggedString *ts;
|
1996-11-22 16:08:02 +03:00
|
|
|
do {
|
|
|
|
save_and_next();
|
|
|
|
} while (isalnum((unsigned char)current) || current == '_');
|
1996-05-30 18:04:07 +04:00
|
|
|
save(0);
|
1996-02-14 16:35:51 +03:00
|
|
|
ts = lua_createstring(yytext);
|
|
|
|
if (ts->marked > 2)
|
|
|
|
return ts->marked; /* reserved word */
|
|
|
|
luaY_lval.pTStr = ts;
|
|
|
|
ts->marked = 2; /* avoid GC */
|
1993-12-23 00:15:16 +03:00
|
|
|
return NAME;
|
|
|
|
}
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
case '.':
|
|
|
|
save_and_next();
|
1994-09-22 16:44:00 +04:00
|
|
|
if (current == '.')
|
|
|
|
{
|
|
|
|
save_and_next();
|
1996-05-29 01:07:32 +04:00
|
|
|
if (current == '.')
|
|
|
|
{
|
|
|
|
save_and_next();
|
|
|
|
return DOTS; /* ... */
|
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
else return CONC; /* .. */
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
1996-11-22 16:08:02 +03:00
|
|
|
else if (!isdigit((unsigned char)current)) return '.';
|
1993-12-23 00:15:16 +03:00
|
|
|
/* current is a digit: goes through to number */
|
1994-10-17 22:01:53 +03:00
|
|
|
a=0.0;
|
1993-12-23 00:15:16 +03:00
|
|
|
goto fraction;
|
|
|
|
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
1994-10-17 22:01:53 +03:00
|
|
|
a=0.0;
|
1996-05-30 18:04:07 +04:00
|
|
|
do {
|
|
|
|
a=10.0*a+(current-'0');
|
|
|
|
save_and_next();
|
1996-11-22 16:08:02 +03:00
|
|
|
} while (isdigit((unsigned char)current));
|
1996-11-08 15:49:35 +03:00
|
|
|
if (current == '.') {
|
|
|
|
save_and_next();
|
|
|
|
if (current == '.')
|
|
|
|
luaI_syntaxerror(
|
|
|
|
"ambiguous syntax (decimal point x string concatenation)");
|
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
fraction:
|
1996-02-27 01:35:51 +03:00
|
|
|
{ double da=0.1;
|
1996-11-22 16:08:02 +03:00
|
|
|
while (isdigit((unsigned char)current))
|
1996-05-30 18:04:07 +04:00
|
|
|
{
|
|
|
|
a+=(current-'0')*da;
|
|
|
|
da/=10.0;
|
|
|
|
save_and_next();
|
|
|
|
}
|
1994-10-17 22:01:53 +03:00
|
|
|
if (current == 'e' || current == 'E')
|
|
|
|
{
|
|
|
|
int e=0;
|
|
|
|
int neg;
|
1996-02-27 01:35:51 +03:00
|
|
|
double ea;
|
1994-10-17 22:01:53 +03:00
|
|
|
save_and_next();
|
|
|
|
neg=(current=='-');
|
|
|
|
if (current == '+' || current == '-') save_and_next();
|
1996-11-22 16:08:02 +03:00
|
|
|
if (!isdigit((unsigned char)current)) {
|
|
|
|
save(0); return WRONGTOKEN; }
|
1996-05-30 18:04:07 +04:00
|
|
|
do {
|
|
|
|
e=10.0*e+(current-'0');
|
|
|
|
save_and_next();
|
1996-11-22 16:08:02 +03:00
|
|
|
} while (isdigit((unsigned char)current));
|
1996-05-30 18:04:07 +04:00
|
|
|
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
1994-10-17 22:01:53 +03:00
|
|
|
{
|
|
|
|
if (e & 1) a*=ea;
|
|
|
|
ea*=ea;
|
|
|
|
}
|
|
|
|
}
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.vFloat = a;
|
1996-05-30 18:04:07 +04:00
|
|
|
save(0);
|
1994-10-17 22:01:53 +03:00
|
|
|
return NUMBER;
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
1994-11-13 17:39:04 +03:00
|
|
|
|
1996-09-09 18:11:11 +04:00
|
|
|
default: /* also end of program (0) */
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
|
|
|
save_and_next();
|
1994-11-15 00:40:14 +03:00
|
|
|
return yytext[0];
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-05-30 18:04:07 +04:00
|
|
|
|