llex.c 12 KB

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