This commit is contained in:
Roberto Ierusalimschy
2005-12-07 13:43:05 -02:00
parent 6cd461633d
commit 87024e257d
3 changed files with 49 additions and 50 deletions

22
llex.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.13 2005/11/08 19:45:14 roberto Exp roberto $ ** $Id: llex.c,v 2.14 2005/12/07 15:32:52 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -319,7 +319,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
} }
int luaX_lex (LexState *ls, SemInfo *seminfo) { static int llex (LexState *ls, SemInfo *seminfo) {
luaZ_resetbuffer(ls->buff); luaZ_resetbuffer(ls->buff);
for (;;) { for (;;) {
switch (ls->current) { switch (ls->current) {
@@ -432,4 +432,20 @@ int luaX_lex (LexState *ls, SemInfo *seminfo) {
} }
} }
#undef next
void luaX_next (LexState *ls) {
ls->lastline = ls->linenumber;
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
ls->t = ls->lookahead; /* use this one */
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else
ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
}
void luaX_lookahead (LexState *ls) {
lua_assert(ls->lookahead.token == TK_EOS);
ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
}

5
llex.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.55 2005/06/06 13:30:25 roberto Exp roberto $ ** $Id: llex.h,v 1.56 2005/12/07 15:33:27 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -71,7 +71,8 @@ LUAI_FUNC void luaX_init (lua_State *L);
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, LUAI_FUNC void luaX_setinput (lua_State *L, LexState *LS, ZIO *z,
TString *source); TString *source);
LUAI_FUNC TString *luaX_newstring (LexState *LS, const char *str, size_t l); LUAI_FUNC TString *luaX_newstring (LexState *LS, const char *str, size_t l);
LUAI_FUNC int luaX_lex (LexState *LS, SemInfo *seminfo); LUAI_FUNC void luaX_next (LexState *ls);
LUAI_FUNC void luaX_lookahead (LexState *ls);
LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token);
LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);
LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.37 2005/10/03 14:02:40 roberto Exp roberto $ ** $Id: lparser.c,v 2.38 2005/10/24 17:38:47 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -54,24 +54,6 @@ static void chunk (LexState *ls);
static void expr (LexState *ls, expdesc *v); static void expr (LexState *ls, expdesc *v);
static void next (LexState *ls) {
ls->lastline = ls->linenumber;
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
ls->t = ls->lookahead; /* use this one */
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else
ls->t.token = luaX_lex(ls, &ls->t.seminfo); /* read next token */
}
static void lookahead (LexState *ls) {
lua_assert(ls->lookahead.token == TK_EOS);
ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
}
static void anchor_token (LexState *ls) { static void anchor_token (LexState *ls) {
if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
TString *ts = ls->t.seminfo.ts; TString *ts = ls->t.seminfo.ts;
@@ -97,7 +79,7 @@ static void errorlimit (FuncState *fs, int limit, const char *what) {
static int testnext (LexState *ls, int c) { static int testnext (LexState *ls, int c) {
if (ls->t.token == c) { if (ls->t.token == c) {
next(ls); luaX_next(ls);
return 1; return 1;
} }
else return 0; else return 0;
@@ -111,7 +93,7 @@ static void check (LexState *ls, int c) {
static void checknext (LexState *ls, int c) { static void checknext (LexState *ls, int c) {
check(ls, c); check(ls, c);
next(ls); luaX_next(ls);
} }
@@ -136,7 +118,7 @@ static TString *str_checkname (LexState *ls) {
TString *ts; TString *ts;
check(ls, TK_NAME); check(ls, TK_NAME);
ts = ls->t.seminfo.ts; ts = ls->t.seminfo.ts;
next(ls); luaX_next(ls);
return ts; return ts;
} }
@@ -404,7 +386,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
luaX_setinput(L, &lexstate, z, luaS_new(L, name)); luaX_setinput(L, &lexstate, z, luaS_new(L, name));
open_func(&lexstate, &funcstate); open_func(&lexstate, &funcstate);
funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
next(&lexstate); /* read first token */ luaX_next(&lexstate); /* read first token */
chunk(&lexstate); chunk(&lexstate);
check(&lexstate, TK_EOS); check(&lexstate, TK_EOS);
close_func(&lexstate); close_func(&lexstate);
@@ -426,7 +408,7 @@ static void field (LexState *ls, expdesc *v) {
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
expdesc key; expdesc key;
luaK_exp2anyreg(fs, v); luaK_exp2anyreg(fs, v);
next(ls); /* skip the dot or colon */ luaX_next(ls); /* skip the dot or colon */
checkname(ls, &key); checkname(ls, &key);
luaK_indexed(fs, v, &key); luaK_indexed(fs, v, &key);
} }
@@ -434,7 +416,7 @@ static void field (LexState *ls, expdesc *v) {
static void yindex (LexState *ls, expdesc *v) { static void yindex (LexState *ls, expdesc *v) {
/* index -> '[' expr ']' */ /* index -> '[' expr ']' */
next(ls); /* skip the '[' */ luaX_next(ls); /* skip the '[' */
expr(ls, v); expr(ls, v);
luaK_exp2val(ls->fs, v); luaK_exp2val(ls->fs, v);
checknext(ls, ']'); checknext(ls, ']');
@@ -530,7 +512,7 @@ static void constructor (LexState *ls, expdesc *t) {
closelistfield(fs, &cc); closelistfield(fs, &cc);
switch(ls->t.token) { switch(ls->t.token) {
case TK_NAME: { /* may be listfields or recfields */ case TK_NAME: { /* may be listfields or recfields */
lookahead(ls); luaX_lookahead(ls);
if (ls->lookahead.token != '=') /* expression? */ if (ls->lookahead.token != '=') /* expression? */
listfield(ls, &cc); listfield(ls, &cc);
else else
@@ -571,7 +553,7 @@ static void parlist (LexState *ls) {
break; break;
} }
case TK_DOTS: { /* param -> `...' */ case TK_DOTS: { /* param -> `...' */
next(ls); luaX_next(ls);
#if defined(LUA_COMPAT_VARARG) #if defined(LUA_COMPAT_VARARG)
/* use `arg' as default name */ /* use `arg' as default name */
new_localvarliteral(ls, "arg", nparams++); new_localvarliteral(ls, "arg", nparams++);
@@ -632,7 +614,7 @@ static void funcargs (LexState *ls, expdesc *f) {
case '(': { /* funcargs -> `(' [ explist1 ] `)' */ case '(': { /* funcargs -> `(' [ explist1 ] `)' */
if (line != ls->lastline) if (line != ls->lastline)
luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
next(ls); luaX_next(ls);
if (ls->t.token == ')') /* arg list is empty? */ if (ls->t.token == ')') /* arg list is empty? */
args.k = VVOID; args.k = VVOID;
else { else {
@@ -648,7 +630,7 @@ static void funcargs (LexState *ls, expdesc *f) {
} }
case TK_STRING: { /* funcargs -> STRING */ case TK_STRING: { /* funcargs -> STRING */
codestring(ls, &args, ls->t.seminfo.ts); codestring(ls, &args, ls->t.seminfo.ts);
next(ls); /* must use `seminfo' before `next' */ luaX_next(ls); /* must use `seminfo' before `next' */
break; break;
} }
default: { default: {
@@ -686,7 +668,7 @@ static void prefixexp (LexState *ls, expdesc *v) {
switch (ls->t.token) { switch (ls->t.token) {
case '(': { case '(': {
int line = ls->linenumber; int line = ls->linenumber;
next(ls); luaX_next(ls);
expr(ls, v); expr(ls, v);
check_match(ls, ')', '(', line); check_match(ls, ')', '(', line);
luaK_dischargevars(ls->fs, v); luaK_dischargevars(ls->fs, v);
@@ -724,7 +706,7 @@ static void primaryexp (LexState *ls, expdesc *v) {
} }
case ':': { /* `:' NAME funcargs */ case ':': { /* `:' NAME funcargs */
expdesc key; expdesc key;
next(ls); luaX_next(ls);
checkname(ls, &key); checkname(ls, &key);
luaK_self(fs, v, &key); luaK_self(fs, v, &key);
funcargs(ls, v); funcargs(ls, v);
@@ -779,7 +761,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
return; return;
} }
case TK_FUNCTION: { case TK_FUNCTION: {
next(ls); luaX_next(ls);
body(ls, v, 0, ls->linenumber); body(ls, v, 0, ls->linenumber);
return; return;
} }
@@ -788,7 +770,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
return; return;
} }
} }
next(ls); luaX_next(ls);
} }
@@ -848,7 +830,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
enterlevel(ls); enterlevel(ls);
uop = getunopr(ls->t.token); uop = getunopr(ls->t.token);
if (uop != OPR_NOUNOPR) { if (uop != OPR_NOUNOPR) {
next(ls); luaX_next(ls);
subexpr(ls, v, UNARY_PRIORITY); subexpr(ls, v, UNARY_PRIORITY);
luaK_prefix(ls->fs, uop, v); luaK_prefix(ls->fs, uop, v);
} }
@@ -858,7 +840,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
while (op != OPR_NOBINOPR && priority[op].left > limit) { while (op != OPR_NOBINOPR && priority[op].left > limit) {
expdesc v2; expdesc v2;
BinOpr nextop; BinOpr nextop;
next(ls); luaX_next(ls);
luaK_infix(ls->fs, op, v); luaK_infix(ls->fs, op, v);
/* read sub-expression with higher priority */ /* read sub-expression with higher priority */
nextop = subexpr(ls, &v2, priority[op].right); nextop = subexpr(ls, &v2, priority[op].right);
@@ -1009,7 +991,7 @@ static void whilestat (LexState *ls, int line) {
int whileinit; int whileinit;
int condexit; int condexit;
BlockCnt bl; BlockCnt bl;
next(ls); /* skip WHILE */ luaX_next(ls); /* skip WHILE */
whileinit = luaK_getlabel(fs); whileinit = luaK_getlabel(fs);
condexit = cond(ls); condexit = cond(ls);
enterblock(fs, &bl, 1); enterblock(fs, &bl, 1);
@@ -1030,7 +1012,7 @@ static void repeatstat (LexState *ls, int line) {
BlockCnt bl1, bl2; BlockCnt bl1, bl2;
enterblock(fs, &bl1, 1); /* loop block */ enterblock(fs, &bl1, 1); /* loop block */
enterblock(fs, &bl2, 0); /* scope block */ enterblock(fs, &bl2, 0); /* scope block */
next(ls); /* skip REPEAT */ luaX_next(ls); /* skip REPEAT */
chunk(ls); chunk(ls);
check_match(ls, TK_UNTIL, TK_REPEAT, line); check_match(ls, TK_UNTIL, TK_REPEAT, line);
condexit = cond(ls); /* read condition (inside scope block) */ condexit = cond(ls); /* read condition (inside scope block) */
@@ -1130,7 +1112,7 @@ static void forstat (LexState *ls, int line) {
TString *varname; TString *varname;
BlockCnt bl; BlockCnt bl;
enterblock(fs, &bl, 1); /* scope for loop and control variables */ enterblock(fs, &bl, 1); /* scope for loop and control variables */
next(ls); /* skip `for' */ luaX_next(ls); /* skip `for' */
varname = str_checkname(ls); /* first variable name */ varname = str_checkname(ls); /* first variable name */
switch (ls->t.token) { switch (ls->t.token) {
case '=': fornum(ls, varname, line); break; case '=': fornum(ls, varname, line); break;
@@ -1145,7 +1127,7 @@ static void forstat (LexState *ls, int line) {
static int test_then_block (LexState *ls) { static int test_then_block (LexState *ls) {
/* test_then_block -> [IF | ELSEIF] cond THEN block */ /* test_then_block -> [IF | ELSEIF] cond THEN block */
int condexit; int condexit;
next(ls); /* skip IF or ELSEIF */ luaX_next(ls); /* skip IF or ELSEIF */
condexit = cond(ls); condexit = cond(ls);
checknext(ls, TK_THEN); checknext(ls, TK_THEN);
block(ls); /* `then' part */ block(ls); /* `then' part */
@@ -1167,7 +1149,7 @@ static void ifstat (LexState *ls, int line) {
if (ls->t.token == TK_ELSE) { if (ls->t.token == TK_ELSE) {
luaK_concat(fs, &escapelist, luaK_jump(fs)); luaK_concat(fs, &escapelist, luaK_jump(fs));
luaK_patchtohere(fs, flist); luaK_patchtohere(fs, flist);
next(ls); /* skip ELSE (after patch, for correct line info) */ luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
block(ls); /* `else' part */ block(ls); /* `else' part */
} }
else else
@@ -1228,7 +1210,7 @@ static void funcstat (LexState *ls, int line) {
/* funcstat -> FUNCTION funcname body */ /* funcstat -> FUNCTION funcname body */
int needself; int needself;
expdesc v, b; expdesc v, b;
next(ls); /* skip FUNCTION */ luaX_next(ls); /* skip FUNCTION */
needself = funcname(ls, &v); needself = funcname(ls, &v);
body(ls, &b, needself, line); body(ls, &b, needself, line);
luaK_storevar(ls->fs, &v, &b); luaK_storevar(ls->fs, &v, &b);
@@ -1255,7 +1237,7 @@ static void retstat (LexState *ls) {
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
expdesc e; expdesc e;
int first, nret; /* registers with returned values */ int first, nret; /* registers with returned values */
next(ls); /* skip RETURN */ luaX_next(ls); /* skip RETURN */
if (block_follow(ls->t.token) || ls->t.token == ';') if (block_follow(ls->t.token) || ls->t.token == ';')
first = nret = 0; /* return no values */ first = nret = 0; /* return no values */
else { else {
@@ -1295,7 +1277,7 @@ static int statement (LexState *ls) {
return 0; return 0;
} }
case TK_DO: { /* stat -> DO block END */ case TK_DO: { /* stat -> DO block END */
next(ls); /* skip DO */ luaX_next(ls); /* skip DO */
block(ls); block(ls);
check_match(ls, TK_END, TK_DO, line); check_match(ls, TK_END, TK_DO, line);
return 0; return 0;
@@ -1313,7 +1295,7 @@ static int statement (LexState *ls) {
return 0; return 0;
} }
case TK_LOCAL: { /* stat -> localstat */ case TK_LOCAL: { /* stat -> localstat */
next(ls); /* skip LOCAL */ luaX_next(ls); /* skip LOCAL */
if (testnext(ls, TK_FUNCTION)) /* local function? */ if (testnext(ls, TK_FUNCTION)) /* local function? */
localfunc(ls); localfunc(ls);
else else
@@ -1325,7 +1307,7 @@ static int statement (LexState *ls) {
return 1; /* must be last statement */ return 1; /* must be last statement */
} }
case TK_BREAK: { /* stat -> breakstat */ case TK_BREAK: { /* stat -> breakstat */
next(ls); /* skip BREAK */ luaX_next(ls); /* skip BREAK */
breakstat(ls); breakstat(ls);
return 1; /* must be last statement */ return 1; /* must be last statement */
} }