lex.c 8.5 KB

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