llex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. ** $Id: llex.c,v 1.33 1999/03/11 18:59:19 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 40
  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 %.50s",
  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[10];
  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. #define PRAGMASIZE 20
  81. static void skipspace (LexState *LS)
  82. {
  83. while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
  84. next(LS);
  85. }
  86. static int checkcond (LexState *LS, char *buff)
  87. {
  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. {
  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. {
  115. while (LS->ifstate[LS->iflevel].skip) {
  116. if (LS->current == '\n')
  117. inclinenumber(LS);
  118. else if (LS->current == EOZ)
  119. luaX_error(LS, "input ends inside a $if");
  120. else next(LS);
  121. }
  122. }
  123. static void inclinenumber (LexState *LS)
  124. {
  125. static char *pragmas [] =
  126. {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
  127. next(LS); /* skip '\n' */
  128. ++LS->linenumber;
  129. if (LS->current == '$') { /* is a pragma? */
  130. char buff[PRAGMASIZE+1];
  131. int ifnot = 0;
  132. int skip = LS->ifstate[LS->iflevel].skip;
  133. next(LS); /* skip $ */
  134. readname(LS, buff);
  135. switch (luaL_findstring(buff, pragmas)) {
  136. case 0: /* debug */
  137. if (!skip) L->debug = 1;
  138. break;
  139. case 1: /* nodebug */
  140. if (!skip) L->debug = 0;
  141. break;
  142. case 2: /* endinput */
  143. if (!skip) {
  144. LS->current = EOZ;
  145. LS->iflevel = 0; /* to allow $endinput inside a $if */
  146. }
  147. break;
  148. case 3: /* end */
  149. if (LS->iflevel-- == 0)
  150. luaX_syntaxerror(LS, "unmatched $end", "$end");
  151. break;
  152. case 4: /* ifnot */
  153. ifnot = 1;
  154. /* go through */
  155. case 5: /* if */
  156. if (LS->iflevel == MAX_IFS-1)
  157. luaX_syntaxerror(LS, "too many nested $ifs", "$if");
  158. readname(LS, buff);
  159. LS->iflevel++;
  160. LS->ifstate[LS->iflevel].elsepart = 0;
  161. LS->ifstate[LS->iflevel].condition = checkcond(LS, buff) ? !ifnot : ifnot;
  162. LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
  163. break;
  164. case 6: /* else */
  165. if (LS->ifstate[LS->iflevel].elsepart)
  166. luaX_syntaxerror(LS, "unmatched $else", "$else");
  167. LS->ifstate[LS->iflevel].elsepart = 1;
  168. LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
  169. LS->ifstate[LS->iflevel].condition;
  170. break;
  171. default:
  172. luaX_syntaxerror(LS, "unknown pragma", buff);
  173. }
  174. skipspace(LS);
  175. if (LS->current == '\n') /* pragma must end with a '\n' ... */
  176. inclinenumber(LS);
  177. else if (LS->current != EOZ) /* or eof */
  178. luaX_syntaxerror(LS, "invalid pragma format", buff);
  179. ifskip(LS);
  180. }
  181. }
  182. /*
  183. ** =======================================================
  184. ** LEXICAL ANALIZER
  185. ** =======================================================
  186. */
  187. static int read_long_string (LexState *LS) {
  188. int cont = 0;
  189. for (;;) {
  190. switch (LS->current) {
  191. case EOZ:
  192. luaX_error(LS, "unfinished long string");
  193. return EOS; /* to avoid warnings */
  194. case '[':
  195. save_and_next(LS);
  196. if (LS->current == '[') {
  197. cont++;
  198. save_and_next(LS);
  199. }
  200. continue;
  201. case ']':
  202. save_and_next(LS);
  203. if (LS->current == ']') {
  204. if (cont == 0) goto endloop;
  205. cont--;
  206. save_and_next(LS);
  207. }
  208. continue;
  209. case '\n':
  210. save('\n');
  211. inclinenumber(LS);
  212. continue;
  213. default:
  214. save_and_next(LS);
  215. }
  216. } endloop:
  217. save_and_next(LS); /* skip the second ']' */
  218. LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+2),
  219. L->Mbuffnext-L->Mbuffbase-4);
  220. return STRING;
  221. }
  222. int luaX_lex (LexState *LS) {
  223. luaL_resetbuffer();
  224. for (;;) {
  225. switch (LS->current) {
  226. case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
  227. next(LS);
  228. continue;
  229. case '\n':
  230. inclinenumber(LS);
  231. continue;
  232. case '-':
  233. save_and_next(LS);
  234. if (LS->current != '-') return '-';
  235. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  236. luaL_resetbuffer();
  237. continue;
  238. case '[':
  239. save_and_next(LS);
  240. if (LS->current != '[') return '[';
  241. else {
  242. save_and_next(LS); /* pass the second '[' */
  243. return read_long_string(LS);
  244. }
  245. case '=':
  246. save_and_next(LS);
  247. if (LS->current != '=') return '=';
  248. else { save_and_next(LS); return EQ; }
  249. case '<':
  250. save_and_next(LS);
  251. if (LS->current != '=') return '<';
  252. else { save_and_next(LS); return LE; }
  253. case '>':
  254. save_and_next(LS);
  255. if (LS->current != '=') return '>';
  256. else { save_and_next(LS); return GE; }
  257. case '~':
  258. save_and_next(LS);
  259. if (LS->current != '=') return '~';
  260. else { save_and_next(LS); return NE; }
  261. case '"':
  262. case '\'': {
  263. int del = LS->current;
  264. save_and_next(LS);
  265. while (LS->current != del) {
  266. switch (LS->current) {
  267. case EOZ:
  268. case '\n':
  269. luaX_error(LS, "unfinished string");
  270. return EOS; /* to avoid warnings */
  271. case '\\':
  272. next(LS); /* do not save the '\' */
  273. switch (LS->current) {
  274. case 'a': save('\a'); next(LS); break;
  275. case 'b': save('\b'); next(LS); break;
  276. case 'f': save('\f'); next(LS); break;
  277. case 'n': save('\n'); next(LS); break;
  278. case 'r': save('\r'); next(LS); break;
  279. case 't': save('\t'); next(LS); break;
  280. case 'v': save('\v'); next(LS); break;
  281. case '\n': save('\n'); inclinenumber(LS); break;
  282. default : {
  283. if (isdigit(LS->current)) {
  284. int c = 0;
  285. int i = 0;
  286. do {
  287. c = 10*c + (LS->current-'0');
  288. next(LS);
  289. } while (++i<3 && isdigit(LS->current));
  290. if (c != (unsigned char)c)
  291. luaX_error(LS, "escape sequence too large");
  292. save(c);
  293. }
  294. else { /* handles \, ", ', and ? */
  295. save(LS->current);
  296. next(LS);
  297. }
  298. break;
  299. }
  300. }
  301. break;
  302. default:
  303. save_and_next(LS);
  304. }
  305. }
  306. save_and_next(LS); /* skip delimiter */
  307. LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+1),
  308. L->Mbuffnext-L->Mbuffbase-2);
  309. return STRING;
  310. }
  311. case '.':
  312. save_and_next(LS);
  313. if (LS->current == '.')
  314. {
  315. save_and_next(LS);
  316. if (LS->current == '.')
  317. {
  318. save_and_next(LS);
  319. return DOTS; /* ... */
  320. }
  321. else return CONC; /* .. */
  322. }
  323. else if (!isdigit(LS->current)) return '.';
  324. goto fraction; /* LS->current is a digit: goes through to number */
  325. case '0': case '1': case '2': case '3': case '4':
  326. case '5': case '6': case '7': case '8': case '9':
  327. do {
  328. save_and_next(LS);
  329. } while (isdigit(LS->current));
  330. if (LS->current == '.') {
  331. save_and_next(LS);
  332. if (LS->current == '.') {
  333. save('.');
  334. luaX_error(LS,
  335. "ambiguous syntax (decimal point x string concatenation)");
  336. }
  337. }
  338. fraction:
  339. while (isdigit(LS->current))
  340. save_and_next(LS);
  341. if (toupper(LS->current) == 'E') {
  342. save_and_next(LS); /* read 'E' */
  343. save_and_next(LS); /* read '+', '-' or first digit */
  344. while (isdigit(LS->current))
  345. save_and_next(LS);
  346. }
  347. save('\0');
  348. LS->seminfo.r = luaO_str2d(L->Mbuffer+L->Mbuffbase);
  349. if (LS->seminfo.r < 0)
  350. luaX_error(LS, "invalid numeric format");
  351. return NUMBER;
  352. case EOZ:
  353. if (LS->iflevel > 0)
  354. luaX_error(LS, "input ends inside a $if");
  355. return EOS;
  356. default:
  357. if (LS->current != '_' && !isalpha(LS->current)) {
  358. int c = LS->current;
  359. if (iscntrl(c))
  360. luaX_invalidchar(LS, c);
  361. save_and_next(LS);
  362. return c;
  363. }
  364. else { /* identifier or reserved word */
  365. TaggedString *ts;
  366. do {
  367. save_and_next(LS);
  368. } while (isalnum(LS->current) || LS->current == '_');
  369. save('\0');
  370. ts = luaS_new(L->Mbuffer+L->Mbuffbase);
  371. if (ts->head.marked >= FIRST_RESERVED)
  372. return ts->head.marked; /* reserved word */
  373. LS->seminfo.ts = ts;
  374. return NAME;
  375. }
  376. }
  377. }
  378. }