llex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #ifndef __ANDROID__
  136. struct lconv *cv = localeconv();
  137. #endif
  138. char old = ls->decpoint;
  139. #ifndef __ANDROID__
  140. ls->decpoint = (cv ? cv->decimal_point[0] : '.');
  141. #else
  142. ls->decpoint = '.';
  143. #endif
  144. buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
  145. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
  146. /* format error with correct decimal point: no more options */
  147. buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
  148. luaX_lexerror(ls, "malformed number", TK_NUMBER);
  149. }
  150. }
  151. /* LUA_NUMBER */
  152. static void read_numeral (LexState *ls, SemInfo *seminfo) {
  153. lua_assert(isdigit(ls->current));
  154. do {
  155. save_and_next(ls);
  156. } while (isdigit(ls->current) || ls->current == '.');
  157. if (check_next(ls, "Ee")) /* `E'? */
  158. check_next(ls, "+-"); /* optional exponent sign */
  159. while (isalnum(ls->current) || ls->current == '_')
  160. save_and_next(ls);
  161. save(ls, '\0');
  162. buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
  163. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
  164. trydecpoint(ls, seminfo); /* try to update decimal point separator */
  165. }
  166. static int skip_sep (LexState *ls) {
  167. int count = 0;
  168. int s = ls->current;
  169. lua_assert(s == '[' || s == ']');
  170. save_and_next(ls);
  171. while (ls->current == '=') {
  172. save_and_next(ls);
  173. count++;
  174. }
  175. return (ls->current == s) ? count : (-count) - 1;
  176. }
  177. static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
  178. int cont = 0;
  179. (void)(cont); /* avoid warnings when `cont' is not used */
  180. save_and_next(ls); /* skip 2nd `[' */
  181. if (currIsNewline(ls)) /* string starts with a newline? */
  182. inclinenumber(ls); /* skip it */
  183. for (;;) {
  184. switch (ls->current) {
  185. case EOZ:
  186. luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
  187. "unfinished long comment", TK_EOS);
  188. break; /* to avoid warnings */
  189. #if defined(LUA_COMPAT_LSTR)
  190. case '[': {
  191. if (skip_sep(ls) == sep) {
  192. save_and_next(ls); /* skip 2nd `[' */
  193. cont++;
  194. #if LUA_COMPAT_LSTR == 1
  195. if (sep == 0)
  196. luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
  197. #endif
  198. }
  199. break;
  200. }
  201. #endif
  202. case ']': {
  203. if (skip_sep(ls) == sep) {
  204. save_and_next(ls); /* skip 2nd `]' */
  205. #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
  206. cont--;
  207. if (sep == 0 && cont >= 0) break;
  208. #endif
  209. goto endloop;
  210. }
  211. break;
  212. }
  213. case '\n':
  214. case '\r': {
  215. save(ls, '\n');
  216. inclinenumber(ls);
  217. if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
  218. break;
  219. }
  220. default: {
  221. if (seminfo) save_and_next(ls);
  222. else next(ls);
  223. }
  224. }
  225. } endloop:
  226. if (seminfo)
  227. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
  228. luaZ_bufflen(ls->buff) - 2*(2 + sep));
  229. }
  230. static void read_string (LexState *ls, int del, SemInfo *seminfo) {
  231. save_and_next(ls);
  232. while (ls->current != del) {
  233. switch (ls->current) {
  234. case EOZ:
  235. luaX_lexerror(ls, "unfinished string", TK_EOS);
  236. continue; /* to avoid warnings */
  237. case '\n':
  238. case '\r':
  239. luaX_lexerror(ls, "unfinished string", TK_STRING);
  240. continue; /* to avoid warnings */
  241. case '\\': {
  242. int c;
  243. next(ls); /* do not save the `\' */
  244. switch (ls->current) {
  245. case 'a': c = '\a'; break;
  246. case 'b': c = '\b'; break;
  247. case 'f': c = '\f'; break;
  248. case 'n': c = '\n'; break;
  249. case 'r': c = '\r'; break;
  250. case 't': c = '\t'; break;
  251. case 'v': c = '\v'; break;
  252. case '\n': /* go through */
  253. case '\r': save(ls, '\n'); inclinenumber(ls); continue;
  254. case EOZ: continue; /* will raise an error next loop */
  255. default: {
  256. if (!isdigit(ls->current))
  257. save_and_next(ls); /* handles \\, \", \', and \? */
  258. else { /* \xxx */
  259. int i = 0;
  260. c = 0;
  261. do {
  262. c = 10*c + (ls->current-'0');
  263. next(ls);
  264. } while (++i<3 && isdigit(ls->current));
  265. if (c > UCHAR_MAX)
  266. luaX_lexerror(ls, "escape sequence too large", TK_STRING);
  267. save(ls, c);
  268. }
  269. continue;
  270. }
  271. }
  272. save(ls, c);
  273. next(ls);
  274. continue;
  275. }
  276. default:
  277. save_and_next(ls);
  278. }
  279. }
  280. save_and_next(ls); /* skip delimiter */
  281. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
  282. luaZ_bufflen(ls->buff) - 2);
  283. }
  284. static int llex (LexState *ls, SemInfo *seminfo) {
  285. luaZ_resetbuffer(ls->buff);
  286. for (;;) {
  287. switch (ls->current) {
  288. case '\n':
  289. case '\r': {
  290. inclinenumber(ls);
  291. continue;
  292. }
  293. case '-': {
  294. next(ls);
  295. if (ls->current != '-') return '-';
  296. /* else is a comment */
  297. next(ls);
  298. if (ls->current == '[') {
  299. int sep = skip_sep(ls);
  300. luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
  301. if (sep >= 0) {
  302. read_long_string(ls, NULL, sep); /* long comment */
  303. luaZ_resetbuffer(ls->buff);
  304. continue;
  305. }
  306. }
  307. /* else short comment */
  308. while (!currIsNewline(ls) && ls->current != EOZ)
  309. next(ls);
  310. continue;
  311. }
  312. case '[': {
  313. int sep = skip_sep(ls);
  314. if (sep >= 0) {
  315. read_long_string(ls, seminfo, sep);
  316. return TK_STRING;
  317. }
  318. else if (sep == -1) return '[';
  319. else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
  320. }
  321. case '=': {
  322. next(ls);
  323. if (ls->current != '=') return '=';
  324. else { next(ls); return TK_EQ; }
  325. }
  326. case '<': {
  327. next(ls);
  328. if (ls->current != '=') return '<';
  329. else { next(ls); return TK_LE; }
  330. }
  331. case '>': {
  332. next(ls);
  333. if (ls->current != '=') return '>';
  334. else { next(ls); return TK_GE; }
  335. }
  336. case '~': {
  337. next(ls);
  338. if (ls->current != '=') return '~';
  339. else { next(ls); return TK_NE; }
  340. }
  341. case '"':
  342. case '\'': {
  343. read_string(ls, ls->current, seminfo);
  344. return TK_STRING;
  345. }
  346. case '.': {
  347. save_and_next(ls);
  348. if (check_next(ls, ".")) {
  349. if (check_next(ls, "."))
  350. return TK_DOTS; /* ... */
  351. else return TK_CONCAT; /* .. */
  352. }
  353. else if (!isdigit(ls->current)) return '.';
  354. else {
  355. read_numeral(ls, seminfo);
  356. return TK_NUMBER;
  357. }
  358. }
  359. case EOZ: {
  360. return TK_EOS;
  361. }
  362. default: {
  363. if (isspace(ls->current)) {
  364. lua_assert(!currIsNewline(ls));
  365. next(ls);
  366. continue;
  367. }
  368. else if (isdigit(ls->current)) {
  369. read_numeral(ls, seminfo);
  370. return TK_NUMBER;
  371. }
  372. else if (isalpha(ls->current) || ls->current == '_') {
  373. /* identifier or reserved word */
  374. TString *ts;
  375. do {
  376. save_and_next(ls);
  377. } while (isalnum(ls->current) || ls->current == '_');
  378. ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  379. luaZ_bufflen(ls->buff));
  380. if (ts->tsv.reserved > 0) /* reserved word? */
  381. return ts->tsv.reserved - 1 + FIRST_RESERVED;
  382. else {
  383. seminfo->ts = ts;
  384. return TK_NAME;
  385. }
  386. }
  387. else {
  388. int c = ls->current;
  389. next(ls);
  390. return c; /* single-char tokens (+ - / ...) */
  391. }
  392. }
  393. }
  394. }
  395. }
  396. void luaX_next (LexState *ls) {
  397. ls->lastline = ls->linenumber;
  398. if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
  399. ls->t = ls->lookahead; /* use this one */
  400. ls->lookahead.token = TK_EOS; /* and discharge it */
  401. }
  402. else
  403. ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
  404. }
  405. void luaX_lookahead (LexState *ls) {
  406. lua_assert(ls->lookahead.token == TK_EOS);
  407. ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
  408. }