llex.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. ** $Id: llex.c,v 1.75 2001/01/15 18:07:56 roberto Exp roberto $
  3. ** Lexical Analyzer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "lua.h"
  10. #include "llex.h"
  11. #include "lobject.h"
  12. #include "lparser.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "lzio.h"
  16. #define next(LS) (LS->current = zgetc(LS->z))
  17. /* ORDER RESERVED */
  18. static const char *const token2string [] = {
  19. "and", "break", "do", "else", "elseif", "end", "for",
  20. "function", "if", "local", "nil", "not", "or", "repeat", "return", "then",
  21. "until", "while", "", "..", "...", "==", ">=", "<=", "~=", "", "", "<eof>"};
  22. void luaX_init (lua_State *L) {
  23. int i;
  24. for (i=0; i<NUM_RESERVED; i++) {
  25. TString *ts = luaS_new(L, token2string[i]);
  26. lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
  27. ts->marked = (unsigned char)(RESERVEDMARK+i); /* reserved word */
  28. }
  29. }
  30. #define MAXSRC 80
  31. void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
  32. if (val > limit) {
  33. char buff[90];
  34. sprintf(buff, "too many %.40s (limit=%d)", msg, limit);
  35. luaX_error(ls, buff, ls->t.token);
  36. }
  37. }
  38. void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
  39. char buff[MAXSRC];
  40. luaO_chunkid(buff, ls->source->str, sizeof(buff));
  41. luaO_verror(ls->L, "%.99s;\n last token read: `%.30s' at line %d in %.80s",
  42. s, token, ls->linenumber, buff);
  43. }
  44. void luaX_error (LexState *ls, const char *s, int token) {
  45. char buff[TOKEN_LEN];
  46. luaX_token2str(token, buff);
  47. if (buff[0] == '\0')
  48. luaX_syntaxerror(ls, s, G(ls->L)->Mbuffer);
  49. else
  50. luaX_syntaxerror(ls, s, buff);
  51. }
  52. void luaX_token2str (int token, char *s) {
  53. if (token < 256) {
  54. s[0] = (char)token;
  55. s[1] = '\0';
  56. }
  57. else
  58. strcpy(s, token2string[token-FIRST_RESERVED]);
  59. }
  60. static void luaX_invalidchar (LexState *ls, int c) {
  61. char buff[8];
  62. sprintf(buff, "0x%02X", c);
  63. luaX_syntaxerror(ls, "invalid control char", buff);
  64. }
  65. static void inclinenumber (LexState *LS) {
  66. next(LS); /* skip '\n' */
  67. ++LS->linenumber;
  68. luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
  69. }
  70. void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
  71. LS->L = L;
  72. LS->lookahead.token = TK_EOS; /* no look-ahead token */
  73. LS->z = z;
  74. LS->fs = NULL;
  75. LS->linenumber = 1;
  76. LS->lastline = 1;
  77. LS->source = source;
  78. next(LS); /* read first char */
  79. if (LS->current == '#') {
  80. do { /* skip first line */
  81. next(LS);
  82. } while (LS->current != '\n' && LS->current != EOZ);
  83. }
  84. }
  85. /*
  86. ** =======================================================
  87. ** LEXICAL ANALYZER
  88. ** =======================================================
  89. */
  90. /* use Mbuffer to store names, literal strings and numbers */
  91. #define EXTRABUFF 128
  92. #define checkbuffer(L, n, len) if ((len)+(n) > G(L)->Mbuffsize) \
  93. luaO_openspace(L, (len)+(n)+EXTRABUFF)
  94. #define save(L, c, l) (G(L)->Mbuffer[l++] = (char)c)
  95. #define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
  96. static size_t readname (LexState *LS) {
  97. lua_State *L = LS->L;
  98. size_t l = 0;
  99. checkbuffer(L, 10, l);
  100. do {
  101. checkbuffer(L, 10, l);
  102. save_and_next(L, LS, l);
  103. } while (isalnum(LS->current) || LS->current == '_');
  104. save(L, '\0', l);
  105. return l-1;
  106. }
  107. /* LUA_NUMBER */
  108. static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
  109. lua_State *L = LS->L;
  110. size_t l = 0;
  111. checkbuffer(L, 10, l);
  112. if (comma) save(L, '.', l);
  113. while (isdigit(LS->current)) {
  114. checkbuffer(L, 10, l);
  115. save_and_next(L, LS, l);
  116. }
  117. if (LS->current == '.') {
  118. save_and_next(L, LS, l);
  119. if (LS->current == '.') {
  120. save_and_next(L, LS, l);
  121. save(L, '\0', l);
  122. luaX_error(LS, "ambiguous syntax"
  123. " (decimal point x string concatenation)", TK_NUMBER);
  124. }
  125. }
  126. while (isdigit(LS->current)) {
  127. checkbuffer(L, 10, l);
  128. save_and_next(L, LS, l);
  129. }
  130. if (LS->current == 'e' || LS->current == 'E') {
  131. save_and_next(L, LS, l); /* read 'E' */
  132. if (LS->current == '+' || LS->current == '-')
  133. save_and_next(L, LS, l); /* optional exponent sign */
  134. while (isdigit(LS->current)) {
  135. checkbuffer(L, 10, l);
  136. save_and_next(L, LS, l);
  137. }
  138. }
  139. save(L, '\0', l);
  140. if (!luaO_str2d(G(L)->Mbuffer, &seminfo->r))
  141. luaX_error(LS, "malformed number", TK_NUMBER);
  142. }
  143. static void read_long_string (LexState *LS, SemInfo *seminfo) {
  144. lua_State *L = LS->L;
  145. int cont = 0;
  146. size_t l = 0;
  147. checkbuffer(L, 10, l);
  148. save(L, '[', l); /* save first '[' */
  149. save_and_next(L, LS, l); /* pass the second '[' */
  150. for (;;) {
  151. checkbuffer(L, 10, l);
  152. switch (LS->current) {
  153. case EOZ:
  154. save(L, '\0', l);
  155. luaX_error(LS, "unfinished long string", TK_STRING);
  156. break; /* to avoid warnings */
  157. case '[':
  158. save_and_next(L, LS, l);
  159. if (LS->current == '[') {
  160. cont++;
  161. save_and_next(L, LS, l);
  162. }
  163. continue;
  164. case ']':
  165. save_and_next(L, LS, l);
  166. if (LS->current == ']') {
  167. if (cont == 0) goto endloop;
  168. cont--;
  169. save_and_next(L, LS, l);
  170. }
  171. continue;
  172. case '\n':
  173. save(L, '\n', l);
  174. inclinenumber(LS);
  175. continue;
  176. default:
  177. save_and_next(L, LS, l);
  178. }
  179. } endloop:
  180. save_and_next(L, LS, l); /* skip the second ']' */
  181. save(L, '\0', l);
  182. seminfo->ts = luaS_newlstr(L, G(L)->Mbuffer+2, l-5);
  183. }
  184. static void read_string (LexState *LS, int del, SemInfo *seminfo) {
  185. lua_State *L = LS->L;
  186. size_t l = 0;
  187. checkbuffer(L, 10, l);
  188. save_and_next(L, LS, l);
  189. while (LS->current != del) {
  190. checkbuffer(L, 10, l);
  191. switch (LS->current) {
  192. case EOZ: case '\n':
  193. save(L, '\0', l);
  194. luaX_error(LS, "unfinished string", TK_STRING);
  195. break; /* to avoid warnings */
  196. case '\\':
  197. next(LS); /* do not save the '\' */
  198. switch (LS->current) {
  199. case 'a': save(L, '\a', l); next(LS); break;
  200. case 'b': save(L, '\b', l); next(LS); break;
  201. case 'f': save(L, '\f', l); next(LS); break;
  202. case 'n': save(L, '\n', l); next(LS); break;
  203. case 'r': save(L, '\r', l); next(LS); break;
  204. case 't': save(L, '\t', l); next(LS); break;
  205. case 'v': save(L, '\v', l); next(LS); break;
  206. case '\n': save(L, '\n', l); inclinenumber(LS); break;
  207. case '0': case '1': case '2': case '3': case '4':
  208. case '5': case '6': case '7': case '8': case '9': {
  209. int c = 0;
  210. int i = 0;
  211. do {
  212. c = 10*c + (LS->current-'0');
  213. next(LS);
  214. } while (++i<3 && isdigit(LS->current));
  215. if (c != (unsigned char)c) {
  216. save(L, '\0', l);
  217. luaX_error(LS, "escape sequence too large", TK_STRING);
  218. }
  219. save(L, c, l);
  220. break;
  221. }
  222. default: /* handles \\, \", \', and \? */
  223. save_and_next(L, LS, l);
  224. }
  225. break;
  226. default:
  227. save_and_next(L, LS, l);
  228. }
  229. }
  230. save_and_next(L, LS, l); /* skip delimiter */
  231. save(L, '\0', l);
  232. seminfo->ts = luaS_newlstr(L, G(L)->Mbuffer+1, l-3);
  233. }
  234. int luaX_lex (LexState *LS, SemInfo *seminfo) {
  235. for (;;) {
  236. switch (LS->current) {
  237. case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
  238. next(LS);
  239. continue;
  240. case '\n':
  241. inclinenumber(LS);
  242. continue;
  243. case '$':
  244. luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$');
  245. break;
  246. case '-':
  247. next(LS);
  248. if (LS->current != '-') return '-';
  249. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  250. continue;
  251. case '[':
  252. next(LS);
  253. if (LS->current != '[') return '[';
  254. else {
  255. read_long_string(LS, seminfo);
  256. return TK_STRING;
  257. }
  258. case '=':
  259. next(LS);
  260. if (LS->current != '=') return '=';
  261. else { next(LS); return TK_EQ; }
  262. case '<':
  263. next(LS);
  264. if (LS->current != '=') return '<';
  265. else { next(LS); return TK_LE; }
  266. case '>':
  267. next(LS);
  268. if (LS->current != '=') return '>';
  269. else { next(LS); return TK_GE; }
  270. case '~':
  271. next(LS);
  272. if (LS->current != '=') return '~';
  273. else { next(LS); return TK_NE; }
  274. case '"':
  275. case '\'':
  276. read_string(LS, LS->current, seminfo);
  277. return TK_STRING;
  278. case '.':
  279. next(LS);
  280. if (LS->current == '.') {
  281. next(LS);
  282. if (LS->current == '.') {
  283. next(LS);
  284. return TK_DOTS; /* ... */
  285. }
  286. else return TK_CONCAT; /* .. */
  287. }
  288. else if (!isdigit(LS->current)) return '.';
  289. else {
  290. read_number(LS, 1, seminfo);
  291. return TK_NUMBER;
  292. }
  293. case '0': case '1': case '2': case '3': case '4':
  294. case '5': case '6': case '7': case '8': case '9':
  295. read_number(LS, 0, seminfo);
  296. return TK_NUMBER;
  297. case EOZ:
  298. return TK_EOS;
  299. case '_': goto tname;
  300. default:
  301. if (!isalpha(LS->current)) {
  302. int c = LS->current;
  303. if (iscntrl(c))
  304. luaX_invalidchar(LS, c);
  305. next(LS);
  306. return c;
  307. }
  308. tname: { /* identifier or reserved word */
  309. size_t l = readname(LS);
  310. TString *ts = luaS_newlstr(LS->L, G(LS->L)->Mbuffer, l);
  311. if (ts->marked >= RESERVEDMARK) /* reserved word? */
  312. return ts->marked-RESERVEDMARK+FIRST_RESERVED;
  313. seminfo->ts = ts;
  314. return TK_NAME;
  315. }
  316. }
  317. }
  318. }