diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 10:03:05 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 10:03:05 -0200 |
commit | b8fed93215a23a3f443c5b0126f0de1725771b44 (patch) | |
tree | 1d7d29388b8a20fb7d0920d94b02a8040612314d /lparser.c | |
parent | 5e76a4fd313a8690d300085c4e8fcb9dca50c01a (diff) | |
download | lua-github-b8fed93215a23a3f443c5b0126f0de1725771b44.tar.gz |
New syntax for to-be-closed variables
The new syntax is <local *toclose x = f()>. The mark '*' allows other
attributes to be added later without the need of new keywords; it
also allows better error messages. The API function was also renamed
('lua_tobeclosed' -> 'lua_toclose').
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -1546,16 +1546,15 @@ static void localfunc (LexState *ls) { } -static void commonlocalstat (LexState *ls, TString *firstvar) { +static void commonlocalstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ - int nvars = 1; + int nvars = 0; int nexps; expdesc e; - new_localvar(ls, firstvar); - while (testnext(ls, ',')) { + do { new_localvar(ls, str_checkname(ls)); nvars++; - } + } while (testnext(ls, ',')); if (testnext(ls, '=')) nexps = explist(ls, &e); else { @@ -1567,8 +1566,12 @@ static void commonlocalstat (LexState *ls, TString *firstvar) { } -static void scopedlocalstat (LexState *ls) { +static void tocloselocalstat (LexState *ls) { FuncState *fs = ls->fs; + TString *attr = str_checkname(ls); + if (strcmp(getstr(attr), "toclose") != 0) + luaK_semerror(ls, + luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); new_localvar(ls, str_checkname(ls)); checknext(ls, '='); exp1(ls, 0); @@ -1580,13 +1583,11 @@ static void scopedlocalstat (LexState *ls) { static void localstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] - | LOCAL SCOPED NAME '=' exp */ - TString *firstvar = str_checkname(ls); - if (ls->t.token == TK_NAME && - eqshrstr(firstvar, luaS_newliteral(ls->L, "scoped"))) - scopedlocalstat(ls); + | LOCAL *toclose NAME '=' exp */ + if (testnext(ls, '*')) + tocloselocalstat(ls); else - commonlocalstat(ls, firstvar); + commonlocalstat(ls); } |