lex.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. char *rcs_lex = "$Id: lex.c,v 2.44 1997/03/31 14:17:09 roberto Exp roberto $";
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include "auxlib.h"
  5. #include "luamem.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 250
  13. #define next() (current = input())
  14. #define save(x) (yytext[tokensize++] = (x))
  15. #define save_and_next() (save(current), next())
  16. static int current; /* look ahead character */
  17. static Input input; /* input function */
  18. void lua_setinput (Input fn)
  19. {
  20. current = '\n';
  21. lua_linenumber = 0;
  22. input = fn;
  23. }
  24. static void luaI_auxsyntaxerror (char *s, char *token)
  25. {
  26. luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
  27. s, token, lua_linenumber, lua_parsedfile);
  28. }
  29. void luaI_syntaxerror (char *s)
  30. {
  31. char *token = luaI_buffer(1);
  32. if (token[0] == 0)
  33. token = "<eof>";
  34. luaI_auxsyntaxerror(s, token);
  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 int inclinenumber (int pragma_allowed)
  68. {
  69. ++lua_linenumber;
  70. if (pragma_allowed && current == '$') { /* is a pragma? */
  71. char buff[MINBUFF+1];
  72. int i = 0;
  73. next(); /* skip $ */
  74. while (isalnum((unsigned char)current)) {
  75. if (i >= MINBUFF) luaI_syntaxerror("pragma too long");
  76. buff[i++] = current;
  77. next();
  78. }
  79. buff[i] = 0;
  80. if (strcmp(buff, "debug") == 0)
  81. lua_debug = 1;
  82. else if (strcmp(buff, "nodebug") == 0)
  83. lua_debug = 0;
  84. else luaI_auxsyntaxerror("invalid pragma", buff);
  85. }
  86. return lua_linenumber;
  87. }
  88. static int read_long_string (char *yytext, int buffsize)
  89. {
  90. int cont = 0;
  91. int tokensize = 2; /* '[[' already stored */
  92. while (1)
  93. {
  94. if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
  95. yytext = luaI_buffer(buffsize *= 2);
  96. switch (current)
  97. {
  98. case 0:
  99. save(0);
  100. return WRONGTOKEN;
  101. case '[':
  102. save_and_next();
  103. if (current == '[')
  104. {
  105. cont++;
  106. save_and_next();
  107. }
  108. continue;
  109. case ']':
  110. save_and_next();
  111. if (current == ']')
  112. {
  113. if (cont == 0) goto endloop;
  114. cont--;
  115. save_and_next();
  116. }
  117. continue;
  118. case '\n':
  119. save_and_next();
  120. inclinenumber(0);
  121. continue;
  122. default:
  123. save_and_next();
  124. }
  125. } endloop:
  126. save_and_next(); /* pass the second ']' */
  127. yytext[tokensize-2] = 0; /* erases ']]' */
  128. luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
  129. yytext[tokensize-2] = ']'; /* restores ']]' */
  130. save(0);
  131. return STRING;
  132. }
  133. int luaY_lex (void)
  134. {
  135. static int linelasttoken = 0;
  136. double a;
  137. int buffsize = MINBUFF;
  138. char *yytext = luaI_buffer(buffsize);
  139. yytext[1] = yytext[2] = yytext[3] = 0;
  140. if (lua_debug)
  141. luaI_codedebugline(linelasttoken);
  142. linelasttoken = lua_linenumber;
  143. while (1)
  144. {
  145. int tokensize = 0;
  146. switch (current)
  147. {
  148. case '\n':
  149. next();
  150. linelasttoken = inclinenumber(1);
  151. continue;
  152. case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
  153. next();
  154. continue;
  155. case '-':
  156. save_and_next();
  157. if (current != '-') return '-';
  158. do { next(); } while (current != '\n' && current != 0);
  159. continue;
  160. case '[':
  161. save_and_next();
  162. if (current != '[') return '[';
  163. else
  164. {
  165. save_and_next(); /* pass the second '[' */
  166. return read_long_string(yytext, buffsize);
  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. save_and_next();
  189. while (current != del)
  190. {
  191. if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
  192. yytext = luaI_buffer(buffsize *= 2);
  193. switch (current)
  194. {
  195. case 0:
  196. case '\n':
  197. save(0);
  198. return WRONGTOKEN;
  199. case '\\':
  200. next(); /* do not save the '\' */
  201. switch (current)
  202. {
  203. case 'n': save('\n'); next(); break;
  204. case 't': save('\t'); next(); break;
  205. case 'r': save('\r'); next(); break;
  206. case '\n': save_and_next(); inclinenumber(0); break;
  207. default : save_and_next(); break;
  208. }
  209. break;
  210. default:
  211. save_and_next();
  212. }
  213. }
  214. next(); /* skip delimiter */
  215. save(0);
  216. luaY_lval.vWord = luaI_findconstantbyname(yytext+1);
  217. tokensize--;
  218. save(del); save(0); /* restore delimiter */
  219. return STRING;
  220. }
  221. case 'a': case 'b': case 'c': case 'd': case 'e':
  222. case 'f': case 'g': case 'h': case 'i': case 'j':
  223. case 'k': case 'l': case 'm': case 'n': case 'o':
  224. case 'p': case 'q': case 'r': case 's': case 't':
  225. case 'u': case 'v': case 'w': case 'x': case 'y':
  226. case 'z':
  227. case 'A': case 'B': case 'C': case 'D': case 'E':
  228. case 'F': case 'G': case 'H': case 'I': case 'J':
  229. case 'K': case 'L': case 'M': case 'N': case 'O':
  230. case 'P': case 'Q': case 'R': case 'S': case 'T':
  231. case 'U': case 'V': case 'W': case 'X': case 'Y':
  232. case 'Z':
  233. case '_':
  234. {
  235. TaggedString *ts;
  236. do {
  237. save_and_next();
  238. } while (isalnum((unsigned char)current) || current == '_');
  239. save(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. if (current == '.')
  253. {
  254. save_and_next();
  255. return DOTS; /* ... */
  256. }
  257. else return CONC; /* .. */
  258. }
  259. else if (!isdigit((unsigned char)current)) return '.';
  260. /* current is a digit: goes through to number */
  261. a=0.0;
  262. goto fraction;
  263. case '0': case '1': case '2': case '3': case '4':
  264. case '5': case '6': case '7': case '8': case '9':
  265. a=0.0;
  266. do {
  267. a=10.0*a+(current-'0');
  268. save_and_next();
  269. } while (isdigit((unsigned char)current));
  270. if (current == '.') {
  271. save_and_next();
  272. if (current == '.')
  273. luaI_syntaxerror(
  274. "ambiguous syntax (decimal point x string concatenation)");
  275. }
  276. fraction:
  277. { double da=0.1;
  278. while (isdigit((unsigned char)current))
  279. {
  280. a+=(current-'0')*da;
  281. da/=10.0;
  282. save_and_next();
  283. }
  284. if (current == 'e' || current == 'E')
  285. {
  286. int e=0;
  287. int neg;
  288. double ea;
  289. save_and_next();
  290. neg=(current=='-');
  291. if (current == '+' || current == '-') save_and_next();
  292. if (!isdigit((unsigned char)current)) {
  293. save(0); return WRONGTOKEN; }
  294. do {
  295. e=10.0*e+(current-'0');
  296. save_and_next();
  297. } while (isdigit((unsigned char)current));
  298. for (ea=neg?0.1:10.0; e>0; e>>=1)
  299. {
  300. if (e & 1) a*=ea;
  301. ea*=ea;
  302. }
  303. }
  304. luaY_lval.vFloat = a;
  305. save(0);
  306. return NUMBER;
  307. }
  308. case 0:
  309. save(0);
  310. return 0;
  311. default:
  312. save_and_next();
  313. return yytext[0];
  314. }
  315. }
  316. }