lj_lex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. ** Lexical analyzer.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lj_lex_c
  9. #define LUA_CORE
  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_str.h"
  14. #if LJ_HASFFI
  15. #include "lj_tab.h"
  16. #include "lj_ctype.h"
  17. #include "lj_cdata.h"
  18. #include "lualib.h"
  19. #endif
  20. #include "lj_state.h"
  21. #include "lj_lex.h"
  22. #include "lj_parse.h"
  23. #include "lj_char.h"
  24. #include "lj_strscan.h"
  25. /* Lua lexer token names. */
  26. static const char *const tokennames[] = {
  27. #define TKSTR1(name) #name,
  28. #define TKSTR2(name, sym) #sym,
  29. TKDEF(TKSTR1, TKSTR2)
  30. #undef TKSTR1
  31. #undef TKSTR2
  32. NULL
  33. };
  34. /* -- Buffer handling ----------------------------------------------------- */
  35. #define char2int(c) ((int)(uint8_t)(c))
  36. #define next(ls) \
  37. (ls->current = (ls->n--) > 0 ? char2int(*ls->p++) : fillbuf(ls))
  38. #define save_and_next(ls) (save(ls, ls->current), next(ls))
  39. #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
  40. #define END_OF_STREAM (-1)
  41. static int fillbuf(LexState *ls)
  42. {
  43. size_t sz;
  44. const char *buf = ls->rfunc(ls->L, ls->rdata, &sz);
  45. if (buf == NULL || sz == 0) return END_OF_STREAM;
  46. ls->n = (MSize)sz - 1;
  47. ls->p = buf;
  48. return char2int(*(ls->p++));
  49. }
  50. static LJ_NOINLINE void save_grow(LexState *ls, int c)
  51. {
  52. MSize newsize;
  53. if (ls->sb.sz >= LJ_MAX_STR/2)
  54. lj_lex_error(ls, 0, LJ_ERR_XELEM);
  55. newsize = ls->sb.sz * 2;
  56. lj_str_resizebuf(ls->L, &ls->sb, newsize);
  57. ls->sb.buf[ls->sb.n++] = (char)c;
  58. }
  59. static LJ_AINLINE void save(LexState *ls, int c)
  60. {
  61. if (LJ_UNLIKELY(ls->sb.n + 1 > ls->sb.sz))
  62. save_grow(ls, c);
  63. else
  64. ls->sb.buf[ls->sb.n++] = (char)c;
  65. }
  66. static void inclinenumber(LexState *ls)
  67. {
  68. int old = ls->current;
  69. lua_assert(currIsNewline(ls));
  70. next(ls); /* skip `\n' or `\r' */
  71. if (currIsNewline(ls) && ls->current != old)
  72. next(ls); /* skip `\n\r' or `\r\n' */
  73. if (++ls->linenumber >= LJ_MAX_LINE)
  74. lj_lex_error(ls, ls->token, LJ_ERR_XLINES);
  75. }
  76. /* -- Scanner for terminals ----------------------------------------------- */
  77. /* Parse a number literal. */
  78. static void lex_number(LexState *ls, TValue *tv)
  79. {
  80. StrScanFmt fmt;
  81. int c, xp = 'e';
  82. lua_assert(lj_char_isdigit(ls->current));
  83. if ((c = ls->current) == '0') {
  84. save_and_next(ls);
  85. if ((ls->current | 0x20) == 'x') xp = 'p';
  86. }
  87. while (lj_char_isident(ls->current) || ls->current == '.' ||
  88. ((ls->current == '-' || ls->current == '+') && (c | 0x20) == xp)) {
  89. c = ls->current;
  90. save_and_next(ls);
  91. }
  92. save(ls, '\0');
  93. fmt = lj_strscan_scan((const uint8_t *)ls->sb.buf, tv,
  94. (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) |
  95. (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0));
  96. if (LJ_DUALNUM && fmt == STRSCAN_INT) {
  97. setitype(tv, LJ_TISNUM);
  98. } else if (fmt == STRSCAN_NUM) {
  99. /* Already in correct format. */
  100. #if LJ_HASFFI
  101. } else if (fmt != STRSCAN_ERROR) {
  102. lua_State *L = ls->L;
  103. GCcdata *cd;
  104. lua_assert(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG);
  105. if (!ctype_ctsG(G(L))) {
  106. ptrdiff_t oldtop = savestack(L, L->top);
  107. luaopen_ffi(L); /* Load FFI library on-demand. */
  108. L->top = restorestack(L, oldtop);
  109. }
  110. if (fmt == STRSCAN_IMAG) {
  111. cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double));
  112. ((double *)cdataptr(cd))[0] = 0;
  113. ((double *)cdataptr(cd))[1] = numV(tv);
  114. } else {
  115. cd = lj_cdata_new_(L, fmt==STRSCAN_I64 ? CTID_INT64 : CTID_UINT64, 8);
  116. *(uint64_t *)cdataptr(cd) = tv->u64;
  117. }
  118. lj_parse_keepcdata(ls, tv, cd);
  119. #endif
  120. } else {
  121. lua_assert(fmt == STRSCAN_ERROR);
  122. lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER);
  123. }
  124. }
  125. static int skip_sep(LexState *ls)
  126. {
  127. int count = 0;
  128. int s = ls->current;
  129. lua_assert(s == '[' || s == ']');
  130. save_and_next(ls);
  131. while (ls->current == '=') {
  132. save_and_next(ls);
  133. count++;
  134. }
  135. return (ls->current == s) ? count : (-count) - 1;
  136. }
  137. static void read_long_string(LexState *ls, TValue *tv, int sep)
  138. {
  139. save_and_next(ls); /* skip 2nd `[' */
  140. if (currIsNewline(ls)) /* string starts with a newline? */
  141. inclinenumber(ls); /* skip it */
  142. for (;;) {
  143. switch (ls->current) {
  144. case END_OF_STREAM:
  145. lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM);
  146. break;
  147. case ']':
  148. if (skip_sep(ls) == sep) {
  149. save_and_next(ls); /* skip 2nd `]' */
  150. goto endloop;
  151. }
  152. break;
  153. case '\n':
  154. case '\r':
  155. save(ls, '\n');
  156. inclinenumber(ls);
  157. if (!tv) lj_str_resetbuf(&ls->sb); /* avoid wasting space */
  158. break;
  159. default:
  160. if (tv) save_and_next(ls);
  161. else next(ls);
  162. break;
  163. }
  164. } endloop:
  165. if (tv) {
  166. GCstr *str = lj_parse_keepstr(ls, ls->sb.buf + (2 + (MSize)sep),
  167. ls->sb.n - 2*(2 + (MSize)sep));
  168. setstrV(ls->L, tv, str);
  169. }
  170. }
  171. static void read_string(LexState *ls, int delim, TValue *tv)
  172. {
  173. save_and_next(ls);
  174. while (ls->current != delim) {
  175. switch (ls->current) {
  176. case END_OF_STREAM:
  177. lj_lex_error(ls, TK_eof, LJ_ERR_XSTR);
  178. continue;
  179. case '\n':
  180. case '\r':
  181. lj_lex_error(ls, TK_string, LJ_ERR_XSTR);
  182. continue;
  183. case '\\': {
  184. int c = next(ls); /* Skip the '\\'. */
  185. switch (c) {
  186. case 'a': c = '\a'; break;
  187. case 'b': c = '\b'; break;
  188. case 'f': c = '\f'; break;
  189. case 'n': c = '\n'; break;
  190. case 'r': c = '\r'; break;
  191. case 't': c = '\t'; break;
  192. case 'v': c = '\v'; break;
  193. case 'x': /* Hexadecimal escape '\xXX'. */
  194. c = (next(ls) & 15u) << 4;
  195. if (!lj_char_isdigit(ls->current)) {
  196. if (!lj_char_isxdigit(ls->current)) goto err_xesc;
  197. c += 9 << 4;
  198. }
  199. c += (next(ls) & 15u);
  200. if (!lj_char_isdigit(ls->current)) {
  201. if (!lj_char_isxdigit(ls->current)) goto err_xesc;
  202. c += 9;
  203. }
  204. break;
  205. case 'z': /* Skip whitespace. */
  206. next(ls);
  207. while (lj_char_isspace(ls->current))
  208. if (currIsNewline(ls)) inclinenumber(ls); else next(ls);
  209. continue;
  210. case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue;
  211. case '\\': case '\"': case '\'': break;
  212. case END_OF_STREAM: continue;
  213. default:
  214. if (!lj_char_isdigit(c))
  215. goto err_xesc;
  216. c -= '0'; /* Decimal escape '\ddd'. */
  217. if (lj_char_isdigit(next(ls))) {
  218. c = c*10 + (ls->current - '0');
  219. if (lj_char_isdigit(next(ls))) {
  220. c = c*10 + (ls->current - '0');
  221. if (c > 255) {
  222. err_xesc:
  223. lj_lex_error(ls, TK_string, LJ_ERR_XESC);
  224. }
  225. next(ls);
  226. }
  227. }
  228. save(ls, c);
  229. continue;
  230. }
  231. save(ls, c);
  232. next(ls);
  233. continue;
  234. }
  235. default:
  236. save_and_next(ls);
  237. break;
  238. }
  239. }
  240. save_and_next(ls); /* skip delimiter */
  241. setstrV(ls->L, tv, lj_parse_keepstr(ls, ls->sb.buf + 1, ls->sb.n - 2));
  242. }
  243. /* -- Main lexical scanner ------------------------------------------------ */
  244. static int llex(LexState *ls, TValue *tv)
  245. {
  246. lj_str_resetbuf(&ls->sb);
  247. for (;;) {
  248. if (lj_char_isident(ls->current)) {
  249. GCstr *s;
  250. if (lj_char_isdigit(ls->current)) { /* Numeric literal. */
  251. lex_number(ls, tv);
  252. return TK_number;
  253. }
  254. /* Identifier or reserved word. */
  255. do {
  256. save_and_next(ls);
  257. } while (lj_char_isident(ls->current));
  258. s = lj_parse_keepstr(ls, ls->sb.buf, ls->sb.n);
  259. setstrV(ls->L, tv, s);
  260. if (s->reserved > 0) /* Reserved word? */
  261. return TK_OFS + s->reserved;
  262. return TK_name;
  263. }
  264. switch (ls->current) {
  265. case '\n':
  266. case '\r':
  267. inclinenumber(ls);
  268. continue;
  269. case ' ':
  270. case '\t':
  271. case '\v':
  272. case '\f':
  273. next(ls);
  274. continue;
  275. case '-':
  276. next(ls);
  277. if (ls->current != '-') return '-';
  278. /* else is a comment */
  279. next(ls);
  280. if (ls->current == '[') {
  281. int sep = skip_sep(ls);
  282. lj_str_resetbuf(&ls->sb); /* `skip_sep' may dirty the buffer */
  283. if (sep >= 0) {
  284. read_long_string(ls, NULL, sep); /* long comment */
  285. lj_str_resetbuf(&ls->sb);
  286. continue;
  287. }
  288. }
  289. /* else short comment */
  290. while (!currIsNewline(ls) && ls->current != END_OF_STREAM)
  291. next(ls);
  292. continue;
  293. case '[': {
  294. int sep = skip_sep(ls);
  295. if (sep >= 0) {
  296. read_long_string(ls, tv, sep);
  297. return TK_string;
  298. } else if (sep == -1) {
  299. return '[';
  300. } else {
  301. lj_lex_error(ls, TK_string, LJ_ERR_XLDELIM);
  302. continue;
  303. }
  304. }
  305. case '=':
  306. next(ls);
  307. if (ls->current != '=') return '='; else { next(ls); return TK_eq; }
  308. case '<':
  309. next(ls);
  310. if (ls->current != '=') return '<'; else { next(ls); return TK_le; }
  311. case '>':
  312. next(ls);
  313. if (ls->current != '=') return '>'; else { next(ls); return TK_ge; }
  314. case '~':
  315. next(ls);
  316. if (ls->current != '=') return '~'; else { next(ls); return TK_ne; }
  317. case ':':
  318. next(ls);
  319. if (ls->current != ':') return ':'; else { next(ls); return TK_label; }
  320. case '"':
  321. case '\'':
  322. read_string(ls, ls->current, tv);
  323. return TK_string;
  324. case '.':
  325. save_and_next(ls);
  326. if (ls->current == '.') {
  327. next(ls);
  328. if (ls->current == '.') {
  329. next(ls);
  330. return TK_dots; /* ... */
  331. }
  332. return TK_concat; /* .. */
  333. } else if (!lj_char_isdigit(ls->current)) {
  334. return '.';
  335. } else {
  336. lex_number(ls, tv);
  337. return TK_number;
  338. }
  339. case END_OF_STREAM:
  340. return TK_eof;
  341. default: {
  342. int c = ls->current;
  343. next(ls);
  344. return c; /* Single-char tokens (+ - / ...). */
  345. }
  346. }
  347. }
  348. }
  349. /* -- Lexer API ----------------------------------------------------------- */
  350. /* Setup lexer state. */
  351. int lj_lex_setup(lua_State *L, LexState *ls)
  352. {
  353. int header = 0;
  354. ls->L = L;
  355. ls->fs = NULL;
  356. ls->n = 0;
  357. ls->p = NULL;
  358. ls->vstack = NULL;
  359. ls->sizevstack = 0;
  360. ls->vtop = 0;
  361. ls->bcstack = NULL;
  362. ls->sizebcstack = 0;
  363. ls->lookahead = TK_eof; /* No look-ahead token. */
  364. ls->linenumber = 1;
  365. ls->lastline = 1;
  366. lj_str_resizebuf(ls->L, &ls->sb, LJ_MIN_SBUF);
  367. next(ls); /* Read-ahead first char. */
  368. if (ls->current == 0xef && ls->n >= 2 && char2int(ls->p[0]) == 0xbb &&
  369. char2int(ls->p[1]) == 0xbf) { /* Skip UTF-8 BOM (if buffered). */
  370. ls->n -= 2;
  371. ls->p += 2;
  372. next(ls);
  373. header = 1;
  374. }
  375. if (ls->current == '#') { /* Skip POSIX #! header line. */
  376. do {
  377. next(ls);
  378. if (ls->current == END_OF_STREAM) return 0;
  379. } while (!currIsNewline(ls));
  380. inclinenumber(ls);
  381. header = 1;
  382. }
  383. if (ls->current == LUA_SIGNATURE[0]) { /* Bytecode dump. */
  384. if (header) {
  385. /*
  386. ** Loading bytecode with an extra header is disabled for security
  387. ** reasons. This may circumvent the usual check for bytecode vs.
  388. ** Lua code by looking at the first char. Since this is a potential
  389. ** security violation no attempt is made to echo the chunkname either.
  390. */
  391. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCBAD));
  392. lj_err_throw(L, LUA_ERRSYNTAX);
  393. }
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. /* Cleanup lexer state. */
  399. void lj_lex_cleanup(lua_State *L, LexState *ls)
  400. {
  401. global_State *g = G(L);
  402. lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine);
  403. lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo);
  404. lj_str_freebuf(g, &ls->sb);
  405. }
  406. void lj_lex_next(LexState *ls)
  407. {
  408. ls->lastline = ls->linenumber;
  409. if (LJ_LIKELY(ls->lookahead == TK_eof)) { /* No lookahead token? */
  410. ls->token = llex(ls, &ls->tokenval); /* Get next token. */
  411. } else { /* Otherwise return lookahead token. */
  412. ls->token = ls->lookahead;
  413. ls->lookahead = TK_eof;
  414. ls->tokenval = ls->lookaheadval;
  415. }
  416. }
  417. LexToken lj_lex_lookahead(LexState *ls)
  418. {
  419. lua_assert(ls->lookahead == TK_eof);
  420. ls->lookahead = llex(ls, &ls->lookaheadval);
  421. return ls->lookahead;
  422. }
  423. const char *lj_lex_token2str(LexState *ls, LexToken token)
  424. {
  425. if (token > TK_OFS)
  426. return tokennames[token-TK_OFS-1];
  427. else if (!lj_char_iscntrl(token))
  428. return lj_str_pushf(ls->L, "%c", token);
  429. else
  430. return lj_str_pushf(ls->L, "char(%d)", token);
  431. }
  432. void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...)
  433. {
  434. const char *tok;
  435. va_list argp;
  436. if (token == 0) {
  437. tok = NULL;
  438. } else if (token == TK_name || token == TK_string || token == TK_number) {
  439. save(ls, '\0');
  440. tok = ls->sb.buf;
  441. } else {
  442. tok = lj_lex_token2str(ls, token);
  443. }
  444. va_start(argp, em);
  445. lj_err_lex(ls->L, ls->chunkname, tok, ls->linenumber, em, argp);
  446. va_end(argp);
  447. }
  448. void lj_lex_init(lua_State *L)
  449. {
  450. uint32_t i;
  451. for (i = 0; i < TK_RESERVED; i++) {
  452. GCstr *s = lj_str_newz(L, tokennames[i]);
  453. fixstring(s); /* Reserved words are never collected. */
  454. s->reserved = (uint8_t)(i+1);
  455. }
  456. }