forked from lep/jassdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotate
executable file
·228 lines (181 loc) · 5.27 KB
/
annotate
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
#!/usr/bin/env perl
use utf8;
use v5.30.0;
use strict;
use warnings;
use open ':std', ':utf8', ':encoding(UTF-8)';
# no feature qw(indirect);
use feature qw(signatures);
no warnings qw(experimental::signatures);
use Getopt::Long;
use Pod::Usage;
use DBI;
use DBD::SQLite::Constants ':dbd_sqlite_string_mode';
my $dbPath = "jass.db";
my $help = 0;
GetOptions(
"db=s" => \$dbPath,
help => \$help
) or die pod2usage(-verbose => 1);
pod2usage(-verbose => 2) if $help;
pod2usage(-verbose => 1) unless @ARGV;
my @files = @ARGV;
my $dbh = DBI->connect("DBI:SQLite:$dbPath");
$dbh->{sqlite_string_mode} = DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK;
my @auto_generated_annotations = ("source-file", "return-type", "common-ai",
"source-code", "start-line", "end-line", "type" );
my $auto_generated_annotations = join ", ", map { "'$_'" } @auto_generated_annotations;
sub fetch_annotations {
my $name = shift;
my $ret = "";
my $stm = $dbh->prepare("
select anname, value
from annotations
where fnname = ? and anname not in ('comment', $auto_generated_annotations)
order by _rowid_
");
$stm->execute($name);
while ( my $row = $stm->fetchrow_hashref() ) {
$ret .= join "", "@", $row->{anname}, " ", $row->{value};
$ret .= "\n";
}
return $ret;
}
sub fetch_comment {
my $name = shift;
my $stm = $dbh->prepare("
select value
from annotations
where fnname = ? and anname = 'comment'
");
$stm->execute($name);
if(my @row = $stm->fetchrow_array() ){
return $row[0];
}
return "";
}
sub fetch_params {
my $name = shift;
my $ret = "";
my $stm = $dbh->prepare("
select doc.param, doc.value
from (
select value, param
from params_extra
where anname = 'param_order'
and fnname = ?
) as ord
left outer join (
select param, value
from parameters
where fnname = ?
) as doc on doc.param = ord.param
order by ord.value
");
$stm->execute($name, $name);
while( my $row = $stm->fetchrow_hashref() ){
next if !$row->{param} or !$row->{value};
$ret .= join " ", '@param', $row->{param}, $row->{value};
$ret .= "\n";
}
return $ret;
}
sub has_docstring {
my $name = shift;
my $stm = $dbh->prepare("
select 1
from annotations
where fnname == ?
and anname not in ($auto_generated_annotations)
limit 1
");
$stm->execute( $name );
my @row = $stm->fetchrow_array();
return scalar @row;
}
sub recreate_docstring {
return "" unless has_docstring ($1);
return join "\n",
"",
"/**",
fetch_comment ($1),
fetch_annotations ($1),
"*/\n";
}
sub recreate_docstring_function {
return "" unless has_docstring $1;
return join "\n",
"",
"/**",
fetch_comment ($1),
fetch_params ($1),
fetch_annotations ($1),
"*/\n";
}
for my $file (@files) {
say STDERR "Annotating $file";
open my $fh, "<", $file or die "Failed to open '$file'";
open my $out, ">", "$file-out" or die "Failed to open '$file-out'";
# TODO: create a "jass-parser" package since this is just copied from mksrc
my @state = ("nothing");
my $src = "";
my $fn = "";
my $start = 0;
my $end = 0;
while (my $line = <$fh>) {
if( $state[0] eq "nothing" && $line =~ /^(?:constant\s+)?function\s+(\w+)/){
unshift @state, "function";
print $out recreate_docstring_function $1;
}elsif($state[0] eq "function" && $line =~ /^endfunction/){
shift @state;
}elsif( $state[0] eq "nothing" && $line =~ /^(?:constant\s+)?native\s+(\w+)/ ){
print $out recreate_docstring_function $1;
}elsif ( $state[0] eq "function"){
# do nothing
}elsif ( $state[0] eq "nothing" && $line =~ m/^\s*globals/){
unshift @state, "globals";
}elsif( $line =~ m/^\/\*\*/ ){
unshift @state, "docstring";
# dont print
next;
}elsif( $state[0] eq "docstring" && $line =~ m(^\*/) ){
shift @state;
# dont print
next;
}elsif( $state[0] eq "docstring" ){
# dont print
next;
}elsif( $state[0] eq "globals" && $line =~ m/^\s*endglobals/){
shift @state;
}elsif ( $state[0] eq "globals" ){
my $name;
if( $line =~ /^\s*constant\s+\w+\s+(\w+)\s*=.+$/ ){
$name = $1;
}elsif( $line =~ /^\s*\w+\s+array\s+(\w+)/ ){
$name = $1;
}elsif( $line =~ /^\s*\w+\s+(\w+)/ ){
$name = $1;
}
print $out recreate_docstring $name if $name;
}elsif( $state[0] eq "nothing" && $line =~ m/^type\s+(\w+)\s+extends\s+.+/){
print $out recreate_docstring $1;
}
print $out $line;
}
}
__END__
=head1 NAME
annotate - Annotates a clean jass-file with annotations from jass.db
=head1 SYNOPSIS
annotate [options] [FILES]
Options:
--db Path to jass.db. Default: jass.db
--help Prints this help message
=head1 DESCRIPTION
B<This program> can be used to annotate a fresh jass file with annotations
allready present in a jass.db-Sqlite file. The workflow in case of a new
common.j or Blizzard.j would be to build the jass.db with the old version
and then running `annotate new-common.j new-Blizzard.j`. This would create
two files called `new-common.j-out` and `new-Blizzard.j-out`.
Another use-case for this is to clean an annotated file. If the database has
not matching annotations the input file will be stripped of all docstrings.