llex.c 11 KB

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