blib/lib/Method/Signatures/Simple/ParseKeyword.pm

filter: Statementstmt 81.3% 174 / 214 Branchbran 41.8% 31 / 74 Conditioncond 71.8% 23 / 32 MC/DCmcdc 60.0% 12 / 20 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 6 totaltotal 70.9% 264 / 372 CC 52 SCAR 29.2
CC
52
Cov
71%
parse_attribute
90.0
parse_signature
19.4
parse_attributes
15.2
CRAP 18.6
↑ Summary
1 package Method::Signatures::Simple::ParseKeyword;
2 11 $Method::Signatures::Simple::ParseKeyword::VERSION = '1.14';
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Method::Signatures::Simple::ParseKeyword - method and func keywords using Parse::Keyword
9  
10 =cut
11  
12 11 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Parse::Keyword {};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 our @EXPORT;
17 11 our %MAP;
18 11 $Carp::Internal{ (__PACKAGE__) }++;
19  
20 20 ▶sub import {
Subroutine import: called CC=12 SCAR=24.9
21 20 my $caller = caller;
22 20 my $class = shift;
23 20 my %args = @_;
24  
25 20 my %kwds;
26  
27 20 ▶ my $into = delete $args{into} || $caller;
Condition66%2/3
delete $args{'into'} || $caller
l!l&&r!l&&!r
2180
MC/DC0%0/2
delete $args{'into'} || $caller
delete $args{'into'} $caller
Truth table66%2/3
A || B
A= delete $args{'into'}B= $caller
ABresult
1X1
011
000
28 20 ▶ my $inv = delete $args{invocant} || '$self';
Condition100%2/2
delete $args{'invocant'} || '$self'
l!l
317
MC/DC100%1/1
delete $args{'invocant'} || '$self'
delete $args{'invocant'}
Truth table100%2/2
A
A= delete $args{'invocant'}
Aresult
11
00
29 20 ▶ my $meth = delete $args{name} || delete $args{method_keyword};
Condition100%3/3
delete $args{'name'} || delete $args{'method_keyword'}
l!l&&r!l&&!r
2117
MC/DC100%2/2
delete $args{'name'} || delete $args{'method_keyword'}
delete $args{'name'} delete $args{'method_keyword'}
Truth table100%3/3
A || B
A= delete $args{'name'}B= delete $args{'method_keyword'}
ABresult
1X1
011
000
30 20 my $func = delete $args{function_keyword};
31  
32 # if no options are provided at all, then we supply defaults
33 20 ▶ unless (defined $meth || defined $func) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $meth or defined $func) 15 5 20
Condition100%3/3
defined $meth or defined $func
l!l&&r!l&&!r
3215
MC/DC100%2/2
defined $meth or defined $func
defined $meth defined $func
Truth table100%3/3
A or B
A= defined $methB= defined $func
ABresult
1X1
011
000
34 15 $meth = 'method';
35 15 $func = 'func';
36 }
37  
38 # input validation
39 20 ▶ $inv =~ m/^ \s* \$ [\p{ID_Start}_] \p{ID_Continue}* \s* $/x
Branch: 1 branch
BranchTrueFalseTotal
$inv =~ /^ \s* \$ [\p{ID_Start}_] \p{ID_Continue}* \s* $/x or croak("invocant must be a valid scalar identifier >$inv<") 19 1 20
40 or croak "invocant must be a valid scalar identifier >$inv<";
41  
42 19 ▶ if ($func) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func) 16 3 19
43 16 ▶ $func =~ m/^ \s* [\p{ID_Start}_] \p{ID_Continue}* \s* $/x
Branch: 1 branch
BranchTrueFalseTotal
$func =~ /^ \s* [\p{ID_Start}_] \p{ID_Continue}* \s* $/x or croak("function_keyword must be a valid identifier >$func<") 15 1 16
44 or croak "function_keyword must be a valid identifier >$func<";
45 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 15 ▶ *$func = sub { @_ ? $_[0] : () };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
47 15 my $parse = "parse_$func";
48 15 ▶ *$parse = sub { my ($kw) = @_; parse_mode($kw); };
Subroutine __ANON__: called CC=1 SCAR=0.0
49 15 $MAP{$func} = undef;
50 15 $kwds{ $func } = \&$parse;
51 15 push @EXPORT, $func;
52 }
53 18 ▶ if ($meth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meth) 17 1 18
54 17 ▶ $meth =~ m/^ \s* [\p{ID_Start}_] \p{ID_Continue}* \s* $/x
Branch: 1 branch
BranchTrueFalseTotal
$meth =~ /^ \s* [\p{ID_Start}_] \p{ID_Continue}* \s* $/x or croak("method_keyword must be a valid identifier >$meth<") 16 1 17
55 or croak "method_keyword must be a valid identifier >$meth<";
56 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 16 ▶ *$meth = sub { @_ ? $_[0] : () };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 14 14
58 16 my $parse = "parse_$meth";
59 16 ▶ *$parse = sub { my ($kw) = @_; parse_mode($kw); };
Subroutine __ANON__: called CC=1 SCAR=0.0
60 16 $MAP{$meth} = $inv;
61 16 @kwds{ $meth } = \&$parse;
62 16 push @EXPORT, $meth;
63 }
64  
65 17 Parse::Keyword->import(\%kwds);
66 17 for my $e (@EXPORT) {
67 48 my $n = $e =~ s/[$%@]//r;
68 48 my $fn = $into. '::' . $n;
69 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 48 *$fn = $e;
71 48 my $k = *$fn; # avoid 'once' warning
72 }
73 }
74  
75 21 ▶sub parse_mode {
Subroutine parse_mode: called CC=5 SCAR=16.6
Pod undocumented
76 21 my ($keyword, $invocant) = @_;
77 21 ▶ $invocant ||= $MAP{$keyword};
Condition66%2/3
$invocant ||= $Method::Signatures::Simple::ParseKeyword::MAP{$keyword}
l!l&&r!l&&!r
0165
MC/DC50%1/2
$invocant ||= $Method::Signatures::Simple::ParseKeyword::MAP{$keyword}
$invocant $Method::Signatures::Simple::ParseKeyword::MAP{$keyword}
Truth table66%2/3
A ||= B
A= $invocantB= $Method::Signatures::Simple::ParseKeyword::MAP{$keyword}
ABresult
1X1
011
000
78  
79 21 my $name = parse_name();
80 21 my $sig = parse_signature($invocant);
81 18 my $attr = parse_attributes();
82 18 my $body = parse_body($sig);
83  
84 18 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 18 0 18
85 18 my $full_name = join('::', compiling_package, $name);
86 {
87 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 18 *$full_name = subname $full_name, $body;
89 18 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 0 18 18
90 11 ▶ use attributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
91 0 attributes->import(compiling_package, $body, $_) for @$attr;
92 }
93 }
94 18 ▶ return (sub {}, 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
95 }
96 else {
97 0 ▶ return (sub { $body }, 0);
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 }
99 }
100  
101 11 my $start_rx = qr/^[\p{ID_Start}_]$/;
102 11 my $cont_rx = qr/^\p{ID_Continue}$/;
103  
104 45 ▶sub parse_name {
Subroutine parse_name: called CC=4 SCAR=13.9
Pod undocumented
105 45 my $name = '';
106  
107 45 lex_read_space;
108  
109 45 my $char_rx = $start_rx;
110  
111 45 while (1) {
112 237 my $char = lex_peek;
113 237 ▶ last unless length $char;
Branch: 1 branch
BranchTrueFalseTotal
unless length $char 0 237 237
114 237 ▶ if ($char =~ $char_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /$char_rx/) { } 192 45 237
115 192 $name .= $char;
116 192 lex_read;
117 192 $char_rx = $cont_rx;
118 }
119 else {
120 45 last;
121 }
122 }
123  
124 45 ▶ return length($name) ? $name : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 45 0 45
125 }
126  
127 21 ▶sub parse_signature {
Subroutine parse_signature: called CC=16 SCAR=29.6
Pod undocumented
128 21 my ($invocant) = @_;
129 21 lex_read_space;
130  
131 21 ▶ my @vars = $invocant ? ({ index => 0, name => $invocant, is_inv => 1 }) : ();
Branch: 1 branch
BranchTrueFalseTotal
$invocant ? : 16 5 21
132 21 ▶ return \@vars unless lex_peek eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless lex_peek() eq "(" 0 0 0
133  
134 19 my @attr = ();
135  
136 19 lex_read;
137 19 lex_read_space;
138  
139 19 ▶ if (lex_peek eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ")") 0 0 0
140 3 lex_read;
141 3 return \@vars;
142 }
143  
144 16 my $seen_slurpy;
145 16 ▶ while ((my $sigil = lex_peek) ne ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $sigil = lex_peek()) ne ")") 0 0 0
146 26 my $var = {};
147 26 ▶ croak qq{syntax error: expected sigil instead of "$sigil"}
Branch: 1 branch
BranchTrueFalseTotal
unless $sigil eq "\$" or $sigil eq "\@" or $sigil eq "%" 1 25 26
Condition100%6/6
$sigil eq "\$" or $sigil eq "\@" or $sigil eq "%"
l!l&&r!l&&!r
2411
$sigil eq "\$" or $sigil eq "\@"
l!l&&r!l&&!r
2132
MC/DC100%3/3
$sigil eq "\$" or $sigil eq "\@" or $sigil eq "%"
$sigil eq "\$" $sigil eq "\@" $sigil eq "%"
Truth table100%4/4
A or B or C
A= $sigil eq "\$"B= $sigil eq "\@"C= $sigil eq "%"
ABCresult
1XX1
01X1
0011
0000
148 unless $sigil eq '$' || $sigil eq '@' || $sigil eq '%';
149 25 ▶ croak "Can't declare parameters after a slurpy parameter"
Branch: 1 branch
BranchTrueFalseTotal
if $seen_slurpy 1 24 25
150 if $seen_slurpy;
151  
152 24 ▶ $seen_slurpy = 1 if $sigil eq '@' || $sigil eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $sigil eq "\@" or $sigil eq "%" 4 20 24
Condition100%3/3
$sigil eq "\@" or $sigil eq "%"
l!l&&r!l&&!r
3120
MC/DC100%2/2
$sigil eq "\@" or $sigil eq "%"
$sigil eq "\@" $sigil eq "%"
Truth table100%3/3
A or B
A= $sigil eq "\@"B= $sigil eq "%"
ABresult
1X1
011
000
153  
154 24 lex_read;
155 24 lex_read_space;
156 24 my $name = parse_name(0);
157 24 lex_read_space;
158  
159 24 $var->{name} = "$sigil$name";
160  
161 24 ▶ if (lex_peek eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq "=") 0 0 0
162 1 lex_read;
163 1 lex_read_space;
164 1 $var->{default} = parse_arithexpr;
165 }
166  
167 24 $var->{index} = @vars - 1;
168  
169 24 ▶ if (lex_peek eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ":") 0 0 0
170 4 $vars[0] = $var;
171 4 lex_read;
172 4 lex_read_space;
173 4 next;
174 }
175  
176 20 push @vars, $var;
177  
178 20 ▶ croak qq{syntax error: expected ')' or ',' instead of "} . lex_peek . q{"}
Branch: 1 branch
BranchTrueFalseTotal
unless lex_peek() eq ")" or lex_peek() eq "," 0 0 0
Condition0%0/3
lex_peek() eq ")" or lex_peek() eq ","
l!l&&r!l&&!r
000
MC/DC0%0/2
lex_peek() eq ")" or lex_peek() eq ","
lex_peek() eq ")" lex_peek() eq ","
Truth table0%0/3
A or B
A= lex_peek() eq ")"B= lex_peek() eq ","
ABresult
1X1
011
000
179 unless lex_peek eq ')' || lex_peek eq ',';
180  
181 19 ▶ if (lex_peek eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ",") 0 0 0
182 9 lex_read;
183 9 lex_read_space;
184 }
185 }
186  
187 13 lex_read;
188  
189 13 return \@vars;
190 }
191  
192 # grabbed these two functions from
193 # https://metacpan.org/release/PEVANS/XS-Parse-Keyword-0.22/source/hax/lexer-additions.c.inc#L74
194 0 ▶sub parse_attribute {
Subroutine parse_attribute: not called CC=9 SCAR=45.0
Pod undocumented
195 0 my $name = parse_name;
196 0 ▶ if (lex_peek ne '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() ne "(") 0 0 0
197 0 return $name;
198 }
199 0 $name .= lex_peek;
200 0 lex_read;
201 0 my $count = 1;
202 0 my $c = lex_peek;
203 0 ▶ while($count && length $c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count and length $c) 0 0 0
Condition0%0/3
$count and length $c
!ll&&!rl&&r
000
MC/DC0%0/2
$count and length $c
$count length $c
Truth table0%0/3
A and B
A= $countB= length $c
ABresult
0X0
100
111
204 0 ▶ if($c eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 0 0 0
205 0 $count++;
206 }
207 0 ▶ if($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 0 0 0
208 0 $count--;
209 }
210 0 ▶ if($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 0 0
211 # The next char does not bump count even if it is ( or );
212 # the \\ is still captured
213 #
214 0 $name .= $c;
215 0 lex_read;
216 0 $c = lex_peek;
217 0 ▶ if(! length $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $c) 0 0 0
218 0 goto unterminated;
219 }
220 }
221  
222 # Don't append final closing ')' on split name/val
223 0 $name .= $c;
224 0 lex_read;
225  
226 0 $c = lex_peek;
227 }
228  
229 0 ▶ if(!length $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $c) 0 0 0
230 0 return;
231 }
232  
233 0 return $name;
234  
235 0 unterminated:
236 croak("Unterminated attribute parameter in attribute list");
237 0 return;
238 }
239  
240 18 ▶sub parse_attributes {
Subroutine parse_attributes: called CC=4 SCAR=27.2
Pod undocumented
241 18 lex_read_space;
242 18 ▶ return unless lex_peek eq ':';
Branch: 1 branch
BranchTrueFalseTotal
unless lex_peek() eq ":" 0 0 0
243 0 lex_read;
244 0 lex_read_space;
245 0 my @attrs;
246 0 ▶ while (my $attr = parse_attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = parse_attribute()) 0 0 0
247 0 push @attrs, $attr;
248 0 lex_read_space;
249 0 ▶ if (lex_peek eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ":") 0 0 0
250 0 lex_read;
251 0 lex_read_space;
252 }
253 }
254  
255 0 return \@attrs;
256 }
257  
258 18 ▶sub parse_body {
Subroutine parse_body: called CC=6 SCAR=18.0
Pod undocumented
259 18 my ($sigs) = @_;
260 18 my $body;
261  
262 18 lex_read_space;
263  
264 18 ▶ if (lex_peek eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq '{') { } 0 0 0
265 18 local $CAPRPK::{'DEFAULTS::'};
266 18 ▶ if ($sigs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigs) 18 0 18
267 18 lex_read;
268  
269 18 my $preamble = '{';
270  
271 # arguments / query params
272 #
273 # if this is a method, unshift the invocant from @_
274 18 ▶ if (@$sigs && $sigs->[0]{is_inv}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$sigs and $sigs->[0]{"is_inv"}) 11 7 18
Condition66%2/3
@$sigs and $sigs->[0]{"is_inv"}
!ll&&!rl&&r
0711
MC/DC50%1/2
@$sigs and $sigs->[0]{"is_inv"}
@$sigs $sigs->[0]{"is_inv"}
Truth table66%2/3
A and B
A= @$sigsB= $sigs->[0]{"is_inv"}
ABresult
0X0
100
111
275 11 $preamble .= "my $sigs->[0]{name} = shift;";
276 11 shift @$sigs;
277 }
278 18 my @names = map { $_->{name} } @$sigs;
279 18 $preamble .= 'my (' . join(', ', @names) . ') = @_;';
280  
281 18 my $index = 0;
282 18 for my $var (grep { defined $_->{default} } @$sigs) {
283 {
284 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
285 1 ▶ *{ 'CAPRPK::DEFAULTS::default_' . $index } = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 $var->{default}
287 1 };
288 }
289 1 $preamble .= $var->{name} . ' = CAPRPK::DEFAULTS::default_' . $index . '->()' . ' unless ' . $var->{name} . ';';
290  
291 1 $index++;
292 }
293  
294 18 $preamble .= "; ();"; # fix for empty method body
295 18 lex_stuff($preamble);
296 }
297 18 $body = parse_block;
298 }
299 else {
300 0 die "syntax error";
301 }
302 18 return $body;
303 }
304  
305 11 1;
306  
307 __END__
↑ Summary