lib/Badger/Logic.pm

filter: Statementstmt 94.7% 91 / 96 Branchbran 62.5% 25 / 40 Conditioncond 50.0% 15 / 30 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 26 / 26 Podpod 90.0% 9 / 10 totaltotal 77.2% 170 / 220 CC 33 SCAR 15.7
CC
33
Cov
79%
parse_expr
12.3
parse_term
8.9
parse_unary
5.3
CRAP 4.8
« lib/Badger/Workspace.pm ↑ Summary lib/Badger/Class.pm »
1 #========================================================================
2 #
3 # Badger::Logic
4 #
5 # DESCRIPTION
6 # Simple parser and evaluator for boolean logic expressions, e.g.
7 # 'purple or orange', 'animal and (eats_nuts or eats_berries)'
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 #========================================================================
13  
14 package Badger::Logic;
15  
16 use Badger::Class
17 2 ▶ version => 0.01,
Subroutine BEGIN: called CC=1 SCAR=0.0
18 debug => 0,
19 base => 'Badger::Base',
20 as_text => 'text',
21 constants => 'HASH',
22 constant => {
23 LOGIC => 'Badger::Logic',
24 },
25 exports => {
26 any => 'LOGIC Logic',
27 },
28 messages => {
29 no_text => 'No text expression specified.',
30 no_rhs => 'Missing expression following "%s"',
31 bad_text => 'Unexpected text in expression: %s',
32 parse => 'Could not parse logic expression: %s',
33 no_rparen => 'Missing ")" at end of nested expression',
34 2 };
35  
36 2 our $NODE = {
37 'item' => 'Badger::Logic::Item',
38 'not' => 'Badger::Logic::Not',
39 'and' => 'Badger::Logic::And',
40 'or' => 'Badger::Logic::Or',
41 };
42  
43 2 *test = \&evaluate;
44  
45  
46 3 ▶sub Logic {
Subroutine Logic: called CC=2 SCAR=6.9
Pod documented
47 return @_
48 3 ▶ ? LOGIC->new(@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 1 3
49 : LOGIC;
50 }
51  
52 26 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
53 26 my $class = shift;
54 26 my $text = shift;
55 26 ▶ return $class->error_msg('no_text')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 26 26
56 unless defined $text;
57 26 ▶ bless {
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 0 26 26
58 text => ref $text ? $text : \$text,
59 }, $class;
60 }
61  
62 21 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=12.5
Pod documented
63 21 my $self = shift;
64 21 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 21 0 21
Condition33%1/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
0021
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
65 21 $self->tree->evaluate($args);
66 }
67  
68 22 ▶sub tree {
Subroutine tree: called CC=2 SCAR=8.1
Pod documented
69 22 my $self = shift;
70 return $self->{ tree }
71 22 ▶ ||= $self->parse($self->{ text });
Condition33%1/3
$self->{"tree"} ||= $self->parse($self->{"text"})
l!l&&r!l&&!r
0220
MC/DC0%0/2
$self->{"tree"} ||= $self->parse($self->{"text"})
$self->{"tree"} $self->parse($self->{"text"})
Truth table33%1/3
A ||= B
A= $self->{"tree"}B= $self->parse($self->{"text"})
ABresult
1X1
011
000
72 }
73  
74 5 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
75 5 ${ shift->{ text } };
76 }
77  
78 1 ▶sub tree_text {
Subroutine tree_text: called CC=1 SCAR=0.0
Pod undocumented
79 1 shift->tree->text;
80 }
81  
82 22 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.6
Pod documented
83 22 my $self = shift;
84 22 my $text = shift;
85 22 ▶ my $tref = ref $text ? $text : \$text;
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 22 0 22
86 22 $self->debug("parse($$tref)\n") if DEBUG;
87 22 ▶ my $expr = $self->parse_expr($tref)
Condition50%1/2
$self->parse_expr($tref) || (return $self->error_msg('parse', $$tref))
l!l
220
MC/DC0%0/1
$self->parse_expr($tref) || (return $self->error_msg('parse', $$tref))
$self->parse_expr($tref)
Truth table50%1/2
A
A= $self->parse_expr($tref)
Aresult
11
00
88 || return $self->error_msg( parse => $$tref );
89 22 $self->debug("expr: ", $expr->text) if DEBUG;
90 22 ▶ if ($$tref =~ / \G \s* (.+) $/cigsx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$tref =~ / \G \s* (.+) $/cgisx) 0 22 22
91 0 return $self->error_msg( bad_text => $1 );
92 }
93 22 return $expr;
94 }
95  
96 73 ▶sub parse_expr {
Subroutine parse_expr: called CC=8 SCAR=25.1
Pod documented
97 73 my $self = shift;
98 73 my $text = shift;
99 73 ▶ my $left = $self->parse_unary($text) || return;
Condition50%1/2
$self->parse_unary($text) || (return)
l!l
730
MC/DC0%0/1
$self->parse_unary($text) || (return)
$self->parse_unary($text)
Truth table50%1/2
A
A= $self->parse_unary($text)
Aresult
11
00
100  
101 73 $self->debug("got unary: ", $left->text) if DEBUG;
102  
103 73 ▶ if ($$text =~ / \G \s+ (and|or) \s+ /cigx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$text =~ / \G \s+ (and|or) \s+ /cgix) { } 35 38 73
elsif ($$text =~ / \G \s* \( /cgx) { } 0 38 38
104 35 my $op = $1;
105 35 ▶ $self->debug("binary op: $op\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Logic::DEBUG 0 35 35
106 35 ▶ my $right = $self->parse_expr($text)
Condition50%1/2
$self->parse_expr($text) || (return $self->error_msg('no_rhs', $op))
l!l
350
MC/DC0%0/1
$self->parse_expr($text) || (return $self->error_msg('no_rhs', $op))
$self->parse_expr($text)
Truth table50%1/2
A
A= $self->parse_expr($text)
Aresult
11
00
107 || return $self->error_msg( no_rhs => $op );
108 35 return $NODE->{ lc $op }->new( $left, $right );
109 }
110 elsif ($$text =~ / \G \s* \( /cgx) {
111 0 ▶ my $expr = $self->parse_expr($text)
Condition0%0/2
$self->parse_expr($text) || (return $self->error_msg('no_rhs', '('))
l!l
00
MC/DC0%0/1
$self->parse_expr($text) || (return $self->error_msg('no_rhs', '('))
$self->parse_expr($text)
Truth table0%0/2
A
A= $self->parse_expr($text)
Aresult
11
00
112 || return $self->error_msg( no_rhs => '(' );
113 0 ▶ $$text =~ / \G \s* \) /cgx
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ / \G \s* \) /cgx or return $self->error_msg("no_rparen") 0 0 0
114 || return $self->error_msg('no_rparen');
115  
116 0 return $self->error_msg( bad_text => $1 );
117 }
118  
119 38 return $left;
120 }
121  
122 73 ▶sub parse_unary {
Subroutine parse_unary: called CC=5 SCAR=16.7
Pod documented
123 73 my $self = shift;
124 73 my $text = shift;
125  
126 73 ▶ if ($$text =~ / \G \s* (not) \s+ /cigx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ / \G \s* (not) \s+ /cgix) 13 60 73
127 13 my $op = $1;
128 13 ▶ $self->debug("unary op: $op\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Logic::DEBUG 0 13 13
129 13 ▶ my $right = $self->parse_term($text)
Condition50%1/2
$self->parse_term($text) || (return $self->error_msg('no_rhs', $op))
l!l
130
MC/DC0%0/1
$self->parse_term($text) || (return $self->error_msg('no_rhs', $op))
$self->parse_term($text)
Truth table50%1/2
A
A= $self->parse_term($text)
Aresult
11
00
130 || return $self->error_msg( no_rhs => $op );
131 13 return $NODE->{ lc $op }->new($right);
132 }
133 60 ▶ return $self->parse_term($text)
Condition33%1/3
$self->parse_term($text) || $self->decline('Not a unary expression')
l!l&&r!l&&!r
6000
MC/DC0%0/2
$self->parse_term($text) || $self->decline('Not a unary expression')
$self->parse_term($text) $self->decline('Not a unary expression')
Truth table33%1/3
A || B
A= $self->parse_term($text)B= $self->decline('Not a unary expression')
ABresult
1X1
011
000
134 || $self->decline('Not a unary expression');
135 }
136  
137 73 ▶sub parse_term {
Subroutine parse_term: called CC=8 SCAR=21.8
Pod documented
138 73 my $self = shift;
139 73 my $text = shift;
140  
141 73 ▶ if ($$text =~ / \G \s* (\w+) /cigx) {
Branch: 3 branches
BranchTrueFalseTotal
if ($$text =~ / \G \s* (\w+) /cgix) { } 54 19 73
elsif ($$text =~ / \G \s* (['"]) ((?:\\?.)*?) \1 /cgix) { } 3 16 19
elsif ($$text =~ / \G \s* \( /cgx) { } 16 0 16
142 54 ▶ $self->debug("item: $1\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Logic::DEBUG 0 54 54
143 54 return $NODE->{ item }->new($1);
144 }
145 elsif ($$text =~ / \G \s* (['"]) ((?:\\?.)*?) \1 /cigx) {
146 3 ▶ $self->debug("string: $2\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Logic::DEBUG 0 3 3
147 3 return $NODE->{ item }->new($2);
148 }
149 elsif ($$text =~ / \G \s* \( /cgx) {
150 16 ▶ my $expr = $self->parse_expr($text)
Condition50%1/2
$self->parse_expr($text) || (return $self->error_msg('no_rhs', '('))
l!l
160
MC/DC0%0/1
$self->parse_expr($text) || (return $self->error_msg('no_rhs', '('))
$self->parse_expr($text)
Truth table50%1/2
A
A= $self->parse_expr($text)
Aresult
11
00
151 || return $self->error_msg( no_rhs => '(' );
152 16 ▶ $$text =~ / \G \s* \) /cgx
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ / \G \s* \) /cgx or return $self->error_msg("no_rparen") 16 0 16
153 || return $self->error_msg('no_rparen');
154 16 return $expr;
155 }
156  
157 0 return $self->decline('Not a term');
158 }
159  
160  
161 #=======================================================================
162 # node types
163 #=======================================================================
164  
165 package Badger::Logic::Expr;
166 2 ▶use base 'Badger::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 105 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
169 105 my $class = shift;
170 105 bless [ @_ ], $class;
171 }
172  
173 package Badger::Logic::Item;
174 2 ▶use base 'Badger::Logic::Expr';
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 41 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=12.5
177 41 my $self = shift;
178 41 ▶ my $args = @_ && ref $_[0] eq 'HASH' ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 41 0 41
Condition33%1/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
0041
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
179 41 return $args->{ $self->[0] };
180 }
181  
182 4 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
183 4 $_[0]->[0];
184 }
185  
186 package Badger::Logic::Not;
187 2 ▶use base 'Badger::Logic::Expr';
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 12 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=6.9
190 12 my $self = shift;
191 12 ▶ return $self->[0]->evaluate(@_) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->[0]->evaluate(@_) ? : 7 5 12
192 }
193  
194 1 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
195 1 my $self = shift;
196 1 '(not ' . $self->[0]->text . ')';
197 }
198  
199 package Badger::Logic::And;
200 2 ▶use base 'Badger::Logic::Expr';
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 15 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=6.9
203 15 my $self = shift;
204 15 ▶ return $self->[0]->evaluate(@_)
Condition100%3/3
$self->[0]->evaluate(@_) && $self->[1]->evaluate(@_)
!ll&&!rl&&r
555
MC/DC100%2/2
$self->[0]->evaluate(@_) && $self->[1]->evaluate(@_)
$self->[0]->evaluate(@_) $self->[1]->evaluate(@_)
Truth table100%3/3
A && B
A= $self->[0]->evaluate(@_)B= $self->[1]->evaluate(@_)
ABresult
0X0
100
111
205 && $self->[1]->evaluate(@_);
206 }
207  
208 2 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
209 2 my $self = shift;
210 2 '(' . $self->[0]->text . ' and ' . $self->[1]->text . ')';
211 }
212  
213 package Badger::Logic::Or;
214 2 ▶use base 'Badger::Logic::Expr';
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 2 ▶use Badger::Debug ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
217 16 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=6.9
218 16 my $self = shift;
219 16 ▶ return $self->[0]->evaluate(@_)
Condition100%3/3
$self->[0]->evaluate(@_) || $self->[1]->evaluate(@_)
l!l&&r!l&&!r
664
MC/DC100%2/2
$self->[0]->evaluate(@_) || $self->[1]->evaluate(@_)
$self->[0]->evaluate(@_) $self->[1]->evaluate(@_)
Truth table100%3/3
A || B
A= $self->[0]->evaluate(@_)B= $self->[1]->evaluate(@_)
ABresult
1X1
011
000
220 || $self->[1]->evaluate(@_);
221 }
222  
223 1 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
224 1 my $self = shift;
225 1 '(' . $self->[0]->text . ' or ' . $self->[1]->text . ')';
226 }
227  
228 2 1;
229 __END__
« lib/Badger/Workspace.pm ↑ Summary lib/Badger/Class.pm »