llex.c 10 KB

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