lex.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. char *rcs_lex = "$Id: lex.c,v 2.10 1994/11/13 14:39:04 roberto Exp roberto $";
  2. #include <ctype.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "tree.h"
  8. #include "table.h"
  9. #include "opcode.h"
  10. #include "inout.h"
  11. #include "y.tab.h"
  12. #include "ugly.h"
  13. #define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
  14. #define next() { current = input(); }
  15. #define save(x) { *yytextLast++ = (x); }
  16. #define save_and_next() { save(current); next(); }
  17. static int current;
  18. static char yytext[256];
  19. static char *yytextLast;
  20. static Input input;
  21. void lua_setinput (Input fn)
  22. {
  23. current = ' ';
  24. input = fn;
  25. }
  26. char *lua_lasttext (void)
  27. {
  28. *yytextLast = 0;
  29. return yytext;
  30. }
  31. /* The reserved words must be listed in lexicographic order */
  32. static struct
  33. {
  34. char *name;
  35. int token;
  36. } reserved [] = {
  37. {"and", AND},
  38. {"do", DO},
  39. {"else", ELSE},
  40. {"elseif", ELSEIF},
  41. {"end", END},
  42. {"function", FUNCTION},
  43. {"if", IF},
  44. {"local", LOCAL},
  45. {"nil", NIL},
  46. {"not", NOT},
  47. {"or", OR},
  48. {"repeat", REPEAT},
  49. {"return", RETURN},
  50. {"then", THEN},
  51. {"until", UNTIL},
  52. {"while", WHILE} };
  53. #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
  54. static int findReserved (char *name)
  55. {
  56. int l = 0;
  57. int h = RESERVEDSIZE - 1;
  58. while (l <= h)
  59. {
  60. int m = (l+h)/2;
  61. int comp = lua_strcmp(name, reserved[m].name);
  62. if (comp < 0)
  63. h = m-1;
  64. else if (comp == 0)
  65. return reserved[m].token;
  66. else
  67. l = m+1;
  68. }
  69. return 0;
  70. }
  71. int yylex (void)
  72. {
  73. float a;
  74. while (1)
  75. {
  76. yytextLast = yytext;
  77. #if 0
  78. fprintf(stderr,"'%c' %d\n",current,current);
  79. #endif
  80. switch (current)
  81. {
  82. case EOF:
  83. case 0:
  84. return 0;
  85. case '\n': lua_linenumber++;
  86. case ' ':
  87. case '\t':
  88. next();
  89. continue;
  90. case '$':
  91. next();
  92. while (isalnum(current) || current == '_')
  93. save_and_next();
  94. *yytextLast = 0;
  95. if (lua_strcmp(yytext, "debug") == 0)
  96. {
  97. yylval.vInt = 1;
  98. return DEBUG;
  99. }
  100. else if (lua_strcmp(yytext, "nodebug") == 0)
  101. {
  102. yylval.vInt = 0;
  103. return DEBUG;
  104. }
  105. return WRONGTOKEN;
  106. case '-':
  107. save_and_next();
  108. if (current != '-') return '-';
  109. do { next(); } while (current != '\n' && current != 0);
  110. continue;
  111. case '=':
  112. save_and_next();
  113. if (current != '=') return '=';
  114. else { save_and_next(); return EQ; }
  115. case '<':
  116. save_and_next();
  117. if (current != '=') return '<';
  118. else { save_and_next(); return LE; }
  119. case '>':
  120. save_and_next();
  121. if (current != '=') return '>';
  122. else { save_and_next(); return GE; }
  123. case '~':
  124. save_and_next();
  125. if (current != '=') return '~';
  126. else { save_and_next(); return NE; }
  127. case '"':
  128. case '\'':
  129. {
  130. int del = current;
  131. next(); /* skip the delimiter */
  132. while (current != del)
  133. {
  134. switch (current)
  135. {
  136. case EOF:
  137. case 0:
  138. case '\n':
  139. return WRONGTOKEN;
  140. case '\\':
  141. next(); /* do not save the '\' */
  142. switch (current)
  143. {
  144. case 'n': save('\n'); next(); break;
  145. case 't': save('\t'); next(); break;
  146. case 'r': save('\r'); next(); break;
  147. case '\'': save('\''); next(); break;
  148. case '"': save('"'); next(); break;
  149. default : save(current); next(); break;
  150. }
  151. break;
  152. default:
  153. save_and_next();
  154. }
  155. }
  156. next(); /* skip the delimiter */
  157. *yytextLast = 0;
  158. yylval.vWord = luaI_findconstant(lua_constcreate(yytext));
  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);
  179. if (res) return res;
  180. yylval.pNode = lua_constcreate(yytext);
  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. a=0.0;
  193. goto fraction;
  194. case '0': case '1': case '2': case '3': case '4':
  195. case '5': case '6': case '7': case '8': case '9':
  196. a=0.0;
  197. do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
  198. if (current == '.') save_and_next();
  199. fraction:
  200. { float da=0.1;
  201. while (isdigit(current))
  202. {a+=(current-'0')*da; da/=10.0; save_and_next()};
  203. if (current == 'e' || current == 'E')
  204. {
  205. int e=0;
  206. int neg;
  207. float ea;
  208. save_and_next();
  209. neg=(current=='-');
  210. if (current == '+' || current == '-') save_and_next();
  211. if (!isdigit(current)) return WRONGTOKEN;
  212. do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
  213. for (ea=neg?0.1:10.0; e>0; e>>=1)
  214. {
  215. if (e & 1) a*=ea;
  216. ea*=ea;
  217. }
  218. }
  219. yylval.vFloat = a;
  220. return NUMBER;
  221. }
  222. case U_and: case U_do: case U_else: case U_elseif: case U_end:
  223. case U_function: case U_if: case U_local: case U_nil: case U_not:
  224. case U_or: case U_repeat: case U_return: case U_then:
  225. case U_until: case U_while:
  226. {
  227. int old = current;
  228. next();
  229. return reserved[old-U_and].token;
  230. }
  231. case U_eq: next(); return EQ;
  232. case U_le: next(); return LE;
  233. case U_ge: next(); return GE;
  234. case U_ne: next(); return NE;
  235. case U_sc: next(); return CONC;
  236. default: /* also end of file */
  237. {
  238. save_and_next();
  239. return yytext[0];
  240. }
  241. }
  242. }
  243. }