blib/lib/Badger/Logic.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 34 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 221 CC 22 SCAR 27.1
CC
22
Cov
0%
parse_term
42.0
parse
30.0
parse_expr
30.0
CRAP 15.0
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 version => 0.01,
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 };
35  
36 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 *test = \&evaluate;
44  
45  
46 sub Logic {
47 return @_
48 ? LOGIC->new(@_)
49 : LOGIC;
50 }
51  
52 sub new {
53 my $class = shift;
54 my $text = shift;
55 return $class->error_msg('no_text')
56 unless defined $text;
57 bless {
58 text => ref $text ? $text : \$text,
59 }, $class;
60 }
61  
62 sub evaluate {
63 my $self = shift;
64 my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
65 $self->tree->evaluate($args);
66 }
67  
68 sub tree {
69 my $self = shift;
70 return $self->{ tree }
71 ||= $self->parse($self->{ text });
72 }
73  
74 sub text {
75 ${ shift->{ text } };
76 }
77  
78 sub tree_text {
79 shift->tree->text;
80 }
81  
82 sub parse {
83 my $self = shift;
84 my $text = shift;
85 my $tref = ref $text ? $text : \$text;
86 $self->debug("parse($$tref)\n") if DEBUG;
87 my $expr = $self->parse_expr($tref)
88 || return $self->error_msg( parse => $$tref );
89 $self->debug("expr: ", $expr->text) if DEBUG;
90 if ($$tref =~ / \G \s* (.+) $/cigsx) {
91 return $self->error_msg( bad_text => $1 );
92 }
93 return $expr;
94 }
95  
96 sub parse_expr {
97 my $self = shift;
98 my $text = shift;
99 my $left = $self->parse_unary($text) || return;
100  
101 $self->debug("got unary: ", $left->text) if DEBUG;
102  
103 if ($$text =~ / \G \s+ (and|or) \s+ /cigx) {
104 my $op = $1;
105 $self->debug("binary op: $op\n") if $DEBUG;
106 my $right = $self->parse_expr($text)
107 || return $self->error_msg( no_rhs => $op );
108 return $NODE->{ lc $op }->new( $left, $right );
109 }
110 elsif ($$text =~ / \G \s* \( /cgx) {
111 my $expr = $self->parse_expr($text)
112 || return $self->error_msg( no_rhs => '(' );
113 $$text =~ / \G \s* \) /cgx
114 || return $self->error_msg('no_rparen');
115  
116 return $self->error_msg( bad_text => $1 );
117 }
118  
119 return $left;
120 }
121  
122 sub parse_unary {
123 my $self = shift;
124 my $text = shift;
125  
126 if ($$text =~ / \G \s* (not) \s+ /cigx) {
127 my $op = $1;
128 $self->debug("unary op: $op\n") if $DEBUG;
129 my $right = $self->parse_term($text)
130 || return $self->error_msg( no_rhs => $op );
131 return $NODE->{ lc $op }->new($right);
132 }
133 return $self->parse_term($text)
134 || $self->decline('Not a unary expression');
135 }
136  
137 sub parse_term {
138 my $self = shift;
139 my $text = shift;
140  
141 if ($$text =~ / \G \s* (\w+) /cigx) {
142 $self->debug("item: $1\n") if $DEBUG;
143 return $NODE->{ item }->new($1);
144 }
145 elsif ($$text =~ / \G \s* (['"]) ((?:\\?.)*?) \1 /cigx) {
146 $self->debug("string: $2\n") if $DEBUG;
147 return $NODE->{ item }->new($2);
148 }
149 elsif ($$text =~ / \G \s* \( /cgx) {
150 my $expr = $self->parse_expr($text)
151 || return $self->error_msg( no_rhs => '(' );
152 $$text =~ / \G \s* \) /cgx
153 || return $self->error_msg('no_rparen');
154 return $expr;
155 }
156  
157 return $self->decline('Not a term');
158 }
159  
160  
161 #=======================================================================
162 # node types
163 #=======================================================================
164  
165 package Badger::Logic::Expr;
166 use base 'Badger::Base';
167  
168 sub new {
169 my $class = shift;
170 bless [ @_ ], $class;
171 }
172  
173 package Badger::Logic::Item;
174 use base 'Badger::Logic::Expr';
175  
176 sub evaluate {
177 my $self = shift;
178 my $args = @_ && ref $_[0] eq 'HASH' ? shift : { @_ };
179 return $args->{ $self->[0] };
180 }
181  
182 sub text {
183 $_[0]->[0];
184 }
185  
186 package Badger::Logic::Not;
187 use base 'Badger::Logic::Expr';
188  
189 sub evaluate {
190 my $self = shift;
191 return $self->[0]->evaluate(@_) ? 0 : 1;
192 }
193  
194 sub text {
195 my $self = shift;
196 '(not ' . $self->[0]->text . ')';
197 }
198  
199 package Badger::Logic::And;
200 use base 'Badger::Logic::Expr';
201  
202 sub evaluate {
203 my $self = shift;
204 return $self->[0]->evaluate(@_)
205 && $self->[1]->evaluate(@_);
206 }
207  
208 sub text {
209 my $self = shift;
210 '(' . $self->[0]->text . ' and ' . $self->[1]->text . ')';
211 }
212  
213 package Badger::Logic::Or;
214 use base 'Badger::Logic::Expr';
215  
216 use Badger::Debug ':all';
217 sub evaluate {
218 my $self = shift;
219 return $self->[0]->evaluate(@_)
220 || $self->[1]->evaluate(@_);
221 }
222  
223 sub text {
224 my $self = shift;
225 '(' . $self->[0]->text . ' or ' . $self->[1]->text . ')';
226 }
227  
228 1;
229 __END__