lex.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. char *rcs_lex = "$Id: lex.c,v 1.2 1993/12/22 21:39:15 celes Exp roberto $";
  2. /*$Log: lex.c,v $
  3. * Revision 1.2 1993/12/22 21:39:15 celes
  4. * Tratamento do token $debug e $nodebug
  5. *
  6. * Revision 1.1 1993/12/22 21:15:16 roberto
  7. * Initial revision
  8. **/
  9. #include <ctype.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "opcode.h"
  14. #include "hash.h"
  15. #include "inout.h"
  16. #include "table.h"
  17. #include "y.tab.h"
  18. #define next() { current = input(); }
  19. #define save(x) { *yytextLast++ = (x); }
  20. #define save_and_next() { save(current); next(); }
  21. static int current;
  22. static char yytext[256];
  23. static char *yytextLast;
  24. static Input input;
  25. void lua_setinput (Input fn)
  26. {
  27. current = ' ';
  28. input = fn;
  29. }
  30. char *lua_lasttext (void)
  31. {
  32. *yytextLast = 0;
  33. return yytext;
  34. }
  35. static struct
  36. {
  37. char *name;
  38. int token;
  39. } reserved [] = {
  40. {"and", AND},
  41. {"do", DO},
  42. {"else", ELSE},
  43. {"elseif", ELSEIF},
  44. {"end", END},
  45. {"function", FUNCTION},
  46. {"if", IF},
  47. {"local", LOCAL},
  48. {"nil", NIL},
  49. {"not", NOT},
  50. {"or", OR},
  51. {"repeat", REPEAT},
  52. {"return", RETURN},
  53. {"then", THEN},
  54. {"until", UNTIL},
  55. {"while", WHILE} };
  56. #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
  57. int findReserved (char *name)
  58. {
  59. int l = 0;
  60. int h = RESERVEDSIZE - 1;
  61. while (l <= h)
  62. {
  63. int m = (l+h)/2;
  64. int comp = strcmp(name, reserved[m].name);
  65. if (comp < 0)
  66. h = m-1;
  67. else if (comp == 0)
  68. return reserved[m].token;
  69. else
  70. l = m+1;
  71. }
  72. return 0;
  73. }
  74. int yylex ()
  75. {
  76. while (1)
  77. {
  78. yytextLast = yytext;
  79. switch (current)
  80. {
  81. case '\n': lua_linenumber++;
  82. case ' ':
  83. case '\t':
  84. next();
  85. continue;
  86. case '$':
  87. next();
  88. while (isalnum(current) || current == '_')
  89. save_and_next();
  90. *yytextLast = 0;
  91. if (strcmp(yytext, "debug") == 0)
  92. {
  93. yylval.vInt = 1;
  94. return DEBUG;
  95. }
  96. else if (strcmp(yytext, "nodebug") == 0)
  97. {
  98. yylval.vInt = 0;
  99. return DEBUG;
  100. }
  101. return WRONGTOKEN;
  102. case '-':
  103. save_and_next();
  104. if (current != '-') return '-';
  105. do { next(); } while (current != '\n' && current != 0);
  106. continue;
  107. case '<':
  108. save_and_next();
  109. if (current != '=') return '<';
  110. else { save_and_next(); return LE; }
  111. case '>':
  112. save_and_next();
  113. if (current != '=') return '>';
  114. else { save_and_next(); return GE; }
  115. case '~':
  116. save_and_next();
  117. if (current != '=') return '~';
  118. else { save_and_next(); return NE; }
  119. case '"':
  120. case '\'':
  121. {
  122. int del = current;
  123. next(); /* skip the delimiter */
  124. while (current != del)
  125. {
  126. switch (current)
  127. {
  128. case 0:
  129. case '\n':
  130. return WRONGTOKEN;
  131. case '\\':
  132. next(); /* do not save the '\' */
  133. switch (current)
  134. {
  135. case 'n': save('\n'); next(); break;
  136. case 't': save('\t'); next(); break;
  137. case 'r': save('\r'); next(); break;
  138. default : save('\\'); break;
  139. }
  140. break;
  141. default:
  142. save_and_next();
  143. }
  144. }
  145. next(); /* skip the delimiter */
  146. *yytextLast = 0;
  147. yylval.vWord = lua_findconstant (yytext);
  148. return STRING;
  149. }
  150. case 'a': case 'b': case 'c': case 'd': case 'e':
  151. case 'f': case 'g': case 'h': case 'i': case 'j':
  152. case 'k': case 'l': case 'm': case 'n': case 'o':
  153. case 'p': case 'q': case 'r': case 's': case 't':
  154. case 'u': case 'v': case 'w': case 'x': case 'y':
  155. case 'z':
  156. case 'A': case 'B': case 'C': case 'D': case 'E':
  157. case 'F': case 'G': case 'H': case 'I': case 'J':
  158. case 'K': case 'L': case 'M': case 'N': case 'O':
  159. case 'P': case 'Q': case 'R': case 'S': case 'T':
  160. case 'U': case 'V': case 'W': case 'X': case 'Y':
  161. case 'Z':
  162. case '_':
  163. {
  164. int res;
  165. do { save_and_next(); } while (isalnum(current) || current == '_');
  166. *yytextLast = 0;
  167. res = findReserved(yytext);
  168. if (res) return res;
  169. yylval.vWord = lua_findsymbol(yytext);
  170. return NAME;
  171. }
  172. case '.':
  173. save_and_next();
  174. if (current == '.')
  175. {
  176. save_and_next();
  177. return CONC;
  178. }
  179. else if (!isdigit(current)) return '.';
  180. /* current is a digit: goes through to number */
  181. goto fraction;
  182. case '0': case '1': case '2': case '3': case '4':
  183. case '5': case '6': case '7': case '8': case '9':
  184. do { save_and_next(); } while (isdigit(current));
  185. if (current == '.') save_and_next();
  186. fraction: while (isdigit(current)) save_and_next();
  187. if (current == 'e' || current == 'E')
  188. {
  189. save_and_next();
  190. if (current == '+' || current == '-') save_and_next();
  191. if (!isdigit(current)) return WRONGTOKEN;
  192. do { save_and_next(); } while (isdigit(current));
  193. }
  194. *yytextLast = 0;
  195. yylval.vFloat = atof(yytext);
  196. return NUMBER;
  197. default: /* also end of file */
  198. {
  199. save_and_next();
  200. return *yytext;
  201. }
  202. }
  203. }
  204. }