llex.c 11 KB

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