lex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. char *rcs_lex = "$Id: lex.c,v 2.31 1996/03/19 16:50:24 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 = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
  70. yytextLast = yytext + size;
  71. }
  72. static int read_long_string (void)
  73. {
  74. int cont = 0;
  75. int spaceleft = textsize - (yytextLast - yytext);
  76. while (1)
  77. {
  78. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  79. {
  80. growtext();
  81. spaceleft = textsize - (yytextLast - yytext);
  82. }
  83. switch (current)
  84. {
  85. case EOF:
  86. case 0:
  87. return WRONGTOKEN;
  88. case '[':
  89. save_and_next(); spaceleft--;
  90. if (current == '[')
  91. {
  92. cont++;
  93. save_and_next(); spaceleft--;
  94. }
  95. continue;
  96. case ']':
  97. save_and_next(); spaceleft--;
  98. if (current == ']')
  99. {
  100. if (cont == 0) return STRING;
  101. cont--;
  102. save_and_next(); spaceleft--;
  103. }
  104. continue;
  105. case '\n':
  106. lua_linenumber++; /* goes through */
  107. default:
  108. save_and_next(); spaceleft--;
  109. }
  110. }
  111. }
  112. int luaY_lex (void)
  113. {
  114. double a;
  115. static int linelasttoken = 0;
  116. if (lua_debug)
  117. luaI_codedebugline(linelasttoken);
  118. linelasttoken = lua_linenumber;
  119. while (1)
  120. {
  121. yytextLast = yytext;
  122. switch (current)
  123. {
  124. case EOF:
  125. case 0:
  126. return 0;
  127. case '\n': linelasttoken = ++lua_linenumber;
  128. case ' ':
  129. case '\r': /* CR: to avoid problems with DOS/Windows */
  130. case '\t':
  131. next();
  132. continue;
  133. case '$':
  134. next();
  135. while (isalnum(current) || current == '_')
  136. save_and_next();
  137. *yytextLast = 0;
  138. if (strcmp(yytext, "debug") == 0)
  139. {
  140. luaY_lval.vInt = 1;
  141. return DEBUG;
  142. }
  143. else if (strcmp(yytext, "nodebug") == 0)
  144. {
  145. luaY_lval.vInt = 0;
  146. return DEBUG;
  147. }
  148. return WRONGTOKEN;
  149. case '-':
  150. save_and_next();
  151. if (current != '-') return '-';
  152. do { next(); } while (current != '\n' && current != 0);
  153. continue;
  154. case '[':
  155. save_and_next();
  156. if (current != '[') return '[';
  157. else
  158. {
  159. save_and_next(); /* pass the second '[' */
  160. if (read_long_string() == WRONGTOKEN)
  161. return WRONGTOKEN;
  162. save_and_next(); /* pass the second ']' */
  163. *(yytextLast-2) = 0; /* erases ']]' */
  164. luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
  165. return STRING;
  166. }
  167. case '=':
  168. save_and_next();
  169. if (current != '=') return '=';
  170. else { save_and_next(); return EQ; }
  171. case '<':
  172. save_and_next();
  173. if (current != '=') return '<';
  174. else { save_and_next(); return LE; }
  175. case '>':
  176. save_and_next();
  177. if (current != '=') return '>';
  178. else { save_and_next(); return GE; }
  179. case '~':
  180. save_and_next();
  181. if (current != '=') return '~';
  182. else { save_and_next(); return NE; }
  183. case '"':
  184. case '\'':
  185. {
  186. int del = current;
  187. int spaceleft = textsize - (yytextLast - yytext);
  188. next(); /* skip the delimiter */
  189. while (current != del)
  190. {
  191. if (spaceleft <= 2) /* may read more than 1 char in one cicle */
  192. {
  193. growtext();
  194. spaceleft = textsize - (yytextLast - yytext);
  195. }
  196. spaceleft--;
  197. switch (current)
  198. {
  199. case EOF:
  200. case 0:
  201. case '\n':
  202. return WRONGTOKEN;
  203. case '\\':
  204. next(); /* do not save the '\' */
  205. switch (current)
  206. {
  207. case 'n': save('\n'); next(); break;
  208. case 't': save('\t'); next(); break;
  209. case 'r': save('\r'); next(); break;
  210. case '\n': lua_linenumber++; /* goes through */
  211. default : save(current); next(); break;
  212. }
  213. break;
  214. default:
  215. save_and_next();
  216. }
  217. }
  218. next(); /* skip the delimiter */
  219. *yytextLast = 0;
  220. luaY_lval.vWord = luaI_findconstantbyname(yytext);
  221. return STRING;
  222. }
  223. case 'a': case 'b': case 'c': case 'd': case 'e':
  224. case 'f': case 'g': case 'h': case 'i': case 'j':
  225. case 'k': case 'l': case 'm': case 'n': case 'o':
  226. case 'p': case 'q': case 'r': case 's': case 't':
  227. case 'u': case 'v': case 'w': case 'x': case 'y':
  228. case 'z':
  229. case 'A': case 'B': case 'C': case 'D': case 'E':
  230. case 'F': case 'G': case 'H': case 'I': case 'J':
  231. case 'K': case 'L': case 'M': case 'N': case 'O':
  232. case 'P': case 'Q': case 'R': case 'S': case 'T':
  233. case 'U': case 'V': case 'W': case 'X': case 'Y':
  234. case 'Z':
  235. case '_':
  236. {
  237. TaggedString *ts;
  238. do { save_and_next(); } while (isalnum(current) || current == '_');
  239. *yytextLast = 0;
  240. ts = lua_createstring(yytext);
  241. if (ts->marked > 2)
  242. return ts->marked; /* reserved word */
  243. luaY_lval.pTStr = ts;
  244. ts->marked = 2; /* avoid GC */
  245. return NAME;
  246. }
  247. case '.':
  248. save_and_next();
  249. if (current == '.')
  250. {
  251. save_and_next();
  252. return CONC;
  253. }
  254. else if (!isdigit(current)) return '.';
  255. /* current is a digit: goes through to number */
  256. a=0.0;
  257. goto fraction;
  258. case '0': case '1': case '2': case '3': case '4':
  259. case '5': case '6': case '7': case '8': case '9':
  260. a=0.0;
  261. do { a=10.0*a+(current-'0'); save_and_next(); } while (isdigit(current));
  262. if (current == '.') save_and_next();
  263. fraction:
  264. { double da=0.1;
  265. while (isdigit(current))
  266. {a+=(current-'0')*da; da/=10.0; save_and_next()};
  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)) return WRONGTOKEN;
  276. do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current));
  277. for (ea=neg?0.1:10.0; e>0; e>>=1)
  278. {
  279. if (e & 1) a*=ea;
  280. ea*=ea;
  281. }
  282. }
  283. luaY_lval.vFloat = a;
  284. return NUMBER;
  285. }
  286. default: /* also end of file */
  287. {
  288. save_and_next();
  289. return yytext[0];
  290. }
  291. }
  292. }
  293. }