lex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. char *rcs_lex = "$Id: lex.c,v 3.8 1997/07/30 22:00:50 roberto Exp roberto $";
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include "auxlib.h"
  5. #include "luamem.h"
  6. #include "tree.h"
  7. #include "table.h"
  8. #include "lex.h"
  9. #include "inout.h"
  10. #include "luadebug.h"
  11. #include "parser.h"
  12. #define MINBUFF 250
  13. static int current; /* look ahead character */
  14. static ZIO *lex_z;
  15. #define next() (current = zgetc(lex_z))
  16. #define save(x) (yytext[tokensize++] = (x))
  17. #define save_and_next() (save(current), next())
  18. #define MAX_IFS 5
  19. /* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
  20. static struct {
  21. int elsepart; /* true if its in the $else part */
  22. int condition; /* true if $if condition is true */
  23. int skip; /* true if part must be skiped */
  24. } ifstate[MAX_IFS];
  25. static int iflevel; /* level of nested $if's */
  26. static void firstline (void)
  27. {
  28. int c = zgetc(lex_z);
  29. if (c == '#')
  30. while((c=zgetc(lex_z)) != '\n' && c != EOZ) /* skip first line */;
  31. zungetc(lex_z);
  32. }
  33. void lua_setinput (ZIO *z)
  34. {
  35. current = '\n';
  36. lua_linenumber = 0;
  37. iflevel = 0;
  38. ifstate[0].skip = 0;
  39. ifstate[0].elsepart = 1; /* to avoid a free $else */
  40. lex_z = z;
  41. firstline();
  42. }
  43. static void luaI_auxsyntaxerror (char *s)
  44. {
  45. luaL_verror("%s;\n> at line %d in file %s",
  46. s, lua_linenumber, lua_parsedfile->str);
  47. }
  48. static void luaI_auxsynterrbf (char *s, char *token)
  49. {
  50. if (token[0] == 0)
  51. token = "<eof>";
  52. luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
  53. s, token, lua_linenumber, lua_parsedfile->str);
  54. }
  55. void luaI_syntaxerror (char *s)
  56. {
  57. luaI_auxsynterrbf(s, luaI_buffer(1));
  58. }
  59. static struct
  60. {
  61. char *name;
  62. int token;
  63. } reserved [] = {
  64. {"and", AND},
  65. {"do", DO},
  66. {"else", ELSE},
  67. {"elseif", ELSEIF},
  68. {"end", END},
  69. {"function", FUNCTION},
  70. {"if", IF},
  71. {"local", LOCAL},
  72. {"nil", NIL},
  73. {"not", NOT},
  74. {"or", OR},
  75. {"repeat", REPEAT},
  76. {"return", RETURN},
  77. {"then", THEN},
  78. {"until", UNTIL},
  79. {"while", WHILE} };
  80. #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
  81. void luaI_addReserved (void)
  82. {
  83. int i;
  84. for (i=0; i<RESERVEDSIZE; i++)
  85. {
  86. TaggedString *ts = luaI_createstring(reserved[i].name);
  87. ts->marked = reserved[i].token; /* reserved word (always > 255) */
  88. }
  89. }
  90. /*
  91. ** Pragma handling
  92. */
  93. #define PRAGMASIZE 20
  94. static void skipspace (void)
  95. {
  96. while (current == ' ' || current == '\t') next();
  97. }
  98. static int checkcond (char *buff)
  99. {
  100. static char *opts[] = {"nil", "1"};
  101. int i = luaI_findstring(buff, opts);
  102. if (i >= 0) return i;
  103. else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
  104. return luaI_globaldefined(buff);
  105. else {
  106. luaI_auxsynterrbf("invalid $if condition", buff);
  107. return 0; /* to avoid warnings */
  108. }
  109. }
  110. static void readname (char *buff)
  111. {
  112. int i = 0;
  113. skipspace();
  114. while (isalnum((unsigned char)current) || current == '_') {
  115. if (i >= PRAGMASIZE) {
  116. buff[PRAGMASIZE] = 0;
  117. luaI_auxsynterrbf("pragma too long", buff);
  118. }
  119. buff[i++] = current;
  120. next();
  121. }
  122. buff[i] = 0;
  123. }
  124. static void inclinenumber (void);
  125. static void ifskip (void)
  126. {
  127. while (ifstate[iflevel].skip) {
  128. if (current == '\n')
  129. inclinenumber();
  130. else if (current == EOZ)
  131. luaI_auxsyntaxerror("input ends inside a $if");
  132. else next();
  133. }
  134. }
  135. static void inclinenumber (void)
  136. {
  137. static char *pragmas [] =
  138. {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
  139. next(); /* skip '\n' */
  140. ++lua_linenumber;
  141. if (current == '$') { /* is a pragma? */
  142. char buff[PRAGMASIZE+1];
  143. int ifnot = 0;
  144. int skip = ifstate[iflevel].skip;
  145. next(); /* skip $ */
  146. readname(buff);
  147. switch (luaI_findstring(buff, pragmas)) {
  148. case 0: /* debug */
  149. if (!skip) lua_debug = 1;
  150. break;
  151. case 1: /* nodebug */
  152. if (!skip) lua_debug = 0;
  153. break;
  154. case 2: /* endinput */
  155. if (!skip) {
  156. current = EOZ;
  157. iflevel = 0; /* to allow $endinput inside a $if */
  158. }
  159. break;
  160. case 3: /* end */
  161. if (iflevel-- == 0)
  162. luaI_auxsyntaxerror("unmatched $endif");
  163. break;
  164. case 4: /* ifnot */
  165. ifnot = 1;
  166. /* go through */
  167. case 5: /* if */
  168. if (iflevel == MAX_IFS-1)
  169. luaI_auxsyntaxerror("too many nested `$ifs'");
  170. readname(buff);
  171. iflevel++;
  172. ifstate[iflevel].elsepart = 0;
  173. ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
  174. ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
  175. break;
  176. case 6: /* else */
  177. if (ifstate[iflevel].elsepart)
  178. luaI_auxsyntaxerror("unmatched $else");
  179. ifstate[iflevel].elsepart = 1;
  180. ifstate[iflevel].skip =
  181. ifstate[iflevel-1].skip || ifstate[iflevel].condition;
  182. break;
  183. default:
  184. luaI_auxsynterrbf("invalid pragma", buff);
  185. }
  186. skipspace();
  187. if (current == '\n') /* pragma must end with a '\n' ... */
  188. inclinenumber();
  189. else if (current != EOZ) /* or eof */
  190. luaI_auxsyntaxerror("invalid pragma format");
  191. ifskip();
  192. }
  193. }
  194. static int read_long_string (char *yytext, int buffsize)
  195. {
  196. int cont = 0;
  197. int tokensize = 2; /* '[[' already stored */
  198. while (1)
  199. {
  200. if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
  201. yytext = luaI_buffer(buffsize *= 2);
  202. switch (current)
  203. {
  204. case EOZ:
  205. save(0);
  206. return WRONGTOKEN;
  207. case '[':
  208. save_and_next();
  209. if (current == '[')
  210. {
  211. cont++;
  212. save_and_next();
  213. }
  214. continue;
  215. case ']':
  216. save_and_next();
  217. if (current == ']')
  218. {
  219. if (cont == 0) goto endloop;
  220. cont--;
  221. save_and_next();
  222. }
  223. continue;
  224. case '\n':
  225. save('\n');
  226. inclinenumber();
  227. continue;
  228. default:
  229. save_and_next();
  230. }
  231. } endloop:
  232. save_and_next(); /* pass the second ']' */
  233. yytext[tokensize-2] = 0; /* erases ']]' */
  234. luaY_lval.pTStr = luaI_createtempstring(yytext+2);
  235. yytext[tokensize-2] = ']'; /* restores ']]' */
  236. save(0);
  237. return STRING;
  238. }
  239. int luaY_lex (void)
  240. {
  241. static int linelasttoken = 0;
  242. double a;
  243. int buffsize = MINBUFF;
  244. char *yytext = luaI_buffer(buffsize);
  245. yytext[1] = yytext[2] = yytext[3] = 0;
  246. if (lua_debug)
  247. luaI_codedebugline(linelasttoken);
  248. linelasttoken = lua_linenumber;
  249. while (1) {
  250. int tokensize = 0;
  251. switch (current) {
  252. case '\n':
  253. inclinenumber();
  254. linelasttoken = lua_linenumber;
  255. continue;
  256. case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
  257. next();
  258. continue;
  259. case '-':
  260. save_and_next();
  261. if (current != '-') return '-';
  262. do { next(); } while (current != '\n' && current != EOZ);
  263. continue;
  264. case '[':
  265. save_and_next();
  266. if (current != '[') return '[';
  267. else
  268. {
  269. save_and_next(); /* pass the second '[' */
  270. return read_long_string(yytext, buffsize);
  271. }
  272. case '=':
  273. save_and_next();
  274. if (current != '=') return '=';
  275. else { save_and_next(); return EQ; }
  276. case '<':
  277. save_and_next();
  278. if (current != '=') return '<';
  279. else { save_and_next(); return LE; }
  280. case '>':
  281. save_and_next();
  282. if (current != '=') return '>';
  283. else { save_and_next(); return GE; }
  284. case '~':
  285. save_and_next();
  286. if (current != '=') return '~';
  287. else { save_and_next(); return NE; }
  288. case '"':
  289. case '\'':
  290. {
  291. int del = current;
  292. save_and_next();
  293. while (current != del)
  294. {
  295. if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
  296. yytext = luaI_buffer(buffsize *= 2);
  297. switch (current)
  298. {
  299. case EOZ:
  300. case '\n':
  301. save(0);
  302. return WRONGTOKEN;
  303. case '\\':
  304. next(); /* do not save the '\' */
  305. switch (current)
  306. {
  307. case 'n': save('\n'); next(); break;
  308. case 't': save('\t'); next(); break;
  309. case 'r': save('\r'); next(); break;
  310. case '\n': save('\n'); inclinenumber(); break;
  311. default : save_and_next(); break;
  312. }
  313. break;
  314. default:
  315. save_and_next();
  316. }
  317. }
  318. next(); /* skip delimiter */
  319. save(0);
  320. luaY_lval.pTStr = luaI_createtempstring(yytext+1);
  321. tokensize--;
  322. save(del); save(0); /* restore delimiter */
  323. return STRING;
  324. }
  325. case '.':
  326. save_and_next();
  327. if (current == '.')
  328. {
  329. save_and_next();
  330. if (current == '.')
  331. {
  332. save_and_next();
  333. return DOTS; /* ... */
  334. }
  335. else return CONC; /* .. */
  336. }
  337. else if (!isdigit((unsigned char)current)) return '.';
  338. /* current is a digit: goes through to number */
  339. a=0.0;
  340. goto fraction;
  341. case '0': case '1': case '2': case '3': case '4':
  342. case '5': case '6': case '7': case '8': case '9':
  343. a=0.0;
  344. do {
  345. a=10.0*a+(current-'0');
  346. save_and_next();
  347. } while (isdigit((unsigned char)current));
  348. if (current == '.') {
  349. save_and_next();
  350. if (current == '.')
  351. luaI_syntaxerror(
  352. "ambiguous syntax (decimal point x string concatenation)");
  353. }
  354. fraction:
  355. { double da=0.1;
  356. while (isdigit((unsigned char)current))
  357. {
  358. a+=(current-'0')*da;
  359. da/=10.0;
  360. save_and_next();
  361. }
  362. if (current == 'e' || current == 'E')
  363. {
  364. int e=0;
  365. int neg;
  366. double ea;
  367. save_and_next();
  368. neg=(current=='-');
  369. if (current == '+' || current == '-') save_and_next();
  370. if (!isdigit((unsigned char)current)) {
  371. save(0); return WRONGTOKEN; }
  372. do {
  373. e=10.0*e+(current-'0');
  374. save_and_next();
  375. } while (isdigit((unsigned char)current));
  376. for (ea=neg?0.1:10.0; e>0; e>>=1)
  377. {
  378. if (e & 1) a*=ea;
  379. ea*=ea;
  380. }
  381. }
  382. luaY_lval.vReal = a;
  383. save(0);
  384. return NUMBER;
  385. }
  386. case EOZ:
  387. save(0);
  388. if (iflevel > 0)
  389. luaI_syntaxerror("missing $endif");
  390. return 0;
  391. default:
  392. if (current != '_' && !isalpha((unsigned char)current)) {
  393. save_and_next();
  394. return yytext[0];
  395. }
  396. else { /* identifier or reserved word */
  397. TaggedString *ts;
  398. do {
  399. save_and_next();
  400. } while (isalnum((unsigned char)current) || current == '_');
  401. save(0);
  402. ts = luaI_createtempstring(yytext);
  403. if (ts->marked > 255)
  404. return ts->marked; /* reserved word */
  405. luaY_lval.pTStr = ts;
  406. return NAME;
  407. }
  408. }
  409. }
  410. }