llex.c 11 KB

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