diff options
Diffstat (limited to 'lparser.c')
| -rw-r--r-- | lparser.c | 37 |
1 files changed, 21 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 2.9 2004/12/03 20:44:19 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.10 2004/12/03 20:50:25 roberto Exp roberto $ |
| 3 | ** Lua Parser | 3 | ** Lua Parser |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -109,10 +109,15 @@ static int testnext (LexState *ls, int c) { | |||
| 109 | 109 | ||
| 110 | 110 | ||
| 111 | static void check (LexState *ls, int c) { | 111 | static void check (LexState *ls, int c) { |
| 112 | if (!testnext(ls, c)) | 112 | if (ls->t.token != c) |
| 113 | error_expected(ls, c); | 113 | error_expected(ls, c); |
| 114 | } | 114 | } |
| 115 | 115 | ||
| 116 | static void checknext (LexState *ls, int c) { | ||
| 117 | check(ls, c); | ||
| 118 | next(ls); | ||
| 119 | } | ||
| 120 | |||
| 116 | 121 | ||
| 117 | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } | 122 | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } |
| 118 | 123 | ||
| @@ -133,7 +138,7 @@ static void check_match (LexState *ls, int what, int who, int where) { | |||
| 133 | 138 | ||
| 134 | static TString *str_checkname (LexState *ls) { | 139 | static TString *str_checkname (LexState *ls) { |
| 135 | TString *ts; | 140 | TString *ts; |
| 136 | if (ls->t.token != TK_NAME) error_expected(ls, TK_NAME); | 141 | check(ls, TK_NAME); |
| 137 | ts = ls->t.seminfo.ts; | 142 | ts = ls->t.seminfo.ts; |
| 138 | next(ls); | 143 | next(ls); |
| 139 | return ts; | 144 | return ts; |
| @@ -396,7 +401,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { | |||
| 396 | funcstate.f->is_vararg = NEWSTYLEVARARG; | 401 | funcstate.f->is_vararg = NEWSTYLEVARARG; |
| 397 | next(&lexstate); /* read first token */ | 402 | next(&lexstate); /* read first token */ |
| 398 | chunk(&lexstate); | 403 | chunk(&lexstate); |
| 399 | check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected"); | 404 | check(&lexstate, TK_EOS); |
| 400 | close_func(&lexstate); | 405 | close_func(&lexstate); |
| 401 | lua_assert(funcstate.prev == NULL); | 406 | lua_assert(funcstate.prev == NULL); |
| 402 | lua_assert(funcstate.f->nups == 0); | 407 | lua_assert(funcstate.f->nups == 0); |
| @@ -428,7 +433,7 @@ static void yindex (LexState *ls, expdesc *v) { | |||
| 428 | next(ls); /* skip the '[' */ | 433 | next(ls); /* skip the '[' */ |
| 429 | expr(ls, v); | 434 | expr(ls, v); |
| 430 | luaK_exp2val(ls->fs, v); | 435 | luaK_exp2val(ls->fs, v); |
| 431 | check(ls, ']'); | 436 | checknext(ls, ']'); |
| 432 | } | 437 | } |
| 433 | 438 | ||
| 434 | 439 | ||
| @@ -460,7 +465,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { | |||
| 460 | } | 465 | } |
| 461 | else /* ls->t.token == '[' */ | 466 | else /* ls->t.token == '[' */ |
| 462 | yindex(ls, &key); | 467 | yindex(ls, &key); |
| 463 | check(ls, '='); | 468 | checknext(ls, '='); |
| 464 | luaK_exp2RK(fs, &key); | 469 | luaK_exp2RK(fs, &key); |
| 465 | expr(ls, &val); | 470 | expr(ls, &val); |
| 466 | luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key), | 471 | luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key), |
| @@ -514,7 +519,7 @@ static void constructor (LexState *ls, expdesc *t) { | |||
| 514 | init_exp(t, VRELOCABLE, pc); | 519 | init_exp(t, VRELOCABLE, pc); |
| 515 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ | 520 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
| 516 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ | 521 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ |
| 517 | check(ls, '{'); | 522 | checknext(ls, '{'); |
| 518 | do { | 523 | do { |
| 519 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); | 524 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
| 520 | testnext(ls, ';'); /* compatibility only */ | 525 | testnext(ls, ';'); /* compatibility only */ |
| @@ -584,13 +589,13 @@ static void body (LexState *ls, expdesc *e, int needself, int line) { | |||
| 584 | FuncState new_fs; | 589 | FuncState new_fs; |
| 585 | open_func(ls, &new_fs); | 590 | open_func(ls, &new_fs); |
| 586 | new_fs.f->lineDefined = line; | 591 | new_fs.f->lineDefined = line; |
| 587 | check(ls, '('); | 592 | checknext(ls, '('); |
| 588 | if (needself) { | 593 | if (needself) { |
| 589 | new_localvarliteral(ls, "self", 0); | 594 | new_localvarliteral(ls, "self", 0); |
| 590 | adjustlocalvars(ls, 1); | 595 | adjustlocalvars(ls, 1); |
| 591 | } | 596 | } |
| 592 | parlist(ls); | 597 | parlist(ls); |
| 593 | check(ls, ')'); | 598 | checknext(ls, ')'); |
| 594 | chunk(ls); | 599 | chunk(ls); |
| 595 | check_match(ls, TK_END, TK_FUNCTION, line); | 600 | check_match(ls, TK_END, TK_FUNCTION, line); |
| 596 | close_func(ls); | 601 | close_func(ls); |
| @@ -944,7 +949,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { | |||
| 944 | } | 949 | } |
| 945 | else { /* assignment -> `=' explist1 */ | 950 | else { /* assignment -> `=' explist1 */ |
| 946 | int nexps; | 951 | int nexps; |
| 947 | check(ls, '='); | 952 | checknext(ls, '='); |
| 948 | nexps = explist1(ls, &e); | 953 | nexps = explist1(ls, &e); |
| 949 | if (nexps != nvars) { | 954 | if (nexps != nvars) { |
| 950 | adjust_assign(ls, nvars, nexps, &e); | 955 | adjust_assign(ls, nvars, nexps, &e); |
| @@ -1011,7 +1016,7 @@ static void whilestat (LexState *ls, int line) { | |||
| 1011 | codeexp[i] = fs->f->code[expinit + i]; | 1016 | codeexp[i] = fs->f->code[expinit + i]; |
| 1012 | fs->pc = expinit; /* remove `exp' code */ | 1017 | fs->pc = expinit; /* remove `exp' code */ |
| 1013 | enterblock(fs, &bl, 1); | 1018 | enterblock(fs, &bl, 1); |
| 1014 | check(ls, TK_DO); | 1019 | checknext(ls, TK_DO); |
| 1015 | blockinit = luaK_getlabel(fs); | 1020 | blockinit = luaK_getlabel(fs); |
| 1016 | block(ls); | 1021 | block(ls); |
| 1017 | luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */ | 1022 | luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */ |
| @@ -1059,7 +1064,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { | |||
| 1059 | FuncState *fs = ls->fs; | 1064 | FuncState *fs = ls->fs; |
| 1060 | int prep, endfor; | 1065 | int prep, endfor; |
| 1061 | adjustlocalvars(ls, 3); /* control variables */ | 1066 | adjustlocalvars(ls, 3); /* control variables */ |
| 1062 | check(ls, TK_DO); | 1067 | checknext(ls, TK_DO); |
| 1063 | prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP); | 1068 | prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP); |
| 1064 | enterblock(fs, &bl, 0); /* scope for declared variables */ | 1069 | enterblock(fs, &bl, 0); /* scope for declared variables */ |
| 1065 | adjustlocalvars(ls, nvars); | 1070 | adjustlocalvars(ls, nvars); |
| @@ -1082,9 +1087,9 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
| 1082 | new_localvarliteral(ls, "(for limit)", 1); | 1087 | new_localvarliteral(ls, "(for limit)", 1); |
| 1083 | new_localvarliteral(ls, "(for step)", 2); | 1088 | new_localvarliteral(ls, "(for step)", 2); |
| 1084 | new_localvar(ls, varname, 3); | 1089 | new_localvar(ls, varname, 3); |
| 1085 | check(ls, '='); | 1090 | checknext(ls, '='); |
| 1086 | exp1(ls); /* initial value */ | 1091 | exp1(ls); /* initial value */ |
| 1087 | check(ls, ','); | 1092 | checknext(ls, ','); |
| 1088 | exp1(ls); /* limit */ | 1093 | exp1(ls); /* limit */ |
| 1089 | if (testnext(ls, ',')) | 1094 | if (testnext(ls, ',')) |
| 1090 | exp1(ls); /* optional step */ | 1095 | exp1(ls); /* optional step */ |
| @@ -1111,7 +1116,7 @@ static void forlist (LexState *ls, TString *indexname) { | |||
| 1111 | new_localvar(ls, indexname, nvars++); | 1116 | new_localvar(ls, indexname, nvars++); |
| 1112 | while (testnext(ls, ',')) | 1117 | while (testnext(ls, ',')) |
| 1113 | new_localvar(ls, str_checkname(ls), nvars++); | 1118 | new_localvar(ls, str_checkname(ls), nvars++); |
| 1114 | check(ls, TK_IN); | 1119 | checknext(ls, TK_IN); |
| 1115 | line = ls->linenumber; | 1120 | line = ls->linenumber; |
| 1116 | adjust_assign(ls, 3, explist1(ls, &e), &e); | 1121 | adjust_assign(ls, 3, explist1(ls, &e), &e); |
| 1117 | luaK_checkstack(fs, 3); /* extra space to call generator */ | 1122 | luaK_checkstack(fs, 3); /* extra space to call generator */ |
| @@ -1142,7 +1147,7 @@ static int test_then_block (LexState *ls) { | |||
| 1142 | int flist; | 1147 | int flist; |
| 1143 | next(ls); /* skip IF or ELSEIF */ | 1148 | next(ls); /* skip IF or ELSEIF */ |
| 1144 | flist = cond(ls); | 1149 | flist = cond(ls); |
| 1145 | check(ls, TK_THEN); | 1150 | checknext(ls, TK_THEN); |
| 1146 | block(ls); /* `then' part */ | 1151 | block(ls); /* `then' part */ |
| 1147 | return flist; | 1152 | return flist; |
| 1148 | } | 1153 | } |
