llex.c 11 KB

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