blib/lib/DTL/Fast/Expression.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 94.1% 32 / 34 Conditioncond 68.0% 17 / 25 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 2 totaltotal 87.2% 150 / 172 SCAR 33.4
CC
28
Cov
92%
_parse_expression
23.0
new
5.0
_parse_brackets
3.0
CRAP 28.4
« blib/lib/DTL/Fast/Cache/Serialized.pm ↑ Summary blib/lib/DTL/Fast/Expression/Operator/Binary/Ge.pm »
1 package DTL::Fast::Expression;
2 98 ▶use strict; use utf8; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 98 ▶use parent 'DTL::Fast::Replacer';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 98 ▶use DTL::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 98 ▶use DTL::Fast::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 98 ▶use DTL::Fast::Expression::Operator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 98 ▶use DTL::Fast::Replacer::Replacement;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 98 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 98 our %EXPRESSION_CACHE = ();
13 98 our $EXPRESSION_CACHE_HITS = 0;
14  
15 # @todo cache mechanism via get_expression
16 sub new
17 {
18 1817 ▶ my( $proto, $expression, %kwargs ) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
19 1817 my $result = undef;
20  
21 1817 $expression =~ s/^\s+|\s+$//xgsi;
22
23 1817 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (not $kwargs{'replacement'} || $kwargs{'level'} and $DTL::Fast::Expression::EXPRESSION_CACHE{$expression}) { } 1367 450 1817
Condition83%5/6
not $kwargs{"replacement"} || $kwargs{"level"} and $DTL::Fast::Expression::EXPRESSION_CACHE{$expression}
!ll&&!rl&&r
3131371367
$kwargs{'replacement'} || $kwargs{'level'}
l!l&&r!l&&!r
31301504
MC/DC66%2/3
not $kwargs{"replacement"} || $kwargs{"level"} and $DTL::Fast::Expression::EXPRESSION_CACHE{$expression}
$kwargs{'replacement'} $kwargs{'level'} $DTL::Fast::Expression::EXPRESSION_CACHE{$expression}
Truth table75%3/4
not(A || B) and C
A= $kwargs{'replacement'}B= $kwargs{'level'}C= $DTL::Fast::Expression::EXPRESSION_CACHE{$expression}
ABCresult
1XX0
01X0
0000
0011
24 not $kwargs{'replacement'} # cache only top-level expressions
25 and not $kwargs{'level'} # same ^^
26 and $EXPRESSION_CACHE{$expression} # has cached expression
27 )
28 {
29 1367 $result = $EXPRESSION_CACHE{$expression};
30 1367 $EXPRESSION_CACHE_HITS++;
31 }
32 else
33 {
34 450 $kwargs{'expression'} = $expression;
35 450 ▶ $kwargs{'level'} //= 0;
Condition100%2/2
$kwargs{"level"} //= 0
l!l
313137
MC/DC100%1/1
$kwargs{"level"} //= 0
$kwargs{"level"}
Truth table100%2/2
A
A= $kwargs{"level"}
Aresult
11
00
36
37 450 my $self = $proto->SUPER::new(%kwargs);
38  
39 450 $self->{'expression'} = $self->_parse_expression(
40 $self->_parse_brackets(
41 $self->backup_strings($expression)
42 )
43 );
44  
45 441 $EXPRESSION_CACHE{$expression} = $result = $self->{'expression'};
46 }
47
48 1808 return $result;
49 }
50  
51 sub _parse_brackets
52 {
53 450 ▶ my( $self, $expression ) = @_;
Subroutine _parse_brackets: called CC=3 SCAR=11.0
54  
55 450 $expression =~ s/\s+/ /xgsi;
56 450 ▶ while( $expression =~ s/
Branch: 1 branch
BranchTrueFalseTotal
if ($expression =~ s/ \(\s*([^()]+)\s*\) /$self->backup_expression($1);/egx) 8 450 458
57 \(\s*([^()]+)\s*\)
58 /
59 11 $self->backup_expression($1)
60 /xge ){};
61
62 450 ▶ die $self->get_parse_error('unpaired brackets in expression')
Branch: 1 branch
BranchTrueFalseTotal
if $expression =~ /[()]/ 1 449 450
63 if $expression =~ /[()]/;
64
65 449 return $expression;
66 }
67  
68 sub get_parse_error
69 {
70 4 ▶ my ($self, $message, @additional) = @_;
Subroutine get_parse_error: called CC=1 SCAR=0.0
Pod undocumented
71
72 return $self->SUPER::get_parse_error(
73 $message
74 , @additional
75 4 , 'Expression' => $self->{'expression'}
76 );
77 }
78  
79 sub _parse_expression
80 {
81 449 ▶ my( $self, $expression ) = @_;
Subroutine _parse_expression: called CC=22 SCAR=31.3
82
83 449 my $result = undef;
84
85 449 ▶ for( my $level = $self->{'level'}; $level < scalar @DTL::Fast::OPS_RE; $level++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($level < scalar @DTL::Fast::OPS_RE) 2034 263 2297
86 {
87 2034 my $operators = $DTL::Fast::OPS_RE[$level];
88 2034 my @result = ();
89 2034 my @source = split /
90 (?:^|\s+)
91 ($operators)
92 (?:$|\s+)
93 /six, $expression;
94  
95 2034 ▶ if( scalar @source > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @source > 1) 186 1848 2034
96 {
97 # processing operands
98 186 ▶ while( defined ( my $token = shift @source) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = shift @source)) 565 182 747
99 {
100 565 ▶ next if $token eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "" 46 519 565
101
102 519 ▶ if( $token =~ /^$operators$/six ) # operation
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^$operators$/six) { } 190 329 519
103 {
104 190 push @result, $token;
105 }
106 else
107 {
108 329 push @result, $self->get_backup_or_expression($token, $level);
109 }
110 }
111
112 # processing operators
113 182 ▶ while( my $token = shift @result )
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @result) 323 179 502
114 {
115 323 ▶ if( ref $token ) # operand
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token) { } 136 187 323
116 {
117 136 $result = $token;
118 }
119 else # operator
120 {
121 187 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @result and ref $result[0]) { } 186 1 187
Condition66%2/3
scalar @result and ref $result[0]
!ll&&!rl&&r
10186
MC/DC50%1/2
scalar @result and ref $result[0]
scalar @result ref $result[0]
Truth table66%2/3
A and B
A= scalar @resultB= ref $result[0]
ABresult
0X0
100
111
122 scalar @result # there is a next token
123 and ref $result[0] # it's an operand
124 )
125 {
126 186 my $operand = shift @result;
127
128 186 ▶ if( not exists $DTL::Fast::OPS_HANDLERS{$token}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $DTL::Fast::OPS_HANDLERS{$token} and exists $DTL::Fast::KNOWN_OPS_PLAIN{$token}) 31 155 186
Condition66%2/3
not exists $DTL::Fast::OPS_HANDLERS{$token} and exists $DTL::Fast::KNOWN_OPS_PLAIN{$token}
!ll&&!rl&&r
155031
MC/DC50%1/2
not exists $DTL::Fast::OPS_HANDLERS{$token} and exists $DTL::Fast::KNOWN_OPS_PLAIN{$token}
not exists $DTL::Fast::OPS_HANDLERS{$token} exists $DTL::Fast::KNOWN_OPS_PLAIN{$token}
Truth table66%2/3
not(A) and B
A= not exists $DTL::Fast::OPS_HANDLERS{$token}B= exists $DTL::Fast::KNOWN_OPS_PLAIN{$token}
ABresult
0X0
100
111
129 and exists $DTL::Fast::KNOWN_OPS_PLAIN{$token}
130 )
131 {
132 31 require Module::Load;
133 31 Module::Load::load($DTL::Fast::KNOWN_OPS_PLAIN{$token});
134 31 $DTL::Fast::LOADED_MODULES{$DTL::Fast::KNOWN_OPS_PLAIN{$token}} = time;
135 31 $DTL::Fast::OPS_HANDLERS{$token} = $DTL::Fast::KNOWN_OPS_PLAIN{$token};
136 }
137
138 186 ▶ my $handler = $DTL::Fast::OPS_HANDLERS{$token} || die $self->get_parse_error("there is no processor for $token operator");
Condition50%1/2
$DTL::Fast::OPS_HANDLERS{$token} || die($self->get_parse_error("there is no processor for $token operator"))
l!l
1860
MC/DC0%0/1
$DTL::Fast::OPS_HANDLERS{$token} || die($self->get_parse_error("there is no processor for $token operator"))
$DTL::Fast::OPS_HANDLERS{$token}
Truth table50%1/2
A
A= $DTL::Fast::OPS_HANDLERS{$token}
Aresult
11
00
139
140 186 ▶ if($handler->isa('DTL::Fast::Expression::Operator::Binary'))
Branch: 2 branches
BranchTrueFalseTotal
if ($handler->isa('DTL::Fast::Expression::Operator::Binary')) { } 140 46 186
elsif ($handler->isa('DTL::Fast::Expression::Operator::Unary')) { } 46 0 46
141 {
142 140 ▶ if( defined $result )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 139 1 140
143 {
144 139 $result = $handler->new( $result, $operand );
145 }
146 else
147 {
148 1 ▶ die $self->get_parse_error(
Condition50%1/2
$token // "undef"
l!l
10
MC/DC0%0/1
$token // "undef"
$token
Truth table50%1/2
A
A= $token
Aresult
11
00
149 sprintf('binary operator `%s` has no left argument'
150 , $token // 'undef'
151 )
152 );
153 }
154 }
155 elsif( $handler->isa('DTL::Fast::Expression::Operator::Unary') )
156 {
157 46 ▶ if( defined $result )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 1 45 46
158 {
159 1 ▶ die $self->get_parse_error(
Condition50%1/2
$token // "undef"
l!l
10
MC/DC0%0/1
$token // "undef"
$token
Truth table50%1/2
A
A= $token
Aresult
11
00
160 sprintf('unary operator `%s` got left argument'
161 , $token // 'undef'
162 )
163 );
164 }
165 else
166 {
167 45 $result = $handler->new( $operand);
168 }
169 }
170 else
171 {
172 0 die $self->get_parse_error('Unknown operator handler: '.$handler);
173 }
174 }
175 else # got operator but there is no more operands
176 {
177 1 ▶ die $self->get_parse_error(
Condition50%1/2
$token // "undef"
l!l
10
MC/DC0%0/1
$token // "undef"
$token
Truth table50%1/2
A
A= $token
Aresult
11
00
178 sprintf('operator `%s` has no right argument'
179 , $token // 'undef'
180 )
181 );
182 }
183 }
184 }
185 179 ▶ last if $result; # parsed level
Branch: 1 branch
BranchTrueFalseTotal
if $result 179 0 179
186 }
187
188 }
189 return
190 442 ▶ $result
Condition66%2/3
$result // $self->get_backup_or_variable($expression)
l!l&&r!l&&!r
1792630
MC/DC0%0/2
$result // $self->get_backup_or_variable($expression)
$result $self->get_backup_or_variable($expression)
Truth table66%2/3
A // B
A= $resultB= $self->get_backup_or_variable($expression)
ABresult
1X1
011
000
191 // $self->get_backup_or_variable($expression)
192 ;
193 }
194  
195 98 1;
« blib/lib/DTL/Fast/Cache/Serialized.pm ↑ Summary blib/lib/DTL/Fast/Expression/Operator/Binary/Ge.pm »