lex.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. char *rcs_lex = "$Id: lex.c,v 2.13 1994/12/20 21:20:36 roberto Exp celes $";
  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 "parser.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. default : save(current); next(); break;
  148. }
  149. break;
  150. default:
  151. save_and_next();
  152. }
  153. }
  154. next(); /* skip the delimiter */
  155. *yytextLast = 0;
  156. yylval.vWord = luaI_findconstant(lua_constcreate(yytext));
  157. return STRING;
  158. }
  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 'A': case 'B': case 'C': case 'D': case 'E':
  166. case 'F': case 'G': case 'H': case 'I': case 'J':
  167. case 'K': case 'L': case 'M': case 'N': case 'O':
  168. case 'P': case 'Q': case 'R': case 'S': case 'T':
  169. case 'U': case 'V': case 'W': case 'X': case 'Y':
  170. case 'Z':
  171. case '_':
  172. {
  173. Word res;
  174. do { save_and_next(); } while (isalnum(current) || current == '_');
  175. *yytextLast = 0;
  176. res = findReserved(yytext);
  177. if (res) return res;
  178. yylval.pNode = lua_constcreate(yytext);
  179. return NAME;
  180. }
  181. case '.':
  182. save_and_next();
  183. if (current == '.')
  184. {
  185. save_and_next();
  186. return CONC;
  187. }
  188. else if (!isdigit(current)) return '.';
  189. /* current is a digit: goes through to number */
  190. a=0.0;
  191. goto fraction;
  192. case '0': case '1': case '2': case '3': case '4':
  193. case '5': case '6': case '7': case '8': case '9':
  194. a=0.0;
  195. do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
  196. if (current == '.') save_and_next();
  197. fraction:
  198. { float da=0.1;
  199. while (isdigit(current))
  200. {a+=(current-'0')*da; da/=10.0; save_and_next()};
  201. if (current == 'e' || current == 'E')
  202. {
  203. int e=0;
  204. int neg;
  205. float ea;
  206. save_and_next();
  207. neg=(current=='-');
  208. if (current == '+' || current == '-') save_and_next();
  209. if (!isdigit(current)) return WRONGTOKEN;
  210. do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
  211. for (ea=neg?0.1:10.0; e>0; e>>=1)
  212. {
  213. if (e & 1) a*=ea;
  214. ea*=ea;
  215. }
  216. }
  217. yylval.vFloat = a;
  218. return NUMBER;
  219. }
  220. case U_and: case U_do: case U_else: case U_elseif: case U_end:
  221. case U_function: case U_if: case U_local: case U_nil: case U_not:
  222. case U_or: case U_repeat: case U_return: case U_then:
  223. case U_until: case U_while:
  224. {
  225. int old = current;
  226. next();
  227. return reserved[old-U_and].token;
  228. }
  229. case U_eq: next(); return EQ;
  230. case U_le: next(); return LE;
  231. case U_ge: next(); return GE;
  232. case U_ne: next(); return NE;
  233. case U_sc: next(); return CONC;
  234. default: /* also end of file */
  235. {
  236. save_and_next();
  237. return yytext[0];
  238. }
  239. }
  240. }
  241. }