llex.c 12 KB

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