lex.c 5.4 KB

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