-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkythe.pl
294 lines (234 loc) · 7.21 KB
/
kythe.pl
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
#!/usr/bin/perl
use v5.18;
use warnings;
# this needs to be my fork of PPI
# see https://github.com/adamkennedy/PPI/pull/186
use PPI;
use PPI::Xref;
use Git::Repository;
use JSON::XS qw(encode_json);
use Digest::SHA1 qw(sha1_hex);
use File::Basename qw(basename);
use MIME::Base64 qw(encode_base64);
my($path_to_repo, $file_in_repo) = @ARGV;
my $reponame = basename($path_to_repo);
my $xref = PPI::Xref->new({
INC => ["$path_to_repo/lib", @INC],
});
$xref->process($file_in_repo);
my %_cache;
my $sfi = $xref->subs_files_iter;
while(my $sf = $sfi->next) {
my($fqsubname, $source_file) = $sf->array;
my @components = split(/::/, $fqsubname);
my $relsubname = pop(@components);
my $pkg = join("::", @components);
my $vname_of_package = package_($pkg);
# TODO make sure PPI::Xref and PPI share a cache
my $doc = $_cache{$source_file} //= PPI::Document->new(
$source_file,
readonly => 1,
) or next;
my $subs = $doc->find("PPI::Statement::Sub") or next;
for my $sub (@$subs) {
my $child = $sub->schild(1);
# this is wrong in multiple ways but oh well
if(
not $child->isa("PPI::Token::Word")
or (
$sub->name ne $fqsubname
and $sub->name ne $relsubname
)
)
{
next;
}
my $span = $child->byte_span or next;
my $vname_of_source = file($source_file);
my $vname_of_sub = sub_($vname_of_source, $fqsubname, $relsubname);
my $vname_of_anchor = anchor($vname_of_source, $span);
edge($vname_of_sub, "childof", $vname_of_package);
edge($vname_of_sub, "defines", $vname_of_sub); # XXX defines/binding?
}
}
my $ifi = $xref->incs_files_iter;
while(my $if = $ifi->next) {
my(
$source_file,
$linenumber,
$target_file,
$inlcude_type,
$include_target,
) = $if->array;
my $vname_of_source = file($source_file);
my $vname_of_target = file($target_file);
#vcs($source_file);
#vcs($target_file);
# need to re-parse to get access to tokens
# XXX expose PPI documents from PPI::Xref?
# TODO make sure PPI::Xref and PPI share a cache
my $doc = $_cache{$source_file} //= PPI::Document->new(
$source_file,
readonly => 1,
) or next;
my $incs = $doc->find("PPI::Statement::Include") or next;
for my $inc (@$incs) {
my $child = $inc->schild(1) or next;
if(
not $child->isa("PPI::Token::Word")
or $child->content ne $include_target
)
{
next;
}
my $span = $child->byte_span or next;
my $vname_of_anchor = anchor($vname_of_source, $span);
# ref/includes is defined as "inlined text" but whatevs
edge($vname_of_anchor, "ref/includes", $vname_of_target);
#edge($vname_of_anchor, "ref", $vname_of_target);
}
}
# TODO synthesise 'main' package for scripts
my $pfi = $xref->packages_files_iter;
while(my $pf = $pfi->next) {
my(
$pkg,
$source_file,
) = $pf->array;
my $doc = $_cache{$source_file} //= PPI::Document->new(
$source_file,
readonly => 1,
) or next;
my $vname_of_source = file($source_file);
my $pkgs = $doc->find("PPI::Statement::Package") or next;
for my $pkg_stmt (@$pkgs) {
my $vname_of_package = package_($pkg);
edge($vname_of_package, "childof", $vname_of_source);
my $child = $pkg_stmt->schild(1);
if(
not $child->isa("PPI::Token::Word")
or $pkg ne $pkg_stmt->namespace
)
{
next;
}
# FIXME the spec says that class definitions span their entire body
my $span = $child->byte_span or next;
my $vname_of_anchor = anchor($vname_of_source, $span);
edge($vname_of_anchor, "ref", $vname_of_package);
# not sure this is picked up by web ui
edge($vname_of_anchor, "defines", $vname_of_package);
}
}
sub anchor {
my($vname_of_file, $span) = @_;
my($start, $end) = @$span;
$end++;
my $vname_of_anchor = {
corpus => $vname_of_file->{corpus},
path => $vname_of_file->{path},
language => "perl5",
signature => "$vname_of_file->{path}#$start,$end",
root => $vname_of_file->{root},
};
# these are the basic minimum to produce a decoration
fact($vname_of_anchor, "node/kind", "anchor");
fact($vname_of_anchor, "loc/start", $start);
fact($vname_of_anchor, "loc/end", $end);
# this is optional, I think
edge($vname_of_anchor, "childof", $vname_of_file);
$vname_of_anchor;
}
sub package_ {
my($pkg) = @_;
my $vname_of_package = {
corpus => $reponame,
language => "perl5",
signature => sha1_hex($pkg),
};
my $vname_of_name = {
corpus => $reponame,
language => "perl5",
signature => "package#$pkg",
};
fact($vname_of_package, "node/kind", "package");
fact($vname_of_name, "node/kind", "name");
edge($vname_of_package, "named", $vname_of_name);
$vname_of_package;
}
sub file {
my($file) = @_;
my $vname_of_file = {
corpus => $reponame,
path => $file =~ s/$path_to_repo//r,
language => "perl5",
};
my $vname_of_name = {
corpus => $reponame,
language => "perl5",
signature => "file#$file",
};
fact($vname_of_file, "node/kind", "file");
fact($vname_of_file, "text", slurp($file));
edge($vname_of_file, "named", $vname_of_name);
$vname_of_file;
}
# TODO import entire history?
sub vcs {
my($vname_of_file) = @_;
state $git = Git::Repository(git_dir => $path_to_repo);
state $rev = $git->run("rev-parse", "HEAD");
my $vname_of_vcs = {
corpus => $vname_of_file->{corpus},
signature => "revision#$rev",
language => "perl5",
};
# XXX need a file -> revision edge?
fact($vname_of_vcs, "node/kind", "vcs");
fact($vname_of_vcs, "vcs/id", $rev);
fact($vname_of_vcs, "vcs/type", "git");
fact($vname_of_vcs, "vcs/uri", "git:/path/$vname_of_file->{corpus}"); # FIXME
$vname_of_vcs;
}
sub sub_ {
my($vname_of_file, $fqname, $relname) = @_;
my $vname = {
corpus => $reponame,
#path => , # ???
signature => "function#".sha1_hex($vname_of_file->{path}.$fqname),
language => "perl5",
};
fact($vname, "node/kind", "function");
fact($vname, "complete", "complete");
if($relname eq "new") {
fact($vname, "subkind", "constructor");
}
elsif($relname eq "DESTROY") {
fact($vname, "subkind", "destructor");
}
$vname;
}
sub edge {
my($from, $what, $to) = @_;
say encode_json({
source => $from,
target => $to,
edge_kind => "/kythe/edge/$what",
fact_name => "/",
fact_value => "",
});
}
sub fact {
my($vname, $name, $value) = @_;
say encode_json({
source => $vname,
fact_name => "/kythe/$name",
fact_value => encode_base64($value, ""),
});
}
sub slurp {
my($file) = @_;
open(my $fh, "<", $file) or warn("$file: $!") and return "";
local $/;
<$fh>;
}