llex.c 15 KB

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