docs.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Generates Documentation
  2. gb_global int print_entity_kind_ordering[Entity_Count] = {
  3. /*Invalid*/ -1,
  4. /*Constant*/ 0,
  5. /*Variable*/ 1,
  6. /*TypeName*/ 4,
  7. /*Procedure*/ 2,
  8. /*ProcGroup*/ 3,
  9. /*Builtin*/ -1,
  10. /*ImportName*/ -1,
  11. /*LibraryName*/ -1,
  12. /*Nil*/ -1,
  13. /*Label*/ -1,
  14. };
  15. gb_global char const *print_entity_names[Entity_Count] = {
  16. /*Invalid*/ "",
  17. /*Constant*/ "constants",
  18. /*Variable*/ "variables",
  19. /*TypeName*/ "types",
  20. /*Procedure*/ "procedures",
  21. /*ProcGroup*/ "proc_group",
  22. /*Builtin*/ "",
  23. /*ImportName*/ "import names",
  24. /*LibraryName*/ "library names",
  25. /*Nil*/ "",
  26. /*Label*/ "",
  27. };
  28. GB_COMPARE_PROC(cmp_entities_for_printing) {
  29. GB_ASSERT(a != nullptr);
  30. GB_ASSERT(b != nullptr);
  31. Entity *x = *cast(Entity **)a;
  32. Entity *y = *cast(Entity **)b;
  33. int res = 0;
  34. if (x->pkg != y->pkg) {
  35. if (x->pkg == nullptr) {
  36. return -1;
  37. }
  38. if (y->pkg == nullptr) {
  39. return +1;
  40. }
  41. res = string_compare(x->pkg->name, y->pkg->name);
  42. if (res != 0) {
  43. return res;
  44. }
  45. }
  46. int ox = print_entity_kind_ordering[x->kind];
  47. int oy = print_entity_kind_ordering[y->kind];
  48. res = ox - oy;
  49. if (res != 0) {
  50. return res;
  51. }
  52. res = string_compare(x->token.string, y->token.string);
  53. return res;
  54. }
  55. GB_COMPARE_PROC(cmp_ast_package_by_name) {
  56. GB_ASSERT(a != nullptr);
  57. GB_ASSERT(b != nullptr);
  58. AstPackage *x = *cast(AstPackage **)a;
  59. AstPackage *y = *cast(AstPackage **)b;
  60. return string_compare(x->name, y->name);
  61. }
  62. #include "docs_format.cpp"
  63. #include "docs_writer.cpp"
  64. void print_doc_line(i32 indent, char const *fmt, ...) {
  65. while (indent --> 0) {
  66. gb_printf("\t");
  67. }
  68. va_list va;
  69. va_start(va, fmt);
  70. gb_printf_va(fmt, va);
  71. va_end(va);
  72. gb_printf("\n");
  73. }
  74. void print_doc_line_no_newline(i32 indent, char const *fmt, ...) {
  75. while (indent --> 0) {
  76. gb_printf("\t");
  77. }
  78. va_list va;
  79. va_start(va, fmt);
  80. gb_printf_va(fmt, va);
  81. va_end(va);
  82. }
  83. bool print_doc_comment_group_string(i32 indent, CommentGroup *g) {
  84. if (g == nullptr) {
  85. return false;
  86. }
  87. isize len = 0;
  88. for_array(i, g->list) {
  89. String comment = g->list[i].string;
  90. len += comment.len;
  91. len += 1; // for \n
  92. }
  93. if (len <= g->list.count) {
  94. return false;
  95. }
  96. isize count = 0;
  97. for_array(i, g->list) {
  98. String comment = g->list[i].string;
  99. String original_comment = comment;
  100. bool slash_slash = comment[1] == '/';
  101. if (comment[1] == '/') {
  102. comment.text += 2;
  103. comment.len -= 2;
  104. } else if (comment[1] == '*') {
  105. comment.text += 2;
  106. comment.len -= 4;
  107. }
  108. // Ignore the first space
  109. if (comment.len > 0 && comment[0] == ' ') {
  110. comment.text += 1;
  111. comment.len -= 1;
  112. }
  113. if (slash_slash) {
  114. if (string_starts_with(comment, str_lit("+"))) {
  115. continue;
  116. }
  117. if (string_starts_with(comment, str_lit("@("))) {
  118. continue;
  119. }
  120. }
  121. if (slash_slash) {
  122. print_doc_line(indent, "%.*s", LIT(comment));
  123. count += 1;
  124. } else {
  125. isize pos = 0;
  126. for (; pos < comment.len; pos++) {
  127. isize end = pos;
  128. for (; end < comment.len; end++) {
  129. if (comment[end] == '\n') {
  130. break;
  131. }
  132. }
  133. String line = substring(comment, pos, end);
  134. pos = end+1;
  135. String trimmed_line = string_trim_whitespace(line);
  136. if (trimmed_line.len == 0) {
  137. if (count == 0) {
  138. continue;
  139. }
  140. }
  141. /*
  142. * Remove comments with
  143. * styles
  144. * like this
  145. */
  146. if (string_starts_with(line, str_lit("* "))) {
  147. line = substring(line, 2, line.len);
  148. }
  149. print_doc_line(indent, "%.*s", LIT(line));
  150. count += 1;
  151. }
  152. }
  153. }
  154. if (count > 0) {
  155. print_doc_line(0, "");
  156. return true;
  157. }
  158. return false;
  159. }
  160. void print_doc_expr(Ast *expr) {
  161. gbString s = nullptr;
  162. if (build_context.cmd_doc_flags & CmdDocFlag_Short) {
  163. s = expr_to_string_shorthand(expr);
  164. } else {
  165. s = expr_to_string(expr);
  166. }
  167. gb_file_write(gb_file_get_standard(gbFileStandard_Output), s, gb_string_length(s));
  168. gb_string_free(s);
  169. }
  170. void print_doc_package(CheckerInfo *info, AstPackage *pkg) {
  171. if (pkg == nullptr) {
  172. return;
  173. }
  174. print_doc_line(0, "package %.*s", LIT(pkg->name));
  175. for_array(i, pkg->files) {
  176. AstFile *f = pkg->files[i];
  177. if (f->pkg_decl) {
  178. GB_ASSERT(f->pkg_decl->kind == Ast_PackageDecl);
  179. print_doc_comment_group_string(1, f->pkg_decl->PackageDecl.docs);
  180. }
  181. }
  182. if (pkg->scope != nullptr) {
  183. auto entities = array_make<Entity *>(heap_allocator(), 0, pkg->scope->elements.entries.count);
  184. defer (array_free(&entities));
  185. for_array(i, pkg->scope->elements.entries) {
  186. Entity *e = pkg->scope->elements.entries[i].value;
  187. switch (e->kind) {
  188. case Entity_Invalid:
  189. case Entity_Builtin:
  190. case Entity_Nil:
  191. case Entity_Label:
  192. continue;
  193. case Entity_Constant:
  194. case Entity_Variable:
  195. case Entity_TypeName:
  196. case Entity_Procedure:
  197. case Entity_ProcGroup:
  198. case Entity_ImportName:
  199. case Entity_LibraryName:
  200. // Fine
  201. break;
  202. }
  203. array_add(&entities, e);
  204. }
  205. gb_sort_array(entities.data, entities.count, cmp_entities_for_printing);
  206. bool show_docs = (build_context.cmd_doc_flags & CmdDocFlag_Short) == 0;
  207. EntityKind curr_entity_kind = Entity_Invalid;
  208. for_array(i, entities) {
  209. Entity *e = entities[i];
  210. if (e->pkg != pkg) {
  211. continue;
  212. }
  213. if (!is_entity_exported(e)) {
  214. continue;
  215. }
  216. if (curr_entity_kind != e->kind) {
  217. if (curr_entity_kind != Entity_Invalid) {
  218. print_doc_line(0, "");
  219. }
  220. curr_entity_kind = e->kind;
  221. print_doc_line(1, "%s", print_entity_names[e->kind]);
  222. }
  223. Ast *type_expr = nullptr;
  224. Ast *init_expr = nullptr;
  225. Ast *decl_node = nullptr;
  226. CommentGroup *comment = nullptr;
  227. CommentGroup *docs = nullptr;
  228. if (e->decl_info != nullptr) {
  229. type_expr = e->decl_info->type_expr;
  230. init_expr = e->decl_info->init_expr;
  231. decl_node = e->decl_info->decl_node;
  232. comment = e->decl_info->comment;
  233. docs = e->decl_info->docs;
  234. }
  235. GB_ASSERT(type_expr != nullptr || init_expr != nullptr);
  236. print_doc_line_no_newline(2, "%.*s", LIT(e->token.string));
  237. if (type_expr != nullptr) {
  238. gbString t = expr_to_string(type_expr);
  239. gb_printf(": %s ", t);
  240. gb_string_free(t);
  241. } else {
  242. gb_printf(" :");
  243. }
  244. if (e->kind == Entity_Variable) {
  245. if (init_expr != nullptr) {
  246. gb_printf("= ");
  247. print_doc_expr(init_expr);
  248. }
  249. } else {
  250. gb_printf(": ");
  251. print_doc_expr(init_expr);
  252. }
  253. gb_printf(";\n");
  254. if (show_docs) {
  255. print_doc_comment_group_string(3, docs);
  256. }
  257. }
  258. print_doc_line(0, "");
  259. }
  260. if (pkg->fullpath.len != 0) {
  261. print_doc_line(0, "");
  262. print_doc_line(1, "fullpath:");
  263. print_doc_line(2, "%.*s", LIT(pkg->fullpath));
  264. print_doc_line(1, "files:");
  265. for_array(i, pkg->files) {
  266. AstFile *f = pkg->files[i];
  267. String filename = remove_directory_from_path(f->fullpath);
  268. print_doc_line(2, "%.*s", LIT(filename));
  269. }
  270. }
  271. }
  272. void generate_documentation(Checker *c) {
  273. CheckerInfo *info = &c->info;
  274. if (build_context.cmd_doc_flags & CmdDocFlag_DocFormat) {
  275. String init_fullpath = c->parser->init_fullpath;
  276. String output_name = {};
  277. String output_base = {};
  278. if (build_context.out_filepath.len == 0) {
  279. output_name = remove_directory_from_path(init_fullpath);
  280. output_name = remove_extension_from_path(output_name);
  281. output_name = string_trim_whitespace(output_name);
  282. if (output_name.len == 0) {
  283. output_name = info->init_scope->pkg->name;
  284. }
  285. output_base = output_name;
  286. } else {
  287. output_name = build_context.out_filepath;
  288. output_name = string_trim_whitespace(output_name);
  289. if (output_name.len == 0) {
  290. output_name = info->init_scope->pkg->name;
  291. }
  292. isize pos = string_extension_position(output_name);
  293. if (pos < 0) {
  294. output_base = output_name;
  295. } else {
  296. output_base = substring(output_name, 0, pos);
  297. }
  298. }
  299. output_base = path_to_full_path(permanent_allocator(), output_base);
  300. gbString output_file_path = gb_string_make_length(heap_allocator(), output_base.text, output_base.len);
  301. output_file_path = gb_string_appendc(output_file_path, ".odin-doc");
  302. defer (gb_string_free(output_file_path));
  303. odin_doc_write(info, output_file_path);
  304. } else {
  305. auto pkgs = array_make<AstPackage *>(permanent_allocator(), 0, info->packages.entries.count);
  306. for_array(i, info->packages.entries) {
  307. AstPackage *pkg = info->packages.entries[i].value;
  308. if (build_context.cmd_doc_flags & CmdDocFlag_AllPackages) {
  309. array_add(&pkgs, pkg);
  310. } else {
  311. if (pkg->kind == Package_Init) {
  312. array_add(&pkgs, pkg);
  313. } else if (pkg->is_extra) {
  314. array_add(&pkgs, pkg);
  315. }
  316. }
  317. }
  318. gb_sort_array(pkgs.data, pkgs.count, cmp_ast_package_by_name);
  319. for_array(i, pkgs) {
  320. print_doc_package(info, pkgs[i]);
  321. }
  322. }
  323. }