llex.c 11 KB

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