llex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. ** $Id: llex.c,v 1.29 1999/02/25 15:17:01 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. {
  25. int i;
  26. for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
  27. TaggedString *ts = luaS_new(reserved[i]);
  28. ts->head.marked = FIRST_RESERVED+i; /* reserved word (always > 255) */
  29. }
  30. }
  31. void luaX_syntaxerror (LexState *ls, char *s, char *token) {
  32. if (token[0] == '\0')
  33. token = "<eof>";
  34. luaL_verror("%.100s;\n last token read: `%.50s' at line %d in chunk `%.50s'",
  35. s, token, ls->linenumber, zname(ls->lex_z));
  36. }
  37. void luaX_error (LexState *ls, char *s) {
  38. save('\0');
  39. luaX_syntaxerror(ls, s, luaL_buffer());
  40. }
  41. void luaX_token2str (int token, char *s) {
  42. if (token < 255) {
  43. s[0] = (char)token;
  44. s[1] = '\0';
  45. }
  46. else
  47. strcpy(s, reserved[token-FIRST_RESERVED]);
  48. }
  49. static void luaX_invalidchar (LexState *ls, int c) {
  50. char buff[10];
  51. sprintf(buff, "0x%X", c);
  52. luaX_syntaxerror(ls, "invalid control char", buff);
  53. }
  54. static void firstline (LexState *LS)
  55. {
  56. int c = zgetc(LS->lex_z);
  57. if (c == '#')
  58. while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
  59. zungetc(LS->lex_z);
  60. }
  61. void luaX_setinput (LexState *LS, ZIO *z)
  62. {
  63. LS->current = '\n';
  64. LS->linenumber = 0;
  65. LS->iflevel = 0;
  66. LS->ifstate[0].skip = 0;
  67. LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
  68. LS->lex_z = z;
  69. LS->fs = NULL;
  70. firstline(LS);
  71. luaL_resetbuffer();
  72. }
  73. /*
  74. ** =======================================================
  75. ** PRAGMAS
  76. ** =======================================================
  77. */
  78. #define PRAGMASIZE 20
  79. static void skipspace (LexState *LS)
  80. {
  81. while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
  82. next(LS);
  83. }
  84. static int checkcond (LexState *LS, char *buff)
  85. {
  86. static char *opts[] = {"nil", "1", NULL};
  87. int i = luaL_findstring(buff, opts);
  88. if (i >= 0) return i;
  89. else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
  90. return luaS_globaldefined(buff);
  91. else {
  92. luaX_syntaxerror(LS, "invalid $if condition", buff);
  93. return 0; /* to avoid warnings */
  94. }
  95. }
  96. static void readname (LexState *LS, char *buff)
  97. {
  98. int i = 0;
  99. skipspace(LS);
  100. while (isalnum(LS->current) || LS->current == '_') {
  101. if (i >= PRAGMASIZE) {
  102. buff[PRAGMASIZE] = 0;
  103. luaX_syntaxerror(LS, "pragma too long", buff);
  104. }
  105. buff[i++] = (char)LS->current;
  106. next(LS);
  107. }
  108. buff[i] = 0;
  109. }
  110. static void inclinenumber (LexState *LS);
  111. static void ifskip (LexState *LS)
  112. {
  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. {
  123. static char *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:
  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. LS->seminfo.r = luaO_str2d(L->Mbuffer+L->Mbuffbase);
  347. if (LS->seminfo.r < 0)
  348. luaX_error(LS, "invalid numeric format");
  349. return NUMBER;
  350. case EOZ:
  351. if (LS->iflevel > 0)
  352. luaX_error(LS, "input ends inside a $if");
  353. return EOS;
  354. default:
  355. if (LS->current != '_' && !isalpha(LS->current)) {
  356. int c = LS->current;
  357. if (iscntrl(c))
  358. luaX_invalidchar(LS, c);
  359. save_and_next(LS);
  360. return c;
  361. }
  362. else { /* identifier or reserved word */
  363. TaggedString *ts;
  364. do {
  365. save_and_next(LS);
  366. } while (isalnum(LS->current) || LS->current == '_');
  367. save('\0');
  368. ts = luaS_new(L->Mbuffer+L->Mbuffbase);
  369. if (ts->head.marked >= FIRST_RESERVED)
  370. return ts->head.marked; /* reserved word */
  371. LS->seminfo.ts = ts;
  372. return NAME;
  373. }
  374. }
  375. }
  376. }