Signature/Val.pm

filter: Statementstmt 94.3% 84 / 89 Branchbran 86.9% 40 / 46 Conditioncond 69.2% 9 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 85.6% 143 / 167 CC 31 SCAR 28.1
CC
31
Cov
90%
to_string
23.6
find_named_param
12.0
to_string
7.0
CRAP 16.7
↑ Summary Signature.pm »
1 package Perl6::Signature::Val;
2  
3 1 ▶use Moose 0.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Signature AST base. Add debug methods here?
6  
7 package Perl6::Signature::Val::Sig;
8  
9 =begin Pugscode
10  
11 |-- AST for function signature.
12  
13 data Sig = MkSig
14 { s_invocant :: Maybe Param
15 , s_requiredPositionalCount :: Int
16 , s_requiredNames :: Set ID
17 , s_positionalList :: [Param]
18 , s_namedSet :: Map.Map ID Param
19 , s_slurpyScalarList :: [Param]
20 , s_slurpyArray :: Maybe Param
21 , s_slurpyHash :: Maybe Param
22 , s_slurpyCode :: Maybe Param
23 , s_slurpyCapture :: Maybe Param
24 }
25  
26 =cut
27  
28 # XXX: L<S06/"Bindings happen in declaration order, not call order">
29 # suggests that requiredNames should be modeled with a list, not a set,
30 # and that the compiler convert mandatory named args to positionals.
31 # Fix this sometime.
32  
33 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 extends 'Perl6::Signature::Val';
35  
36 # Sorry, Haskell style is the only sane way I know to format these.
37 # :,/^$/s/^ *(/ (/
38 # :,/^$/s/^ *,/ ,/
39 # :,/^$/s/^ *)/ )/
40 1 has 's_invocant' =>
41 ( isa => 'Perl6::Signature::Val::SigParam'
42 , is => 'rw'
43 , required => 0
44 );
45 1 has 's_requiredPositionalCount' =>
46 ( isa => 'Int'
47 , is => 'rw'
48 );
49 1 has 's_requiredNames' =>
50 ( isa => 'HashRef' # Set of names
51 , is => 'rw'
52 );
53 1 has 's_positionalList' =>
54 ( isa => 'ArrayRef[Perl6::Signature::Val::SigParam]'
55 , is => 'rw'
56 );
57 1 has 's_namedList' =>
58 ( isa => 'ArrayRef[Perl6::Signature::Val::SigParam]'
59 , is => 'rw'
60 );
61 1 has 's_slurpyScalarList' =>
62 ( isa => 'ArrayRef[Perl6::Signature::Val::SigParam]'
63 , is => 'rw'
64 , required => 0
65 , predicate => "has_s_slurpyScalarList"
66 );
67 1 has 's_slurpyArray' =>
68 ( isa => 'Perl6::Signature::Val::SigParam'
69 , is => 'rw'
70 , required => 0
71 , predicate => "has_s_slurpyArray"
72 );
73 1 has 's_slurpyHash' =>
74 ( isa => 'Perl6::Signature::Val::SigParam'
75 , is => 'rw'
76 , required => 0
77 , predicate => "has_s_slurpyHash"
78 );
79 1 has 's_slurpyCode' =>
80 ( isa => 'Perl6::Signature::Val::SigParam'
81 , is => 'rw'
82 , required => 0
83 , predicate => "has_s_slurpyCode"
84 );
85 1 has 's_slurpyCapture' =>
86 ( isa => 'Perl6::Signature::Val::SigParam'
87 , is => 'rw'
88 , required => 0
89 , predicate => "has_s_slurpyCapture"
90 );
91  
92 0 ▶sub find_named_param {
Subroutine find_named_param: not called CC=3 SCAR=24.8
Pod undocumented
93 0 my($self, $label) = @_;
94 0 for my $param (@{ $self->s_namedList }) {
95 0 ▶ return $param if $param->p_label eq $label;
Branch: 1 branch
BranchTrueFalseTotal
if $param->p_label eq $label 0 0 0
96 }
97 0 return; # Or should this die?
98 }
99  
100 45 ▶sub to_string {
Subroutine to_string: called CC=7 SCAR=19.5
Pod undocumented
101 45 my($self) = @_;
102  
103 45 my $inv_str;
104 45 ▶ if (my $inv = $self->s_invocant) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inv = $self->s_invocant) 5 40 45
105 5 $inv_str = $inv->to_string . ":";
106 }
107  
108 45 my @params;
109 45 my $positionals = $self->s_positionalList;
110 45 for my $i (0 .. $#$positionals) {
111 36 push @params, $positionals->[$i]->to_string(
112 required => $i < $self->s_requiredPositionalCount);
113 }
114 push @params, map {
115 $_->to_string( style => 'named'
116 14 , required => exists $self->s_requiredNames->{$_->p_label}
117 )
118 45 } @{ $self->s_namedList };
119  
120 45 ▶ push @params, '*' . $self->s_slurpyArray->to_string if
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_s_slurpyArray 2 43 45
121 $self->has_s_slurpyArray;
122 45 ▶ push @params, '*' . $self->s_slurpyHash->to_string if
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_s_slurpyHash 2 43 45
123 $self->has_s_slurpyHash;
124  
125 45 ▶ return ":(" .
Branch: 2 branches
BranchTrueFalseTotal
$inv_str ? : 5 40 45
@params ? : 40 5 45
126 join(" ", ($inv_str ? $inv_str : ()),
127 (@params ? join(", ", @params) : ())) .
128 ")";
129 }
130  
131 package Perl6::Signature::Val::SigParam;
132  
133 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 extends 'Perl6::Signature::Val';
135  
136 =begin Pugscode
137  
138 -- | Single parameter for a function or method, e.g.:
139 -- Elk $m where { $m.antlers ~~ Velvet }
140 {-|
141 A formal parameter of a sub (or other callable).
142  
143 These represent declared parameters; don't confuse them with actual argument
144 values.
145 -}
146 data SigParam = MkParam
147 { p_variable :: Var -- ^ E.g. $m above
148 , p_types :: [Types.Type] -- ^ Static pieces of inferencer-food
149 -- E.g. Elk above
150 , p_constraints :: [Code] -- ^ Dynamic pieces of runtime-mood
151 -- E.g. where {...} above
152 , p_unpacking :: Maybe PureSig -- ^ E.g. BinTree $t (Left $l, Right $r)
153 , p_default :: ParamDefault -- ^ E.g. $answer? = 42
154 , p_label :: ID -- ^ The external name for the param ('m' above)
155 , p_slots :: Table -- ^ Any additional attrib not
156 -- explicitly mentioned below
157 , p_hasAccess :: ParamAccess -- ^ is ro, is rw, is copy
158 , p_isRef :: Bool -- ^ must be true if hasAccess = AccessRW
159 , p_isContext :: Bool -- ^ "is context"
160 , p_isLazy :: Bool
161 }
162  
163 =cut
164  
165 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 1 enum __PACKAGE__ . "::Access" => [ qw(rw ro copy) ];
168 1 enum __PACKAGE__ . "::Sigil" => [ qw($ % @ &) ];
169  
170 1 has 'p_variable' => ( is => 'rw', isa => 'Str' );
171 1 has 'p_types' => ( is => 'rw', isa => 'ArrayRef' ); # of types
172 # I don't actually remember why this isn't a scalar :(
173 1 has 'p_constraints' => ( is => 'rw', isa => 'ArrayRef' ); # of code
174 1 has 'p_unpacking' => ( isa => 'Perl6::Signature::Val::Sig|Undef'
175 , is => 'rw'
176 , required => 0
177 );
178 1 has 'p_default' => ( is => 'rw', required => 0 );
179 1 has 'p_label' => ( is => 'rw', isa => 'Str' );
180 1 has 'p_slots' => ( is => 'rw', isa => 'HashRef' );
181 1 has 'p_hasAccess' => ( isa => __PACKAGE__ . "::Access"
182 , is => 'rw'
183 , default => "ro"
184 );
185 1 has 'p_isRef' => ( is => 'rw', isa => 'Bool' );
186 1 has 'p_isContext' => ( is => 'rw', isa => 'Bool' );
187 1 has 'p_isLazy' => ( is => 'rw', isa => 'Bool' );
188  
189 has 'p_sigil' => ( isa => __PACKAGE__ . "::Sigil"
190 , is => "ro"
191 , lazy => 1
192 4 ▶ , default => sub { substr( shift->p_variable, 0, 1 ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
193 1 );
194  
195 1 my %quoted_slots = map { $_ => 1 } qw(ro rw copy ref context lazy);
196  
197 59 ▶sub to_string {
Subroutine to_string: called CC=23 SCAR=31.6
Pod undocumented
198 59 my($self, %args) = @_;
199  
200 59 ▶ $args{required} = 1 if not exists $args{required};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"required"} 9 50 59
201 59 ▶ $args{style} = 'positional' if not exists $args{style};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"style"} 45 14 59
202  
203 die "required param can't have a default value" if
204 59 ▶ $args{required} && $self->p_default;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"required"} and $self->p_default 0 59 59
Condition66%2/3
$args{"required"} and $self->p_default
!ll&&!rl&&r
18410
MC/DC0%0/2
$args{"required"} and $self->p_default
$args{"required"} $self->p_default
Truth table66%2/3
A and B
A= $args{"required"}B= $self->p_default
ABresult
0X0
100
111
205  
206 59 my $ident;
207 59 ▶ if ($args{style} eq 'positional') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'style'} eq 'positional') { } 45 14 59
208 45 $ident = $self->p_variable;
209 45 ▶ $ident .= "?" if !$args{required} && not defined $self->p_default;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"required"} or defined $self->p_default 3 42 45
Condition100%3/3
$args{"required"} or defined $self->p_default
l!l&&r!l&&!r
3933
MC/DC100%2/2
$args{"required"} or defined $self->p_default
$args{"required"} defined $self->p_default
Truth table100%3/3
A or B
A= $args{"required"}B= defined $self->p_default
ABresult
1X1
011
000
210 } else {
211 # TODO: implement a Perl6::...::Variable::basename
212 14 my($label, $variable) = ($self->p_label, $self->p_variable);
213 14 ▶ $ident = ":" . (($variable =~ /^.\Q$label\E$/) ?
Branch: 1 branch
BranchTrueFalseTotal
$variable =~ /^.\Q$label\E$/ ? : 8 6 14
214 $variable : "$label($variable)");
215 14 ▶ $ident .= "!" if $args{required};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"required"} 2 12 14
216 }
217  
218 59 ▶ my $default = "= " . $self->p_default if $self->p_default;
Branch: 1 branch
BranchTrueFalseTotal
if $self->p_default 9 50 59
219  
220 59 ▶ my $p_slots = $self->p_slots || {};
Condition50%1/2
$self->p_slots || {}
l!l
590
MC/DC0%0/1
$self->p_slots || {}
$self->p_slots
Truth table50%1/2
A
A= $self->p_slots
Aresult
11
00
221 59 my @slots;
222 59 ▶ push @slots, "is " . $self->p_hasAccess if $self->p_hasAccess ne 'ro';
Branch: 1 branch
BranchTrueFalseTotal
if $self->p_hasAccess ne "ro" 1 58 59
223 59 ▶ push @slots, "is ref" if $self->p_isRef;
Branch: 1 branch
BranchTrueFalseTotal
if $self->p_isRef 1 58 59
224 59 ▶ push @slots, "is context" if $self->p_isContext;
Branch: 1 branch
BranchTrueFalseTotal
if $self->p_isContext 0 59 59
225 59 ▶ push @slots, "is lazy" if $self->p_isLazy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->p_isLazy 1 58 59
226 push @slots, map {
227 59 ▶ my $qkey = $quoted_slots{$_} ? "'$_'" : $_;
Branch: 1 branch
BranchTrueFalseTotal
$quoted_slots{$_} ? : 0 1 1
228 1 ▶ my $val = defined $p_slots->{$_} && $p_slots->{$_} == 1 ?
Branch: 1 branch
BranchTrueFalseTotal
defined $p_slots->{$_} && $p_slots->{$_} == 1 ? : 1 0 1
Condition33%1/3
defined $p_slots->{$_} and $p_slots->{$_} == 1
!ll&&!rl&&r
001
MC/DC0%0/2
defined $p_slots->{$_} and $p_slots->{$_} == 1
defined $p_slots->{$_} $p_slots->{$_} == 1
Truth table33%1/3
A and B
A= defined $p_slots->{$_}B= $p_slots->{$_} == 1
ABresult
0X0
100
111
229 "" : "<$p_slots->{$_}>";
230 1 "is $qkey$val" } keys %$p_slots;
231  
232 59 ▶ my @constraints = map { "where $_" } @{ $self->p_constraints || [] };
Condition100%2/2
$self->p_constraints || []
l!l
257
MC/DC100%1/1
$self->p_constraints || []
$self->p_constraints
Truth table100%2/2
A
A= $self->p_constraints
Aresult
11
00
233  
234 return join(" ",
235 59 ▶ (@{ $self->p_types } ? join("|", @{ $self->p_types }) : ()),
Branch: 3 branches
BranchTrueFalseTotal
@{$self->p_types;} ? : 5 54 59
$self->p_unpacking ? : 2 57 59
$default ? : 9 50 59
236 $ident,
237 ($self->p_unpacking ? $self->p_unpacking->to_string : ()),
238 ($default ? $default : ()),
239 @slots,
240 @constraints);
241 }
242  
243 1 6;
244  
↑ Summary Signature.pm »