From 34ca4e92eb5316cebb66aa1c69d4ced5719e7773 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 17 Nov 2020 15:45:55 +0000 Subject: [PATCH] Fix parser logic for first comment group line in a file --- src/docs.cpp | 36 +++++++++++++++++++++++++----------- src/parser.cpp | 6 ++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/docs.cpp b/src/docs.cpp index 67e3ebbe5..50586ed8f 100644 --- a/src/docs.cpp +++ b/src/docs.cpp @@ -110,20 +110,23 @@ void print_doc_line_no_newline(i32 indent, char const *fmt, ...) { va_end(va); } -bool print_doc_comment_group_string(i32 indent, CommentGroup const &g) { +bool print_doc_comment_group_string(i32 indent, CommentGroup *g) { + if (g == nullptr) { + return false; + } isize len = 0; - for_array(i, g.list) { - String comment = g.list[i].string; + for_array(i, g->list) { + String comment = g->list[i].string; len += comment.len; len += 1; // for \n } - if (len == 0) { + if (len <= g->list.count) { return false; } isize count = 0; - for_array(i, g.list) { - String comment = g.list[i].string; + for_array(i, g->list) { + String comment = g->list[i].string; if (comment[1] == '/') { comment.text += 2; comment.len -= 2; @@ -131,7 +134,11 @@ bool print_doc_comment_group_string(i32 indent, CommentGroup const &g) { comment.text += 2; comment.len -= 4; } - comment = string_trim_whitespace(comment); + if (comment.len > 0 && comment[0] == ' ') { + comment.text += 1; + comment.len -= 1; + } + if (string_starts_with(comment, str_lit("@("))) { continue; } @@ -164,6 +171,15 @@ void print_doc_package(CheckerInfo *info, AstPackage *pkg) { print_doc_line(0, "package %.*s", LIT(pkg->name)); + + for_array(i, pkg->files) { + AstFile *f = pkg->files[i]; + if (f->pkg_decl) { + GB_ASSERT(f->pkg_decl->kind == Ast_PackageDecl); + print_doc_comment_group_string(1, f->pkg_decl->PackageDecl.docs); + } + } + if (pkg->scope != nullptr) { auto entities = array_make(heap_allocator(), 0, pkg->scope->elements.entries.count); defer (array_free(&entities)); @@ -244,10 +260,8 @@ void print_doc_package(CheckerInfo *info, AstPackage *pkg) { if (comment) { // gb_printf(" "); } - if (docs) { - if (print_doc_comment_group_string(3, *docs)) { - gb_printf("\n"); - } + if (print_doc_comment_group_string(3, docs)) { + gb_printf("\n"); } } } diff --git a/src/parser.cpp b/src/parser.cpp index ce5e53d92..9e9708f9c 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1193,6 +1193,12 @@ CommentGroup *consume_comment_group(AstFile *f, isize n, isize *end_line_) { Array list = {}; list.allocator = heap_allocator(); isize end_line = f->curr_token.pos.line; + if (f->curr_token_index == 1 && + f->prev_token.kind == Token_Comment && + f->prev_token.pos.line+1 == f->curr_token.pos.line) { + // NOTE(bill): Special logic for the first comment in the file + array_add(&list, f->prev_token); + } while (f->curr_token.kind == Token_Comment && f->curr_token.pos.line <= end_line+n) { array_add(&list, consume_comment(f, &end_line));