llex.c 12 KB

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