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 11KB

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