llex.c 12 KB

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