lex.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. char *rcs_lex = "$Id: lex.c,v 2.27 1996/02/14 13:35:51 roberto Exp roberto $";
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include "mem.h"
  6. #include "tree.h"
  7. #include "table.h"
  8. #include "lex.h"
  9. #include "inout.h"
  10. #include "luadebug.h"
  11. #include "parser.h"
  12. #include "ugly.h"
  13. #define MINBUFF 260
  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 = NULL;
  19. static int textsize = 0;
  20. static char *yytextLast;
  21. static Input input;
  22. void lua_setinput (Input fn)
  23. {
  24. current = ' ';
  25. input = fn;
  26. if (yytext == NULL)
  27. {
  28. textsize = MINBUFF;
  29. yytext = newvector(textsize, char);
  30. }
  31. }
  32. char *lua_lasttext (void)
  33. {
  34. *yytextLast = 0;
  35. return yytext;
  36. }
  37. static struct
  38. {
  39. char *name;
  40. int token;
  41. } reserved [] = {
  42. {"and", AND},
  43. {"do", DO},
  44. {"else", ELSE},
  45. {"elseif", ELSEIF},
  46. {"end", END},
  47. {"function", FUNCTION},
  48. {"if", IF},
  49. {"local", LOCAL},
  50. {"nil", NIL},
  51. {"not", NOT},
  52. {"or", OR},
  53. {"repeat", REPEAT},
  54. {"return", RETURN},
  55. {"then", THEN},
  56. {"until", UNTIL},
  57. {"while", WHILE} };
  58. #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
  59. void luaI_addReserved (void)
  60. {
  61. int i;
  62. for (i=0; i<RESERVEDSIZE; i++)
  63. {
  64. TaggedString *ts = lua_createstring(reserved[i].name);
  65. ts->marked = reserved[i].token; /* reserved word (always > 255) */
  66. }
  67. }
  68. static void growtext (void)
  69. {
  70. int size = yytextLast - yytext;
  71. textsize *= 2;
  72. yytext = growvector(yytext, textsize, char);
  73. yytextLast = yytext + size;
  74. }
  75. static int read_long_string (void)
  76. {
  77. int cont = 0;
  78. int spaceleft = textsize - (yytextLast - yytext);
  79. while (1)
  80. {
  81. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  82. {
  83. growtext();
  84. spaceleft = textsize - (yytextLast - yytext);
  85. }
  86. switch (current)
  87. {
  88. case EOF:
  89. case 0:
  90. return WRONGTOKEN;
  91. case '[':
  92. save_and_next(); spaceleft--;
  93. if (current == '[')
  94. {
  95. cont++;
  96. save_and_next(); spaceleft--;
  97. }
  98. continue;
  99. case ']':
  100. save_and_next(); spaceleft--;
  101. if (current == ']')
  102. {
  103. if (cont == 0) return STRING;
  104. cont--;
  105. save_and_next(); spaceleft--;
  106. }
  107. continue;
  108. case '\n':
  109. lua_linenumber++; /* goes through */
  110. default:
  111. save_and_next(); spaceleft--;
  112. }
  113. }
  114. }
  115. int luaY_lex (void)
  116. {
  117. float a;
  118. static int linelasttoken = 0;
  119. if (lua_debug)
  120. luaI_codedebugline(linelasttoken);
  121. linelasttoken = lua_linenumber;
  122. while (1)
  123. {
  124. yytextLast = yytext;
  125. #if 0
  126. fprintf(stderr,"'%c' %d\n",current,current);
  127. #endif
  128. switch (current)
  129. {
  130. case EOF:
  131. case 0:
  132. return 0;
  133. case '\n': linelasttoken = ++lua_linenumber;
  134. case ' ':
  135. case '\r': /* CR: to avoid problems with DOS/Windows */
  136. case '\t':
  137. next();
  138. continue;
  139. case '$':
  140. next();
  141. while (isalnum(current) || current == '_')
  142. save_and_next();
  143. *yytextLast = 0;
  144. if (strcmp(yytext, "debug") == 0)
  145. {
  146. luaY_lval.vInt = 1;
  147. return DEBUG;
  148. }
  149. else if (strcmp(yytext, "nodebug") == 0)
  150. {
  151. luaY_lval.vInt = 0;
  152. return DEBUG;
  153. }
  154. return WRONGTOKEN;
  155. case '-':
  156. save_and_next();
  157. if (current != '-') return '-'; /* else goes through */
  158. case '#':
  159. do { next(); } while (current != '\n' && current != 0);
  160. continue;
  161. case '[':
  162. save_and_next();
  163. if (current != '[') return '[';
  164. else
  165. {
  166. save_and_next(); /* pass the second '[' */
  167. if (read_long_string() == WRONGTOKEN)
  168. return WRONGTOKEN;
  169. save_and_next(); /* pass the second ']' */
  170. *(yytextLast-2) = 0; /* erases ']]' */
  171. luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
  172. return STRING;
  173. }
  174. case '=':
  175. save_and_next();
  176. if (current != '=') return '=';
  177. else { save_and_next(); return EQ; }
  178. case '<':
  179. save_and_next();
  180. if (current != '=') return '<';
  181. else { save_and_next(); return LE; }
  182. case '>':
  183. save_and_next();
  184. if (current != '=') return '>';
  185. else { save_and_next(); return GE; }
  186. case '~':
  187. save_and_next();
  188. if (current != '=') return '~';
  189. else { save_and_next(); return NE; }
  190. case '"':
  191. case '\'':
  192. {
  193. int del = current;
  194. int spaceleft = textsize - (yytextLast - yytext);
  195. next(); /* skip the delimiter */
  196. while (current != del)
  197. {
  198. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  199. {
  200. growtext();
  201. spaceleft = textsize - (yytextLast - yytext);
  202. }
  203. spaceleft--;
  204. switch (current)
  205. {
  206. case EOF:
  207. case 0:
  208. case '\n':
  209. return WRONGTOKEN;
  210. case '\\':
  211. next(); /* do not save the '\' */
  212. switch (current)
  213. {
  214. case 'n': save('\n'); next(); break;
  215. case 't': save('\t'); next(); break;
  216. case 'r': save('\r'); next(); break;
  217. case '\n': lua_linenumber++; /* goes through */
  218. default : save(current); next(); break;
  219. }
  220. break;
  221. default:
  222. save_and_next();
  223. }
  224. }
  225. next(); /* skip the delimiter */
  226. *yytextLast = 0;
  227. luaY_lval.vWord = luaI_findconstantbyname(yytext);
  228. return STRING;
  229. }
  230. case 'a': case 'b': case 'c': case 'd': case 'e':
  231. case 'f': case 'g': case 'h': case 'i': case 'j':
  232. case 'k': case 'l': case 'm': case 'n': case 'o':
  233. case 'p': case 'q': case 'r': case 's': case 't':
  234. case 'u': case 'v': case 'w': case 'x': case 'y':
  235. case 'z':
  236. case 'A': case 'B': case 'C': case 'D': case 'E':
  237. case 'F': case 'G': case 'H': case 'I': case 'J':
  238. case 'K': case 'L': case 'M': case 'N': case 'O':
  239. case 'P': case 'Q': case 'R': case 'S': case 'T':
  240. case 'U': case 'V': case 'W': case 'X': case 'Y':
  241. case 'Z':
  242. case '_':
  243. {
  244. TaggedString *ts;
  245. do { save_and_next(); } while (isalnum(current) || current == '_');
  246. *yytextLast = 0;
  247. ts = lua_createstring(yytext);
  248. if (ts->marked > 2)
  249. return ts->marked; /* reserved word */
  250. luaY_lval.pTStr = ts;
  251. ts->marked = 2; /* avoid GC */
  252. return NAME;
  253. }
  254. case '.':
  255. save_and_next();
  256. if (current == '.')
  257. {
  258. save_and_next();
  259. return CONC;
  260. }
  261. else if (!isdigit(current)) return '.';
  262. /* current is a digit: goes through to number */
  263. a=0.0;
  264. goto fraction;
  265. case '0': case '1': case '2': case '3': case '4':
  266. case '5': case '6': case '7': case '8': case '9':
  267. a=0.0;
  268. do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
  269. if (current == '.') save_and_next();
  270. fraction:
  271. { float da=0.1;
  272. while (isdigit(current))
  273. {a+=(current-'0')*da; da/=10.0; save_and_next()};
  274. if (current == 'e' || current == 'E')
  275. {
  276. int e=0;
  277. int neg;
  278. float ea;
  279. save_and_next();
  280. neg=(current=='-');
  281. if (current == '+' || current == '-') save_and_next();
  282. if (!isdigit(current)) return WRONGTOKEN;
  283. do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
  284. for (ea=neg?0.1:10.0; e>0; e>>=1)
  285. {
  286. if (e & 1) a*=ea;
  287. ea*=ea;
  288. }
  289. }
  290. luaY_lval.vFloat = a;
  291. return NUMBER;
  292. }
  293. case U_and: case U_do: case U_else: case U_elseif: case U_end:
  294. case U_function: case U_if: case U_local: case U_nil: case U_not:
  295. case U_or: case U_repeat: case U_return: case U_then:
  296. case U_until: case U_while:
  297. {
  298. int old = current;
  299. next();
  300. return reserved[old-U_and].token;
  301. }
  302. case U_eq: next(); return EQ;
  303. case U_le: next(); return LE;
  304. case U_ge: next(); return GE;
  305. case U_ne: next(); return NE;
  306. case U_sc: next(); return CONC;
  307. default: /* also end of file */
  308. {
  309. save_and_next();
  310. return yytext[0];
  311. }
  312. }
  313. }
  314. }