Signatures.pm

filter: Statementstmt 97.5% 390 / 400 Branchbran 90.8% 169 / 186 Conditioncond 86.1% 81 / 94 MC/DCmcdc 60.7% 34 / 56 Subroutinesub 98.3% 60 / 61 Podpod 18.7% 3 / 16 totaltotal 90.6% 737 / 813 CC 133 SCAR 17.9
CC
133
Cov
94%
signature
19.0
param
11.2
_param_where
7.0
CRAP 6.0
« Signatures/TypeConstraint.pm ↑ Summary Signatures/Sig.pm »
1 package Parse::Method::Signatures;
2  
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 use MooseX::Types::Moose qw/
5 ArrayRef HashRef ScalarRef CodeRef Int Str ClassName
6 3 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Class::Load qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Parse::Method::Signatures::ParamCollection;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 use Parse::Method::Signatures::Types qw/
13 PositionalParam NamedParam UnpackedParam
14 3 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 our $VERSION = '1.003018_01';
20 3 $VERSION = eval $VERSION;
21 3 our $ERROR_LEVEL = 0;
22 3 our %LEXTABLE;
23 3 ▶our $DEBUG = $ENV{PMS_DEBUG} || 0;
Condition50%1/2
$ENV{'PMS_DEBUG'} || 0
l!l
03
MC/DC0%0/1
$ENV{'PMS_DEBUG'} || 0
$ENV{'PMS_DEBUG'}
Truth table50%1/2
A
A= $ENV{'PMS_DEBUG'}
Aresult
11
00
24  
25 # Setup what we need for specific PPI subclasses
26 3 @PPI::Token::EOF::ISA = 'PPI::Token';
27  
28 3 class_type "PPI::Document";
29 3 class_type "PPI::Element";
30  
31 3 has 'input' => (
32 is => 'ro',
33 isa => Str,
34 required => 1
35 );
36  
37 3 has 'offset' => (
38 is => 'rw',
39 isa => Int,
40 default => 0,
41 );
42  
43 3 has 'signature_class' => (
44 is => 'ro',
45 isa => Str,
46 default => 'Parse::Method::Signatures::Sig',
47 );
48  
49 3 has 'param_class' => (
50 is => 'ro',
51 isa => Str,
52 default => 'Parse::Method::Signatures::Param',
53 );
54  
55 3 has 'type_constraint_class' => (
56 is => 'ro',
57 isa => Str,
58 default => 'Parse::Method::Signatures::TypeConstraint',
59 );
60  
61 3 has 'type_constraint_callback' => (
62 is => 'ro',
63 isa => CodeRef,
64 predicate => 'has_type_constraint_callback',
65 );
66  
67 3 has 'from_namespace' => (
68 is => 'rw',
69 isa => ClassName,
70 predicate => 'has_from_namespace'
71 );
72  
73 3 has 'ppi_doc' => (
74 is => 'ro',
75 isa => 'PPI::Document',
76 lazy_build => 1,
77 builder => 'parse',
78 );
79  
80 # A bit dirty, but we set this with local most of the time
81 3 has 'ppi' => (
82 is => 'ro',
83 isa => 'PPI::Element',
84 lazy_build => 1,
85 writer => '_set_ppi'
86 );
87  
88 sub BUILD {
89 147 ▶ my ($self) = @_;
Subroutine BUILD: called CC=3 SCAR=11.1
Pod undocumented
90  
91 load_class($_)
92 147 for map { $self->$_ } qw/
93 signature_class
94 param_class
95 type_constraint_class
96 /;
97  
98 147 my $ppi = $self->ppi;
99  
100 # Skip leading whitespace
101 147 ▶ $self->consume_token
Branch: 1 branch
BranchTrueFalseTotal
unless $ppi->significant 0 147 147
102 unless $ppi->significant;
103 }
104  
105 sub create_param {
106 206 ▶ my ($self, $args) = @_;
Subroutine create_param: called CC=5 SCAR=16.1
Pod undocumented
107  
108 206 my @traits;
109 push @traits, $args->{ variable_name } ? 'Bindable' : 'Placeholder'
110 206 ▶ if !exists $args->{unpacking};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $args->{"unpacking"} 190 16 206
$args->{'variable_name'} ? : 180 10 190
111 206 ▶ push @traits, $args->{ named } ? 'Named' : 'Positional';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'named'} ? : 51 155 206
112 push @traits, 'Unpacked::' . $args->{unpacking}
113 206 ▶ if exists $args->{unpacking};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"unpacking"} 16 190 206
114  
115 206 return $self->param_class->new_with_traits(traits => \@traits, %{ $args });
116 }
117  
118 override BUILDARGS => sub {
119 147 ▶ my $class = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
120  
121 147 ▶ return { input => $_[0] } if @_ == 1 and !ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not ref $_[0] 146 1 147
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
10146
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
122  
123 1 return super();
124 3 };
125  
126 sub parse {
127 147 ▶ my ($self) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
128
129 147 my $input = substr($self->input, $self->offset);
130 147 my $doc = PPI::Document->new(\$input);
131  
132 # Append the magic EOF Token
133 147 $doc->add_element(PPI::Token::EOF->new(""));
134  
135 # Annoyingly "m($x)" gets treated as a regex operator. This isn't what we
136 # want. so replace it with a Word, then a list. The way we do this is by
137 # taking the operator off the front, then reparsing the rest of the content
138 # This will look the same (so wont affect anything in a code block) but is
139 # just store different token wise.
140 147 $self->_replace_regexps($doc);
141  
142 # ($, $x) parses the $, as a single var. not what we want. FIX UP
143 # While we're att it lets fixup $: $? and $!
144 147 $self->_replace_magic($doc);
145  
146 # (Str :$x) yields a label of "Str :"
147 # (Foo Bar :$x) yields a label of "Bar :"
148 147 $self->_replace_labels($doc);
149  
150 # This one is actually a bug in PPI, rather than just an oddity
151 # (Str $x = 0xfF) parses as "Oxf" and a word of "F"
152 147 $self->_fixup_hex($doc);
153  
154 147 return $doc;
155 }
156  
157 sub _replace_regexps {
158 147 ▶ my ($self, $doc) = @_;
Subroutine _replace_regexps: called CC=5 SCAR=16.1
159  
160 REGEXP:
161 147 ▶ foreach my $node ( @{ $doc->find('Token::Regexp') || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$doc->find("Token::Regexp") or [] 145 2 147
162 2 my $str = $node->content;
163  
164 2 ▶ next REGEXP unless defined $node->{operator};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{"operator"} 1 1 2
165  
166 # Rather annoyingly, there are *no* methods on Token::Regexp;
167 1 my ($word, $rest) = $str =~ /^(\Q@{[$node->{operator}]}\E)(.*)$/s;
168  
169 1 my $subdoc = PPI::Document->new(\$rest);
170 1 my @to_add = reverse map { $_->remove } $subdoc->children;
171 1 push @to_add, new PPI::Token::Word($word);
172 # insert_after restricts what you can insert.
173 # $node->insert_after($_) for @to_add;
174 1 $node->__insert_after($_) for @to_add;
175  
176 1 $node->delete;
177 }
178 }
179  
180  
181 sub _replace_magic {
182 147 ▶ my ($self, $doc) = @_;
Subroutine _replace_magic: called CC=4 SCAR=13.9
183  
184 147 ▶ foreach my $node ( @{ $doc->find('Token::Magic') || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$doc->find("Token::Magic") or [] 135 12 147
185 17 ▶ my ($op) = $node->content =~ /^\$([,?:!)])$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($op) = $node->content =~ /^\$([,?:!)])$/ or next 10 7 17
186  
187 7 $node->insert_after(new PPI::Token::Operator($op));
188 7 $node->insert_after(new PPI::Token::Cast('$'));
189 7 $node->delete;
190 }
191 }
192  
193 sub _replace_labels {
194 147 ▶ my ($self, $doc) = @_;
Subroutine _replace_labels: called CC=5 SCAR=16.1
195  
196 147 ▶ foreach my $node ( @{ $doc->find('Token::Label') || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$doc->find("Token::Label") or [] 143 4 147
197 4 ▶ my ($word, $ws) = $node->content =~ /^(.*?)(\s+)?:$/s or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $node->content =~ /^(.*?)(\s+)?:$/s or next 0 4 4
198  
199 4 $node->insert_after(new PPI::Token::Operator(':'));
200 4 ▶ $node->insert_after(new PPI::Token::Whitespace($ws)) if defined $ws;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ws 3 1 4
201 4 $node->insert_after(new PPI::Token::Word($word));
202 4 $node->delete;
203 }
204 }
205  
206 sub _fixup_hex {
207 147 ▶ my ($self, $doc) = @_;
Subroutine _fixup_hex: called CC=5 SCAR=17.2
208  
209 147 ▶ foreach my $node ( @{ $doc->find('Token::Number::Hex') || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$doc->find("Token::Number::Hex") or [] 144 3 147
210 3 my $next = $node->next_token;
211 3 ▶ next unless $next->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
unless $next->isa("PPI::Token::Word") and $next->content =~ /^[0-9a-f]+$/i 3 0 3
Condition66%2/3
$next->isa("PPI::Token::Word") and $next->content =~ /^[0-9a-f]+$/i
!ll&&!rl&&r
210
MC/DC0%0/2
$next->isa("PPI::Token::Word") and $next->content =~ /^[0-9a-f]+$/i
$next->isa("PPI::Token::Word") $next->content =~ /^[0-9a-f]+$/i
Truth table66%2/3
A and B
A= $next->isa("PPI::Token::Word")B= $next->content =~ /^[0-9a-f]+$/i
ABresult
0X0
100
111
212 && $next->content =~ /^[0-9a-f]+$/i;
213  
214 0 $node->add_content($next->content);
215 0 $next->delete;
216 }
217 }
218  
219 sub _build_ppi {
220 147 ▶ my ($self) = @_;
Subroutine _build_ppi: called CC=3 SCAR=11.0
221 147 my $ppi = $self->ppi_doc->first_token;
222  
223 147 ▶ if ($ppi->class eq 'PPI::Token::Word' && exists $LEXTABLE{"$ppi"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppi->class eq "PPI::Token::Word" and exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}) 1 146 147
Condition100%3/3
$ppi->class eq "PPI::Token::Word" and exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
!ll&&!rl&&r
134121
MC/DC100%2/2
$ppi->class eq "PPI::Token::Word" and exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
$ppi->class eq "PPI::Token::Word" exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
Truth table100%3/3
A and B
A= $ppi->class eq "PPI::Token::Word"B= exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
ABresult
0X0
100
111
224 1 bless $ppi, "PPI::Token::LexSymbol";
225 1 $ppi->{lex} = $LEXTABLE{"$ppi"};
226 }
227 147 return $ppi;
228 }
229  
230 # signature: O_PAREN
231 # invocant
232 # params
233 # C_PAREN
234 #
235 # invocant: param ':'
236 #
237 # params: param COMMA params
238 # | param
239 # | /* NUL */
240 sub signature {
241 120 ▶ my $self = shift;
Subroutine signature: called CC=19 SCAR=29.4
Pod documented
242  
243 120 ▶ $self = $self->new(@_) unless blessed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 119 1 120
244  
245 120 $self->assert_token('(');
246  
247 120 my $args = {};
248 120 my $params = [];
249  
250 120 my $param = $self->param;
251  
252 108 ▶ if ($param && $self->ppi->content eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param and $self->ppi->content eq ":") 13 95 108
Condition100%3/3
$param and $self->ppi->content eq ":"
!ll&&!rl&&r
68913
MC/DC100%2/2
$param and $self->ppi->content eq ":"
$param $self->ppi->content eq ":"
Truth table100%3/3
A and B
A= $paramB= $self->ppi->content eq ":"
ABresult
0X0
100
111
253 # That param was actually the invocant
254 13 $args->{invocant} = $param;
255 13 ▶ croak "Invocant cannot be named"
Branch: 1 branch
BranchTrueFalseTotal
if NamedParam()->check($param) 1 12 13
256 if NamedParam->check($param);
257 12 ▶ croak "Invocant cannot be optional"
Branch: 1 branch
BranchTrueFalseTotal
unless $param->required 1 11 12
258 if !$param->required;
259 11 ▶ croak "Invocant cannot have a default value"
Branch: 1 branch
BranchTrueFalseTotal
if $param->has_default_value 1 10 11
260 if $param->has_default_value;
261  
262 10 ▶ croak "Invocant must be a simple scalar"
Branch: 1 branch
BranchTrueFalseTotal
if UnpackedParam()->check($param) or $param->sigil ne "\$" 3 7 10
Condition66%2/3
UnpackedParam()->check($param) or $param->sigil ne "\$"
l!l&&r!l&&!r
037
MC/DC50%1/2
UnpackedParam()->check($param) or $param->sigil ne "\$"
UnpackedParam()->check($param) $param->sigil ne "\$"
Truth table66%2/3
A or B
A= UnpackedParam()->check($param)B= $param->sigil ne "\$"
ABresult
1X1
011
000
263 if UnpackedParam->check($param) || $param->sigil ne '$';
264  
265 7 $self->consume_token;
266 7 $param = $self->param;
267  
268 }
269  
270 102 ▶ if ($param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 95 7 102
271 95 push @$params, $param;
272  
273 95 ▶ my $greedy = $param->sigil ne '$' ? $param : undef;
Branch: 1 branch
BranchTrueFalseTotal
$param->sigil ne '$' ? : 7 88 95
274 95 my $opt_pos_param = !$param->required;
275  
276 95 ▶ while ($self->ppi->content eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->content eq ",") 42 89 131
277 42 $self->consume_token;
278  
279 42 my $err_ctx = $self->ppi;
280 42 $param = $self->param;
281 42 ▶ $self->error($err_ctx, "Parameter expected")
Branch: 1 branch
BranchTrueFalseTotal
unless $param 1 41 42
282 if !$param;
283  
284 41 my $is_named = NamedParam->check($param);
285 41 ▶ if (!$is_named) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_named) 26 15 41
286 26 ▶ if ($param->required && $opt_pos_param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->required and $opt_pos_param) 1 25 26
Condition100%3/3
$param->required and $opt_pos_param
!ll&&!rl&&r
3221
MC/DC100%2/2
$param->required and $opt_pos_param
$param->required $opt_pos_param
Truth table100%3/3
A and B
A= $param->requiredB= $opt_pos_param
ABresult
0X0
100
111
287 1 $self->error($err_ctx, "Invalid: Required positional param " .
288 " found after optional one");
289 }
290 25 ▶ if ($greedy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($greedy) 4 21 25
291 4 croak "Invalid: Un-named parameter '" . $param->variable_name
292 . "' after greedy '"
293 . $greedy->variable_name . "'\n";
294 }
295 }
296  
297 36 push @$params, $param;
298 36 ▶ $opt_pos_param = $opt_pos_param || !$param->required;
Condition100%3/3
$opt_pos_param || !$param->required
l!l&&r!l&&!r
9720
MC/DC100%2/2
$opt_pos_param || !$param->required
$opt_pos_param !$param->required
Truth table100%3/3
A || not(B)
A= $opt_pos_paramB= !$param->required
ABresult
1X1
011
000
299 36 ▶ $greedy = $param->sigil ne '$' ? $param : undef;
Branch: 1 branch
BranchTrueFalseTotal
$param->sigil ne '$' ? : 4 32 36
300 }
301 }
302  
303 96 $self->assert_token(')');
304 87 $args->{params} = $params;
305  
306 87 my $sig = $self->signature_class->new($args);
307  
308 87 return $sig;
309 }
310  
311  
312 # param: tc?
313 # var
314 # (OPTIONAL|REQUIRED)?
315 # default?
316 # where*
317 # trait*
318 #
319 # where: WHERE <code block>
320 #
321 # trait: TRAIT class
322 #
323 # var : COLON label '(' var_or_unpack ')' # label is classish, with only /a-z0-9_/i allowed
324 # | COLON VAR
325 # | var_or_unpack
326 #
327 # var_or_unpack : '[' param* ']' # should all be required + un-named
328 # | '{' param* '}' # Should all be named
329 # | VAR
330 #
331 # OPTIONAL: '?'
332 # REQUIRED: '!'
333 sub param {
334 232 ▶ my $self = shift;
Subroutine param: called CC=11 SCAR=24.1
Pod documented
335 232 my $class_meth;
336 232 ▶ unless (blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self)) 2 230 232
337 2 ▶ $self = $self->new(@_) unless blessed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 2 0 2
338 2 $class_meth = 1;
339 }
340  
341 # Also used to check if a anything has been consumed
342 232 my $err_ctx = $self->ppi;
343  
344 232 my $param = {
345 required => 1,
346 };
347  
348 232 $self->_param_typed($param);
349  
350 230 ▶ $self->_param_opt_or_req(
Branch: 1 branch
BranchTrueFalseTotal
$self->_param_opt_or_req($self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param)) or $err_ctx == $self->ppi and return or $self->error($err_ctx) 2 209 211
Condition85%12/14
$self->_param_opt_or_req($self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param)) or $err_ctx == $self->ppi and return
l!l&&r!l&&!r
209010
$self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param)
l!l&&r!l&&!r
197240
$self->_param_labeled($param) or $self->_param_named($param) or $self->_param_variable($param)
l!l&&r!l&&!r
5114724
$self->_param_labeled($param) or $self->_param_named($param)
l!l&&r!l&&!r
943173
$err_ctx == $self->ppi and return
!ll
28
MC/DC0%0/2
$self->_param_opt_or_req($self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param)) or $err_ctx == $self->ppi and return
$self->_param_opt_or_req($self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param)) $err_ctx == $self->ppi
MC/DC0%0/4
$self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param)
$self->_param_labeled($param) $self->_param_named($param) $self->_param_variable($param) $self->_unpacked_param($param)
Truth table33%1/3
A or B
A= $self->_param_opt_or_req($self->_param_labeled($param) || $self->_param_named($param) || $self->_param_variable($param) || $self->_unpacked_param($param))B= $err_ctx == $self->ppi
ABresult
1X1
011
000
Truth table80%4/5
A or B or C || D
A= $self->_param_labeled($param)B= $self->_param_named($param)C= $self->_param_variable($param)D= $self->_unpacked_param($param)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
351 $self->_param_labeled($param)
352 || $self->_param_named($param)
353 || $self->_param_variable($param)
354 || $self->_unpacked_param($param)
355 ) or ($err_ctx == $self->ppi and return)
356 or $self->error($err_ctx);
357  
358 209 $self->_param_default($param);
359 209 $self->_param_constraint_or_traits($param);
360  
361 206 $param = $self->create_param($param);
362  
363 206 ▶ return !$class_meth
Branch: 2 branches
BranchTrueFalseTotal
!$class_meth ? : 204 2 206
wantarray ? : 0 2 2
364 ? $param
365 : wantarray
366 ? ($param, $self->remaining_input)
367 : $param;
368 }
369  
370 sub _param_opt_or_req {
371 219 ▶ my ($self, $param) = @_;
Subroutine _param_opt_or_req: called CC=5 SCAR=16.1
372  
373 219 ▶ return unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 10 209 219
374  
375 209 ▶ if ($self->ppi->class eq 'PPI::Token::Operator') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->class eq "PPI::Token::Operator") 115 94 209
376 115 my $c = $self->ppi->content;
377 115 ▶ if ($c eq '?') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '?') { } 9 106 115
elsif ($c eq '!') { } 7 99 106
378 9 $param->{required} = 0;
379 9 $self->consume_token;
380 } elsif ($c eq '!') {
381 7 $param->{required} = 1;
382 7 $self->consume_token;
383 }
384 }
385 209 return $param;
386  
387 }
388  
389 sub _param_constraint_or_traits {
390 209 ▶ my ($self, $param) = @_;
Subroutine _param_constraint_or_traits: called CC=3 SCAR=11.0
391  
392 209 ▶ while ($self->_param_where($param) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_param_where($param) or $self->_param_traits($param)) 19 206 225
Condition100%3/3
$self->_param_where($param) or $self->_param_traits($param)
l!l&&r!l&&!r
118207
MC/DC100%2/2
$self->_param_where($param) or $self->_param_traits($param)
$self->_param_where($param) $self->_param_traits($param)
Truth table100%3/3
A or B
A= $self->_param_where($param)B= $self->_param_traits($param)
ABresult
1X1
011
000
393 $self->_param_traits($param) ) {
394 # No op;
395  
396 }
397 206 return $param;
398 }
399  
400 sub _param_where {
401 228 ▶ my ($self, $param) = @_;
Subroutine _param_where: called CC=7 SCAR=19.5
402  
403 228 ▶ return unless $self->ppi->isa('PPI::Token::LexSymbol')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->isa("PPI::Token::LexSymbol") and $self->ppi->lex eq "WHERE" 215 13 228
Condition100%3/3
$self->ppi->isa("PPI::Token::LexSymbol") and $self->ppi->lex eq "WHERE"
!ll&&!rl&&r
206913
MC/DC100%2/2
$self->ppi->isa("PPI::Token::LexSymbol") and $self->ppi->lex eq "WHERE"
$self->ppi->isa("PPI::Token::LexSymbol") $self->ppi->lex eq "WHERE"
Truth table100%3/3
A and B
A= $self->ppi->isa("PPI::Token::LexSymbol")B= $self->ppi->lex eq "WHERE"
ABresult
0X0
100
111
404 && $self->ppi->lex eq 'WHERE';
405  
406 13 $self->consume_token;
407  
408 13 ▶ $param->{constraints} ||= [];
Condition100%2/2
$param->{"constraints"} ||= []
l!l
211
MC/DC100%1/1
$param->{"constraints"} ||= []
$param->{"constraints"}
Truth table100%2/2
A
A= $param->{"constraints"}
Aresult
11
00
409  
410 13 my $ppi = $self->ppi;
411  
412 13 ▶ $self->error($ppi, "Block expected after where")
Branch: 1 branch
BranchTrueFalseTotal
unless $ppi->class eq "PPI::Token::Structure" and $ppi->content eq "{" 1 12 13
Condition66%2/3
$ppi->class eq "PPI::Token::Structure" and $ppi->content eq "{"
!ll&&!rl&&r
0112
MC/DC50%1/2
$ppi->class eq "PPI::Token::Structure" and $ppi->content eq "{"
$ppi->class eq "PPI::Token::Structure" $ppi->content eq "{"
Truth table66%2/3
A and B
A= $ppi->class eq "PPI::Token::Structure"B= $ppi->content eq "{"
ABresult
0X0
100
111
413 unless $ppi->class eq 'PPI::Token::Structure'
414 && $ppi->content eq '{';
415  
416 # Go from token to block
417 12 $ppi = $ppi->parent;
418  
419 12 ▶ $ppi->finish or $self->error($ppi,
Branch: 1 branch
BranchTrueFalseTotal
$ppi->finish or $self->error($ppi, q[Runaway '] . $ppi->braces . q[' in ] . $self->_parsing_area(1), 1) 1 11 12
420 "Runaway '" . $ppi->braces . "' in " . $self->_parsing_area(1), 1);
421  
422 11 push @{$param->{constraints}}, $ppi->content;
423  
424 11 $self->_set_ppi($ppi->finish);
425 11 $self->consume_token;
426 11 return $param;
427 }
428  
429 sub _param_traits {
430 215 ▶ my ($self, $param) = @_;
Subroutine _param_traits: called CC=5 SCAR=16.1
431 215 ▶ return unless $self->ppi->isa('PPI::Token::LexSymbol')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->isa("PPI::Token::LexSymbol") and $self->ppi->lex eq "TRAIT" 206 9 215
Condition66%2/3
$self->ppi->isa("PPI::Token::LexSymbol") and $self->ppi->lex eq "TRAIT"
!ll&&!rl&&r
20609
MC/DC50%1/2
$self->ppi->isa("PPI::Token::LexSymbol") and $self->ppi->lex eq "TRAIT"
$self->ppi->isa("PPI::Token::LexSymbol") $self->ppi->lex eq "TRAIT"
Truth table66%2/3
A and B
A= $self->ppi->isa("PPI::Token::LexSymbol")B= $self->ppi->lex eq "TRAIT"
ABresult
0X0
100
111
432 && $self->ppi->lex eq 'TRAIT';
433  
434 9 my $op = $self->consume_token->content;
435  
436 9 ▶ $self->error($self->ppi, "Error parsing parameter trait")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->isa("PPI::Token::Word") 1 8 9
437 unless $self->ppi->isa('PPI::Token::Word');
438  
439 8 ▶ $param->{param_traits} ||= [];
Condition100%2/2
$param->{"param_traits"} ||= []
l!l
26
MC/DC100%1/1
$param->{"param_traits"} ||= []
$param->{"param_traits"}
Truth table100%2/2
A
A= $param->{"param_traits"}
Aresult
11
00
440  
441 8 push @{$param->{param_traits}}, [$op, $self->consume_token->content];
442 8 return $param;
443 }
444  
445 sub _param_labeled {
446 230 ▶ my ($self, $param) = @_;
Subroutine _param_labeled: called CC=6 SCAR=17.9
447  
448 return unless
449 230 ▶ $self->ppi->content eq ':' &&
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->content eq ":" and $self->ppi->next_token->isa("PPI::Token::Word") 216 14 230
Condition100%3/3
$self->ppi->content eq ":" and $self->ppi->next_token->isa("PPI::Token::Word")
!ll&&!rl&&r
1674914
MC/DC100%2/2
$self->ppi->content eq ":" and $self->ppi->next_token->isa("PPI::Token::Word")
$self->ppi->content eq ":" $self->ppi->next_token->isa("PPI::Token::Word")
Truth table100%3/3
A and B
A= $self->ppi->content eq ":"B= $self->ppi->next_token->isa("PPI::Token::Word")
ABresult
0X0
100
111
450 $self->ppi->next_token->isa('PPI::Token::Word');
451  
452 14 $self->consume_token;
453  
454 14 ▶ $self->error($self->ppi, "Invalid label")
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi->content =~ /[^-\w]/ 0 14 14
455 if $self->ppi->content =~ /[^-\w]/;
456  
457 14 $param->{named} = 1;
458 14 $param->{required} = 0;
459 14 $param->{label} = $self->consume_token->content;
460  
461 14 $self->assert_token('(');
462 14 ▶ $self->_unpacked_param($param)
Branch: 1 branch
BranchTrueFalseTotal
$self->_unpacked_param($param) or $self->_param_variable($param) or $self->error($self->ppi) 1 9 10
Condition100%3/3
$self->_unpacked_param($param) or $self->_param_variable($param)
l!l&&r!l&&!r
451
MC/DC100%2/2
$self->_unpacked_param($param) or $self->_param_variable($param)
$self->_unpacked_param($param) $self->_param_variable($param)
Truth table100%3/3
A or B
A= $self->_unpacked_param($param)B= $self->_param_variable($param)
ABresult
1X1
011
000
463 || $self->_param_variable($param)
464 || $self->error($self->ppi);
465  
466 9 $self->assert_token(')');
467  
468 9 return $param;
469 }
470  
471 sub _unpacked_param {
472 38 ▶ my ($self, $param) = @_;
Subroutine _unpacked_param: called CC=2 SCAR=6.9
473  
474 38 ▶ return $self->bracketed('[', \&unpacked_array, $param) ||
Condition100%3/3
$self->bracketed('[', \&Parse::Method::Signatures::unpacked_array, $param) || $self->bracketed('{', \&Parse::Method::Signatures::unpacked_hash, $param)
l!l&&r!l&&!r
8206
MC/DC100%2/2
$self->bracketed('[', \&Parse::Method::Signatures::unpacked_array, $param) || $self->bracketed('{', \&Parse::Method::Signatures::unpacked_hash, $param)
$self->bracketed('[', \&Parse::Method::Signatures::unpacked_array, $param) $self->bracketed('{', \&Parse::Method::Signatures::unpacked_hash, $param)
Truth table100%3/3
A || B
A= $self->bracketed('[', \&Parse::Method::Signatures::unpacked_array, $param)B= $self->bracketed('{', \&Parse::Method::Signatures::unpacked_hash, $param)
ABresult
1X1
011
000
475 $self->bracketed('{', \&unpacked_hash, $param);
476 }
477  
478 sub _param_named {
479 216 ▶ my ($self, $param) = @_;
Subroutine _param_named: called CC=4 SCAR=13.9
480  
481 return unless
482 216 ▶ $self->ppi->content eq ':' &&
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->content eq ":" and $self->ppi->next_token->isa("PPI::Token::Symbol") 171 45 216
Condition100%3/3
$self->ppi->content eq ":" and $self->ppi->next_token->isa("PPI::Token::Symbol")
!ll&&!rl&&r
167445
MC/DC100%2/2
$self->ppi->content eq ":" and $self->ppi->next_token->isa("PPI::Token::Symbol")
$self->ppi->content eq ":" $self->ppi->next_token->isa("PPI::Token::Symbol")
Truth table100%3/3
A and B
A= $self->ppi->content eq ":"B= $self->ppi->next_token->isa("PPI::Token::Symbol")
ABresult
0X0
100
111
483 $self->ppi->next_token->isa('PPI::Token::Symbol');
484  
485 45 $param->{required} = 0;
486 45 $param->{named} = 1;
487 45 $self->consume_token;
488  
489 45 my $err_ctx = $self->ppi;
490 45 $param = $self->_param_variable($param);
491  
492 $self->error($err_ctx, "Arrays or hashes cannot be named")
493 45 ▶ if $param->{sigil} ne '$';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"sigil"} ne "\$" 3 42 45
494  
495 42 return $param;
496 }
497  
498 sub _param_typed {
499 232 ▶ my ($self, $param) = @_;
Subroutine _param_typed: called CC=4 SCAR=13.9
500  
501 232 ▶ my $tc = $self->tc
Branch: 1 branch
BranchTrueFalseTotal
my $tc = $self->tc or return 201 29 230
502 or return;
503  
504  
505 29 ▶ $tc = $self->type_constraint_class->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_type_constraint_callback ? : 1 28 29
$self->has_from_namespace ? : 0 29 29
506 ppi => $tc,
507 ( $self->has_type_constraint_callback
508 ? (tc_callback => $self->type_constraint_callback)
509 : ()
510 ),
511 ( $self->has_from_namespace
512 ? ( from_namespace => $self->from_namespace )
513 : ()
514 ),
515 );
516 29 $param->{type_constraints} = $tc;
517  
518 29 return $param;
519 }
520
521 sub _param_default {
522 209 ▶ my ($self, $param) = @_;
Subroutine _param_default: called CC=5 SCAR=16.2
523  
524 209 ▶ return unless $self->ppi->content eq '=';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->content eq "=" 184 25 209
525  
526 25 $self->consume_token;
527  
528 $param->{default_value} =
529 25 ▶ $self->_consume_if_isa(qw/
Branch: 1 branch
BranchTrueFalseTotal
$param->{"default_value"} = $self->_consume_if_isa("PPI::Token::QuoteLike", "PPI::Token::Number", "PPI::Token::Quote", "PPI::Token::Symbol", "PPI::Token::Magic", "PPI::Token::ArrayIndex") || $self->bracketed("[") || $self->bracketed("{") or $self->error($self->ppi) 0 25 25
Condition83%5/6
$self->_consume_if_isa('PPI::Token::QuoteLike', 'PPI::Token::Number', 'PPI::Token::Quote', 'PPI::Token::Symbol', 'PPI::Token::Magic', 'PPI::Token::ArrayIndex') || $self->bracketed('[') || $self->bracketed('{')
l!l&&r!l&&!r
2410
$self->_consume_if_isa("PPI::Token::QuoteLike", "PPI::Token::Number", "PPI::Token::Quote", "PPI::Token::Symbol", "PPI::Token::Magic", "PPI::Token::ArrayIndex") or $self->bracketed("[")
l!l&&r!l&&!r
2311
MC/DC0%0/3
$self->_consume_if_isa('PPI::Token::QuoteLike', 'PPI::Token::Number', 'PPI::Token::Quote', 'PPI::Token::Symbol', 'PPI::Token::Magic', 'PPI::Token::ArrayIndex') || $self->bracketed('[') || $self->bracketed('{')
$self->_consume_if_isa("PPI::Token::QuoteLike", "PPI::Token::Number", "PPI::Token::Quote", "PPI::Token::Symbol", "PPI::Token::Magic", "PPI::Token::ArrayIndex") $self->bracketed("[") $self->bracketed('{')
Truth table75%3/4
A or B || C
A= $self->_consume_if_isa("PPI::Token::QuoteLike", "PPI::Token::Number", "PPI::Token::Quote", "PPI::Token::Symbol", "PPI::Token::Magic", "PPI::Token::ArrayIndex")B= $self->bracketed("[")C= $self->bracketed('{')
ABCresult
1XX1
01X1
0011
0000
530 PPI::Token::QuoteLike
531 PPI::Token::Number
532 PPI::Token::Quote
533 PPI::Token::Symbol
534 PPI::Token::Magic
535 PPI::Token::ArrayIndex
536 /) ||
537 $self->bracketed('[') ||
538 $self->bracketed('{')
539 or $self->error($self->ppi);
540
541 25 $param->{default_value} = $param->{default_value}->content;
542 }
543  
544  
545 sub _param_variable {
546 222 ▶ my ($self, $param) = @_;
Subroutine _param_variable: called CC=5 SCAR=16.1
547  
548 222 my $ppi = $self->ppi;
549 222 my $class = $ppi->class;
550 222 ▶ return unless $class eq 'PPI::Token::Symbol'
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq "PPI::Token::Symbol" or $class eq "PPI::Token::Cast" 25 197 222
Condition100%3/3
$class eq "PPI::Token::Symbol" or $class eq "PPI::Token::Cast"
l!l&&r!l&&!r
1871025
MC/DC100%2/2
$class eq "PPI::Token::Symbol" or $class eq "PPI::Token::Cast"
$class eq "PPI::Token::Symbol" $class eq "PPI::Token::Cast"
Truth table100%3/3
A or B
A= $class eq "PPI::Token::Symbol"B= $class eq "PPI::Token::Cast"
ABresult
1X1
011
000
551 || $class eq 'PPI::Token::Cast';
552  
553 197 ▶ if ($class eq 'PPI::Token::Symbol') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'PPI::Token::Symbol') { } 187 10 197
554 187 ▶ $ppi->symbol_type eq $ppi->raw_type or $self->error($ppi);
Branch: 1 branch
BranchTrueFalseTotal
$ppi->symbol_type eq $ppi->raw_type or $self->error($ppi) 1 186 187
555  
556 186 $param->{sigil} = $ppi->raw_type;
557 186 $param->{variable_name} = $self->consume_token->content;
558 } else {
559 10 $param->{sigil} = $self->consume_token->content;
560 }
561  
562 196 return $param;
563 }
564  
565 sub unpacked_hash {
566 9 ▶ my ($self, $list, $param) = @_;
Subroutine unpacked_hash: called CC=6 SCAR=18.0
Pod undocumented
567  
568 9 my $params = [];
569 9 ▶ while ($self->ppi->content ne '}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->content ne "}") 20 0 20
570 20 my $errctx = $self->ppi;
571 20 ▶ my $p = $self->param
Branch: 1 branch
BranchTrueFalseTotal
my $p = $self->param or $self->error($self->ppi) 0 20 20
572 or $self->error($self->ppi);
573  
574 20 ▶ $self->error($errctx, "Cannot have positional parameters in an unpacked-array")
Branch: 1 branch
BranchTrueFalseTotal
if $p->sigil eq "\$" and PositionalParam()->check($p) 1 19 20
Condition100%3/3
$p->sigil eq "\$" and PositionalParam()->check($p)
!ll&&!rl&&r
8111
MC/DC100%2/2
$p->sigil eq "\$" and PositionalParam()->check($p)
$p->sigil eq "\$" PositionalParam()->check($p)
Truth table100%3/3
A and B
A= $p->sigil eq "\$"B= PositionalParam()->check($p)
ABresult
0X0
100
111
575 if $p->sigil eq '$' && PositionalParam->check($p);
576 19 push @$params, $p;
577  
578 19 ▶ last if $self->ppi->content eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi->content eq "}" 8 11 19
579 11 $self->assert_token(',');
580 }
581 8 $param->{params} = $params;
582 8 $param->{sigil} = '$';
583 8 $param->{unpacking} = 'Hash';
584 8 return $param;
585 }
586  
587 sub unpacked_array {
588 12 ▶ my ($self, $list, $param) = @_;
Subroutine unpacked_array: called CC=6 SCAR=18.0
Pod undocumented
589  
590 12 my $params = [];
591 12 ▶ while ($self->ppi->content ne ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->content ne "]") 27 0 27
592 27 my $watermark = $self->ppi;
593 27 ▶ my $param = $self->param
Branch: 1 branch
BranchTrueFalseTotal
my $param = $self->param or $self->error($self->ppi) 0 26 26
594 or $self->error($self->ppi);
595  
596 26 ▶ $self->error($watermark, "Cannot have named parameters in an unpacked-array")
Branch: 1 branch
BranchTrueFalseTotal
if NamedParam()->check($param) 2 24 26
597 if NamedParam->check($param);
598  
599 24 ▶ $self->error($watermark, "Cannot have optional parameters in an unpacked-array")
Branch: 1 branch
BranchTrueFalseTotal
unless $param->required 1 23 24
600 unless $param->required;
601  
602 23 push @$params, $param;
603  
604 23 ▶ last if $self->ppi->content eq ']';
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi->content eq "]" 8 15 23
605 15 $self->assert_token(',');
606 }
607 8 $param->{params} = $params;
608 8 $param->{sigil} = '$';
609 8 $param->{unpacking} = 'Array';
610 8 return $param;
611 }
612  
613 sub tc {
614 267 ▶ my ($self, $required) = @_;
Subroutine tc: called CC=5 SCAR=16.5
Pod undocumented
615  
616 267 my $ident = $self->_ident;
617  
618 267 ▶ $ident or ($required and $self->error($self->ppi)) or return;
Branch: 1 branch
BranchTrueFalseTotal
$ident or $required and $self->error($self->ppi) or return 201 64 265
Condition66%4/6
$ident or $required and $self->error($self->ppi)
l!l&&r!l&&!r
640203
$required and $self->error($self->ppi)
!ll&&!rl&&r
20120
MC/DC0%0/3
$ident or $required and $self->error($self->ppi)
$ident $required $self->error($self->ppi)
Truth table25%1/4
A or B and C
A= $identB= $requiredC= $self->error($self->ppi)
ABCresult
1XX1
0111
00X0
0100
619  
620 64 ▶ return $self->_tc_union(
Condition66%2/3
$self->bracketed('[', \&Parse::Method::Signatures::_tc_params, $ident) || $ident->clone
l!l&&r!l&&!r
13480
MC/DC0%0/2
$self->bracketed('[', \&Parse::Method::Signatures::_tc_params, $ident) || $ident->clone
$self->bracketed('[', \&Parse::Method::Signatures::_tc_params, $ident) $ident->clone
Truth table66%2/3
A || B
A= $self->bracketed('[', \&Parse::Method::Signatures::_tc_params, $ident)B= $ident->clone
ABresult
1X1
011
000
621 $self->bracketed('[', \&_tc_params, $ident)
622 || $ident->clone
623 );
624 }
625  
626 # Handle parameterized TCs. e.g.:
627 # ArrayRef[Str]
628 # Dict[Str => Str]
629 # Dict["foo bar", Baz]
630 sub _tc_params {
631 15 ▶ my ($self, $list, $tc) = @_;
Subroutine _tc_params: called CC=4 SCAR=13.9
632  
633 15 my $new = PPI::Statement::Expression::TCParams->new($tc->clone);
634  
635 15 ▶ return $new if $self->ppi->content eq ']';
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi->content eq "]" 1 14 15
636  
637 14 $new->add_element($self->_tc_param);
638  
639 13 ▶ while ($self->ppi->content =~ /^,|=>$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->content =~ /^,|=>$/) 6 13 19
640  
641 6 my $op = $self->consume_token;
642 6 ▶ $self->_stringify_last($new) if $op->content eq '=>';
Branch: 1 branch
BranchTrueFalseTotal
if $op->content eq "=>" 4 2 6
643  
644 6 $new->add_element($self->tc(1));
645 }
646  
647 13 return $new;
648 }
649  
650 # Valid token for individual component of parameterized TC
651 sub _tc_param {
652 14 ▶ my ($self) = @_;
Subroutine _tc_param: called CC=3 SCAR=11.0
653  
654 14 (my $class = $self->ppi->class) =~ s/^PPI:://;
655 14 ▶ return $self->consume_token->clone
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Token::Number" or $class =~ /^Token::Quote::(?:Single|Double|Literal|Interpolate)/ 2 12 14
Condition100%3/3
$class eq "Token::Number" or $class =~ /^Token::Quote::(?:Single|Double|Literal|Interpolate)/
l!l&&r!l&&!r
1112
MC/DC100%2/2
$class eq "Token::Number" or $class =~ /^Token::Quote::(?:Single|Double|Literal|Interpolate)/
$class eq "Token::Number" $class =~ /^Token::Quote::(?:Single|Double|Literal|Interpolate)/
Truth table100%3/3
A or B
A= $class eq "Token::Number"B= $class =~ /^Token::Quote::(?:Single|Double|Literal|Interpolate)/
ABresult
1X1
011
000
656 if $class eq 'Token::Number' ||
657 $class =~ /^Token::Quote::(?:Single|Double|Literal|Interpolate)/;
658  
659 12 return $self->tc(1);
660 }
661  
662 sub _tc_union {
663 61 ▶ my ($self, $tc) = @_;
Subroutine _tc_union: called CC=3 SCAR=11.0
664
665 61 ▶ return $tc unless $self->ppi->content eq '|';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ppi->content eq "|" 52 9 61
666  
667 9 my $union = PPI::Statement::Expression::TCUnion->new;
668 9 $union->add_element($tc);
669 9 ▶ while ( $self->ppi->content eq '|' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->content eq "|") 9 8 17
670
671 9 $self->consume_token;
672 9 $union->add_element($self->tc(1));
673 }
674  
675 8 return $union;
676 }
677  
678 # Stringify LHS of fat comma
679 sub _stringify_last {
680 4 ▶ my ($self, $list) = @_;
Subroutine _stringify_last: called CC=2 SCAR=6.9
681 4 my $last = $list->last_token;
682 4 ▶ return unless $last->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $last->isa("PPI::Token::Word") 1 3 4
683  
684 # Is this conditional on the content of the word?
685 3 bless $last, "PPI::Token::StringifiedWord";
686 3 return $list;
687 }
688  
689 # Handle the boring bits of bracketed product, then call $code->($self, ...)
690 sub bracketed {
691 131 ▶ my ($self, $type, $code, @args) = @_;
Subroutine bracketed: called CC=5 SCAR=16.1
Pod undocumented
692  
693 131 local $ERROR_LEVEL = $ERROR_LEVEL + 1;
694 131 my $ppi = $self->ppi;
695 131 ▶ return unless $ppi->content eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $ppi->content eq $type 91 40 131
696  
697 40 $self->consume_token; # consume '[';
698  
699 # Get from the '[' token the to Strucure::Constructor
700 40 $ppi = $ppi->parent;
701  
702 40 ▶ $ppi->finish or $self->error($ppi,
Branch: 1 branch
BranchTrueFalseTotal
$ppi->finish or $self->error($ppi, q[Runaway '] . $ppi->braces . q[' in ] . $self->_parsing_area(1), 1) 2 38 40
703 "Runaway '" . $ppi->braces . "' in " . $self->_parsing_area(1), 1);
704  
705  
706 38 my $ret;
707 38 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 36 2 38
708 36 my $list = PPI::Structure::Constructor->new($ppi->start->clone);
709 36 $ret = $code->($self, $list, @args);
710  
711 30 ▶ $self->error($self->ppi)
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi != $ppi->finish 1 29 30
712 if $self->ppi != $ppi->finish;
713  
714 # There is no public way to do this as of PPI 1.204_06. I'll add one to the
715 # next release, 1.205 (or so)
716 29 $list->{finish} = $self->consume_token->clone;
717 } else {
718 # Just clone the entire [] or {}
719 2 $ret = $ppi->clone;
720 2 $self->_set_ppi($ppi->finish);
721 2 $self->consume_token;
722 }
723  
724 31 return $ret;
725 }
726  
727 # Work out what sort of production we are in for sane default error messages
728 sub _parsing_area {
729 19 ▶ shift;
Subroutine _parsing_area: called CC=6 SCAR=18.2
730 19 ▶ my $height = shift || 0;
Condition50%1/2
shift() || 0
l!l
190
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
731 19 my (undef, undef, undef, $sub) = caller($height+$ERROR_LEVEL);
732  
733 19 ▶ return "type constraint" if $sub =~ /(?:\b|_)tc(?:\b|_)/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /(?:\b|_)tc(?:\b|_)/ 4 15 19
734 15 ▶ return "unpacked parameter"
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /(?:\b|_)unpacked(?:\b|_)/ 1 14 15
735 if $sub =~ /(?:\b|_)unpacked(?:\b|_)/;
736 14 ▶ return "parameter" if $sub =~ /(?:\b|_)param(?:\b|_)/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /(?:\b|_)param(?:\b|_)/ 5 9 14
737 9 ▶ return "signature" if $sub =~ /(?:\b|_)signature(?:\b|_)/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /(?:\b|_)signature(?:\b|_)/ 9 0 9
738  
739 0 " unknown production ($sub)";
740 }
741  
742 # error(PPI::Token $token, Str $msg?, Bool $no_in = 0)
743 sub error {
744 30 ▶ my ($self, $token, $msg, $no_in) = @_;
Subroutine error: called CC=4 SCAR=14.0
Pod undocumented
745  
746 30 ▶ $msg = "Error parsing " . $self->_parsing_area(2)
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 7 23 30
747 unless ($msg);
748  
749  
750 30 ▶ $msg = $msg . " near '$token'" .
Branch: 1 branch
BranchTrueFalseTotal
$no_in ? : 3 27 30
751 ($no_in ? ""
752 : " in '" . $token->statement . "'"
753 );
754  
755 30 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Method::Signatures::DEBUG) { } 0 30 30
756 0 Carp::confess($msg);
757 } else {
758 30 Carp::croak($msg);
759 }
760 }
761  
762 sub assert_token {
763 265 ▶ my ($self, $need, $msg) = @_;
Subroutine assert_token: called CC=2 SCAR=6.9
Pod undocumented
764  
765 265 ▶ if ($self->ppi->content ne $need) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ppi->content ne $need) 9 256 265
766 9 $self->error($self->ppi, "'$need' expected whilst parsing " . $self->_parsing_area(2));
767 }
768 256 return $self->consume_token;
769 }
770  
771  
772 3 %LEXTABLE = (
773 where => 'WHERE',
774 is => 'TRAIT',
775 does => 'TRAIT',
776 );
777  
778 sub _ident {
779 270 ▶ my ($self) = @_;
Subroutine _ident: called CC=2 SCAR=6.9
780  
781 270 my $ppi = $self->ppi;
782 270 ▶ return $self->consume_token
Branch: 1 branch
BranchTrueFalseTotal
if $ppi->class eq "PPI::Token::Word" 66 204 270
783 if $ppi->class eq 'PPI::Token::Word';
784 204 return undef;
785 }
786  
787 sub _consume_if_isa {
788 25 ▶ my ($self, @classes) = @_;
Subroutine _consume_if_isa: called CC=3 SCAR=11.0
789  
790 25 for (@classes) {
791 74 ▶ return $self->consume_token
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi->isa($_) 23 51 74
792 if $self->ppi->isa($_);
793 }
794  
795 }
796  
797 sub consume_token {
798 833 ▶ my ($self) = @_;
Subroutine consume_token: called CC=5 SCAR=16.1
Pod undocumented
799  
800 833 my $ppi = $self->ppi;
801 833 my $ret = $ppi;
802  
803 833 ▶ while (!$ppi->isa('PPI::Token::EOF') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ppi->isa("PPI::Token::EOF")) 1052 0 1052
804 1052 $ppi = $ppi->next_token;
805 1052 ▶ last if $ppi->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $ppi->significant 833 219 1052
806 }
807  
808 833 ▶ if ($ppi->class eq 'PPI::Token::Word' && exists $LEXTABLE{"$ppi"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppi->class eq "PPI::Token::Word" and exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}) 22 811 833
Condition100%3/3
$ppi->class eq "PPI::Token::Word" and exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
!ll&&!rl&&r
7298222
MC/DC100%2/2
$ppi->class eq "PPI::Token::Word" and exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
$ppi->class eq "PPI::Token::Word" exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
Truth table100%3/3
A and B
A= $ppi->class eq "PPI::Token::Word"B= exists $Parse::Method::Signatures::LEXTABLE{"$ppi"}
ABresult
0X0
100
111
809 22 bless $ppi, "PPI::Token::LexSymbol";
810 22 $ppi->{lex} = $LEXTABLE{"$ppi"};
811 }
812 833 $self->_set_ppi( $ppi );
813 833 return $ret;
814 }
815  
816 sub remaining_input {
817 0 ▶ my $tok = $_[0]->ppi;
Subroutine remaining_input: not called CC=2 SCAR=17.9
Pod undocumented
818 0 my $buff;
819  
820 0 ▶ while ( !$tok->isa('PPI::Token::EOF') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tok->isa("PPI::Token::EOF")) 0 0 0
821 0 $buff .= $tok->content;
822 0 $tok = $tok->next_token;
823 }
824 0 return $buff;
825 }
826  
827 3 __PACKAGE__->meta->make_immutable;
828  
829  
830 # Extra PPI classes to represent what we want.
831 { package
832 PPI::Statement::Expression::TCUnion;
833 3 ▶ use base 'PPI::Statement::Expression';
Subroutine BEGIN: called CC=1 SCAR=0.0
834  
835 sub content {
836 6 ▶ join('|', $_[0]->children );
Subroutine content: called CC=1 SCAR=0.0
837 }
838 }
839  
840 3 { package
841 PPI::Statement::Expression::TCParams;
842
843 3 ▶ use base 'PPI::Statement::Expression';
Subroutine BEGIN: called CC=1 SCAR=0.0
844 3 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
845  
846 # $self->children stores everything so PPI can track parents
847 # params just contains the keywords (not commas) inside the []
848 3 has type => ( is => 'ro');
849 has params => (
850 is => 'ro',
851 15 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
852 3 );
853  
854 sub new {
855 15 ▶ my ($class, $type) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
856  
857 15 return $class->meta->new_object(
858 __INSTANCE__ => $class->SUPER::new($type),
859 type => $type
860 );
861 };
862  
863 override add_element => sub {
864 19 ▶ my ($self, $ele) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
865 19 super();
866 19 push @{$self->params}, $ele;
867 3 };
868  
869 sub content {
870 10 ▶ $_[0]->type->content . '[' . join(',', @{$_[0]->params}) . ']'
Subroutine content: called CC=1 SCAR=0.0
Pod documented
871 }
872  
873 3 ▶ no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
874 }
875  
876 3 { package
877 PPI::Token::LexSymbol;
878 3 ▶ use base 'PPI::Token::Word';
Subroutine BEGIN: called CC=1 SCAR=0.0
879  
880 sub lex {
881 31 ▶ my ($self) = @_;
Subroutine lex: called CC=1 SCAR=0.0
882 return $self->{lex}
883 31 }
884 }
885  
886 # Used for LHS of fat comma
887 3 { package
888 PPI::Token::StringifiedWord;
889 3 ▶ use base 'PPI::Token::Word';
Subroutine BEGIN: called CC=1 SCAR=0.0
890  
891 3 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
892 override content => sub {
893 2 ▶ return '"' . super() . '"';
Subroutine __ANON__: called CC=1 SCAR=0.0
894 3 };
895  
896 sub string {
897 1 ▶ return $_[0]->PPI::Token::Word::content();
Subroutine string: called CC=1 SCAR=0.0
Pod undocumented
898 }
899 3 ▶ no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
900 }
901  
902 3 1;
903  
904 __END__
905  
906 =head1 NAME
907  
908 Parse::Method::Signatures - Perl6 like method signature parser
909  
910 =head1 DESCRIPTION
911  
912 Inspired by L<Perl6::Signature> but streamlined to just support the subset
913 deemed useful for L<TryCatch> and L<MooseX::Method::Signatures>.
914  
915 =head1 TODO
916  
917 =over
918  
919 =item * Document the parameter return types.
920  
921 =item * Probably lots of other things
922  
923 =back
924  
925 =head1 METHODS
926  
927 There are only two public methods to this module, both of which should be
928 called as class methods. Both methods accept either a single (non-ref) scalar
929 as the value for the L</input> attribute, or normal new style arguments (hash
930 or hash-ref).
931  
932 =head2 signature
933  
934 my $sig = Parse::Method::Signatures->signature( '(Str $foo)' )
935  
936 Attempts to parse the (bracketed) method signature. Returns a value or croaks
937 on error.
938  
939 =head2 param
940  
941 my $param = Parse::Method::Signatures->param( 'Str $foo where { length($_) < 10 }')
942  
943 Attempts to parse the specification for a single parameter. Returns value or
944 croaks on error.
945  
946 =head1 ATTRIBUTES
947  
948 All the attributes on this class are read-only.
949  
950 =head2 input
951  
952 B<Type:> Str
953  
954 The string to parse.
955  
956 =head2 offset
957  
958 B<Type:> Int
959  
960 Offset into L</input> at which to start parsing. Useful for using with
961 Devel::Declare linestring
962  
963 =head2 signature_class
964  
965 B<Default:> Parse::Method::Signatures::Sig
966  
967 B<Type:> Str (loaded on demand class name)
968  
969 =head2 param_class
970  
971 B<Default:> Parse::Method::Signatures::Param
972  
973 B<Type:> Str (loaded on demand class name)
974  
975 =head2 type_constraint_class
976  
977 B<Default:> L<Parse::Method::Signatures::TypeConstraint>
978  
979 B<Type:> Str (loaded on demand class name)
980  
981 Class that is used to turn the parsed type constraint into an actual
982 L<Moose::Meta::TypeConstraint> object.
983  
984 =head2 from_namespace
985  
986 B<Type:> ClassName
987  
988 Let this module know which package it is parsing signatures form. This is
989 entirely optional, and the only effect is has is on parsing type constraints.
990  
991 If this attribute is set it is passed to L</type_constraint_class> which can
992 use it to introspect the package (commonly for L<MooseX::Types> exported
993 types). See
994 L<Parse::Method::Signature::TypeConstraints/find_registered_constraint> for
995 more details.
996  
997 =head2 type_constraint_callback
998  
999 B<Type:> CodeRef
1000  
1001 Passed to the constructor of L</type_constraint_class>. Default implementation
1002 of this callback asks Moose for a type constrain matching the name passed in.
1003 If you have more complex requirements, such as parsing types created by
1004 L<MooseX::Types> then you will want a callback similar to this:
1005  
1006 # my $target_package defined elsewhere.
1007 my $tc_cb = sub {
1008 my ($pms_tc, $name) = @_;
1009 my $code = $target_package->can($name);
1010 $code ? eval { $code->() }
1011 : $pms_tc->find_registered_constraint($name);
1012 }
1013  
1014 Note that the above example is better provided by providing the
1015 L</from_namespace> attribute.
1016  
1017 =head1 CAVEATS
1018  
1019 Like Perl6::Signature, the parsing of certain constructs is currently only a
1020 'best effort' - specifically default values and where code blocks might not
1021 successfully for certain complex cases. Patches/Failing tests welcome.
1022  
1023 Additionally, default value specifications are not evaluated which means that
1024 no such lexical or similar errors will not be produced by this module.
1025 Constant folding will also not be performed.
1026  
1027 There are certain constructs that are simply too much hassle to avoid when the
1028 work around is simple. Currently the only cases that are known to parse wrong
1029 are when using anonymous variables (i.e. just sigils) in unpacked arrays. Take
1030 the following example:
1031  
1032 method foo (ArrayRef [$, $], $some_value_we_care_about) {
1033  
1034 In this case the C<$]> is treated as one of perl's magic variables
1035 (specifically, the patch level of the Perl interpreter) rather than a C<$>
1036 followed by a C<]> as was almost certainly intended. The work around for this
1037 is simple: introduce a space between the characters:
1038  
1039 method foo (ArrayRef [ $, $ ], $some_value_we_care_about) {
1040  
1041 The same applies
1042  
1043 =head1 AUTHOR
1044  
1045 Ash Berlin <ash@cpan.org>.
1046  
1047 Thanks to Florian Ragwitz <rafl@debian.org>.
1048  
1049 Many thanks to Piers Cawley to showing me the way to refactor my spaghetti
1050 code into something more manageable.
1051  
1052 =head1 SEE ALSO
1053  
1054 L<Devel::Declare> which is used by most modules that use this (currently by
1055 all modules known to the author.)
1056  
1057 L<http://github.com/ashb/trycatch/tree>.
1058  
1059 =head1 LICENSE
1060  
1061 Licensed under the same terms as Perl itself.
1062  
1063 This distribution copyright 2008-2009, Ash Berlin <ash@cpan.org>
1064  
« Signatures/TypeConstraint.pm ↑ Summary Signatures/Sig.pm »