lex.c 11 KB

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