lex.c 8.3 KB

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