MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

improve template literal scanning with dedicated helpers

+68 -25
+68 -25
src/ant.c
··· 4569 4569 return TOK_ERR; 4570 4570 } 4571 4571 4572 - static inline uint8_t scan_template(struct js *js, const char *buf, jsoff_t rem) { 4573 - jsoff_t i = 1; 4572 + static inline jsoff_t skip_string_literal(const char *buf, jsoff_t rem, jsoff_t start, char quote) { 4573 + jsoff_t i = start + 1; 4574 + while (i < rem) { 4575 + if (buf[i] == '\\') { i += 2; continue; } 4576 + if (buf[i] == quote) { return i + 1; } i++; 4577 + } 4578 + return rem; 4579 + } 4580 + 4581 + static inline jsoff_t skip_line_comment(const char *buf, jsoff_t rem, jsoff_t start) { 4582 + jsoff_t i = start + 2; 4583 + while (i < rem && buf[i] != '\n') i++; 4584 + return i; 4585 + } 4586 + 4587 + static inline jsoff_t skip_block_comment(const char *buf, jsoff_t rem, jsoff_t start) { 4588 + jsoff_t i = start + 2; 4589 + while (i + 1 < rem && !(buf[i] == '*' && buf[i + 1] == '/')) i++; 4590 + return (i + 1 < rem) ? (i + 2) : rem; 4591 + } 4592 + 4593 + static jsoff_t skip_template_literal(const char *buf, jsoff_t rem, jsoff_t start) { 4594 + jsoff_t i = start + 1; 4595 + int expr_depth = 0; 4574 4596 4575 4597 while (i < rem) { 4576 - const char *p = buf + i; 4577 - jsoff_t search_len = rem - i; 4598 + char c = buf[i]; 4578 4599 4579 - const char *q = memchr(p, '`', search_len); 4580 - const char *b = memchr(p, '\\', search_len); 4600 + if (c == '\\') { 4601 + i += 2; 4602 + continue; 4603 + } 4604 + 4605 + if (expr_depth == 0) { 4606 + if (c == '`') return i + 1; 4607 + if (c == '$' && i + 1 < rem && buf[i + 1] == '{') { 4608 + expr_depth = 1; 4609 + i += 2; 4610 + continue; 4611 + } i++; continue; 4612 + } 4581 4613 4582 - if (q == NULL) { 4583 - js->tok = TOK_ERR; 4584 - js->tlen = rem; 4585 - return TOK_ERR; 4614 + if (c == '\'' || c == '"') { 4615 + i = skip_string_literal(buf, rem, i, c); 4616 + continue; 4586 4617 } 4587 4618 4588 - if (b == NULL || q < b) { 4589 - i = (jsoff_t)((q - buf) + 1); 4590 - js->tok = TOK_TEMPLATE; 4591 - js->tlen = i; 4592 - return TOK_TEMPLATE; 4619 + if (c == '`') { 4620 + jsoff_t next = skip_template_literal(buf, rem, i); 4621 + if (next <= i) return rem; 4622 + i = next; continue; 4593 4623 } 4594 4624 4595 - jsoff_t esc_pos = (jsoff_t)(b - buf); 4596 - i = esc_pos + 2; 4597 - if (i > rem) { 4598 - js->tok = TOK_ERR; 4599 - js->tlen = rem; 4600 - return TOK_ERR; 4625 + if (c == '/' && i + 1 < rem) { 4626 + if (buf[i + 1] == '/') { i = skip_line_comment(buf, rem, i); continue; } 4627 + if (buf[i + 1] == '*') { i = skip_block_comment(buf, rem, i); continue; } 4601 4628 } 4629 + 4630 + if (c == '{') { expr_depth++; i++; continue; } 4631 + if (c == '}') { expr_depth--; i++; continue; } 4632 + 4633 + i++; 4602 4634 } 4603 4635 4604 - js->tok = TOK_ERR; 4605 - js->tlen = rem; 4606 - return TOK_ERR; 4636 + return rem; 4637 + } 4638 + 4639 + static inline uint8_t scan_template(struct js *js, const char *buf, jsoff_t rem) { 4640 + jsoff_t end = skip_template_literal(buf, rem, 0); 4641 + if (end <= 1 || end > rem) { 4642 + js->tok = TOK_ERR; 4643 + js->tlen = rem; 4644 + return TOK_ERR; 4645 + } 4646 + 4647 + js->tok = TOK_TEMPLATE; 4648 + js->tlen = end; 4649 + return TOK_TEMPLATE; 4607 4650 } 4608 4651 4609 4652 static inline uint8_t parse_operator(struct js *js, const char *buf, jsoff_t rem) { ··· 23491 23534 fprintf(stream, "\x1b[0m)\n"); 23492 23535 } 23493 23536 } 23494 - } 23537 + }