lex.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. char *rcs_lex = "$Id: lex.c,v 2.1 1994/04/15 19:00:28 celes Exp celes $";
  2. /*$Log: lex.c,v $
  3. * Revision 2.1 1994/04/15 19:00:28 celes
  4. * Retirar chamada da funcao lua_findsymbol associada a cada
  5. * token NAME. A decisao de chamar lua_findsymbol ou lua_findconstant
  6. * fica a cargo do modulo "lua.stx".
  7. *
  8. * Revision 1.3 1993/12/28 16:42:29 roberto
  9. * "include"s de string.h e stdlib.h para evitar warnings
  10. *
  11. * Revision 1.2 1993/12/22 21:39:15 celes
  12. * Tratamento do token $debug e $nodebug
  13. *
  14. * Revision 1.1 1993/12/22 21:15:16 roberto
  15. * Initial revision
  16. **/
  17. #include <ctype.h>
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "opcode.h"
  22. #include "hash.h"
  23. #include "inout.h"
  24. #include "table.h"
  25. #include "y.tab.h"
  26. #define next() { current = input(); }
  27. #define save(x) { *yytextLast++ = (x); }
  28. #define save_and_next() { save(current); next(); }
  29. static int current;
  30. static char yytext[2][256];
  31. static char *yytextLast;
  32. static int currentText = 0;
  33. static Input input;
  34. void lua_setinput (Input fn)
  35. {
  36. current = ' ';
  37. input = fn;
  38. }
  39. char *lua_lasttext (void)
  40. {
  41. *yytextLast = 0;
  42. return yytext[currentText];
  43. }
  44. /* The reserved words must be listed in lexicographic order */
  45. static struct
  46. {
  47. char *name;
  48. int token;
  49. } reserved [] = {
  50. {"and", AND},
  51. {"do", DO},
  52. {"else", ELSE},
  53. {"elseif", ELSEIF},
  54. {"end", END},
  55. {"function", FUNCTION},
  56. {"if", IF},
  57. {"local", LOCAL},
  58. {"nil", NIL},
  59. {"not", NOT},
  60. {"or", OR},
  61. {"repeat", REPEAT},
  62. {"return", RETURN},
  63. {"then", THEN},
  64. {"until", UNTIL},
  65. {"while", WHILE} };
  66. #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
  67. static int findReserved (char *name)
  68. {
  69. int l = 0;
  70. int h = RESERVEDSIZE - 1;
  71. while (l <= h)
  72. {
  73. int m = (l+h)/2;
  74. int comp = strcmp(name, reserved[m].name);
  75. if (comp < 0)
  76. h = m-1;
  77. else if (comp == 0)
  78. return reserved[m].token;
  79. else
  80. l = m+1;
  81. }
  82. return 0;
  83. }
  84. int yylex ()
  85. {
  86. currentText = !currentText;
  87. while (1)
  88. {
  89. yytextLast = yytext[currentText];
  90. switch (current)
  91. {
  92. case '\n': lua_linenumber++;
  93. case ' ':
  94. case '\t':
  95. next();
  96. continue;
  97. case '$':
  98. next();
  99. while (isalnum(current) || current == '_')
  100. save_and_next();
  101. *yytextLast = 0;
  102. if (strcmp(yytext[currentText], "debug") == 0)
  103. {
  104. yylval.vInt = 1;
  105. return DEBUG;
  106. }
  107. else if (strcmp(yytext[currentText], "nodebug") == 0)
  108. {
  109. yylval.vInt = 0;
  110. return DEBUG;
  111. }
  112. return WRONGTOKEN;
  113. case '-':
  114. save_and_next();
  115. if (current != '-') return '-';
  116. do { next(); } while (current != '\n' && current != 0);
  117. continue;
  118. case '<':
  119. save_and_next();
  120. if (current != '=') return '<';
  121. else { save_and_next(); return LE; }
  122. case '>':
  123. save_and_next();
  124. if (current != '=') return '>';
  125. else { save_and_next(); return GE; }
  126. case '~':
  127. save_and_next();
  128. if (current != '=') return '~';
  129. else { save_and_next(); return NE; }
  130. case '"':
  131. case '\'':
  132. {
  133. int del = current;
  134. next(); /* skip the delimiter */
  135. while (current != del)
  136. {
  137. switch (current)
  138. {
  139. case 0:
  140. case '\n':
  141. return WRONGTOKEN;
  142. case '\\':
  143. next(); /* do not save the '\' */
  144. switch (current)
  145. {
  146. case 'n': save('\n'); next(); break;
  147. case 't': save('\t'); next(); break;
  148. case 'r': save('\r'); next(); break;
  149. default : save('\\'); break;
  150. }
  151. break;
  152. default:
  153. save_and_next();
  154. }
  155. }
  156. next(); /* skip the delimiter */
  157. *yytextLast = 0;
  158. yylval.vWord = lua_findconstant (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. goto fraction;
  193. case '0': case '1': case '2': case '3': case '4':
  194. case '5': case '6': case '7': case '8': case '9':
  195. do { save_and_next(); } while (isdigit(current));
  196. if (current == '.') save_and_next();
  197. fraction: while (isdigit(current)) save_and_next();
  198. if (current == 'e' || current == 'E')
  199. {
  200. save_and_next();
  201. if (current == '+' || current == '-') save_and_next();
  202. if (!isdigit(current)) return WRONGTOKEN;
  203. do { save_and_next(); } while (isdigit(current));
  204. }
  205. *yytextLast = 0;
  206. yylval.vFloat = atof(yytext[currentText]);
  207. return NUMBER;
  208. default: /* also end of file */
  209. {
  210. save_and_next();
  211. return yytext[currentText][0];
  212. }
  213. }
  214. }
  215. }