support/SigParse.yp

filter: Statementstmt 92.7% 89 / 96 Branchbran 86.1% 31 / 36 Conditioncond 100% 13 / 13 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 94.4% 17 / 18 Podpod 50.0% 1 / 2 totaltotal 91.2% 156 / 171 CC 25 SCAR 18.1
CC
25
Cov
92%
_next_token
14.0
_report_error
4.0
Parse
3.1
CRAP 6.1
« blib/lib/Sub/Multi/Tiny.pm ↑ Summary blib/lib/Sub/Multi/Tiny/Dispatcher/TypeParams.pm »
1 # SigParse.yp: Parse::Yapp input to parse signatures in Sub::Multi::Tiny
2  
3 #############################################################################
4 # Header
5  
6 %{
7  
8 # Imports {{{1
9  
10 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Text::Balanced qw(extract_codeblock);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Types of constraints we've seen - bit offsets
17 use enum
18 # Flags set by the parser
19 8 ▶ 'SEEN_WHERE', # `where` clause
Subroutine BEGIN: called CC=1 SCAR=0.0
20 'SEEN_TYPE', # Type constraint
21 'SEEN_POS', # Positional argument
22 'SEEN_NAMED', # Named argument
23 # future: SEEN_LITERAL for signatures holding literal values
24 # instead of name matches.
25  
26 # Flags set by later processing
27 'HAS_MULTIPLE_ARITIES', # Set if there are at least two
28 # different positional arities in a
29 # set of impls
30 8 ;
31  
32 # Set bits in YYData->{SEEN}
33 45 ▶sub _seen {
Subroutine _seen: called CC=2 SCAR=6.9
34 45 vec($_[0]->YYData->{SEEN}, $_[$_], 1) = 1 foreach 1..$#_;
35 }
36  
37 # }}}1
38 # Documentation {{{1
39  
40 =head1 NAME
41  
42 Sub::Multi::Tiny::SigParse - Parse::Yapp input to parse signatures in Sub::Multi::Tiny
43  
44 =head1 SYNOPSIS
45  
46 Generate the .pm file:
47  
48 yapp -m Sub::Multi::Tiny::SigParse -o lib/Sub/Multi/Tiny/SigParse.pm support/SigParse.yp
49  
50 And then:
51  
52 use Sub::Multi::Tiny::SigParse;
53 my $ast = Sub::Multi::Tiny::SigParse::Parse($signature);
54  
55 =head1 FUNCTIONS
56  
57 =cut
58  
59 # }}}1
60  
61 %}
62  
63 26 ▶#############################################################################
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
64 26 # Token and precedence definitions
65 26 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 26 26
66 # TODO: slurpies (prefix *, +); trailing ?, !
67  
68 # Separator (usually a comma)
69 %token SEPAR
70  
71 # Type before a variable name
72 %token TYPE
73  
74 # Parameter, named or positional
75 %token PARAM
76  
77 # "where BLOCK"
78 %token WHERE
79  
80 %%
81  
82 #############################################################################
83 # Rules
84  
85 signature:
86 2 ▶ { [] } # always return arrayref
Subroutine __ANON__: called CC=1 SCAR=0.0
87 23 ▶ | parameter { [ $_[1] ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
88 7 ▶ | parameter SEPAR signature { [ $_[1], @{$_[3]} ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
89 # Permit trailing comma
90 0 ▶ | parameter SEPAR signature SEPAR { [ $_[1], @{$_[3]} ] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 ;
92  
93 parameter:
94 PARAM
95 15 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 15 ▶ _seen $_[0], $_[1]->{named} ? SEEN_NAMED : SEEN_POS;
Branch: 1 branch
BranchTrueFalseTotal
$_[1]{'named'} ? : 4 11 15
97 15 return $_[1];
98 }
99  
100 | PARAM WHERE
101 4 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
102 4 ▶ _seen $_[0], $_[1]->{named} ? SEEN_NAMED : SEEN_POS;
Branch: 1 branch
BranchTrueFalseTotal
$_[1]{'named'} ? : 1 3 4
103 4 _seen $_[0], SEEN_WHERE;
104 4 return +{%{$_[1]}, where=>$_[2]};
105 }
106  
107 | TYPE PARAM
108 6 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
109 6 ▶ _seen $_[0], $_[2]->{named} ? SEEN_NAMED : SEEN_POS;
Branch: 1 branch
BranchTrueFalseTotal
$_[2]{'named'} ? : 1 5 6
110 6 _seen $_[0], SEEN_TYPE;
111 6 return +{%{$_[2]}, type => $_[1]};
112 }
113  
114 | TYPE PARAM WHERE
115 5 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
116 5 ▶ _seen $_[0], $_[2]->{named} ? SEEN_NAMED : SEEN_POS;
Branch: 1 branch
BranchTrueFalseTotal
$_[2]{'named'} ? : 3 2 5
117 5 _seen $_[0], SEEN_TYPE, SEEN_WHERE;
118 5 return +{%{$_[2]}, where=>$_[3], type => $_[1]}
119 }
120 26 ;
121  
122 %%
123 26  
124 #############################################################################
125 # Footer
126  
127 # Tokenizer and error-reporting routine for Parse::Yapp {{{1
128  
129 # The lexer
130 155 ▶sub _next_token {
Subroutine _next_token: called CC=14 SCAR=26.4
131 155 my $parser = shift;
132 155 my $text = $parser->YYData->{TEXT};
133  
134 155 $$text =~ m/\G\s+/gc; # Skip H and V whitespace
135 155 $parser->YYData->{CURR_TOK_POS} = pos($$text);
136  
137 155 ▶ return ('', undef) unless (pos($$text)||0) < length($$text); # EOF
Branch: 1 branch
BranchTrueFalseTotal
unless (pos $$text || 0) < length $$text 30 125 155
Condition100%2/2
pos $$text || 0
l!l
9659
MC/DC100%1/1
pos $$text || 0
pos $$text
Truth table100%2/2
A
A= pos $$text
Aresult
11
00
138  
139 125 ▶ $$text =~ m/\G,/gc and return (SEPAR => 0); # 0 is a dummy value
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\G,/cg and return "SEPAR", 0 20 105 125
140  
141 105 ▶ if($$text =~ m/\G([:]?)([\$\@\%\&\*]\w+)\b([?!]?)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G([:]?)([\$\@\%\&\*]\w+)\b([?!]?)/cg) 45 60 105
142 45 my $retval = {};
143 45 $retval->{name} = $2;
144 45 $retval->{named} = !!$1;
145 $retval->{reqd} = (
146 ($retval->{named} && $3 eq '!') || # Named: optional unless !
147 45 ▶ (!$retval->{named} && $3 ne '?') # Positional: reqd unless ?
Condition100%9/9
$retval->{'named'} && $3 eq '!' || !$retval->{'named'} && $3 ne '?'
l!l&&r!l&&!r
12816
$retval->{"named"} and $3 eq "!"
!ll&&!rl&&r
29151
not $retval->{"named"} and $3 ne "?"
!ll&&!rl&&r
15128
MC/DC75%3/4
$retval->{'named'} && $3 eq '!' || !$retval->{'named'} && $3 ne '?'
$retval->{"named"} $3 eq "!" not $retval->{"named"} $3 ne "?"
Truth table57%4/7
A and B || not(C) and D
A= $retval->{"named"}B= $3 eq "!"C= not $retval->{"named"}D= $3 ne "?"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
148 );
149 45 return (PARAM => $retval);
150 }
151  
152 60 ▶ if($$text =~ m/\Gwhere\s*\{/gci) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\Gwhere\s*\{/cig) 18 42 60
153 18 pos($$text) -= 1; # Get the lbrace back
154 18 my ($block) = extract_codeblock($$text); # Updates pos()
155 18 ▶ return (WHERE => $block) if defined $block;
Branch: 1 branch
BranchTrueFalseTotal
if defined $block 17 1 18
156 1 die "Saw a 'where' without a valid block after it";
157 }
158  
159 # Permit braced expressions for complex type checks
160 42 ▶ if($$text =~ m/\G\{/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\{/cg) 15 27 42
161 15 pos($$text) -= 1; # Get the lbrace back
162 15 my ($block) = extract_codeblock($$text); # Updates pos()
163 15 ▶ return (TYPE => $block) if defined $block;
Branch: 1 branch
BranchTrueFalseTotal
if defined $block 11 4 15
164 4 die "Saw an opening brace without a valid block after it";
165 }
166  
167 # If the next thing is a backslash, die --- prohibit backslash to start
168 # a typecheck as a guard against '' vs "" confusion. If you want a
169 # backslash, use the {} form.
170 27 ▶ if($$text =~ m{\G\\}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\\/cg) 1 26 27
171 1 die "Saw a backslash where I don't know what to do with it! ('' vs \"\" confusion?)";
172 }
173  
174 # Otherwise, assume a single word is a typecheck
175 26 ▶ $$text =~ m/\G(\S+)/gc and return (TYPE => $1);
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\G(\S+)/cg and return "TYPE", $1 26 0 26
176  
177 0 die "This should never happen! Unlexable text was: " .
178 substr($$text, pos($$text));
179 } #_next_token()
180  
181 # Report an error
182 2 ▶sub _report_error {
Subroutine _report_error: called CC=4 SCAR=13.9
183 2 my $parser = shift;
184 2 my $startpos = $parser->YYData->{CURR_TOK_POS};
185 2 my $endpos = pos(${ $parser->YYData->{TEXT} });
186 2 ▶ my $got = $parser->YYCurtok || '<end of input>';
Condition100%2/2
$parser->YYCurtok || '<end of input>'
l!l
11
MC/DC100%1/1
$parser->YYCurtok || '<end of input>'
$parser->YYCurtok
Truth table100%2/2
A
A= $parser->YYCurtok
Aresult
11
00
187 2 my $val='';
188 2 ▶ $val = ' (' . $parser->YYCurval . ')' if $parser->YYCurval;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->YYCurval 1 1 2
189  
190 2 my $errmsg = 'Syntax error: could not understand ' . $got . $val .
191 " at positions $startpos..$endpos";
192 2 ▶ if(ref($parser->YYExpect) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parser->YYExpect eq 'ARRAY') { } 0 2 2
193 0 $errmsg .= ".\nExpected one of: " . join(',', @{$parser->YYExpect});
194 } else {
195 2 $errmsg .= ':'
196 }
197  
198 # Print the text and flag the error
199 2 my $copy = ${ $parser->YYData->{TEXT} };
200 2 $copy =~ s/\s/ /g; # Normalize spaces so pos values line up
201 2 $errmsg .= "\n$copy";
202 2 $errmsg .= "\n" . (' ' x $startpos) . ('^' x ($endpos-$startpos));
203  
204 2 $errmsg .= "\n"; # No stack trace
205 2 die $errmsg;
206 } #_report_error()
207  
208 # }}}1
209 # Top-level parse function {{{1
210  
211 =head2 Parse
212  
213 Parse arguments. Usage:
214  
215 my $ast = Sub::Multi::Tiny::SigParse::Parse($signature);
216  
217 =cut
218  
219 26 ▶sub Parse {
Subroutine Parse: called CC=3 SCAR=11.4
Pod documented
220 26 my $text = shift;
221 26 ▶ unless(defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 26 26
222 0 require Carp;
223 0 Carp::croak 'Parse: Need a signature to parse';
224 }
225  
226 26 my $parser = __PACKAGE__->new;
227 26 my $hrData = $parser->YYData;
228  
229 # Data we use while parsing.
230  
231 # TEXT: The input text. Store it as a reference so pos() will
232 # be preserved across calls to _next_token.
233 26 $hrData->{TEXT} = \"$text";
234  
235 # CURR_TOK_POS: the pos() value where the current token started.
236 # Used in reporting errors.
237 26 $hrData->{CURR_TOK_POS} = -1;
238  
239 # SEEN: bit flags for which types of things we've seen
240 26 $hrData->{SEEN} = '';
241  
242 26 ▶ my $lrParms = $parser->YYParse(yylex => \&_next_token,
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 26 26
243 yyerror => \&_report_error,
244 (@_ ? (yydebug => $_[0]) : ()),
245 );
246 24 my %retval = (seen => $hrData->{SEEN}, parms => $lrParms);
247  
248 24 return \%retval;
249 } #Parse()
250  
251 # }}}1
252 # Rest of the docs {{{1
253  
254 =head1 AUTHOR
255  
256 Chris White E<lt>cxw@cpan.orgE<gt>
257  
258 =head1 LICENSE
259  
260 Copyright (C) 2019 Chris White E<lt>cxw@cpan.orgE<gt>
261  
262 This library is free software; you can redistribute it and/or modify
263 it under the same terms as Perl itself.
264  
265 =cut
266  
267 # }}}1
268  
269 # vi: set fdm=marker: #
« blib/lib/Sub/Multi/Tiny.pm ↑ Summary blib/lib/Sub/Multi/Tiny/Dispatcher/TypeParams.pm »