From 87024e257dbbe704bfedf27b1f5d144688ddc9d2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Dec 2005 13:43:05 -0200 Subject: details --- llex.c | 22 ++++++++++++++++--- llex.h | 5 +++-- lparser.c | 72 ++++++++++++++++++++++++--------------------------------------- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/llex.c b/llex.c index 48dbcd5b..86d9ee60 100644 --- a/llex.c +++ b/llex.c @@ -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 ** 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); for (;;) { 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); +} + diff --git a/llex.h b/llex.h index 2a7a3664..4e1ac798 100644 --- a/llex.h +++ b/llex.h @@ -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 ** 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, TString *source); 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_syntaxerror (LexState *ls, const char *s); LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); diff --git a/lparser.c b/lparser.c index f9034e0e..b23ebd6f 100644 --- a/lparser.c +++ b/lparser.c @@ -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 ** See Copyright Notice in lua.h */ @@ -54,24 +54,6 @@ static void chunk (LexState *ls); 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) { if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { 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) { if (ls->t.token == c) { - next(ls); + luaX_next(ls); return 1; } else return 0; @@ -111,7 +93,7 @@ static void check (LexState *ls, int c) { static void checknext (LexState *ls, int c) { check(ls, c); - next(ls); + luaX_next(ls); } @@ -136,7 +118,7 @@ static TString *str_checkname (LexState *ls) { TString *ts; check(ls, TK_NAME); ts = ls->t.seminfo.ts; - next(ls); + luaX_next(ls); 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)); open_func(&lexstate, &funcstate); funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ - next(&lexstate); /* read first token */ + luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); close_func(&lexstate); @@ -426,7 +408,7 @@ static void field (LexState *ls, expdesc *v) { FuncState *fs = ls->fs; expdesc key; luaK_exp2anyreg(fs, v); - next(ls); /* skip the dot or colon */ + luaX_next(ls); /* skip the dot or colon */ checkname(ls, &key); luaK_indexed(fs, v, &key); } @@ -434,7 +416,7 @@ static void field (LexState *ls, expdesc *v) { static void yindex (LexState *ls, expdesc *v) { /* index -> '[' expr ']' */ - next(ls); /* skip the '[' */ + luaX_next(ls); /* skip the '[' */ expr(ls, v); luaK_exp2val(ls->fs, v); checknext(ls, ']'); @@ -530,7 +512,7 @@ static void constructor (LexState *ls, expdesc *t) { closelistfield(fs, &cc); switch(ls->t.token) { case TK_NAME: { /* may be listfields or recfields */ - lookahead(ls); + luaX_lookahead(ls); if (ls->lookahead.token != '=') /* expression? */ listfield(ls, &cc); else @@ -571,7 +553,7 @@ static void parlist (LexState *ls) { break; } case TK_DOTS: { /* param -> `...' */ - next(ls); + luaX_next(ls); #if defined(LUA_COMPAT_VARARG) /* use `arg' as default name */ new_localvarliteral(ls, "arg", nparams++); @@ -632,7 +614,7 @@ static void funcargs (LexState *ls, expdesc *f) { case '(': { /* funcargs -> `(' [ explist1 ] `)' */ if (line != ls->lastline) luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); - next(ls); + luaX_next(ls); if (ls->t.token == ')') /* arg list is empty? */ args.k = VVOID; else { @@ -648,7 +630,7 @@ static void funcargs (LexState *ls, expdesc *f) { } case TK_STRING: { /* funcargs -> STRING */ codestring(ls, &args, ls->t.seminfo.ts); - next(ls); /* must use `seminfo' before `next' */ + luaX_next(ls); /* must use `seminfo' before `next' */ break; } default: { @@ -686,7 +668,7 @@ static void prefixexp (LexState *ls, expdesc *v) { switch (ls->t.token) { case '(': { int line = ls->linenumber; - next(ls); + luaX_next(ls); expr(ls, v); check_match(ls, ')', '(', line); luaK_dischargevars(ls->fs, v); @@ -724,7 +706,7 @@ static void primaryexp (LexState *ls, expdesc *v) { } case ':': { /* `:' NAME funcargs */ expdesc key; - next(ls); + luaX_next(ls); checkname(ls, &key); luaK_self(fs, v, &key); funcargs(ls, v); @@ -779,7 +761,7 @@ static void simpleexp (LexState *ls, expdesc *v) { return; } case TK_FUNCTION: { - next(ls); + luaX_next(ls); body(ls, v, 0, ls->linenumber); return; } @@ -788,7 +770,7 @@ static void simpleexp (LexState *ls, expdesc *v) { return; } } - next(ls); + luaX_next(ls); } @@ -848,7 +830,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { enterlevel(ls); uop = getunopr(ls->t.token); if (uop != OPR_NOUNOPR) { - next(ls); + luaX_next(ls); subexpr(ls, v, UNARY_PRIORITY); 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) { expdesc v2; BinOpr nextop; - next(ls); + luaX_next(ls); luaK_infix(ls->fs, op, v); /* read sub-expression with higher priority */ nextop = subexpr(ls, &v2, priority[op].right); @@ -1009,7 +991,7 @@ static void whilestat (LexState *ls, int line) { int whileinit; int condexit; BlockCnt bl; - next(ls); /* skip WHILE */ + luaX_next(ls); /* skip WHILE */ whileinit = luaK_getlabel(fs); condexit = cond(ls); enterblock(fs, &bl, 1); @@ -1030,7 +1012,7 @@ static void repeatstat (LexState *ls, int line) { BlockCnt bl1, bl2; enterblock(fs, &bl1, 1); /* loop block */ enterblock(fs, &bl2, 0); /* scope block */ - next(ls); /* skip REPEAT */ + luaX_next(ls); /* skip REPEAT */ chunk(ls); check_match(ls, TK_UNTIL, TK_REPEAT, line); condexit = cond(ls); /* read condition (inside scope block) */ @@ -1130,7 +1112,7 @@ static void forstat (LexState *ls, int line) { TString *varname; BlockCnt bl; 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 */ switch (ls->t.token) { case '=': fornum(ls, varname, line); break; @@ -1145,7 +1127,7 @@ static void forstat (LexState *ls, int line) { static int test_then_block (LexState *ls) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ int condexit; - next(ls); /* skip IF or ELSEIF */ + luaX_next(ls); /* skip IF or ELSEIF */ condexit = cond(ls); checknext(ls, TK_THEN); block(ls); /* `then' part */ @@ -1167,7 +1149,7 @@ static void ifstat (LexState *ls, int line) { if (ls->t.token == TK_ELSE) { luaK_concat(fs, &escapelist, luaK_jump(fs)); 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 */ } else @@ -1228,7 +1210,7 @@ static void funcstat (LexState *ls, int line) { /* funcstat -> FUNCTION funcname body */ int needself; expdesc v, b; - next(ls); /* skip FUNCTION */ + luaX_next(ls); /* skip FUNCTION */ needself = funcname(ls, &v); body(ls, &b, needself, line); luaK_storevar(ls->fs, &v, &b); @@ -1255,7 +1237,7 @@ static void retstat (LexState *ls) { FuncState *fs = ls->fs; expdesc e; 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 == ';') first = nret = 0; /* return no values */ else { @@ -1295,7 +1277,7 @@ static int statement (LexState *ls) { return 0; } case TK_DO: { /* stat -> DO block END */ - next(ls); /* skip DO */ + luaX_next(ls); /* skip DO */ block(ls); check_match(ls, TK_END, TK_DO, line); return 0; @@ -1313,7 +1295,7 @@ static int statement (LexState *ls) { return 0; } case TK_LOCAL: { /* stat -> localstat */ - next(ls); /* skip LOCAL */ + luaX_next(ls); /* skip LOCAL */ if (testnext(ls, TK_FUNCTION)) /* local function? */ localfunc(ls); else @@ -1325,7 +1307,7 @@ static int statement (LexState *ls) { return 1; /* must be last statement */ } case TK_BREAK: { /* stat -> breakstat */ - next(ls); /* skip BREAK */ + luaX_next(ls); /* skip BREAK */ breakstat(ls); return 1; /* must be last statement */ } -- cgit v1.2.3-55-g6feb