llex.c 10 KB

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