lex.c 8.4 KB

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