llex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. ** $Id: llex.c,v 2.11 2005/05/16 21:19:00 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 " LUA_QS, 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. (void)(cont); /* avoid warnings when `cont' is not used */
  159. save_and_next(ls); /* skip 2nd `[' */
  160. if (currIsNewline(ls)) /* string starts with a newline? */
  161. inclinenumber(ls); /* skip it */
  162. for (;;) {
  163. switch (ls->current) {
  164. case EOZ:
  165. luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
  166. "unfinished long comment", TK_EOS);
  167. break; /* to avoid warnings */
  168. #if defined(LUA_COMPAT_LSTR)
  169. case '[': {
  170. if (skip_sep(ls) == sep) {
  171. save_and_next(ls); /* skip 2nd `[' */
  172. cont++;
  173. #if LUA_COMPAT_LSTR == 1
  174. if (sep == 0)
  175. luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
  176. #endif
  177. }
  178. break;
  179. }
  180. #endif
  181. case ']': {
  182. if (skip_sep(ls) == sep) {
  183. save_and_next(ls); /* skip 2nd `]' */
  184. #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
  185. cont--;
  186. if (sep == 0 && cont >= 0) break;
  187. #endif
  188. goto endloop;
  189. }
  190. break;
  191. }
  192. case '\n':
  193. case '\r': {
  194. save(ls, '\n');
  195. inclinenumber(ls);
  196. if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
  197. break;
  198. }
  199. default: {
  200. if (seminfo) save_and_next(ls);
  201. else next(ls);
  202. }
  203. }
  204. } endloop:
  205. if (seminfo)
  206. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
  207. luaZ_bufflen(ls->buff) - 2*(2 + sep));
  208. }
  209. static void read_string (LexState *ls, int del, SemInfo *seminfo) {
  210. save_and_next(ls);
  211. while (ls->current != del) {
  212. switch (ls->current) {
  213. case EOZ:
  214. luaX_lexerror(ls, "unfinished string", TK_EOS);
  215. continue; /* to avoid warnings */
  216. case '\n':
  217. case '\r':
  218. luaX_lexerror(ls, "unfinished string", TK_STRING);
  219. continue; /* to avoid warnings */
  220. case '\\': {
  221. int c;
  222. next(ls); /* do not save the `\' */
  223. switch (ls->current) {
  224. case 'a': c = '\a'; break;
  225. case 'b': c = '\b'; break;
  226. case 'f': c = '\f'; break;
  227. case 'n': c = '\n'; break;
  228. case 'r': c = '\r'; break;
  229. case 't': c = '\t'; break;
  230. case 'v': c = '\v'; break;
  231. case '\n': /* go through */
  232. case '\r': save(ls, '\n'); inclinenumber(ls); continue;
  233. case EOZ: continue; /* will raise an error next loop */
  234. default: {
  235. if (!isdigit(ls->current))
  236. save_and_next(ls); /* handles \\, \", \', and \? */
  237. else { /* \xxx */
  238. int i = 0;
  239. c = 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. luaX_lexerror(ls, "escape sequence too large", TK_STRING);
  246. save(ls, c);
  247. }
  248. continue;
  249. }
  250. }
  251. save(ls, c);
  252. next(ls);
  253. continue;
  254. }
  255. default:
  256. save_and_next(ls);
  257. }
  258. }
  259. save_and_next(ls); /* skip delimiter */
  260. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
  261. luaZ_bufflen(ls->buff) - 2);
  262. }
  263. int luaX_lex (LexState *ls, SemInfo *seminfo) {
  264. luaZ_resetbuffer(ls->buff);
  265. for (;;) {
  266. switch (ls->current) {
  267. case '\n':
  268. case '\r': {
  269. inclinenumber(ls);
  270. continue;
  271. }
  272. case '-': {
  273. next(ls);
  274. if (ls->current != '-') return '-';
  275. /* else is a comment */
  276. next(ls);
  277. if (ls->current == '[') {
  278. int sep = skip_sep(ls);
  279. luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
  280. if (sep >= 0) {
  281. read_long_string(ls, NULL, sep); /* long comment */
  282. luaZ_resetbuffer(ls->buff);
  283. continue;
  284. }
  285. }
  286. /* else short comment */
  287. while (!currIsNewline(ls) && ls->current != EOZ)
  288. next(ls);
  289. continue;
  290. }
  291. case '[': {
  292. int sep = skip_sep(ls);
  293. if (sep >= 0) {
  294. read_long_string(ls, seminfo, sep);
  295. return TK_STRING;
  296. }
  297. else if (sep == -1) return '[';
  298. else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
  299. }
  300. case '=': {
  301. next(ls);
  302. if (ls->current != '=') return '=';
  303. else { next(ls); return TK_EQ; }
  304. }
  305. case '<': {
  306. next(ls);
  307. if (ls->current != '=') return '<';
  308. else { next(ls); return TK_LE; }
  309. }
  310. case '>': {
  311. next(ls);
  312. if (ls->current != '=') return '>';
  313. else { next(ls); return TK_GE; }
  314. }
  315. case '~': {
  316. next(ls);
  317. if (ls->current != '=') return '~';
  318. else { next(ls); return TK_NE; }
  319. }
  320. case '"':
  321. case '\'': {
  322. read_string(ls, ls->current, seminfo);
  323. return TK_STRING;
  324. }
  325. case '.': {
  326. save_and_next(ls);
  327. if (ls->current == '.') {
  328. next(ls);
  329. if (ls->current == '.') {
  330. next(ls);
  331. return TK_DOTS; /* ... */
  332. }
  333. else return TK_CONCAT; /* .. */
  334. }
  335. else if (!isdigit(ls->current)) return '.';
  336. else {
  337. read_numeral(ls, seminfo);
  338. return TK_NUMBER;
  339. }
  340. }
  341. case EOZ: {
  342. return TK_EOS;
  343. }
  344. default: {
  345. if (isspace(ls->current)) {
  346. lua_assert(!currIsNewline(ls));
  347. next(ls);
  348. continue;
  349. }
  350. else if (isdigit(ls->current)) {
  351. read_numeral(ls, seminfo);
  352. return TK_NUMBER;
  353. }
  354. else if (isalpha(ls->current) || ls->current == '_') {
  355. /* identifier or reserved word */
  356. TString *ts;
  357. do {
  358. save_and_next(ls);
  359. } while (isalnum(ls->current) || ls->current == '_');
  360. ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  361. luaZ_bufflen(ls->buff));
  362. if (ts->tsv.reserved > 0) /* reserved word? */
  363. return ts->tsv.reserved - 1 + FIRST_RESERVED;
  364. else {
  365. seminfo->ts = ts;
  366. return TK_NAME;
  367. }
  368. }
  369. else {
  370. int c = ls->current;
  371. next(ls);
  372. return c; /* single-char tokens (+ - / ...) */
  373. }
  374. }
  375. }
  376. }
  377. }
  378. #undef next