llex.c 15 KB

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