llex.c 12 KB

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