aboutsummaryrefslogtreecommitdiff
path: root/src/docs.cpp
blob: a8347b39292659a172305bb48dc71217e58bd60c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Generates Documentation

gbString expr_to_string(AstNode *expression);

void print_declaration(Parser *parser, AstNode *decl) {
	switch (decl->kind) {
	case_ast_node(pd, ProcDecl, decl);
		GB_ASSERT(pd->name->kind == AstNode_Ident);
		String name = pd->name->Ident.string;
		if (name.len == 0) {
			break;
		}
		if (name[0] == '_') {
			break;
		}

		for_array(i, pd->docs.list) {
			String comment = pd->docs.list[i].string;
			if (comment[1] == '/') {
				comment.text += 2;
				comment.len  -= 2;
			} else if (comment[1] == '*') {
				comment.text += 2;
				comment.len  -= 4;
			}
			comment = string_trim_whitespace(comment);

			gb_printf("%.*s\n", LIT(comment));
		}

		ast_node(proc_type, ProcType, pd->type);

		gbString params = expr_to_string(proc_type->params);
		defer (gb_string_free(params));
		gb_printf("proc %.*s(%s)", LIT(name), params);
		if (proc_type->results != NULL)  {
			ast_node(fl, FieldList, proc_type->results);
			isize count = fl->list.count;
			if (count > 0) {
				gbString results = expr_to_string(proc_type->results);
				defer (gb_string_free(results));
				gb_printf(" -> ");
				if (count != 1) {
					gb_printf("(");
				}
				gb_printf("%s", results);
				if (count != 1) {
					gb_printf(")");
				}
			}
		}
		gb_printf("\n\n");
	case_end;

	case_ast_node(gd, GenDecl, decl);
	case_end;
	}
}

void generate_documentation(Parser *parser) {
	for_array(file_index, parser->files) {
		AstFile *file = &parser->files[file_index];
		Tokenizer *tokenizer = &file->tokenizer;
		String fullpath = tokenizer->fullpath;
		gb_printf("%.*s\n", LIT(fullpath));

		for_array(decl_index, file->decls) {
			AstNode *decl = file->decls[decl_index];
			print_declaration(parser, decl);
		}
	}
}