blib/lib/Zed/Range/Parser.pm

filter: Statementstmt 13.5% 11 / 81 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 25.0% 3 / 12 Podpod 0.0% 0 / 6 totaltotal 7.8% 14 / 179 CC 31 SCAR 44.5
CC
31
Cov
7%
parse
210.0
__ANON__
56.0
oprange
20.0
CRAP 85.8
« blib/lib/Zed.pm ↑ Summary blib/lib/Zed/Plugin.pm »
1 package Zed::Range::Parser;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Zed::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Zed::Range::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # i{01~09,-05}.xx.corp.net,i{1~3,9}.yy.corp.com,-/4/,&/net/
8 #transter to:
9 # i*{01~09-05}*.xx.corp.net,i*{1~3,9}*.yy.corp.com-/4/&/net/
10 #priority:
11 # 0: }
12 # 1: - &
13 # 2: ,
14 # 3: *
15 # 4: ~
16 # 5: {
17  
18 1 our %PRI = (
19 '}' => 0, '#' => 0,
20 '-' => 1, '&' => 1,
21 ',' => 2,
22 '*' => 3,
23 '~' => 4,
24 '{' => 5,
25 );
26  
27 0 ▶sub _pri { $_[0] eq '{' ? $_[1] eq '}' : $PRI{ $_[0] } >= $PRI{ $_[1] } }
Subroutine _pri: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq '{' ? : 0 0 0
28  
29 0 ▶sub _toset{ map{ ref $_ ? $_ : Zed::Range::Set->new($_) }@_ }
Subroutine _toset: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
30  
31 sub oprange
32 {
33 0 ▶ my $values = shift;
Subroutine oprange: not called CC=4 SCAR=30.0
Pod undocumented
34 0 ▶ return if @$values < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$values < 2 0 0 0
35  
36 0 my ($a, $b, $zero) = splice @$values, -2 ,2;
37  
38 0 ▶ map{$zero = length $_ if $zero < length $_; s/^(0*)//; }($a, $b);
Branch: 1 branch
BranchTrueFalseTotal
if $zero < length $_ 0 0 0
39  
40 0 ▶ my @num = $a > $b ? ($b..$a) : ($a..$b);
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 0 0 0
41 0 debug("num:", \@num);
42 0 debug("zero:", $zero);
43  
44 0 my $set = Zed::Range::Set->new( map{sprintf "%0$zero\d",$_}@num );
45 0 push @$values, $set;
46  
47 }
48 sub opdescart
49 {
50 0 ▶ my $values = shift;
Subroutine opdescart: not called CC=1 SCAR=6.9
Pod undocumented
51 0 my ($a, $b) = _toset( splice @$values, -2 ,2 );
52 0 push @$values, $a*$b;
53
54 }
55 sub opunion
56 {
57 0 ▶ my $values = shift;
Subroutine opunion: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my ($a, $b) = _toset( splice @$values, -2 ,2 );
59 0 push @$values, $a + $b;
60 }
61 sub opdiff
62 {
63 0 ▶ my $values = shift;
Subroutine opdiff: not called CC=4 SCAR=30.0
Pod undocumented
64 0 my ($a, $b) = splice @$values, -2 ,2;
65 0 ▶ $a = ref $a ? $a : Zed::Range::Set->new($a);
Branch: 1 branch
BranchTrueFalseTotal
ref $a ? : 0 0 0
66 0 ▶ $b = ref $b ? $b : $b =~ qr!^/(.*)/$! ? $1 : Zed::Range::Set->new($b);
Branch: 2 branches
BranchTrueFalseTotal
ref $b ? : 0 0 0
$b =~ qr"^/(.*)/$" ? : 0 0 0
67 0 push @$values, $a - $b;
68 }
69 sub opintersec
70 {
71 0 ▶ my $values = shift;
Subroutine opintersec: not called CC=4 SCAR=30.0
Pod undocumented
72 0 my ($a, $b) = splice @$values, -2 ,2;
73 0 ▶ $a = ref $a ? $a : Zed::Range::Set->new($a);
Branch: 1 branch
BranchTrueFalseTotal
ref $a ? : 0 0 0
74 0 ▶ $b = ref $b ? $b : $b =~ qr!^/(.*)/$! ? $1 : Zed::Range::Set->new($b);
Branch: 2 branches
BranchTrueFalseTotal
ref $b ? : 0 0 0
$b =~ qr"^/(.*)/$" ? : 0 0 0
75 0 push @$values, $a & $b;
76 }
77  
78 sub parse
79 {
80 0 ▶ my( $class, $str ) = @_;
Subroutine parse: not called CC=14 SCAR=53.5
Pod undocumented
81 0 $str =~ s/$/#/;
82 0 $str =~ s/,([\,\&])/$1/g;
83 #add *
84 #foo.{} => foo.*{}
85 #{}.bar => {}*.bar
86 #{foo,bar}{baz,foobar} => {foo,bar}*{baz,foobar}
87 0 $str =~ s!([\w\.}]){!$1*{!g;
88 0 $str =~ s!}([\w\.])!}*$1!g;
89  
90 0 my @strs = split qr!(,-)|([{},~&*#])!, $str;
91  
92 0 my (@values, @ops);
93 my $calcu = sub {
94 0 ▶ return if @values < 2;
Subroutine __ANON__: not called CC=7 SCAR=40.3
Branch: 1 branch
BranchTrueFalseTotal
if @values < 2 0 0 0
95  
96 #use feature "switch";
97 #for(pop @ops)
98 #{
99 # when(/\~/) { oprange(\@values) }
100 # when(/\*/) { opdescart(\@values) }
101 # when(/\,/) { opunion(\@values) }
102 # when(/\-/) { opdiff(\@values) }
103 # when(/\&/) { opintersec(\@values) }
104 #}
105 0 $_ = pop @ops;
106 {
107 0 ▶ if(/\~/) { oprange(\@values); last }
Branch: 1 branch
BranchTrueFalseTotal
if (/\~/) 0 0 0
108 0 ▶ if(/\*/) { opdescart(\@values); last }
Branch: 1 branch
BranchTrueFalseTotal
if (/\*/) 0 0 0
109 0 ▶ if(/\,/) { opunion(\@values); last }
Branch: 1 branch
BranchTrueFalseTotal
if (/\,/) 0 0 0
110 0 ▶ if(/\-/) { opdiff(\@values); last }
Branch: 1 branch
BranchTrueFalseTotal
if (/\-/) 0 0 0
111 0 ▶ if(/\&/) { opintersec(\@values); last }
Branch: 1 branch
BranchTrueFalseTotal
if (/\&/) 0 0 0
112 }
113  
114 0 };
115 0 PARSE: foreach my $str (@strs)
116 {
117 0 ▶ next unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 0 0
118 0 $str =~ s!,-!-!;
119 0 ▶ push @values, $str and next unless $str =~ /^[\{\}\,\~\-\&*\#]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^[\{\}\,\~\-\&*\#]$/ 0 0 0
Condition0%0/5
$str =~ /^[\{\}\,\~\-\&*\#]$/ || push(@values, $str) && (next)
l!l&&r!l&&!r
000
push @values, $str and next
!ll
00
MC/DC0%0/2
$str =~ /^[\{\}\,\~\-\&*\#]$/ || push(@values, $str) && (next)
$str =~ /^[\{\}\,\~\-\&*\#]$/ push @values, $str
Truth table0%0/3
A || B
A= $str =~ /^[\{\}\,\~\-\&*\#]$/B= push @values, $str
ABresult
1X1
011
000
120  
121 0 ▶ while( $ops[-1] && _pri( $ops[-1], $str ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($ops[-1] and _pri($ops[-1], $str)) 0 0 0
Condition0%0/3
$ops[-1] and _pri($ops[-1], $str)
!ll&&!rl&&r
000
MC/DC0%0/2
$ops[-1] and _pri($ops[-1], $str)
$ops[-1] _pri($ops[-1], $str)
Truth table0%0/3
A and B
A= $ops[-1]B= _pri($ops[-1], $str)
ABresult
0X0
100
111
122 {
123 0 ▶ pop @ops and next PARSE if $ops[-1] eq '{' && $str eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $ops[-1] eq "{" and $str eq "}" 0 0 0
Condition0%0/8
$ops[-1] eq '{' && $str eq '}' && (pop @ops && (next PARSE))
!ll&&!rl&&r
000
$ops[-1] eq "{" and $str eq "}"
!ll&&!rl&&r
000
pop @ops and next PARSE
!ll
00
MC/DC0%0/3
$ops[-1] eq '{' && $str eq '}' && (pop @ops && (next PARSE))
$ops[-1] eq "{" $str eq "}" pop @ops
Truth table0%0/4
A and B && C
A= $ops[-1] eq "{"B= $str eq "}"C= pop @ops
ABCresult
0XX0
10X0
1100
1111
124 0 $calcu->($str);
125 }
126 0 push @ops, $str;
127 }
128 0 debug("ops:", \@ops);
129 0 debug("values:", \@values);
130 0 ▶ warn "Range expression error!" and return if scalar @ops > 1 || scalar @values > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ops > 1 or scalar @values > 1 0 0 0
Condition0%0/8
(scalar @ops > 1 || scalar @values > 1) && (warn('Range expression error!') && (return))
!ll&&!rl&&r
000
scalar @ops > 1 or scalar @values > 1
l!l&&r!l&&!r
000
warn "Range expression error!" and return
!ll
00
MC/DC0%0/3
(scalar @ops > 1 || scalar @values > 1) && (warn('Range expression error!') && (return))
scalar @ops > 1 scalar @values > 1 warn "Range expression error!"
Truth table0%0/5
A or B && C
A= scalar @ops > 1B= scalar @values > 1C= warn "Range expression error!"
ABCresult
00X0
1X00
0100
1X11
0111
131 0 my $ret = $values[0];
132 0 ▶ return ref $ret ? $ret : Zed::Range::Set->new($ret);
Branch: 1 branch
BranchTrueFalseTotal
ref $ret ? : 0 0 0
133 }
134  
135 1 1;
« blib/lib/Zed.pm ↑ Summary blib/lib/Zed/Plugin.pm »