blib/lib/Salvation/TC/Parser/PP.pm

filter: Statementstmt 97.2% 141 / 145 Branchbran 81.9% 95 / 116 Conditioncond 52.0% 13 / 25 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 84.7% 262 / 309 SCAR 43.6
CC
68
Cov
87%
tokenize_type_str_impl
43.6
tokenize_signature_str_impl
27.4
tokenize_signature_parameter_str
6.0
CRAP 78.0
« blib/lib/Salvation/TC.pm ↑ Summary blib/lib/Salvation/TC/Type/Time.pm »
1 package Salvation::TC::Parser::PP;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = 0.06;
8  
9 =head2 tokenize_type_str_impl( Str str, Maybe[HashRef( Bool :loose )] options? )
10  
11 Разбирает строку с именем типа C<str> и возвращает ArrayRef[HashRef]
12 с найденными токенами.
13  
14 Краткое описание опций в C<options>:
15  
16 =over
17  
18 =item loose
19  
20 Не выполнять дополнительных проверок возможности токена на основе внешних данных.
21  
22 =back
23  
24 =cut
25  
26 sub tokenize_type_str_impl {
27  
28 256 ▶ my ( $class, $str, $options ) = @_;
Subroutine tokenize_type_str_impl: called CC=38 SCAR=37.8
Pod documented
29  
30 256 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
2560
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
31  
32 256 $str =~ s/\s+/ /gs;
33 256 $str =~ s/^\s+//;
34 256 $str =~ s/\s+$//;
35  
36 256 my @chars = split( //, $str );
37 256 my @stack = ();
38 256 my $word = '';
39 256 my $parameterizable_type = '';
40  
41 256 ▶ while( defined( my $char = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @chars)) 2000 256 2256
42  
43 2000 ▶ next if( $char eq ' ' );
Branch: 1 branch
BranchTrueFalseTotal
if $char eq " " 1 1999 2000
44  
45 1999 ▶ if( ( $char eq '[' ) && ( $word ne 'Maybe' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "[" and $word ne "Maybe") 51 1948 1999
Condition100%3/3
$char eq "[" and $word ne "Maybe"
!ll&&!rl&&r
1946251
MC/DC100%2/2
$char eq "[" and $word ne "Maybe"
$char eq "[" $word ne "Maybe"
Truth table100%3/3
A and B
A= $char eq "["B= $word ne "Maybe"
ABresult
0X0
100
111
46  
47 51 ▶ if( $options -> { 'loose' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'loose'}) { } 0 51 51
48  
49 0 $parameterizable_type = $word;
50  
51 } else {
52  
53 51 $parameterizable_type = $class -> load_parameterizable_type_class( $word );
54  
55 51 ▶ die( "Can't parameterize type ${word}" ) if( $parameterizable_type eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $parameterizable_type eq "" 0 51 51
56 }
57 }
58  
59 1999 ▶ if( $char eq '|' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($char eq '|') { } 10 1989 1999
elsif ($char eq '[') { } 53 1936 1989
elsif ($char eq '(') { } 12 1924 1936
elsif ($char eq '{') { } 28 1896 1924
60  
61 10 ▶ if( $word eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq '') { } 7 3 10
62  
63 7 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stack[$#stack]{"maybe"} or exists $stack[$#stack]{"class"} or exists $stack[$#stack]{"signed"} or exists $stack[$#stack]{"length"}) 0 7 7
Condition33%3/9
exists $stack[$#stack]{"maybe"} or exists $stack[$#stack]{"class"} or exists $stack[$#stack]{"signed"} or exists $stack[$#stack]{"length"}
l!l&&r!l&&!r
700
exists $stack[$#stack]{"maybe"} or exists $stack[$#stack]{"class"} or exists $stack[$#stack]{"signed"}
l!l&&r!l&&!r
700
exists $stack[$#stack]{"maybe"} or exists $stack[$#stack]{"class"}
l!l&&r!l&&!r
070
MC/DC0%0/4
exists $stack[$#stack]{"maybe"} or exists $stack[$#stack]{"class"} or exists $stack[$#stack]{"signed"} or exists $stack[$#stack]{"length"}
exists $stack[$#stack]{"maybe"} exists $stack[$#stack]{"class"} exists $stack[$#stack]{"signed"} exists $stack[$#stack]{"length"}
Truth table20%1/5
A or B or C or D
A= exists $stack[$#stack]{"maybe"}B= exists $stack[$#stack]{"class"}C= exists $stack[$#stack]{"signed"}D= exists $stack[$#stack]{"length"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
64 exists $stack[ $#stack ] -> { 'maybe' }
65 || exists $stack[ $#stack ] -> { 'class' }
66 || exists $stack[ $#stack ] -> { 'signed' }
67 || exists $stack[ $#stack ] -> { 'length' }
68 ) {
69  
70 0 die( 'Invalid type string' );
71 }
72  
73 } else {
74  
75 3 push( @stack, { type => $word } );
76  
77 3 $word = '';
78 }
79  
80 } elsif( $char eq '[' ) {
81  
82 53 my $cnt = 1;
83 53 my $substr = '';
84  
85 53 ▶ while( defined( my $subchar = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subchar = shift @chars)) 796 0 796
86  
87 796 ▶ ++$cnt if( $subchar eq '[' );
Branch: 1 branch
BranchTrueFalseTotal
if $subchar eq "[" 53 743 796
88 796 ▶ --$cnt if( $subchar eq ']' );
Branch: 1 branch
BranchTrueFalseTotal
if $subchar eq "]" 106 690 796
89  
90 796 ▶ last if( $cnt == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 53 743 796
91  
92 743 $substr .= $subchar;
93 }
94  
95 53 ▶ die( 'Invalid type parameterization' ) if( ( $substr eq '' ) || ( $word eq '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $substr eq "" or $word eq "" 0 53 53
Condition33%1/3
$substr eq "" or $word eq ""
l!l&&r!l&&!r
0053
MC/DC0%0/2
$substr eq "" or $word eq ""
$substr eq "" $word eq ""
Truth table33%1/3
A or B
A= $substr eq ""B= $word eq ""
ABresult
1X1
011
000
96  
97 53 ▶ if( $parameterizable_type eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameterizable_type eq '') { } 2 51 53
98  
99 2 push( @stack, { maybe => tokenize_type_str_impl( $class, $substr, $options ) } );
100  
101 } else {
102  
103 51 push( @stack, {
104 class => $parameterizable_type,
105 param => tokenize_type_str_impl( $class, $substr, $options ),
106 base => tokenize_type_str_impl( $class, $word, $options ),
107 } );
108  
109 51 $parameterizable_type = '';
110 }
111  
112 53 $word = '';
113  
114 } elsif( $char eq '(' ) {
115  
116 12 ▶ if( $word eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq '') { } 4 8 12
117  
118 4 ▶ unless( exists $stack[ $#stack ] -> { 'class' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stack[$#stack]{"class"}) 0 4 4
119  
120 0 die( 'Invalid type description' );
121 }
122  
123 } else {
124  
125 8 push( @stack, { type => $word } );
126 8 $word = '';
127 }
128  
129 12 my $cnt = 1;
130 12 my $substr = $char;
131  
132 12 ▶ while( defined( my $subchar = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subchar = shift @chars)) 239 0 239
133  
134 239 ▶ ++$cnt if( $subchar eq '(' );
Branch: 1 branch
BranchTrueFalseTotal
if $subchar eq "(" 2 237 239
135 239 ▶ --$cnt if( $subchar eq ')' );
Branch: 1 branch
BranchTrueFalseTotal
if $subchar eq ")" 14 225 239
136  
137 239 $substr .= $subchar;
138  
139 239 ▶ last if( $cnt == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 12 227 239
140 }
141  
142 12 ▶ die( 'Invalid signature' ) if( $substr eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $substr eq "" 0 12 12
143  
144 12 push( @stack, {
145 signed => {
146 type => pop( @stack ),
147 signature => tokenize_signature_str_impl( $class, $substr, $options ),
148 source => $substr,
149 }
150 } );
151  
152 } elsif( $char eq '{' ) {
153  
154 28 ▶ if( $word ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word ne "") 23 5 28
155  
156 23 push( @stack, { type => $word } );
157 23 $word = '';
158 }
159  
160 28 my $substr = '';
161  
162 28 ▶ while( defined( my $subchar = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subchar = shift @chars)) 81 0 81
163  
164 81 ▶ last if( $subchar eq '}' );
Branch: 1 branch
BranchTrueFalseTotal
if $subchar eq "}" 28 53 81
165  
166 53 $substr .= $subchar;
167 }
168  
169 28 $substr =~ s/\s+//g;
170  
171 28 my ( $min, $max ) = ( undef, undef );
172  
173 28 ▶ if( $substr =~ m/^(0|[1-9][0-9]*),(0|[1-9][0-9]*)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($substr =~ /^(0|[1-9][0-9]*),(0|[1-9][0-9]*)$/) { } 6 22 28
elsif ($substr =~ /^(0|[1-9][0-9]*),$/) { } 7 15 22
elsif ($substr =~ /^(0|[1-9][0-9]*)$/) { } 15 0 15
174  
175 6 ( $min, $max ) = ( $1, $2 );
176  
177 } elsif( $substr =~ m/^(0|[1-9][0-9]*),$/ ) {
178  
179 7 $min = $1;
180  
181 } elsif( $substr =~ m/^(0|[1-9][0-9]*)$/ ) {
182  
183 15 ( $min, $max ) = ( $1 )x2;
184  
185 } else {
186  
187 0 die( 'Invalid length limits' );
188 }
189  
190 28 push( @stack, {
191 length => {
192 type => pop( @stack ),
193 min => $min,
194 max => $max,
195 }
196 } );
197  
198 } else {
199  
200 1896 $word .= $char;
201 }
202 }
203  
204 256 ▶ push( @stack, { type => $word } ) if( $word ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $word ne "" 179 77 256
205  
206 256 return \@stack;
207 }
208  
209 =head2 tokenize_signature_str_impl( Str str, Maybe[HashRef] options? )
210  
211 Разбирает строку с подписью C<str> и возвращает ArrayRef[HashRef]
212 с найденными токенами.
213  
214 Набор опций в C<options> и их значений эквивалентен оному для C<tokenize_type_str_impl>.
215  
216 =cut
217  
218 sub tokenize_signature_str_impl {
219  
220 12 ▶ my ( $class, $str, $options ) = @_;
Subroutine tokenize_signature_str_impl: called CC=26 SCAR=33.1
Pod documented
221  
222 12 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
120
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
223  
224 12 $str =~ s/\s+/ /gs;
225  
226 12 ▶ die( "Invalid signature: ${str}" ) if( $str !~ m/^\s*\(\s*(.+?)\s*\)\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^\s*\(\s*(.+?)\s*\)\s*$/ 0 12 12
227  
228 12 $str = $1;
229  
230 12 my @chars = split( //, $str );
231 12 my @stack = ();
232 12 my @seq = ( 'type', 'name', 'delimiter' );
233 12 my %word = ();
234  
235 12 my %opened_parens = (
236 '{' => 0,
237 '(' => 0,
238 '[' => 0,
239 );
240  
241 12 my %closed_parens = (
242 '}' => '{',
243 ')' => '(',
244 ']' => '[',
245 );
246  
247 12 my $delimiter_re = qr/^[\s,]$/;
248  
249 12 ▶ while( defined( my $item_type = shift( @seq ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item_type = shift @seq)) 45 0 45
250  
251 45 my $word = '';
252  
253 45 ▶ if( $item_type eq 'type' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($item_type eq 'type') { } 15 30 45
elsif ($item_type eq 'name') { } 15 15 30
elsif ($item_type eq 'delimiter') { } 15 0 15
254  
255 15 ▶ while( defined( my $char = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @chars)) 153 0 153
256  
257 153 ▶ ++$opened_parens{ $char } if( exists $opened_parens{ $char } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opened_parens{$char} 9 144 153
258 153 ▶ --$opened_parens{ $closed_parens{ $char } } if( exists $closed_parens{ $char } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $closed_parens{$char} 9 144 153
259  
260 153 ▶ if( $char =~ $delimiter_re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /$delimiter_re/) 33 120 153
261  
262 33 my $word_end = true;
263  
264 33 ▶ while( my ( $key, $value ) = each( %opened_parens ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %opened_parens) 99 33 132
265  
266 99 ▶ $word_end = false if( $word_end && $value != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $word_end and $value != 0 18 81 99
Condition100%3/3
$word_end and $value != 0
!ll&&!rl&&r
245718
MC/DC100%2/2
$word_end and $value != 0
$word_end $value != 0
Truth table100%3/3
A and B
A= $word_endB= $value != 0
ABresult
0X0
100
111
267 }
268  
269 33 ▶ if( $word_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_end) 15 18 33
270  
271 15 ▶ while( defined( my $subchar = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subchar = shift @chars)) 15 0 15
272  
273 15 ▶ if( $subchar !~ $delimiter_re ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subchar =~ /$delimiter_re/) 15 0 15
274  
275 15 unshift( @chars, $subchar );
276 15 last;
277 }
278 }
279  
280 15 last;
281 }
282 }
283  
284 138 $word .= $char;
285 }
286  
287 15 ▶ die( 'Invalid type string' ) if( $word eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "" 0 15 15
288  
289 } elsif( $item_type eq 'name' ) {
290  
291 15 ▶ while( defined( my $char = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @chars)) 63 12 75
292  
293 63 ▶ if( $char =~ $delimiter_re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /$delimiter_re/) 3 60 63
294  
295 3 ▶ while( defined( my $subchar = shift( @chars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $subchar = shift @chars)) 6 0 6
296  
297 6 ▶ if( $subchar !~ $delimiter_re ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subchar =~ /$delimiter_re/) 3 3 6
298  
299 3 unshift( @chars, $subchar );
300 3 last;
301 }
302 }
303  
304 3 last;
305 }
306  
307 60 $word .= $char;
308 }
309  
310 15 ▶ die( 'Invalid parameter name' ) if( $word eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "" 0 15 15
311  
312 } elsif( $item_type eq 'delimiter' ) {
313  
314 15 my ( $type, $name ) = delete @word{ 'type', 'name' };
315  
316 15 ▶ die( 'Invalid signature' ) if( ( $type eq '' ) || ( $name eq '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "" or $name eq "" 0 15 15
Condition33%1/3
$type eq "" or $name eq ""
l!l&&r!l&&!r
0015
MC/DC0%0/2
$type eq "" or $name eq ""
$type eq "" $name eq ""
Truth table33%1/3
A or B
A= $type eq ""B= $name eq ""
ABresult
1X1
011
000
317  
318 15 $type = tokenize_type_str_impl( $class, $type, $options );
319 15 $name = tokenize_signature_parameter_str( $name );
320  
321 15 push( @stack, {
322 type => $type,
323 param => $name,
324 } );
325  
326 15 push( @seq, $item_type );
327  
328 15 ▶ last if( scalar( @chars ) == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @chars == 0 12 3 15
329 3 next;
330 }
331  
332 30 $word{ $item_type } = $word;
333  
334 30 push( @seq, $item_type );
335 }
336  
337 12 return \@stack;
338 }
339  
340 =head2 tokenize_signature_parameter_str( Str $str )
341  
342 Разбирает строку с именем параметра C<$str> и возвращает HashRef, представляющий
343 токен.
344  
345 =cut
346  
347 sub tokenize_signature_parameter_str {
348  
349 15 ▶ my ( $str ) = @_;
Subroutine tokenize_signature_parameter_str: called CC=6 SCAR=17.9
Pod documented
350  
351 15 my %out = ();
352 15 my $first_char = substr( $str, 0, 1 );
353  
354 15 ▶ if( $first_char eq ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_char eq ':') { } 9 6 15
355  
356 9 $out{ 'named' } = true;
357 9 $str = substr( $str, 1 );
358  
359 } else {
360  
361 6 $out{ 'positional' } = true;
362 }
363  
364 15 my $last_char = substr( $str, -1, 1 );
365  
366 15 ▶ if( $last_char eq '!' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($last_char eq '!') { } 8 7 15
elsif ($last_char eq '?') { } 1 6 7
elsif ($out{'positional'}) { } 5 1 6
elsif ($out{'named'}) { } 1 0 1
367  
368 8 $out{ 'required' } = true;
369 8 $str = substr( $str, 0, -1 );
370  
371 } elsif( $last_char eq '?' ) {
372  
373 1 $out{ 'optional' } = true;
374 1 $str = substr( $str, 0, -1 );
375  
376 } elsif( $out{ 'positional' } ) {
377  
378 5 $out{ 'required' } = true;
379  
380 } elsif( $out{ 'named' } ) {
381  
382 1 $out{ 'optional' } = true;
383 }
384  
385 15 $out{ 'name' } = $str;
386  
387 15 return \%out;
388 }
389  
390 4 1;
391  
392 __END__
« blib/lib/Salvation/TC.pm ↑ Summary blib/lib/Salvation/TC/Type/Time.pm »