lex.c 7.7 KB

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