1 /* 2 ** $Id: llex.c,v 2.63.1.3 2015/02/09 17:56:34 roberto Exp $ 3 ** Lexical Analyzer 4 ** See Copyright Notice in lua.h 5 */ 6 7 #include <sys/zfs_context.h> 8 9 #define llex_c 10 #define LUA_CORE 11 12 #include "lua.h" 13 14 #include "lctype.h" 15 #include "ldo.h" 16 #include "llex.h" 17 #include "lobject.h" 18 #include "lparser.h" 19 #include "lstate.h" 20 #include "lstring.h" 21 #include "ltable.h" 22 #include "lzio.h" 23 24 25 26 #define next(ls) (ls->current = zgetc(ls->z)) 27 28 29 30 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') 31 32 33 /* ORDER RESERVED */ 34 static const char *const luaX_tokens [] = { 35 "and", "break", "do", "else", "elseif", 36 "end", "false", "for", "function", "goto", "if", 37 "in", "local", "nil", "not", "or", "repeat", 38 "return", "then", "true", "until", "while", 39 "..", "...", "==", ">=", "<=", "~=", "::", "<eof>", 40 "<number>", "<name>", "<string>" 41 }; 42 43 44 #define save_and_next(ls) (save(ls, ls->current), next(ls)) 45 46 47 static l_noret lexerror (LexState *ls, const char *msg, int token); 48 49 50 static void save (LexState *ls, int c) { 51 Mbuffer *b = ls->buff; 52 if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { 53 size_t newsize; 54 if (luaZ_sizebuffer(b) >= MAX_SIZET/2) 55 lexerror(ls, "lexical element too long", 0); 56 newsize = luaZ_sizebuffer(b) * 2; 57 luaZ_resizebuffer(ls->L, b, newsize); 58 } 59 b->buffer[luaZ_bufflen(b)++] = cast(char, c); 60 } 61 62 63 void luaX_init (lua_State *L) { 64 int i; 65 for (i=0; i<NUM_RESERVED; i++) { 66 TString *ts = luaS_new(L, luaX_tokens[i]); 67 luaS_fix(ts); /* reserved words are never collected */ 68 ts->tsv.extra = cast_byte(i+1); /* reserved word */ 69 } 70 } 71 72 73 const char *luaX_token2str (LexState *ls, int token) { 74 if (token < FIRST_RESERVED) { /* single-byte symbols? */ 75 lua_assert(token == cast(unsigned char, token)); 76 return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) : 77 luaO_pushfstring(ls->L, "char(%d)", token); 78 } 79 else { 80 const char *s = luaX_tokens[token - FIRST_RESERVED]; 81 if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ 82 return luaO_pushfstring(ls->L, LUA_QS, s); 83 else /* names, strings, and numerals */ 84 return s; 85 } 86 } 87 88 89 static const char *txtToken (LexState *ls, int token) { 90 switch (token) { 91 case TK_NAME: 92 case TK_STRING: 93 case TK_NUMBER: 94 save(ls, '\0'); 95 return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); 96 default: 97 return luaX_token2str(ls, token); 98 } 99 } 100 101 102 static l_noret lexerror (LexState *ls, const char *msg, int token) { 103 char buff[LUA_IDSIZE]; 104 luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE); 105 msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); 106 if (token) 107 luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); 108 luaD_throw(ls->L, LUA_ERRSYNTAX); 109 } 110 111 112 l_noret luaX_syntaxerror (LexState *ls, const char *msg) { 113 lexerror(ls, msg, ls->t.token); 114 } 115 116 117 /* 118 ** creates a new string and anchors it in function's table so that 119 ** it will not be collected until the end of the function's compilation 120 ** (by that time it should be anchored in function's prototype) 121 */ 122 TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 123 lua_State *L = ls->L; 124 TValue *o; /* entry for `str' */ 125 TString *ts = luaS_newlstr(L, str, l); /* create new string */ 126 setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ 127 o = luaH_set(L, ls->fs->h, L->top - 1); 128 if (ttisnil(o)) { /* not in use yet? (see 'addK') */ 129 /* boolean value does not need GC barrier; 130 table has no metatable, so it does not need to invalidate cache */ 131 setbvalue(o, 1); /* t[string] = true */ 132 luaC_checkGC(L); 133 } 134 else { /* string already present */ 135 ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ 136 } 137 L->top--; /* remove string from stack */ 138 return ts; 139 } 140 141 142 /* 143 ** increment line number and skips newline sequence (any of 144 ** \n, \r, \n\r, or \r\n) 145 */ 146 static void inclinenumber (LexState *ls) { 147 int old = ls->current; 148 lua_assert(currIsNewline(ls)); 149 next(ls); /* skip `\n' or `\r' */ 150 if (currIsNewline(ls) && ls->current != old) 151 next(ls); /* skip `\n\r' or `\r\n' */ 152 if (++ls->linenumber >= MAX_INT) 153 lexerror(ls, "chunk has too many lines", 0); 154 } 155 156 157 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, 158 int firstchar) { 159 ls->decpoint = '.'; 160 ls->L = L; 161 ls->current = firstchar; 162 ls->lookahead.token = TK_EOS; /* no look-ahead token */ 163 ls->z = z; 164 ls->fs = NULL; 165 ls->linenumber = 1; 166 ls->lastline = 1; 167 ls->source = source; 168 ls->envn = luaS_new(L, LUA_ENV); /* create env name */ 169 luaS_fix(ls->envn); /* never collect this name */ 170 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ 171 } 172 173 174 175 /* 176 ** ======================================================= 177 ** LEXICAL ANALYZER 178 ** ======================================================= 179 */ 180 181 182 183 static int check_next (LexState *ls, const char *set) { 184 if (ls->current == '\0' || !strchr(set, ls->current)) 185 return 0; 186 save_and_next(ls); 187 return 1; 188 } 189 190 191 /* 192 ** change all characters 'from' in buffer to 'to' 193 */ 194 static void buffreplace (LexState *ls, char from, char to) { 195 size_t n = luaZ_bufflen(ls->buff); 196 char *p = luaZ_buffer(ls->buff); 197 while (n--) 198 if (p[n] == from) p[n] = to; 199 } 200 201 202 #if !defined(getlocaledecpoint) 203 #define getlocaledecpoint() (localeconv()->decimal_point[0]) 204 #endif 205 206 207 #define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e) 208 209 /* 210 ** in case of format error, try to change decimal point separator to 211 ** the one defined in the current locale and check again 212 */ 213 static void trydecpoint (LexState *ls, SemInfo *seminfo) { 214 char old = ls->decpoint; 215 ls->decpoint = getlocaledecpoint(); 216 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ 217 if (!buff2d(ls->buff, &seminfo->r)) { 218 /* format error with correct decimal point: no more options */ 219 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ 220 lexerror(ls, "malformed number", TK_NUMBER); 221 } 222 } 223 224 225 /* LUA_NUMBER */ 226 /* 227 ** this function is quite liberal in what it accepts, as 'luaO_str2d' 228 ** will reject ill-formed numerals. 229 */ 230 static void read_numeral (LexState *ls, SemInfo *seminfo) { 231 const char *expo = "Ee"; 232 int first = ls->current; 233 lua_assert(lisdigit(ls->current)); 234 save_and_next(ls); 235 if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ 236 expo = "Pp"; 237 for (;;) { 238 if (check_next(ls, expo)) /* exponent part? */ 239 check_next(ls, "+-"); /* optional exponent sign */ 240 if (lisxdigit(ls->current) || ls->current == '.') 241 save_and_next(ls); 242 else break; 243 } 244 save(ls, '\0'); 245 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ 246 if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ 247 trydecpoint(ls, seminfo); /* try to update decimal point separator */ 248 } 249 250 251 /* 252 ** skip a sequence '[=*[' or ']=*]' and return its number of '='s or 253 ** -1 if sequence is malformed 254 */ 255 static int skip_sep (LexState *ls) { 256 int count = 0; 257 int s = ls->current; 258 lua_assert(s == '[' || s == ']'); 259 save_and_next(ls); 260 while (ls->current == '=') { 261 save_and_next(ls); 262 count++; 263 } 264 return (ls->current == s) ? count : (-count) - 1; 265 } 266 267 268 static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { 269 save_and_next(ls); /* skip 2nd `[' */ 270 if (currIsNewline(ls)) /* string starts with a newline? */ 271 inclinenumber(ls); /* skip it */ 272 for (;;) { 273 switch (ls->current) { 274 case EOZ: 275 lexerror(ls, (seminfo) ? "unfinished long string" : 276 "unfinished long comment", TK_EOS); 277 break; /* to avoid warnings */ 278 case ']': { 279 if (skip_sep(ls) == sep) { 280 save_and_next(ls); /* skip 2nd `]' */ 281 goto endloop; 282 } 283 break; 284 } 285 case '\n': case '\r': { 286 save(ls, '\n'); 287 inclinenumber(ls); 288 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ 289 break; 290 } 291 default: { 292 if (seminfo) save_and_next(ls); 293 else next(ls); 294 } 295 } 296 } endloop: 297 if (seminfo) 298 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), 299 luaZ_bufflen(ls->buff) - 2*(2 + sep)); 300 } 301 302 303 static void escerror (LexState *ls, int *c, int n, const char *msg) { 304 int i; 305 luaZ_resetbuffer(ls->buff); /* prepare error message */ 306 save(ls, '\\'); 307 for (i = 0; i < n && c[i] != EOZ; i++) 308 save(ls, c[i]); 309 lexerror(ls, msg, TK_STRING); 310 } 311 312 313 static int readhexaesc (LexState *ls) { 314 int c[3], i; /* keep input for error message */ 315 int r = 0; /* result accumulator */ 316 c[0] = 'x'; /* for error message */ 317 for (i = 1; i < 3; i++) { /* read two hexadecimal digits */ 318 c[i] = next(ls); 319 if (!lisxdigit(c[i])) 320 escerror(ls, c, i + 1, "hexadecimal digit expected"); 321 r = (r << 4) + luaO_hexavalue(c[i]); 322 } 323 return r; 324 } 325 326 327 static int readdecesc (LexState *ls) { 328 int c[3], i; 329 int r = 0; /* result accumulator */ 330 for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ 331 c[i] = ls->current; 332 r = 10*r + c[i] - '0'; 333 next(ls); 334 } 335 if (r > UCHAR_MAX) 336 escerror(ls, c, i, "decimal escape too large"); 337 return r; 338 } 339 340 341 static void read_string (LexState *ls, int del, SemInfo *seminfo) { 342 save_and_next(ls); /* keep delimiter (for error messages) */ 343 while (ls->current != del) { 344 switch (ls->current) { 345 case EOZ: 346 lexerror(ls, "unfinished string", TK_EOS); 347 break; /* to avoid warnings */ 348 case '\n': 349 case '\r': 350 lexerror(ls, "unfinished string", TK_STRING); 351 break; /* to avoid warnings */ 352 case '\\': { /* escape sequences */ 353 int c; /* final character to be saved */ 354 next(ls); /* do not save the `\' */ 355 switch (ls->current) { 356 case 'a': c = '\a'; goto read_save; 357 case 'b': c = '\b'; goto read_save; 358 case 'f': c = '\f'; goto read_save; 359 case 'n': c = '\n'; goto read_save; 360 case 'r': c = '\r'; goto read_save; 361 case 't': c = '\t'; goto read_save; 362 case 'v': c = '\v'; goto read_save; 363 case 'x': c = readhexaesc(ls); goto read_save; 364 case '\n': case '\r': 365 inclinenumber(ls); c = '\n'; goto only_save; 366 case '\\': case '\"': case '\'': 367 c = ls->current; goto read_save; 368 case EOZ: goto no_save; /* will raise an error next loop */ 369 case 'z': { /* zap following span of spaces */ 370 next(ls); /* skip the 'z' */ 371 while (lisspace(ls->current)) { 372 if (currIsNewline(ls)) inclinenumber(ls); 373 else next(ls); 374 } 375 goto no_save; 376 } 377 default: { 378 if (!lisdigit(ls->current)) 379 escerror(ls, &ls->current, 1, "invalid escape sequence"); 380 /* digital escape \ddd */ 381 c = readdecesc(ls); 382 goto only_save; 383 } 384 } 385 read_save: next(ls); /* read next character */ 386 only_save: save(ls, c); /* save 'c' */ 387 no_save: break; 388 } 389 default: 390 save_and_next(ls); 391 } 392 } 393 save_and_next(ls); /* skip delimiter */ 394 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, 395 luaZ_bufflen(ls->buff) - 2); 396 } 397 398 399 static int llex (LexState *ls, SemInfo *seminfo) { 400 luaZ_resetbuffer(ls->buff); 401 for (;;) { 402 switch (ls->current) { 403 case '\n': case '\r': { /* line breaks */ 404 inclinenumber(ls); 405 break; 406 } 407 case ' ': case '\f': case '\t': case '\v': { /* spaces */ 408 next(ls); 409 break; 410 } 411 case '-': { /* '-' or '--' (comment) */ 412 next(ls); 413 if (ls->current != '-') return '-'; 414 /* else is a comment */ 415 next(ls); 416 if (ls->current == '[') { /* long comment? */ 417 int sep = skip_sep(ls); 418 luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ 419 if (sep >= 0) { 420 read_long_string(ls, NULL, sep); /* skip long comment */ 421 luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ 422 break; 423 } 424 } 425 /* else short comment */ 426 while (!currIsNewline(ls) && ls->current != EOZ) 427 next(ls); /* skip until end of line (or end of file) */ 428 break; 429 } 430 case '[': { /* long string or simply '[' */ 431 int sep = skip_sep(ls); 432 if (sep >= 0) { 433 read_long_string(ls, seminfo, sep); 434 return TK_STRING; 435 } 436 else if (sep == -1) return '['; 437 else lexerror(ls, "invalid long string delimiter", TK_STRING); 438 } 439 case '=': { 440 next(ls); 441 if (ls->current != '=') return '='; 442 else { next(ls); return TK_EQ; } 443 } 444 case '<': { 445 next(ls); 446 if (ls->current != '=') return '<'; 447 else { next(ls); return TK_LE; } 448 } 449 case '>': { 450 next(ls); 451 if (ls->current != '=') return '>'; 452 else { next(ls); return TK_GE; } 453 } 454 case '~': { 455 next(ls); 456 if (ls->current != '=') return '~'; 457 else { next(ls); return TK_NE; } 458 } 459 case ':': { 460 next(ls); 461 if (ls->current != ':') return ':'; 462 else { next(ls); return TK_DBCOLON; } 463 } 464 case '"': case '\'': { /* short literal strings */ 465 read_string(ls, ls->current, seminfo); 466 return TK_STRING; 467 } 468 case '.': { /* '.', '..', '...', or number */ 469 save_and_next(ls); 470 if (check_next(ls, ".")) { 471 if (check_next(ls, ".")) 472 return TK_DOTS; /* '...' */ 473 else return TK_CONCAT; /* '..' */ 474 } 475 else if (!lisdigit(ls->current)) return '.'; 476 /* else go through */ 477 } 478 case '0': case '1': case '2': case '3': case '4': 479 case '5': case '6': case '7': case '8': case '9': { 480 read_numeral(ls, seminfo); 481 return TK_NUMBER; 482 } 483 case EOZ: { 484 return TK_EOS; 485 } 486 default: { 487 if (lislalpha(ls->current)) { /* identifier or reserved word? */ 488 TString *ts; 489 do { 490 save_and_next(ls); 491 } while (lislalnum(ls->current)); 492 ts = luaX_newstring(ls, luaZ_buffer(ls->buff), 493 luaZ_bufflen(ls->buff)); 494 seminfo->ts = ts; 495 if (isreserved(ts)) /* reserved word? */ 496 return ts->tsv.extra - 1 + FIRST_RESERVED; 497 else { 498 return TK_NAME; 499 } 500 } 501 else { /* single-char tokens (+ - / ...) */ 502 int c = ls->current; 503 next(ls); 504 return c; 505 } 506 } 507 } 508 } 509 } 510 511 512 void luaX_next (LexState *ls) { 513 ls->lastline = ls->linenumber; 514 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ 515 ls->t = ls->lookahead; /* use this one */ 516 ls->lookahead.token = TK_EOS; /* and discharge it */ 517 } 518 else 519 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ 520 } 521 522 523 int luaX_lookahead (LexState *ls) { 524 lua_assert(ls->lookahead.token == TK_EOS); 525 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); 526 return ls->lookahead.token; 527 } 528 529