llex.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. ** $Id: llex.c,v 2.36 2010/04/05 16:35:37 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", "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. lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
  50. ts->tsv.reserved = cast_byte(i+1); /* reserved word */
  51. }
  52. }
  53. const char *luaX_token2str (LexState *ls, int token) {
  54. if (token < FIRST_RESERVED) {
  55. lua_assert(token == cast(unsigned char, token));
  56. return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) :
  57. luaO_pushfstring(ls->L, "char(%d)", token);
  58. }
  59. else {
  60. const char *s = luaX_tokens[token - FIRST_RESERVED];
  61. if (token < TK_EOS)
  62. return luaO_pushfstring(ls->L, LUA_QS, s);
  63. else
  64. return s;
  65. }
  66. }
  67. static const char *txtToken (LexState *ls, int token) {
  68. switch (token) {
  69. case TK_NAME:
  70. case TK_STRING:
  71. case TK_NUMBER:
  72. save(ls, '\0');
  73. return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff));
  74. default:
  75. return luaX_token2str(ls, token);
  76. }
  77. }
  78. static void lexerror (LexState *ls, const char *msg, int token) {
  79. char buff[LUA_IDSIZE];
  80. luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
  81. msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
  82. if (token)
  83. luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
  84. luaD_throw(ls->L, LUA_ERRSYNTAX);
  85. }
  86. void luaX_syntaxerror (LexState *ls, const char *msg) {
  87. lexerror(ls, msg, ls->t.token);
  88. }
  89. /*
  90. ** creates a new string and anchors it in function's table so that
  91. ** it will not be collected until the end of the function's compilation
  92. ** (by that time it should be anchored in function's prototype)
  93. */
  94. TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
  95. lua_State *L = ls->L;
  96. TValue *o; /* entry for `str' */
  97. TString *ts = luaS_newlstr(L, str, l); /* create new string */
  98. setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
  99. o = luaH_setstr(L, ls->fs->h, ts);
  100. if (ttisnil(o)) {
  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. ls->decpoint = '.';
  122. ls->L = L;
  123. ls->lookahead.token = TK_EOS; /* no look-ahead token */
  124. ls->z = z;
  125. ls->fs = NULL;
  126. ls->linenumber = 1;
  127. ls->lastline = 1;
  128. ls->source = source;
  129. ls->envn = luaS_new(L, "_ENV"); /* create env name */
  130. luaS_fix(ls->envn); /* never collect this name */
  131. luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
  132. next(ls); /* read first char */
  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. /*
  158. ** in case of format error, try to change decimal point separator to
  159. ** the one defined in the current locale and check again
  160. */
  161. static void trydecpoint (LexState *ls, SemInfo *seminfo) {
  162. char old = ls->decpoint;
  163. ls->decpoint = getlocaledecpoint();
  164. buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
  165. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
  166. /* format error with correct decimal point: no more options */
  167. buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
  168. lexerror(ls, "malformed number", TK_NUMBER);
  169. }
  170. }
  171. /* LUA_NUMBER */
  172. static void read_numeral (LexState *ls, SemInfo *seminfo) {
  173. lua_assert(lisdigit(ls->current));
  174. do {
  175. save_and_next(ls);
  176. } while (lisdigit(ls->current) || ls->current == '.');
  177. if (check_next(ls, "Ee")) /* `E'? */
  178. check_next(ls, "+-"); /* optional exponent sign */
  179. while (lislalnum(ls->current))
  180. save_and_next(ls);
  181. save(ls, '\0');
  182. buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
  183. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
  184. trydecpoint(ls, seminfo); /* try to update decimal point separator */
  185. }
  186. /*
  187. ** skip a sequence '[=*=[' or ']=*]' and return its number of '='s or
  188. ** -1 if sequence is malformed
  189. */
  190. static int skip_sep (LexState *ls) {
  191. int count = 0;
  192. int s = ls->current;
  193. lua_assert(s == '[' || s == ']');
  194. save_and_next(ls);
  195. while (ls->current == '=') {
  196. save_and_next(ls);
  197. count++;
  198. }
  199. return (ls->current == s) ? count : (-count) - 1;
  200. }
  201. static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
  202. save_and_next(ls); /* skip 2nd `[' */
  203. if (currIsNewline(ls)) /* string starts with a newline? */
  204. inclinenumber(ls); /* skip it */
  205. for (;;) {
  206. switch (ls->current) {
  207. case EOZ:
  208. lexerror(ls, (seminfo) ? "unfinished long string" :
  209. "unfinished long comment", TK_EOS);
  210. break; /* to avoid warnings */
  211. case ']': {
  212. if (skip_sep(ls) == sep) {
  213. save_and_next(ls); /* skip 2nd `]' */
  214. goto endloop;
  215. }
  216. break;
  217. }
  218. case '\n': case '\r': {
  219. save(ls, '\n');
  220. inclinenumber(ls);
  221. if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
  222. break;
  223. }
  224. default: {
  225. if (seminfo) save_and_next(ls);
  226. else next(ls);
  227. }
  228. }
  229. } endloop:
  230. if (seminfo)
  231. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
  232. luaZ_bufflen(ls->buff) - 2*(2 + sep));
  233. }
  234. static int hexavalue (int c) {
  235. if (lisdigit(c)) return c - '0';
  236. else if (lisupper(c)) return c - 'A' + 10;
  237. else return c - 'a' + 10;
  238. }
  239. static int readhexaesc (LexState *ls) {
  240. int c1, c2 = EOZ;
  241. if (!lisxdigit(c1 = next(ls)) || !lisxdigit(c2 = next(ls))) {
  242. luaZ_resetbuffer(ls->buff); /* prepare error message */
  243. save(ls, '\\'); save(ls, 'x');
  244. if (c1 != EOZ) save(ls, c1);
  245. if (c2 != EOZ) save(ls, c2);
  246. lexerror(ls, "hexadecimal digit expected", TK_STRING);
  247. }
  248. return (hexavalue(c1) << 4) + hexavalue(c2);
  249. }
  250. static int readdecesc (LexState *ls) {
  251. int c1 = ls->current, c2, c3;
  252. int c = c1 - '0';
  253. if (lisdigit(c2 = next(ls))) {
  254. c = 10*c + c2 - '0';
  255. if (lisdigit(c3 = next(ls))) {
  256. c = 10*c + c3 - '0';
  257. if (c > UCHAR_MAX) {
  258. luaZ_resetbuffer(ls->buff); /* prepare error message */
  259. save(ls, '\\');
  260. save(ls, c1); save(ls, c2); save(ls, c3);
  261. lexerror(ls, "decimal escape too large", TK_STRING);
  262. }
  263. return c;
  264. }
  265. }
  266. /* else, has read one character that was not a digit */
  267. zungetc(ls->z); /* return it to input stream */
  268. return c;
  269. }
  270. static void read_string (LexState *ls, int del, SemInfo *seminfo) {
  271. save_and_next(ls); /* keep delimiter (for error messages) */
  272. while (ls->current != del) {
  273. switch (ls->current) {
  274. case EOZ:
  275. lexerror(ls, "unfinished string", TK_EOS);
  276. break; /* to avoid warnings */
  277. case '\n':
  278. case '\r':
  279. lexerror(ls, "unfinished string", TK_STRING);
  280. break; /* to avoid warnings */
  281. case '\\': { /* escape sequences */
  282. int c; /* final character to be saved */
  283. next(ls); /* do not save the `\' */
  284. switch (ls->current) {
  285. case 'a': c = '\a'; break;
  286. case 'b': c = '\b'; break;
  287. case 'f': c = '\f'; break;
  288. case 'n': c = '\n'; break;
  289. case 'r': c = '\r'; break;
  290. case 't': c = '\t'; break;
  291. case 'v': c = '\v'; break;
  292. case 'x': c = readhexaesc(ls); break;
  293. case '\n':
  294. case '\r': save(ls, '\n'); inclinenumber(ls); continue;
  295. case EOZ: continue; /* will raise an error next loop */
  296. case '*': { /* skip following span of spaces */
  297. next(ls); /* skip the '*' */
  298. while (lisspace(ls->current)) {
  299. if (currIsNewline(ls)) inclinenumber(ls);
  300. else next(ls);
  301. }
  302. continue; /* do not save 'c' */
  303. }
  304. default: {
  305. if (!lisdigit(ls->current))
  306. c = ls->current; /* handles \\, \", \', and \? */
  307. else /* digital escape \ddd */
  308. c = readdecesc(ls);
  309. break;
  310. }
  311. }
  312. next(ls);
  313. save(ls, c);
  314. break;
  315. }
  316. default:
  317. save_and_next(ls);
  318. }
  319. }
  320. save_and_next(ls); /* skip delimiter */
  321. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
  322. luaZ_bufflen(ls->buff) - 2);
  323. }
  324. static int llex (LexState *ls, SemInfo *seminfo) {
  325. luaZ_resetbuffer(ls->buff);
  326. for (;;) {
  327. switch (ls->current) {
  328. case '\n': case '\r': { /* line breaks */
  329. inclinenumber(ls);
  330. break;
  331. }
  332. case ' ': case '\f': case '\t': case '\v': { /* spaces */
  333. next(ls);
  334. break;
  335. }
  336. case '-': { /* '-' or '--' (comment) */
  337. next(ls);
  338. if (ls->current != '-') return '-';
  339. /* else is a comment */
  340. next(ls);
  341. if (ls->current == '[') { /* long comment? */
  342. int sep = skip_sep(ls);
  343. luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
  344. if (sep >= 0) {
  345. read_long_string(ls, NULL, sep); /* skip long comment */
  346. luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
  347. break;
  348. }
  349. }
  350. /* else short comment */
  351. while (!currIsNewline(ls) && ls->current != EOZ)
  352. next(ls); /* skip until end of line (or end of file) */
  353. break;
  354. }
  355. case '[': { /* long string or simply '[' */
  356. int sep = skip_sep(ls);
  357. if (sep >= 0) {
  358. read_long_string(ls, seminfo, sep);
  359. return TK_STRING;
  360. }
  361. else if (sep == -1) return '[';
  362. else lexerror(ls, "invalid long string delimiter", TK_STRING);
  363. }
  364. case '=': {
  365. next(ls);
  366. if (ls->current != '=') return '=';
  367. else { next(ls); return TK_EQ; }
  368. }
  369. case '<': {
  370. next(ls);
  371. if (ls->current != '=') return '<';
  372. else { next(ls); return TK_LE; }
  373. }
  374. case '>': {
  375. next(ls);
  376. if (ls->current != '=') return '>';
  377. else { next(ls); return TK_GE; }
  378. }
  379. case '~': {
  380. next(ls);
  381. if (ls->current != '=') return '~';
  382. else { next(ls); return TK_NE; }
  383. }
  384. case '"': case '\'': { /* short literal strings */
  385. read_string(ls, ls->current, seminfo);
  386. return TK_STRING;
  387. }
  388. case '.': { /* '.', '..', '...', or number */
  389. save_and_next(ls);
  390. if (check_next(ls, ".")) {
  391. if (check_next(ls, "."))
  392. return TK_DOTS; /* '...' */
  393. else return TK_CONCAT; /* '..' */
  394. }
  395. else if (!lisdigit(ls->current)) return '.';
  396. /* else go through */
  397. }
  398. case '0': case '1': case '2': case '3': case '4':
  399. case '5': case '6': case '7': case '8': case '9': {
  400. read_numeral(ls, seminfo);
  401. return TK_NUMBER;
  402. }
  403. case EOZ: {
  404. return TK_EOS;
  405. }
  406. default: {
  407. if (lislalpha(ls->current)) { /* identifier or reserved word? */
  408. TString *ts;
  409. do {
  410. save_and_next(ls);
  411. } while (lislalnum(ls->current));
  412. ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  413. luaZ_bufflen(ls->buff));
  414. if (ts->tsv.reserved > 0) /* reserved word? */
  415. return ts->tsv.reserved - 1 + FIRST_RESERVED;
  416. else {
  417. seminfo->ts = ts;
  418. return TK_NAME;
  419. }
  420. }
  421. else { /* single-char tokens (+ - / ...) */
  422. int c = ls->current;
  423. next(ls);
  424. return c;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. void luaX_next (LexState *ls) {
  431. ls->lastline = ls->linenumber;
  432. if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
  433. ls->t = ls->lookahead; /* use this one */
  434. ls->lookahead.token = TK_EOS; /* and discharge it */
  435. }
  436. else
  437. ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
  438. }
  439. int luaX_lookahead (LexState *ls) {
  440. lua_assert(ls->lookahead.token == TK_EOS);
  441. ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
  442. return ls->lookahead.token;
  443. }