llex.c 12 KB

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