Query/Parser.pm

filter: Statementstmt 90.4% 351 / 388 Branchbran 75.5% 142 / 188 Conditioncond 71.8% 79 / 110 MC/DCmcdc 50.7% 32 / 63 Subroutinesub 100% 44 / 44 Podpod 100% 7 / 7 totaltotal 81.8% 655 / 800 CC 140 SCAR 38.4
CC
140
Cov
83%
_parse
109.1
set_fields
26.5
parse
17.1
CRAP 46.4
↑ Summary Query/Dialect/SWISH.pm »
1 package Search::Query::Parser;
2 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Search::Query::Dialect::Native;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Search::Query::Clause;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Search::Query::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $VERSION = '0.306';
13  
14 27 ▶has 'and_regex' => ( is => 'rw', default => sub {qr/\&|AND|ET|UND|E/i} );
Subroutine __ANON__: called CC=1 SCAR=0.0
15 has 'clause_class' =>
16 27 ▶ ( is => 'rw', default => sub {'Search::Query::Clause'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
17 13 ▶has 'croak_on_error' => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
18 21 ▶has 'default_boolop' => ( is => 'rw', default => sub {'+'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 9 has 'default_field' => ( is => 'rw' );
20 27 ▶has 'default_op' => ( is => 'rw', default => sub {':'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 27 ▶has 'field_class' => ( is => 'rw', default => sub {'Search::Query::Field'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 # match prefix.field: or field
24 27 ▶has 'field_regex' => ( is => 'rw', default => sub {qr/[\.\w]+/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 27 ▶has 'fixup' => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
27 27 ▶has 'near_regex' => ( is => 'rw', default => sub {qr/NEAR\d+/i}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 26 ▶has 'not_regex' => ( is => 'rw', default => sub {qr/NOT|PAS|NICHT|NON/i}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 9 has 'null_term' => ( is => 'rw', );
30  
31 # ops that admit an empty left operand
32 27 ▶has 'op_nofield_regex' => ( is => 'rw', default => sub {qr/=~|!~|[~:#]/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 # longest ops first !
35 has 'op_regex' =>
36 27 ▶ ( is => 'rw', default => sub {qr/~\d+|==|<=|>=|!=|!:|=~|!~|[:=<>~#]/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 27 ▶has 'or_regex' => ( is => 'rw', default => sub {qr/\||OR|OU|ODER|O/i}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
39 27 ▶has 'phrase_delim' => ( is => 'rw', default => sub {q/"/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
40 has 'query_class' =>
41 10 ▶ ( is => 'rw', default => sub {'Search::Query::Dialect::Native'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
42 18 ▶has 'query_class_opts' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 27 ▶has 'range_regex' => ( is => 'rw', default => sub {qr/\.\./}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 26 ▶has 'sloppy' => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
45 27 ▶has 'sloppy_term_regex' => ( is => 'rw', default => sub {qr/[\.\w]+/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
46 9 has 'term_expander' => ( is => 'rw' );
47 27 ▶has 'term_regex' => ( is => 'rw', default => sub {qr/[^\s()]+/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 9 has 'error' => ( is => 'ro' );
49 9 has 'fields' => ( is => 'ro' );
50  
51 9 my %SQPCOMPAT = (
52 rxAnd => 'and_regex',
53 rxOr => 'or_regex',
54 rxNot => 'not_regex',
55 defField => 'default_field',
56 rxTerm => 'term_regex',
57 rxField => 'field_regex',
58 rxOp => 'op_regex',
59 rxOpNoField => 'op_nofield_regex',
60 dialect => 'query_class', # our own compat
61 );
62  
63 =head1 NAME
64  
65 Search::Query::Parser - convert query strings into query objects
66  
67 =head1 SYNOPSIS
68  
69 use Search::Query;
70 my $parser = Search::Query->parser(
71 term_regex => qr/[^\s()]+/,
72 field_regex => qr/\w+/,
73 op_regex => qr/==|<=|>=|!=|=~|!~|[:=<>~#]/,
74  
75 # ops that admit an empty left operand
76 op_nofield_regex => qr/=~|!~|[~:#]/,
77  
78 # case insensitive
79 and_regex => qr/\&|AND|ET|UND|E/i,
80 or_regex => qr/\||OR|OU|ODER|O/i,
81 not_regex => qr/NOT|PAS|NICHT|NON/i,
82  
83 default_field => 'myfield', # or ['myfield', 'myfield2']
84 phrase_delim => q/"/,
85 default_boolop => '+',
86 query_class => 'Search::Query::Dialect::Native',
87 field_class => 'Search::Query::Field',
88 query_class_opts => {
89 default_field => 'foo', # or ['foo', 'bar']
90 },
91
92 # a generous mode, overlooking boolean-parser syntax errors
93 sloppy => 0,
94 sloppy_term_regex => qr/[\.\w]+/,
95 fixup => 0,
96
97 # if set, this special term indicates a NULL query
98 null_term => 'NULL',
99 );
100  
101 my $query = $parser->parse('+hello -world now');
102 print $query;
103  
104 =head1 DESCRIPTION
105  
106 Search::Query::Parser is a fork of Search::QueryParser
107 that supports multiple query dialects.
108  
109 The Parser class transforms a query string into a Dialect object structure
110 to be handled by external search engines.
111  
112 The query string can contain simple terms, "exact phrases", field
113 names and comparison operators, '+/-' prefixes, parentheses, and
114 boolean connectors.
115  
116 The parser can be customized using regular expressions for specific
117 notions of "term", "field name" or "operator" -- see the L<new>
118 method.
119  
120 The Dialect object resulting from a parsed query is a tree of terms
121 and operators. Each Dialect can be re-serialized as a string
122 using the stringify() method, or simply by printing the Dialect object,
123 since the string-related Perl operations are overloaded using stringify().
124  
125 =head1 QUERY STRING
126  
127 The query string is decomposed into Clause objects, where
128 each Clause has an optional sign prefix,
129 an optional field name and comparison operator,
130 and a mandatory value.
131  
132 =head2 Sign prefix
133  
134 Prefix '+' means that the item is mandatory.
135 Prefix '-' means that the item must be excluded.
136 No prefix means that the item will be searched
137 for, but is not mandatory.
138  
139 See also section L<Boolean connectors> below, which is another
140 way to combine items into a query.
141  
142 =head2 Field name and comparison operator
143  
144 Internally, each query item has a field name and comparison
145 operator; if not written explicitly in the query, these
146 take default values C<''> (empty field name) and
147 C<':'> (colon operator).
148  
149 Operators have a left operand (the field name) and
150 a right operand (the value to be compared with);
151 for example, C<foo:bar> means "search documents containing
152 term 'bar' in field 'foo'", whereas C<foo=bar> means
153 "search documents where field 'foo' has exact value 'bar'".
154  
155 Here is the list of admitted operators with their intended meaning:
156  
157 =over
158  
159 =item C<:>
160  
161 treat value as a term to be searched within field.
162 This is the default operator.
163  
164 =item C<~> or C<=~>
165  
166 treat value as a regex; match field against the regex.
167  
168 Note that C<~>
169 after a phrase indicates a proximity assertion:
170  
171 "foo bar"~5
172  
173 means "match 'foo' and 'bar' within 5 positions of each other."
174  
175 =item C<!~>
176  
177 negation of above
178  
179 =item C<==> or C<=>, C<E<lt>=>, C<E<gt>=>, C<!=>, C<E<lt>>, C<E<gt>>
180  
181 classical relational operators
182  
183 =item C<#>
184  
185 Inclusion in the set of comma-separated integers supplied
186 on the right-hand side.
187  
188 =back
189  
190 Operators C<:>, C<~>, C<=~>, C<!~> and C<#> admit an empty
191 left operand (so the field name will be C<''>).
192 Search engines will usually interpret this as
193 "any field" or "the whole data record". But see the B<default_field>
194 feature.
195  
196 =head2 Value
197  
198 A value (right operand to a comparison operator) can be
199  
200 =over
201  
202 =item *
203  
204 A term (as recognized by regex C<term_regex>, see L<new> method below).
205  
206 =item *
207  
208 A quoted phrase, i.e. a collection of terms within
209 single or double quotes.
210  
211 Quotes can be used not only for "exact phrases", but also
212 to prevent misinterpretation of some values : for example
213 C<-2> would mean "value '2' with prefix '-'",
214 in other words "exclude term '2'", so if you want to search for
215 value -2, you should write C<"-2"> instead.
216  
217 Note that C<~>
218 after a phrase indicates a proximity assertion:
219  
220 "foo bar"~5
221  
222 means "match 'foo' and 'bar' within 5 positions of each other."
223  
224 =item *
225  
226 A subquery within parentheses.
227 Field names and operators distribute over parentheses, so for
228 example C<foo:(bar bie)> is equivalent to
229 C<foo:bar foo:bie>.
230  
231 Nested field names such as C<foo:(bar:bie)> are not allowed.
232  
233 Sign prefixes do not distribute : C<+(foo bar) +bie> is not
234 equivalent to C<+foo +bar +bie>.
235  
236 =back
237  
238 =head2 Boolean connectors
239  
240 Queries can contain boolean connectors 'AND', 'OR', 'NOT'
241 (or their equivalent in some other languages -- see the *_regex
242 features in new()).
243 This is mere syntactic sugar for the '+' and '-' prefixes :
244 C<a AND b> is equivalent to C<+a +b>;
245 C<a OR b> is equivalent to C<(a b)>;
246 C<NOT a> is equivalent to C<-a>.
247 C<+a OR b> does not make sense,
248 but it is translated into C<(a b)>, under the assumption
249 that the user understands "OR" better than a
250 '+' prefix.
251 C<-a OR b> does not make sense either,
252 but has no meaningful approximation, so it is rejected.
253  
254 Combinations of AND/OR clauses must be surrounded by
255 parentheses, i.e. C<(a AND b) OR c> or C<a AND (b OR c)> are
256 allowed, but C<a AND b OR c> is not.
257  
258 The C<NEAR> connector is treated like the proximity phrase assertion.
259  
260 foo NEAR5 bar
261  
262 is treated as if it were:
263  
264 "foo bar"~5
265  
266 See the B<near_regex> option.
267  
268 =head1 METHODS
269  
270 =head2 new
271  
272 The following attributes may be initialized in new().
273 These are also available as get/set methods on the returned
274 Parser object.
275  
276 =over
277  
278 =item default_boolop
279  
280 =item term_regex
281  
282 =item field_regex
283  
284 =item op_regex
285  
286 =item op_nofield_regex
287  
288 =item and_regex
289  
290 =item or_regex
291  
292 =item not_regex
293  
294 =item near_regex
295  
296 =item range_regex
297  
298 =item default_field
299  
300 Applied to all terms where no field is defined.
301 The default value is undef (no default).
302  
303 =item default_op
304  
305 The operator used when default_field is applied.
306  
307 =item fields
308  
309 =item phrase_delim
310  
311 =item query_class
312  
313 C<dialect> is an alias for C<query_class>.
314  
315 =item field_class
316  
317 =item clause_class
318  
319 =item query_class_opts
320  
321 Will be passed to I<query_class> new() method each time a query is parse()'d.
322  
323 =item dialect_opts
324  
325 Alias for query_class_opts.
326  
327 =item croak_on_error
328  
329 Default value is false (0). Set to true to automatically throw an exception
330 via Carp::croak() if parse() would return undef.
331  
332 =item term_expander
333  
334 A function reference for transforming query terms after they have been parsed.
335 Examples might include adding alternate spellings, synonyms, or
336 expanding wildcards based on lexicon listings.
337  
338 Example:
339  
340 my $parser = Search::Query->parser(
341 term_expander => sub {
342 my ($term, $field) = @_;
343 return ($term) if ref $term; # skip ranges
344 return ( qw( one two three ), $term );
345 }
346 );
347  
348 my $query = $parser->parse("foo=bar")
349 print "$query\n"; # +foo=(one OR two OR three OR bar)
350  
351 The term_expander reference should expect two arguments: the term value
352 and, if available, the term field name. It should return an array of values.
353  
354 The term_expander reference is called internally during the parse() method,
355 B<before> any field alias expansion or validation is performed.
356  
357 =item sloppy( 0|1 )
358  
359 If the string passed to parse() has any incorrect or unsupported syntax
360 in it, the default behavior is for parsing to stop immediately, error()
361 to be set, and for parse() to return undef.
362  
363 In certain cases (as on a web form) this is undesirable. Set sloppy
364 mode to true to fallback to non-boolean evaluation of the string,
365 which in most cases should still return a Dialect object.
366  
367 Example:
368  
369 $parser->parse('foo -- OR bar'); # if sloppy==0, returns undef
370 $parser->parse('foo -- OR bar'); # if sloppy==1, equivalent to 'foo bar'
371  
372 =item sloppy_term_regex
373  
374 The regex definition used to match a term when sloppy==1.
375  
376 =item fixup( 0|1 )
377  
378 Attempt to fix syntax errors like the lack of a closing parenthesis
379 or a missing double-quote. Different than sloppy() which will not
380 attempt to fix broken syntax, but should probably be used together
381 if you really do not care about strict syntax checking.
382  
383 =item null_term
384  
385 If set to I<term>, the B<null_term> feature will treat field value
386 of I<term> as if it was undefined. Example:
387  
388 $parser->parse('foo='); # throws fatal error
389 $parser->null_term('NULL');
390 $parser->parse('foo=NULL'); # field foo has NULL value
391  
392 This feature is most useful with the SQL dialect, where you might want to
393 find NULL values. Use it like:
394  
395 my $parser = Search::Query->parser(
396 dialect => 'SQL',
397 null_term => 'NULL'
398 );
399 my $query = $parser->parse('foo!=NULL');
400 print $query; # prints "foo is not NULL"
401  
402  
403 =back
404  
405 =head2 BUILDARGS
406  
407 Internal method for mangling constructor params.
408  
409 =cut
410  
411 sub BUILDARGS {
412 27 ▶ my ( $class, %args ) = @_;
Subroutine BUILDARGS: called CC=4 SCAR=14.1
Pod documented
413  
414 # Search::QueryParser compatability
415 27 ▶ if ( exists $args{dialect_opts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"dialect_opts"}) 0 27 27
416 0 $args{query_class_opts} = delete $args{dialect_opts};
417 }
418 27 for my $key ( keys %args ) {
419 83 ▶ if ( exists $SQPCOMPAT{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SQPCOMPAT{$key}) 17 66 83
420 17 $args{ $SQPCOMPAT{$key} } = delete $args{$key};
421 }
422 }
423 27 return \%args;
424 }
425  
426 =head2 BUILD
427  
428 Called internally to initialize the object.
429  
430 =cut
431  
432 sub BUILD {
433 27 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
434  
435 # query class can be shortcut
436 $self->{query_class}
437 27 = Search::Query->get_query_class( $self->{query_class} );
438  
439 # use field class if query class defines one
440 # and we weren't passed one explicitly
441 27 ▶ if ( $self->{query_class}->field_class ne $self->{field_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"query_class"}->field_class ne $self->{"field_class"}) 17 10 27
442 17 $self->{field_class} = $self->{query_class}->field_class;
443 }
444  
445 27 ▶ $self->set_fields( $self->{fields} ) if $self->{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fields"} 22 5 27
446  
447 27 return $self;
448 }
449  
450 =head2 error
451  
452 Returns the last error message.
453  
454 =cut
455  
456 =head2 clear_error
457  
458 Sets error message to undef.
459  
460 =cut
461  
462 sub clear_error {
463 2 ▶ $_->{error} = undef;
Subroutine clear_error: called CC=1 SCAR=0.0
Pod documented
464 }
465  
466 =head2 get_field( I<name> )
467  
468 Returns Field object for I<name> or undef if there isn't one
469 defined.
470  
471 =cut
472  
473 sub get_field {
474 230 ▶ my $self = shift;
Subroutine get_field: called CC=3 SCAR=11.0
Pod documented
475 230 ▶ my $name = shift or croak "name required";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or croak("name required") 0 230 230
476 230 ▶ if ( !exists $self->{fields}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"fields"}{$name}) 8 222 230
477 8 return undef;
478 }
479 222 return $self->{fields}->{$name};
480 }
481  
482 =head2 set_fields( I<fields> )
483  
484 Set the I<fields> structure. Called internally by BUILD()
485 if you pass a C<fields> key/value pair to new().
486  
487 The structure of I<fields> may be one of the following:
488  
489 my $fields = {
490 field1 => 1,
491 field2 => { alias_for => 'field1' },
492 field3 => Search::Query::Field->new( name => 'field3' ),
493 field4 => { alias_for => [qw( field1 field3 )] },
494 };
495  
496 # or
497  
498 my $fields = [
499 'field1',
500 { name => 'field2', alias_for => 'field1' },
501 Search::Query::Field->new( name => 'field3' ),
502 { name => 'field4', alias_for => [qw( field1 field3 )] },
503 ];
504  
505  
506 =cut
507  
508 sub set_fields {
509 22 ▶ my $self = shift;
Subroutine set_fields: called CC=16 SCAR=32.8
Pod documented
510 22 my $origfields = shift;
511 22 ▶ if ( !defined $origfields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $origfields) 0 22 22
512 0 croak "fields required";
513 }
514  
515 22 my %fields;
516 22 my $field_class = $self->{field_class};
517  
518 22 my $reftype = ref($origfields);
519 22 ▶ if ( !$reftype or ( $reftype ne 'ARRAY' and $reftype ne 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $reftype or $reftype ne "ARRAY" and $reftype ne "HASH") 0 22 22
Condition50%3/6
not $reftype or $reftype ne "ARRAY" and $reftype ne "HASH"
l!l&&r!l&&!r
0022
$reftype ne "ARRAY" and $reftype ne "HASH"
!ll&&!rl&&r
1840
MC/DC0%0/3
not $reftype or $reftype ne "ARRAY" and $reftype ne "HASH"
not $reftype $reftype ne "ARRAY" $reftype ne "HASH"
Truth table25%1/4
not(A) or B and C
A= not $reftypeB= $reftype ne "ARRAY"C= $reftype ne "HASH"
ABCresult
1XX1
0111
00X0
0100
520 0 croak "fields must be an ARRAY or HASH ref";
521 }
522  
523 # convert simple array to hash
524 22 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 18 4 22
elsif ($reftype eq 'HASH') { } 4 0 4
525 18 for my $name (@$origfields) {
526 34 ▶ if ( blessed($name) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($name)) { } 0 34 34
elsif (ref $name eq 'HASH') { } 1 33 34
527 0 $fields{ $name->name } = $name;
528 }
529 elsif ( ref($name) eq 'HASH' ) {
530 1 ▶ if ( !exists $name->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $name->{"name"}) 0 1 1
531 0 croak "'name' required in hashref: " . dump($name);
532 }
533 1 $fields{ $name->{name} } = $field_class->new(%$name);
534 }
535 else {
536 33 $fields{$name} = $field_class->new( name => $name, );
537 }
538 }
539 }
540 elsif ( $reftype eq 'HASH' ) {
541 4 for my $name ( keys %$origfields ) {
542 12 my $val = $origfields->{$name};
543 12 my $obj;
544 12 ▶ if ( blessed($val) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($val)) { } 0 12 12
elsif (ref $val eq 'HASH') { } 12 0 12
elsif (not ref $val) { } 0 0 0
545 0 $obj = $val;
546 }
547 elsif ( ref($val) eq 'HASH' ) {
548 12 ▶ if ( !exists $val->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $val->{"name"}) 12 0 12
549 12 $val->{name} = $name;
550 }
551 12 $obj = $field_class->new(%$val);
552 }
553 elsif ( !ref $val ) {
554 0 $obj = $field_class->new( name => $name );
555 }
556 else {
557 0 croak
558 "field value for $name must be a field name, hashref or Field object";
559 }
560 12 $fields{$name} = $obj;
561 }
562 }
563  
564 22 $self->{fields} = \%fields;
565 22 return $self->{fields};
566 }
567  
568 =head2 set_field( I<name> => I<field_object> )
569  
570 Sets field I<name> to Field object I<field_object>.
571  
572 =cut
573  
574 sub set_field {
575 8 ▶ my $self = shift;
Subroutine set_field: called CC=4 SCAR=14.5
Pod documented
576 8 my ( $name, $field ) = @_;
577 8 ▶ confess "name required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 8 8
578 8 ▶ confess "field object required" unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 8 8
579 8 ▶ confess "field not an object: $field" unless blessed($field);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($field) 0 8 8
580 8 $self->{fields}->{$name} = $field;
581 }
582  
583 =head2 parse( I<string> )
584  
585 Returns a Search::Query::Dialect object of type
586 I<query_class>.
587  
588 If there is a syntax error in I<string>,
589 parse() will return C<undef> and set error().
590  
591 =cut
592  
593 sub parse {
594 82 ▶ my $self = shift;
Subroutine parse: called CC=16 SCAR=28.4
Pod documented
595 82 my $q = shift;
596 82 ▶ croak "query required" unless defined $q;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q 0 82 82
597 82 ▶ my $class = shift || $self->query_class;
Condition33%1/3
shift() || $self->query_class
l!l&&r!l&&!r
0820
MC/DC0%0/2
shift() || $self->query_class
shift() $self->query_class
Truth table33%1/3
A || B
A= shift()B= $self->query_class
ABresult
1X1
011
000
598  
599 # reset state in case we are called multiple times
600 82 $self->{error} = undef;
601 82 $self->{_paren_count} = 0;
602  
603 82 $q = $class->preprocess($q);
604 82 my ($query) = $self->_parse( $q, undef, undef, $class );
605 82 ▶ if ( !defined $query && !$self->sloppy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query or $self->sloppy) 2 80 82
Condition100%3/3
defined $query or $self->sloppy
l!l&&r!l&&!r
7912
MC/DC100%2/2
defined $query or $self->sloppy
defined $query $self->sloppy
Truth table100%3/3
A or B
A= defined $queryB= $self->sloppy
ABresult
1X1
011
000
606 2 ▶ croak $self->error if $self->croak_on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->croak_on_error 1 1 2
607 1 return $query;
608 }
609  
610 # if in sloppy mode and we failed to parse,
611 # extract what looks like terms and re-parse.
612 80 ▶ if ( !defined $query && $self->sloppy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $query and $self->sloppy) 1 79 80
Condition66%2/3
not defined $query and $self->sloppy
!ll&&!rl&&r
7901
MC/DC50%1/2
not defined $query and $self->sloppy
not defined $query $self->sloppy
Truth table66%2/3
not(A) and B
A= not defined $queryB= $self->sloppy
ABresult
0X0
100
111
613 1 return $self->_sloppify( $q, $class );
614 }
615  
616 79 ▶ if ( $self->{term_expander} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term_expander"}) 3 76 79
617 3 $self->_call_term_expander($query);
618 }
619  
620 79 ▶ if ( $self->{fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fields"}) 66 13 79
621 66 $self->_expand($query);
622 66 $self->_validate($query);
623 }
624  
625 # if in sloppy mode and we failed to parse,
626 # extract what looks like terms and re-parse.
627 77 ▶ if ( $self->error && $self->sloppy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error and $self->sloppy) 1 76 77
Condition66%2/3
$self->error and $self->sloppy
!ll&&!rl&&r
7601
MC/DC50%1/2
$self->error and $self->sloppy
$self->error $self->sloppy
Truth table66%2/3
A and B
A= $self->errorB= $self->sloppy
ABresult
0X0
100
111
628 1 return $self->_sloppify( $q, $class );
629 }
630  
631 76 $query->{parser} = $self;
632  
633 #warn dump $query;
634  
635 # if the query isn't re-parse-able once stringified
636 # then it is broken, somehow.
637 76 ▶ if ( defined $query
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and not $self->error and $self->croak_on_error) 26 50 76
Condition50%3/6
defined $query and not $self->error and $self->croak_on_error
!ll&&!rl&&r
05026
defined $query and not $self->error
!ll&&!rl&&r
0076
MC/DC33%1/3
defined $query and not $self->error and $self->croak_on_error
defined $query not $self->error $self->croak_on_error
Truth table50%2/4
A and not(B) and C
A= defined $queryB= not $self->errorC= $self->croak_on_error
ABCresult
0XX0
10X0
1100
1111
638 and !$self->error
639 and $self->croak_on_error )
640 {
641 26 my ($reparsed) = $self->_parse( "$query", undef, undef, $class );
642 26 ▶ if ( !defined $reparsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reparsed) 0 26 26
643 0 croak sprintf( "Error: unable to parse '%s'. Reason: '%s'.",
644 $q, $self->error );
645 }
646 }
647  
648 #weaken( $query->{parser} ); # TODO leaks possible?
649  
650 76 return $query;
651 }
652  
653 sub _sloppify {
654 2 ▶ my ( $self, $q, $class ) = @_;
Subroutine _sloppify: called CC=5 SCAR=16.2
655 2 my $term = $self->{sloppy_term_regex};
656 2 my $and = $self->{and_regex};
657 2 my $or = $self->{or_regex};
658 2 my $not = $self->{not_regex};
659 2 my $near = $self->{near_regex};
660 2 my $ops = $self->{op_regex};
661 2 my $bools = qr/($and|$or|$not|$near|$ops)/;
662 2 my @terms;
663  
664 2 ▶ while ( $q =~ m/($term)/ig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q =~ /($term)/gi) 14 2 16
665 14 my $t = $1;
666  
667 #warn "$t =~ $bools\n";
668 14 ▶ if ( $t =~ m/^$bools$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /^$bools$/) 7 7 14
669 7 next;
670 }
671 7 push @terms, split( /$ops/, $t );
672 }
673  
674 #dump \@terms;
675  
676 # reset errors since we will re-parse
677 2 $self->{error} = undef;
678 2 my ($query) = $self->_parse( join( ' ', @terms ), undef, undef, $class );
679 2 ▶ if ( !defined $query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $query) { } 0 2 2
680 0 ▶ $self->croak_on_error and croak $self->error;
Branch: 1 branch
BranchTrueFalseTotal
$self->croak_on_error and croak($self->error) 0 0 0
681 }
682 else {
683 2 $query->{parser} = $self;
684 }
685 2 return $query;
686 }
687  
688 sub _call_term_expander {
689 3 ▶ my ( $self, $query ) = @_;
Subroutine _call_term_expander: called CC=2 SCAR=7.2
690 3 my $expander = $self->{term_expander};
691 3 ▶ if ( ref($expander) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expander ne "CODE") 0 3 3
692 0 croak "term_expander must be a CODE reference";
693 }
694  
695 3 my $query_class = $self->{query_class};
696  
697 $query->walk(
698 sub {
699 3 ▶ my ( $clause, $tree, $code, $prefix ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.7
700 3 ▶ if ( $clause->is_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->is_tree) 0 3 3
701 0 $clause->value->walk($code);
702 0 return;
703 }
704  
705 3 my @newterms = $expander->( $clause->value, $clause->field );
706 3 ▶ if ( ref $newterms[0] and ref $clause->value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $newterms[0] and ref $clause->value) { } 0 3 3
elsif (@newterms > 1) { } 1 2 3
Condition33%1/3
ref $newterms[0] and ref $clause->value
!ll&&!rl&&r
300
MC/DC0%0/2
ref $newterms[0] and ref $clause->value
ref $newterms[0] ref $clause->value
Truth table33%1/3
A and B
A= ref $newterms[0]B= ref $clause->value
ABresult
0X0
100
111
707 0 $clause->value( $newterms[0] );
708 }
709 elsif ( @newterms > 1 ) {
710  
711 # turn $clause into a tree
712 1 my $class = blessed($clause);
713 1 my $op = $clause->op;
714 1 my $field = $clause->field;
715 1 my $proximity = $clause->proximity;
716 1 my $quote = $clause->quote;
717  
718 #warn "before tree: " . dump $tree;
719  
720 #warn "code clause: " . dump $clause;
721 1 my @subclauses;
722 1 for my $term (@newterms) {
723 4 push(
724 @subclauses,
725 $class->new(
726 field => $field,
727 op => $op,
728 value => $term,
729 quote => $quote,
730 proximity => $proximity,
731 )
732 );
733 }
734  
735 # OR the fields together. TODO optional?
736  
737 # we must set "" key here explicitly, because
738 # our bool op keys are not methods.
739 my $subclause
740 1 = $query_class->new( %{ $self->query_class_opts },
741 parser => $self );
742 1 $subclause->{""} = \@subclauses;
743  
744 1 $clause->op('()');
745 1 $clause->value($subclause);
746 }
747 else {
748 2 $clause->value( $newterms[0] );
749 }
750  
751 }
752 3 );
753  
754 }
755  
756 sub _expand {
757 66 ▶ my ( $self, $query ) = @_;
Subroutine _expand: called CC=2 SCAR=6.9
758  
759 66 ▶ return if !exists $self->{fields};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fields"} 0 66 66
760 66 my $fields = $self->{fields};
761 66 my $query_class = $self->{query_class};
762 66 my $default_field = $self->{default_field};
763  
764 #dump $fields;
765  
766 $query->walk(
767 sub {
768 165 ▶ my ( $clause, $tree, $code, $prefix ) = @_;
Subroutine __ANON__: called CC=16 SCAR=27.7
769  
770 #warn "code clause: " . dump $clause;
771  
772 #warn "code tree: " . dump $tree;
773  
774 165 ▶ if ( $clause->is_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->is_tree) 46 119 165
775 46 $clause->value->walk($code);
776 46 return;
777 }
778 119 ▶ if ( ( !defined $clause->field || !length $clause->field )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clause->field and length $clause->field or defined $default_field) 13 106 119
Condition100%6/6
defined $clause->field and length $clause->field or defined $default_field
l!l&&r!l&&!r
901613
defined $clause->field and length $clause->field
!ll&&!rl&&r
28190
MC/DC100%3/3
defined $clause->field and length $clause->field or defined $default_field
defined $clause->field length $clause->field defined $default_field
Truth table80%4/5
A and B or C
A= defined $clause->fieldB= length $clause->fieldC= defined $default_field
ABCresult
11X1
0X11
1011
0X00
1000
779 && !defined $default_field )
780 {
781 13 return;
782 }
783  
784 # make sure clause has an op
785 106 ▶ if ( !$clause->op ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($clause->op) 16 90 106
786 16 $clause->op( $self->default_op );
787 }
788  
789 # even if $clause has a field defined,
790 # it may be aliased to multiple others,
791 # so check field def and default_field to determine.
792 106 my @field_names;
793  
794 # first, which field name to start with?
795 my @clause_fields; # could be plural
796 106 ▶ if ( !defined $clause->field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $clause->field) { } 16 90 106
797 @clause_fields
798 16 ▶ = ref($default_field)
Branch: 1 branch
BranchTrueFalseTotal
ref $default_field ? : 2 14 16
799 ? @$default_field
800 : ($default_field);
801 }
802 else {
803 90 @clause_fields = ( $clause->field );
804 }
805  
806 # second, resolve any aliases
807 106 for my $cfield (@clause_fields) {
808  
809 # if we have no definition for $cfield, it's invalid
810 108 ▶ if ( !exists $fields->{$cfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$cfield}) 3 105 108
811 3 return;
812 }
813  
814 105 my $field_def = $fields->{$cfield};
815 105 ▶ if ( $field_def->alias_for ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_def->alias_for) { } 10 95 105
816 my @aliases
817 = ref $field_def->alias_for
818 10 ▶ ? @{ $field_def->alias_for }
Branch: 1 branch
BranchTrueFalseTotal
ref $field_def->alias_for ? : 2 8 10
819 : ( $field_def->alias_for );
820 10 push @field_names, @aliases;
821 }
822 else {
823 95 push @field_names, $cfield;
824 }
825 }
826  
827 #warn "resolved field_names: " . dump( \@field_names );
828  
829 # third, apply our canonical names to the $clause
830 103 ▶ if ( @field_names > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@field_names > 1) { } 4 99 103
831  
832 # turn $clause into a tree
833 4 my $class = blessed($clause);
834 4 my $op = $clause->op;
835  
836 #warn "before tree: " . dump $tree;
837  
838 #warn "code clause: " . dump $clause;
839 4 my @newfields;
840 4 for my $name (@field_names) {
841 8 push(
842 @newfields,
843 $class->new(
844 field => $name,
845 op => $op,
846 value => $clause->value,
847 quote => $clause->quote,
848 proximity => $clause->proximity,
849 )
850 );
851 }
852  
853 # OR the fields together. TODO optional?
854  
855 # we must bless here because
856 # our bool op keys are not methods.
857 my $newfield
858 4 = $query_class->new( %{ $self->query_class_opts },
859 parser => $self );
860 4 $newfield->{""} = \@newfields;
861  
862 4 $clause->op('()');
863 4 $clause->value($newfield);
864  
865 #warn "after tree: " . dump $tree;
866  
867 }
868 else {
869  
870 # if no field defined in clause, or it differs, override.
871 99 ▶ if ( !defined $clause->field
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $clause->field or $field_names[0] ne $clause->field) 20 79 99
Condition100%3/3
not defined $clause->field or $field_names[0] ne $clause->field
l!l&&r!l&&!r
14679
MC/DC100%2/2
not defined $clause->field or $field_names[0] ne $clause->field
not defined $clause->field $field_names[0] ne $clause->field
Truth table100%3/3
not(A) or B
A= not defined $clause->fieldB= $field_names[0] ne $clause->field
ABresult
1X1
011
000
872 or $field_names[0] ne $clause->field )
873 {
874 20 $clause->field( $field_names[0] );
875 }
876 }
877  
878 103 return $clause;
879 }
880 66 );
881 }
882  
883 sub _validate {
884 66 ▶ my ( $self, $query ) = @_;
Subroutine _validate: called CC=1 SCAR=0.1
885  
886 66 my $fields = $self->{fields};
887 my $validator = sub {
888 173 ▶ my ( $clause, $tree, $code, $prefix ) = @_;
Subroutine __ANON__: called CC=8 SCAR=21.4
889 173 ▶ if ( $clause->is_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->is_tree) { } 50 123 173
890 50 $clause->value->walk($code);
891 }
892 else {
893 123 ▶ return unless defined $clause->field and length $clause->field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $clause->field and length $clause->field 13 110 123
Condition100%3/3
defined $clause->field and length $clause->field
!ll&&!rl&&r
121110
MC/DC100%2/2
defined $clause->field and length $clause->field
defined $clause->field length $clause->field
Truth table100%3/3
A and B
A= defined $clause->fieldB= length $clause->field
ABresult
0X0
100
111
894 110 my $field_name = $clause->field;
895 110 my $field_value = $clause->value;
896 110 my $field = $fields->{$field_name};
897 110 ▶ if ( !$field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 3 107 110
898 3 ▶ if ( $self->croak_on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->croak_on_error) { } 2 1 3
899 2 croak "No such field: $field_name";
900 }
901 else {
902 1 $self->{error} = "No such field: $field_name";
903 1 return;
904 }
905 }
906 107 ▶ if ( !$field->validate($field_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $field->validate($field_value)) 0 107 107
907 0 ▶ if ( $self->croak_on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->croak_on_error) 0 0 0
908 0 my $err = $field->error;
909 0 croak
910 "Invalid field value for $field_name: $field_value ($err)";
911 }
912 }
913 }
914 66 };
915 66 $query->walk($validator);
916 }
917  
918 sub _parse {
919 173 ▶ my $self = shift;
Subroutine _parse: called CC=67 SCAR=46.9
920 173 my $str = shift;
921 173 my $parent_field = shift; # only for recursive calls
922 173 my $parent_op = shift; # only for recursive calls
923 173 my $query_class = shift;
924  
925 #warn "_parse: " . dump [ $str, $parent_field, $parent_op, $query_class ];
926  
927 #dump $self;
928  
929 173 my $q = {};
930 173 my $pre_bool = '';
931 173 my $err = undef;
932 173 my $s_orig = $str;
933 173 my $phrase_delim = $self->{phrase_delim};
934 173 my $field_regex = $self->{field_regex};
935 173 my $and_regex = $self->{and_regex};
936 173 my $or_regex = $self->{or_regex};
937 173 my $not_regex = $self->{not_regex};
938 173 my $op_regex = $self->{op_regex};
939 173 my $op_nofield_regex = $self->{op_nofield_regex};
940 173 my $term_regex = $self->{term_regex};
941 173 my $phrase_regex = qr/[^"()]+/;
942 173 my $near_regex = $self->{near_regex};
943 173 my $range_regex = $self->{range_regex};
944 173 my $clause_class = $self->{clause_class};
945 173 my $fixup = $self->{fixup};
946 173 my $null_term = $self->{null_term};
947  
948 173 $str =~ s/^\s+//; # remove leading spaces
949  
950 LOOP:
951 173 ▶ while ( length $str ) { # while query string is not empty
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 350 110 460
952 350 for ($str) { # temporary alias to $_ for easier regex application
953  
954 #warn "LOOP start: " . dump [ $str, $parent_field, $parent_op ];
955  
956 350 my $sign = $self->{default_boolop};
957 350 my $field = $parent_field;
958 350 ▶ my $op = $parent_op || "";
Condition100%2/2
$parent_op || ''
l!l
33317
MC/DC100%1/1
$parent_op || ''
$parent_op
Truth table100%2/2
A
A= $parent_op
Aresult
11
00
959  
960 #warn "LOOP after start: " . dump [ $sign, $field, $op ];
961  
962 350 ▶ if (m/^\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\)/) 64 286 350
963 64 $self->{_paren_count}--;
964  
965 #warn "leaving loop on ) [paren_count==$self->{_paren_count}]";
966 64 ▶ if ( $self->{_paren_count} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_paren_count'} < 0) { } 4 60 64
967 4 ▶ if ( !$fixup ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fixup) { } 1 3 4
968  
969 #warn "unbalanced parens -- extra right-hand )";
970 1 $err = "unbalanced parentheses -- extra right-hand )";
971 1 last LOOP;
972 }
973 else {
974 3 s/^[\)\s]+//; # trim all trailing ) and space
975 3 next LOOP;
976 }
977 }
978 else {
979 60 last LOOP; # return from recursive call if meeting a ')'
980 }
981 }
982  
983 # try to parse sign prefix ('+', '-' or '!|NOT')
984 286 ▶ if (s/^(\+|-)\s*//) { $sign = $1; }
Branch: 3 branches
BranchTrueFalseTotal
if (s/^(\+|-)\s*//) { } 11 275 286
elsif (s/^($not_regex)\b\s*//) { } 5 270 275
elsif (s/^\!\s*([^:=~])/$1/) { } 3 267 270
985 5 elsif (s/^($not_regex)\b\s*//) { $sign = '-'; }
986  
987 # special check because of \b above
988 3 elsif (s/^\!\s*([^:=~])/$1/) { $sign = '-'; }
989  
990 # try to parse field name and operator
991 286 ▶ if (s/^"($field_regex)"\s*($op_regex)\s*// # "field name" and op
Branch: 1 branch
BranchTrueFalseTotal
if (s/^"($field_regex)"\s*($op_regex)\s*// or s/^'?($field_regex)'?\s*($op_regex)\s*// or s/^()($op_nofield_regex)\s*//) 132 154 286
Condition83%5/6
s/^"($field_regex)"\s*($op_regex)\s*// or s/^'?($field_regex)'?\s*($op_regex)\s*// or s/^()($op_nofield_regex)\s*//
l!l&&r!l&&!r
1311154
s/^"($field_regex)"\s*($op_regex)\s*// or s/^'?($field_regex)'?\s*($op_regex)\s*//
l!l&&r!l&&!r
0131155
MC/DC66%2/3
s/^"($field_regex)"\s*($op_regex)\s*// or s/^'?($field_regex)'?\s*($op_regex)\s*// or s/^()($op_nofield_regex)\s*//
s/^"($field_regex)"\s*($op_regex)\s*// s/^'?($field_regex)'?\s*($op_regex)\s*// s/^()($op_nofield_regex)\s*//
Truth table75%3/4
A or B or C
A= s/^"($field_regex)"\s*($op_regex)\s*//B= s/^'?($field_regex)'?\s*($op_regex)\s*//C= s/^()($op_nofield_regex)\s*//
ABCresult
1XX1
01X1
0011
0000
992 or
993 s/^'?($field_regex)'?\s*($op_regex)\s*// # 'field name' and op
994 or s/^()($op_nofield_regex)\s*// # no field, just op
995 )
996 {
997 132 ( $field, $op ) = ( $1, $2 );
998  
999 #warn "matched field+op = " . dump [ $field, $op ];
1000 132 ▶ if ($parent_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_field) 0 132 132
1001 0 $err = "field '$field' inside '$parent_field' (op=$op)";
1002 0 last LOOP;
1003 }
1004 }
1005  
1006 # parse a value (single term or quoted list or parens)
1007 286 my $clause = undef;
1008  
1009 286 ▶ if ( s/^(")([^"]*?)"~(\d+)\s*//
Branch: 5 branches
BranchTrueFalseTotal
if (s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*// or s/^(')([^']*?)'\s*//) { } 31 255 286
elsif ($fixup and s/^(")([^"]*?)\s*$//) { } 1 254 255
elsif (s/^\(\s*"?($phrase_regex)"?$range_regex"?($phrase_regex)"?\s*\)\s*//) { } 7 247 254
elsif (s/^\(\s*//) { } 63 184 247
elsif (s/^($term_regex)\s*//) { } 184 0 184
Condition100%6/6
s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*// or s/^(')([^']*?)'\s*//
l!l&&r!l&&!r
922255
s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*//
l!l&&r!l&&!r
36277
MC/DC100%3/3
s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*// or s/^(')([^']*?)'\s*//
s/^(")([^"]*?)"~(\d+)\s*// s/^(")([^"]*?)"\s*// s/^(')([^']*?)'\s*//
Truth table100%4/4
A or B or C
A= s/^(")([^"]*?)"~(\d+)\s*//B= s/^(")([^"]*?)"\s*//C= s/^(')([^']*?)'\s*//
ABCresult
1XX1
01X1
0011
0000
1010 or s/^(")([^"]*?)"\s*//
1011 or s/^(')([^']*?)'\s*// )
1012 { # parse a quoted string.
1013 31 my ( $quote, $val, $proximity ) = ( $1, $2, $3 );
1014 31 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 0 7 7
Condition66%4/6
$op || $parent_op || ($field ? ':' : '')
l!l&&r!l&&!r
2407
$op or $parent_op
l!l&&r!l&&!r
2407
MC/DC33%1/3
$op || $parent_op || ($field ? ':' : '')
$op $parent_op ($field ? ':' : '')
Truth table50%2/4
A or B || C
A= $opB= $parent_opC= ($field ? ':' : '')
ABCresult
1XX1
01X1
0011
0000
1015 field => $field,
1016 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1017 value => $val,
1018 quote => $quote,
1019 proximity => $proximity
1020 );
1021 }
1022  
1023 # fixup mode allows for a partially quoted string.
1024 ▶ elsif ( $fixup and s/^(")([^"]*?)\s*$// ) {
Condition100%3/3
$fixup and s/^(")([^"]*?)\s*$//
!ll&&!rl&&r
244101
MC/DC100%2/2
$fixup and s/^(")([^"]*?)\s*$//
$fixup s/^(")([^"]*?)\s*$//
Truth table100%3/3
A and B
A= $fixupB= s/^(")([^"]*?)\s*$//
ABresult
0X0
100
111
1025 1 my ( $quote, $val, $proximity ) = ( $1, $2, $3 );
1026 1 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 0 1 1
Condition33%2/6
$op || $parent_op || ($field ? ':' : '')
l!l&&r!l&&!r
001
$op or $parent_op
l!l&&r!l&&!r
001
MC/DC0%0/3
$op || $parent_op || ($field ? ':' : '')
$op $parent_op ($field ? ':' : '')
Truth table25%1/4
A or B || C
A= $opB= $parent_opC= ($field ? ':' : '')
ABCresult
1XX1
01X1
0011
0000
1027 field => $field,
1028 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1029 value => $val,
1030 quote => $quote,
1031 proximity => $proximity
1032 );
1033 }
1034  
1035 # special case for range grouped with () since we do not
1036 # want the op of record to be the ().
1037 elsif (
1038 s/^\(\s*"?($phrase_regex)"?$range_regex"?($phrase_regex)"?\s*\)\s*//
1039 )
1040 {
1041 7 my ( $t1, $t2 ) = ( $1, $2 );
1042  
1043 # trim any spaces since phrase_regex includes it
1044 7 $t1 =~ s/^\ +|\ +$//g;
1045 7 $t2 =~ s/^\ +|\ +$//g;
1046  
1047 7 ▶ my $this_op = $op =~ m/\!/ ? '!..' : '..';
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /\!/ ? : 2 5 7
1048 7 my $has_spaces = 0;
1049 7 ▶ if ( index( $t1, ' ' ) != -1 or index( $t2, ' ' ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($t1, " ") != -1 or index($t2, " ") != -1) 1 6 7
Condition66%2/3
index($t1, " ") != -1 or index($t2, " ") != -1
l!l&&r!l&&!r
106
MC/DC50%1/2
index($t1, " ") != -1 or index($t2, " ") != -1
index($t1, " ") != -1 index($t2, " ") != -1
Truth table66%2/3
A or B
A= index($t1, " ") != -1B= index($t2, " ") != -1
ABresult
1X1
011
000
1050 1 $has_spaces = 1;
1051 }
1052 7 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$has_spaces ? : 1 6 7
1053 field => $field,
1054 op => $this_op,
1055 value => [ $t1, $t2 ],
1056 quote => ( $has_spaces ? '"' : undef ),
1057 );
1058 }
1059 elsif (s/^\(\s*//) { # parse parentheses
1060 63 $self->{_paren_count}++;
1061 63 my ( $r, $s2 )
1062 = $self->_parse( $str, $field, $op, $query_class );
1063 63 ▶ if ( !$r ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 63 63
1064 0 $err = $self->error;
1065 0 last LOOP;
1066 }
1067 63 $str = $s2;
1068 63 ▶ if ( !defined($str) or !( $str =~ s/^\)\s*// ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str and $str =~ s/^\)\s*//) 4 59 63
Condition66%2/3
defined $str and $str =~ s/^\)\s*//
!ll&&!rl&&r
0459
MC/DC50%1/2
defined $str and $str =~ s/^\)\s*//
defined $str $str =~ s/^\)\s*//
Truth table66%2/3
A and B
A= defined $strB= $str =~ s/^\)\s*//
ABresult
0X0
100
111
1069 4 ▶ if ( defined($str) and $fixup ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str and $fixup) { } 2 2 4
Condition66%2/3
defined $str and $fixup
!ll&&!rl&&r
022
MC/DC50%1/2
defined $str and $fixup
defined $str $fixup
Truth table66%2/3
A and B
A= defined $strB= $fixup
ABresult
0X0
100
111
1070 2 $str = ') ' . $str;
1071 }
1072 else {
1073 2 $err = "no matching ) ";
1074 2 last LOOP;
1075 }
1076 }
1077  
1078 61 $clause = $clause_class->new(
1079 field => '',
1080 op => '()',
1081 value => bless( $r, $query_class ), # re-bless
1082 );
1083  
1084 }
1085 elsif (s/^($term_regex)\s*//) { # parse a single term
1086 184 my $term = $1;
1087 184 ▶ if ( $term =~ m/^($term_regex)$range_regex($term_regex)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($term =~ /^($term_regex)$range_regex($term_regex)$/) { } 0 184 184
elsif ($null_term and $term eq $null_term) { } 5 179 184
1088 0 my $t1 = $1;
1089 0 my $t2 = $2;
1090  
1091 #warn "found range ($op $parent_op): $term => $t1 .. $t2";
1092 0 ▶ my $this_op = $op =~ m/\!/ ? '!..' : '..';
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /\!/ ? : 0 0 0
1093 0 $clause = $clause_class->new(
1094 field => $field,
1095 op => $this_op,
1096 value => [ $t1, $t2 ],
1097 );
1098 }
1099 ▶ elsif ( $null_term and $term eq $null_term ) {
Condition66%2/3
$null_term and $term eq $null_term
!ll&&!rl&&r
17905
MC/DC50%1/2
$null_term and $term eq $null_term
$null_term $term eq $null_term
Truth table66%2/3
A and B
A= $null_termB= $term eq $null_term
ABresult
0X0
100
111
1100 5 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 0 0 0
Condition33%2/6
$op || $parent_op || ($field ? ':' : '')
l!l&&r!l&&!r
500
$op or $parent_op
l!l&&r!l&&!r
500
MC/DC0%0/3
$op || $parent_op || ($field ? ':' : '')
$op $parent_op ($field ? ':' : '')
Truth table25%1/4
A or B || C
A= $opB= $parent_opC= ($field ? ':' : '')
ABCresult
1XX1
01X1
0011
0000
1101 field => $field,
1102 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1103 value => undef, # mimic NULL
1104 );
1105  
1106 }
1107 else {
1108  
1109 179 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 0 72 72
Condition66%4/6
$op || $parent_op || ($field ? ':' : '')
l!l&&r!l&&!r
107072
$op or $parent_op
l!l&&r!l&&!r
107072
MC/DC33%1/3
$op || $parent_op || ($field ? ':' : '')
$op $parent_op ($field ? ':' : '')
Truth table50%2/4
A or B || C
A= $opB= $parent_opC= ($field ? ':' : '')
ABCresult
1XX1
01X1
0011
0000
1110 field => $field,
1111 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1112 value => $term,
1113 );
1114  
1115 }
1116 }
1117  
1118 284 ▶ if (s/^($near_regex)\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($near_regex)\s+//) 2 282 284
1119  
1120 # modify the existing clause
1121 # and treat what comes next like a phrase
1122 # matching the syntax "foo bar"~\d+
1123 2 my ($prox_match) = ($1);
1124 2 my ($proximity) = $prox_match;
1125 2 $proximity =~ s/\D+//; # leave only number
1126 2 ▶ if (s/^($term_regex)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($term_regex)\s*//) { } 2 0 2
1127 2 my $term = $1;
1128 2 $clause->{value} .= ' ' . $term;
1129 2 $clause->{proximity} = $proximity;
1130 2 $clause->{quote} = '"';
1131 }
1132 else {
1133 0 $err = "missing term after $prox_match";
1134 0 last LOOP;
1135 }
1136  
1137 }
1138  
1139 # deal with boolean connectors
1140 284 my $post_bool = '';
1141 284 ▶ if (s/^($and_regex)\s+//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^($and_regex)\s+//) { } 26 258 284
elsif (s/^($or_regex)\s+//) { } 42 216 258
1142 26 $post_bool = 'AND';
1143 }
1144 elsif (s/^($or_regex)\s+//) {
1145 42 $post_bool = 'OR';
1146 }
1147  
1148 284 ▶ if ( $pre_bool
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_bool and $post_bool and $pre_bool ne $post_bool) 0 284 284
Condition83%5/6
$pre_bool and $post_bool and $pre_bool ne $post_bool
!ll&&!rl&&r
28130
$pre_bool and $post_bool
!ll&&!rl&&r
216653
MC/DC0%0/3
$pre_bool and $post_bool and $pre_bool ne $post_bool
$pre_bool $post_bool $pre_bool ne $post_bool
Truth table75%3/4
A and B and C
A= $pre_boolB= $post_boolC= $pre_bool ne $post_bool
ABCresult
0XX0
10X0
1100
1111
1149 and $post_bool
1150 and $pre_bool ne $post_bool )
1151 {
1152 0 $err = "cannot mix AND/OR in requests; use parentheses";
1153 0 last LOOP;
1154 }
1155  
1156 284 ▶ my $bool = $pre_bool || $post_bool;
Condition100%3/3
$pre_bool || $post_bool
l!l&&r!l&&!r
6865151
MC/DC100%2/2
$pre_bool || $post_bool
$pre_bool $post_bool
Truth table100%3/3
A || B
A= $pre_boolB= $post_bool
ABresult
1X1
011
000
1157 284 $pre_bool = $post_bool; # for next loop
1158  
1159 # insert clause in query structure
1160 284 ▶ if ($clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause) { } 284 0 284
1161 284 ▶ $sign = '' if $sign eq '+' and $bool eq 'OR';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" and $bool eq "OR" 64 220 284
Condition100%3/3
$sign eq "+" and $bool eq "OR"
!ll&&!rl&&r
6115964
MC/DC100%2/2
$sign eq "+" and $bool eq "OR"
$sign eq "+" $bool eq "OR"
Truth table100%3/3
A and B
A= $sign eq "+"B= $bool eq "OR"
ABresult
0X0
100
111
1162 284 ▶ $sign = '+' if $sign eq '' and $bool eq 'AND';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "" and $bool eq "AND" 3 281 284
Condition100%3/3
$sign eq "" and $bool eq "AND"
!ll&&!rl&&r
1751063
MC/DC100%2/2
$sign eq "" and $bool eq "AND"
$sign eq "" $bool eq "AND"
Truth table100%3/3
A and B
A= $sign eq ""B= $bool eq "AND"
ABresult
0X0
100
111
1163 284 ▶ if ( $sign eq '-' and $bool eq 'OR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq "-" and $bool eq "OR") 0 284 284
Condition66%2/3
$sign eq "-" and $bool eq "OR"
!ll&&!rl&&r
268160
MC/DC0%0/2
$sign eq "-" and $bool eq "OR"
$sign eq "-" $bool eq "OR"
Truth table66%2/3
A and B
A= $sign eq "-"B= $bool eq "OR"
ABresult
0X0
100
111
1164 0 $err = 'operands of "OR" cannot have "-" or "NOT" prefix';
1165 0 last LOOP;
1166 }
1167 284 push @{ $q->{$sign} }, $clause;
1168 }
1169 else {
1170 0 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 0 0 0
1171 0 $err = "unexpected string in query: '$_'";
1172 0 last LOOP;
1173 }
1174 0 ▶ if ($field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) 0 0 0
1175 0 $err = "missing value after $field $op";
1176 0 last LOOP;
1177 }
1178 }
1179 }
1180 }
1181  
1182 # handle error
1183 173 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 3 170 173
1184 3 $self->{error} = "[$s_orig] : $err";
1185 3 $q = undef;
1186 }
1187  
1188 #dump $q;
1189  
1190 173 ▶ if ( !defined $q ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $q) 3 170 173
1191 3 return ( $q, $str );
1192 }
1193 my $query
1194 170 = $query_class->new( %{ $self->query_class_opts }, parser => $self );
1195 170 $query->{$_} = $q->{$_} for keys %$q;
1196 170 return ( $query, $str );
1197 }
1198  
1199 9 1;
1200  
1201 __END__
↑ Summary Query/Dialect/SWISH.pm »