lex.c 7.8 KB

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