line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Function::Interface; |
2
|
|
|
|
|
|
|
|
3
|
10
|
|
|
10
|
|
1297008
|
use v5.14.0; |
|
10
|
|
|
|
|
59
|
|
4
|
10
|
|
|
10
|
|
50
|
use warnings; |
|
10
|
|
|
|
|
17
|
|
|
10
|
|
|
|
|
411
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.04"; |
7
|
|
|
|
|
|
|
|
8
|
10
|
|
|
10
|
|
52
|
use Carp qw(croak); |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
493
|
|
9
|
10
|
|
|
10
|
|
3907
|
use Keyword::Simple; |
|
10
|
|
|
|
|
21955
|
|
|
10
|
|
|
|
|
302
|
|
10
|
10
|
|
|
10
|
|
6366
|
use PPR; |
|
10
|
|
|
|
|
348902
|
|
|
10
|
|
|
|
|
438
|
|
11
|
|
|
|
|
|
|
|
12
|
10
|
|
|
10
|
|
4554
|
use Function::Interface::Info; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
306
|
|
13
|
10
|
|
|
10
|
|
3600
|
use Function::Interface::Info::Function; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
287
|
|
14
|
10
|
|
|
10
|
|
3606
|
use Function::Interface::Info::Function::Param; |
|
10
|
|
|
|
|
24
|
|
|
10
|
|
|
|
|
288
|
|
15
|
10
|
|
|
10
|
|
3547
|
use Function::Interface::Info::Function::ReturnParam; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
9434
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub import { |
18
|
23
|
|
|
23
|
|
3616
|
my $class = shift; |
19
|
23
|
|
|
|
|
73
|
my %args = @_; |
20
|
|
|
|
|
|
|
|
21
|
23
|
100
|
|
|
|
144
|
my $pkg = $args{pkg} ? $args{pkg} : caller; |
22
|
|
|
|
|
|
|
|
23
|
23
|
|
|
|
|
83
|
Keyword::Simple::define 'fun' => _define_interface($pkg, 'fun'); |
24
|
23
|
|
|
|
|
686
|
Keyword::Simple::define 'method' => _define_interface($pkg, 'method'); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub unimport { |
28
|
1
|
|
|
1
|
|
97
|
Keyword::Simple::undefine 'fun'; |
29
|
1
|
|
|
|
|
20
|
Keyword::Simple::undefine 'method'; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _define_interface { |
33
|
46
|
|
|
46
|
|
104
|
my ($pkg, $keyword) = @_; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
return sub { |
36
|
44
|
|
|
44
|
|
140659
|
my $ref = shift; |
37
|
|
|
|
|
|
|
|
38
|
44
|
|
|
|
|
179
|
my $match = _assert_valid_interface($$ref); |
39
|
31
|
|
|
|
|
176
|
my $src = _render_src($pkg, $keyword, $match); |
40
|
|
|
|
|
|
|
|
41
|
31
|
|
|
|
|
3023
|
substr($$ref, 0, length $match->{statement}) = $src; |
42
|
|
|
|
|
|
|
} |
43
|
46
|
|
|
|
|
327
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _render_src { |
46
|
31
|
|
|
31
|
|
106
|
my ($pkg, $keyword, $match) = @_; |
47
|
|
|
|
|
|
|
|
48
|
31
|
|
|
|
|
210
|
my $src = <<"```"; |
49
|
|
|
|
|
|
|
BEGIN { |
50
|
|
|
|
|
|
|
Function::Interface::_register_info({ |
51
|
|
|
|
|
|
|
package => '$pkg', |
52
|
|
|
|
|
|
|
keyword => '$keyword', |
53
|
|
|
|
|
|
|
subname => '$match->{subname}', |
54
|
|
|
|
|
|
|
params => [ @{[ join ',', map { |
55
|
7
|
100
|
|
|
|
28
|
my $named = $_->{named} ? 1 : 0; |
56
|
7
|
100
|
|
|
|
17
|
my $optional = $_->{optional} ? 1 : 0; |
57
|
7
|
|
|
|
|
58
|
qq!{ type => $_->{type}, name => '$_->{name}', named => $named, optional => $optional }! |
58
|
31
|
|
|
|
|
66
|
} @{$match->{params}} ]} ], |
|
31
|
|
|
|
|
179
|
|
59
|
31
|
|
|
|
|
67
|
return => [ @{[ join ',', @{$match->{return}}] } ], |
|
31
|
|
|
|
|
128
|
|
60
|
|
|
|
|
|
|
}); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
``` |
63
|
31
|
|
|
|
|
99
|
return $src; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
our %metadata; |
67
|
|
|
|
|
|
|
sub _register_info { |
68
|
31
|
|
|
31
|
|
383
|
my ($args) = @_; |
69
|
|
|
|
|
|
|
|
70
|
31
|
|
|
|
|
6794
|
push @{$metadata{$args->{package}}} => +{ |
71
|
|
|
|
|
|
|
subname => $args->{subname}, |
72
|
|
|
|
|
|
|
keyword => $args->{keyword}, |
73
|
|
|
|
|
|
|
params => $args->{params}, |
74
|
|
|
|
|
|
|
return => $args->{return}, |
75
|
31
|
|
|
|
|
60
|
}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub info { |
79
|
30
|
|
|
30
|
1
|
21093
|
my ($interface_package) = @_; |
80
|
30
|
100
|
|
|
|
118
|
my $info = $metadata{$interface_package} or return undef; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Function::Interface::Info->new( |
83
|
|
|
|
|
|
|
package => $interface_package, |
84
|
|
|
|
|
|
|
functions => [ map { |
85
|
|
|
|
|
|
|
Function::Interface::Info::Function->new( |
86
|
|
|
|
|
|
|
subname => $_->{subname}, |
87
|
|
|
|
|
|
|
keyword => $_->{keyword}, |
88
|
55
|
|
|
|
|
69
|
params => [ map { _make_function_param($_) } @{$_->{params}} ], |
|
151
|
|
|
|
|
251
|
|
89
|
151
|
|
|
|
|
296
|
return => [ map { _make_function_return_param($_) } @{$_->{return}} ], |
|
28
|
|
|
|
|
39
|
|
|
151
|
|
|
|
|
421
|
|
90
|
|
|
|
|
|
|
) |
91
|
28
|
|
|
|
|
54
|
} @{$info}], |
|
28
|
|
|
|
|
72
|
|
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub _make_function_param { |
96
|
55
|
|
|
55
|
|
64
|
my $param = shift; |
97
|
|
|
|
|
|
|
Function::Interface::Info::Function::Param->new( |
98
|
|
|
|
|
|
|
type => $param->{type}, |
99
|
|
|
|
|
|
|
name => $param->{name}, |
100
|
|
|
|
|
|
|
named => $param->{named}, |
101
|
|
|
|
|
|
|
optional => $param->{optional}, |
102
|
|
|
|
|
|
|
) |
103
|
55
|
|
|
|
|
123
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _make_function_return_param { |
106
|
28
|
|
|
28
|
|
36
|
my $type = shift; |
107
|
28
|
|
|
|
|
56
|
Function::Interface::Info::Function::ReturnParam->new( |
108
|
|
|
|
|
|
|
type => $type, |
109
|
|
|
|
|
|
|
) |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _assert_valid_interface { |
113
|
44
|
|
|
44
|
|
103
|
my $src = shift; |
114
|
|
|
|
|
|
|
|
115
|
44
|
100
|
|
|
|
1278520
|
$src =~ m{ |
116
|
|
|
|
|
|
|
\A |
117
|
|
|
|
|
|
|
(? |
118
|
|
|
|
|
|
|
(?&PerlOWS) (?(?&PerlIdentifier)) |
119
|
|
|
|
|
|
|
(?&PerlOWS) \((?.*?)\) |
120
|
|
|
|
|
|
|
(?&PerlOWS) :Return\((?.*?)\) |
121
|
|
|
|
|
|
|
; |
122
|
|
|
|
|
|
|
) |
123
|
|
|
|
|
|
|
$PPR::GRAMMAR |
124
|
|
|
|
|
|
|
}sx or croak "invalid interface"; |
125
|
|
|
|
|
|
|
|
126
|
37
|
|
|
|
|
3456
|
my %match; |
127
|
37
|
|
|
|
|
637
|
$match{statement} = $+{statement}; |
128
|
37
|
|
|
|
|
203
|
$match{subname} = $+{subname}; |
129
|
37
|
100
|
|
|
|
285
|
$match{params} = $+{params} ? _assert_valid_interface_params($+{params}) : []; |
130
|
33
|
100
|
|
|
|
257
|
$match{return} = $+{return} ? _assert_valid_interface_return($+{return}) : []; |
131
|
|
|
|
|
|
|
|
132
|
31
|
|
|
|
|
632
|
return \%match; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$Function::Interface::GRAMMAR = qr{ |
136
|
|
|
|
|
|
|
(?(DEFINE) |
137
|
|
|
|
|
|
|
(? |
138
|
|
|
|
|
|
|
(?&PerlIdentifier) |
139
|
|
|
|
|
|
|
(?: \s* \[ |
140
|
|
|
|
|
|
|
\s* (?&PerlTypeParameter) \s* |
141
|
|
|
|
|
|
|
(?: , \s* (?&PerlTypeParameter) \s* )*+ |
142
|
|
|
|
|
|
|
\] )? |
143
|
|
|
|
|
|
|
) |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
(? |
146
|
|
|
|
|
|
|
(?&PerlString)|(?&PerlVariable)|(?&PerlType) |
147
|
|
|
|
|
|
|
) |
148
|
|
|
|
|
|
|
) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$PPR::GRAMMAR |
151
|
|
|
|
|
|
|
}x; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub _assert_valid_interface_params { |
154
|
20
|
|
|
20
|
|
15406
|
my $src = shift; |
155
|
|
|
|
|
|
|
|
156
|
20
|
|
|
|
|
571556
|
my @list = grep { defined } $src =~ m{ |
|
2227
|
|
|
|
|
2795
|
|
157
|
|
|
|
|
|
|
((?&PerlType)) \s* |
158
|
|
|
|
|
|
|
(:?) # named \s* |
159
|
|
|
|
|
|
|
((?&PerlVariable)) \s* |
160
|
|
|
|
|
|
|
(=?) # optional |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$Function::Interface::GRAMMAR |
163
|
|
|
|
|
|
|
}xg; |
164
|
|
|
|
|
|
|
|
165
|
20
|
|
|
|
|
1693
|
my @params; |
166
|
20
|
|
|
|
|
154
|
while (my ($type, $named, $name, $optional) = splice @list, 0, 4) { |
167
|
17
|
|
|
|
|
172
|
push @params => { |
168
|
|
|
|
|
|
|
type => $type, |
169
|
|
|
|
|
|
|
named => !!$named, |
170
|
|
|
|
|
|
|
name => $name, |
171
|
|
|
|
|
|
|
optional => !!$optional, |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
my $regex = join '\s*,\s*', map { |
176
|
20
|
|
|
|
|
64
|
quotemeta sprintf('%s %s%s%s', |
177
|
|
|
|
|
|
|
$_->{type}, |
178
|
|
|
|
|
|
|
$_->{named} ? ':' : '', |
179
|
|
|
|
|
|
|
$_->{name}, |
180
|
17
|
100
|
|
|
|
205
|
$_->{optional} ? '=' : '', |
|
|
100
|
|
|
|
|
|
181
|
|
|
|
|
|
|
) |
182
|
|
|
|
|
|
|
} @params; |
183
|
|
|
|
|
|
|
|
184
|
20
|
100
|
|
|
|
2010
|
croak "invalid interface params: $src" |
185
|
|
|
|
|
|
|
unless $src =~ m{ \A \s* $regex \s* \z }x; |
186
|
|
|
|
|
|
|
|
187
|
12
|
|
|
|
|
409
|
return \@params; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub _assert_valid_interface_return { |
191
|
23
|
|
|
23
|
|
18838
|
my $src = shift; |
192
|
|
|
|
|
|
|
|
193
|
23
|
|
|
|
|
655027
|
my @list = grep { defined } $src =~ m{ |
|
4224
|
|
|
|
|
5146
|
|
194
|
|
|
|
|
|
|
((?&PerlType)) |
195
|
|
|
|
|
|
|
$Function::Interface::GRAMMAR |
196
|
|
|
|
|
|
|
}xg; |
197
|
|
|
|
|
|
|
|
198
|
23
|
100
|
|
|
|
2114
|
croak "invalid interface return: $src. It should be TYPELIST." |
199
|
|
|
|
|
|
|
unless $src =~ m{ |
200
|
23
|
|
|
|
|
61
|
\A \s* @{[join '\s*,\s*', map { quotemeta $_ } @list]} \s* \z |
|
33
|
|
|
|
|
2044
|
|
201
|
|
|
|
|
|
|
}x; |
202
|
|
|
|
|
|
|
|
203
|
16
|
|
|
|
|
510
|
return \@list; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
__END__ |