aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-12-07 13:43:05 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-12-07 13:43:05 -0200
commit87024e257dbbe704bfedf27b1f5d144688ddc9d2 (patch)
tree920a733d2e3fb536f96ad5a1c71d5774829bf5dc
parent6cd461633d0da9003e03a0e198f4055e37a137a9 (diff)
downloadlua-87024e257dbbe704bfedf27b1f5d144688ddc9d2.tar.gz
lua-87024e257dbbe704bfedf27b1f5d144688ddc9d2.tar.bz2
lua-87024e257dbbe704bfedf27b1f5d144688ddc9d2.zip
details
-rw-r--r--llex.c22
-rw-r--r--llex.h5
-rw-r--r--lparser.c72
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 @@
1/* 1/*
2** $Id: llex.c,v 2.13 2005/11/08 19:45:14 roberto Exp roberto $ 2** $Id: llex.c,v 2.14 2005/12/07 15:32:52 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*/
@@ -319,7 +319,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
319} 319}
320 320
321 321
322int luaX_lex (LexState *ls, SemInfo *seminfo) { 322static int llex (LexState *ls, SemInfo *seminfo) {
323 luaZ_resetbuffer(ls->buff); 323 luaZ_resetbuffer(ls->buff);
324 for (;;) { 324 for (;;) {
325 switch (ls->current) { 325 switch (ls->current) {
@@ -432,4 +432,20 @@ int luaX_lex (LexState *ls, SemInfo *seminfo) {
432 } 432 }
433} 433}
434 434
435#undef next 435
436void luaX_next (LexState *ls) {
437 ls->lastline = ls->linenumber;
438 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
439 ls->t = ls->lookahead; /* use this one */
440 ls->lookahead.token = TK_EOS; /* and discharge it */
441 }
442 else
443 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
444}
445
446
447void luaX_lookahead (LexState *ls) {
448 lua_assert(ls->lookahead.token == TK_EOS);
449 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
450}
451
diff --git a/llex.h b/llex.h
index 2a7a3664..4e1ac798 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.55 2005/06/06 13:30:25 roberto Exp roberto $ 2** $Id: llex.h,v 1.56 2005/12/07 15:33:27 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*/
@@ -71,7 +71,8 @@ LUAI_FUNC void luaX_init (lua_State *L);
71LUAI_FUNC void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, 71LUAI_FUNC void luaX_setinput (lua_State *L, LexState *LS, ZIO *z,
72 TString *source); 72 TString *source);
73LUAI_FUNC TString *luaX_newstring (LexState *LS, const char *str, size_t l); 73LUAI_FUNC TString *luaX_newstring (LexState *LS, const char *str, size_t l);
74LUAI_FUNC int luaX_lex (LexState *LS, SemInfo *seminfo); 74LUAI_FUNC void luaX_next (LexState *ls);
75LUAI_FUNC void luaX_lookahead (LexState *ls);
75LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 76LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token);
76LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 77LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);
77LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 78LUAI_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 @@
1/* 1/*
2** $Id: lparser.c,v 2.37 2005/10/03 14:02:40 roberto Exp roberto $ 2** $Id: lparser.c,v 2.38 2005/10/24 17:38:47 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*/
@@ -54,24 +54,6 @@ static void chunk (LexState *ls);
54static void expr (LexState *ls, expdesc *v); 54static void expr (LexState *ls, expdesc *v);
55 55
56 56
57
58static void next (LexState *ls) {
59 ls->lastline = ls->linenumber;
60 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
61 ls->t = ls->lookahead; /* use this one */
62 ls->lookahead.token = TK_EOS; /* and discharge it */
63 }
64 else
65 ls->t.token = luaX_lex(ls, &ls->t.seminfo); /* read next token */
66}
67
68
69static void lookahead (LexState *ls) {
70 lua_assert(ls->lookahead.token == TK_EOS);
71 ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
72}
73
74
75static void anchor_token (LexState *ls) { 57static void anchor_token (LexState *ls) {
76 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { 58 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
77 TString *ts = ls->t.seminfo.ts; 59 TString *ts = ls->t.seminfo.ts;
@@ -97,7 +79,7 @@ static void errorlimit (FuncState *fs, int limit, const char *what) {
97 79
98static int testnext (LexState *ls, int c) { 80static int testnext (LexState *ls, int c) {
99 if (ls->t.token == c) { 81 if (ls->t.token == c) {
100 next(ls); 82 luaX_next(ls);
101 return 1; 83 return 1;
102 } 84 }
103 else return 0; 85 else return 0;
@@ -111,7 +93,7 @@ static void check (LexState *ls, int c) {
111 93
112static void checknext (LexState *ls, int c) { 94static void checknext (LexState *ls, int c) {
113 check(ls, c); 95 check(ls, c);
114 next(ls); 96 luaX_next(ls);
115} 97}
116 98
117 99
@@ -136,7 +118,7 @@ static TString *str_checkname (LexState *ls) {
136 TString *ts; 118 TString *ts;
137 check(ls, TK_NAME); 119 check(ls, TK_NAME);
138 ts = ls->t.seminfo.ts; 120 ts = ls->t.seminfo.ts;
139 next(ls); 121 luaX_next(ls);
140 return ts; 122 return ts;
141} 123}
142 124
@@ -404,7 +386,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
404 luaX_setinput(L, &lexstate, z, luaS_new(L, name)); 386 luaX_setinput(L, &lexstate, z, luaS_new(L, name));
405 open_func(&lexstate, &funcstate); 387 open_func(&lexstate, &funcstate);
406 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ 388 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
407 next(&lexstate); /* read first token */ 389 luaX_next(&lexstate); /* read first token */
408 chunk(&lexstate); 390 chunk(&lexstate);
409 check(&lexstate, TK_EOS); 391 check(&lexstate, TK_EOS);
410 close_func(&lexstate); 392 close_func(&lexstate);
@@ -426,7 +408,7 @@ static void field (LexState *ls, expdesc *v) {
426 FuncState *fs = ls->fs; 408 FuncState *fs = ls->fs;
427 expdesc key; 409 expdesc key;
428 luaK_exp2anyreg(fs, v); 410 luaK_exp2anyreg(fs, v);
429 next(ls); /* skip the dot or colon */ 411 luaX_next(ls); /* skip the dot or colon */
430 checkname(ls, &key); 412 checkname(ls, &key);
431 luaK_indexed(fs, v, &key); 413 luaK_indexed(fs, v, &key);
432} 414}
@@ -434,7 +416,7 @@ static void field (LexState *ls, expdesc *v) {
434 416
435static void yindex (LexState *ls, expdesc *v) { 417static void yindex (LexState *ls, expdesc *v) {
436 /* index -> '[' expr ']' */ 418 /* index -> '[' expr ']' */
437 next(ls); /* skip the '[' */ 419 luaX_next(ls); /* skip the '[' */
438 expr(ls, v); 420 expr(ls, v);
439 luaK_exp2val(ls->fs, v); 421 luaK_exp2val(ls->fs, v);
440 checknext(ls, ']'); 422 checknext(ls, ']');
@@ -530,7 +512,7 @@ static void constructor (LexState *ls, expdesc *t) {
530 closelistfield(fs, &cc); 512 closelistfield(fs, &cc);
531 switch(ls->t.token) { 513 switch(ls->t.token) {
532 case TK_NAME: { /* may be listfields or recfields */ 514 case TK_NAME: { /* may be listfields or recfields */
533 lookahead(ls); 515 luaX_lookahead(ls);
534 if (ls->lookahead.token != '=') /* expression? */ 516 if (ls->lookahead.token != '=') /* expression? */
535 listfield(ls, &cc); 517 listfield(ls, &cc);
536 else 518 else
@@ -571,7 +553,7 @@ static void parlist (LexState *ls) {
571 break; 553 break;
572 } 554 }
573 case TK_DOTS: { /* param -> `...' */ 555 case TK_DOTS: { /* param -> `...' */
574 next(ls); 556 luaX_next(ls);
575#if defined(LUA_COMPAT_VARARG) 557#if defined(LUA_COMPAT_VARARG)
576 /* use `arg' as default name */ 558 /* use `arg' as default name */
577 new_localvarliteral(ls, "arg", nparams++); 559 new_localvarliteral(ls, "arg", nparams++);
@@ -632,7 +614,7 @@ static void funcargs (LexState *ls, expdesc *f) {
632 case '(': { /* funcargs -> `(' [ explist1 ] `)' */ 614 case '(': { /* funcargs -> `(' [ explist1 ] `)' */
633 if (line != ls->lastline) 615 if (line != ls->lastline)
634 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); 616 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
635 next(ls); 617 luaX_next(ls);
636 if (ls->t.token == ')') /* arg list is empty? */ 618 if (ls->t.token == ')') /* arg list is empty? */
637 args.k = VVOID; 619 args.k = VVOID;
638 else { 620 else {
@@ -648,7 +630,7 @@ static void funcargs (LexState *ls, expdesc *f) {
648 } 630 }
649 case TK_STRING: { /* funcargs -> STRING */ 631 case TK_STRING: { /* funcargs -> STRING */
650 codestring(ls, &args, ls->t.seminfo.ts); 632 codestring(ls, &args, ls->t.seminfo.ts);
651 next(ls); /* must use `seminfo' before `next' */ 633 luaX_next(ls); /* must use `seminfo' before `next' */
652 break; 634 break;
653 } 635 }
654 default: { 636 default: {
@@ -686,7 +668,7 @@ static void prefixexp (LexState *ls, expdesc *v) {
686 switch (ls->t.token) { 668 switch (ls->t.token) {
687 case '(': { 669 case '(': {
688 int line = ls->linenumber; 670 int line = ls->linenumber;
689 next(ls); 671 luaX_next(ls);
690 expr(ls, v); 672 expr(ls, v);
691 check_match(ls, ')', '(', line); 673 check_match(ls, ')', '(', line);
692 luaK_dischargevars(ls->fs, v); 674 luaK_dischargevars(ls->fs, v);
@@ -724,7 +706,7 @@ static void primaryexp (LexState *ls, expdesc *v) {
724 } 706 }
725 case ':': { /* `:' NAME funcargs */ 707 case ':': { /* `:' NAME funcargs */
726 expdesc key; 708 expdesc key;
727 next(ls); 709 luaX_next(ls);
728 checkname(ls, &key); 710 checkname(ls, &key);
729 luaK_self(fs, v, &key); 711 luaK_self(fs, v, &key);
730 funcargs(ls, v); 712 funcargs(ls, v);
@@ -779,7 +761,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
779 return; 761 return;
780 } 762 }
781 case TK_FUNCTION: { 763 case TK_FUNCTION: {
782 next(ls); 764 luaX_next(ls);
783 body(ls, v, 0, ls->linenumber); 765 body(ls, v, 0, ls->linenumber);
784 return; 766 return;
785 } 767 }
@@ -788,7 +770,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
788 return; 770 return;
789 } 771 }
790 } 772 }
791 next(ls); 773 luaX_next(ls);
792} 774}
793 775
794 776
@@ -848,7 +830,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
848 enterlevel(ls); 830 enterlevel(ls);
849 uop = getunopr(ls->t.token); 831 uop = getunopr(ls->t.token);
850 if (uop != OPR_NOUNOPR) { 832 if (uop != OPR_NOUNOPR) {
851 next(ls); 833 luaX_next(ls);
852 subexpr(ls, v, UNARY_PRIORITY); 834 subexpr(ls, v, UNARY_PRIORITY);
853 luaK_prefix(ls->fs, uop, v); 835 luaK_prefix(ls->fs, uop, v);
854 } 836 }
@@ -858,7 +840,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
858 while (op != OPR_NOBINOPR && priority[op].left > limit) { 840 while (op != OPR_NOBINOPR && priority[op].left > limit) {
859 expdesc v2; 841 expdesc v2;
860 BinOpr nextop; 842 BinOpr nextop;
861 next(ls); 843 luaX_next(ls);
862 luaK_infix(ls->fs, op, v); 844 luaK_infix(ls->fs, op, v);
863 /* read sub-expression with higher priority */ 845 /* read sub-expression with higher priority */
864 nextop = subexpr(ls, &v2, priority[op].right); 846 nextop = subexpr(ls, &v2, priority[op].right);
@@ -1009,7 +991,7 @@ static void whilestat (LexState *ls, int line) {
1009 int whileinit; 991 int whileinit;
1010 int condexit; 992 int condexit;
1011 BlockCnt bl; 993 BlockCnt bl;
1012 next(ls); /* skip WHILE */ 994 luaX_next(ls); /* skip WHILE */
1013 whileinit = luaK_getlabel(fs); 995 whileinit = luaK_getlabel(fs);
1014 condexit = cond(ls); 996 condexit = cond(ls);
1015 enterblock(fs, &bl, 1); 997 enterblock(fs, &bl, 1);
@@ -1030,7 +1012,7 @@ static void repeatstat (LexState *ls, int line) {
1030 BlockCnt bl1, bl2; 1012 BlockCnt bl1, bl2;
1031 enterblock(fs, &bl1, 1); /* loop block */ 1013 enterblock(fs, &bl1, 1); /* loop block */
1032 enterblock(fs, &bl2, 0); /* scope block */ 1014 enterblock(fs, &bl2, 0); /* scope block */
1033 next(ls); /* skip REPEAT */ 1015 luaX_next(ls); /* skip REPEAT */
1034 chunk(ls); 1016 chunk(ls);
1035 check_match(ls, TK_UNTIL, TK_REPEAT, line); 1017 check_match(ls, TK_UNTIL, TK_REPEAT, line);
1036 condexit = cond(ls); /* read condition (inside scope block) */ 1018 condexit = cond(ls); /* read condition (inside scope block) */
@@ -1130,7 +1112,7 @@ static void forstat (LexState *ls, int line) {
1130 TString *varname; 1112 TString *varname;
1131 BlockCnt bl; 1113 BlockCnt bl;
1132 enterblock(fs, &bl, 1); /* scope for loop and control variables */ 1114 enterblock(fs, &bl, 1); /* scope for loop and control variables */
1133 next(ls); /* skip `for' */ 1115 luaX_next(ls); /* skip `for' */
1134 varname = str_checkname(ls); /* first variable name */ 1116 varname = str_checkname(ls); /* first variable name */
1135 switch (ls->t.token) { 1117 switch (ls->t.token) {
1136 case '=': fornum(ls, varname, line); break; 1118 case '=': fornum(ls, varname, line); break;
@@ -1145,7 +1127,7 @@ static void forstat (LexState *ls, int line) {
1145static int test_then_block (LexState *ls) { 1127static int test_then_block (LexState *ls) {
1146 /* test_then_block -> [IF | ELSEIF] cond THEN block */ 1128 /* test_then_block -> [IF | ELSEIF] cond THEN block */
1147 int condexit; 1129 int condexit;
1148 next(ls); /* skip IF or ELSEIF */ 1130 luaX_next(ls); /* skip IF or ELSEIF */
1149 condexit = cond(ls); 1131 condexit = cond(ls);
1150 checknext(ls, TK_THEN); 1132 checknext(ls, TK_THEN);
1151 block(ls); /* `then' part */ 1133 block(ls); /* `then' part */
@@ -1167,7 +1149,7 @@ static void ifstat (LexState *ls, int line) {
1167 if (ls->t.token == TK_ELSE) { 1149 if (ls->t.token == TK_ELSE) {
1168 luaK_concat(fs, &escapelist, luaK_jump(fs)); 1150 luaK_concat(fs, &escapelist, luaK_jump(fs));
1169 luaK_patchtohere(fs, flist); 1151 luaK_patchtohere(fs, flist);
1170 next(ls); /* skip ELSE (after patch, for correct line info) */ 1152 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
1171 block(ls); /* `else' part */ 1153 block(ls); /* `else' part */
1172 } 1154 }
1173 else 1155 else
@@ -1228,7 +1210,7 @@ static void funcstat (LexState *ls, int line) {
1228 /* funcstat -> FUNCTION funcname body */ 1210 /* funcstat -> FUNCTION funcname body */
1229 int needself; 1211 int needself;
1230 expdesc v, b; 1212 expdesc v, b;
1231 next(ls); /* skip FUNCTION */ 1213 luaX_next(ls); /* skip FUNCTION */
1232 needself = funcname(ls, &v); 1214 needself = funcname(ls, &v);
1233 body(ls, &b, needself, line); 1215 body(ls, &b, needself, line);
1234 luaK_storevar(ls->fs, &v, &b); 1216 luaK_storevar(ls->fs, &v, &b);
@@ -1255,7 +1237,7 @@ static void retstat (LexState *ls) {
1255 FuncState *fs = ls->fs; 1237 FuncState *fs = ls->fs;
1256 expdesc e; 1238 expdesc e;
1257 int first, nret; /* registers with returned values */ 1239 int first, nret; /* registers with returned values */
1258 next(ls); /* skip RETURN */ 1240 luaX_next(ls); /* skip RETURN */
1259 if (block_follow(ls->t.token) || ls->t.token == ';') 1241 if (block_follow(ls->t.token) || ls->t.token == ';')
1260 first = nret = 0; /* return no values */ 1242 first = nret = 0; /* return no values */
1261 else { 1243 else {
@@ -1295,7 +1277,7 @@ static int statement (LexState *ls) {
1295 return 0; 1277 return 0;
1296 } 1278 }
1297 case TK_DO: { /* stat -> DO block END */ 1279 case TK_DO: { /* stat -> DO block END */
1298 next(ls); /* skip DO */ 1280 luaX_next(ls); /* skip DO */
1299 block(ls); 1281 block(ls);
1300 check_match(ls, TK_END, TK_DO, line); 1282 check_match(ls, TK_END, TK_DO, line);
1301 return 0; 1283 return 0;
@@ -1313,7 +1295,7 @@ static int statement (LexState *ls) {
1313 return 0; 1295 return 0;
1314 } 1296 }
1315 case TK_LOCAL: { /* stat -> localstat */ 1297 case TK_LOCAL: { /* stat -> localstat */
1316 next(ls); /* skip LOCAL */ 1298 luaX_next(ls); /* skip LOCAL */
1317 if (testnext(ls, TK_FUNCTION)) /* local function? */ 1299 if (testnext(ls, TK_FUNCTION)) /* local function? */
1318 localfunc(ls); 1300 localfunc(ls);
1319 else 1301 else
@@ -1325,7 +1307,7 @@ static int statement (LexState *ls) {
1325 return 1; /* must be last statement */ 1307 return 1; /* must be last statement */
1326 } 1308 }
1327 case TK_BREAK: { /* stat -> breakstat */ 1309 case TK_BREAK: { /* stat -> breakstat */
1328 next(ls); /* skip BREAK */ 1310 luaX_next(ls); /* skip BREAK */
1329 breakstat(ls); 1311 breakstat(ls);
1330 return 1; /* must be last statement */ 1312 return 1; /* must be last statement */
1331 } 1313 }