llex.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. ** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $
  3. ** Lexical Analyzer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <locale.h>
  8. #include <string.h>
  9. #define llex_c
  10. #define LUA_CORE
  11. #include "lua.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. const char *const luaX_tokens [] =
  24. {
  25. "and", "break", "do", "else", "elseif",
  26. "end", "false", "for", "function", "if",
  27. "in", "local", "nil", "not", "or", "repeat",
  28. "return", "then", "true", "until", "while",
  29. "..", "...", "==", ">=", "<=", "~=",
  30. "<number>", "<name>", "<string>", "<eof>",
  31. NULL
  32. };
  33. #define save_and_next(ls) (save(ls, ls->current), next(ls))
  34. static void save(LexState *ls, int c)
  35. {
  36. Mbuffer *b = ls->buff;
  37. if (b->n + 1 > b->buffsize)
  38. {
  39. size_t newsize;
  40. if (b->buffsize >= MAX_SIZET / 2)
  41. luaX_lexerror(ls, "lexical element too long", 0);
  42. newsize = b->buffsize * 2;
  43. luaZ_resizebuffer(ls->L, b, newsize);
  44. }
  45. b->buffer[b->n++] = cast(char, c);
  46. }
  47. void luaX_init(lua_State *L)
  48. {
  49. }
  50. #define MAXSRC 80
  51. const char *luaX_token2str(LexState *ls, int token)
  52. {
  53. if (token < FIRST_RESERVED)
  54. {
  55. lua_assert(token == cast(unsigned char, token));
  56. return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
  57. luaO_pushfstring(ls->L, "%c", token);
  58. }
  59. else
  60. return luaX_tokens[token - FIRST_RESERVED];
  61. }
  62. static const char *txtToken(LexState *ls, int token)
  63. {
  64. switch (token)
  65. {
  66. case TK_NAME:
  67. case TK_STRING:
  68. case TK_NUMBER:
  69. save(ls, '\0');
  70. return luaZ_buffer(ls->buff);
  71. default:
  72. return luaX_token2str(ls, token);
  73. }
  74. }
  75. void luaX_lexerror(LexState *ls, const char *msg, int token)
  76. {
  77. char buff[MAXSRC];
  78. luaO_chunkid(buff, getstr(ls->source), MAXSRC);
  79. msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
  80. if (token)
  81. luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
  82. luaD_throw(ls->L, LUA_ERRSYNTAX);
  83. }
  84. void luaX_syntaxerror(LexState *ls, const char *msg)
  85. {
  86. luaX_lexerror(ls, msg, ls->t.token);
  87. }
  88. TString *luaX_newstring(LexState *ls, const char *str, size_t l)
  89. {
  90. lua_State *L = ls->L;
  91. TString *ts = luaS_newlstr(L, str, l);
  92. TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
  93. if (ttisnil(o))
  94. {
  95. setbvalue(o, 1); /* make sure `str' will not be collected */
  96. luaC_checkGC(L);
  97. }
  98. return ts;
  99. }
  100. static void inclinenumber(LexState *ls)
  101. {
  102. int old = ls->current;
  103. lua_assert(currIsNewline(ls));
  104. next(ls); /* skip `\n' or `\r' */
  105. if (currIsNewline(ls) && ls->current != old)
  106. next(ls); /* skip `\n\r' or `\r\n' */
  107. if (++ls->linenumber >= MAX_INT)
  108. luaX_syntaxerror(ls, "chunk has too many lines");
  109. }
  110. void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, TString *source)
  111. {
  112. ls->decpoint = '.';
  113. ls->L = L;
  114. ls->lookahead.token = TK_EOS; /* no look-ahead token */
  115. ls->z = z;
  116. ls->fs = NULL;
  117. ls->linenumber = 1;
  118. ls->lastline = 1;
  119. ls->source = source;
  120. luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
  121. next(ls); /* read first char */
  122. }
  123. /*
  124. ** =======================================================
  125. ** LEXICAL ANALYZER
  126. ** =======================================================
  127. */
  128. static int check_next(LexState *ls, const char *set)
  129. {
  130. if (!strchr(set, ls->current))
  131. return 0;
  132. save_and_next(ls);
  133. return 1;
  134. }
  135. static void buffreplace(LexState *ls, char from, char to)
  136. {
  137. size_t n = luaZ_bufflen(ls->buff);
  138. char *p = luaZ_buffer(ls->buff);
  139. while (n--)
  140. if (p[n] == from) p[n] = to;
  141. }
  142. static void trydecpoint(LexState *ls, SemInfo *seminfo)
  143. {
  144. /* format error: try to update decimal point separator */
  145. struct lconv *cv = localeconv();
  146. char old = ls->decpoint;
  147. ls->decpoint = (cv ? cv->decimal_point[0] : '.');
  148. buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
  149. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r))
  150. {
  151. /* format error with correct decimal point: no more options */
  152. buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
  153. luaX_lexerror(ls, "malformed number", TK_NUMBER);
  154. }
  155. }
  156. /* LUA_NUMBER */
  157. static void read_numeral(LexState *ls, SemInfo *seminfo)
  158. {
  159. lua_assert(isdigit(ls->current));
  160. do
  161. {
  162. save_and_next(ls);
  163. }
  164. while (isdigit(ls->current) || ls->current == '.');
  165. if (check_next(ls, "Ee")) /* `E'? */
  166. check_next(ls, "+-"); /* optional exponent sign */
  167. while (isalnum(ls->current) || ls->current == '_')
  168. save_and_next(ls);
  169. save(ls, '\0');
  170. buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
  171. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
  172. trydecpoint(ls, seminfo); /* try to update decimal point separator */
  173. }
  174. static int skip_sep(LexState *ls)
  175. {
  176. int count = 0;
  177. int s = ls->current;
  178. lua_assert(s == '[' || s == ']');
  179. save_and_next(ls);
  180. while (ls->current == '=')
  181. {
  182. save_and_next(ls);
  183. count++;
  184. }
  185. return (ls->current == s) ? count : (-count) - 1;
  186. }
  187. static void read_long_string(LexState *ls, SemInfo *seminfo, int sep)
  188. {
  189. int cont = 0;
  190. (void)(cont); /* avoid warnings when `cont' is not used */
  191. save_and_next(ls); /* skip 2nd `[' */
  192. if (currIsNewline(ls)) /* string starts with a newline? */
  193. inclinenumber(ls); /* skip it */
  194. for (;;)
  195. {
  196. switch (ls->current)
  197. {
  198. case EOZ:
  199. luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
  200. "unfinished long comment", TK_EOS);
  201. break; /* to avoid warnings */
  202. #if defined(LUA_COMPAT_LSTR)
  203. case '[':
  204. {
  205. if (skip_sep(ls) == sep)
  206. {
  207. save_and_next(ls); /* skip 2nd `[' */
  208. cont++;
  209. #if LUA_COMPAT_LSTR == 1
  210. if (sep == 0)
  211. luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
  212. #endif
  213. }
  214. break;
  215. }
  216. #endif
  217. case ']':
  218. {
  219. if (skip_sep(ls) == sep)
  220. {
  221. save_and_next(ls); /* skip 2nd `]' */
  222. #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
  223. cont--;
  224. if (sep == 0 && cont >= 0) break;
  225. #endif
  226. goto endloop;
  227. }
  228. break;
  229. }
  230. case '\n':
  231. case '\r':
  232. {
  233. save(ls, '\n');
  234. inclinenumber(ls);
  235. if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
  236. break;
  237. }
  238. default:
  239. {
  240. if (seminfo) save_and_next(ls);
  241. else next(ls);
  242. }
  243. }
  244. }
  245. endloop:
  246. if (seminfo)
  247. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
  248. luaZ_bufflen(ls->buff) - 2 * (2 + sep));
  249. }
  250. static void read_string(LexState *ls, int del, SemInfo *seminfo)
  251. {
  252. save_and_next(ls);
  253. while (ls->current != del)
  254. {
  255. switch (ls->current)
  256. {
  257. case EOZ:
  258. luaX_lexerror(ls, "unfinished string", TK_EOS);
  259. continue; /* to avoid warnings */
  260. case '\n':
  261. case '\r':
  262. luaX_lexerror(ls, "unfinished string", TK_STRING);
  263. continue; /* to avoid warnings */
  264. case '\\':
  265. {
  266. int c;
  267. next(ls); /* do not save the `\' */
  268. switch (ls->current)
  269. {
  270. case 'a':
  271. c = '\a';
  272. break;
  273. case 'b':
  274. c = '\b';
  275. break;
  276. case 'f':
  277. c = '\f';
  278. break;
  279. case 'n':
  280. c = '\n';
  281. break;
  282. case 'r':
  283. c = '\r';
  284. break;
  285. case 't':
  286. c = '\t';
  287. break;
  288. case 'v':
  289. c = '\v';
  290. break;
  291. case '\n': /* go through */
  292. case '\r':
  293. save(ls, '\n');
  294. inclinenumber(ls);
  295. continue;
  296. case EOZ:
  297. continue; /* will raise an error next loop */
  298. default:
  299. {
  300. if (!isdigit(ls->current))
  301. save_and_next(ls); /* handles \\, \", \', and \? */
  302. else /* \xxx */
  303. {
  304. int i = 0;
  305. c = 0;
  306. do
  307. {
  308. c = 10 * c + (ls->current - '0');
  309. next(ls);
  310. }
  311. while (++i < 3 && isdigit(ls->current));
  312. if (c > UCHAR_MAX)
  313. luaX_lexerror(ls, "escape sequence too large", TK_STRING);
  314. save(ls, c);
  315. }
  316. continue;
  317. }
  318. }
  319. save(ls, c);
  320. next(ls);
  321. continue;
  322. }
  323. default:
  324. save_and_next(ls);
  325. }
  326. }
  327. save_and_next(ls); /* skip delimiter */
  328. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
  329. luaZ_bufflen(ls->buff) - 2);
  330. }
  331. static int llex(LexState *ls, SemInfo *seminfo)
  332. {
  333. luaZ_resetbuffer(ls->buff);
  334. for (;;)
  335. {
  336. switch (ls->current)
  337. {
  338. case '\n':
  339. case '\r':
  340. {
  341. inclinenumber(ls);
  342. continue;
  343. }
  344. case '-':
  345. {
  346. next(ls);
  347. if (ls->current != '-') return '-';
  348. /* else is a comment */
  349. next(ls);
  350. if (ls->current == '[')
  351. {
  352. int sep = skip_sep(ls);
  353. luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
  354. if (sep >= 0)
  355. {
  356. read_long_string(ls, NULL, sep); /* long comment */
  357. luaZ_resetbuffer(ls->buff);
  358. continue;
  359. }
  360. }
  361. /* else short comment */
  362. while (!currIsNewline(ls) && ls->current != EOZ)
  363. next(ls);
  364. continue;
  365. }
  366. case '[':
  367. {
  368. int sep = skip_sep(ls);
  369. if (sep >= 0)
  370. {
  371. read_long_string(ls, seminfo, sep);
  372. return TK_STRING;
  373. }
  374. else if (sep == -1) return '[';
  375. else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
  376. }
  377. case '=':
  378. {
  379. next(ls);
  380. if (ls->current != '=') return '=';
  381. else
  382. {
  383. next(ls);
  384. return TK_EQ;
  385. }
  386. }
  387. case '<':
  388. {
  389. next(ls);
  390. if (ls->current != '=') return '<';
  391. else
  392. {
  393. next(ls);
  394. return TK_LE;
  395. }
  396. }
  397. case '>':
  398. {
  399. next(ls);
  400. if (ls->current != '=') return '>';
  401. else
  402. {
  403. next(ls);
  404. return TK_GE;
  405. }
  406. }
  407. case '~':
  408. {
  409. next(ls);
  410. if (ls->current != '=') return '~';
  411. else
  412. {
  413. next(ls);
  414. return TK_NE;
  415. }
  416. }
  417. case '"':
  418. case '\'':
  419. {
  420. read_string(ls, ls->current, seminfo);
  421. return TK_STRING;
  422. }
  423. case '.':
  424. {
  425. save_and_next(ls);
  426. if (check_next(ls, "."))
  427. {
  428. if (check_next(ls, "."))
  429. return TK_DOTS; /* ... */
  430. else return TK_CONCAT; /* .. */
  431. }
  432. else if (!isdigit(ls->current)) return '.';
  433. else
  434. {
  435. read_numeral(ls, seminfo);
  436. return TK_NUMBER;
  437. }
  438. }
  439. case EOZ:
  440. {
  441. return TK_EOS;
  442. }
  443. default:
  444. {
  445. if (isspace(ls->current))
  446. {
  447. lua_assert(!currIsNewline(ls));
  448. next(ls);
  449. continue;
  450. }
  451. else if (isdigit(ls->current))
  452. {
  453. read_numeral(ls, seminfo);
  454. return TK_NUMBER;
  455. }
  456. else if (isalpha(ls->current) || ls->current == '_')
  457. {
  458. /* identifier or reserved word */
  459. TString *ts;
  460. int i;
  461. do
  462. {
  463. save_and_next(ls);
  464. }
  465. while (isalnum(ls->current) || ls->current == '_');
  466. /* look for reserved word */
  467. save(ls, '\0');
  468. for (i = 0; i < NUM_RESERVED; i++)
  469. if (!strcmp(luaX_tokens[i], luaZ_buffer(ls->buff)))
  470. return i + FIRST_RESERVED;
  471. ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  472. luaZ_bufflen(ls->buff) - 1);
  473. seminfo->ts = ts;
  474. return TK_NAME;
  475. }
  476. else
  477. {
  478. int c = ls->current;
  479. next(ls);
  480. return c; /* single-char tokens (+ - / ...) */
  481. }
  482. }
  483. }
  484. }
  485. }
  486. void luaX_next(LexState *ls)
  487. {
  488. ls->lastline = ls->linenumber;
  489. if (ls->lookahead.token != TK_EOS) /* is there a look-ahead token? */
  490. {
  491. ls->t = ls->lookahead; /* use this one */
  492. ls->lookahead.token = TK_EOS; /* and discharge it */
  493. }
  494. else
  495. ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
  496. }
  497. void luaX_lookahead(LexState *ls)
  498. {
  499. lua_assert(ls->lookahead.token == TK_EOS);
  500. ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
  501. }