llex.c 12 KB

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