lex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. char *rcs_lex = "$Id: lex.c,v 3.5 1997/06/16 16:50:22 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. int tokensize = 0;
  243. switch (current) {
  244. case '\n':
  245. inclinenumber();
  246. linelasttoken = lua_linenumber;
  247. continue;
  248. case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
  249. next();
  250. continue;
  251. case '-':
  252. save_and_next();
  253. if (current != '-') return '-';
  254. do { next(); } while (current != '\n' && current != EOZ);
  255. continue;
  256. case '[':
  257. save_and_next();
  258. if (current != '[') return '[';
  259. else
  260. {
  261. save_and_next(); /* pass the second '[' */
  262. return read_long_string(yytext, buffsize);
  263. }
  264. case '=':
  265. save_and_next();
  266. if (current != '=') return '=';
  267. else { save_and_next(); return EQ; }
  268. case '<':
  269. save_and_next();
  270. if (current != '=') return '<';
  271. else { save_and_next(); return LE; }
  272. case '>':
  273. save_and_next();
  274. if (current != '=') return '>';
  275. else { save_and_next(); return GE; }
  276. case '~':
  277. save_and_next();
  278. if (current != '=') return '~';
  279. else { save_and_next(); return NE; }
  280. case '"':
  281. case '\'':
  282. {
  283. int del = current;
  284. save_and_next();
  285. while (current != del)
  286. {
  287. if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
  288. yytext = luaI_buffer(buffsize *= 2);
  289. switch (current)
  290. {
  291. case EOZ:
  292. case '\n':
  293. save(0);
  294. return WRONGTOKEN;
  295. case '\\':
  296. next(); /* do not save the '\' */
  297. switch (current)
  298. {
  299. case 'n': save('\n'); next(); break;
  300. case 't': save('\t'); next(); break;
  301. case 'r': save('\r'); next(); break;
  302. case '\n': save('\n'); inclinenumber(); break;
  303. default : save_and_next(); break;
  304. }
  305. break;
  306. default:
  307. save_and_next();
  308. }
  309. }
  310. next(); /* skip delimiter */
  311. save(0);
  312. luaY_lval.vWord = luaI_findconstantbyname(yytext+1);
  313. tokensize--;
  314. save(del); save(0); /* restore delimiter */
  315. return STRING;
  316. }
  317. case '.':
  318. save_and_next();
  319. if (current == '.')
  320. {
  321. save_and_next();
  322. if (current == '.')
  323. {
  324. save_and_next();
  325. return DOTS; /* ... */
  326. }
  327. else return CONC; /* .. */
  328. }
  329. else if (!isdigit((unsigned char)current)) return '.';
  330. /* current is a digit: goes through to number */
  331. a=0.0;
  332. goto fraction;
  333. case '0': case '1': case '2': case '3': case '4':
  334. case '5': case '6': case '7': case '8': case '9':
  335. a=0.0;
  336. do {
  337. a=10.0*a+(current-'0');
  338. save_and_next();
  339. } while (isdigit((unsigned char)current));
  340. if (current == '.') {
  341. save_and_next();
  342. if (current == '.')
  343. luaI_syntaxerror(
  344. "ambiguous syntax (decimal point x string concatenation)");
  345. }
  346. fraction:
  347. { double da=0.1;
  348. while (isdigit((unsigned char)current))
  349. {
  350. a+=(current-'0')*da;
  351. da/=10.0;
  352. save_and_next();
  353. }
  354. if (current == 'e' || current == 'E')
  355. {
  356. int e=0;
  357. int neg;
  358. double ea;
  359. save_and_next();
  360. neg=(current=='-');
  361. if (current == '+' || current == '-') save_and_next();
  362. if (!isdigit((unsigned char)current)) {
  363. save(0); return WRONGTOKEN; }
  364. do {
  365. e=10.0*e+(current-'0');
  366. save_and_next();
  367. } while (isdigit((unsigned char)current));
  368. for (ea=neg?0.1:10.0; e>0; e>>=1)
  369. {
  370. if (e & 1) a*=ea;
  371. ea*=ea;
  372. }
  373. }
  374. luaY_lval.vFloat = a;
  375. save(0);
  376. return NUMBER;
  377. }
  378. case EOZ:
  379. save(0);
  380. if (iflevel > 0)
  381. luaI_syntaxerror("missing $endif");
  382. return 0;
  383. default:
  384. if (current != '_' && !isalpha((unsigned char)current)) {
  385. save_and_next();
  386. return yytext[0];
  387. }
  388. else { /* identifier or reserved word */
  389. TaggedString *ts;
  390. do {
  391. save_and_next();
  392. } while (isalnum((unsigned char)current) || current == '_');
  393. save(0);
  394. ts = lua_createstring(yytext);
  395. if (ts->marked > 2)
  396. return ts->marked; /* reserved word */
  397. luaY_lval.pTStr = ts;
  398. ts->marked = 2; /* avoid GC */
  399. return NAME;
  400. }
  401. }
  402. }
  403. }