llex.c 12 KB

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