llex.c 12 KB

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