llex.c 11 KB

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