-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmksrc
executable file
·226 lines (182 loc) · 5.92 KB
/
mksrc
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
#!/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;
my $help = 0;
GetOptions(
help => \$help
) or die pod2usage(-verbose => 1);
pod2usage(-verbose => 2) if $help;
pod2usage(-verbose => 1) unless @ARGV;
my @files = @ARGV;
sub escape {
my ($in) = @_;
$in =~ s/'/''/g;
return $in;
}
foreach my $file (@files){
open(my $fh, "<", $file);
my $state = "nothing";
my $src = "";
my $fn = "";
my $start = 0;
my $end = 0;
my $prev_state;
say "BEGIN TRANSACTION;";
while (my $line = <$fh>){
if( $state eq "nothing" && $line =~ /^(?:constant\s+)?function\s+(\w+)/){
$start = $.;
$state = "function";
$fn = $1;
$src = $line;
}elsif($state eq "function" && $line =~ /^endfunction/){
$end = $.;
chomp $line;
$state = "nothing";
$src .= $line;
$src = escape($src);
$fn = escape($fn);
say <<SQL;
insert into annotations (fnname, anname, value) values (
'$fn',
'source-code',
'$src'
);
insert into annotations (fnname, anname, value) values (
'$fn',
'start-line',
$start
);
insert into annotations (fnname, anname, value) values (
'$fn',
'end-line',
$end
);
insert into annotations (fnname, anname, value) values (
'$fn',
'type',
'function'
);
SQL
}elsif( ( $state eq "nothing" || $state eq "globals") && $line =~ m[^/\*\*] ){
$prev_state = $state;
$state = "docstring";
}elsif( $state eq "docstring" && $prev_state && $line =~ m[^\*/] ){
$state = $prev_state;
$prev_state = undef;
}elsif( $state eq "nothing" && $line =~ /^(?:constant\s+)?native\s+(\w+)/ ){
chomp $line;
$src = $line;
$src = escape($src);
$fn = escape($1);
say <<SQL;
insert into annotations (fnname, anname, value) values (
'$fn',
'source-code',
'$src'
);
insert into annotations (fnname, anname, value) values (
'$fn',
'start-line',
$.
);
insert into annotations (fnname, anname, value) values (
'$fn',
'end-line',
$.
);
insert into annotations (fnname, anname, value) values (
'$fn',
'type',
'native'
);
SQL
}elsif ( $state eq "function"){
$src .= $line;
}elsif ( $state eq "nothing" && $line =~ m/^\s*globals/){
$state = "globals";
}elsif( $state eq "globals" && $line =~ m/^\s*endglobals/){
$state = "nothing";
}elsif ( $state eq "globals" ){
chomp $line;
my $name;
given($line){
$name = $1 when /^\s*constant\s+\w+\s+(\w+)\s*=.+$/;
$name = $1 when /^\s*\w+\s+array\s+(\w+)/;
$name = $1 when /^\s*\w+\s+(\w+)/;
}
if($name){
$name = escape($name);
$line = escape($line);
say <<SQL;
insert into annotations (fnname, anname, value) values (
'$name',
'source-code',
'$line'
);
insert into annotations (fnname, anname, value) values (
'$name',
'start-line',
$.
);
insert into annotations (fnname, anname, value) values (
'$name',
'end-line',
$.
);
insert into annotations (fnname, anname, value) values (
'$name',
'type',
'global'
);
SQL
}
}elsif( $state eq "nothing" && $line =~m /^type\s+(\w+)\s+extends\s+.+/){
chomp $line;
my $type = escape($1);
my $line = escape($line);
say <<SQL;
insert into annotations (fnname, anname, value) values (
'$type',
'source-code',
'$line'
);
insert into annotations (fnname, anname, value) values (
'$type',
'start-line',
$.
);
insert into annotations (fnname, anname, value) values (
'$type',
'end-line',
$.
);
insert into annotations (fnname, anname, value) values (
'$type',
'type',
'type'
);
SQL
}
}
say "END TRANSACTION;";
}
__END__
=head1 NAME
mksrc - Write jass source to SQL
=head1 SYNOPSIS
mksrc [options] [FILES]
Options:
--help Prints this help message
=head1 DESCRIPTION
B<This program> takes any number of jass files and writes an SQL program to
stdout. That SQL program will add a bunch of annotations to the annotations
table, like the literal source code of each toplevel defintion, the start- and
end line numbers and the kind of the definition (funciton, native, global, etc.).