llex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. ** $Id: llex.c,v 1.22 1998/06/19 18:47:06 roberto Exp roberto $
  3. ** Lexical Analizer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "lauxlib.h"
  9. #include "llex.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lparser.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "luadebug.h"
  16. #include "lzio.h"
  17. int lua_debug=0;
  18. #define next(LS) (LS->current = zgetc(LS->lex_z))
  19. #define save(c) luaL_addchar(c)
  20. #define save_and_next(LS) (save(LS->current), next(LS))
  21. char *reserved [] = {"and", "do", "else", "elseif", "end", "function",
  22. "if", "local", "nil", "not", "or", "repeat", "return", "then",
  23. "until", "while"};
  24. void luaX_init (void)
  25. {
  26. int i;
  27. for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
  28. TaggedString *ts = luaS_new(reserved[i]);
  29. ts->head.marked = FIRST_RESERVED+i; /* reserved word (always > 255) */
  30. }
  31. }
  32. void luaX_syntaxerror (LexState *ls, char *s, char *token) {
  33. if (token[0] == 0)
  34. token = "<eof>";
  35. luaL_verror("%.100s;\n last token read: `%.50s' at line %d in chunk `%.50s'",
  36. s, token, ls->linenumber, zname(ls->lex_z));
  37. }
  38. void luaX_error (LexState *ls, char *s) {
  39. save(0);
  40. luaX_syntaxerror(ls, s, luaL_buffer());
  41. }
  42. void luaX_token2str (LexState *ls, int token, char *s) {
  43. if (token < 255) {
  44. s[0] = token;
  45. s[1] = 0;
  46. }
  47. else
  48. strcpy(s, reserved[token-FIRST_RESERVED]);
  49. }
  50. static void luaX_invalidchar (LexState *ls, int c) {
  51. char buff[10];
  52. sprintf(buff, "0x%X", c);
  53. luaX_syntaxerror(ls, "invalid control char", buff);
  54. }
  55. static void firstline (LexState *LS)
  56. {
  57. int c = zgetc(LS->lex_z);
  58. if (c == '#')
  59. while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
  60. zungetc(LS->lex_z);
  61. }
  62. void luaX_setinput (LexState *LS, ZIO *z)
  63. {
  64. LS->current = '\n';
  65. LS->linenumber = 0;
  66. LS->iflevel = 0;
  67. LS->ifstate[0].skip = 0;
  68. LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
  69. LS->lex_z = z;
  70. LS->fs = NULL;
  71. firstline(LS);
  72. luaL_resetbuffer();
  73. }
  74. /*
  75. ** =======================================================
  76. ** PRAGMAS
  77. ** =======================================================
  78. */
  79. #define PRAGMASIZE 20
  80. static void skipspace (LexState *LS)
  81. {
  82. while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
  83. next(LS);
  84. }
  85. static int checkcond (LexState *LS, char *buff)
  86. {
  87. static char *opts[] = {"nil", "1", NULL};
  88. int i = luaL_findstring(buff, opts);
  89. if (i >= 0) return i;
  90. else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
  91. return luaS_globaldefined(buff);
  92. else {
  93. luaX_syntaxerror(LS, "invalid $if condition", buff);
  94. return 0; /* to avoid warnings */
  95. }
  96. }
  97. static void readname (LexState *LS, char *buff)
  98. {
  99. int i = 0;
  100. skipspace(LS);
  101. while (isalnum(LS->current) || LS->current == '_') {
  102. if (i >= PRAGMASIZE) {
  103. buff[PRAGMASIZE] = 0;
  104. luaX_syntaxerror(LS, "pragma too long", buff);
  105. }
  106. buff[i++] = LS->current;
  107. next(LS);
  108. }
  109. buff[i] = 0;
  110. }
  111. static void inclinenumber (LexState *LS);
  112. static void ifskip (LexState *LS)
  113. {
  114. while (LS->ifstate[LS->iflevel].skip) {
  115. if (LS->current == '\n')
  116. inclinenumber(LS);
  117. else if (LS->current == EOZ)
  118. luaX_error(LS, "input ends inside a $if");
  119. else next(LS);
  120. }
  121. }
  122. static void inclinenumber (LexState *LS)
  123. {
  124. static char *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) lua_debug = 1;
  137. break;
  138. case 1: /* nodebug */
  139. if (!skip) lua_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(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(LS);
  176. else if (LS->current != EOZ) /* or eof */
  177. luaX_syntaxerror(LS, "invalid pragma format", buff);
  178. ifskip(LS);
  179. }
  180. }
  181. /*
  182. ** =======================================================
  183. ** LEXICAL ANALIZER
  184. ** =======================================================
  185. */
  186. static int read_long_string (LexState *LS)
  187. {
  188. int cont = 0;
  189. while (1) {
  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(LS);
  196. if (LS->current == '[') {
  197. cont++;
  198. save_and_next(LS);
  199. }
  200. continue;
  201. case ']':
  202. save_and_next(LS);
  203. if (LS->current == ']') {
  204. if (cont == 0) goto endloop;
  205. cont--;
  206. save_and_next(LS);
  207. }
  208. continue;
  209. case '\n':
  210. save('\n');
  211. inclinenumber(LS);
  212. continue;
  213. default:
  214. save_and_next(LS);
  215. }
  216. } endloop:
  217. save_and_next(LS); /* pass the second ']' */
  218. LS->seminfo.ts = luaS_newlstr(L->Mbuffbase+2,
  219. L->Mbuffnext-(L->Mbuffbase-L->Mbuffer)-4);
  220. return STRING;
  221. }
  222. int luaX_lex (LexState *LS) {
  223. double a;
  224. luaL_resetbuffer();
  225. while (1) {
  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(LS);
  232. continue;
  233. case '-':
  234. save_and_next(LS);
  235. if (LS->current != '-') return '-';
  236. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  237. luaL_resetbuffer();
  238. continue;
  239. case '[':
  240. save_and_next(LS);
  241. if (LS->current != '[') return '[';
  242. else {
  243. save_and_next(LS); /* pass the second '[' */
  244. return read_long_string(LS);
  245. }
  246. case '=':
  247. save_and_next(LS);
  248. if (LS->current != '=') return '=';
  249. else { save_and_next(LS); return EQ; }
  250. case '<':
  251. save_and_next(LS);
  252. if (LS->current != '=') return '<';
  253. else { save_and_next(LS); return LE; }
  254. case '>':
  255. save_and_next(LS);
  256. if (LS->current != '=') return '>';
  257. else { save_and_next(LS); return GE; }
  258. case '~':
  259. save_and_next(LS);
  260. if (LS->current != '=') return '~';
  261. else { save_and_next(LS); return NE; }
  262. case '"':
  263. case '\'': {
  264. int del = LS->current;
  265. save_and_next(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('\a'); next(LS); break;
  276. case 'b': save('\b'); next(LS); break;
  277. case 'f': save('\f'); next(LS); break;
  278. case 'n': save('\n'); next(LS); break;
  279. case 'r': save('\r'); next(LS); break;
  280. case 't': save('\t'); next(LS); break;
  281. case 'v': save('\v'); next(LS); break;
  282. case '\n': save('\n'); inclinenumber(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 >= 256)
  292. luaX_error(LS, "escape sequence too large");
  293. save(c);
  294. }
  295. else { /* handles \, ", ', and ? */
  296. save(LS->current);
  297. next(LS);
  298. }
  299. break;
  300. }
  301. }
  302. break;
  303. default:
  304. save_and_next(LS);
  305. }
  306. }
  307. save_and_next(LS); /* skip delimiter */
  308. LS->seminfo.ts = luaS_newlstr(L->Mbuffbase+1,
  309. L->Mbuffnext-(L->Mbuffbase-L->Mbuffer)-2);
  310. return STRING;
  311. }
  312. case '.':
  313. save_and_next(LS);
  314. if (LS->current == '.')
  315. {
  316. save_and_next(LS);
  317. if (LS->current == '.')
  318. {
  319. save_and_next(LS);
  320. return DOTS; /* ... */
  321. }
  322. else return CONC; /* .. */
  323. }
  324. else if (!isdigit(LS->current)) return '.';
  325. /* LS->current is a digit: goes through to number */
  326. a=0.0;
  327. goto fraction;
  328. case '0': case '1': case '2': case '3': case '4':
  329. case '5': case '6': case '7': case '8': case '9':
  330. a=0.0;
  331. do {
  332. a = 10.0*a + (LS->current-'0');
  333. save_and_next(LS);
  334. } while (isdigit(LS->current));
  335. if (LS->current == '.') {
  336. save_and_next(LS);
  337. if (LS->current == '.') {
  338. save('.');
  339. luaX_error(LS,
  340. "ambiguous syntax (decimal point x string concatenation)");
  341. }
  342. }
  343. fraction:
  344. { double da=0.1;
  345. while (isdigit(LS->current))
  346. {
  347. a += (LS->current-'0')*da;
  348. da /= 10.0;
  349. save_and_next(LS);
  350. }
  351. if (toupper(LS->current) == 'E') {
  352. int e = 0;
  353. int neg;
  354. double ea;
  355. save_and_next(LS);
  356. neg = (LS->current=='-');
  357. if (LS->current == '+' || LS->current == '-') save_and_next(LS);
  358. if (!isdigit(LS->current))
  359. luaX_error(LS, "invalid numeral format");
  360. do {
  361. e = 10*e + (LS->current-'0');
  362. save_and_next(LS);
  363. } while (isdigit(LS->current));
  364. for (ea=neg?0.1:10.0; e>0; e>>=1)
  365. {
  366. if (e & 1) a *= ea;
  367. ea *= ea;
  368. }
  369. }
  370. LS->seminfo.r = a;
  371. return NUMBER;
  372. }
  373. case EOZ:
  374. if (LS->iflevel > 0)
  375. luaX_error(LS, "input ends inside a $if");
  376. return EOS;
  377. default:
  378. if (LS->current != '_' && !isalpha(LS->current)) {
  379. int c = LS->current;
  380. if (iscntrl(c))
  381. luaX_invalidchar(LS, c);
  382. save_and_next(LS);
  383. return c;
  384. }
  385. else { /* identifier or reserved word */
  386. TaggedString *ts;
  387. do {
  388. save_and_next(LS);
  389. } while (isalnum(LS->current) || LS->current == '_');
  390. save(0);
  391. ts = luaS_new(L->Mbuffbase);
  392. if (ts->head.marked >= 'A')
  393. return ts->head.marked; /* reserved word */
  394. LS->seminfo.ts = ts;
  395. return NAME;
  396. }
  397. }
  398. }
  399. }