lex.c 6.8 KB

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