2
0

llex.c 9.3 KB

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