-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_reference
executable file
·247 lines (200 loc) · 4.2 KB
/
mk_reference
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
#! /usr/bin/perl
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
sub word_sort;
sub process_comment;
sub xref;
# vocabulary definitions
my $prim = $ARGV[0];
# C source
my $source = $ARGV[1];
# doc template
my $doc = $ARGV[2];
# generated doc
my $dst = $ARGV[3];
my $vocab;
my $groups;
open my $f, $prim or die "$prim: $!\n";
while(<$f>) {
next if /^\s*(#|$)/;
my @i = split /\s+/;
my $name = $i[1] ? $i[1] : $i[0];
$vocab->{$name}{name} = $name;
$vocab->{$name}{str} = $i[0];
}
close $f;
open my $f, $source or die "$source: $!\n";
my $comm;
while(<$f>) {
if(m#^// ?(.*?)$#) {
my $c = $1;
push @$comm, $c unless $c =~ /^- - -/;
next;
}
if(/^void gfx_prim_([^\(]+)/ && exists $vocab->{$1}) {
$vocab->{$1}{doc} = $comm;
undef $comm;
next;
}
if(/^\}/) {
undef $comm;
next;
}
}
close $f;
process_comment $vocab->{$_} for keys %$vocab;
my $doc_template;
open my $f, $doc or die "$doc: $!\n";
{
local $/;
$doc_template = <$f>;
}
close $f;
my $all_docs;
for (sort { word_sort } keys %$vocab) {
next if @{$vocab->{$_}{doc}} == 0;
my $str = $vocab->{$_}{doc_str};
$str = xref $str, $_;
$all_docs .= $str;
$all_docs .= "\n";
}
$doc_template =~ s/PRIMITIVE_WORD_LIST/$all_docs/;
open my $f, ">", $dst or die "$dst: $!\n";
print $f $doc_template;
close $f;
my $undoc;
for (sort { word_sort } keys %$vocab) {
next if @{$vocab->{$_}{doc}} != 0;
push @$undoc, $vocab->{$_}{name};
}
print "undocumented words: ", join(", ", @$undoc), "\n" if $undoc;
# print Dumper $vocab;
sub process_comment
{
my $d = $_[0];
if($d->{doc}) {
while(@{$d->{doc}} >= 1) {
if($d->{doc}[-1] eq "") {
pop @{$d->{doc}}
}
else {
last;
}
}
}
my $vars;
for my $c (@{$d->{doc}}) {
if($c =~ /^\(.*--.*\)/) {
while($c =~ m/([a-z0-9_]+)/g) {
my $v1 = $1;
my $v2 = $v1;
$v2 =~ s/_(\S+)/~$1~/;
$vars->{$v1} = "__${v2}__";
}
}
}
my $all_vars = join '|', keys %$vars;
my $doc;
my $new_para;
my $example;
my $cnt = 0;
my $item_start;
my $notes;
for my $c (@{$d->{doc}}) {
$cnt++;
if($cnt == 1) {
$doc = "* **+$d->{str}+** - $c [[$d->{name}]]\n";
next;
}
if($example) {
$doc .= "$c\n";
next;
}
if($c eq "") {
$doc .= "+\n" unless $new_para;
$new_para = 1;
next;
}
if($c =~ /^groups?:\s*(\S+)/i) {
for my $g (split /,/, $1) {
$groups->{$g}{$d->{name}} = 1;
$d->{groups}{$g} = 1;
}
next;
}
if($c =~ /^examples?:/i) {
if($item_start) {
$doc .= "--\n+\n";
$item_start = 0;
}
$doc .= ".Examples\n```\n";
$example = 1;
next;
}
$new_para = 0;
my $x = $c;
if($c =~ /^\(.*--.*\)/) {
$x = "** $x";
$x = "--\n$x" if !$item_start;
$item_start = 1;
}
elsif(
$item_start &&
$c ne "" &&
(!($c =~ /^($all_vars):/ || ($c =~ /^($all_vars)\b/ && $c =~ /\b($all_vars)\b/)) || $all_vars eq "")
) {
$x = "XXX--\n$x";
$item_start = 0;
$notes = 1;
}
else {
$x = "+\n$x" unless $notes;
}
for my $v (keys %$vars) {
$x =~ s/\b$v\b/$vars->{$v}/g;
}
$doc .= "$x\n";
}
$doc .= "--\n" if $item_start;
$doc =~ s/\+\nXXX--\n/--\n+\n/;
$doc .= "```\n" if $example;
if($d->{groups}) {
# $doc .= "+\nSee also: XXX_XREF\n";
$doc .= "XXX_XREF";
}
$doc =~ s/\+\n\+\n/+\n/g;
$d->{doc_str} = $doc;
}
sub xref
{
my $doc = $_[0];
my $name = $_[1];
my $refs;
if($vocab->{$name}{groups}) {
for my $g (keys %{$vocab->{$name}{groups}}) {
for my $n (keys %{$groups->{$g}}) {
$refs->{$n} = "xref:$n\[+$vocab->{$n}{str}+\]";
}
}
delete $refs->{$name};
my $r = join ", ", map { $refs->{$_} } sort { word_sort } keys %$refs;
if($r ne "") {
$doc =~ s/XXX_XREF/+\nSee also: $r\n/;
}
else {
$doc =~ s/XXX_XREF//;
}
}
return $doc;
}
sub word_sort
{
my $x = $vocab->{$a}{str};
my $y = $vocab->{$b}{str};
my $x = $x eq '{' || $x eq '}' ? " $x" : $x;
my $y = $y eq '{' || $y eq '}' ? " $y" : $y;
return $x cmp $y;
}