llex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. ** $Id: llex.c,v 1.9 1997/12/02 12:43:54 roberto Exp roberto $
  3. ** Lexical Analizer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "llex.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lparser.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #include "lstx.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. static struct {
  20. char *name;
  21. int token;
  22. } reserved [] = {
  23. {"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
  24. {"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
  25. {"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
  26. {"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
  27. };
  28. void luaX_init (void)
  29. {
  30. int i;
  31. for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
  32. TaggedString *ts = luaS_new(reserved[i].name);
  33. ts->head.marked = reserved[i].token; /* reserved word (always > 255) */
  34. }
  35. }
  36. static void firstline (LexState *LS)
  37. {
  38. int c = zgetc(LS->lex_z);
  39. if (c == '#')
  40. while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
  41. zungetc(LS->lex_z);
  42. }
  43. void luaX_setinput (ZIO *z)
  44. {
  45. LexState *LS = L->lexstate;
  46. LS->current = '\n';
  47. LS->linelasttoken = 0;
  48. LS->linenumber = 0;
  49. LS->iflevel = 0;
  50. LS->ifstate[0].skip = 0;
  51. LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
  52. LS->lex_z = z;
  53. firstline(LS);
  54. LS->textbuff.buffsize = 20;
  55. LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize);
  56. }
  57. /*
  58. ** =======================================================
  59. ** PRAGMAS
  60. ** =======================================================
  61. */
  62. #define PRAGMASIZE 20
  63. static void skipspace (LexState *LS)
  64. {
  65. while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
  66. next(LS);
  67. }
  68. static int checkcond (char *buff)
  69. {
  70. static char *opts[] = {"nil", "1", NULL};
  71. int i = luaO_findstring(buff, opts);
  72. if (i >= 0) return i;
  73. else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
  74. return luaS_globaldefined(buff);
  75. else {
  76. luaY_syntaxerror("invalid $if condition", buff);
  77. return 0; /* to avoid warnings */
  78. }
  79. }
  80. static void readname (LexState *LS, char *buff)
  81. {
  82. int i = 0;
  83. skipspace(LS);
  84. while (isalnum(LS->current) || LS->current == '_') {
  85. if (i >= PRAGMASIZE) {
  86. buff[PRAGMASIZE] = 0;
  87. luaY_syntaxerror("pragma too long", buff);
  88. }
  89. buff[i++] = LS->current;
  90. next(LS);
  91. }
  92. buff[i] = 0;
  93. }
  94. static void inclinenumber (LexState *LS);
  95. static void ifskip (LexState *LS)
  96. {
  97. while (LS->ifstate[LS->iflevel].skip) {
  98. if (LS->current == '\n')
  99. inclinenumber(LS);
  100. else if (LS->current == EOZ)
  101. luaY_syntaxerror("input ends inside a $if", "");
  102. else next(LS);
  103. }
  104. }
  105. static void inclinenumber (LexState *LS)
  106. {
  107. static char *pragmas [] =
  108. {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
  109. next(LS); /* skip '\n' */
  110. ++LS->linenumber;
  111. if (LS->current == '$') { /* is a pragma? */
  112. char buff[PRAGMASIZE+1];
  113. int ifnot = 0;
  114. int skip = LS->ifstate[LS->iflevel].skip;
  115. next(LS); /* skip $ */
  116. readname(LS, buff);
  117. switch (luaO_findstring(buff, pragmas)) {
  118. case 0: /* debug */
  119. if (!skip) lua_debug = 1;
  120. break;
  121. case 1: /* nodebug */
  122. if (!skip) lua_debug = 0;
  123. break;
  124. case 2: /* endinput */
  125. if (!skip) {
  126. LS->current = EOZ;
  127. LS->iflevel = 0; /* to allow $endinput inside a $if */
  128. }
  129. break;
  130. case 3: /* end */
  131. if (LS->iflevel-- == 0)
  132. luaY_syntaxerror("unmatched $end", "$end");
  133. break;
  134. case 4: /* ifnot */
  135. ifnot = 1;
  136. /* go through */
  137. case 5: /* if */
  138. if (LS->iflevel == MAX_IFS-1)
  139. luaY_syntaxerror("too many nested $ifs", "$if");
  140. readname(LS, buff);
  141. LS->iflevel++;
  142. LS->ifstate[LS->iflevel].elsepart = 0;
  143. LS->ifstate[LS->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
  144. LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
  145. break;
  146. case 6: /* else */
  147. if (LS->ifstate[LS->iflevel].elsepart)
  148. luaY_syntaxerror("unmatched $else", "$else");
  149. LS->ifstate[LS->iflevel].elsepart = 1;
  150. LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
  151. LS->ifstate[LS->iflevel].condition;
  152. break;
  153. default:
  154. luaY_syntaxerror("unknown pragma", buff);
  155. }
  156. skipspace(LS);
  157. if (LS->current == '\n') /* pragma must end with a '\n' ... */
  158. inclinenumber(LS);
  159. else if (LS->current != EOZ) /* or eof */
  160. luaY_syntaxerror("invalid pragma format", buff);
  161. ifskip(LS);
  162. }
  163. }
  164. /*
  165. ** =======================================================
  166. ** LEXICAL ANALIZER
  167. ** =======================================================
  168. */
  169. static void save (LexState *LS, int c)
  170. {
  171. if (LS->textbuff.tokensize >= LS->textbuff.buffsize)
  172. LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize *= 2);
  173. LS->textbuff.text[LS->textbuff.tokensize++] = c;
  174. }
  175. char *luaX_lasttoken (void)
  176. {
  177. save(L->lexstate, 0);
  178. return L->lexstate->textbuff.text;
  179. }
  180. #define save_and_next(LS) (save(LS, LS->current), next(LS))
  181. static int read_long_string (LexState *LS, YYSTYPE *l)
  182. {
  183. int cont = 0;
  184. while (1) {
  185. switch (LS->current) {
  186. case EOZ:
  187. save(LS, 0);
  188. return WRONGTOKEN;
  189. case '[':
  190. save_and_next(LS);
  191. if (LS->current == '[') {
  192. cont++;
  193. save_and_next(LS);
  194. }
  195. continue;
  196. case ']':
  197. save_and_next(LS);
  198. if (LS->current == ']') {
  199. if (cont == 0) goto endloop;
  200. cont--;
  201. save_and_next(LS);
  202. }
  203. continue;
  204. case '\n':
  205. save(LS, '\n');
  206. inclinenumber(LS);
  207. continue;
  208. default:
  209. save_and_next(LS);
  210. }
  211. } endloop:
  212. save_and_next(LS); /* pass the second ']' */
  213. LS->textbuff.text[LS->textbuff.tokensize-2] = 0; /* erases ']]' */
  214. l->pTStr = luaS_new(LS->textbuff.text+2);
  215. LS->textbuff.text[LS->textbuff.tokensize-2] = ']'; /* restores ']]' */
  216. return STRING;
  217. }
  218. /* to avoid warnings; this declaration cannot be public since YYSTYPE
  219. ** cannot be visible in llex.h (otherwise there is an error, since
  220. ** the parser body redefines it!)
  221. */
  222. int luaY_lex (YYSTYPE *l);
  223. int luaY_lex (YYSTYPE *l)
  224. {
  225. LexState *LS = L->lexstate;
  226. double a;
  227. LS->textbuff.tokensize = 0;
  228. if (lua_debug)
  229. luaY_codedebugline(LS->linelasttoken);
  230. LS->linelasttoken = LS->linenumber;
  231. while (1) {
  232. switch (LS->current) {
  233. case '\n':
  234. inclinenumber(LS);
  235. LS->linelasttoken = LS->linenumber;
  236. continue;
  237. case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
  238. next(LS);
  239. continue;
  240. case '-':
  241. save_and_next(LS);
  242. if (LS->current != '-') return '-';
  243. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  244. LS->textbuff.tokensize = 0;
  245. continue;
  246. case '[':
  247. save_and_next(LS);
  248. if (LS->current != '[') return '[';
  249. else {
  250. save_and_next(LS); /* pass the second '[' */
  251. return read_long_string(LS, l);
  252. }
  253. case '=':
  254. save_and_next(LS);
  255. if (LS->current != '=') return '=';
  256. else { save_and_next(LS); return EQ; }
  257. case '<':
  258. save_and_next(LS);
  259. if (LS->current != '=') return '<';
  260. else { save_and_next(LS); return LE; }
  261. case '>':
  262. save_and_next(LS);
  263. if (LS->current != '=') return '>';
  264. else { save_and_next(LS); return GE; }
  265. case '~':
  266. save_and_next(LS);
  267. if (LS->current != '=') return '~';
  268. else { save_and_next(LS); return NE; }
  269. case '"':
  270. case '\'': {
  271. int del = LS->current;
  272. save_and_next(LS);
  273. while (LS->current != del) {
  274. switch (LS->current) {
  275. case EOZ:
  276. case '\n':
  277. save(LS, 0);
  278. return WRONGTOKEN;
  279. case '\\':
  280. next(LS); /* do not save the '\' */
  281. switch (LS->current) {
  282. case 'n': save(LS, '\n'); next(LS); break;
  283. case 't': save(LS, '\t'); next(LS); break;
  284. case 'r': save(LS, '\r'); next(LS); break;
  285. case '\n': save(LS, '\n'); inclinenumber(LS); break;
  286. default : save_and_next(LS); break;
  287. }
  288. break;
  289. default:
  290. save_and_next(LS);
  291. }
  292. }
  293. next(LS); /* skip delimiter */
  294. save(LS, 0);
  295. l->pTStr = luaS_new(LS->textbuff.text+1);
  296. LS->textbuff.text[LS->textbuff.tokensize-1] = del; /* restore delimiter */
  297. return STRING;
  298. }
  299. case '.':
  300. save_and_next(LS);
  301. if (LS->current == '.')
  302. {
  303. save_and_next(LS);
  304. if (LS->current == '.')
  305. {
  306. save_and_next(LS);
  307. return DOTS; /* ... */
  308. }
  309. else return CONC; /* .. */
  310. }
  311. else if (!isdigit(LS->current)) return '.';
  312. /* LS->current is a digit: goes through to number */
  313. a=0.0;
  314. goto fraction;
  315. case '0': case '1': case '2': case '3': case '4':
  316. case '5': case '6': case '7': case '8': case '9':
  317. a=0.0;
  318. do {
  319. a=10.0*a+(LS->current-'0');
  320. save_and_next(LS);
  321. } while (isdigit(LS->current));
  322. if (LS->current == '.') {
  323. save_and_next(LS);
  324. if (LS->current == '.') {
  325. save(LS, 0);
  326. luaY_error(
  327. "ambiguous syntax (decimal point x string concatenation)");
  328. }
  329. }
  330. fraction:
  331. { double da=0.1;
  332. while (isdigit(LS->current))
  333. {
  334. a+=(LS->current-'0')*da;
  335. da/=10.0;
  336. save_and_next(LS);
  337. }
  338. if (toupper(LS->current) == 'E') {
  339. int e=0;
  340. int neg;
  341. double ea;
  342. save_and_next(LS);
  343. neg=(LS->current=='-');
  344. if (LS->current == '+' || LS->current == '-') save_and_next(LS);
  345. if (!isdigit(LS->current)) {
  346. save(LS, 0); return WRONGTOKEN; }
  347. do {
  348. e=10.0*e+(LS->current-'0');
  349. save_and_next(LS);
  350. } while (isdigit(LS->current));
  351. for (ea=neg?0.1:10.0; e>0; e>>=1)
  352. {
  353. if (e & 1) a*=ea;
  354. ea*=ea;
  355. }
  356. }
  357. l->vReal = a;
  358. return NUMBER;
  359. }
  360. case EOZ:
  361. save(LS, 0);
  362. if (LS->iflevel > 0)
  363. luaY_syntaxerror("input ends inside a $if", "");
  364. return 0;
  365. default:
  366. if (LS->current != '_' && !isalpha(LS->current)) {
  367. save_and_next(LS);
  368. return LS->textbuff.text[0];
  369. }
  370. else { /* identifier or reserved word */
  371. TaggedString *ts;
  372. do {
  373. save_and_next(LS);
  374. } while (isalnum(LS->current) || LS->current == '_');
  375. save(LS, 0);
  376. ts = luaS_new(LS->textbuff.text);
  377. if (ts->head.marked > 255)
  378. return ts->head.marked; /* reserved word */
  379. l->pTStr = ts;
  380. return NAME;
  381. }
  382. }
  383. }
  384. }