llex.c 10 KB

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