diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-25 15:59:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-25 15:59:59 -0300 |
commit | 58fbdc76d558c8039a966852ca07802f0a413b11 (patch) | |
tree | f0de4a6d4ed8e7dc88d476b9f5348e353f05f6eb | |
parent | a3013046121b186bf4b5e54b247d3209fd76de5a (diff) | |
download | lua-58fbdc76d558c8039a966852ca07802f0a413b11.tar.gz lua-58fbdc76d558c8039a966852ca07802f0a413b11.tar.bz2 lua-58fbdc76d558c8039a966852ca07802f0a413b11.zip |
better implementation for looh-ahead
-rw-r--r-- | llex.c | 4 | ||||
-rw-r--r-- | llex.h | 8 | ||||
-rw-r--r-- | lparser.c | 23 |
3 files changed, 15 insertions, 20 deletions
@@ -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) | |||
108 | void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) { | 108 | void 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; |
@@ -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 | ||
55 | typedef struct LexState { | 55 | typedef 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 */ |
@@ -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 | ||
58 | static void next (LexState *ls) { | 58 | static 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 | ||
68 | static void ungettoken (LexState *ls, Token *t) { | 68 | static 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, ¤t); | ||
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 | } |