authorgravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2020-10-30 10:52:30-04:00
committergravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2020-10-30 10:52:30-04:00
log5dd36ee6ceddc6ea5c6e3353198b7f8c2a703365
tree19ec7080c308a7e184fc1306ccee42494a40749f
parent72343ffd06dbbc95424d74c93188ba4a6aa74c49

Add support for comments in multiline string literal


1 files changed, 38 insertions(+), 4 deletions(-)

src/stage1/parser.cpp+38-4
...@@ -493,6 +493,31 @@ static AstNode *ast_parse_root(ParseContext *pc) {...@@ -493,6 +493,31 @@ static AstNode *ast_parse_root(ParseContext *pc) {
493 return node;493 return node;
494}494}
495495
496static Token *ast_parse_multiline_string_literal(ParseContext *pc, Buf *buf) {
497 Token *first_str_token = nullptr;
498 Token *str_token = nullptr;
499 while ((str_token = eat_token_if(pc, TokenIdMultilineStringLiteral))) {
500 if (first_str_token == nullptr) {
501 first_str_token = str_token;
502 }
503 if (buf->list.length == 0) {
504 buf_resize(buf, 0);
505 }
506 buf_append_buf(buf, token_buf(str_token));
507
508 // Ignore inline comments
509 size_t cur_token = pc->current_token;
510 while (eat_token_if(pc, TokenIdDocComment));
511
512 // Lookahead to see if there's another multilne string literal,
513 // if not, we have to revert back to before the doc comment
514 if (peek_token(pc)->id != TokenIdMultilineStringLiteral) {
515 pc->current_token = cur_token;
516 }
517 }
518 return first_str_token;
519}
520
496static Token *ast_parse_doc_comments(ParseContext *pc, Buf *buf) {521static Token *ast_parse_doc_comments(ParseContext *pc, Buf *buf) {
497 Token *first_doc_token = nullptr;522 Token *first_doc_token = nullptr;
498 Token *doc_token = nullptr;523 Token *doc_token = nullptr;
...@@ -605,7 +630,7 @@ static AstNodeContainerDecl ast_parse_container_members(ParseContext *pc) {...@@ -605,7 +630,7 @@ static AstNodeContainerDecl ast_parse_container_members(ParseContext *pc) {
605 case ContainerFieldStateSeen:630 case ContainerFieldStateSeen:
606 break;631 break;
607 case ContainerFieldStateEnd:632 case ContainerFieldStateEnd:
608 ast_error(pc, first_token, "declarations are not allowed between container fields"); 633 ast_error(pc, first_token, "declarations are not allowed between container fields");
609 }634 }
610635
611 assert(container_field->type == NodeTypeStructField);636 assert(container_field->type == NodeTypeStructField);
...@@ -1754,12 +1779,21 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {...@@ -1754,12 +1779,21 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
1754 if (unreachable != nullptr)1779 if (unreachable != nullptr)
1755 return ast_create_node(pc, NodeTypeUnreachable, unreachable);1780 return ast_create_node(pc, NodeTypeUnreachable, unreachable);
17561781
1782
1783 Buf *string_buf;
1757 Token *string_lit = eat_token_if(pc, TokenIdStringLiteral);1784 Token *string_lit = eat_token_if(pc, TokenIdStringLiteral);
1758 if (string_lit == nullptr)1785 if (string_lit != nullptr) {
1759 string_lit = eat_token_if(pc, TokenIdMultilineStringLiteral);1786 string_buf = token_buf(string_lit);
1787 } else {
1788 Buf multiline_string_buf = BUF_INIT;
1789 string_lit = ast_parse_multiline_string_literal(pc, &multiline_string_buf);
1790 if (string_lit != nullptr) {
1791 string_buf = buf_create_from_buf(&multiline_string_buf);
1792 }
1793 }
1760 if (string_lit != nullptr) {1794 if (string_lit != nullptr) {
1761 AstNode *res = ast_create_node(pc, NodeTypeStringLiteral, string_lit);1795 AstNode *res = ast_create_node(pc, NodeTypeStringLiteral, string_lit);
1762 res->data.string_literal.buf = token_buf(string_lit);1796 res->data.string_literal.buf = string_buf;
1763 return res;1797 return res;
1764 }1798 }
17651799