This file is indexed.

/usr/share/perl5/CQL/Parser.pm is in libcql-parser-perl 1.12-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package CQL::Parser;

use strict;
use warnings;
use CQL::Lexer;
use CQL::Relation;
use CQL::Token;
use CQL::TermNode;
use CQL::AndNode;
use CQL::OrNode;
use CQL::NotNode;
use CQL::PrefixNode;
use CQL::ProxNode;
use Carp qw( croak );

our $VERSION = '1.12';

my $lexer;
my $token;

=head1 NAME

CQL::Parser - compiles CQL strings into parse trees of Node subtypes.

=head1 SYNOPSIS

    use CQL::Parser;
    my $parser = CQL::Parser->new();
    my $root = $parser->parse( $cql );

=head1 DESCRIPTION

CQL::Parser provides a mechanism to parse Common Query Language (CQL)
statements. The best description of CQL comes from the CQL homepage
at the Library of Congress L<http://www.loc.gov/z3950/agency/zing/cql/>

CQL is a formal language for representing queries to information
retrieval systems such as web indexes, bibliographic catalogs and museum
collection information. The CQL design objective is that queries be
human readable and human writable, and that the language be intuitive
while maintaining the expressiveness of more complex languages.

A CQL statement can be as simple as a single keyword, or as complicated as a set
of compoenents indicating search indexes, relations, relational modifiers,
proximity clauses and boolean logic. CQL::Parser will parse CQL statements
and return the root node for a tree of nodes which describes the CQL statement.
This data structure can then be used by a client application to analyze the
statement, and possibly turn it into a query for a local repository.

Each CQL component in the tree inherits from L<CQL::Node> and can be one
of the following: L<CQL::AndNode>, L<CQL::NotNode>, L<CQL::OrNode>,
L<CQL::ProxNode>, L<CQL::TermNode>, L<CQL::PrefixNode>. See the
documentation for those modules for their respective APIs.

Here are some examples of CQL statements:

=over 4

=item * george

=item * dc.creator=george

=item * dc.creator="George Clinton"

=item * clinton and funk

=item * clinton and parliament and funk

=item * (clinton or bootsy) and funk

=item * dc.creator="clinton" and dc.date="1976"

=back

=head1 METHODS

=head2 new()

=cut

## for convenience the lexer is located at the package level
## just need to be sure to reinitialize it in very call to parse()

sub new {
    my ( $class, $debug ) = @_;
    $CQL::DEBUG = $debug ? 1 : 0;
    return bless { }, ref($class) || $class;
}

=head2 parse( $query )

Pass in a CQL query and you'll get back the root node for the CQL parse tree.
If the CQL is invalid an exception will be thrown.

=cut

sub parse {
    my ($self,$query) = @_;

    ## initialize lexer
    $lexer = CQL::Lexer->new();

    debug( "about to parse query: $query" ); 
    
    ## create the lexer and get the first token
    $lexer->tokenize( $query );
    $token = $lexer->nextToken();

    my $root = parseQuery( 'srw.ServerChoice', CQL::Relation->new( 'scr' ) );
    if ( $token->getType() != CQL_EOF ) { 
        croak( "junk after end ".$token->getString() );
    }
    
    return $root;
}

=head2 parseSafe( $query )

Pass in a CQL query and you'll get back the root node for the CQL parse tree.
If the CQL is invalid, an error code from the SRU Diagnostics List 
will be returned.

=cut

my @cql_errors = (
    { regex => qr/does not support relational modifiers/,   code => 20 },
    { regex => qr/expected boolean got /,                   code => 37 },
    { regex => qr/expected relation modifier got /,         code => 20 },
    { regex => qr/unknown first-class relation modifier: /, code => 20 },
    { regex => qr/missing term/,                            code => 27 },
    { regex => qr/expected proximity relation got /,        code => 40 },
    { regex => qr/expected proximity distance got /,        code => 41 },
    { regex => qr/expected proximity unit got/,             code => 42 },
    { regex => qr/expected proximity ordering got /,        code => 43 },
    { regex => qr/unknown first class relation: /,          code => 19 },
    { regex => qr/must supply name/,                        code => 15 },
    { regex => qr/must supply identifier/,                  code => 15 },
    { regex => qr/must supply subtree/,                     code => 15 },
    { regex => qr/must supply term parameter/,              code => 27 },
    { regex => qr/doesn\'t support relations other than/,   code => 20 },
);

sub parseSafe {
    my ($self,$query) = @_;

    my $root = eval { $self->parse( $query ); };

    if ( my $error = $@ ) {
        my $code = 10;
        for( @cql_errors ) {
            $code = $_->{ code } if $error =~ $_->{ regex };
        }
        return $code;
    }

    return $root;
}

sub parseQuery {
    my ( $qualifier, $relation ) = @_;
    debug( "in parseQuery() with term=" . $token->getString() );
    my $term = parseTerm( $qualifier, $relation );

    my $type = $token->getType();
    while ( $type != CQL_EOF and $type != CQL_RPAREN ) { 
        if ( $type == CQL_AND ) { 
            match($token);
            my $term2 = parseTerm( $qualifier, $relation );
            $term = CQL::AndNode->new( left=>$term, right=>$term2 );
        } 
        elsif ( $type == CQL_OR ) {
            match($token);
            my $term2 = parseTerm( $qualifier, $relation );
            $term = CQL::OrNode->new( left=>$term, right=>$term2 );
        }
        elsif ( $type == CQL_NOT ) { 
            match($token);
            my $term2 = parseTerm( $qualifier, $relation );
            $term = CQL::NotNode->new( left=>$term, right=>$term2 );
        }
        elsif ( $type == CQL_PROX ) { 
            match($token);
            my $proxNode = CQL::ProxNode->new( $term );
            gatherProxParameters( $proxNode );
            my $term2 = parseTerm( $qualifier, $relation );
            $proxNode->addSecondTerm( $term2 );
            $term = $proxNode;
        } 
        else {
            croak( "expected boolean got ".$token->getString() );
        }
        $type = $token->getType();
    }
    debug( "no more ops" );
    return( $term );
}

sub parseTerm {
    my ( $qualifier, $relation ) = @_;
    debug( "in parseTerm()" );
    my $word;
    while ( 1 ) { 
        if ( $token->getType() == CQL_LPAREN ) { 
            debug( "parenthesized term" );
            match( CQL::Token->new('(') );
            my $expr = parseQuery( $qualifier, $relation );
            match( CQL::Token->new(')') );
            return $expr;
        } 
        elsif ( $token->getType() == CQL_GT ) {
            match( $token );
            return parsePrefix( $qualifier, $relation );
        }

        debug( "non-parenthesised term" );
        $word = matchSymbol( "qualifier or term" );

        last if ! isBaseRelation();

        $qualifier = $word;
        debug( "creating relation with word=$word" );
        $relation = CQL::Relation->new( $token->getString() );
        match( $token );

        while ($token->getType() == CQL_MODIFIER ) {
            match( $token );
            if ( !isRelationModifier() ) {
                croak( "expected relation modifier got " . $token->getString() );
            }
            $relation->addModifier( $token->getString() );
            match( $token );
        }
    }

    debug( "qualifier=$qualifier relation=$relation term=$word" );
    croak( "missing term" ) if ! defined($word) or $word eq '';

    my $node = CQL::TermNode->new( 
        qualifier   => $qualifier, 
        relation    => $relation, 
        term        => $word 
    );
    debug( "made term node: ".$node->toCQL() );
    return $node;
}

sub parsePrefix {
    my ( $qualifier, $relation ) = @_;
    debug( "prefix mapping" );
    my $name = undef;
    my $identifier = matchSymbol( "prefix name" );
    if ( $token->getType() == CQL_EQ ) {
        match( $token );
        $name = $identifier;
        $identifier = matchSymbol( "prefix identifier" );
    }
    my $node = parseQuery( $qualifier, $relation );
    return CQL::PrefixNode->new(
        name        => $name,
        identifier  => $identifier,
        subtree     => $node 
    );
}

sub gatherProxParameters {
    my $node = shift;
    if (0) {	# CQL 1.0 (obsolete)
    for (my $i=0; $i<4; $i++ ) {
        if ( $token->getType() != CQL_MODIFIER ) { 
            ## end of proximity parameters 
            return;
        }
        match($token);
        if ( $token->getType() != CQL_MODIFIER ) { 
            if ( $i==0 ) { gatherProxRelation($node); }
            elsif ( $i==1 ) { gatherProxDistance($node); }
            elsif ( $i==2 ) { gatherProxUnit($node); }
            elsif ( $i==3 ) { gatherProxOrdering($node); }
        }
    }
    } else {
        while ( $token->getType() == CQL_MODIFIER ) {
	    match( $token );
	    if ( $token->getType() == CQL_DISTANCE ) {
		match( $token );
		gatherProxRelation( $node );
		gatherProxDistance( $node );
	    } elsif ( $token->getType() == CQL_UNIT ) {
		match( $token );
		if ( $token->getType() != CQL_EQ ) {
		    croak( "expected proximity unit parameter got ".$token->getString() );
		}
		match( $token );
		gatherProxUnit( $node );
	    } elsif ( $token->getType() == CQL_ORDERED
		      || $token->getType() == CQL_UNORDERED ) {
		gatherProxOrdering( $node );
	    } else {
		croak( "expected proximity parameter got ". $token->getString()  ."(". $token->getType() .")" );
	    }
        }
    }
}

sub gatherProxRelation {
    my $node = shift;
    if ( ! isProxRelation() ) { 
        croak( "expected proximity relation got ".$token->getString() );
    }
    $node->addModifier( "relation", $token->getString() );
    match( $token );
    debug( "gatherProxRelation matched ".$token->getString() );
}

sub gatherProxDistance {
    my $node = shift;
    if ( $token->getString() !~ /^\d+$/ ) { 
        croak( "expected proximity distance got ".$token->getString() );
    }
    $node->addModifier( "distance", $token->getString() );
    match( $token );
    debug( "gatherProxDistance matched ".$token->getString() );
}

sub gatherProxUnit {
    my $node = shift;
    my $type = $token->getType();
    if( $type != CQL_PWORD and $type != CQL_SENTENCE and $type != CQL_PARAGRAPH
        and $type != CQL_ELEMENT ) {
        croak( "expected proximity unit got ".$token->getString() );
    }
    $node->addModifier( "unit", $token->getString() );
    match( $token );
    debug( "gatherProxUnit matched ".$token->getString() );
}

sub gatherProxOrdering {
    my $node = shift;
    my $type = $token->getType();
    if ( $type != CQL_ORDERED and $type != CQL_UNORDERED ) {
        croak( "expected proximity ordering got ".$token->getString() );
    }
    $node->addModifier( "ordering", $token->getString() );
    match( $token );
}

sub isBaseRelation {
    debug( "inside base relation: checking ttype=".$token->getType()." sval=".
        $token->getString() );
    if( $token->getType() == CQL_WORD and $token->getString() !~ /\./ ) {
        croak( "unknown first class relation: ".$token->getString() );
    }
    my $type = $token->getType();
    return( isProxRelation() or $type==CQL_ANY or $type==CQL_ALL 
        or $type==CQL_EXACT or $type==CQL_SCR or $type==CQL_WORD 
        or $type==CQL_WITHIN or $type==CQL_ENCLOSES);
}

sub isProxRelation {
    debug( "isProxRelation: checking ttype=".$token->getType()." sval=".
        $token->getString() );
    my $type = $token->getType();
    return( $type==CQL_LT or $type==CQL_GT or $type==CQL_EQ or $type==CQL_LE
        or $type==CQL_GE or $type==CQL_NE );
}

sub isRelationModifier {
    my $type = $token->getType();
    if ($type == CQL_WORD) {
        return $token->getString() =~ /\./;
    }
    return ($type==CQL_RELEVANT or $type==CQL_FUZZY or $type==CQL_STEM
        or $type==CQL_PHONETIC or $type==CQL_PWORD or $type==CQL_STRING
        or $type==CQL_ISODATE or $type==CQL_NUMBER or $type==CQL_URI
        or $type==CQL_PARTIAL or $type==CQL_MASKED or $type==CQL_UNMASKED
        or $type==CQL_NWSE);
}

sub match {
    my $expected = shift;
    debug( "in match(".$expected->getString().")" );
    if ( $token->getType() != $expected->getType() ) {
        croak( "expected ".$expected->getString() . 
            " but got " . $token->getString() );
    }
    $token = $lexer->nextToken();
    debug( "got token type=".$token->getType()." string=".$token->getString() );
}

sub matchSymbol {
    debug( "in match symbol" );
    my $return = $token->getString();
    match( $token );
    return $return;
}

sub debug {
    return unless $CQL::DEBUG;
    print STDERR "CQL::Parser: ", shift, "\n";
}

=head1 XCQL 

CQL has an XML representation which you can generate from a CQL parse
tree. Just call the toXCQL() method on the root node you get back
from a call to parse().

=head1 ERRORS AND DIAGNOSTICS

As mentioned above, a CQL syntax error will result in an exception being 
thrown. So if you have any doubts about the CQL that you are parsing you
should wrap the call to parse() in an eval block, and check $@
afterwards to make sure everything went ok.

    eval {
        my $node = $parser->parse( $cql );
    };
    if ( $@ ) {
        print "uhoh, exception $@\n";
    }

If you'd like to see blow by blow details while your CQL is being parsed
set $CQL::DEBUG equal to 1, and you will get details on STDERR. This is
useful if the parse tree is incorrect and you want to locate where things
are going wrong. Hopefully this won't happen, but if it does please notify the
author.

=head1 TODO

=over 4

=item * toYourEngineHere() please feel free to add functionality and send in
patches!

=back

=head1 THANKYOUS 

CQL::Parser is essentially a Perl port of Mike Taylor's cql-java package 
http://zing.z3950.org/cql/java/. Mike and IndexData were kind enough
to allow the author to write this port, and to make it available under
the terms of the Artistic License. Thanks Mike!

The CQL::Lexer package relies heavily on Stevan Little's excellent
String::Tokenizer. Thanks Stevan!

CQL::Parser was developed as a component of the Ockham project,
which is funded by the National Science Foundation. See http://www.ockham.org
for more information about Ockham.

=head1 AUTHOR

=over 4

=item * Ed Summers - ehs at pobox dot com

=item * Brian Cassidy - bricas at cpan dot org

=item * Wilbert Hengst - W.Hengst at uva dot nl

=back

=head1 COPYRIGHT AND LICENSE

Copyright 2004-2009 by Ed Summers

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut

1;