aboutsummaryrefslogtreecommitdiff
path: root/llex.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-21 12:22:02 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-21 12:22:02 -0300
commitc4eff10322f5e1e8213c9004f8b91c3c7bd36e3f (patch)
tree4dcebbb23586a50791e0f019ee2237e1d3b15b24 /llex.c
parent8a0acf0898fc9bd990fc10275e72c89a6ca829a6 (diff)
downloadlua-c4eff10322f5e1e8213c9004f8b91c3c7bd36e3f.tar.gz
lua-c4eff10322f5e1e8213c9004f8b91c3c7bd36e3f.tar.bz2
lua-c4eff10322f5e1e8213c9004f8b91c3c7bd36e3f.zip
small improvements concerning 'check_next'
Diffstat (limited to 'llex.c')
-rw-r--r--llex.c64
1 files changed, 39 insertions, 25 deletions
diff --git a/llex.c b/llex.c
index 046182cb..9d55d670 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 2.76 2014/05/01 18:18:06 roberto Exp roberto $ 2** $Id: llex.c,v 2.77 2014/05/11 14:45:43 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*/
@@ -183,12 +183,26 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
183*/ 183*/
184 184
185 185
186static int check_next1 (LexState *ls, int c) {
187 if (ls->current == c) {
188 next(ls);
189 return 1;
190 }
191 else return 0;
192}
186 193
187static int check_next (LexState *ls, const char *set) { 194
188 if (ls->current == '\0' || !strchr(set, ls->current)) 195/*
189 return 0; 196** Check whether current char is in set 'set' (with two chars) and
190 save_and_next(ls); 197** saves it
191 return 1; 198*/
199static int check_next2 (LexState *ls, const char *set) {
200 lua_assert(set[2] == '\0');
201 if (ls->current == set[0] || ls->current == set[1]) {
202 save_and_next(ls);
203 return 1;
204 }
205 else return 0;
192} 206}
193 207
194 208
@@ -239,11 +253,11 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
239 int first = ls->current; 253 int first = ls->current;
240 lua_assert(lisdigit(ls->current)); 254 lua_assert(lisdigit(ls->current));
241 save_and_next(ls); 255 save_and_next(ls);
242 if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ 256 if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
243 expo = "Pp"; 257 expo = "Pp";
244 for (;;) { 258 for (;;) {
245 if (check_next(ls, expo)) /* exponent part? */ 259 if (check_next2(ls, expo)) /* exponent part? */
246 check_next(ls, "+-"); /* optional exponent sign */ 260 check_next2(ls, "-+"); /* optional exponent sign */
247 if (lisxdigit(ls->current)) 261 if (lisxdigit(ls->current))
248 save_and_next(ls); 262 save_and_next(ls);
249 else if (ls->current == '.') 263 else if (ls->current == '.')
@@ -490,35 +504,35 @@ static int llex (LexState *ls, SemInfo *seminfo) {
490 } 504 }
491 case '=': { 505 case '=': {
492 next(ls); 506 next(ls);
493 if (ls->current != '=') return '='; 507 if (check_next1(ls, '=')) return TK_EQ;
494 else { next(ls); return TK_EQ; } 508 else return '=';
495 } 509 }
496 case '<': { 510 case '<': {
497 next(ls); 511 next(ls);
498 if (ls->current == '=') { next(ls); return TK_LE; } 512 if (check_next1(ls, '=')) return TK_LE;
499 if (ls->current == '<') { next(ls); return TK_SHL; } 513 else if (check_next1(ls, '<')) return TK_SHL;
500 return '<'; 514 else return '<';
501 } 515 }
502 case '>': { 516 case '>': {
503 next(ls); 517 next(ls);
504 if (ls->current == '=') { next(ls); return TK_GE; } 518 if (check_next1(ls, '=')) return TK_GE;
505 if (ls->current == '>') { next(ls); return TK_SHR; } 519 else if (check_next1(ls, '>')) return TK_SHR;
506 return '>'; 520 else return '>';
507 } 521 }
508 case '/': { 522 case '/': {
509 next(ls); 523 next(ls);
510 if (ls->current != '/') return '/'; 524 if (check_next1(ls, '/')) return TK_IDIV;
511 else { next(ls); return TK_IDIV; } 525 else return '/';
512 } 526 }
513 case '~': { 527 case '~': {
514 next(ls); 528 next(ls);
515 if (ls->current != '=') return '~'; 529 if (check_next1(ls, '=')) return TK_NE;
516 else { next(ls); return TK_NE; } 530 else return '~';
517 } 531 }
518 case ':': { 532 case ':': {
519 next(ls); 533 next(ls);
520 if (ls->current != ':') return ':'; 534 if (check_next1(ls, ':')) return TK_DBCOLON;
521 else { next(ls); return TK_DBCOLON; } 535 else return ':';
522 } 536 }
523 case '"': case '\'': { /* short literal strings */ 537 case '"': case '\'': { /* short literal strings */
524 read_string(ls, ls->current, seminfo); 538 read_string(ls, ls->current, seminfo);
@@ -526,8 +540,8 @@ static int llex (LexState *ls, SemInfo *seminfo) {
526 } 540 }
527 case '.': { /* '.', '..', '...', or number */ 541 case '.': { /* '.', '..', '...', or number */
528 save_and_next(ls); 542 save_and_next(ls);
529 if (check_next(ls, ".")) { 543 if (check_next1(ls, '.')) {
530 if (check_next(ls, ".")) 544 if (check_next1(ls, '.'))
531 return TK_DOTS; /* '...' */ 545 return TK_DOTS; /* '...' */
532 else return TK_CONCAT; /* '..' */ 546 else return TK_CONCAT; /* '..' */
533 } 547 }