llex.c 12 KB

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