/usr/share/perl5/RDF/Query/Parser/RDQL.pm is in librdf-query-perl 2.918-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 | # RDF::Query::Parser::RDQL
# -----------------------------------------------------------------------------
=head1 NAME
RDF::Query::Parser::RDQL - An RDQL parser for RDF::Query
=head1 VERSION
This document describes RDF::Query::Parser::RDQL version 2.918.
=cut
package RDF::Query::Parser::RDQL;
use strict;
use warnings;
no warnings 'redefine';
use base qw(RDF::Query::Parser);
use Data::Dumper;
use Parse::RecDescent;
use Carp qw(carp croak confess);
use RDF::Query::Error qw(:try);
use Scalar::Util qw(blessed);
######################################################################
our ($VERSION, $lang, $languri);
BEGIN {
$::RD_TRACE = undef;
$::RD_HINT = undef;
$VERSION = '2.918';
$lang = 'rdql';
$languri = 'http://jena.hpl.hp.com/2003/07/query/RDQL';
}
our($RDQL_GRAMMAR);
BEGIN {
our $RDQL_GRAMMAR = <<'END';
query: 'SELECT' variable(s) SourceClause(?) 'WHERE' triplepattern(s) constraints(?) OptOrderBy(?) prefixes(?)
{
my $triples = RDF::Query::Algebra::GroupGraphPattern->new( @{ $item[5] } );
my $filter = ($item[6][0] || []);
if (scalar(@$filter)) {
$triples = RDF::Query::Parser->new_filter( $filter, $triples );
}
$return = {
method => 'SELECT',
variables => $item[2],
sources => $item[3][0],
triples => [ $triples ],
namespaces => (scalar(@{$item[8]}) ? $item[8][0] : {})
};
if (@{ $item[7] }) {
$return->{options}{orderby} = $item[9][0];
}
}
prefixes: 'USING' namespaces { $return = $item[2] }
OptOrderBy: 'ORDER BY' orderbyvariable(s) { $return = $item[2] }
orderbyvariable: variable { $return = ['ASC', $item[1]] }
| /ASC|DESC/i '[' variable ']' { $return = [uc($item[1]), $item[3]] }
SourceClause: ('SOURCE' | 'FROM') Source(s) { $return = $item[2] }
Source: URI { $return = [$item[1]] }
variable: '?' identifier { $return = RDF::Query::Parser->new_variable($item[2]) }
triplepattern: '(' VarUri VarUri VarUriConst ')' { $return = RDF::Query::Parser::RDQL::Triple->new(@item[2,3,4]) }
constraints: 'AND' Expression OptExpression(s?) {
if (scalar(@{ $item[3] })) {
my ($op, $expr) = @{ $item[3][0] };
$return = RDF::Query::Parser->new_function_expression( $op, $item[2], $expr );
} else {
$return = $item[2];
}
}
OptExpression: (',' | 'AND') Expression {
$return = [ 'sparql:logical-and', $item[2] ];
}
Expression: CondOrExpr {
$return = $item[1]
}
CondOrExpr: CondAndExpr CondOrExprOrPart(?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
$return = RDF::Query::Parser->new_function_expression( $op, $item[1], $expr );
} else {
$return = $item[1];
}
}
CondOrExprOrPart: '||' CondAndExpr { $return = [ 'sparql:logical-or', $item[2] ] }
CondAndExpr: ValueLogical CondAndExprAndPart(?) {
if (scalar(@{ $item[2] })) {
$return = RDF::Query::Parser->new_function_expression( 'sparql:logical-and', $item[1], $item[2][0][1] );
} else {
$return = $item[1];
}
}
CondAndExprAndPart: '&&' ValueLogical { $return = [ @item[1,2] ] }
ValueLogical: StringEqualityExpression { $return = $item[1] }
StringEqualityExpression: NumericalLogical StrEqExprPart(s?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
if ($op eq '~~') {
$return = RDF::Query::Parser->new_function_expression( 'sparql:regex', $item[1], $expr );
} else {
$return = RDF::Query::Parser->new_binary_expression( $op, $item[1], $expr );
}
} else {
$return = $item[1];
}
}
StrEqExprPart: ('==' | '!=' | '=~' | '~~') NumericalLogical { $return = [ @item[1,2] ] }
NumericalLogical: InclusiveOrExpression { $return = $item[1] }
InclusiveOrExpression: ExclusiveOrExpression InclusiveOrExprPart(s?) {
if (scalar(@{ $item[2] })) {
$return = [ $item[2][0][0], $item[1], $item[2][0][1] ];
} else {
$return = $item[1];
}
}
InclusiveOrExprPart: '|' ExclusiveOrExpression { $return = [ @item[1,2] ] }
ExclusiveOrExpression: AndExpression ExclusiveOrExprPart(s?) {
if (scalar(@{ $item[2] })) {
$return = [ $item[2][0][0], $item[1], map { $_->[1] } @{ $item[2] } ];
} else {
$return = $item[1];
}
}
ExclusiveOrExprPart: '^' AndExpression { $return = [ @item[1,2] ] }
AndExpression: ArithmeticCondition AndExprPart(s?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
$return = RDF::Query::Parser->new_binary_expression( $op, $item[1], $expr );
} else {
$return = $item[1];
}
}
AndExprPart: '&' ArithmeticCondition { $return = [ @item[1,2] ] }
ArithmeticCondition: EqualityExpression { $return = $item[1]; }
EqualityExpression: RelationalExpression EqualityExprPart(?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
$return = RDF::Query::Parser->new_binary_expression( $op, $item[1], $expr );
} else {
$return = $item[1];
}
}
EqualityExprPart: /(==|!=)/ RelationalExpression { $return = [ @item[1,2] ] }
RelationalExpression: NumericExpression RelationalExprPart(?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
$return = RDF::Query::Parser->new_binary_expression( $op, $item[1], $expr );
} else {
$return = $item[1];
}
}
RelationalExprPart: /(<|>|<=|>=)/ NumericExpression { $return = [ @item[1,2] ] }
NumericExpression: MultiplicativeExpression NumericExprPart(s?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
$return = RDF::Query::Parser->new_binary_expression( $op, $item[1], $expr );
} else {
$return = $item[1];
}
}
NumericExprPart: /([-+])/ MultiplicativeExpression { $return = [ @item[1,2] ] }
MultiplicativeExpression: UnaryExpression MultExprPart(s?) {
if (scalar(@{ $item[2] })) {
my ($op, $expr) = @{ $item[2][0] };
$return = RDF::Query::Parser->new_binary_expression( $op, $item[1], $expr );
} else {
$return = $item[1];
}
}
MultExprPart: /([\/*])/ UnaryExpression { $return = [ @item[1,2] ] }
UnaryExpression: UnaryExprNotPlusMinus { $return = $item[1] }
| /([-+])/ UnaryExpression { $return = [ @item[1,2] ] }
UnaryExprNotPlusMinus: /([~!])/ UnaryExpression { $return = [ @item[1,2] ] }
| PrimaryExpression { $return = $item[1] }
PrimaryExpression: (VarUriConst | FunctionCall) { $return = $item[1] }
| '(' Expression ')' {
$return = $item[2];
}
FunctionCall: identifier '(' ArgList ')' { $return = [ 'function', map { @{ $_ } } @item[1,3] ] }
ArgList: VarUriConst MoreArg(s) { $return = [ $item[1], @{ $item[2] } ] }
MoreArg: "," VarUriConst { $return = $item[2] }
Literal: (URI | CONST) { $return = $item[1] }
URL: qURI { $return = $item[1] }
VarUri: (variable | URI) { $return = $item[1] }
VarUriConst: (variable | CONST | URI) { $return = $item[1] }
namespaces: namespace morenamespace(s?) { $return = { map { %{ $_ } } ($item[1], @{ $item[2] }) } }
morenamespace: OptComma namespace { $return = $item[2] }
namespace: identifier 'FOR' qURI { $return = {@item[1,3]} }
OptComma: ',' | ''
identifier: /(([a-zA-Z0-9_.-])+)/ { $return = $1 }
URI: qURI { $return = RDF::Query::Parser->new_uri( $item[1] ) }
| QName { $return = RDF::Query::Parser::RDQL::URI->new( $item[1] ) }
qURI: '<' /[A-Za-z0-9_.!~*'()%;\/?:@&=+,#\$-]+/ '>' { $return = $item[2] }
QName: identifier ':' /([^ \t<>()]+)/ { $return = [@item[1,3]] }
CONST: Text { $return = RDF::Query::Parser->new_literal($item[1]) }
| Number { $return = RDF::Query::Parser->new_literal($item[1], undef, ($item[1] =~ /[.]/ ? 'http://www.w3.org/2001/XMLSchema#float' : 'http://www.w3.org/2001/XMLSchema#integer')) }
Number: /([0-9]+(\.[0-9]+)?)/ { $return = $item[1] }
Text: dQText | sQText | Pattern { $return = $item[1] }
sQText: "'" /([^']+)/ '"' { $return = $item[2] }
dQText: '"' /([^"]+)/ '"' { $return = $item[2] }
Pattern: '/' /([^\/]+(?:\\.[^\/]*)*)/ '/' { $return = $item[2] }
END
}
######################################################################
=head1 METHODS
=over 4
=item C<new ( $query_object ) >
Returns a new RDF::Query object.
=cut
{ my $parser;
sub new {
my $class = shift;
unless ($parser) {
$parser = new Parse::RecDescent ($RDQL_GRAMMAR);
}
my $self = bless( {
parser => $parser
}, $class );
return $self;
} }
=item C<parse ( $query ) >
Parses the supplied RDQL query string, returning a parse tree.
=cut
sub parse {
my $self = shift;
my $query = shift;
my $parser = $self->parser;
my $parsed = $parser->query( $query );
if ($parsed) {
my $pattern = $parsed->{triples}[0];
if (blessed($pattern)) {
my $ns = $parsed->{namespaces};
$pattern = $self->_fixup_pattern( $pattern, $ns );
my $fixed = $pattern->qualify_uris( $ns );
$parsed->{triples}[0] = $fixed;
}
$pattern = RDF::Query::Algebra::Project->new( $parsed->{triples}[0], $parsed->{variables} );
$parsed->{triples}[0] = $pattern;
return $parsed;
} else {
return $self->fail( "Failed to parse: '$query'" );
}
}
sub _fixup_pattern {
my $self = shift;
my $pattern = shift;
my $ns = shift;
my @uris = $pattern->subpatterns_of_type('RDF::Query::Parser::RDQL::URI');
foreach my $u (@uris) {
my $ns = $ns->{ $u->[0] };
my $uri = join('', $ns, $u->[1]);
@{ $u } = ( 'URI', $uri );
bless($u, 'RDF::Query::Node::Resource'); # evil
}
my @triples = $pattern->subpatterns_of_type('RDF::Query::Parser::RDQL::Triple');
foreach my $t (@triples) {
bless($t, 'RDF::Query::Algebra::Triple'); # evil
}
return $pattern;
}
sub AUTOLOAD {
my $self = $_[0];
throw RDF::Query::Error::MethodInvocationError unless (blessed($self));
my $class = ref($_[0]);
our $AUTOLOAD;
return if ($AUTOLOAD =~ /DESTROY$/);
my $method = $AUTOLOAD;
$method =~ s/^.*://;
if (exists($self->{ $method })) {
no strict 'refs';
*$AUTOLOAD = sub {
my $self = shift;
my $class = ref($self);
return $self->{ $method };
};
goto &$method;
} else {
throw RDF::Query::Error::MethodError ( -text => qq[Can't locate object method "$method" via package $class] );
}
}
package RDF::Query::Parser::RDQL::URI;
use strict;
use warnings;
use base qw(RDF::Query::Algebra);
sub new {
my $class = shift;
my $data = shift;
my ($ns, $local) = @{ $data };
return bless([$ns, $local], $class);
}
sub construct_args {
my $self = shift;
return [ @$self ];
}
package RDF::Query::Parser::RDQL::Triple;
use strict;
use warnings;
use base qw(RDF::Query::Algebra);
sub new {
my $class = shift;
my @nodes = @_;
return bless([@nodes], $class);
}
sub construct_args {
my $self = shift;
return @$self;
}
1;
__END__
=back
=head1 REVISION HISTORY
$Log$
Revision 1.5 2006/01/11 06:03:45 greg
- Removed use of Data::Dumper::Simple.
Revision 1.4 2005/05/08 08:26:09 greg
- Added initial support for SPARQL ASK, DESCRIBE and CONSTRUCT queries.
- Added new test files for new query types.
- Added methods to bridge classes for creating statements and blank nodes.
- Added as_string method to bridge classes for getting string versions of nodes.
- Broke out triple fixup code into fixup_triple_bridge_variables().
- Updated FILTER test to use new Geo::Distance API.
Revision 1.3 2005/04/26 02:54:40 greg
- added core support for custom function constraints support
- added initial SPARQL support for custom function constraints
- SPARQL variables may now begin with the '$' sigil
- broke out URL fixups into its own method
- added direction support for ORDER BY (ascending/descending)
- added 'next', 'current', and 'end' to Stream API
Revision 1.2 2005/04/25 00:59:29 greg
- streams are now objects usinig the Redland QueryResult API
- RDF namespace is now always available in queries
- row() now uses a stream when calling execute()
- check_constraints() now copies args for recursive calls (instead of pass-by-ref)
- added ORDER BY support to RDQL parser
- SPARQL constraints now properly use the 'FILTER' keyword
- SPARQL constraints can now use '&&' as an operator
- SPARQL namespace declaration is now optional
Revision 1.1 2005/04/21 02:21:44 greg
- major changes (resurecting the project)
- broke out the query parser into it's own RDQL class
- added initial support for a SPARQL parser
- added support for blank nodes
- added lots of syntactic sugar (with blank nodes, multiple predicates and objects)
- moved model-specific code into RDF::Query::Model::*
- cleaned up the model-bridge code
- moving over to redland's query API (pass in the model when query is executed)
=head1 AUTHOR
Gregory Williams <gwilliams@cpan.org>
=cut
|