llex.c 11 KB

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