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

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