llex.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. ** $Id: llex.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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, n, len) \
  103. if (((len)+(n))*sizeof(char) > G(L)->Mbuffsize) \
  104. luaO_openspace(L, (len)+(n)+EXTRABUFF, char)
  105. #define save(L, c, l) (cast(char *, G(L)->Mbuffer)[l++] = (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, 10, l);
  111. do {
  112. checkbuffer(L, 10, 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, 10, l);
  123. if (comma) save(L, '.', l);
  124. while (isdigit(LS->current)) {
  125. checkbuffer(L, 10, 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, 10, 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, 10, 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, 10, 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, 10, l);
  166. switch (LS->current) {
  167. case EOZ:
  168. save(L, '\0', l);
  169. luaX_error(LS, "unfinished long string", TK_STRING);
  170. break; /* to avoid warnings */
  171. case '[':
  172. save_and_next(L, LS, l);
  173. if (LS->current == '[') {
  174. cont++;
  175. save_and_next(L, LS, l);
  176. }
  177. continue;
  178. case ']':
  179. save_and_next(L, LS, l);
  180. if (LS->current == ']') {
  181. if (cont == 0) goto endloop;
  182. cont--;
  183. save_and_next(L, LS, l);
  184. }
  185. continue;
  186. case '\n':
  187. save(L, '\n', l);
  188. inclinenumber(LS);
  189. continue;
  190. default:
  191. save_and_next(L, LS, l);
  192. }
  193. } endloop:
  194. save_and_next(L, LS, l); /* skip the second `]' */
  195. save(L, '\0', l);
  196. seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+2, l-5);
  197. }
  198. static void read_string (LexState *LS, int del, SemInfo *seminfo) {
  199. lua_State *L = LS->L;
  200. size_t l = 0;
  201. checkbuffer(L, 10, l);
  202. save_and_next(L, LS, l);
  203. while (LS->current != del) {
  204. checkbuffer(L, 10, l);
  205. switch (LS->current) {
  206. case EOZ: case '\n':
  207. save(L, '\0', l);
  208. luaX_error(LS, "unfinished string", TK_STRING);
  209. break; /* to avoid warnings */
  210. case '\\':
  211. next(LS); /* do not save the `\' */
  212. switch (LS->current) {
  213. case 'a': save(L, '\a', l); next(LS); break;
  214. case 'b': save(L, '\b', l); next(LS); break;
  215. case 'f': save(L, '\f', l); next(LS); break;
  216. case 'n': save(L, '\n', l); next(LS); break;
  217. case 'r': save(L, '\r', l); next(LS); break;
  218. case 't': save(L, '\t', l); next(LS); break;
  219. case 'v': save(L, '\v', l); next(LS); break;
  220. case '\n': save(L, '\n', l); inclinenumber(LS); break;
  221. default: {
  222. if (!isdigit(LS->current))
  223. save_and_next(L, LS, l); /* handles \\, \", \', and \? */
  224. else { /* \xxx */
  225. int c = 0;
  226. int i = 0;
  227. do {
  228. c = 10*c + (LS->current-'0');
  229. next(LS);
  230. } while (++i<3 && isdigit(LS->current));
  231. if (c > UCHAR_MAX) {
  232. save(L, '\0', l);
  233. luaX_error(LS, "escape sequence too large", TK_STRING);
  234. }
  235. save(L, c, l);
  236. }
  237. }
  238. }
  239. break;
  240. default:
  241. save_and_next(L, LS, l);
  242. }
  243. }
  244. save_and_next(L, LS, l); /* skip delimiter */
  245. save(L, '\0', l);
  246. seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+1, l-3);
  247. }
  248. int luaX_lex (LexState *LS, SemInfo *seminfo) {
  249. for (;;) {
  250. switch (LS->current) {
  251. case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
  252. next(LS);
  253. continue;
  254. case '\n':
  255. inclinenumber(LS);
  256. continue;
  257. case '$':
  258. luaX_error(LS,
  259. "unexpected `$' (pragmas are no longer supported)",
  260. LS->current);
  261. break;
  262. case '-':
  263. next(LS);
  264. if (LS->current != '-') return '-';
  265. do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
  266. continue;
  267. case '[':
  268. next(LS);
  269. if (LS->current != '[') return '[';
  270. else {
  271. read_long_string(LS, seminfo);
  272. return TK_STRING;
  273. }
  274. case '=':
  275. next(LS);
  276. if (LS->current != '=') return '=';
  277. else { next(LS); return TK_EQ; }
  278. case '<':
  279. next(LS);
  280. if (LS->current != '=') return '<';
  281. else { next(LS); return TK_LE; }
  282. case '>':
  283. next(LS);
  284. if (LS->current != '=') return '>';
  285. else { next(LS); return TK_GE; }
  286. case '~':
  287. next(LS);
  288. if (LS->current != '=') return '~';
  289. else { next(LS); return TK_NE; }
  290. case '"':
  291. case '\'':
  292. read_string(LS, LS->current, seminfo);
  293. return TK_STRING;
  294. case '.':
  295. next(LS);
  296. if (LS->current == '.') {
  297. next(LS);
  298. if (LS->current == '.') {
  299. next(LS);
  300. return TK_DOTS; /* ... */
  301. }
  302. else return TK_CONCAT; /* .. */
  303. }
  304. else if (!isdigit(LS->current)) return '.';
  305. else {
  306. read_number(LS, 1, seminfo);
  307. return TK_NUMBER;
  308. }
  309. case EOZ:
  310. return TK_EOS;
  311. default: {
  312. if (isdigit(LS->current)) {
  313. read_number(LS, 0, seminfo);
  314. return TK_NUMBER;
  315. }
  316. else if (isalpha(LS->current) || LS->current == '_') {
  317. /* identifier or reserved word */
  318. size_t l = readname(LS);
  319. TString *ts = luaS_newlstr(LS->L, cast(char *, G(LS->L)->Mbuffer), l);
  320. if (ts->tsv.marked >= RESERVEDMARK) /* reserved word? */
  321. return ts->tsv.marked-RESERVEDMARK+FIRST_RESERVED;
  322. seminfo->ts = ts;
  323. return TK_NAME;
  324. }
  325. else {
  326. int c = LS->current;
  327. if (iscntrl(c))
  328. luaX_invalidchar(LS, c);
  329. next(LS);
  330. return c; /* single-char tokens (+ - / ...) */
  331. }
  332. }
  333. }
  334. }
  335. }