You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lex.c 12KB

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