lex.c 7.5 KB

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