-
-
Notifications
You must be signed in to change notification settings - Fork 411
/
docgen.c
1145 lines (908 loc) · 30.6 KB
/
docgen.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "docgen.h"
#include "../ast/id.h"
#include "../ast/printbuf.h"
#include "../pkg/package.h"
#include "../../libponyrt/mem/pool.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
// Define a type with the docgen state that needs to passed around the
// functions below.
// The doc directory is the root directory that we put our generated files
// into. This is supplied by the caller, presumably from a command line arg.
// Most of the time we have 3 files open:
// 1. The index file. This is the mkdocs yaml file that specifies the structure
// of the document and lives in the doc directory.
// This file is opened in the top level function before any processing occurs
// and is closed by the same function once we've processed everything.
// 2. The home file. This is the page that is used as the top level. It
// contains a contents list of the packages documented (with links).
// This file is opened in the top level function before any processing occurs
// and is closed by the same function once we've processed everything.
// 3. The type file. This is the md file for a specific type and lives in the
// "docs" sub directory of the doc directory. We have one file per type.
// This file is opened and closed by the functions that handles the type. When
// closed the file pointer should always be put back to NULL.
typedef struct docgen_t
{
FILE* index_file;
FILE* home_file;
FILE* package_file;
printbuf_t* test_types;
printbuf_t* public_types;
printbuf_t* private_types;
FILE* type_file;
const char* base_dir;
const char* sub_dir;
size_t base_dir_buf_len;
size_t sub_dir_buf_len;
errors_t* errors;
} docgen_t;
// Define a list for keeping lists of ASTs ordered by name.
// Each list should have an empty head node (prefereably on the stack). Other
// nodes will be pool alloced, 1 node per AST item.
// Lists are kept ordered by interesting new items in the correct place, with a
// simple linear search.
typedef struct ast_list_t
{
ast_t* ast;
const char* name;
struct ast_list_t* next;
} ast_list_t;
// Free the contents of the given list, but not the head node which must be
// handled separately
static void doc_list_free(ast_list_t* list)
{
assert(list != NULL);
ast_list_t* p = list->next;
while(p != NULL)
{
ast_list_t* next = p->next;
POOL_FREE(ast_list_t, p);
p = next;
}
}
// Add the given AST to the given list, under the specified name
static void doc_list_add(ast_list_t* list, ast_t* ast, const char* name,
bool sort)
{
assert(list != NULL);
assert(ast != NULL);
ast_list_t* n = POOL_ALLOC(ast_list_t);
n->ast = ast;
n->name = name;
n->next = NULL;
// Find where to add name in sorted list
ast_list_t* prev = list;
for(ast_list_t* p = prev->next; p != NULL; prev = p, p = p->next)
{
assert(p->name != NULL);
if (sort && strcmp(p->name, name) > 0)
{
// Add new node before p
n->next = p;
prev->next = n;
return;
}
}
// Add new node at end of list
prev->next = n;
}
// Add the given AST to the given list, using the name from the specified
// child.
// ASTs with hygenic names are ignored.
// Leading underscores on names are ignored for sorting purposes.
// When false the allow_public parameter causes ASTs with public names to be
// ignored. allow_private does the same for private names.
static void doc_list_add_named(ast_list_t* list, ast_t* ast, size_t id_index,
bool allow_public, bool allow_private)
{
assert(list != NULL);
assert(ast != NULL);
const char* name = ast_name(ast_childidx(ast, id_index));
assert(name != NULL);
if(is_name_internal_test(name)) // Ignore internally generated names
return;
if(is_name_private(name) && !allow_private) // Ignore private
return;
if(!is_name_private(name) && !allow_public) // Ignore public
return;
if(is_name_private(name)) // Ignore leading underscore for ordering
name++;
doc_list_add(list, ast, name, false);
}
// Utilities
// Cat together the given strings into a newly allocated buffer.
// Any unneeded strings should be passed as "", not NULL.
// The returned buffer must be freed with ponyint_pool_free_size() when
// no longer needed.
// The out_buf_size parameter returns the size of the buffer (which is needed
// for freeing), not the length of the string.
static char* doc_cat(const char* a, const char* b, const char* c,
const char* d, const char* e, size_t* out_buf_size)
{
assert(a != NULL);
assert(b != NULL);
assert(c != NULL);
assert(d != NULL);
assert(e != NULL);
assert(out_buf_size != NULL);
size_t a_len = strlen(a);
size_t b_len = strlen(b);
size_t c_len = strlen(c);
size_t d_len = strlen(d);
size_t e_len = strlen(e);
size_t buf_len = a_len + b_len + c_len + d_len + e_len + 1;
char* buffer = (char*)ponyint_pool_alloc_size(buf_len);
char *p = buffer;
if(a_len > 0) { memcpy(p, a, a_len); p += a_len; }
if(b_len > 0) { memcpy(p, b, b_len); p += b_len; }
if(c_len > 0) { memcpy(p, c, c_len); p += c_len; }
if(d_len > 0) { memcpy(p, d, d_len); p += d_len; }
if(e_len > 0) { memcpy(p, e, e_len); p += e_len; }
*(p++) = '\0';
assert(p == (buffer + buf_len));
*out_buf_size = buf_len;
return buffer;
}
// Fully qualified type names (TQFNs).
// We need unique names for types, for use in file names and links. The format
// we use is:
// qualfied_package_name-type_name
// All /s in the package name are replaced with dashes.
// This may fail if there are 2 or more packages with the same qualified name.
// This is unlikely, but possible and needs to be fixed later.
// Write the TQFN for the given type to a new buffer.
// By default the type name is taken from the given AST, however this can be
// overridden by the type_name parameter. Pass NULL to use the default.
// The returned buffer must be freed using ponyint_pool_free_size when no longer
// needed. Note that the size reported is the size of the buffer and includes a
// terminator.
static char* write_tqfn(ast_t* type, const char* type_name, size_t* out_size)
{
assert(type != NULL);
assert(out_size != NULL);
ast_t* package = ast_nearest(type, TK_PACKAGE);
assert(package != NULL);
// We need the qualified package name and the type name
const char* pkg_qual_name = package_qualified_name(package);
if(type_name == NULL)
type_name = ast_name(ast_child(type));
assert(pkg_qual_name != NULL);
assert(type_name != NULL);
char* buffer = doc_cat(pkg_qual_name, "-", type_name, "", "", out_size);
// Change slashes to dashes
for(char* p = buffer; *p != '\0'; p++)
{
if(*p == '/')
*p = '-';
#ifdef PLATFORM_IS_WINDOWS
if(*p == '\\')
*p = '-';
#endif
}
return buffer;
}
// Open a file with the specified info.
// The given filename extension should include a dot if one is needed.
// The returned file handle must be fclosed() with no longer needed.
// If the specified file cannot be opened an error will be generated and NULL
// returned.
static FILE* doc_open_file(docgen_t* docgen, bool in_sub_dir,
const char* filename, const char* extn)
{
assert(docgen != NULL);
assert(filename != NULL);
assert(extn != NULL);
// Build the type file name in a buffer.
// Full file name is:
// directory/filenameextn
const char* dir = in_sub_dir ? docgen->sub_dir : docgen->base_dir;
size_t buf_len;
char* buffer = doc_cat(dir, filename, extn, "", "", &buf_len);
// Now we have the file name open the file
FILE* file = fopen(buffer, "w");
if(file == NULL)
errorf(docgen->errors, NULL,
"Could not write documentation to file %s", buffer);
ponyint_pool_free_size(buf_len, buffer);
return file;
}
// Functions to handle types
static void doc_type_list(docgen_t* docgen, ast_t* list, const char* preamble,
const char* separator, const char* postamble, bool generate_links, bool line_breaks);
// Report the human readable description for the given capability node.
// The returned string is valid forever and should not be freed.
// NULL is returned for no description.
static const char* doc_get_cap(ast_t* cap)
{
if(cap == NULL)
return NULL;
switch(ast_id(cap))
{
case TK_ISO:
case TK_TRN:
case TK_REF:
case TK_VAL:
case TK_BOX:
case TK_TAG:
case TK_CAP_READ:
case TK_CAP_SEND:
case TK_CAP_SHARE:
return ast_get_print(cap);
default:
return NULL;
}
}
// Write the given type to the current type file
static void doc_type(docgen_t* docgen, ast_t* type, bool generate_links)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(type != NULL);
switch(ast_id(type))
{
case TK_NOMINAL:
{
AST_GET_CHILDREN(type, package, id, tparams, cap, ephemeral);
// Generate links only if directed to and if the type is not anonymous (as
// indicated by a name created by package_hygienic_id).
if(generate_links && *ast_name(id) != '$')
{
// Find type we reference so we can link to it
ast_t* target = (ast_t*)ast_data(type);
assert(target != NULL);
size_t link_len;
char* tqfn = write_tqfn(target, NULL, &link_len);
// Links are of the form: [text](target)
fprintf(docgen->type_file, "[%s](%s)", ast_nice_name(id), tqfn);
ponyint_pool_free_size(link_len, tqfn);
doc_type_list(docgen, tparams, "\\[", ", ", "\\]", true, false);
}
else
{
fprintf(docgen->type_file, "%s", ast_nice_name(id));
doc_type_list(docgen, tparams, "[", ", ", "]", false, false);
}
const char* cap_text = doc_get_cap(cap);
if(cap_text != NULL)
fprintf(docgen->type_file, " %s", cap_text);
if(ast_id(ephemeral) != TK_NONE)
fprintf(docgen->type_file, "%s", ast_get_print(ephemeral));
break;
}
case TK_UNIONTYPE:
doc_type_list(docgen, type, "(", " | ", ")", generate_links, true);
break;
case TK_ISECTTYPE:
doc_type_list(docgen, type, "(", " & ", ")", generate_links, false);
break;
case TK_TUPLETYPE:
doc_type_list(docgen, type, "(", " , ", ")", generate_links, false);
break;
case TK_TYPEPARAMREF:
{
AST_GET_CHILDREN(type, id, cap, ephemeral);
fprintf(docgen->type_file, "%s", ast_nice_name(id));
const char* cap_text = doc_get_cap(cap);
if(cap_text != NULL)
fprintf(docgen->type_file, " %s", cap_text);
if(ast_id(ephemeral) != TK_NONE)
fprintf(docgen->type_file, "%s", ast_get_print(ephemeral));
break;
}
case TK_ARROW:
{
AST_GET_CHILDREN(type, left, right);
doc_type(docgen, left, generate_links);
fprintf(docgen->type_file, "->");
doc_type(docgen, right, generate_links);
break;
}
case TK_THISTYPE:
fprintf(docgen->type_file, "this");
break;
case TK_ISO:
case TK_TRN:
case TK_REF:
case TK_VAL:
case TK_BOX:
case TK_TAG:
fprintf(docgen->type_file, "%s", ast_get_print(type));
break;
default:
assert(0);
}
}
// Write the given list of types to the current type file, with the specified
// preamble, separator and psotamble text. If the list is empty nothing is
// written.
static void doc_type_list(docgen_t* docgen, ast_t* list, const char* preamble,
const char* separator, const char* postamble, bool generate_links, bool line_breaks)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(list != NULL);
assert(preamble != NULL);
assert(separator != NULL);
assert(postamble != NULL);
if(ast_id(list) == TK_NONE)
return;
fprintf(docgen->type_file, "%s", preamble);
int listItemCount = 0;
for(ast_t* p = ast_child(list); p != NULL; p = ast_sibling(p))
{
doc_type(docgen, p, generate_links);
if(ast_sibling(p) != NULL) {
fprintf(docgen->type_file, "%s", separator);
if (line_breaks) {
if (listItemCount++ == 2) {
fprintf(docgen->type_file, "\n ");
listItemCount = 0;
}
}
}
}
fprintf(docgen->type_file, "%s", postamble);
}
static bool is_for_testing(const char* name, ast_t* list)
{
assert(name != NULL);
assert(list != NULL);
if (strncmp(name, "_Test", 5) == 0) return true;
if(ast_id(list) == TK_NONE) return false;
for(ast_t* p = ast_child(list); p != NULL; p = ast_sibling(p))
{
if(ast_id(p) == TK_NOMINAL)
{
ast_t* id = ast_childidx(p, 1);
if(strcmp(ast_name(id), "TestList") == 0) return true;
if(strcmp(ast_name(id), "UnitTest") == 0) return true;
ast_t* p_def = (ast_t*)ast_data(p);
ast_t* p_list = ast_childidx(p_def, 3);
return is_for_testing(ast_name(id), p_list);
}
}
return false;
}
// Functions to handle everything else
// Write the given list of fields to the current type file.
// The given title text is used as a section header.
// If the field list is empty nothing is written.
static void doc_fields(docgen_t* docgen, ast_list_t* fields, const char* title)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(fields != NULL);
assert(title != NULL);
if(fields->next == NULL) // No fields
return;
fprintf(docgen->type_file, "## %s\n\n", title);
for(ast_list_t* p = fields->next; p != NULL; p = p->next)
{
ast_t* field = p->ast;
assert(field != NULL);
AST_GET_CHILDREN(field, id, type, init);
const char* name = ast_name(id);
assert(name != NULL);
// Don't want ast_get_print() as that will give us flet or fvar
const char* ftype = NULL;
switch(ast_id(field))
{
case TK_FVAR: ftype = "var"; break;
case TK_FLET: ftype = "let"; break;
case TK_EMBED: ftype = "embed"; break;
default: assert(0);
}
fprintf(docgen->type_file, "* %s %s: ", ftype, name);
doc_type(docgen, type, true);
fprintf(docgen->type_file, "\n\n---\n\n");
}
}
// Write the given list of type parameters to the current type file, with
// surrounding []. If the given list is empty nothing is written.
static void doc_type_params(docgen_t* docgen, ast_t* t_params,
bool generate_links)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(t_params != NULL);
if(ast_id(t_params) == TK_NONE)
return;
assert(ast_id(t_params) == TK_TYPEPARAMS);
if(generate_links)
fprintf(docgen->type_file, "\\[");
else
fprintf(docgen->type_file, "[");
ast_t* first = ast_child(t_params);
for(ast_t* t_param = first; t_param != NULL; t_param = ast_sibling(t_param))
{
if(t_param != first)
fprintf(docgen->type_file, ", ");
AST_GET_CHILDREN(t_param, id, constraint, default_type);
const char* name = ast_name(id);
assert(name != NULL);
if(ast_id(default_type) != TK_NONE)
fprintf(docgen->type_file, "optional ");
fprintf(docgen->type_file, "%s: ", name);
if(ast_id(constraint) != TK_NONE)
doc_type(docgen, constraint, generate_links);
else
fprintf(docgen->type_file, "no constraint");
}
if(generate_links)
fprintf(docgen->type_file, "\\]");
else
fprintf(docgen->type_file, "]");
}
// Write the given list of parameters to the current type file, with
// surrounding (). If the given list is empty () is still written.
static void code_block_doc_params(docgen_t* docgen, ast_t* params)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(params != NULL);
fprintf(docgen->type_file, "(");
ast_t* first = ast_child(params);
for(ast_t* param = first; param != NULL; param = ast_sibling(param))
{
if(param != first)
fprintf(docgen->type_file, ",\n");
else
fprintf(docgen->type_file, "\n");
AST_GET_CHILDREN(param, id, type, def_val);
const char* name = ast_name(id);
assert(name != NULL);
fprintf(docgen->type_file, " %s: ", name);
doc_type(docgen, type, false);
// if we have a default value, add it to the documentation
if(ast_id(def_val) != TK_NONE)
{
switch(ast_id(def_val))
{
case TK_STRING:
fprintf(docgen->type_file, "= \"%s\"", ast_get_print(def_val));
break;
default:
fprintf(docgen->type_file, " = %s", ast_get_print(def_val));
break;
}
}
}
fprintf(docgen->type_file, ")");
}
static void list_doc_params(docgen_t* docgen, ast_t* params)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(params != NULL);
ast_t* first = ast_child(params);
for(ast_t* param = first; param != NULL; param = ast_sibling(param))
{
if(param == first)
fprintf(docgen->type_file, "#### Parameters\n\n");
fprintf(docgen->type_file, "* ");
AST_GET_CHILDREN(param, id, type, def_val);
const char* name = ast_name(id);
assert(name != NULL);
fprintf(docgen->type_file, " %s: ", name);
doc_type(docgen, type, true);
// if we have a default value, add it to the documentation
if(ast_id(def_val) != TK_NONE)
{
switch(ast_id(def_val))
{
case TK_STRING:
fprintf(docgen->type_file, "= \"%s\"", ast_get_print(def_val));
break;
default:
fprintf(docgen->type_file, " = %s", ast_get_print(def_val));
break;
}
}
fprintf(docgen->type_file, "\n");
}
fprintf(docgen->type_file, "\n");
}
// Write a description of the given method to the current type file
static void doc_method(docgen_t* docgen, ast_t* method)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(method != NULL);
AST_GET_CHILDREN(method, cap, id, t_params, params, ret, error, body, doc);
const char* name = ast_name(id);
assert(name != NULL);
// Method
fprintf(docgen->type_file, "### %s", name);
doc_type_params(docgen, t_params, true);
fprintf(docgen->type_file, "\n\n");
// The docstring, if any
if(ast_id(doc) != TK_NONE)
fprintf(docgen->type_file, "%s\n\n", ast_name(doc));
// SYLVAN'S FULL CODE BLOCK HERE
fprintf(docgen->type_file, "```pony\n");
fprintf(docgen->type_file, "%s ", ast_get_print(method));
if(ast_id(method) == TK_FUN || ast_id(method) == TK_NEW)
{
const char* cap_text = doc_get_cap(cap);
if(cap_text != NULL) fprintf(docgen->type_file, "%s ", cap_text);
}
fprintf(docgen->type_file, "%s", name);
doc_type_params(docgen, t_params, false);
// parameters of the code block
code_block_doc_params(docgen, params);
// return type
if(ast_id(method) == TK_FUN || ast_id(method) == TK_NEW)
{
fprintf(docgen->type_file, "\n: ");
doc_type(docgen, ret, false);
if(ast_id(error) == TK_QUESTION)
fprintf(docgen->type_file, " ?");
}
// close the block
fprintf(docgen->type_file, "\n```\n");
// Parameters
list_doc_params(docgen, params);
// Return value
if(ast_id(method) == TK_FUN || ast_id(method) == TK_NEW)
{
fprintf(docgen->type_file, "#### Returns\n\n");
fprintf(docgen->type_file, "* ");
doc_type(docgen, ret, true);
if(ast_id(error) == TK_QUESTION)
fprintf(docgen->type_file, " ?");
fprintf(docgen->type_file, "\n\n");
}
// horizontal rule at the end
// separate us from the next method visually
fprintf(docgen->type_file, "---\n\n");
}
// Write the given list of methods to the current type file.
// The variety text is used as a heading.
// If the list is empty nothing is written.
static void doc_methods(docgen_t* docgen, ast_list_t* methods,
const char* variety)
{
assert(docgen != NULL);
assert(docgen->type_file != NULL);
assert(methods != NULL);
assert(variety != NULL);
if(methods->next == NULL)
return;
fprintf(docgen->type_file, "## %s\n\n", variety);
for(ast_list_t* p = methods->next; p != NULL; p = p->next)
doc_method(docgen, p->ast);
}
// Write a description of the given entity to its own type file.
static void doc_entity(docgen_t* docgen, ast_t* ast)
{
assert(docgen != NULL);
assert(docgen->index_file != NULL);
assert(docgen->package_file != NULL);
assert(docgen->test_types != NULL);
assert(docgen->public_types != NULL);
assert(docgen->private_types != NULL);
assert(docgen->type_file == NULL);
assert(ast != NULL);
// First open a file
size_t tqfn_len;
char* tqfn = write_tqfn(ast, NULL, &tqfn_len);
docgen->type_file = doc_open_file(docgen, true, tqfn, ".md");
if(docgen->type_file == NULL)
return;
// Add reference to new file to index file
AST_GET_CHILDREN(ast, id, tparams, cap, provides, members, c_api, doc);
const char* name = ast_name(id);
assert(name != NULL);
fprintf(docgen->index_file, " - %s %s: \"%s.md\"\n",
ast_get_print(ast), name, tqfn);
// Add to appropriate package types buffer
printbuf_t* buffer = docgen->public_types;
if(is_for_testing(name, provides)) buffer = docgen->test_types;
else if(name[0] == '_') buffer = docgen->private_types;
printbuf(buffer,
"* [%s %s](%s.md)\n",
ast_get_print(ast), name, tqfn);
ponyint_pool_free_size(tqfn_len, tqfn);
// Now we can write the actual documentation for the entity
fprintf(docgen->type_file, "# %s", name);
doc_type_params(docgen, tparams, true);
fprintf(docgen->type_file, "\n\n");
if(ast_id(doc) != TK_NONE)
fprintf(docgen->type_file, "%s\n\n", ast_name(doc));
// code block
fprintf(docgen->type_file, "```pony\n");
fprintf(docgen->type_file, "%s ",ast_get_print(ast));
const char* cap_text = doc_get_cap(cap);
if(cap_text != NULL) fprintf(docgen->type_file, "%s ", cap_text);
fprintf(docgen->type_file, "%s", name);
doc_type_params(docgen, tparams, false);
doc_type_list(docgen, provides, " is\n ", ",\n ", "", false, false);
fprintf(docgen->type_file, "\n```\n\n");
if (ast_id(ast) != TK_TYPE)
doc_type_list(docgen, provides,
"#### Implements\n\n* ", "\n* ", "\n\n---\n\n", true, false);
else
doc_type_list(docgen, provides,
"#### Type Alias For\n\n* ", "\n* ", "\n\n---\n\n", true, false);
// Sort members into varieties
ast_list_t pub_fields = { NULL, NULL, NULL };
ast_list_t news = { NULL, NULL, NULL };
ast_list_t pub_bes = { NULL, NULL, NULL };
ast_list_t priv_bes = { NULL, NULL, NULL };
ast_list_t pub_funs = { NULL, NULL, NULL };
ast_list_t priv_funs = { NULL, NULL, NULL };
for(ast_t* p = ast_child(members); p != NULL; p = ast_sibling(p))
{
switch(ast_id(p))
{
case TK_FVAR:
case TK_FLET:
case TK_EMBED:
doc_list_add_named(&pub_fields, p, 0, true, false);
break;
case TK_NEW:
doc_list_add_named(&news, p, 1, true, true);
break;
case TK_BE:
doc_list_add_named(&pub_bes, p, 1, true, false);
doc_list_add_named(&priv_bes, p, 1, false, true);
break;
case TK_FUN:
doc_list_add_named(&pub_funs, p, 1, true, false);
doc_list_add_named(&priv_funs, p, 1, false, true);
break;
default:
assert(0);
break;
}
}
// Handle member variety lists
doc_methods(docgen, &news, "Constructors");
doc_fields(docgen, &pub_fields, "Public fields");
doc_methods(docgen, &pub_bes, "Public Behaviours");
doc_methods(docgen, &pub_funs, "Public Functions");
doc_methods(docgen, &priv_bes, "Private Behaviours");
doc_methods(docgen, &priv_funs, "Private Functions");
doc_list_free(&pub_fields);
doc_list_free(&news);
doc_list_free(&pub_bes);
doc_list_free(&priv_bes);
doc_list_free(&pub_funs);
doc_list_free(&priv_funs);
fclose(docgen->type_file);
docgen->type_file = NULL;
}
// Write the given package home page to its own file
static void doc_package_home(docgen_t* docgen,
ast_t* package,
ast_t* doc_string)
{
assert(docgen != NULL);
assert(docgen->index_file != NULL);
assert(docgen->home_file != NULL);
assert(docgen->package_file == NULL);
assert(docgen->test_types == NULL);
assert(docgen->public_types == NULL);
assert(docgen->private_types == NULL);
assert(docgen->type_file == NULL);
assert(package != NULL);
assert(ast_id(package) == TK_PACKAGE);
// First open a file
size_t tqfn_len;
char* tqfn = write_tqfn(package, "-index", &tqfn_len);
// Package group
fprintf(docgen->index_file, "- package %s:\n",
package_qualified_name(package));
docgen->type_file = doc_open_file(docgen, true, tqfn, ".md");
if(docgen->type_file == NULL)
return;
// Add reference to new file to index file
fprintf(docgen->index_file, " - Package: \"%s.md\"\n", tqfn);
// Add reference to package to home file
fprintf(docgen->home_file, "* [%s](%s)\n", package_qualified_name(package),
tqfn);
// Now we can write the actual documentation for the package
if(doc_string != NULL)
{
assert(ast_id(doc_string) == TK_STRING);
fprintf(docgen->type_file, "%s", ast_name(doc_string));
}
else
{
fprintf(docgen->type_file, "No package doc string provided for %s.",
package_qualified_name(package));
}
ponyint_pool_free_size(tqfn_len, tqfn);
docgen->test_types = printbuf_new();
docgen->public_types = printbuf_new();
docgen->private_types = printbuf_new();
docgen->package_file = docgen->type_file;
docgen->type_file = NULL;
}
// Document the given package
static void doc_package(docgen_t* docgen, ast_t* ast)
{
assert(ast != NULL);
assert(ast_id(ast) == TK_PACKAGE);
assert(docgen->package_file == NULL);
assert(docgen->test_types == NULL);
assert(docgen->public_types == NULL);
assert(docgen->private_types == NULL);
ast_list_t types = { NULL, NULL, NULL };
ast_t* package_doc = NULL;
// Find and sort package contents
for(ast_t* m = ast_child(ast); m != NULL; m = ast_sibling(m))
{
if(ast_id(m) == TK_STRING)
{
// Package docstring
assert(package_doc == NULL);
package_doc = m;
}
else
{
assert(ast_id(m) == TK_MODULE);
for(ast_t* t = ast_child(m); t != NULL; t = ast_sibling(t))
{
if(ast_id(t) != TK_USE)
{
assert(ast_id(t) == TK_TYPE || ast_id(t) == TK_INTERFACE ||
ast_id(t) == TK_TRAIT || ast_id(t) == TK_PRIMITIVE ||
ast_id(t) == TK_STRUCT || ast_id(t) == TK_CLASS ||
ast_id(t) == TK_ACTOR);
// We have a type
doc_list_add_named(&types, t, 0, true, true);
}
}
}
}
doc_package_home(docgen, ast, package_doc);
// Process types
for(ast_list_t* p = types.next; p != NULL; p = p->next)
doc_entity(docgen, p->ast);
// Add listing of subpackages and links
if(docgen->public_types->offset > 0)
{
fprintf(docgen->package_file, "\n\n## Public Types\n\n");
fprintf(docgen->package_file, "%s", docgen->public_types->m);
}
if(docgen->private_types->offset > 0)
{
fprintf(docgen->package_file, "\n\n## Private Types\n\n");
fprintf(docgen->package_file, "%s", docgen->private_types->m);
}
if(docgen->test_types->offset > 0)
{
fprintf(docgen->package_file, "\n\n## Test Types\n\n");
fprintf(docgen->package_file, "%s", docgen->test_types->m);
}
fclose(docgen->package_file);
docgen->package_file = NULL;
printbuf_free(docgen->test_types);
printbuf_free(docgen->public_types);
printbuf_free(docgen->private_types);
docgen->test_types = NULL;
docgen->public_types = NULL;
docgen->private_types = NULL;
}
// Document the packages in the given program
static void doc_packages(docgen_t* docgen, ast_t* ast)
{
assert(ast != NULL);
assert(ast_id(ast) == TK_PROGRAM);
// The Main package appears first, other packages in alphabetical order
ast_t* package_1 = ast_child(ast);
assert(package_1 != NULL);
ast_list_t packages = { NULL, NULL, NULL };