lex.c 7.7 KB

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