llex.c 11 KB

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