llex.c 12 KB

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