llex.c 8.9 KB

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