Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #include "parse/lex.h"
  2. #include <stdlib.h>
  3. static void log_token(struct l2_token *tok) {
  4. switch (l2_token_get_kind(tok)) {
  5. case L2_TOK_STRING:
  6. case L2_TOK_IDENT:
  7. case L2_TOK_ERROR:
  8. if (l2_token_is_small(tok)) {
  9. printf("%i:%i\t%s '%s'\n", tok->line, tok->ch,
  10. l2_token_get_name(tok), tok->v.strbuf);
  11. } else {
  12. printf("%i:%i\t%s '%s'\n", tok->line, tok->ch,
  13. l2_token_get_name(tok), tok->v.str);
  14. }
  15. break;
  16. case L2_TOK_NUMBER:
  17. printf("%i:%i\t%s '%g'\n", tok->line, tok->ch,
  18. l2_token_get_name(tok), tok->v.num);
  19. break;
  20. default:
  21. printf("%i:%i\t%s\n", tok->line, tok->ch,
  22. l2_token_get_name(tok));
  23. break;
  24. }
  25. }
  26. const char *l2_token_kind_name(enum l2_token_kind kind) {
  27. switch (kind) {
  28. case L2_TOK_OPEN_PAREN_NS:
  29. return "open-paren-no-space";
  30. case L2_TOK_OPEN_PAREN:
  31. return "open-paren";
  32. case L2_TOK_CLOSE_PAREN:
  33. return "close-paren";
  34. case L2_TOK_OPEN_BRACE:
  35. return "open-brace";
  36. case L2_TOK_CLOSE_BRACE:
  37. return "close-brace";
  38. case L2_TOK_OPEN_BRACKET:
  39. return "open-bracket";
  40. case L2_TOK_CLOSE_BRACKET:
  41. return "close-bracket";
  42. case L2_TOK_QUOT:
  43. return "single-quote";
  44. case L2_TOK_COMMA:
  45. return "comma";
  46. case L2_TOK_PERIOD:
  47. return "period";
  48. case L2_TOK_DOT_NUMBER:
  49. return "dot-number";
  50. case L2_TOK_COLON:
  51. return "colon";
  52. case L2_TOK_COLON_EQ:
  53. return "colon-equals";
  54. case L2_TOK_EQUALS:
  55. return "equals";
  56. case L2_TOK_EOL:
  57. return "end-of-line";
  58. case L2_TOK_EOF:
  59. return "end-of-file";
  60. case L2_TOK_NUMBER:
  61. return "number";
  62. case L2_TOK_STRING:
  63. return "string";
  64. case L2_TOK_IDENT:
  65. return "ident";
  66. case L2_TOK_ERROR:
  67. return "error";
  68. }
  69. return "(unknown)";
  70. }
  71. void l2_token_free(struct l2_token *tok) {
  72. enum l2_token_kind kind = l2_token_get_kind(tok);
  73. if (
  74. (kind == L2_TOK_STRING || kind == L2_TOK_IDENT) &&
  75. !l2_token_is_small(tok)) {
  76. free(tok->v.str);
  77. }
  78. }
  79. struct l2_token_value l2_token_extract_val(struct l2_token *tok) {
  80. struct l2_token_value v = tok->v;
  81. tok->v.str = NULL;
  82. return v;
  83. }
  84. void l2_lexer_init(struct l2_lexer *lexer, struct l2_io_reader *r) {
  85. lexer->toks[0].v.flags = L2_TOK_EOF,
  86. lexer->tokidx = 0;
  87. lexer->line = 1;
  88. lexer->ch = 1;
  89. lexer->parens = 0;
  90. lexer->do_log_tokens = 0;
  91. l2_bufio_reader_init(&lexer->reader, r);
  92. }
  93. static int peek_ch(struct l2_lexer *lexer) {
  94. int ch = l2_bufio_peek(&lexer->reader, 1);
  95. return ch;
  96. }
  97. static int peek_ch_n(struct l2_lexer *lexer, int n) {
  98. int ch = l2_bufio_peek(&lexer->reader, n);
  99. return ch;
  100. }
  101. static int read_ch(struct l2_lexer *lexer) {
  102. int ch = l2_bufio_get(&lexer->reader);
  103. lexer->ch += 1;
  104. if (ch == '\n') {
  105. lexer->ch = 1;
  106. lexer->line += 1;
  107. }
  108. return ch;
  109. }
  110. static int is_whitespace(int ch) {
  111. return ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t';
  112. }
  113. static int is_numeric(int ch) {
  114. return ch >= '0' && ch <= '9';
  115. }
  116. static int is_ident(int ch) {
  117. return !is_whitespace(ch) && ch != EOF &&
  118. ch != '(' && ch != ')' &&
  119. ch != '{' && ch != '}' &&
  120. ch != '[' && ch != ']' &&
  121. ch != '\'' &&
  122. ch != ',' && ch != '.' &&
  123. ch != ':' && ch != ';';
  124. }
  125. static void skip_whitespace(struct l2_lexer *lexer, int *nl, int *skipped) {
  126. while (1) {
  127. if (is_whitespace(peek_ch(lexer))) {
  128. *skipped = 1;
  129. do {
  130. int ch = read_ch(lexer);
  131. if (ch == '\n') {
  132. *nl = 1;
  133. }
  134. } while (is_whitespace(peek_ch(lexer)));
  135. }
  136. if (peek_ch(lexer) == '#') {
  137. *nl = 1;
  138. while (read_ch(lexer) != '\n');
  139. } else {
  140. break;
  141. }
  142. *skipped = 1;
  143. }
  144. }
  145. static int read_integer(struct l2_lexer *lexer, long long *num, long long *base, char **err) {
  146. unsigned char buffer[32]; // Should be enough
  147. size_t len = 0;
  148. long long b = -1;
  149. while (len < sizeof(buffer)) {
  150. int ch = peek_ch(lexer);
  151. if (is_numeric(ch)) {
  152. buffer[len++] = ch;
  153. } else if (ch != '\'') {
  154. break;
  155. }
  156. read_ch(lexer);
  157. }
  158. int ch = peek_ch(lexer);
  159. int abbrev_prefix = len == 1 && buffer[0] == '0';
  160. if (abbrev_prefix && ch == 'b') {
  161. b = 2;
  162. } else if (abbrev_prefix && ch == 'o') {
  163. b = 8;
  164. } else if (abbrev_prefix && ch == 'x') {
  165. b = 16;
  166. } else {
  167. // Need to parse the number as base 10 now
  168. long long n = 0;
  169. long long pow = 1;
  170. for (ssize_t i = len - 1; i >= 0; --i) {
  171. n += (buffer[i] - '0') * pow;
  172. pow *= 10;
  173. }
  174. if (ch == 'r') {
  175. b = n;
  176. } else {
  177. *num = n;
  178. *base = 10;
  179. return 0;
  180. }
  181. }
  182. if (b < 2) {
  183. *err = "Number with base lower than 2";
  184. return -1;
  185. } else if (b > 36) {
  186. *err = "Number with base higher than 36";
  187. return -1;
  188. }
  189. // Now that we know the base, we can read in the next part of the number
  190. read_ch(lexer); // Skip the base marker ('x', 'r', etc)
  191. len = 0;
  192. while (len < sizeof(buffer)) {
  193. int ch = peek_ch(lexer);
  194. if (ch == '\'') {
  195. read_ch(lexer);
  196. continue;
  197. }
  198. int digit;
  199. if (ch >= '0' && ch <= '9') {
  200. digit = ch - '0';
  201. } else if (ch >= 'a' && ch <= 'z') {
  202. digit = ch - 'a' + 10;
  203. } else if (ch >= 'A' && ch <= 'Z') {
  204. digit = ch - 'A' + 10;
  205. } else {
  206. break;
  207. }
  208. if (digit >= b) {
  209. *err = "Number with digit too big for the base";
  210. return -1;
  211. }
  212. buffer[len++] = digit;
  213. read_ch(lexer);
  214. }
  215. if (len < 1) {
  216. *err = "Number with no digits";
  217. }
  218. long long n = 0;
  219. long long pow = 1;
  220. for (ssize_t i = len - 1; i >= 0; --i) {
  221. n += buffer[i] * pow;
  222. pow *= b;
  223. }
  224. *num = n;
  225. *base = b;
  226. return 0;
  227. }
  228. static void read_number(struct l2_lexer *lexer, struct l2_token *tok) {
  229. tok->v.flags = L2_TOK_NUMBER;
  230. float sign = 1;
  231. if (peek_ch(lexer) == '-') {
  232. sign = -1;
  233. read_ch(lexer);
  234. }
  235. if (!is_numeric(peek_ch(lexer))) {
  236. tok->v.flags = L2_TOK_ERROR;
  237. tok->v.str = "No number in number literal";
  238. return;
  239. }
  240. long long integral;
  241. long long base;
  242. char *err;
  243. if (read_integer(lexer, &integral, &base, &err) < 0) {
  244. tok->v.flags = L2_TOK_ERROR;
  245. tok->v.str = err;
  246. return;
  247. }
  248. if (peek_ch(lexer) != '.') {
  249. tok->v.num = (double)integral * sign;
  250. return;
  251. }
  252. read_ch(lexer); // '.'
  253. unsigned char buffer[32];
  254. size_t fraction_len = 0;
  255. while (fraction_len < sizeof(buffer)) {
  256. int ch = peek_ch(lexer);
  257. if (ch == '\'') {
  258. read_ch(lexer);
  259. continue;
  260. }
  261. int digit;
  262. if (ch >= '0' && ch <= '9') {
  263. digit = ch - '0';
  264. } else if (ch >= 'a' && ch <= 'z') {
  265. digit = ch - 'a' + 10;
  266. } else if (ch >= 'A' && ch <= 'Z') {
  267. digit = ch - 'A' + 10;
  268. } else {
  269. break;
  270. }
  271. if (digit >= base) {
  272. tok->v.flags = L2_TOK_ERROR;
  273. tok->v.str = "Number with digits too big for the base";
  274. return;
  275. }
  276. buffer[fraction_len++] = digit;
  277. read_ch(lexer);
  278. }
  279. if (fraction_len < 1) {
  280. tok->v.flags = L2_TOK_ERROR;
  281. tok->v.str = "Trailing dot in number literal";
  282. return;
  283. }
  284. long long fraction = 0;
  285. long long fraction_power = 1;
  286. for (ssize_t i = fraction_len - 1; (ssize_t)i >= 0; --i) {
  287. fraction += buffer[i] * fraction_power;
  288. fraction_power *= base;
  289. }
  290. double num = (double)integral + ((double)fraction / (double)fraction_power);
  291. tok->v.num = num * sign;
  292. }
  293. static void read_string(struct l2_lexer *lexer, struct l2_token *tok) {
  294. tok->v.flags = L2_TOK_STRING | L2_TOK_SMALL;
  295. char *dest = tok->v.strbuf;
  296. size_t size = sizeof(tok->v.strbuf);
  297. size_t idx = 0;
  298. while (1) {
  299. int ch = read_ch(lexer);
  300. if (ch == '"') {
  301. dest[idx] = '\0';
  302. return;
  303. } else if (ch == EOF) {
  304. if (!l2_token_is_small(tok)) {
  305. free(tok->v.str);
  306. }
  307. tok->v.flags = L2_TOK_ERROR;
  308. tok->v.str = "Unexpected EOF";
  309. return;
  310. } else if (ch == '\\') {
  311. int ch2 = read_ch(lexer);
  312. switch (ch2) {
  313. case 'n':
  314. ch = '\n';
  315. break;
  316. case 'r':
  317. ch = '\r';
  318. break;
  319. case 't':
  320. ch = '\t';
  321. break;
  322. case EOF:
  323. if (!l2_token_is_small(tok)) {
  324. free(tok->v.str);
  325. }
  326. tok->v.flags = L2_TOK_ERROR;
  327. tok->v.str = "Unexpected EOF";
  328. return;
  329. default:
  330. ch = ch2;
  331. break;
  332. }
  333. }
  334. dest[idx++] = (char)ch;
  335. // The first time we run out of space, we have to switch away from
  336. // the small-string optimization and malloc memory.
  337. if (idx + 1 >= size) {
  338. char *newbuf;
  339. if (l2_token_is_small(tok)) {
  340. tok->v.flags &= ~L2_TOK_SMALL;
  341. size = 32;
  342. newbuf = malloc(size);
  343. if (newbuf == NULL) {
  344. tok->v.flags = L2_TOK_ERROR;
  345. tok->v.str = "Allocation failure";
  346. return;
  347. }
  348. memcpy(newbuf, tok->v.strbuf, idx);
  349. } else {
  350. size *= 2;
  351. newbuf = realloc(tok->v.str, size);
  352. if (newbuf == NULL) {
  353. free(tok->v.str);
  354. tok->v.flags = L2_TOK_ERROR;
  355. tok->v.str = "Allocation failure";
  356. return;
  357. }
  358. }
  359. tok->v.str = newbuf;
  360. dest = newbuf;
  361. }
  362. }
  363. }
  364. static void read_ident(struct l2_lexer *lexer, struct l2_token *tok) {
  365. tok->v.flags = L2_TOK_IDENT | L2_TOK_SMALL;
  366. char *dest = tok->v.strbuf;
  367. size_t size = sizeof(tok->v.strbuf);
  368. size_t idx = 0;
  369. while (1) {
  370. int ch = peek_ch(lexer);
  371. if (!is_ident(ch)) {
  372. dest[idx] = '\0';
  373. return;
  374. }
  375. dest[idx++] = (char)read_ch(lexer);
  376. // The first time we run out of space, we have to switch away from
  377. // the small-string optimization and malloc memory.
  378. if (idx + 1 >= size) {
  379. char *newbuf;
  380. if (l2_token_is_small(tok)) {
  381. tok->v.flags &= ~L2_TOK_SMALL;
  382. size = 32;
  383. newbuf = malloc(size);
  384. if (newbuf == NULL) {
  385. tok->v.flags = L2_TOK_ERROR;
  386. tok->v.str = "Allocation failure";
  387. return;
  388. }
  389. memcpy(newbuf, tok->v.strbuf, idx);
  390. } else {
  391. size *= 2;
  392. newbuf = realloc(tok->v.str, size);
  393. if (newbuf == NULL) {
  394. free(tok->v.str);
  395. tok->v.flags = L2_TOK_ERROR;
  396. tok->v.str = "Allocation failure";
  397. return;
  398. }
  399. }
  400. tok->v.str = newbuf;
  401. dest = newbuf;
  402. }
  403. }
  404. }
  405. static void read_tok(struct l2_lexer *lexer, struct l2_token *tok) {
  406. tok->line = lexer->line;
  407. tok->ch = lexer->ch;
  408. int nl = 0, skipped_whitespace = 0;
  409. skip_whitespace(lexer, &nl, &skipped_whitespace);
  410. if (nl && lexer->parens == 0) {
  411. tok->v.flags = L2_TOK_EOL;
  412. return;
  413. }
  414. int ch = peek_ch(lexer);
  415. switch (ch) {
  416. case '(':
  417. read_ch(lexer);
  418. if (skipped_whitespace) {
  419. tok->v.flags = L2_TOK_OPEN_PAREN;
  420. } else {
  421. tok->v.flags =L2_TOK_OPEN_PAREN_NS;
  422. }
  423. lexer->parens += 1;
  424. break;
  425. case ')':
  426. read_ch(lexer);
  427. tok->v.flags = L2_TOK_CLOSE_PAREN;
  428. lexer->parens -= 1;
  429. break;
  430. case '{':
  431. read_ch(lexer);
  432. tok->v.flags = L2_TOK_OPEN_BRACE;
  433. break;
  434. case '}':
  435. read_ch(lexer);
  436. tok->v.flags = L2_TOK_CLOSE_BRACE;
  437. break;
  438. case '[':
  439. read_ch(lexer);
  440. tok->v.flags = L2_TOK_OPEN_BRACKET;
  441. break;
  442. case ']':
  443. read_ch(lexer);
  444. tok->v.flags = L2_TOK_CLOSE_BRACKET;
  445. break;
  446. case ';':
  447. tok->v.flags = L2_TOK_EOL;
  448. do {
  449. read_ch(lexer);
  450. skip_whitespace(lexer, &nl, &skipped_whitespace);
  451. } while (peek_ch(lexer) == ';');
  452. break;
  453. case '\'':
  454. read_ch(lexer);
  455. tok->v.flags = L2_TOK_QUOT;
  456. break;
  457. case ',':
  458. read_ch(lexer);
  459. tok->v.flags = L2_TOK_COMMA;
  460. break;
  461. case '.':
  462. read_ch(lexer);
  463. if (is_numeric(peek_ch(lexer))) {
  464. tok->v.flags = L2_TOK_DOT_NUMBER;
  465. long long num, base;
  466. char *err;
  467. if (read_integer(lexer, &num, &base, &err) < 0) {
  468. tok->v.flags = L2_TOK_ERROR;
  469. tok->v.str = err;
  470. } else {
  471. tok->v.integer = (int)num;
  472. }
  473. } else {
  474. tok->v.flags = L2_TOK_PERIOD;
  475. }
  476. break;
  477. case ':':
  478. read_ch(lexer);
  479. ch = peek_ch(lexer);
  480. switch (ch) {
  481. case '=':
  482. read_ch(lexer);
  483. tok->v.flags = L2_TOK_COLON_EQ;
  484. break;
  485. default:
  486. tok->v.flags = L2_TOK_COLON;
  487. break;
  488. }
  489. break;
  490. case EOF:
  491. tok->v.flags = L2_TOK_EOF;
  492. break;
  493. case '"':
  494. read_ch(lexer);
  495. read_string(lexer, tok);
  496. break;
  497. default:
  498. {
  499. int ch2 = peek_ch_n(lexer, 2);
  500. if (
  501. is_numeric(ch) ||
  502. (ch == '-' && is_numeric(ch2))) {
  503. read_number(lexer, tok);
  504. break;
  505. }
  506. tok->v.flags = L2_TOK_IDENT;
  507. read_ident(lexer, tok);
  508. if (l2_token_is_small(tok) && strcmp(tok->v.strbuf, "=") == 0) {
  509. tok->v.flags = L2_TOK_EQUALS;
  510. }
  511. }
  512. }
  513. }
  514. struct l2_token *l2_lexer_peek(struct l2_lexer *lexer, int count) {
  515. int offset = count - 1;
  516. while (offset >= lexer->tokidx) {
  517. read_tok(lexer, &lexer->toks[lexer->tokidx++]);
  518. if (lexer->do_log_tokens) {
  519. log_token(&lexer->toks[lexer->tokidx - 1]);
  520. }
  521. }
  522. return &lexer->toks[offset];
  523. }
  524. void l2_lexer_consume(struct l2_lexer *lexer) {
  525. l2_token_free(&lexer->toks[0]);
  526. lexer->tokidx -= 1;
  527. memmove(lexer->toks, lexer->toks + 1, lexer->tokidx * sizeof(*lexer->toks));
  528. }
  529. void l2_lexer_skip_opt(struct l2_lexer *lexer, enum l2_token_kind kind) {
  530. if (l2_token_get_kind(l2_lexer_peek(lexer, 1)) == kind) {
  531. l2_lexer_consume(lexer);
  532. }
  533. }