llex.c 10 KB

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