llex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. ** $Id: llex.c,v 1.116 2002/10/23 19:08:13 roberto Exp roberto $
  3. ** Lexical Analyzer
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <string.h>
  8. #define llex_c
  9. #include "lua.h"
  10. #include "ldo.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 char *const token2string [] = {
  20. "and", "break", "do", "else", "elseif",
  21. "end", "false", "for", "function", "if",
  22. "in", "local", "nil", "not", "or", "repeat",
  23. "return", "then", "true", "until", "while", "*name",
  24. "..", "...", "==", ">=", "<=", "~=",
  25. "*number", "*string", "<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. luaS_fix(ts); /* reserved words are never collected */
  32. lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
  33. ts->tsv.reserved = cast(lu_byte, i+1); /* reserved word */
  34. }
  35. }
  36. #define MAXSRC 80
  37. void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
  38. if (val > limit) {
  39. msg = luaO_pushfstring(ls->L, "too many %s (limit=%d)", msg, limit);
  40. luaX_syntaxerror(ls, msg);
  41. }
  42. }
  43. static void luaX_error (LexState *ls, const char *s, const char *token) {
  44. lua_State *L = ls->L;
  45. char buff[MAXSRC];
  46. luaO_chunkid(buff, getstr(ls->source), MAXSRC);
  47. luaO_pushfstring(L, "%s:%d: %s near `%s'", buff, ls->linenumber, s, token);
  48. luaD_throw(L, LUA_ERRSYNTAX);
  49. }
  50. void luaX_syntaxerror (LexState *ls, const char *msg) {
  51. const char *lasttoken;
  52. switch (ls->t.token) {
  53. case TK_NAME:
  54. lasttoken = getstr(ls->t.seminfo.ts);
  55. break;
  56. case TK_STRING:
  57. case TK_NUMBER:
  58. lasttoken = luaZ_buffer(ls->buff);
  59. break;
  60. default:
  61. lasttoken = luaX_token2str(ls, ls->t.token);
  62. break;
  63. }
  64. luaX_error(ls, msg, lasttoken);
  65. }
  66. const char *luaX_token2str (LexState *ls, int token) {
  67. if (token < FIRST_RESERVED) {
  68. lua_assert(token == (char)token);
  69. return luaO_pushfstring(ls->L, "%c", token);
  70. }
  71. else
  72. return token2string[token-FIRST_RESERVED];
  73. }
  74. static void luaX_lexerror (LexState *ls, const char *s, int token) {
  75. if (token == TK_EOS)
  76. luaX_error(ls, s, luaX_token2str(ls, token));
  77. else
  78. luaX_error(ls, s, luaZ_buffer(ls->buff));
  79. }
  80. static void inclinenumber (LexState *LS) {
  81. next(LS); /* skip `\n' */
  82. ++LS->linenumber;
  83. luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
  84. }
  85. void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
  86. LS->L = L;
  87. LS->lookahead.token = TK_EOS; /* no look-ahead token */
  88. LS->z = z;
  89. LS->fs = NULL;
  90. LS->linenumber = 1;
  91. LS->lastline = 1;
  92. LS->source = source;
  93. next(LS); /* read first char */
  94. if (LS->current == '#') {
  95. do { /* skip first line */
  96. next(LS);
  97. } while (LS->current != '\n' && LS->current != EOZ);
  98. }
  99. }
  100. /*
  101. ** =======================================================
  102. ** LEXICAL ANALYZER
  103. ** =======================================================
  104. */
  105. /* use buffer to store names, literal strings and numbers */
  106. /* extra space to allocate when growing buffer */
  107. #define EXTRABUFF 32
  108. /* maximum number of chars that can be read without checking buffer size */
  109. #define MAXNOCHECK 5
  110. #define checkbuffer(LS, len) \
  111. if (((len)+MAXNOCHECK)*sizeof(char) > luaZ_sizebuffer((LS)->buff)) \
  112. luaZ_openspace((LS)->L, (LS)->buff, (len)+EXTRABUFF)
  113. #define save(LS, c, l) \
  114. (luaZ_buffer((LS)->buff)[l++] = cast(char, c))
  115. #define save_and_next(LS, l) (save(LS, LS->current, l), next(LS))
  116. static size_t readname (LexState *LS) {
  117. size_t l = 0;
  118. checkbuffer(LS, l);
  119. do {
  120. checkbuffer(LS, l);
  121. save_and_next(LS, l);
  122. } while (isalnum(LS->current) || LS->current == '_');
  123. save(LS, '\0', l);
  124. return l-1;
  125. }
  126. /* LUA_NUMBER */
  127. static void read_numeral (LexState *LS, int comma, SemInfo *seminfo) {
  128. size_t l = 0;
  129. checkbuffer(LS, l);
  130. if (comma) save(LS, '.', l);
  131. while (isdigit(LS->current)) {
  132. checkbuffer(LS, l);
  133. save_and_next(LS, l);
  134. }
  135. if (LS->current == '.') {
  136. save_and_next(LS, l);
  137. if (LS->current == '.') {
  138. save_and_next(LS, l);
  139. save(LS, '\0', l);
  140. luaX_lexerror(LS,
  141. "ambiguous syntax (decimal point x string concatenation)",
  142. TK_NUMBER);
  143. }
  144. }
  145. while (isdigit(LS->current)) {
  146. checkbuffer(LS, l);
  147. save_and_next(LS, l);
  148. }
  149. if (LS->current == 'e' || LS->current == 'E') {
  150. save_and_next(LS, l); /* read `E' */
  151. if (LS->current == '+' || LS->current == '-')
  152. save_and_next(LS, l); /* optional exponent sign */
  153. while (isdigit(LS->current)) {
  154. checkbuffer(LS, l);
  155. save_and_next(LS, l);
  156. }
  157. }
  158. save(LS, '\0', l);
  159. if (!luaO_str2d(luaZ_buffer(LS->buff), &seminfo->r))
  160. luaX_lexerror(LS, "malformed number", TK_NUMBER);
  161. }
  162. static void read_long_string (LexState *LS, SemInfo *seminfo) {
  163. int cont = 0;
  164. size_t l = 0;
  165. checkbuffer(LS, l);
  166. save(LS, '[', l); /* save first `[' */
  167. save_and_next(LS, l); /* pass the second `[' */
  168. if (LS->current == '\n') /* string starts with a newline? */
  169. inclinenumber(LS); /* skip it */
  170. for (;;) {
  171. checkbuffer(LS, l);
  172. switch (LS->current) {
  173. case EOZ:
  174. save(LS, '\0', l);
  175. luaX_lexerror(LS, (seminfo) ? "unfinished long string" :
  176. "unfinished long comment", TK_EOS);
  177. break; /* to avoid warnings */
  178. case '[':
  179. save_and_next(LS, l);
  180. if (LS->current == '[') {
  181. cont++;
  182. save_and_next(LS, l);
  183. }
  184. continue;
  185. case ']':
  186. save_and_next(LS, l);
  187. if (LS->current == ']') {
  188. if (cont == 0) goto endloop;
  189. cont--;
  190. save_and_next(LS, l);
  191. }
  192. continue;
  193. case '\n':
  194. save(LS, '\n', l);
  195. inclinenumber(LS);
  196. if (!seminfo) l = 0; /* reset buffer to avoid wasting space */
  197. continue;
  198. default:
  199. save_and_next(LS, l);
  200. }
  201. } endloop:
  202. save_and_next(LS, l); /* skip the second `]' */
  203. save(LS, '\0', l);
  204. if (seminfo)
  205. seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 2, l - 5);
  206. }
  207. static void read_string (LexState *LS, int del, SemInfo *seminfo) {
  208. size_t l = 0;
  209. checkbuffer(LS, l);
  210. save_and_next(LS, l);
  211. while (LS->current != del) {
  212. checkbuffer(LS, l);
  213. switch (LS->current) {
  214. case EOZ:
  215. save(LS, '\0', l);
  216. luaX_lexerror(LS, "unfinished string", TK_EOS);
  217. break; /* to avoid warnings */
  218. case '\n':
  219. save(LS, '\0', l);
  220. luaX_lexerror(LS, "unfinished string", TK_STRING);
  221. break; /* to avoid warnings */
  222. case '\\':
  223. next(LS); /* do not save the `\' */
  224. switch (LS->current) {
  225. case 'a': save(LS, '\a', l); next(LS); break;
  226. case 'b': save(LS, '\b', l); next(LS); break;
  227. case 'f': save(LS, '\f', l); next(LS); break;
  228. case 'n': save(LS, '\n', l); next(LS); break;
  229. case 'r': save(LS, '\r', l); next(LS); break;
  230. case 't': save(LS, '\t', l); next(LS); break;
  231. case 'v': save(LS, '\v', l); next(LS); break;
  232. case '\n': save(LS, '\n', l); inclinenumber(LS); break;
  233. case EOZ: break; /* will raise an error next loop */
  234. default: {
  235. if (!isdigit(LS->current))
  236. save_and_next(LS, l); /* handles \\, \", \', and \? */
  237. else { /* \xxx */
  238. int c = 0;
  239. int i = 0;
  240. do {
  241. c = 10*c + (LS->current-'0');
  242. next(LS);
  243. } while (++i<3 && isdigit(LS->current));
  244. if (c > UCHAR_MAX) {
  245. save(LS, '\0', l);
  246. luaX_lexerror(LS, "escape sequence too large", TK_STRING);
  247. }
  248. save(LS, c, l);
  249. }
  250. }
  251. }
  252. break;
  253. default:
  254. save_and_next(LS, l);
  255. }
  256. }
  257. save_and_next(LS, l); /* skip delimiter */
  258. save(LS, '\0', l);
  259. seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 1, l - 3);
  260. }
  261. int luaX_lex (LexState *LS, SemInfo *seminfo) {
  262. for (;;) {
  263. switch (LS->current) {
  264. case '\n': {
  265. inclinenumber(LS);
  266. continue;
  267. }
  268. case '-': {
  269. next(LS);
  270. if (LS->current != '-') return '-';
  271. /* else is a comment */
  272. next(LS);
  273. if (LS->current == '[' && (next(LS), LS->current == '['))
  274. read_long_string(LS, NULL); /* long comment */
  275. else /* short comment */
  276. while (LS->current != '\n' && LS->current != EOZ)
  277. next(LS);
  278. continue;
  279. }
  280. case '[': {
  281. next(LS);
  282. if (LS->current != '[') return '[';
  283. else {
  284. read_long_string(LS, seminfo);
  285. return TK_STRING;
  286. }
  287. }
  288. case '=': {
  289. next(LS);
  290. if (LS->current != '=') return '=';
  291. else { next(LS); return TK_EQ; }
  292. }
  293. case '<': {
  294. next(LS);
  295. if (LS->current != '=') return '<';
  296. else { next(LS); return TK_LE; }
  297. }
  298. case '>': {
  299. next(LS);
  300. if (LS->current != '=') return '>';
  301. else { next(LS); return TK_GE; }
  302. }
  303. case '~': {
  304. next(LS);
  305. if (LS->current != '=') return '~';
  306. else { next(LS); return TK_NE; }
  307. }
  308. case '"':
  309. case '\'': {
  310. read_string(LS, LS->current, seminfo);
  311. return TK_STRING;
  312. }
  313. case '.': {
  314. next(LS);
  315. if (LS->current == '.') {
  316. next(LS);
  317. if (LS->current == '.') {
  318. next(LS);
  319. return TK_DOTS; /* ... */
  320. }
  321. else return TK_CONCAT; /* .. */
  322. }
  323. else if (!isdigit(LS->current)) return '.';
  324. else {
  325. read_numeral(LS, 1, seminfo);
  326. return TK_NUMBER;
  327. }
  328. }
  329. case EOZ: {
  330. return TK_EOS;
  331. }
  332. default: {
  333. if (isspace(LS->current)) {
  334. next(LS);
  335. continue;
  336. }
  337. else if (isdigit(LS->current)) {
  338. read_numeral(LS, 0, seminfo);
  339. return TK_NUMBER;
  340. }
  341. else if (isalpha(LS->current) || LS->current == '_') {
  342. /* identifier or reserved word */
  343. size_t l = readname(LS);
  344. TString *ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff), l);
  345. if (ts->tsv.reserved > 0) /* reserved word? */
  346. return ts->tsv.reserved - 1 + FIRST_RESERVED;
  347. seminfo->ts = ts;
  348. return TK_NAME;
  349. }
  350. else {
  351. int c = LS->current;
  352. if (iscntrl(c))
  353. luaX_error(LS, "invalid control char",
  354. luaO_pushfstring(LS->L, "char(%d)", c));
  355. next(LS);
  356. return c; /* single-char tokens (+ - / ...) */
  357. }
  358. }
  359. }
  360. }
  361. }
  362. #undef next