lex.c 7.7 KB

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