lex.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. char *rcs_lex = "$Id: lex.c,v 2.18 1995/10/06 13:10:53 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. while (1)
  130. {
  131. yytextLast = yytext;
  132. #if 0
  133. fprintf(stderr,"'%c' %d\n",current,current);
  134. #endif
  135. switch (current)
  136. {
  137. case EOF:
  138. case 0:
  139. return 0;
  140. case '\n': lua_linenumber++;
  141. case ' ':
  142. case '\r': /* CR: to avoid problems with DOS/Windows */
  143. case '\t':
  144. next();
  145. continue;
  146. case '$':
  147. next();
  148. while (isalnum(current) || current == '_')
  149. save_and_next();
  150. *yytextLast = 0;
  151. if (lua_strcmp(yytext, "debug") == 0)
  152. {
  153. yylval.vInt = 1;
  154. return DEBUG;
  155. }
  156. else if (lua_strcmp(yytext, "nodebug") == 0)
  157. {
  158. yylval.vInt = 0;
  159. return DEBUG;
  160. }
  161. return WRONGTOKEN;
  162. case '-':
  163. save_and_next();
  164. if (current != '-') return '-'; /* else goes through */
  165. case '#':
  166. do { next(); } while (current != '\n' && current != 0);
  167. continue;
  168. case '[':
  169. save_and_next();
  170. if (current != '[') return '[';
  171. else
  172. {
  173. save_and_next(); /* pass the second '[' */
  174. if (read_long_string() == WRONGTOKEN)
  175. return WRONGTOKEN;
  176. save_and_next(); /* pass the second ']' */
  177. *(yytextLast-2) = 0; /* erases ']]' */
  178. yylval.vWord = luaI_findconstantbyname(yytext+2);
  179. return STRING;
  180. }
  181. case '=':
  182. save_and_next();
  183. if (current != '=') return '=';
  184. else { save_and_next(); return EQ; }
  185. case '<':
  186. save_and_next();
  187. if (current != '=') return '<';
  188. else { save_and_next(); return LE; }
  189. case '>':
  190. save_and_next();
  191. if (current != '=') return '>';
  192. else { save_and_next(); return GE; }
  193. case '~':
  194. save_and_next();
  195. if (current != '=') return '~';
  196. else { save_and_next(); return NE; }
  197. case '"':
  198. case '\'':
  199. {
  200. int del = current;
  201. int spaceleft = textsize - (yytextLast - yytext);
  202. next(); /* skip the delimiter */
  203. while (current != del)
  204. {
  205. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  206. {
  207. growtext();
  208. spaceleft = textsize - (yytextLast - yytext);
  209. }
  210. spaceleft--;
  211. switch (current)
  212. {
  213. case EOF:
  214. case 0:
  215. case '\n':
  216. return WRONGTOKEN;
  217. case '\\':
  218. next(); /* do not save the '\' */
  219. switch (current)
  220. {
  221. case 'n': save('\n'); next(); break;
  222. case 't': save('\t'); next(); break;
  223. case 'r': save('\r'); next(); break;
  224. default : save(current); next(); break;
  225. }
  226. break;
  227. default:
  228. save_and_next();
  229. }
  230. }
  231. next(); /* skip the delimiter */
  232. *yytextLast = 0;
  233. yylval.vWord = luaI_findconstantbyname(yytext);
  234. return STRING;
  235. }
  236. case 'a': case 'b': case 'c': case 'd': case 'e':
  237. case 'f': case 'g': case 'h': case 'i': case 'j':
  238. case 'k': case 'l': case 'm': case 'n': case 'o':
  239. case 'p': case 'q': case 'r': case 's': case 't':
  240. case 'u': case 'v': case 'w': case 'x': case 'y':
  241. case 'z':
  242. case 'A': case 'B': case 'C': case 'D': case 'E':
  243. case 'F': case 'G': case 'H': case 'I': case 'J':
  244. case 'K': case 'L': case 'M': case 'N': case 'O':
  245. case 'P': case 'Q': case 'R': case 'S': case 'T':
  246. case 'U': case 'V': case 'W': case 'X': case 'Y':
  247. case 'Z':
  248. case '_':
  249. {
  250. Word res;
  251. do { save_and_next(); } while (isalnum(current) || current == '_');
  252. *yytextLast = 0;
  253. res = findReserved(yytext);
  254. if (res) return res;
  255. yylval.pNode = lua_constcreate(yytext);
  256. return NAME;
  257. }
  258. case '.':
  259. save_and_next();
  260. if (current == '.')
  261. {
  262. save_and_next();
  263. return CONC;
  264. }
  265. else if (!isdigit(current)) return '.';
  266. /* current is a digit: goes through to number */
  267. a=0.0;
  268. goto fraction;
  269. case '0': case '1': case '2': case '3': case '4':
  270. case '5': case '6': case '7': case '8': case '9':
  271. a=0.0;
  272. do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
  273. if (current == '.') save_and_next();
  274. fraction:
  275. { float da=0.1;
  276. while (isdigit(current))
  277. {a+=(current-'0')*da; da/=10.0; save_and_next()};
  278. if (current == 'e' || current == 'E')
  279. {
  280. int e=0;
  281. int neg;
  282. float ea;
  283. save_and_next();
  284. neg=(current=='-');
  285. if (current == '+' || current == '-') save_and_next();
  286. if (!isdigit(current)) return WRONGTOKEN;
  287. do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
  288. for (ea=neg?0.1:10.0; e>0; e>>=1)
  289. {
  290. if (e & 1) a*=ea;
  291. ea*=ea;
  292. }
  293. }
  294. yylval.vFloat = a;
  295. return NUMBER;
  296. }
  297. case U_and: case U_do: case U_else: case U_elseif: case U_end:
  298. case U_function: case U_if: case U_local: case U_nil: case U_not:
  299. case U_or: case U_repeat: case U_return: case U_then:
  300. case U_until: case U_while:
  301. {
  302. int old = current;
  303. next();
  304. return reserved[old-U_and].token;
  305. }
  306. case U_eq: next(); return EQ;
  307. case U_le: next(); return LE;
  308. case U_ge: next(); return GE;
  309. case U_ne: next(); return NE;
  310. case U_sc: next(); return CONC;
  311. default: /* also end of file */
  312. {
  313. save_and_next();
  314. return yytext[0];
  315. }
  316. }
  317. }
  318. }