PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/engines/sword25/util/lua/llex.cpp

http://github.com/scummvm/scummvm
C++ | 474 lines | 395 code | 56 blank | 23 comment | 92 complexity | 26ea083b055f8173bbb4e39a63982066 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. /*
  2. ** $Id$
  3. ** Lexical Analyzer
  4. ** See Copyright Notice in lua.h
  5. */
  6. // FIXME: Do not directly use iscntrl from ctype.h.
  7. #define FORBIDDEN_SYMBOL_EXCEPTION_iscntrl
  8. #include "common/util.h"
  9. #define llex_c
  10. #define LUA_CORE
  11. #include "lua.h"
  12. #include "ldo.h"
  13. #include "llex.h"
  14. #include "lobject.h"
  15. #include "lparser.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "lzio.h"
  20. #define next(ls) (ls->current = zgetc(ls->z))
  21. #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
  22. /* ORDER RESERVED */
  23. const char *const luaX_tokens [] = {
  24. "and", "break", "do", "else", "elseif",
  25. "end", "false", "for", "function", "if",
  26. "in", "local", "nil", "not", "or", "repeat",
  27. "return", "then", "true", "until", "while",
  28. "..", "...", "==", ">=", "<=", "~=",
  29. "<number>", "<name>", "<string>", "<eof>",
  30. NULL
  31. };
  32. #define save_and_next(ls) (save(ls, ls->current), next(ls))
  33. static void save (LexState *ls, int c) {
  34. Mbuffer *b = ls->buff;
  35. if (b->n + 1 > b->buffsize) {
  36. size_t newsize;
  37. if (b->buffsize >= MAX_SIZET/2)
  38. luaX_lexerror(ls, "lexical element too long", 0);
  39. newsize = b->buffsize * 2;
  40. luaZ_resizebuffer(ls->L, b, newsize);
  41. }
  42. b->buffer[b->n++] = cast(char, c);
  43. }
  44. void luaX_init (lua_State *L) {
  45. int i;
  46. for (i=0; i<NUM_RESERVED; i++) {
  47. TString *ts = luaS_new(L, luaX_tokens[i]);
  48. luaS_fix(ts); /* reserved words are never collected */
  49. lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
  50. ts->tsv.reserved = cast_byte(i+1); /* reserved word */
  51. }
  52. }
  53. #define MAXSRC 80
  54. const char *luaX_token2str (LexState *ls, int token) {
  55. if (token < FIRST_RESERVED) {
  56. lua_assert(token == cast(unsigned char, token));
  57. return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
  58. luaO_pushfstring(ls->L, "%c", token);
  59. }
  60. else
  61. return luaX_tokens[token-FIRST_RESERVED];
  62. }
  63. static const char *txtToken (LexState *ls, int token) {
  64. switch (token) {
  65. case TK_NAME:
  66. case TK_STRING:
  67. case TK_NUMBER:
  68. save(ls, '\0');
  69. return luaZ_buffer(ls->buff);
  70. default:
  71. return luaX_token2str(ls, token);
  72. }
  73. }
  74. void luaX_lexerror (LexState *ls, const char *msg, int token) {
  75. char buff[MAXSRC];
  76. luaO_chunkid(buff, getstr(ls->source), MAXSRC);
  77. msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
  78. if (token)
  79. luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
  80. luaD_throw(ls->L, LUA_ERRSYNTAX);
  81. }
  82. void luaX_syntaxerror (LexState *ls, const char *msg) {
  83. luaX_lexerror(ls, msg, ls->t.token);
  84. }
  85. TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
  86. lua_State *L = ls->L;
  87. TString *ts = luaS_newlstr(L, str, l);
  88. TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
  89. if (ttisnil(o))
  90. setbvalue(o, 1); /* make sure `str' will not be collected */
  91. return ts;
  92. }
  93. static void inclinenumber (LexState *ls) {
  94. int old = ls->current;
  95. lua_assert(currIsNewline(ls));
  96. next(ls); /* skip `\n' or `\r' */
  97. if (currIsNewline(ls) && ls->current != old)
  98. next(ls); /* skip `\n\r' or `\r\n' */
  99. if (++ls->linenumber >= MAX_INT)
  100. luaX_syntaxerror(ls, "chunk has too many lines");
  101. }
  102. void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
  103. ls->decpoint = '.';
  104. ls->L = L;
  105. ls->lookahead.token = TK_EOS; /* no look-ahead token */
  106. ls->z = z;
  107. ls->fs = NULL;
  108. ls->linenumber = 1;
  109. ls->lastline = 1;
  110. ls->source = source;
  111. luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
  112. next(ls); /* read first char */
  113. }
  114. /*
  115. ** =======================================================
  116. ** LEXICAL ANALYZER
  117. ** =======================================================
  118. */
  119. static int check_next (LexState *ls, const char *set) {
  120. if (!strchr(set, ls->current))
  121. return 0;
  122. save_and_next(ls);
  123. return 1;
  124. }
  125. static void buffreplace (LexState *ls, char from, char to) {
  126. size_t n = luaZ_bufflen(ls->buff);
  127. char *p = luaZ_buffer(ls->buff);
  128. while (n--)
  129. if (p[n] == from) p[n] = to;
  130. }
  131. static void trydecpoint (LexState *ls, SemInfo *seminfo) {
  132. /* format error: try to update decimal point separator */
  133. // Normally we'd use localeconv() to get the decimal point separator, but
  134. // annoyingly that is not available on some platforms, e.g. Android. Figure
  135. // it out by formatting a known value and extract the separator from that
  136. // instead. The result could be cached, but considering the game I doubt
  137. // this will ever be a bottleneck. Note that the separator is assumed to fit
  138. // in a char, but that was a limitation in the original code as well.
  139. char old = ls->decpoint;
  140. char buf[5];
  141. int i;
  142. sprintf(buf, "%.1f", 1.0);
  143. ls->decpoint = '.';
  144. for (i = 0; buf[i]; i++) {
  145. if (!Common::isSpace(buf[i]) && !Common::isDigit(buf[i])) {
  146. ls->decpoint = buf[i];
  147. break;
  148. }
  149. }
  150. buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
  151. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
  152. /* format error with correct decimal point: no more options */
  153. buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
  154. luaX_lexerror(ls, "malformed number", TK_NUMBER);
  155. }
  156. }
  157. /* LUA_NUMBER */
  158. static void read_numeral (LexState *ls, SemInfo *seminfo) {
  159. lua_assert(Common::isDigit(ls->current));
  160. do {
  161. save_and_next(ls);
  162. } while (Common::isDigit(ls->current) || ls->current == '.');
  163. if (check_next(ls, "Ee")) /* `E'? */
  164. check_next(ls, "+-"); /* optional exponent sign */
  165. while (Common::isAlnum(ls->current) || ls->current == '_')
  166. save_and_next(ls);
  167. save(ls, '\0');
  168. buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
  169. if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
  170. trydecpoint(ls, seminfo); /* try to update decimal point separator */
  171. }
  172. static int skip_sep (LexState *ls) {
  173. int count = 0;
  174. int s = ls->current;
  175. lua_assert(s == '[' || s == ']');
  176. save_and_next(ls);
  177. while (ls->current == '=') {
  178. save_and_next(ls);
  179. count++;
  180. }
  181. return (ls->current == s) ? count : (-count) - 1;
  182. }
  183. static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
  184. int cont = 0;
  185. (void)(cont); /* avoid warnings when `cont' is not used */
  186. save_and_next(ls); /* skip 2nd `[' */
  187. if (currIsNewline(ls)) /* string starts with a newline? */
  188. inclinenumber(ls); /* skip it */
  189. for (;;) {
  190. switch (ls->current) {
  191. case EOZ:
  192. luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
  193. "unfinished long comment", TK_EOS);
  194. break; /* to avoid warnings */
  195. #if defined(LUA_COMPAT_LSTR)
  196. case '[': {
  197. if (skip_sep(ls) == sep) {
  198. save_and_next(ls); /* skip 2nd `[' */
  199. cont++;
  200. #if LUA_COMPAT_LSTR == 1
  201. if (sep == 0)
  202. luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
  203. #endif
  204. }
  205. break;
  206. }
  207. #endif
  208. case ']': {
  209. if (skip_sep(ls) == sep) {
  210. save_and_next(ls); /* skip 2nd `]' */
  211. #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
  212. cont--;
  213. if (sep == 0 && cont >= 0) break;
  214. #endif
  215. goto endloop;
  216. }
  217. break;
  218. }
  219. case '\n':
  220. case '\r': {
  221. save(ls, '\n');
  222. inclinenumber(ls);
  223. if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
  224. break;
  225. }
  226. default: {
  227. if (seminfo) save_and_next(ls);
  228. else next(ls);
  229. }
  230. }
  231. } endloop:
  232. if (seminfo)
  233. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
  234. luaZ_bufflen(ls->buff) - 2*(2 + sep));
  235. }
  236. static void read_string (LexState *ls, int del, SemInfo *seminfo) {
  237. save_and_next(ls);
  238. while (ls->current != del) {
  239. switch (ls->current) {
  240. case EOZ:
  241. luaX_lexerror(ls, "unfinished string", TK_EOS);
  242. continue; /* to avoid warnings */
  243. case '\n':
  244. case '\r':
  245. luaX_lexerror(ls, "unfinished string", TK_STRING);
  246. continue; /* to avoid warnings */
  247. case '\\': {
  248. int c;
  249. next(ls); /* do not save the `\' */
  250. switch (ls->current) {
  251. case 'a': c = '\a'; break;
  252. case 'b': c = '\b'; break;
  253. case 'f': c = '\f'; break;
  254. case 'n': c = '\n'; break;
  255. case 'r': c = '\r'; break;
  256. case 't': c = '\t'; break;
  257. case 'v': c = '\v'; break;
  258. case '\n': /* go through */
  259. case '\r': save(ls, '\n'); inclinenumber(ls); continue;
  260. case EOZ: continue; /* will raise an error next loop */
  261. default: {
  262. if (!Common::isDigit(ls->current))
  263. save_and_next(ls); /* handles \\, \", \', and \? */
  264. else { /* \xxx */
  265. int i = 0;
  266. c = 0;
  267. do {
  268. c = 10*c + (ls->current-'0');
  269. next(ls);
  270. } while (++i<3 && Common::isDigit(ls->current));
  271. if (c > UCHAR_MAX)
  272. luaX_lexerror(ls, "escape sequence too large", TK_STRING);
  273. save(ls, c);
  274. }
  275. continue;
  276. }
  277. }
  278. save(ls, c);
  279. next(ls);
  280. continue;
  281. }
  282. default:
  283. save_and_next(ls);
  284. }
  285. }
  286. save_and_next(ls); /* skip delimiter */
  287. seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
  288. luaZ_bufflen(ls->buff) - 2);
  289. }
  290. static int llex (LexState *ls, SemInfo *seminfo) {
  291. luaZ_resetbuffer(ls->buff);
  292. for (;;) {
  293. switch (ls->current) {
  294. case '\n':
  295. case '\r': {
  296. inclinenumber(ls);
  297. continue;
  298. }
  299. case '-': {
  300. next(ls);
  301. if (ls->current != '-') return '-';
  302. /* else is a comment */
  303. next(ls);
  304. if (ls->current == '[') {
  305. int sep = skip_sep(ls);
  306. luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
  307. if (sep >= 0) {
  308. read_long_string(ls, NULL, sep); /* long comment */
  309. luaZ_resetbuffer(ls->buff);
  310. continue;
  311. }
  312. }
  313. /* else short comment */
  314. while (!currIsNewline(ls) && ls->current != EOZ)
  315. next(ls);
  316. continue;
  317. }
  318. case '[': {
  319. int sep = skip_sep(ls);
  320. if (sep >= 0) {
  321. read_long_string(ls, seminfo, sep);
  322. return TK_STRING;
  323. }
  324. else if (sep == -1) return '[';
  325. else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
  326. }
  327. case '=': {
  328. next(ls);
  329. if (ls->current != '=') return '=';
  330. else { next(ls); return TK_EQ; }
  331. }
  332. case '<': {
  333. next(ls);
  334. if (ls->current != '=') return '<';
  335. else { next(ls); return TK_LE; }
  336. }
  337. case '>': {
  338. next(ls);
  339. if (ls->current != '=') return '>';
  340. else { next(ls); return TK_GE; }
  341. }
  342. case '~': {
  343. next(ls);
  344. if (ls->current != '=') return '~';
  345. else { next(ls); return TK_NE; }
  346. }
  347. case '"':
  348. case '\'': {
  349. read_string(ls, ls->current, seminfo);
  350. return TK_STRING;
  351. }
  352. case '.': {
  353. save_and_next(ls);
  354. if (check_next(ls, ".")) {
  355. if (check_next(ls, "."))
  356. return TK_DOTS; /* ... */
  357. else return TK_CONCAT; /* .. */
  358. }
  359. else if (!Common::isDigit(ls->current)) return '.';
  360. else {
  361. read_numeral(ls, seminfo);
  362. return TK_NUMBER;
  363. }
  364. }
  365. case EOZ: {
  366. return TK_EOS;
  367. }
  368. default: {
  369. if (Common::isSpace(ls->current)) {
  370. lua_assert(!currIsNewline(ls));
  371. next(ls);
  372. continue;
  373. }
  374. else if (Common::isDigit(ls->current)) {
  375. read_numeral(ls, seminfo);
  376. return TK_NUMBER;
  377. }
  378. else if (Common::isAlpha(ls->current) || ls->current == '_') {
  379. /* identifier or reserved word */
  380. TString *ts;
  381. do {
  382. save_and_next(ls);
  383. } while (Common::isAlnum(ls->current) || ls->current == '_');
  384. ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  385. luaZ_bufflen(ls->buff));
  386. if (ts->tsv.reserved > 0) /* reserved word? */
  387. return ts->tsv.reserved - 1 + FIRST_RESERVED;
  388. else {
  389. seminfo->ts = ts;
  390. return TK_NAME;
  391. }
  392. }
  393. else {
  394. int c = ls->current;
  395. next(ls);
  396. return c; /* single-char tokens (+ - / ...) */
  397. }
  398. }
  399. }
  400. }
  401. }
  402. void luaX_next (LexState *ls) {
  403. ls->lastline = ls->linenumber;
  404. if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
  405. ls->t = ls->lookahead; /* use this one */
  406. ls->lookahead.token = TK_EOS; /* and discharge it */
  407. }
  408. else
  409. ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
  410. }
  411. void luaX_lookahead (LexState *ls) {
  412. lua_assert(ls->lookahead.token == TK_EOS);
  413. ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
  414. }