lex.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. char *rcs_lex = "$Id: lex.c,v 2.19 1995/10/13 15:16:25 roberto Exp roberto $";
  2. #include <ctype.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "mem.h"
  8. #include "tree.h"
  9. #include "table.h"
  10. #include "opcode.h"
  11. #include "inout.h"
  12. #include "parser.h"
  13. #include "ugly.h"
  14. #define MINBUFF 260
  15. #define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
  16. #define next() { current = input(); }
  17. #define save(x) { *yytextLast++ = (x); }
  18. #define save_and_next() { save(current); next(); }
  19. static int current;
  20. static char *yytext = NULL;
  21. static int textsize = 0;
  22. static char *yytextLast;
  23. static Input input;
  24. void lua_setinput (Input fn)
  25. {
  26. current = ' ';
  27. input = fn;
  28. if (yytext == NULL)
  29. {
  30. textsize = MINBUFF;
  31. yytext = newvector(textsize, char);
  32. }
  33. }
  34. char *lua_lasttext (void)
  35. {
  36. *yytextLast = 0;
  37. return yytext;
  38. }
  39. /* The reserved words must be listed in lexicographic order */
  40. static struct
  41. {
  42. char *name;
  43. int token;
  44. } reserved [] = {
  45. {"and", AND},
  46. {"do", DO},
  47. {"else", ELSE},
  48. {"elseif", ELSEIF},
  49. {"end", END},
  50. {"function", FUNCTION},
  51. {"if", IF},
  52. {"local", LOCAL},
  53. {"nil", NIL},
  54. {"not", NOT},
  55. {"or", OR},
  56. {"repeat", REPEAT},
  57. {"return", RETURN},
  58. {"then", THEN},
  59. {"until", UNTIL},
  60. {"while", WHILE} };
  61. #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
  62. static int findReserved (char *name)
  63. {
  64. int l = 0;
  65. int h = RESERVEDSIZE - 1;
  66. while (l <= h)
  67. {
  68. int m = (l+h)/2;
  69. int comp = lua_strcmp(name, reserved[m].name);
  70. if (comp < 0)
  71. h = m-1;
  72. else if (comp == 0)
  73. return reserved[m].token;
  74. else
  75. l = m+1;
  76. }
  77. return 0;
  78. }
  79. static void growtext (void)
  80. {
  81. int size = yytextLast - yytext;
  82. textsize *= 2;
  83. yytext = growvector(yytext, textsize, char);
  84. yytextLast = yytext + size;
  85. }
  86. static int read_long_string (void)
  87. {
  88. int cont = 0;
  89. int spaceleft = textsize - (yytextLast - yytext);
  90. while (1)
  91. {
  92. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  93. {
  94. growtext();
  95. spaceleft = textsize - (yytextLast - yytext);
  96. }
  97. switch (current)
  98. {
  99. case EOF:
  100. case 0:
  101. return WRONGTOKEN;
  102. case '[':
  103. save_and_next(); spaceleft--;
  104. if (current == '[')
  105. {
  106. cont++;
  107. save_and_next(); spaceleft--;
  108. }
  109. continue;
  110. case ']':
  111. save_and_next(); spaceleft--;
  112. if (current == ']')
  113. {
  114. if (cont == 0) return STRING;
  115. cont--;
  116. save_and_next(); spaceleft--;
  117. }
  118. continue;
  119. case '\n':
  120. lua_linenumber++; /* goes through */
  121. default:
  122. save_and_next(); spaceleft--;
  123. }
  124. }
  125. }
  126. int yylex (void)
  127. {
  128. float a;
  129. static int linelasttoken = 0;
  130. if (lua_debug)
  131. luaI_codedebugline(linelasttoken);
  132. linelasttoken = lua_linenumber;
  133. while (1)
  134. {
  135. yytextLast = yytext;
  136. #if 0
  137. fprintf(stderr,"'%c' %d\n",current,current);
  138. #endif
  139. switch (current)
  140. {
  141. case EOF:
  142. case 0:
  143. return 0;
  144. case '\n': linelasttoken = ++lua_linenumber;
  145. case ' ':
  146. case '\r': /* CR: to avoid problems with DOS/Windows */
  147. case '\t':
  148. next();
  149. continue;
  150. case '$':
  151. next();
  152. while (isalnum(current) || current == '_')
  153. save_and_next();
  154. *yytextLast = 0;
  155. if (lua_strcmp(yytext, "debug") == 0)
  156. {
  157. yylval.vInt = 1;
  158. return DEBUG;
  159. }
  160. else if (lua_strcmp(yytext, "nodebug") == 0)
  161. {
  162. yylval.vInt = 0;
  163. return DEBUG;
  164. }
  165. return WRONGTOKEN;
  166. case '-':
  167. save_and_next();
  168. if (current != '-') return '-'; /* else goes through */
  169. case '#':
  170. do { next(); } while (current != '\n' && current != 0);
  171. continue;
  172. case '[':
  173. save_and_next();
  174. if (current != '[') return '[';
  175. else
  176. {
  177. save_and_next(); /* pass the second '[' */
  178. if (read_long_string() == WRONGTOKEN)
  179. return WRONGTOKEN;
  180. save_and_next(); /* pass the second ']' */
  181. *(yytextLast-2) = 0; /* erases ']]' */
  182. yylval.vWord = luaI_findconstantbyname(yytext+2);
  183. return STRING;
  184. }
  185. case '=':
  186. save_and_next();
  187. if (current != '=') return '=';
  188. else { save_and_next(); return EQ; }
  189. case '<':
  190. save_and_next();
  191. if (current != '=') return '<';
  192. else { save_and_next(); return LE; }
  193. case '>':
  194. save_and_next();
  195. if (current != '=') return '>';
  196. else { save_and_next(); return GE; }
  197. case '~':
  198. save_and_next();
  199. if (current != '=') return '~';
  200. else { save_and_next(); return NE; }
  201. case '"':
  202. case '\'':
  203. {
  204. int del = current;
  205. int spaceleft = textsize - (yytextLast - yytext);
  206. next(); /* skip the delimiter */
  207. while (current != del)
  208. {
  209. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  210. {
  211. growtext();
  212. spaceleft = textsize - (yytextLast - yytext);
  213. }
  214. spaceleft--;
  215. switch (current)
  216. {
  217. case EOF:
  218. case 0:
  219. case '\n':
  220. return WRONGTOKEN;
  221. case '\\':
  222. next(); /* do not save the '\' */
  223. switch (current)
  224. {
  225. case 'n': save('\n'); next(); break;
  226. case 't': save('\t'); next(); break;
  227. case 'r': save('\r'); next(); break;
  228. default : save(current); next(); break;
  229. }
  230. break;
  231. default:
  232. save_and_next();
  233. }
  234. }
  235. next(); /* skip the delimiter */
  236. *yytextLast = 0;
  237. yylval.vWord = luaI_findconstantbyname(yytext);
  238. return STRING;
  239. }
  240. case 'a': case 'b': case 'c': case 'd': case 'e':
  241. case 'f': case 'g': case 'h': case 'i': case 'j':
  242. case 'k': case 'l': case 'm': case 'n': case 'o':
  243. case 'p': case 'q': case 'r': case 's': case 't':
  244. case 'u': case 'v': case 'w': case 'x': case 'y':
  245. case 'z':
  246. case 'A': case 'B': case 'C': case 'D': case 'E':
  247. case 'F': case 'G': case 'H': case 'I': case 'J':
  248. case 'K': case 'L': case 'M': case 'N': case 'O':
  249. case 'P': case 'Q': case 'R': case 'S': case 'T':
  250. case 'U': case 'V': case 'W': case 'X': case 'Y':
  251. case 'Z':
  252. case '_':
  253. {
  254. Word res;
  255. do { save_and_next(); } while (isalnum(current) || current == '_');
  256. *yytextLast = 0;
  257. res = findReserved(yytext);
  258. if (res) return res;
  259. yylval.pNode = lua_constcreate(yytext);
  260. return NAME;
  261. }
  262. case '.':
  263. save_and_next();
  264. if (current == '.')
  265. {
  266. save_and_next();
  267. return CONC;
  268. }
  269. else if (!isdigit(current)) return '.';
  270. /* current is a digit: goes through to number */
  271. a=0.0;
  272. goto fraction;
  273. case '0': case '1': case '2': case '3': case '4':
  274. case '5': case '6': case '7': case '8': case '9':
  275. a=0.0;
  276. do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
  277. if (current == '.') save_and_next();
  278. fraction:
  279. { float da=0.1;
  280. while (isdigit(current))
  281. {a+=(current-'0')*da; da/=10.0; save_and_next()};
  282. if (current == 'e' || current == 'E')
  283. {
  284. int e=0;
  285. int neg;
  286. float ea;
  287. save_and_next();
  288. neg=(current=='-');
  289. if (current == '+' || current == '-') save_and_next();
  290. if (!isdigit(current)) return WRONGTOKEN;
  291. do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
  292. for (ea=neg?0.1:10.0; e>0; e>>=1)
  293. {
  294. if (e & 1) a*=ea;
  295. ea*=ea;
  296. }
  297. }
  298. yylval.vFloat = a;
  299. return NUMBER;
  300. }
  301. case U_and: case U_do: case U_else: case U_elseif: case U_end:
  302. case U_function: case U_if: case U_local: case U_nil: case U_not:
  303. case U_or: case U_repeat: case U_return: case U_then:
  304. case U_until: case U_while:
  305. {
  306. int old = current;
  307. next();
  308. return reserved[old-U_and].token;
  309. }
  310. case U_eq: next(); return EQ;
  311. case U_le: next(); return LE;
  312. case U_ge: next(); return GE;
  313. case U_ne: next(); return NE;
  314. case U_sc: next(); return CONC;
  315. default: /* also end of file */
  316. {
  317. save_and_next();
  318. return yytext[0];
  319. }
  320. }
  321. }
  322. }