lex.c 7.3 KB

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