lex.c 8.4 KB

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