llex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. ** $Id: llex.c,v 1.98 2002/03/08 19:07:01 roberto Exp roberto $
  3. ** Lexical Analyzer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "lua.h"
  10. #include "llex.h"
  11. #include "lobject.h"
  12. #include "lparser.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "lzio.h"
  16. #define next(LS) (LS->current = zgetc(LS->z))
  17. /* ORDER RESERVED */
  18. static const char *const token2string [] = {
  19. "and", "break", "do", "else", "elseif",
  20. "end", "false", "for", "function", "global", "if",
  21. "in", "local", "nil", "not", "or", "repeat",
  22. "return", "then", "true", "until", "while", "",
  23. "..", "...", "==", ">=", "<=", "~=",
  24. "", "", "<eof>"
  25. };
  26. void luaX_init (lua_State *L) {
  27. int i;
  28. for (i=0; i<NUM_RESERVED; i++) {
  29. TString *ts = luaS_new(L, token2string[i]);
  30. lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
  31. ts->tsv.marked = cast(unsigned short, RESERVEDMARK+i); /* reserved word */
  32. }
  33. }
  34. #define MAXSRC 80
  35. void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
  36. if (val > limit) {
  37. char buff[90];
  38. sprintf(buff, "too many %.40s (limit=%d)", msg, limit);
  39. luaX_error(ls, buff, ls->t.token);
  40. }
  41. }
  42. static void luaX_syntaxerror (LexState *ls, const char *s,
  43. const char *token) {
  44. char buff[MAXSRC];
  45. luaO_chunkid(buff, getstr(ls->source), MAXSRC);
  46. luaO_verror(ls->L,
  47. "%.99s;\n last token read: `%.30s' at line %d in %.80s",
  48. s, token, ls->linenumber, buff);
  49. }
  50. void luaX_token2str (int token, char *s) {
  51. if (token < FIRST_RESERVED) {
  52. lua_assert(token == (char)token);
  53. s[0] = (char)token;
  54. s[1] = '\0';
  55. }
  56. else
  57. strcpy(s, token2string[token-FIRST_RESERVED]);
  58. }
  59. static char *token2str_all (LexState *ls, int token, char *s) {
  60. luaX_token2str(token, s);
  61. if (s[0] == '\0')
  62. return cast(char *, G(ls->L)->Mbuffer);
  63. else
  64. return s;
  65. }
  66. void luaX_error (LexState *ls, const char *s, int token) {
  67. char buff[TOKEN_LEN];
  68. luaX_syntaxerror(ls, s, token2str_all(ls, token, buff));
  69. }
  70. static void luaX_invalidchar (LexState *ls, int c) {
  71. char buff[8];
  72. sprintf(buff, "0x%02X", (unsigned char)c);
  73. luaX_syntaxerror(ls, "invalid control char", buff);
  74. }
  75. static void inclinenumber (LexState *LS) {
  76. next(LS); /* skip `\n' */
  77. ++LS->linenumber;
  78. luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
  79. }
  80. void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
  81. LS->L = L;
  82. LS->lookahead.token = TK_EOS; /* no look-ahead token */
  83. LS->z = z;
  84. LS->fs = NULL;
  85. LS->linenumber = 1;
  86. LS->lastline = 1;
  87. LS->source = source;
  88. next(LS); /* read first char */
  89. if (LS->current == '#') {
  90. do { /* skip first line */
  91. next(LS);
  92. } while (LS->current != '\n' && LS->current != EOZ);
  93. }
  94. }
  95. /*
  96. ** =======================================================
  97. ** LEXICAL ANALYZER
  98. ** =======================================================
  99. */
  100. /* use Mbuffer to store names, literal strings and numbers */
  101. #define EXTRABUFF 128
  102. #define checkbuffer(L, len) \
  103. if (((len)+10)*sizeof(char) > G(L)->Mbuffsize) \
  104. luaO_openspace(L, (len)+EXTRABUFF, char)
  105. #define save(L, c, l) (cast(char *, G(L)->Mbuffer)[l++] = cast(char, c))
  106. #define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
  107. static size_t readname (LexState *LS) {
  108. lua_State *L = LS->L;
  109. size_t l = 0;
  110. checkbuffer(L, l);
  111. do {
  112. checkbuffer(L, l);
  113. save_and_next(L, LS, l);
  114. } while (isalnum(LS->current) || LS->current == '_');
  115. save(L, '\0', l);
  116. return l-1;
  117. }
  118. /* LUA_NUMBER */
  119. static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
  120. lua_State *L = LS->L;
  121. size_t l = 0;
  122. checkbuffer(L, l);
  123. if (comma) save(L, '.', l);
  124. while (isdigit(LS->current)) {
  125. checkbuffer(L, l);
  126. save_and_next(L, LS, l);
  127. }
  128. if (LS->current == '.') {
  129. save_and_next(L, LS, l);
  130. if (LS->current == '.') {
  131. save_and_next(L, LS, l);
  132. save(L, '\0', l);
  133. luaX_error(LS,
  134. "ambiguous syntax (decimal point x string concatenation)",
  135. TK_NUMBER);
  136. }
  137. }
  138. while (isdigit(LS->current)) {
  139. checkbuffer(L, l);
  140. save_and_next(L, LS, l);
  141. }
  142. if (LS->current == 'e' || LS->current == 'E') {
  143. save_and_next(L, LS, l); /* read `E' */
  144. if (LS->current == '+' || LS->current == '-')
  145. save_and_next(L, LS, l); /* optional exponent sign */
  146. while (isdigit(LS->current)) {
  147. checkbuffer(L, l);
  148. save_and_next(L, LS, l);
  149. }
  150. }
  151. save(L, '\0', l);
  152. if (!luaO_str2d(cast(char *, G(L)->Mbuffer), &seminfo->r))
  153. luaX_error(LS, "malformed number", TK_NUMBER);
  154. }
  155. static void read_long_string (LexState *LS, SemInfo *seminfo) {
  156. lua_State *L = LS->L;
  157. int cont = 0;
  158. size_t l = 0;
  159. checkbuffer(L, l);
  160. save(L, '[', l); /* save first `[' */
  161. save_and_next(L, LS, l); /* pass the second `[' */
  162. if (LS->current == '\n') /* string starts with a newline? */
  163. inclinenumber(LS); /* skip it */
  164. for (;;) {
  165. checkbuffer(L, l);
  166. switch (LS->current) {
  167. case EOZ:
  168. save(L, '\0', l);
  169. luaX_error(LS, (seminfo) ? "unfinished long string" :
  170. "unfinished long comment", TK_EOS);
  171. break; /* to avoid warnings */
  172. case '[':
  173. save_and_next(L, LS, l);
  174. if (LS->current == '[') {
  175. cont++;
  176. save_and_next(L, LS, l);
  177. }
  178. continue;
  179. case ']':
  180. save_and_next(L, LS, l);
  181. if (LS->current == ']') {
  182. if (cont == 0) goto endloop;
  183. cont--;
  184. save_and_next(L, LS, l);
  185. }
  186. continue;
  187. case '\n':
  188. save(L, '\n', l);
  189. inclinenumber(LS);
  190. if (!seminfo) l = 0; /* reset buffer to avoid wasting space */
  191. continue;
  192. default:
  193. save_and_next(L, LS, l);
  194. }
  195. } endloop:
  196. save_and_next(L, LS, l); /* skip the second `]' */
  197. save(L, '\0', l);
  198. if (seminfo)
  199. seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+2, l-5);
  200. }
  201. static void read_string (LexState *LS, int del, SemInfo *seminfo) {
  202. lua_State *L = LS->L;
  203. size_t l = 0;
  204. checkbuffer(L, l);
  205. save_and_next(L, LS, l);
  206. while (LS->current != del) {
  207. checkbuffer(L, l);
  208. switch (LS->current) {
  209. case EOZ: case '\n':
  210. save(L, '\0', l);
  211. luaX_error(LS, "unfinished string",
  212. (LS->current == EOZ) ? TK_EOS : TK_STRING);
  213. break; /* to avoid warnings */
  214. case '\\':
  215. next(LS); /* do not save the `\' */
  216. switch (LS->current) {
  217. case 'a': save(L, '\a', l); next(LS); break;
  218. case 'b': save(L, '\b', l); next(LS); break;
  219. case 'f': save(L, '\f', l); next(LS); break;
  220. case 'n': save(L, '\n', l); next(LS); break;
  221. case 'r': save(L, '\r', l); next(LS); break;
  222. case 't': save(L, '\t', l); next(LS); break;
  223. case 'v': save(L, '\v', l); next(LS); break;
  224. case '\n': save(L, '\n', l); inclinenumber(LS); break;
  225. default: {
  226. if (!isdigit(LS->current))
  227. save_and_next(L, LS, l); /* handles \\, \", \', and \? */
  228. else { /* \xxx */
  229. int c = 0;
  230. int i = 0;
  231. do {
  232. c = 10*c + (LS->current-'0');
  233. next(LS);
  234. } while (++i<3 && isdigit(LS->current));
  235. if (c > UCHAR_MAX) {
  236. save(L, '\0', l);
  237. luaX_error(LS, "escape sequence too large", TK_STRING);
  238. }
  239. save(L, c, l);
  240. }
  241. }
  242. }
  243. break;
  244. default:
  245. save_and_next(L, LS, l);
  246. }
  247. }
  248. save_and_next(L, LS, l); /* skip delimiter */
  249. save(L, '\0', l);
  250. seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+1, l-3);
  251. }
  252. int luaX_lex (LexState *LS, SemInfo *seminfo) {
  253. for (;;) {
  254. switch (LS->current) {
  255. case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
  256. next(LS);
  257. continue;
  258. case '\n':
  259. inclinenumber(LS);
  260. continue;
  261. case '-':
  262. next(LS);
  263. if (LS->current != '-') return '-';
  264. /* else is a comment */
  265. next(LS);
  266. if (LS->current == '[' && (next(LS), LS->current == '['))
  267. read_long_string(LS, NULL); /* long comment */
  268. else /* short comment */
  269. while (LS->current != '\n' && LS->current != EOZ)
  270. next(LS);
  271. continue;
  272. case '[':
  273. next(LS);
  274. if (LS->current != '[') return '[';
  275. else {
  276. read_long_string(LS, seminfo);
  277. return TK_STRING;
  278. }
  279. case '=':
  280. next(LS);
  281. if (LS->current != '=') return '=';
  282. else { next(LS); return TK_EQ; }
  283. case '<':
  284. next(LS);
  285. if (LS->current != '=') return '<';
  286. else { next(LS); return TK_LE; }
  287. case '>':
  288. next(LS);
  289. if (LS->current != '=') return '>';
  290. else { next(LS); return TK_GE; }
  291. case '~':
  292. next(LS);
  293. if (LS->current != '=') return '~';
  294. else { next(LS); return TK_NE; }
  295. case '"':
  296. case '\'':
  297. read_string(LS, LS->current, seminfo);
  298. return TK_STRING;
  299. case '.':
  300. next(LS);
  301. if (LS->current == '.') {
  302. next(LS);
  303. if (LS->current == '.') {
  304. next(LS);
  305. return TK_DOTS; /* ... */
  306. }
  307. else return TK_CONCAT; /* .. */
  308. }
  309. else if (!isdigit(LS->current)) return '.';
  310. else {
  311. read_number(LS, 1, seminfo);
  312. return TK_NUMBER;
  313. }
  314. case EOZ:
  315. return TK_EOS;
  316. default: {
  317. if (isdigit(LS->current)) {
  318. read_number(LS, 0, seminfo);
  319. return TK_NUMBER;
  320. }
  321. else if (isalpha(LS->current) || LS->current == '_') {
  322. /* identifier or reserved word */
  323. size_t l = readname(LS);
  324. TString *ts = luaS_newlstr(LS->L, cast(char *, G(LS->L)->Mbuffer), l);
  325. if (ts->tsv.marked >= RESERVEDMARK) /* reserved word? */
  326. return ts->tsv.marked-RESERVEDMARK+FIRST_RESERVED;
  327. seminfo->ts = ts;
  328. return TK_NAME;
  329. }
  330. else {
  331. int c = LS->current;
  332. if (iscntrl(c))
  333. luaX_invalidchar(LS, c);
  334. next(LS);
  335. return c; /* single-char tokens (+ - / ...) */
  336. }
  337. }
  338. }
  339. }
  340. }