Handle line comment better

This commit is contained in:
gingerBill
2022-01-24 23:42:04 +00:00
parent fe0b5bf4e2
commit c0479f1564
+15 -5
View File
@@ -1244,7 +1244,7 @@ CommentGroup *consume_comment_group(AstFile *f, isize n, isize *end_line_) {
return comments; return comments;
} }
void comsume_comment_groups(AstFile *f, Token prev) { void consume_comment_groups(AstFile *f, Token prev) {
if (f->curr_token.kind == Token_Comment) { if (f->curr_token.kind == Token_Comment) {
CommentGroup *comment = nullptr; CommentGroup *comment = nullptr;
isize end_line = 0; isize end_line = 0;
@@ -1288,7 +1288,7 @@ Token advance_token(AstFile *f) {
if (ok) { if (ok) {
switch (f->curr_token.kind) { switch (f->curr_token.kind) {
case Token_Comment: case Token_Comment:
comsume_comment_groups(f, prev); consume_comment_groups(f, prev);
break; break;
case Token_Semicolon: case Token_Semicolon:
if (ignore_newlines(f) && f->curr_token.string == "\n") { if (ignore_newlines(f) && f->curr_token.string == "\n") {
@@ -1699,6 +1699,16 @@ Array<Ast *> parse_element_list(AstFile *f) {
return elems; return elems;
} }
CommentGroup *consume_line_comment(AstFile *f) {
CommentGroup *comment = f->line_comment;
if (f->line_comment == f->lead_comment) {
f->lead_comment = nullptr;
}
f->line_comment = nullptr;
return comment;
}
Array<Ast *> parse_enum_field_list(AstFile *f) { Array<Ast *> parse_enum_field_list(AstFile *f) {
auto elems = array_make<Ast *>(heap_allocator()); auto elems = array_make<Ast *>(heap_allocator());
@@ -1713,7 +1723,7 @@ Array<Ast *> parse_enum_field_list(AstFile *f) {
value = parse_value(f); value = parse_value(f);
} }
comment = f->line_comment; comment = consume_line_comment(f);
Ast *elem = ast_enum_field_value(f, name, value, docs, comment); Ast *elem = ast_enum_field_value(f, name, value, docs, comment);
array_add(&elems, elem); array_add(&elems, elem);
@@ -1723,7 +1733,7 @@ Array<Ast *> parse_enum_field_list(AstFile *f) {
} }
if (!elem->EnumFieldValue.comment) { if (!elem->EnumFieldValue.comment) {
elem->EnumFieldValue.comment = f->line_comment; elem->EnumFieldValue.comment = consume_line_comment(f);
} }
} }
@@ -5438,7 +5448,7 @@ bool parse_file(Parser *p, AstFile *f) {
String filepath = f->tokenizer.fullpath; String filepath = f->tokenizer.fullpath;
String base_dir = dir_from_path(filepath); String base_dir = dir_from_path(filepath);
if (f->curr_token.kind == Token_Comment) { if (f->curr_token.kind == Token_Comment) {
comsume_comment_groups(f, f->prev_token); consume_comment_groups(f, f->prev_token);
} }
CommentGroup *docs = f->lead_comment; CommentGroup *docs = f->lead_comment;