Query/Parser.pm

filter: Statementstmt 91.2% 354 / 388 Branchbran 76.6% 144 / 188 Conditioncond 72.5% 82 / 113 MC/DCmcdc 52.3% 34 / 65 Subroutinesub 100% 44 / 44 Podpod 100% 7 / 7 totaltotal 82.6% 665 / 805 CC 141 SCAR 38.3
CC
141
Cov
84%
_parse
109.1
set_fields
26.5
parse
17.1
CRAP 46.1
↑ Summary Query/Dialect/SWISH.pm »
1 package Search::Query::Parser;
2 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Search::Query::Dialect::Native;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Search::Query::Clause;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Search::Query::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '0.307';
13  
14 30 ▶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 30 ▶ ( 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 24 ▶has 'default_boolop' => ( is => 'rw', default => sub {'+'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 10 has 'default_field' => ( is => 'rw' );
20 30 ▶has 'default_op' => ( is => 'rw', default => sub {':'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 28 ▶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 30 ▶has 'field_regex' => ( is => 'rw', default => sub {qr/[\.\w]+/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 30 ▶has 'fixup' => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
27 30 ▶has 'near_regex' => ( is => 'rw', default => sub {qr/NEAR\d+/i}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 29 ▶has 'not_regex' => ( is => 'rw', default => sub {qr/NOT|PAS|NICHT|NON/i}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 10 has 'null_term' => ( is => 'rw', );
30  
31 # ops that admit an empty left operand
32 30 ▶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 30 ▶ ( is => 'rw', default => sub {qr/~\d+|==|<=|>=|!=|!:|=~|!~|[:=<>~#]/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 30 ▶has 'or_regex' => ( is => 'rw', default => sub {qr/\||OR|OU|ODER|O/i}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
39 30 ▶has 'phrase_delim' => ( is => 'rw', default => sub {q/"/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
40 has 'query_class' =>
41 11 ▶ ( is => 'rw', default => sub {'Search::Query::Dialect::Native'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
42 21 ▶has 'query_class_opts' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 30 ▶has 'range_regex' => ( is => 'rw', default => sub {qr/\.\./}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 29 ▶has 'sloppy' => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
45 30 ▶has 'sloppy_term_regex' => ( is => 'rw', default => sub {qr/[\.\w]+/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
46 10 has 'term_expander' => ( is => 'rw' );
47 30 ▶has 'term_regex' => ( is => 'rw', default => sub {qr/[^\s()]+/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 10 has 'error' => ( is => 'ro' );
49 10 has 'fields' => ( is => 'ro' );
50  
51 10 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 30 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=14.1
Pod documented
412 30 my ( $class, %args ) = @_;
413  
414 # Search::QueryParser compatability
415 30 ▶ if ( exists $args{dialect_opts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"dialect_opts"}) 0 30 30
416 0 $args{query_class_opts} = delete $args{dialect_opts};
417 }
418 30 for my $key ( keys %args ) {
419 93 ▶ if ( exists $SQPCOMPAT{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SQPCOMPAT{$key}) 19 74 93
420 19 $args{ $SQPCOMPAT{$key} } = delete $args{$key};
421 }
422 }
423 30 return \%args;
424 }
425  
426 =head2 BUILD
427  
428 Called internally to initialize the object.
429  
430 =cut
431  
432 30 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod documented
433 30 my $self = shift;
434  
435 # query class can be shortcut
436 $self->{query_class}
437 30 = 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 30 ▶ if ( $self->{field_class} eq 'Search::Query::Field'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"field_class"} eq "Search::Query::Field" and $self->{"query_class"}->field_class ne $self->{"field_class"}) 18 12 30
Condition100%3/3
$self->{"field_class"} eq "Search::Query::Field" and $self->{"query_class"}->field_class ne $self->{"field_class"}
!ll&&!rl&&r
21018
MC/DC100%2/2
$self->{"field_class"} eq "Search::Query::Field" and $self->{"query_class"}->field_class ne $self->{"field_class"}
$self->{"field_class"} eq "Search::Query::Field" $self->{"query_class"}->field_class ne $self->{"field_class"}
Truth table100%3/3
A and B
A= $self->{"field_class"} eq "Search::Query::Field"B= $self->{"query_class"}->field_class ne $self->{"field_class"}
ABresult
0X0
100
111
442 && $self->{query_class}->field_class ne $self->{field_class} )
443 {
444 18 $self->{field_class} = $self->{query_class}->field_class;
445 }
446  
447 30 ▶ $self->set_fields( $self->{fields} ) if $self->{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fields"} 25 5 30
448  
449 30 return $self;
450 }
451  
452 =head2 error
453  
454 Returns the last error message.
455  
456 =cut
457  
458 =head2 clear_error
459  
460 Sets error message to undef.
461  
462 =cut
463  
464 2 ▶sub clear_error {
Subroutine clear_error: called CC=1 SCAR=0.0
Pod documented
465 2 $_->{error} = undef;
466 }
467  
468 =head2 get_field( I<name> )
469  
470 Returns Field object for I<name> or undef if there isn't one
471 defined.
472  
473 =cut
474  
475 230 ▶sub get_field {
Subroutine get_field: called CC=3 SCAR=11.0
Pod documented
476 230 my $self = shift;
477 230 ▶ my $name = shift or croak "name required";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or croak("name required") 230 0 230
478 230 ▶ if ( !exists $self->{fields}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"fields"}{$name}) 8 222 230
479 8 return undef;
480 }
481 222 return $self->{fields}->{$name};
482 }
483  
484 =head2 set_fields( I<fields> )
485  
486 Set the I<fields> structure. Called internally by BUILD()
487 if you pass a C<fields> key/value pair to new().
488  
489 The structure of I<fields> may be one of the following:
490  
491 my $fields = {
492 field1 => 1,
493 field2 => { alias_for => 'field1' },
494 field3 => Search::Query::Field->new( name => 'field3' ),
495 field4 => { alias_for => [qw( field1 field3 )] },
496 };
497  
498 # or
499  
500 my $fields = [
501 'field1',
502 { name => 'field2', alias_for => 'field1' },
503 Search::Query::Field->new( name => 'field3' ),
504 { name => 'field4', alias_for => [qw( field1 field3 )] },
505 ];
506  
507  
508 =cut
509  
510 25 ▶sub set_fields {
Subroutine set_fields: called CC=16 SCAR=32.8
Pod documented
511 25 my $self = shift;
512 25 my $origfields = shift;
513 25 ▶ if ( !defined $origfields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $origfields) 0 25 25
514 0 croak "fields required";
515 }
516  
517 25 my %fields;
518 25 my $field_class = $self->{field_class};
519  
520 25 my $reftype = ref($origfields);
521 25 ▶ 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 25 25
Condition50%3/6
not $reftype or $reftype ne "ARRAY" and $reftype ne "HASH"
l!l&&r!l&&!r
0025
$reftype ne "ARRAY" and $reftype ne "HASH"
!ll&&!rl&&r
2140
MC/DC0%0/3
not $reftype or $reftype ne "ARRAY" and $reftype ne "HASH"
not $reftype $reftype ne "ARRAY" $reftype ne "HASH"
Truth table50%2/4
not(A) or B and C
A= not $reftypeB= $reftype ne "ARRAY"C= $reftype ne "HASH"
ABCresult
1XX1
0111
00X0
0100
522 0 croak "fields must be an ARRAY or HASH ref";
523 }
524  
525 # convert simple array to hash
526 25 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 21 4 25
elsif ($reftype eq 'HASH') { } 4 0 4
527 21 for my $name (@$origfields) {
528 37 ▶ if ( blessed($name) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($name)) { } 0 37 37
elsif (ref $name eq 'HASH') { } 1 36 37
529 0 $fields{ $name->name } = $name;
530 }
531 elsif ( ref($name) eq 'HASH' ) {
532 1 ▶ if ( !exists $name->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $name->{"name"}) 0 1 1
533 0 croak "'name' required in hashref: " . dump($name);
534 }
535 1 $fields{ $name->{name} } = $field_class->new(%$name);
536 }
537 else {
538 36 $fields{$name} = $field_class->new( name => $name, );
539 }
540 }
541 }
542 elsif ( $reftype eq 'HASH' ) {
543 4 for my $name ( keys %$origfields ) {
544 12 my $val = $origfields->{$name};
545 12 my $obj;
546 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
547 0 $obj = $val;
548 }
549 elsif ( ref($val) eq 'HASH' ) {
550 12 ▶ if ( !exists $val->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $val->{"name"}) 12 0 12
551 12 $val->{name} = $name;
552 }
553 12 $obj = $field_class->new(%$val);
554 }
555 elsif ( !ref $val ) {
556 0 $obj = $field_class->new( name => $name );
557 }
558 else {
559 0 croak
560 "field value for $name must be a field name, hashref or Field object";
561 }
562 12 $fields{$name} = $obj;
563 }
564 }
565  
566 25 $self->{fields} = \%fields;
567 25 return $self->{fields};
568 }
569  
570 =head2 set_field( I<name> => I<field_object> )
571  
572 Sets field I<name> to Field object I<field_object>.
573  
574 =cut
575  
576 8 ▶sub set_field {
Subroutine set_field: called CC=4 SCAR=14.5
Pod documented
577 8 my $self = shift;
578 8 my ( $name, $field ) = @_;
579 8 ▶ confess "name required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 8 8
580 8 ▶ confess "field object required" unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 8 8
581 8 ▶ confess "field not an object: $field" unless blessed($field);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($field) 0 8 8
582 8 $self->{fields}->{$name} = $field;
583 }
584  
585 =head2 parse( I<string> )
586  
587 Returns a Search::Query::Dialect object of type
588 I<query_class>.
589  
590 If there is a syntax error in I<string>,
591 parse() will return C<undef> and set error().
592  
593 =cut
594  
595 84 ▶sub parse {
Subroutine parse: called CC=16 SCAR=28.4
Pod documented
596 84 my $self = shift;
597 84 my $q = shift;
598 84 ▶ croak "query required" unless defined $q;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q 0 84 84
599 84 ▶ my $class = shift || $self->query_class;
Condition33%1/3
shift() || $self->query_class
l!l&&r!l&&!r
0840
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
600  
601 # reset state in case we are called multiple times
602 84 $self->{error} = undef;
603 84 $self->{_paren_count} = 0;
604  
605 84 $q = $class->preprocess($q);
606 84 my ($query) = $self->_parse( $q, undef, undef, $class );
607 84 ▶ if ( !defined $query && !$self->sloppy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query or $self->sloppy) 2 82 84
Condition100%3/3
defined $query or $self->sloppy
l!l&&r!l&&!r
8112
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
608 2 ▶ croak $self->error if $self->croak_on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->croak_on_error 1 1 2
609 1 return $query;
610 }
611  
612 # if in sloppy mode and we failed to parse,
613 # extract what looks like terms and re-parse.
614 82 ▶ if ( !defined $query && $self->sloppy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $query and $self->sloppy) 1 81 82
Condition66%2/3
not defined $query and $self->sloppy
!ll&&!rl&&r
8101
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
615 1 return $self->_sloppify( $q, $class );
616 }
617  
618 81 ▶ if ( $self->{term_expander} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term_expander"}) 3 78 81
619 3 $self->_call_term_expander($query);
620 }
621  
622 81 ▶ if ( $self->{fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fields"}) 68 13 81
623 68 $self->_expand($query);
624 68 $self->_validate($query);
625 }
626  
627 # if in sloppy mode and we failed to parse,
628 # extract what looks like terms and re-parse.
629 78 ▶ if ( $self->error && $self->sloppy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error and $self->sloppy) 1 77 78
Condition66%2/3
$self->error and $self->sloppy
!ll&&!rl&&r
7701
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
630 1 return $self->_sloppify( $q, $class );
631 }
632  
633 77 $query->{parser} = $self;
634  
635 #warn dump $query;
636  
637 # if the query isn't re-parse-able once stringified
638 # then it is broken, somehow.
639 77 ▶ if ( defined $query
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and not $self->error and $self->croak_on_error) 27 50 77
Condition50%3/6
defined $query and not $self->error and $self->croak_on_error
!ll&&!rl&&r
05027
defined $query and not $self->error
!ll&&!rl&&r
0077
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
640 and !$self->error
641 and $self->croak_on_error )
642 {
643 27 my ($reparsed) = $self->_parse( "$query", undef, undef, $class );
644 27 ▶ if ( !defined $reparsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reparsed) 0 27 27
645 0 croak sprintf( "Error: unable to parse '%s'. Reason: '%s'.",
646 $q, $self->error );
647 }
648 }
649  
650 #weaken( $query->{parser} ); # TODO leaks possible?
651  
652 77 return $query;
653 }
654  
655 2 ▶sub _sloppify {
Subroutine _sloppify: called CC=5 SCAR=16.2
656 2 my ( $self, $q, $class ) = @_;
657 2 my $term = $self->{sloppy_term_regex};
658 2 my $and = $self->{and_regex};
659 2 my $or = $self->{or_regex};
660 2 my $not = $self->{not_regex};
661 2 my $near = $self->{near_regex};
662 2 my $ops = $self->{op_regex};
663 2 my $bools = qr/($and|$or|$not|$near|$ops)/;
664 2 my @terms;
665  
666 2 ▶ while ( $q =~ m/($term)/ig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q =~ /($term)/gi) 14 2 16
667 14 my $t = $1;
668  
669 #warn "$t =~ $bools\n";
670 14 ▶ if ( $t =~ m/^$bools$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /^$bools$/) 7 7 14
671 7 next;
672 }
673 7 push @terms, split( /$ops/, $t );
674 }
675  
676 #dump \@terms;
677  
678 # reset errors since we will re-parse
679 2 $self->{error} = undef;
680 2 my ($query) = $self->_parse( join( ' ', @terms ), undef, undef, $class );
681 2 ▶ if ( !defined $query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $query) { } 0 2 2
682 0 ▶ $self->croak_on_error and croak $self->error;
Branch: 1 branch
BranchTrueFalseTotal
$self->croak_on_error and croak($self->error) 0 0 0
683 }
684 else {
685 2 $query->{parser} = $self;
686 }
687 2 return $query;
688 }
689  
690 3 ▶sub _call_term_expander {
Subroutine _call_term_expander: called CC=2 SCAR=7.2
691 3 my ( $self, $query ) = @_;
692 3 my $expander = $self->{term_expander};
693 3 ▶ if ( ref($expander) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expander ne "CODE") 0 3 3
694 0 croak "term_expander must be a CODE reference";
695 }
696  
697 3 my $query_class = $self->{query_class};
698  
699 $query->walk(
700 3 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.7
701 3 my ( $clause, $tree, $code, $prefix ) = @_;
702 3 ▶ if ( $clause->is_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->is_tree) 0 3 3
703 0 $clause->value->walk($code);
704 0 return;
705 }
706  
707 3 my @newterms = $expander->( $clause->value, $clause->field );
708 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
709 0 $clause->value( $newterms[0] );
710 }
711 elsif ( @newterms > 1 ) {
712  
713 # turn $clause into a tree
714 1 my $class = blessed($clause);
715 1 my $op = $clause->op;
716 1 my $field = $clause->field;
717 1 my $proximity = $clause->proximity;
718 1 my $quote = $clause->quote;
719  
720 #warn "before tree: " . dump $tree;
721  
722 #warn "code clause: " . dump $clause;
723 1 my @subclauses;
724 1 for my $term (@newterms) {
725 4 push(
726 @subclauses,
727 $class->new(
728 field => $field,
729 op => $op,
730 value => $term,
731 quote => $quote,
732 proximity => $proximity,
733 )
734 );
735 }
736  
737 # OR the fields together. TODO optional?
738  
739 # we must set "" key here explicitly, because
740 # our bool op keys are not methods.
741 my $subclause
742 1 = $query_class->new( %{ $self->query_class_opts },
743 parser => $self );
744 1 $subclause->{""} = \@subclauses;
745  
746 1 $clause->op('()');
747 1 $clause->value($subclause);
748 }
749 else {
750 2 $clause->value( $newterms[0] );
751 }
752  
753 }
754 3 );
755  
756 }
757  
758 68 ▶sub _expand {
Subroutine _expand: called CC=2 SCAR=6.9
759 68 my ( $self, $query ) = @_;
760  
761 68 ▶ return if !exists $self->{fields};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fields"} 0 68 68
762 68 my $fields = $self->{fields};
763 68 my $query_class = $self->{query_class};
764 68 my $default_field = $self->{default_field};
765  
766 #dump $fields;
767  
768 $query->walk(
769 168 ▶ sub {
Subroutine __ANON__: called CC=16 SCAR=27.7
770 168 my ( $clause, $tree, $code, $prefix ) = @_;
771  
772 #warn "code clause: " . dump $clause;
773  
774 #warn "code tree: " . dump $tree;
775  
776 168 ▶ if ( $clause->is_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->is_tree) 46 122 168
777 46 $clause->value->walk($code);
778 46 return;
779 }
780 122 ▶ if ( ( !defined $clause->field || !length $clause->field )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clause->field and length $clause->field or defined $default_field) 14 108 122
Condition100%6/6
defined $clause->field and length $clause->field or defined $default_field
l!l&&r!l&&!r
921614
defined $clause->field and length $clause->field
!ll&&!rl&&r
29192
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
781 && !defined $default_field )
782 {
783 14 return;
784 }
785  
786 # make sure clause has an op
787 108 ▶ if ( !$clause->op ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($clause->op) 16 92 108
788 16 $clause->op( $self->default_op );
789 }
790  
791 # even if $clause has a field defined,
792 # it may be aliased to multiple others,
793 # so check field def and default_field to determine.
794 108 my @field_names;
795  
796 # first, which field name to start with?
797 my @clause_fields; # could be plural
798 108 ▶ if ( !defined $clause->field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $clause->field) { } 16 92 108
799 @clause_fields
800 16 ▶ = ref($default_field)
Branch: 1 branch
BranchTrueFalseTotal
ref $default_field ? : 2 14 16
801 ? @$default_field
802 : ($default_field);
803 }
804 else {
805 92 @clause_fields = ( $clause->field );
806 }
807  
808 # second, resolve any aliases
809 108 for my $cfield (@clause_fields) {
810  
811 # if we have no definition for $cfield, it's invalid
812 110 ▶ if ( !exists $fields->{$cfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$cfield}) 3 107 110
813 3 return;
814 }
815  
816 107 my $field_def = $fields->{$cfield};
817 107 ▶ if ( $field_def->alias_for ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_def->alias_for) { } 10 97 107
818 my @aliases
819 = ref $field_def->alias_for
820 10 ▶ ? @{ $field_def->alias_for }
Branch: 1 branch
BranchTrueFalseTotal
ref $field_def->alias_for ? : 2 8 10
821 : ( $field_def->alias_for );
822 10 push @field_names, @aliases;
823 }
824 else {
825 97 push @field_names, $cfield;
826 }
827 }
828  
829 #warn "resolved field_names: " . dump( \@field_names );
830  
831 # third, apply our canonical names to the $clause
832 105 ▶ if ( @field_names > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@field_names > 1) { } 4 101 105
833  
834 # turn $clause into a tree
835 4 my $class = blessed($clause);
836 4 my $op = $clause->op;
837  
838 #warn "before tree: " . dump $tree;
839  
840 #warn "code clause: " . dump $clause;
841 4 my @newfields;
842 4 for my $name (@field_names) {
843 8 push(
844 @newfields,
845 $class->new(
846 field => $name,
847 op => $op,
848 value => $clause->value,
849 quote => $clause->quote,
850 proximity => $clause->proximity,
851 )
852 );
853 }
854  
855 # OR the fields together. TODO optional?
856  
857 # we must bless here because
858 # our bool op keys are not methods.
859 my $newfield
860 4 = $query_class->new( %{ $self->query_class_opts },
861 parser => $self );
862 4 $newfield->{""} = \@newfields;
863  
864 4 $clause->op('()');
865 4 $clause->value($newfield);
866  
867 #warn "after tree: " . dump $tree;
868  
869 }
870 else {
871  
872 # if no field defined in clause, or it differs, override.
873 101 ▶ if ( !defined $clause->field
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $clause->field or $field_names[0] ne $clause->field) 20 81 101
Condition100%3/3
not defined $clause->field or $field_names[0] ne $clause->field
l!l&&r!l&&!r
14681
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
874 or $field_names[0] ne $clause->field )
875 {
876 20 $clause->field( $field_names[0] );
877 }
878 }
879  
880 105 return $clause;
881 }
882 68 );
883 }
884  
885 68 ▶sub _validate {
Subroutine _validate: called CC=1 SCAR=0.0
886 68 my ( $self, $query ) = @_;
887  
888 68 my $fields = $self->{fields};
889 176 ▶ my $validator = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
890 176 my ( $clause, $tree, $code, $prefix ) = @_;
891 176 ▶ if ( $clause->is_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->is_tree) { } 50 126 176
892 50 $clause->value->walk($code);
893 }
894 else {
895 126 ▶ return unless defined $clause->field and length $clause->field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $clause->field and length $clause->field 14 112 126
Condition100%3/3
defined $clause->field and length $clause->field
!ll&&!rl&&r
131112
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
896 112 my $field_name = $clause->field;
897 112 my $field_value = $clause->value;
898 112 my $field = $fields->{$field_name};
899 112 ▶ if ( !$field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 3 109 112
900 3 ▶ if ( $self->croak_on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->croak_on_error) { } 2 1 3
901 2 croak "No such field: $field_name";
902 }
903 else {
904 1 $self->{error} = "No such field: $field_name";
905 1 return;
906 }
907 }
908 109 ▶ if ( !$field->validate($field_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $field->validate($field_value)) 1 108 109
909 1 ▶ if ( $self->croak_on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->croak_on_error) 1 0 1
910 1 my $err = $field->error;
911 1 croak
912 "Invalid field value for $field_name: $field_value ($err)";
913 }
914 }
915 }
916 68 };
917 68 $query->walk($validator);
918 }
919  
920 176 ▶sub _parse {
Subroutine _parse: called CC=67 SCAR=46.9
921 176 my $self = shift;
922 176 my $str = shift;
923 176 my $parent_field = shift; # only for recursive calls
924 176 my $parent_op = shift; # only for recursive calls
925 176 my $query_class = shift;
926  
927 #warn "_parse: " . dump [ $str, $parent_field, $parent_op, $query_class ];
928  
929 #dump $self;
930  
931 176 my $q = {};
932 176 my $pre_bool = '';
933 176 my $err = undef;
934 176 my $s_orig = $str;
935 176 my $phrase_delim = $self->{phrase_delim};
936 176 my $field_regex = $self->{field_regex};
937 176 my $and_regex = $self->{and_regex};
938 176 my $or_regex = $self->{or_regex};
939 176 my $not_regex = $self->{not_regex};
940 176 my $op_regex = $self->{op_regex};
941 176 my $op_nofield_regex = $self->{op_nofield_regex};
942 176 my $term_regex = $self->{term_regex};
943 176 my $phrase_regex = qr/[^"()]+/;
944 176 my $near_regex = $self->{near_regex};
945 176 my $range_regex = $self->{range_regex};
946 176 my $clause_class = $self->{clause_class};
947 176 my $fixup = $self->{fixup};
948 176 my $null_term = $self->{null_term};
949  
950 176 $str =~ s/^\s+//; # remove leading spaces
951  
952 LOOP:
953 176 ▶ while ( length $str ) { # while query string is not empty
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 355 113 468
954 355 for ($str) { # temporary alias to $_ for easier regex application
955  
956 #warn "LOOP start: " . dump [ $str, $parent_field, $parent_op ];
957  
958 355 my $sign = $self->{default_boolop};
959 355 my $field = $parent_field;
960 355 ▶ my $op = $parent_op || "";
Condition100%2/2
$parent_op || ''
l!l
33322
MC/DC100%1/1
$parent_op || ''
$parent_op
Truth table100%2/2
A
A= $parent_op
Aresult
11
00
961  
962 #warn "LOOP after start: " . dump [ $sign, $field, $op ];
963  
964 355 ▶ if (m/^\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\)/) 64 291 355
965 64 $self->{_paren_count}--;
966  
967 #warn "leaving loop on ) [paren_count==$self->{_paren_count}]";
968 64 ▶ if ( $self->{_paren_count} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_paren_count'} < 0) { } 4 60 64
969 4 ▶ if ( !$fixup ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fixup) { } 1 3 4
970  
971 #warn "unbalanced parens -- extra right-hand )";
972 1 $err = "unbalanced parentheses -- extra right-hand )";
973 1 last LOOP;
974 }
975 else {
976 3 s/^[\)\s]+//; # trim all trailing ) and space
977 3 next LOOP;
978 }
979 }
980 else {
981 60 last LOOP; # return from recursive call if meeting a ')'
982 }
983 }
984  
985 # try to parse sign prefix ('+', '-' or '!|NOT')
986 291 ▶ if (s/^(\+|-)\s*//) { $sign = $1; }
Branch: 3 branches
BranchTrueFalseTotal
if (s/^(\+|-)\s*//) { } 11 280 291
elsif (s/^($not_regex)\b\s*//) { } 5 275 280
elsif (s/^\!\s*([^:=~])/$1/) { } 3 272 275
987 5 elsif (s/^($not_regex)\b\s*//) { $sign = '-'; }
988  
989 # special check because of \b above
990 3 elsif (s/^\!\s*([^:=~])/$1/) { $sign = '-'; }
991  
992 # try to parse field name and operator
993 291 ▶ 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*//) 135 156 291
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
1341156
s/^"($field_regex)"\s*($op_regex)\s*// or s/^'?($field_regex)'?\s*($op_regex)\s*//
l!l&&r!l&&!r
0134157
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
994 or
995 s/^'?($field_regex)'?\s*($op_regex)\s*// # 'field name' and op
996 or s/^()($op_nofield_regex)\s*// # no field, just op
997 )
998 {
999 135 ( $field, $op ) = ( $1, $2 );
1000  
1001 #warn "matched field+op = " . dump [ $field, $op ];
1002 135 ▶ if ($parent_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_field) 0 135 135
1003 0 $err = "field '$field' inside '$parent_field' (op=$op)";
1004 0 last LOOP;
1005 }
1006 }
1007  
1008 # parse a value (single term or quoted list or parens)
1009 291 my $clause = undef;
1010  
1011 291 ▶ if ( s/^(")([^"]*?)"~(\d+)\s*//
Branch: 5 branches
BranchTrueFalseTotal
if (s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*// or s/^(')([^']*?)'\s*//) { } 31 260 291
elsif ($fixup and s/^(")([^"]*?)\s*$//) { } 1 259 260
elsif (s/^\(\s*"?($phrase_regex)"?$range_regex"?($phrase_regex)"?\s*\)\s*//) { } 7 252 259
elsif (s/^\(\s*//) { } 63 189 252
elsif (s/^($term_regex)\s*//) { } 189 0 189
Condition100%6/6
s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*// or s/^(')([^']*?)'\s*//
l!l&&r!l&&!r
922260
s/^(")([^"]*?)"~(\d+)\s*// or s/^(")([^"]*?)"\s*//
l!l&&r!l&&!r
36282
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
1012 or s/^(")([^"]*?)"\s*//
1013 or s/^(')([^']*?)'\s*// )
1014 { # parse a quoted string.
1015 31 my ( $quote, $val, $proximity ) = ( $1, $2, $3 );
1016 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
1017 field => $field,
1018 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1019 value => $val,
1020 quote => $quote,
1021 proximity => $proximity
1022 );
1023 }
1024  
1025 # fixup mode allows for a partially quoted string.
1026 ▶ elsif ( $fixup and s/^(")([^"]*?)\s*$// ) {
Condition100%3/3
$fixup and s/^(")([^"]*?)\s*$//
!ll&&!rl&&r
249101
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
1027 1 my ( $quote, $val, $proximity ) = ( $1, $2, $3 );
1028 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
1029 field => $field,
1030 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1031 value => $val,
1032 quote => $quote,
1033 proximity => $proximity
1034 );
1035 }
1036  
1037 # special case for range grouped with () since we do not
1038 # want the op of record to be the ().
1039 elsif (
1040 s/^\(\s*"?($phrase_regex)"?$range_regex"?($phrase_regex)"?\s*\)\s*//
1041 )
1042 {
1043 7 my ( $t1, $t2 ) = ( $1, $2 );
1044  
1045 # trim any spaces since phrase_regex includes it
1046 7 $t1 =~ s/^\ +|\ +$//g;
1047 7 $t2 =~ s/^\ +|\ +$//g;
1048  
1049 7 ▶ my $this_op = $op =~ m/\!/ ? '!..' : '..';
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /\!/ ? : 2 5 7
1050 7 my $has_spaces = 0;
1051 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
1052 1 $has_spaces = 1;
1053 }
1054 7 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$has_spaces ? : 1 6 7
1055 field => $field,
1056 op => $this_op,
1057 value => [ $t1, $t2 ],
1058 quote => ( $has_spaces ? '"' : undef ),
1059 );
1060 }
1061 elsif (s/^\(\s*//) { # parse parentheses
1062 63 $self->{_paren_count}++;
1063 63 my ( $r, $s2 )
1064 = $self->_parse( $str, $field, $op, $query_class );
1065 63 ▶ if ( !$r ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 63 63
1066 0 $err = $self->error;
1067 0 last LOOP;
1068 }
1069 63 $str = $s2;
1070 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
1071 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
1072 2 $str = ') ' . $str;
1073 }
1074 else {
1075 2 $err = "no matching ) ";
1076 2 last LOOP;
1077 }
1078 }
1079  
1080 61 $clause = $clause_class->new(
1081 field => '',
1082 op => '()',
1083 value => bless( $r, $query_class ), # re-bless
1084 );
1085  
1086 }
1087 elsif (s/^($term_regex)\s*//) { # parse a single term
1088 189 my $term = $1;
1089 189 ▶ if ( $term =~ m/^($term_regex)$range_regex($term_regex)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($term =~ /^($term_regex)$range_regex($term_regex)$/) { } 0 189 189
elsif ($null_term and $term eq $null_term) { } 5 184 189
1090 0 my $t1 = $1;
1091 0 my $t2 = $2;
1092  
1093 #warn "found range ($op $parent_op): $term => $t1 .. $t2";
1094 0 ▶ my $this_op = $op =~ m/\!/ ? '!..' : '..';
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /\!/ ? : 0 0 0
1095 0 $clause = $clause_class->new(
1096 field => $field,
1097 op => $this_op,
1098 value => [ $t1, $t2 ],
1099 );
1100 }
1101 ▶ elsif ( $null_term and $term eq $null_term ) {
Condition66%2/3
$null_term and $term eq $null_term
!ll&&!rl&&r
18405
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
1102 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
1103 field => $field,
1104 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1105 value => undef, # mimic NULL
1106 );
1107  
1108 }
1109 else {
1110  
1111 184 ▶ $clause = $clause_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 0 74 74
Condition66%4/6
$op || $parent_op || ($field ? ':' : '')
l!l&&r!l&&!r
110074
$op or $parent_op
l!l&&r!l&&!r
110074
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
1112 field => $field,
1113 op => ( $op || $parent_op || ( $field ? ":" : "" ) ),
1114 value => $term,
1115 );
1116  
1117 }
1118 }
1119  
1120 289 ▶ if (s/^($near_regex)\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($near_regex)\s+//) 2 287 289
1121  
1122 # modify the existing clause
1123 # and treat what comes next like a phrase
1124 # matching the syntax "foo bar"~\d+
1125 2 my ($prox_match) = ($1);
1126 2 my ($proximity) = $prox_match;
1127 2 $proximity =~ s/\D+//; # leave only number
1128 2 ▶ if (s/^($term_regex)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($term_regex)\s*//) { } 2 0 2
1129 2 my $term = $1;
1130 2 $clause->{value} .= ' ' . $term;
1131 2 $clause->{proximity} = $proximity;
1132 2 $clause->{quote} = '"';
1133 }
1134 else {
1135 0 $err = "missing term after $prox_match";
1136 0 last LOOP;
1137 }
1138  
1139 }
1140  
1141 # deal with boolean connectors
1142 289 my $post_bool = '';
1143 289 ▶ if (s/^($and_regex)\s+//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^($and_regex)\s+//) { } 26 263 289
elsif (s/^($or_regex)\s+//) { } 43 220 263
1144 26 $post_bool = 'AND';
1145 }
1146 elsif (s/^($or_regex)\s+//) {
1147 43 $post_bool = 'OR';
1148 }
1149  
1150 289 ▶ if ( $pre_bool
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_bool and $post_bool and $pre_bool ne $post_bool) 0 289 289
Condition83%5/6
$pre_bool and $post_bool and $pre_bool ne $post_bool
!ll&&!rl&&r
28630
$pre_bool and $post_bool
!ll&&!rl&&r
220663
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
1151 and $post_bool
1152 and $pre_bool ne $post_bool )
1153 {
1154 0 $err = "cannot mix AND/OR in requests; use parentheses";
1155 0 last LOOP;
1156 }
1157  
1158 289 ▶ my $bool = $pre_bool || $post_bool;
Condition100%3/3
$pre_bool || $post_bool
l!l&&r!l&&!r
6966154
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
1159 289 $pre_bool = $post_bool; # for next loop
1160  
1161 # insert clause in query structure
1162 289 ▶ if ($clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause) { } 289 0 289
1163 289 ▶ $sign = '' if $sign eq '+' and $bool eq 'OR';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" and $bool eq "OR" 66 223 289
Condition100%3/3
$sign eq "+" and $bool eq "OR"
!ll&&!rl&&r
6116266
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
1164 289 ▶ $sign = '+' if $sign eq '' and $bool eq 'AND';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "" and $bool eq "AND" 3 286 289
Condition100%3/3
$sign eq "" and $bool eq "AND"
!ll&&!rl&&r
1781083
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
1165 289 ▶ if ( $sign eq '-' and $bool eq 'OR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq "-" and $bool eq "OR") 0 289 289
Condition66%2/3
$sign eq "-" and $bool eq "OR"
!ll&&!rl&&r
273160
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
1166 0 $err = 'operands of "OR" cannot have "-" or "NOT" prefix';
1167 0 last LOOP;
1168 }
1169 289 push @{ $q->{$sign} }, $clause;
1170 }
1171 else {
1172 0 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 0 0 0
1173 0 $err = "unexpected string in query: '$_'";
1174 0 last LOOP;
1175 }
1176 0 ▶ if ($field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) 0 0 0
1177 0 $err = "missing value after $field $op";
1178 0 last LOOP;
1179 }
1180 }
1181 }
1182 }
1183  
1184 # handle error
1185 176 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 3 173 176
1186 3 $self->{error} = "[$s_orig] : $err";
1187 3 $q = undef;
1188 }
1189  
1190 #dump $q;
1191  
1192 176 ▶ if ( !defined $q ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $q) 3 173 176
1193 3 return ( $q, $str );
1194 }
1195 my $query
1196 173 = $query_class->new( %{ $self->query_class_opts }, parser => $self );
1197 173 $query->{$_} = $q->{$_} for keys %$q;
1198 173 return ( $query, $str );
1199 }
1200  
1201 10 1;
1202  
1203 __END__
↑ Summary Query/Dialect/SWISH.pm »