better implementation for looh-ahead

This commit is contained in:
Roberto Ierusalimschy 2000-05-25 15:59:59 -03:00
parent a301304612
commit 58fbdc76d5
3 changed files with 15 additions and 20 deletions

4
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 1.59 2000/05/24 13:54:49 roberto Exp roberto $ ** $Id: llex.c,v 1.60 2000/05/24 18:04:17 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -108,7 +108,7 @@ static void firstline (LexState *LS)
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) { void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) {
LS->L = L; LS->L = L;
LS->current = '\n'; LS->current = '\n';
LS->next.token = TK_EOS; /* no next token */ LS->lookahead.token = TK_EOS; /* no look-ahead token */
LS->linenumber = 0; LS->linenumber = 0;
LS->iflevel = 0; LS->iflevel = 0;
LS->ifstate[0].skip = 0; LS->ifstate[0].skip = 0;

8
llex.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.25 2000/05/24 13:54:49 roberto Exp roberto $ ** $Id: llex.h,v 1.26 2000/05/24 18:04:17 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -53,9 +53,9 @@ typedef struct Token {
} Token; } Token;
typedef struct LexState { typedef struct LexState {
int current; /* look ahead character */ int current; /* current character */
Token t; /* look ahead token */ Token t; /* current token */
Token next; /* to `unget' a token */ Token lookahead; /* look ahead token */
struct FuncState *fs; /* `FuncState' is private to the parser */ struct FuncState *fs; /* `FuncState' is private to the parser */
struct lua_State *L; struct lua_State *L;
struct zio *z; /* input stream */ struct zio *z; /* input stream */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 1.90 2000/05/24 18:04:17 roberto Exp roberto $ ** $Id: lparser.c,v 1.91 2000/05/25 18:26:42 roberto Exp roberto $
** LL(1) Parser and code generator for Lua ** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -56,18 +56,18 @@ static void exp1 (LexState *ls);
static void next (LexState *ls) { static void next (LexState *ls) {
if (ls->next.token != TK_EOS) { /* is there an `unget' token? */ if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
ls->t = ls->next; /* use this one */ ls->t = ls->lookahead; /* use this one */
ls->next.token = TK_EOS; /* and discharge it */ ls->lookahead.token = TK_EOS; /* and discharge it */
} }
else else
ls->t.token = luaX_lex(ls); /* read next token */ ls->t.token = luaX_lex(ls); /* read next token */
} }
static void ungettoken (LexState *ls, Token *t) { static void lookahead (LexState *ls) {
ls->next = ls->t; LUA_ASSERT(ls->L, ls->lookahead.token == TK_EOS, "two look-aheads");
ls->t = *t; ls->lookahead.token = luaX_lex(ls);
} }
@ -633,13 +633,8 @@ static void constructor_part (LexState *ls, Constdesc *cd) {
break; break;
} }
case TK_NAME: { /* may be listfields or recfields */ case TK_NAME: { /* may be listfields or recfields */
Token current; lookahead(ls);
int nexttoken; /* to get the look ahead */ if (ls->lookahead.token != '=') /* expression? */
current = ls->t; /* save for `unget' */
next(ls);
nexttoken = ls->t.token;
ungettoken(ls, &current);
if (nexttoken != '=') /* expression? */
goto case_default; goto case_default;
/* else go through to recfields */ /* else go through to recfields */
} }