llex.c 11 KB

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