lex.c 7.9 KB

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