llex.c 12 KB

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