llex.c 12 KB

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