llex.c 12 KB

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