llex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. ** $Id: llex.c,v 1.58 2000/05/08 19:32:53 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 "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->token);
  41. }
  42. }
  43. void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
  44. char buff[MAXSRC];
  45. luaL_chunkid(buff, zname(ls->z), 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 void firstline (LexState *LS)
  73. {
  74. int c = zgetc(LS->z);
  75. if (c == '#')
  76. while ((c=zgetc(LS->z)) != '\n' && c != EOZ) /* skip first line */;
  77. zungetc(LS->z);
  78. }
  79. void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) {
  80. LS->L = L;
  81. LS->current = '\n';
  82. LS->linenumber = 0;
  83. LS->iflevel = 0;
  84. LS->ifstate[0].skip = 0;
  85. LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
  86. LS->z = z;
  87. LS->fs = NULL;
  88. firstline(LS);
  89. luaL_resetbuffer(L);
  90. }
  91. /*
  92. ** =======================================================
  93. ** PRAGMAS
  94. ** =======================================================
  95. */
  96. static void skipspace (LexState *LS) {
  97. while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
  98. next(LS);
  99. }
  100. static int globaldefined (lua_State *L, const char *name) {
  101. const TObject *value = luaH_getglobal(L, name);
  102. return ttype(value) != TAG_NIL;
  103. }
  104. static int checkcond (lua_State *L, LexState *LS, const char *buff) {
  105. static const char *const opts[] = {"nil", "1", NULL};
  106. int i = luaL_findstring(buff, opts);
  107. if (i >= 0) return i;
  108. else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
  109. return globaldefined(L, buff);
  110. else {
  111. luaX_syntaxerror(LS, "invalid $if condition", buff);
  112. return 0; /* to avoid warnings */
  113. }
  114. }
  115. static void readname (LexState *LS, char *buff) {
  116. int i = 0;
  117. skipspace(LS);
  118. while (isalnum(LS->current) || LS->current == '_') {
  119. if (i >= PRAGMASIZE) {
  120. buff[PRAGMASIZE] = 0;
  121. luaX_syntaxerror(LS, "pragma too long", buff);
  122. }
  123. buff[i++] = (char)LS->current;
  124. next(LS);
  125. }
  126. buff[i] = 0;
  127. }
  128. static void inclinenumber (lua_State *L, LexState *LS);
  129. static void ifskip (lua_State *L, LexState *LS) {
  130. while (LS->ifstate[LS->iflevel].skip) {
  131. if (LS->current == '\n')
  132. inclinenumber(L, LS);
  133. else if (LS->current == EOZ)
  134. luaX_error(LS, "input ends inside a $if", TK_EOS);
  135. else next(LS);
  136. }
  137. }
  138. static void inclinenumber (lua_State *L, LexState *LS) {
  139. static const char *const pragmas [] =
  140. {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
  141. next(LS); /* skip '\n' */
  142. ++LS->linenumber;
  143. luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
  144. if (LS->current == '$') { /* is a pragma? */
  145. char buff[PRAGMASIZE+1];
  146. int ifnot = 0;
  147. int skip = LS->ifstate[LS->iflevel].skip;
  148. next(LS); /* skip $ */
  149. readname(LS, buff);
  150. switch (luaL_findstring(buff, pragmas)) {
  151. case 0: /* debug */
  152. if (!skip) L->debug = 1;
  153. break;
  154. case 1: /* nodebug */
  155. if (!skip) L->debug = 0;
  156. break;
  157. case 2: /* endinput */
  158. if (!skip) {
  159. LS->current = EOZ;
  160. LS->iflevel = 0; /* to allow $endinput inside a $if */
  161. }
  162. break;
  163. case 3: /* end */
  164. if (LS->iflevel-- == 0)
  165. luaX_syntaxerror(LS, "unmatched $end", "$end");
  166. break;
  167. case 4: /* ifnot */
  168. ifnot = 1;
  169. /* go through */
  170. case 5: /* if */
  171. if (LS->iflevel == MAX_IFS-1)
  172. luaX_syntaxerror(LS, "too many nested $ifs", "$if");
  173. readname(LS, buff);
  174. LS->iflevel++;
  175. LS->ifstate[LS->iflevel].elsepart = 0;
  176. LS->ifstate[LS->iflevel].condition = checkcond(L, LS, buff) ? !ifnot : ifnot;
  177. LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
  178. break;
  179. case 6: /* else */
  180. if (LS->ifstate[LS->iflevel].elsepart)
  181. luaX_syntaxerror(LS, "unmatched $else", "$else");
  182. LS->ifstate[LS->iflevel].elsepart = 1;
  183. LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
  184. LS->ifstate[LS->iflevel].condition;
  185. break;
  186. default:
  187. luaX_syntaxerror(LS, "unknown pragma", buff);
  188. }
  189. skipspace(LS);
  190. if (LS->current == '\n') /* pragma must end with a '\n' ... */
  191. inclinenumber(L, LS);
  192. else if (LS->current != EOZ) /* or eof */
  193. luaX_syntaxerror(LS, "invalid pragma format", buff);
  194. ifskip(L, LS);
  195. }
  196. }
  197. /*
  198. ** =======================================================
  199. ** LEXICAL ANALYZER
  200. ** =======================================================
  201. */
  202. static void read_long_string (lua_State *L, LexState *LS) {
  203. int cont = 0;
  204. for (;;) {
  205. switch (LS->current) {
  206. case EOZ:
  207. luaX_error(LS, "unfinished long string", TK_STRING);
  208. break; /* to avoid warnings */
  209. case '[':
  210. save_and_next(L, LS);
  211. if (LS->current == '[') {
  212. cont++;
  213. save_and_next(L, LS);
  214. }
  215. continue;
  216. case ']':
  217. save_and_next(L, LS);
  218. if (LS->current == ']') {
  219. if (cont == 0) goto endloop;
  220. cont--;
  221. save_and_next(L, LS);
  222. }
  223. continue;
  224. case '\n':
  225. save(L, '\n');
  226. inclinenumber(L, LS);
  227. continue;
  228. default:
  229. save_and_next(L, LS);
  230. }
  231. } endloop:
  232. save_and_next(L, LS); /* skip the second ']' */
  233. LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+2),
  234. L->Mbuffnext-L->Mbuffbase-4);
  235. }
  236. static void read_string (lua_State *L, LexState *LS, int del) {
  237. save_and_next(L, LS);
  238. while (LS->current != del) {
  239. switch (LS->current) {
  240. case EOZ: case '\n':
  241. luaX_error(LS, "unfinished string", TK_STRING);
  242. break; /* to avoid warnings */
  243. case '\\':
  244. next(LS); /* do not save the '\' */
  245. switch (LS->current) {
  246. case 'a': save(L, '\a'); next(LS); break;
  247. case 'b': save(L, '\b'); next(LS); break;
  248. case 'f': save(L, '\f'); next(LS); break;
  249. case 'n': save(L, '\n'); next(LS); break;
  250. case 'r': save(L, '\r'); next(LS); break;
  251. case 't': save(L, '\t'); next(LS); break;
  252. case 'v': save(L, '\v'); next(LS); break;
  253. case '\n': save(L, '\n'); inclinenumber(L, LS); break;
  254. case '0': case '1': case '2': case '3': case '4':
  255. case '5': case '6': case '7': case '8': case '9': {
  256. int c = 0;
  257. int i = 0;
  258. do {
  259. c = 10*c + (LS->current-'0');
  260. next(LS);
  261. } while (++i<3 && isdigit(LS->current));
  262. if (c != (unsigned char)c)
  263. luaX_error(LS, "escape sequence too large", TK_STRING);
  264. save(L, c);
  265. break;
  266. }
  267. default: /* handles \\, \", \', and \? */
  268. save(L, LS->current);
  269. next(LS);
  270. }
  271. break;
  272. default:
  273. save_and_next(L, LS);
  274. }
  275. }
  276. save_and_next(L, LS); /* skip delimiter */
  277. LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+1),
  278. L->Mbuffnext-L->Mbuffbase-2);
  279. }
  280. int luaX_lex (LexState *LS) {
  281. lua_State *L = LS->L;
  282. for (;;) {
  283. switch (LS->current) {
  284. case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
  285. next(LS);
  286. continue;
  287. case '\n':
  288. inclinenumber(L, LS);
  289. continue;
  290. case '-':
  291. next(LS);
  292. if (LS->current != '-') return '-';
  293. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  294. continue;
  295. case '[':
  296. luaL_resetbuffer(L);
  297. save_and_next(L, LS);
  298. if (LS->current != '[') return '[';
  299. else {
  300. save_and_next(L, LS); /* pass the second '[' */
  301. read_long_string(L, LS);
  302. return TK_STRING;
  303. }
  304. case '=':
  305. next(LS);
  306. if (LS->current != '=') return '=';
  307. else { next(LS); return TK_EQ; }
  308. case '<':
  309. next(LS);
  310. if (LS->current != '=') return '<';
  311. else { next(LS); return TK_LE; }
  312. case '>':
  313. next(LS);
  314. if (LS->current != '=') return '>';
  315. else { next(LS); return TK_GE; }
  316. case '~':
  317. next(LS);
  318. if (LS->current != '=') return '~';
  319. else { next(LS); return TK_NE; }
  320. case '"':
  321. case '\'':
  322. luaL_resetbuffer(L);
  323. read_string(L, LS, LS->current);
  324. return TK_STRING;
  325. case '.':
  326. luaL_resetbuffer(L);
  327. save_and_next(L, LS);
  328. if (LS->current == '.') {
  329. next(LS);
  330. if (LS->current == '.') {
  331. next(LS);
  332. return TK_DOTS; /* ... */
  333. }
  334. else return TK_CONCAT; /* .. */
  335. }
  336. else if (!isdigit(LS->current)) return '.';
  337. else goto fraction; /* LS->current is a digit */
  338. case '0': case '1': case '2': case '3': case '4':
  339. case '5': case '6': case '7': case '8': case '9':
  340. luaL_resetbuffer(L);
  341. do {
  342. save_and_next(L, LS);
  343. } while (isdigit(LS->current));
  344. if (LS->current == '.') {
  345. save_and_next(L, LS);
  346. if (LS->current == '.') {
  347. save(L, '.');
  348. luaX_error(LS, "ambiguous syntax"
  349. " (decimal point x string concatenation)", TK_NUMBER);
  350. }
  351. }
  352. fraction: /* LUA_NUMBER */
  353. while (isdigit(LS->current))
  354. save_and_next(L, LS);
  355. if (LS->current == 'e' || LS->current == 'E') {
  356. save_and_next(L, LS); /* read 'E' */
  357. if (LS->current == '+' || LS->current == '-')
  358. save_and_next(L, LS); /* optional exponent sign */
  359. while (isdigit(LS->current))
  360. save_and_next(L, LS);
  361. }
  362. save(L, '\0');
  363. if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r))
  364. luaX_error(LS, "malformed number", TK_NUMBER);
  365. return TK_NUMBER;
  366. case EOZ:
  367. if (LS->iflevel > 0)
  368. luaX_error(LS, "input ends inside a $if", TK_EOS);
  369. return TK_EOS;
  370. case '_': goto tname;
  371. default:
  372. if (!isalpha(LS->current)) {
  373. int c = LS->current;
  374. if (iscntrl(c))
  375. luaX_invalidchar(LS, c);
  376. next(LS);
  377. return c;
  378. }
  379. tname: { /* identifier or reserved word */
  380. TString *ts;
  381. luaL_resetbuffer(L);
  382. do {
  383. save_and_next(L, LS);
  384. } while (isalnum(LS->current) || LS->current == '_');
  385. save(L, '\0');
  386. ts = luaS_new(L, L->Mbuffer+L->Mbuffbase);
  387. if (ts->marked >= RESERVEDMARK) /* reserved word? */
  388. return ts->marked-RESERVEDMARK+FIRST_RESERVED;
  389. LS->seminfo.ts = ts;
  390. return TK_NAME;
  391. }
  392. }
  393. }
  394. }