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

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