2
0

llex.c 11 KB

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