1995-12-21 19:14:04 +03:00
|
|
|
char *rcs_lex = "$Id: lex.c,v 2.21 1995/11/16 20:46:24 roberto Exp roberto $";
|
1994-11-03 20:09:20 +03:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
1993-12-28 19:42:29 +03:00
|
|
|
#include <string.h>
|
1995-12-21 19:14:04 +03:00
|
|
|
#include <stdio.h>
|
1993-12-23 00:15:16 +03:00
|
|
|
|
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"
|
1993-12-23 00:15:16 +03:00
|
|
|
#include "opcode.h"
|
|
|
|
#include "inout.h"
|
1994-12-27 23:50:38 +03:00
|
|
|
#include "parser.h"
|
1994-10-18 20:34:34 +03:00
|
|
|
#include "ugly.h"
|
1993-12-23 00:15:16 +03:00
|
|
|
|
1995-09-16 00:48:26 +04:00
|
|
|
#define MINBUFF 260
|
|
|
|
|
1994-09-22 16:44:00 +04:00
|
|
|
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
1994-08-17 21:41:50 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
#define next() { current = input(); }
|
|
|
|
#define save(x) { *yytextLast++ = (x); }
|
|
|
|
#define save_and_next() { save(current); next(); }
|
|
|
|
|
|
|
|
static int current;
|
1995-09-16 00:48:26 +04:00
|
|
|
static char *yytext = NULL;
|
|
|
|
static int textsize = 0;
|
1993-12-23 00:15:16 +03:00
|
|
|
static char *yytextLast;
|
|
|
|
|
|
|
|
static Input input;
|
|
|
|
|
|
|
|
void lua_setinput (Input fn)
|
|
|
|
{
|
|
|
|
current = ' ';
|
|
|
|
input = fn;
|
1995-09-16 00:48:26 +04:00
|
|
|
if (yytext == NULL)
|
|
|
|
{
|
|
|
|
textsize = MINBUFF;
|
|
|
|
yytext = newvector(textsize, char);
|
|
|
|
}
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *lua_lasttext (void)
|
|
|
|
{
|
|
|
|
*yytextLast = 0;
|
1994-11-15 00:40:14 +03:00
|
|
|
return yytext;
|
1993-12-23 00:15:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-08-05 23:27:41 +04:00
|
|
|
/* The reserved words must be listed in lexicographic order */
|
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]))
|
|
|
|
|
|
|
|
|
1994-08-05 23:27:41 +04:00
|
|
|
static int findReserved (char *name)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
|
|
|
int l = 0;
|
|
|
|
int h = RESERVEDSIZE - 1;
|
|
|
|
while (l <= h)
|
|
|
|
{
|
|
|
|
int m = (l+h)/2;
|
1994-08-17 21:41:50 +04:00
|
|
|
int comp = lua_strcmp(name, reserved[m].name);
|
1993-12-23 00:15:16 +03:00
|
|
|
if (comp < 0)
|
|
|
|
h = m-1;
|
|
|
|
else if (comp == 0)
|
|
|
|
return reserved[m].token;
|
|
|
|
else
|
|
|
|
l = m+1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-09-16 00:48:26 +04:00
|
|
|
static void growtext (void)
|
|
|
|
{
|
|
|
|
int size = yytextLast - yytext;
|
|
|
|
textsize *= 2;
|
|
|
|
yytext = growvector(yytext, textsize, char);
|
|
|
|
yytextLast = yytext + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-07-06 21:47:08 +04:00
|
|
|
static int read_long_string (void)
|
|
|
|
{
|
|
|
|
int cont = 0;
|
1995-09-16 00:48:26 +04:00
|
|
|
int spaceleft = textsize - (yytextLast - yytext);
|
1995-07-06 21:47:08 +04:00
|
|
|
while (1)
|
|
|
|
{
|
1995-09-16 00:48:26 +04:00
|
|
|
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
|
|
|
|
{
|
|
|
|
growtext();
|
|
|
|
spaceleft = textsize - (yytextLast - yytext);
|
|
|
|
}
|
1995-07-06 21:47:08 +04:00
|
|
|
switch (current)
|
|
|
|
{
|
|
|
|
case EOF:
|
|
|
|
case 0:
|
|
|
|
return WRONGTOKEN;
|
|
|
|
case '[':
|
1995-09-16 00:48:26 +04:00
|
|
|
save_and_next(); spaceleft--;
|
1995-07-06 21:47:08 +04:00
|
|
|
if (current == '[')
|
|
|
|
{
|
|
|
|
cont++;
|
1995-09-16 00:48:26 +04:00
|
|
|
save_and_next(); spaceleft--;
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case ']':
|
1995-09-16 00:48:26 +04:00
|
|
|
save_and_next(); spaceleft--;
|
1995-07-06 21:47:08 +04:00
|
|
|
if (current == ']')
|
|
|
|
{
|
|
|
|
if (cont == 0) return STRING;
|
|
|
|
cont--;
|
1995-09-16 00:48:26 +04:00
|
|
|
save_and_next(); spaceleft--;
|
1995-07-06 21:47:08 +04:00
|
|
|
}
|
|
|
|
continue;
|
1995-09-16 00:48:26 +04:00
|
|
|
case '\n':
|
|
|
|
lua_linenumber++; /* goes through */
|
1995-07-06 21:47:08 +04:00
|
|
|
default:
|
1995-09-16 00:48:26 +04:00
|
|
|
save_and_next(); spaceleft--;
|
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
|
|
|
{
|
1994-10-17 22:01:53 +03:00
|
|
|
float a;
|
1995-10-25 16:05:51 +03:00
|
|
|
static int linelasttoken = 0;
|
|
|
|
if (lua_debug)
|
|
|
|
luaI_codedebugline(linelasttoken);
|
|
|
|
linelasttoken = lua_linenumber;
|
1993-12-23 00:15:16 +03:00
|
|
|
while (1)
|
|
|
|
{
|
1994-11-15 00:40:14 +03:00
|
|
|
yytextLast = yytext;
|
1994-09-26 19:21:52 +03:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr,"'%c' %d\n",current,current);
|
|
|
|
#endif
|
1993-12-23 00:15:16 +03:00
|
|
|
switch (current)
|
|
|
|
{
|
1994-10-17 22:01:53 +03:00
|
|
|
case EOF:
|
|
|
|
case 0:
|
|
|
|
return 0;
|
1995-10-25 16:05:51 +03:00
|
|
|
case '\n': linelasttoken = ++lua_linenumber;
|
1993-12-23 00:15:16 +03:00
|
|
|
case ' ':
|
1995-10-03 21:06:10 +03:00
|
|
|
case '\r': /* CR: to avoid problems with DOS/Windows */
|
1993-12-23 00:15:16 +03:00
|
|
|
case '\t':
|
1993-12-23 00:39:15 +03:00
|
|
|
next();
|
1993-12-23 00:15:16 +03:00
|
|
|
continue;
|
1993-12-23 00:39:15 +03:00
|
|
|
|
|
|
|
case '$':
|
|
|
|
next();
|
|
|
|
while (isalnum(current) || current == '_')
|
|
|
|
save_and_next();
|
|
|
|
*yytextLast = 0;
|
1994-11-15 00:40:14 +03:00
|
|
|
if (lua_strcmp(yytext, "debug") == 0)
|
1993-12-23 00:39:15 +03:00
|
|
|
{
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.vInt = 1;
|
1993-12-23 00:39:15 +03:00
|
|
|
return DEBUG;
|
|
|
|
}
|
1994-11-15 00:40:14 +03:00
|
|
|
else if (lua_strcmp(yytext, "nodebug") == 0)
|
1993-12-23 00:39:15 +03:00
|
|
|
{
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.vInt = 0;
|
1993-12-23 00:39:15 +03:00
|
|
|
return DEBUG;
|
|
|
|
}
|
|
|
|
return WRONGTOKEN;
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:15:16 +03:00
|
|
|
case '-':
|
|
|
|
save_and_next();
|
1995-10-06 16:10:53 +03:00
|
|
|
if (current != '-') return '-'; /* else goes through */
|
|
|
|
case '#':
|
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 '[' */
|
|
|
|
if (read_long_string() == WRONGTOKEN)
|
|
|
|
return WRONGTOKEN;
|
|
|
|
save_and_next(); /* pass the second ']' */
|
|
|
|
*(yytextLast-2) = 0; /* erases ']]' */
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
|
1995-07-06 21:47:08 +04:00
|
|
|
return STRING;
|
|
|
|
}
|
|
|
|
|
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;
|
1995-09-16 00:48:26 +04:00
|
|
|
int spaceleft = textsize - (yytextLast - yytext);
|
1993-12-23 00:15:16 +03:00
|
|
|
next(); /* skip the delimiter */
|
1994-09-22 16:44:00 +04:00
|
|
|
while (current != del)
|
1993-12-23 00:15:16 +03:00
|
|
|
{
|
1995-09-16 00:48:26 +04:00
|
|
|
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
|
|
|
|
{
|
|
|
|
growtext();
|
|
|
|
spaceleft = textsize - (yytextLast - yytext);
|
|
|
|
}
|
|
|
|
spaceleft--;
|
1993-12-23 00:15:16 +03:00
|
|
|
switch (current)
|
|
|
|
{
|
1994-10-17 22:01:53 +03:00
|
|
|
case EOF:
|
1994-09-22 16:44:00 +04:00
|
|
|
case 0:
|
|
|
|
case '\n':
|
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;
|
1994-09-05 23:14:40 +04:00
|
|
|
default : save(current); 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next(); /* skip the delimiter */
|
|
|
|
*yytextLast = 0;
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.vWord = luaI_findconstantbyname(yytext);
|
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 '_':
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
Word res;
|
1993-12-23 00:15:16 +03:00
|
|
|
do { save_and_next(); } while (isalnum(current) || current == '_');
|
|
|
|
*yytextLast = 0;
|
1994-11-15 00:40:14 +03:00
|
|
|
res = findReserved(yytext);
|
1993-12-23 00:15:16 +03:00
|
|
|
if (res) return res;
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.pNode = lua_constcreate(yytext);
|
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();
|
1993-12-23 00:15:16 +03:00
|
|
|
return CONC;
|
|
|
|
}
|
|
|
|
else if (!isdigit(current)) return '.';
|
|
|
|
/* 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;
|
|
|
|
do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
|
1993-12-23 00:15:16 +03:00
|
|
|
if (current == '.') save_and_next();
|
1994-10-17 22:01:53 +03:00
|
|
|
fraction:
|
|
|
|
{ float da=0.1;
|
|
|
|
while (isdigit(current))
|
|
|
|
{a+=(current-'0')*da; da/=10.0; save_and_next()};
|
|
|
|
if (current == 'e' || current == 'E')
|
|
|
|
{
|
|
|
|
int e=0;
|
|
|
|
int neg;
|
|
|
|
float ea;
|
|
|
|
save_and_next();
|
|
|
|
neg=(current=='-');
|
|
|
|
if (current == '+' || current == '-') save_and_next();
|
|
|
|
if (!isdigit(current)) return WRONGTOKEN;
|
|
|
|
do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
|
|
|
|
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
|
|
|
{
|
|
|
|
if (e & 1) a*=ea;
|
|
|
|
ea*=ea;
|
|
|
|
}
|
|
|
|
}
|
1995-12-21 19:14:04 +03:00
|
|
|
luaY_lval.vFloat = a;
|
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
|
|
|
|
|
|
|
case U_and: case U_do: case U_else: case U_elseif: case U_end:
|
|
|
|
case U_function: case U_if: case U_local: case U_nil: case U_not:
|
|
|
|
case U_or: case U_repeat: case U_return: case U_then:
|
|
|
|
case U_until: case U_while:
|
|
|
|
{
|
|
|
|
int old = current;
|
|
|
|
next();
|
|
|
|
return reserved[old-U_and].token;
|
|
|
|
}
|
|
|
|
|
1994-10-18 20:34:34 +03:00
|
|
|
case U_eq: next(); return EQ;
|
1994-09-26 19:21:52 +03:00
|
|
|
case U_le: next(); return LE;
|
|
|
|
case U_ge: next(); return GE;
|
|
|
|
case U_ne: next(); return NE;
|
|
|
|
case U_sc: next(); return CONC;
|
1994-09-22 16:44:00 +04:00
|
|
|
|
1993-12-23 00:39:15 +03:00
|
|
|
default: /* also end of file */
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|