/
BooleanLogic.pm
542 lines (418 loc) · 14.6 KB
/
BooleanLogic.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
=head1 NAME
Parse::BooleanLogic - parser of boolean expressions
=head1 SYNOPSIS
my $parser = new Parse::BooleanLogic;
my $tree = $parser->as_array( string => 'x = 10' );
$tree = $parser->as_array( string => 'x = 10 OR (x > 20 AND x < 30)' );
$parser->parse(
string => 'x = 10 OR (x > 20 AND x < 30)',
callback => {
open_paren => sub { ... },
operator => sub { ... },
operand => sub { ... },
close_paren => sub { ... },
error => sub { ... },
},
);
=head1 DESCRIPTION
This module is quite fast parser for boolean expressions. Originally it's been writen for
Request Tracker to parse SQL like expressions and it's still capable, but
it can be used to parse other boolean logic sentences with OPERANDs joined using
binary OPERATORs and grouped and nested using parentheses (OPEN_PAREN and CLOSE_PAREN).
Operand is not qualified strictly what makes parser flexible enough to parse different
things, for example:
# SQL like expressions
(task.status = "new" OR task.status = "open") AND task.owner_id = 123
# Google like search syntax used in Gmail and other service
subject:"some text" (from:me OR to:me) label:todo !label:done
# Binary boolean logic expressions
(a | b) & (c | d)
You can change literals used for boolean operators and parens. Read more
about this in description of constructor's arguments.
As you can see quoted strings are supported and based on delimited strings
from L<Regexp::Common> with ' and " as delimiters.
=cut
use strict;
use warnings;
package Parse::BooleanLogic;
our $VERSION = '0.05';
use constant OPERAND => 1;
use constant OPERATOR => 2;
use constant OPEN_PAREN => 4;
use constant CLOSE_PAREN => 8;
use constant STOP => 16;
my @tokens = qw[OPERAND OPERATOR OPEN_PAREN CLOSE_PAREN STOP];
use Regexp::Common qw(delimited);
my $re_delim = qr{$RE{delimited}{-delim=>qq{\'\"}}};
=head1 METHODS
=head2 Building parser
=head3 new
A constuctor, takes the following named arguments:
=over 4
=item operators, default is ['AND' 'OR']
Pair of literal strings representing boolean operators AND and OR,
pass it as array reference. For example:
# from t/custom_ops.t
my $parser = Parse::BooleanLogic->new( operators => [qw(& |)] );
# from t/custom_googlish.t
my $parser = Parse::BooleanLogic->new( operators => ['', 'OR'] );
It's ok to have any operators and even empty.
=item parens, default is ['(', ')']
Pair of literal strings representing parentheses, for example it's
possible to use curly braces:
# from t/custom_parens.t
my $parser = Parse::BooleanLogic->new( parens => [qw({ })] );
No matter which pair is used parens must be balanced in expression.
=back
This constructor compiles several heavy weight regular expressions
so it's better avoid building object right before parsing, but instead
use global or cached one.
=cut
sub new {
my $proto = shift;
my $self = bless {}, ref($proto) || $proto;
return $self->init( @_ );
}
=head3 init
An initializer, called from the constructor. Compiles regular expressions
and do other things with constructor's arguments. Returns this object back.
=cut
sub init {
my $self = shift;
my %args = @_;
if ( $args{'operators'} ) {
my @ops = map lc $_, @{ $args{'operators'} };
$self->{'operators'} = [ @ops ];
@ops = reverse @ops if length $ops[1] > length $ops[0];
foreach ( @ops ) {
unless ( length ) {
$_ = "(?<=\\s)";
}
else {
if ( /^\w/ ) {
$_ = '\b'. "\Q$_\E";
}
else {
$_ = "\Q$_\E";
}
if ( /\w$/ ) {
$_ .= '\b';
}
}
$self->{'re_operator'} = qr{(?:$ops[0]|$ops[1])}i;
}
} else {
$self->{'operators'} = [qw(and or)];
$self->{'re_operator'} = qr{\b(?:AND|OR)\b}i;
}
if ( $args{'parens'} ) {
$self->{'parens'} = $args{'parens'};
$self->{'re_open_paren'} = qr{\Q$args{'parens'}[0]\E};
$self->{'re_close_paren'} = qr{\Q$args{'parens'}[1]\E};
} else {
$self->{'re_open_paren'} = qr{\(};
$self->{'re_close_paren'} = qr{\)};
}
$self->{'re_tokens'} = qr{(?:$self->{'re_operator'}|$self->{'re_open_paren'}|$self->{'re_close_paren'})};
# the following need some explanation
# operand is something consisting of delimited strings and other strings that are not our major tokens
# so it's a (delim string or anything until a token, ['"](start of a delim) or \Z) - this is required part
# then you can have zero or more ocurences of above group, but with one exception - "anything" can not start with a token or ["']
$self->{'re_operand'} = qr{(?:$re_delim|.+?(?=$self->{re_tokens}|["']|\Z))(?:$re_delim|(?!$self->{re_tokens}|["']).+?(?=$self->{re_tokens}|["']|\Z))*};
foreach my $re (qw(re_operator re_operand re_open_paren re_close_paren)) {
$self->{"m$re"} = qr{\G($self->{$re})};
}
return $self;
}
=head2 Parsing expressions
=head3 as_array $string [ %options ]
Takes a string and parses it into perl structure, where parentheses represented using
array references, operands are hash references with one key/value pair: operand,
when binary operators are simple scalars. So string C<x = 10 OR (x > 20 AND x < 30)>
is parsed into the following structure:
[
{ operand => 'x = 10' },
'OR',
[
{ operand => 'x > 20' },
'AND',
{ operand => 'x < 30' },
]
]
Aditional options:
=over 4
=item operand_cb - custom operands handler
=item error_cb - custom errors handler
=back
=cut
{ # static variables
my ($tree, $node, @pnodes);
my %callback;
$callback{'open_paren'} = sub {
push @pnodes, $node;
push @{ $pnodes[-1] }, $node = []
};
$callback{'close_paren'} = sub { $node = pop @pnodes };
$callback{'operator'} = sub { push @$node, $_[0] };
$callback{'operand'} = sub { push @$node, { operand => $_[0] } };
sub as_array {
my $self = shift;
my $string = shift;
my %arg = (@_);
$node = $tree = [];
@pnodes = ();
unless ( $arg{'operand_cb'} || $arg{'error_cb'} ) {
$self->parse(string => $string, callback => \%callback);
return $tree;
}
my %cb = %callback;
if ( $arg{'operand_cb'} ) {
$cb{'operand'} = sub { push @$node, $arg{'operand_cb'}->( $_[0] ) };
}
$cb{'error'} = $arg{'error_cb'} if $arg{'error_cb'};
$self->parse(string => $string, callback => \%cb);
return $tree;
} }
=head3 parse
Takes named arguments: string and callback. Where the first one is scalar with
expression, the latter is a reference to hash with callbacks: open_paren, operator
operand, close_paren and error. Callback for errors is optional and parser dies if
it's omitted. Each callback is called when parser finds corresponding element in the
string. In all cases the current match is passed as argument into the callback.
Here is simple example based on L</as_array> method:
# result tree and the current group
my ($tree, $node);
$tree = $node = [];
# stack with nested groups, outer most in the bottom, inner on the top
my @pnodes = ();
my %callback;
# on open_paren put the current group on top of the stack,
# create new empty group and at the same time put it into
# the end of previous one
$callback{'open_paren'} = sub {
push @pnodes, $node;
push @{ $pnodes[-1] }, $node = []
};
# on close_paren just switch to previous group by taking it
# from the top of the stack
$callback{'close_paren'} = sub { $node = pop @pnodes };
# push binary operators as is and operands as hash references
$callback{'operator'} = sub { push @$node, $_[0] };
$callback{'operand'} = sub { push @$node, { operand => $_[0] } };
# run parser
$parser->parse( string => $string, callback => \%callback );
return $tree;
Using this method you can build other representations of an expression.
=cut
sub parse {
my $self = shift;
my %args = (
string => '',
callback => {},
@_
);
my ($string, $cb) = @args{qw(string callback)};
$string = '' unless defined $string;
# States
my $want = OPERAND | OPEN_PAREN | STOP;
my $last = 0;
my $depth = 0;
while (1) {
# State Machine
if ( $string =~ /\G\s+/gc ) {
}
elsif ( ($want & OPERATOR ) && $string =~ /$self->{'mre_operator'}/gc ) {
$cb->{'operator'}->( $1 );
$last = OPERATOR;
$want = OPERAND | OPEN_PAREN;
}
elsif ( ($want & OPEN_PAREN ) && $string =~ /$self->{'mre_open_paren'}/gc ) {
$cb->{'open_paren'}->( $1 );
$depth++;
$last = OPEN_PAREN;
$want = OPERAND | OPEN_PAREN;
}
elsif ( ($want & CLOSE_PAREN) && $string =~ /$self->{'mre_close_paren'}/gc ) {
$cb->{'close_paren'}->( $1 );
$depth--;
$last = CLOSE_PAREN;
$want = OPERATOR;
$want |= $depth? CLOSE_PAREN : STOP;
}
elsif ( ($want & OPERAND ) && $string =~ /$self->{'mre_operand'}/gc ) {
my $m = $1;
$m=~ s/\s+$//;
$cb->{'operand'}->( $m );
$last = OPERAND;
$want = OPERATOR;
$want |= $depth? CLOSE_PAREN : STOP;
}
elsif ( ($want & STOP) && $string =~ /\G\s*$/igc ) {
$last = STOP;
last;
}
else {
last;
}
}
if (!$last || !($want & $last)) {
my $tmp = substr( $string, 0, pos($string) );
$tmp .= '>>>here<<<'. substr($string, pos($string));
my $msg = "Incomplete or incorrect expression, expecting a ". $self->bitmask_to_string($want) ." in '$tmp'";
$cb->{'error'}? $cb->{'error'}->($msg): die $msg;
return;
}
if ( $depth ) {
my $msg = "Incomplete query, $depth paren(s) isn't closed in '$string'";
$cb->{'error'}? $cb->{'error'}->($msg): die $msg;
return;
}
}
sub bitmask_to_string {
my $self = shift;
my $mask = shift;
my @res;
for( my $i = 0; $i < @tokens; $i++ ) {
next unless $mask & (1<<$i);
push @res, $tokens[$i];
}
my $tmp = join ', ', splice @res, 0, -1;
unshift @res, $tmp if $tmp;
return join ' or ', @res;
}
=head2 Tree modifications
Several functions taking a tree of boolean expressions as returned by
L<as_array> method and changing it using a callback.
=head3 filter $tree $callback
Filters a tree using provided callback. The callback is called for each operand
in the tree and operand is left when it returns true value.
Boolean operators (AND/OR) are skipped according to parens and left first rule,
for example:
X OR Y AND Z -> X AND Z
X OR (Y AND Z) -> X OR Z
X OR Y AND Z -> Y AND Z
X OR (Y AND Z) -> Y AND Z
X OR Y AND Z -> X OR Y
X OR (Y AND Z) -> X OR Y
Returns new sub-tree. Original tree is not changed, but operands in new tree
still refer to the same hashes in original.
=cut
sub filter {
my ($self, $tree, $cb, $inner) = @_;
my $skip_next = 0;
my @res;
foreach my $entry ( @$tree ) {
$skip_next-- and next if $skip_next > 0;
if ( ref $entry eq 'ARRAY' ) {
my $tmp = $self->filter( $entry, $cb, 1 );
if ( !$tmp || (ref $tmp eq 'ARRAY' && !@$tmp) ) {
pop @res;
$skip_next++ unless @res;
} else {
push @res, $tmp;
}
} elsif ( ref $entry eq 'HASH' ) {
if ( $cb->( $entry ) ) {
push @res, $entry;
} else {
pop @res;
$skip_next++ unless @res;
}
} else {
push @res, $entry;
}
}
return $res[0] if @res == 1 && ($inner || ref $res[0] eq 'ARRAY');
return \@res;
}
=head3 solve $tree $callback
Solves a boolean expression using provided callback. Callback is called
for operands and should return a boolean value.
Functions matrixes:
A B AND OR
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Whole branches of the tree can be skipped when result is obvious, for example:
1 OR (...)
0 AND (...)
Returns result of the expression.
See also L</fsolve>.
=cut
sub solve {
my ($self, $tree, $cb) = @_;
my ($res, $ea, $skip_next) = (0, $self->{'operators'}[1], 0);
foreach my $entry ( @$tree ) {
$skip_next-- and next if $skip_next > 0;
unless ( ref $entry ) {
$ea = lc $entry;
$skip_next++ if
( $res && $ea eq $self->{'operators'}[1])
|| (!$res && $ea eq $self->{'operators'}[0]);
next;
}
my $cur;
if ( ref $entry eq 'ARRAY' ) {
$cur = $self->solve( $entry, $cb );
} else {
$cur = $cb->( $entry );
}
if ( $ea eq $self->{'operators'}[1] ) {
$res ||= $cur;
} else {
$res &&= $cur;
}
}
return $res;
}
=head3 fsolve $tree $callback
Does in filter+solve in one go. Callback can return undef to filter out an operand,
and a defined boolean value to be used in solve.
Returns boolean result of the equation or undef if all operands have been filtered.
See also L</filter> and L</solve>.
=cut
sub fsolve {
my ($self, $tree, $cb) = @_;
my ($res, $ea, $skip_next) = (undef, $self->{'operators'}[1], 0);
foreach my $entry ( @$tree ) {
$skip_next-- and next if $skip_next > 0;
unless ( ref $entry ) {
$ea = lc $entry;
$skip_next++ if
( $res && $ea eq $self->{'operators'}[1])
|| (!$res && $ea eq $self->{'operators'}[0]);
next;
}
my $cur;
if ( ref $entry eq 'ARRAY' ) {
$cur = $self->fsolve( $entry, $cb );
} else {
$cur = $cb->( $entry );
}
if ( defined $cur ) {
$res ||= 0;
if ( $ea eq $self->{'operators'}[1] ) {
$res ||= $cur;
} else {
$res &&= $cur;
}
} else {
$skip_next++ unless defined $res;
}
}
return $res;
}
1;
=head1 ALTERNATIVES
There are some alternative implementations available on the CPAN.
=over 4
=item L<Search::QueryParser> - similar purpose with several differences.
=item Another?
=back
=head1 AUTHORS
Ruslan Zakirov E<lt>ruz@cpan.orgE<gt>, Robert Spier E<lt>rspier@pobox.comE<gt>
=head1 COPYRIGHT
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut