llex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. ** $Id: llex.c,v 1.12 1997/12/22 17:52:20 roberto Exp roberto $
  3. ** Lexical Analizer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "lauxlib.h"
  9. #include "llex.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lparser.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "lstx.h"
  16. #include "luadebug.h"
  17. #include "lzio.h"
  18. int lua_debug=0;
  19. #define next(LS) (LS->current = zgetc(LS->lex_z))
  20. static struct {
  21. char *name;
  22. int token;
  23. } reserved [] = {
  24. {"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
  25. {"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
  26. {"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
  27. {"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
  28. };
  29. void luaX_init (void)
  30. {
  31. int i;
  32. for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
  33. TaggedString *ts = luaS_new(reserved[i].name);
  34. ts->head.marked = reserved[i].token; /* reserved word (always > 255) */
  35. }
  36. }
  37. static void firstline (LexState *LS)
  38. {
  39. int c = zgetc(LS->lex_z);
  40. if (c == '#') {
  41. LS->linenumber++;
  42. while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
  43. }
  44. zungetc(LS->lex_z);
  45. }
  46. void luaX_setinput (ZIO *z)
  47. {
  48. LexState *LS = L->lexstate;
  49. LS->current = '\n';
  50. LS->linelasttoken = 0;
  51. LS->linenumber = 0;
  52. LS->iflevel = 0;
  53. LS->ifstate[0].skip = 0;
  54. LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
  55. LS->lex_z = z;
  56. firstline(LS);
  57. luaL_resetbuffer();
  58. }
  59. /*
  60. ** =======================================================
  61. ** PRAGMAS
  62. ** =======================================================
  63. */
  64. #define PRAGMASIZE 20
  65. static void skipspace (LexState *LS)
  66. {
  67. while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
  68. next(LS);
  69. }
  70. static int checkcond (char *buff)
  71. {
  72. static char *opts[] = {"nil", "1", NULL};
  73. int i = luaO_findstring(buff, opts);
  74. if (i >= 0) return i;
  75. else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
  76. return luaS_globaldefined(buff);
  77. else {
  78. luaY_syntaxerror("invalid $if condition", buff);
  79. return 0; /* to avoid warnings */
  80. }
  81. }
  82. static void readname (LexState *LS, char *buff)
  83. {
  84. int i = 0;
  85. skipspace(LS);
  86. while (isalnum(LS->current) || LS->current == '_') {
  87. if (i >= PRAGMASIZE) {
  88. buff[PRAGMASIZE] = 0;
  89. luaY_syntaxerror("pragma too long", buff);
  90. }
  91. buff[i++] = LS->current;
  92. next(LS);
  93. }
  94. buff[i] = 0;
  95. }
  96. static void inclinenumber (LexState *LS);
  97. static void ifskip (LexState *LS)
  98. {
  99. while (LS->ifstate[LS->iflevel].skip) {
  100. if (LS->current == '\n')
  101. inclinenumber(LS);
  102. else if (LS->current == EOZ)
  103. luaY_syntaxerror("input ends inside a $if", "");
  104. else next(LS);
  105. }
  106. }
  107. static void inclinenumber (LexState *LS)
  108. {
  109. static char *pragmas [] =
  110. {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
  111. next(LS); /* skip '\n' */
  112. ++LS->linenumber;
  113. if (LS->current == '$') { /* is a pragma? */
  114. char buff[PRAGMASIZE+1];
  115. int ifnot = 0;
  116. int skip = LS->ifstate[LS->iflevel].skip;
  117. next(LS); /* skip $ */
  118. readname(LS, buff);
  119. switch (luaO_findstring(buff, pragmas)) {
  120. case 0: /* debug */
  121. if (!skip) lua_debug = 1;
  122. break;
  123. case 1: /* nodebug */
  124. if (!skip) lua_debug = 0;
  125. break;
  126. case 2: /* endinput */
  127. if (!skip) {
  128. LS->current = EOZ;
  129. LS->iflevel = 0; /* to allow $endinput inside a $if */
  130. }
  131. break;
  132. case 3: /* end */
  133. if (LS->iflevel-- == 0)
  134. luaY_syntaxerror("unmatched $end", "$end");
  135. break;
  136. case 4: /* ifnot */
  137. ifnot = 1;
  138. /* go through */
  139. case 5: /* if */
  140. if (LS->iflevel == MAX_IFS-1)
  141. luaY_syntaxerror("too many nested $ifs", "$if");
  142. readname(LS, buff);
  143. LS->iflevel++;
  144. LS->ifstate[LS->iflevel].elsepart = 0;
  145. LS->ifstate[LS->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
  146. LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
  147. break;
  148. case 6: /* else */
  149. if (LS->ifstate[LS->iflevel].elsepart)
  150. luaY_syntaxerror("unmatched $else", "$else");
  151. LS->ifstate[LS->iflevel].elsepart = 1;
  152. LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
  153. LS->ifstate[LS->iflevel].condition;
  154. break;
  155. default:
  156. luaY_syntaxerror("unknown pragma", buff);
  157. }
  158. skipspace(LS);
  159. if (LS->current == '\n') /* pragma must end with a '\n' ... */
  160. inclinenumber(LS);
  161. else if (LS->current != EOZ) /* or eof */
  162. luaY_syntaxerror("invalid pragma format", buff);
  163. ifskip(LS);
  164. }
  165. }
  166. /*
  167. ** =======================================================
  168. ** LEXICAL ANALIZER
  169. ** =======================================================
  170. */
  171. #define save(c) luaL_addchar(c)
  172. #define save_and_next(LS) (save(LS->current), next(LS))
  173. char *luaX_lasttoken (void)
  174. {
  175. save(0);
  176. return luaL_buffer();
  177. }
  178. static int read_long_string (LexState *LS, YYSTYPE *l)
  179. {
  180. int cont = 0;
  181. while (1) {
  182. switch (LS->current) {
  183. case EOZ:
  184. save(0);
  185. return WRONGTOKEN;
  186. case '[':
  187. save_and_next(LS);
  188. if (LS->current == '[') {
  189. cont++;
  190. save_and_next(LS);
  191. }
  192. continue;
  193. case ']':
  194. save_and_next(LS);
  195. if (LS->current == ']') {
  196. if (cont == 0) goto endloop;
  197. cont--;
  198. save_and_next(LS);
  199. }
  200. continue;
  201. case '\n':
  202. save('\n');
  203. inclinenumber(LS);
  204. continue;
  205. default:
  206. save_and_next(LS);
  207. }
  208. } endloop:
  209. save_and_next(LS); /* pass the second ']' */
  210. L->Mbuffer[L->Mbuffnext-2] = 0; /* erases ']]' */
  211. l->pTStr = luaS_new(L->Mbuffbase+2);
  212. L->Mbuffer[L->Mbuffnext-2] = ']'; /* restores ']]' */
  213. return STRING;
  214. }
  215. /* to avoid warnings; this declaration cannot be public since YYSTYPE
  216. ** cannot be visible in llex.h (otherwise there is an error, since
  217. ** the parser body redefines it!)
  218. */
  219. int luaY_lex (YYSTYPE *l);
  220. int luaY_lex (YYSTYPE *l)
  221. {
  222. LexState *LS = L->lexstate;
  223. double a;
  224. luaL_resetbuffer();
  225. if (lua_debug)
  226. luaY_codedebugline(LS->linelasttoken);
  227. LS->linelasttoken = LS->linenumber;
  228. while (1) {
  229. switch (LS->current) {
  230. case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
  231. next(LS);
  232. continue;
  233. case '\n':
  234. inclinenumber(LS);
  235. LS->linelasttoken = LS->linenumber;
  236. continue;
  237. case '-':
  238. save_and_next(LS);
  239. if (LS->current != '-') return '-';
  240. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  241. luaL_resetbuffer();
  242. continue;
  243. case '[':
  244. save_and_next(LS);
  245. if (LS->current != '[') return '[';
  246. else {
  247. save_and_next(LS); /* pass the second '[' */
  248. return read_long_string(LS, l);
  249. }
  250. case '=':
  251. save_and_next(LS);
  252. if (LS->current != '=') return '=';
  253. else { save_and_next(LS); return EQ; }
  254. case '<':
  255. save_and_next(LS);
  256. if (LS->current != '=') return '<';
  257. else { save_and_next(LS); return LE; }
  258. case '>':
  259. save_and_next(LS);
  260. if (LS->current != '=') return '>';
  261. else { save_and_next(LS); return GE; }
  262. case '~':
  263. save_and_next(LS);
  264. if (LS->current != '=') return '~';
  265. else { save_and_next(LS); return NE; }
  266. case '"':
  267. case '\'': {
  268. int del = LS->current;
  269. save_and_next(LS);
  270. while (LS->current != del) {
  271. switch (LS->current) {
  272. case EOZ:
  273. case '\n':
  274. save(0);
  275. return WRONGTOKEN;
  276. case '\\':
  277. next(LS); /* do not save the '\' */
  278. switch (LS->current) {
  279. case 'n': save('\n'); next(LS); break;
  280. case 't': save('\t'); next(LS); break;
  281. case 'r': save('\r'); next(LS); break;
  282. case '\n': save('\n'); inclinenumber(LS); break;
  283. default : save_and_next(LS); break;
  284. }
  285. break;
  286. default:
  287. save_and_next(LS);
  288. }
  289. }
  290. next(LS); /* skip delimiter */
  291. save(0);
  292. l->pTStr = luaS_new(L->Mbuffbase+1);
  293. L->Mbuffer[L->Mbuffnext-1] = del; /* restore delimiter */
  294. return STRING;
  295. }
  296. case '.':
  297. save_and_next(LS);
  298. if (LS->current == '.')
  299. {
  300. save_and_next(LS);
  301. if (LS->current == '.')
  302. {
  303. save_and_next(LS);
  304. return DOTS; /* ... */
  305. }
  306. else return CONC; /* .. */
  307. }
  308. else if (!isdigit(LS->current)) return '.';
  309. /* LS->current is a digit: goes through to number */
  310. a=0.0;
  311. goto fraction;
  312. case '0': case '1': case '2': case '3': case '4':
  313. case '5': case '6': case '7': case '8': case '9':
  314. a=0.0;
  315. do {
  316. a=10.0*a+(LS->current-'0');
  317. save_and_next(LS);
  318. } while (isdigit(LS->current));
  319. if (LS->current == '.') {
  320. save_and_next(LS);
  321. if (LS->current == '.') {
  322. save(0);
  323. luaY_error(
  324. "ambiguous syntax (decimal point x string concatenation)");
  325. }
  326. }
  327. fraction:
  328. { double da=0.1;
  329. while (isdigit(LS->current))
  330. {
  331. a+=(LS->current-'0')*da;
  332. da/=10.0;
  333. save_and_next(LS);
  334. }
  335. if (toupper(LS->current) == 'E') {
  336. int e=0;
  337. int neg;
  338. double ea;
  339. save_and_next(LS);
  340. neg=(LS->current=='-');
  341. if (LS->current == '+' || LS->current == '-') save_and_next(LS);
  342. if (!isdigit(LS->current)) {
  343. save(0); return WRONGTOKEN; }
  344. do {
  345. e=10.0*e+(LS->current-'0');
  346. save_and_next(LS);
  347. } while (isdigit(LS->current));
  348. for (ea=neg?0.1:10.0; e>0; e>>=1)
  349. {
  350. if (e & 1) a*=ea;
  351. ea*=ea;
  352. }
  353. }
  354. l->vReal = a;
  355. return NUMBER;
  356. }
  357. case EOZ:
  358. save(0);
  359. if (LS->iflevel > 0)
  360. luaY_syntaxerror("input ends inside a $if", "");
  361. return 0;
  362. default:
  363. if (LS->current != '_' && !isalpha(LS->current)) {
  364. int c = LS->current;
  365. save_and_next(LS);
  366. return c;
  367. }
  368. else { /* identifier or reserved word */
  369. TaggedString *ts;
  370. do {
  371. save_and_next(LS);
  372. } while (isalnum(LS->current) || LS->current == '_');
  373. save(0);
  374. ts = luaS_new(L->Mbuffbase);
  375. if (ts->head.marked > 255)
  376. return ts->head.marked; /* reserved word */
  377. l->pTStr = ts;
  378. return NAME;
  379. }
  380. }
  381. }
  382. }