llex.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. ** $Id: llex.c,v 1.66 2000/08/08 20:42:07 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. static void checkpragma (lua_State *L, LexState *LS) {
  86. static const char *const pragmas [] = {"debug", "nodebug", NULL};
  87. if (LS->current == '$') { /* is a pragma? */
  88. switch (luaL_findstring(readname(L, LS)+1, pragmas)) {
  89. case 0: /* debug */
  90. case 1: /* nodebug */
  91. break;
  92. default:
  93. luaX_error(LS, "unknown pragma", TK_STRING);
  94. }
  95. }
  96. }
  97. void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
  98. LS->L = L;
  99. LS->lookahead.token = TK_EOS; /* no look-ahead token */
  100. LS->z = z;
  101. LS->fs = NULL;
  102. LS->linenumber = 1;
  103. LS->lastline = 1;
  104. LS->source = source;
  105. next(LS); /* read first char */
  106. if (LS->current == '#') {
  107. do { /* skip first line */
  108. next(LS);
  109. } while (LS->current != '\n' && LS->current != EOZ);
  110. }
  111. else checkpragma(L, LS);
  112. }
  113. /*
  114. ** =======================================================
  115. ** LEXICAL ANALYZER
  116. ** =======================================================
  117. */
  118. static void read_long_string (lua_State *L, LexState *LS) {
  119. int cont = 0;
  120. for (;;) {
  121. switch (LS->current) {
  122. case EOZ:
  123. luaX_error(LS, "unfinished long string", TK_STRING);
  124. break; /* to avoid warnings */
  125. case '[':
  126. save_and_next(L, LS);
  127. if (LS->current == '[') {
  128. cont++;
  129. save_and_next(L, LS);
  130. }
  131. continue;
  132. case ']':
  133. save_and_next(L, LS);
  134. if (LS->current == ']') {
  135. if (cont == 0) goto endloop;
  136. cont--;
  137. save_and_next(L, LS);
  138. }
  139. continue;
  140. case '\n':
  141. save(L, '\n');
  142. inclinenumber(LS);
  143. continue;
  144. default:
  145. save_and_next(L, LS);
  146. }
  147. } endloop:
  148. save_and_next(L, LS); /* skip the second ']' */
  149. LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+2),
  150. L->Mbuffnext-L->Mbuffbase-4);
  151. }
  152. static void read_string (lua_State *L, LexState *LS, int del) {
  153. save_and_next(L, LS);
  154. while (LS->current != del) {
  155. switch (LS->current) {
  156. case EOZ: case '\n':
  157. luaX_error(LS, "unfinished string", TK_STRING);
  158. break; /* to avoid warnings */
  159. case '\\':
  160. next(LS); /* do not save the '\' */
  161. switch (LS->current) {
  162. case 'a': save(L, '\a'); next(LS); break;
  163. case 'b': save(L, '\b'); next(LS); break;
  164. case 'f': save(L, '\f'); next(LS); break;
  165. case 'n': save(L, '\n'); next(LS); break;
  166. case 'r': save(L, '\r'); next(LS); break;
  167. case 't': save(L, '\t'); next(LS); break;
  168. case 'v': save(L, '\v'); next(LS); break;
  169. case '\n': save(L, '\n'); inclinenumber(LS); break;
  170. case '0': case '1': case '2': case '3': case '4':
  171. case '5': case '6': case '7': case '8': case '9': {
  172. int c = 0;
  173. int i = 0;
  174. do {
  175. c = 10*c + (LS->current-'0');
  176. next(LS);
  177. } while (++i<3 && isdigit(LS->current));
  178. if (c != (unsigned char)c)
  179. luaX_error(LS, "escape sequence too large", TK_STRING);
  180. save(L, c);
  181. break;
  182. }
  183. default: /* handles \\, \", \', and \? */
  184. save(L, LS->current);
  185. next(LS);
  186. }
  187. break;
  188. default:
  189. save_and_next(L, LS);
  190. }
  191. }
  192. save_and_next(L, LS); /* skip delimiter */
  193. LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+1),
  194. L->Mbuffnext-L->Mbuffbase-2);
  195. }
  196. int luaX_lex (LexState *LS) {
  197. lua_State *L = LS->L;
  198. for (;;) {
  199. switch (LS->current) {
  200. case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
  201. next(LS);
  202. continue;
  203. case '\n':
  204. inclinenumber(LS);
  205. checkpragma(L, LS);
  206. continue;
  207. case '-':
  208. next(LS);
  209. if (LS->current != '-') return '-';
  210. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  211. continue;
  212. case '[':
  213. luaL_resetbuffer(L);
  214. save_and_next(L, LS);
  215. if (LS->current != '[') return '[';
  216. else {
  217. save_and_next(L, LS); /* pass the second '[' */
  218. read_long_string(L, LS);
  219. return TK_STRING;
  220. }
  221. case '=':
  222. next(LS);
  223. if (LS->current != '=') return '=';
  224. else { next(LS); return TK_EQ; }
  225. case '<':
  226. next(LS);
  227. if (LS->current != '=') return '<';
  228. else { next(LS); return TK_LE; }
  229. case '>':
  230. next(LS);
  231. if (LS->current != '=') return '>';
  232. else { next(LS); return TK_GE; }
  233. case '~':
  234. next(LS);
  235. if (LS->current != '=') return '~';
  236. else { next(LS); return TK_NE; }
  237. case '"':
  238. case '\'':
  239. luaL_resetbuffer(L);
  240. read_string(L, LS, LS->current);
  241. return TK_STRING;
  242. case '.':
  243. luaL_resetbuffer(L);
  244. save_and_next(L, LS);
  245. if (LS->current == '.') {
  246. next(LS);
  247. if (LS->current == '.') {
  248. next(LS);
  249. return TK_DOTS; /* ... */
  250. }
  251. else return TK_CONCAT; /* .. */
  252. }
  253. else if (!isdigit(LS->current)) return '.';
  254. else goto fraction; /* LS->current is a digit */
  255. case '0': case '1': case '2': case '3': case '4':
  256. case '5': case '6': case '7': case '8': case '9':
  257. luaL_resetbuffer(L);
  258. do {
  259. save_and_next(L, LS);
  260. } while (isdigit(LS->current));
  261. if (LS->current == '.') {
  262. save_and_next(L, LS);
  263. if (LS->current == '.') {
  264. save(L, '.');
  265. luaX_error(LS, "ambiguous syntax"
  266. " (decimal point x string concatenation)", TK_NUMBER);
  267. }
  268. }
  269. fraction: /* LUA_NUMBER */
  270. while (isdigit(LS->current))
  271. save_and_next(L, LS);
  272. if (LS->current == 'e' || LS->current == 'E') {
  273. save_and_next(L, LS); /* read 'E' */
  274. if (LS->current == '+' || LS->current == '-')
  275. save_and_next(L, LS); /* optional exponent sign */
  276. while (isdigit(LS->current))
  277. save_and_next(L, LS);
  278. }
  279. save(L, '\0');
  280. if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->t.seminfo.r))
  281. luaX_error(LS, "malformed number", TK_NUMBER);
  282. return TK_NUMBER;
  283. case EOZ:
  284. return TK_EOS;
  285. case '_': goto tname;
  286. default:
  287. if (!isalpha(LS->current)) {
  288. int c = LS->current;
  289. if (iscntrl(c))
  290. luaX_invalidchar(LS, c);
  291. next(LS);
  292. return c;
  293. }
  294. tname: { /* identifier or reserved word */
  295. TString *ts = luaS_new(L, readname(L, LS));
  296. if (ts->marked >= RESERVEDMARK) /* reserved word? */
  297. return ts->marked-RESERVEDMARK+FIRST_RESERVED;
  298. LS->t.seminfo.ts = ts;
  299. return TK_NAME;
  300. }
  301. }
  302. }
  303. }