llex.c 15 KB

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