aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-05-25 15:59:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-05-25 15:59:59 -0300
commit58fbdc76d558c8039a966852ca07802f0a413b11 (patch)
treef0de4a6d4ed8e7dc88d476b9f5348e353f05f6eb
parenta3013046121b186bf4b5e54b247d3209fd76de5a (diff)
downloadlua-58fbdc76d558c8039a966852ca07802f0a413b11.tar.gz
lua-58fbdc76d558c8039a966852ca07802f0a413b11.tar.bz2
lua-58fbdc76d558c8039a966852ca07802f0a413b11.zip
better implementation for looh-ahead
-rw-r--r--llex.c4
-rw-r--r--llex.h8
-rw-r--r--lparser.c23
3 files changed, 15 insertions, 20 deletions
diff --git a/llex.c b/llex.c
index e5275682..3c446e23 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.59 2000/05/24 13:54:49 roberto Exp roberto $ 2** $Id: llex.c,v 1.60 2000/05/24 18:04:17 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -108,7 +108,7 @@ static void firstline (LexState *LS)
108void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) { 108void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) {
109 LS->L = L; 109 LS->L = L;
110 LS->current = '\n'; 110 LS->current = '\n';
111 LS->next.token = TK_EOS; /* no next token */ 111 LS->lookahead.token = TK_EOS; /* no look-ahead token */
112 LS->linenumber = 0; 112 LS->linenumber = 0;
113 LS->iflevel = 0; 113 LS->iflevel = 0;
114 LS->ifstate[0].skip = 0; 114 LS->ifstate[0].skip = 0;
diff --git a/llex.h b/llex.h
index 7235bdc8..f6029d72 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.25 2000/05/24 13:54:49 roberto Exp roberto $ 2** $Id: llex.h,v 1.26 2000/05/24 18:04:17 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -53,9 +53,9 @@ typedef struct Token {
53} Token; 53} Token;
54 54
55typedef struct LexState { 55typedef struct LexState {
56 int current; /* look ahead character */ 56 int current; /* current character */
57 Token t; /* look ahead token */ 57 Token t; /* current token */
58 Token next; /* to `unget' a token */ 58 Token lookahead; /* look ahead token */
59 struct FuncState *fs; /* `FuncState' is private to the parser */ 59 struct FuncState *fs; /* `FuncState' is private to the parser */
60 struct lua_State *L; 60 struct lua_State *L;
61 struct zio *z; /* input stream */ 61 struct zio *z; /* input stream */
diff --git a/lparser.c b/lparser.c
index b39cafd0..5fd467b1 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.90 2000/05/24 18:04:17 roberto Exp roberto $ 2** $Id: lparser.c,v 1.91 2000/05/25 18:26:42 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -56,18 +56,18 @@ static void exp1 (LexState *ls);
56 56
57 57
58static void next (LexState *ls) { 58static void next (LexState *ls) {
59 if (ls->next.token != TK_EOS) { /* is there an `unget' token? */ 59 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
60 ls->t = ls->next; /* use this one */ 60 ls->t = ls->lookahead; /* use this one */
61 ls->next.token = TK_EOS; /* and discharge it */ 61 ls->lookahead.token = TK_EOS; /* and discharge it */
62 } 62 }
63 else 63 else
64 ls->t.token = luaX_lex(ls); /* read next token */ 64 ls->t.token = luaX_lex(ls); /* read next token */
65} 65}
66 66
67 67
68static void ungettoken (LexState *ls, Token *t) { 68static void lookahead (LexState *ls) {
69 ls->next = ls->t; 69 LUA_ASSERT(ls->L, ls->lookahead.token == TK_EOS, "two look-aheads");
70 ls->t = *t; 70 ls->lookahead.token = luaX_lex(ls);
71} 71}
72 72
73 73
@@ -633,13 +633,8 @@ static void constructor_part (LexState *ls, Constdesc *cd) {
633 break; 633 break;
634 } 634 }
635 case TK_NAME: { /* may be listfields or recfields */ 635 case TK_NAME: { /* may be listfields or recfields */
636 Token current; 636 lookahead(ls);
637 int nexttoken; /* to get the look ahead */ 637 if (ls->lookahead.token != '=') /* expression? */
638 current = ls->t; /* save for `unget' */
639 next(ls);
640 nexttoken = ls->t.token;
641 ungettoken(ls, &current);
642 if (nexttoken != '=') /* expression? */
643 goto case_default; 638 goto case_default;
644 /* else go through to recfields */ 639 /* else go through to recfields */
645 } 640 }