lex.c 6.2 KB

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