| 1 | # | ||
| 2 | # The contents of this file are subject to the Mozilla Public | ||
| 3 | # License Version 1.1 (the "License"); you may not use this file | ||
| 4 | # except in compliance with the License. You may obtain a copy of | ||
| 5 | # the License at http://www.mozilla.org/MPL/ | ||
| 6 | # | ||
| 7 | # Software distributed under the License is distributed on an "AS | ||
| 8 | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
| 9 | # implied. See the License for the specific language governing | ||
| 10 | # rights and limitations under the License. | ||
| 11 | # | ||
| 12 | # The Original Code is the RDF::Core module | ||
| 13 | # | ||
| 14 | # The Initial Developer of the Original Code is Ginger Alliance Ltd. | ||
| 15 | # Portions created by Ginger Alliance are | ||
| 16 | # Copyright (C) 2001 Ginger Alliance Ltd. | ||
| 17 | # All Rights Reserved. | ||
| 18 | # | ||
| 19 | # Contributor(s): | ||
| 20 | # | ||
| 21 | # Alternatively, the contents of this file may be used under the | ||
| 22 | # terms of the GNU General Public License Version 2 or later (the | ||
| 23 | # "GPL"), in which case the provisions of the GPL are applicable | ||
| 24 | # instead of those above. If you wish to allow use of your | ||
| 25 | # version of this file only under the terms of the GPL and not to | ||
| 26 | # allow others to use your version of this file under the MPL, | ||
| 27 | # indicate your decision by deleting the provisions above and | ||
| 28 | # replace them with the notice and other provisions required by | ||
| 29 | # the GPL. If you do not delete the provisions above, a recipient | ||
| 30 | # may use your version of this file under either the MPL or the | ||
| 31 | # GPL. | ||
| 32 | # | ||
| 33 | |||
| 34 | package RDF::Core::Query; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | require Exporter; | ||
| 38 | |||
| 39 | |||
| 40 | use Carp; | ||
| 41 | |||
| 42 | use Exporter; | ||
| 43 | use vars qw(@ISA @EXPORT_OK @EXP_SYNTAX %EXPORT_TAGS); | ||
| 44 | |||
| 45 | @ISA = qw(Exporter); | ||
| 46 | @EXP_SYNTAX = qw(Q_QUERY Q_RESULTSET Q_SOURCE Q_SOURCEPATH Q_HASTARGET | ||
| 47 | Q_TARGET Q_CONDITION Q_NAMESPACE Q_MATCH Q_PATH | ||
| 48 | Q_CLASS Q_BINDING Q_ELEMENTS Q_ELEMENTPATH Q_ELEMENT | ||
| 49 | Q_FUNCTION Q_NODE Q_VARIABLE Q_URIDEF Q_NAME Q_EXPRESSION | ||
| 50 | Q_CONNECTION Q_RELATION Q_OPERATION Q_LITERAL Q_URI | ||
| 51 | Q_SUBSTITUTION); | ||
| 52 | @EXPORT_OK = (@EXP_SYNTAX); | ||
| 53 | %EXPORT_TAGS = (syntax => \@EXP_SYNTAX); | ||
| 54 | |||
| 55 | # token types | ||
| 56 | use constant TOK_NONE => 'TOK_NONE'; | ||
| 57 | use constant TOK_END => 'TOK_END'; | ||
| 58 | |||
| 59 | use constant TOK_LITERAL => 'TOK_LITERAL'; | ||
| 60 | use constant TOK_VAR => 'TOK_VAR'; | ||
| 61 | use constant TOK_URI => 'TOK_URI'; | ||
| 62 | use constant TOK_NAME => 'TOK_NAME'; | ||
| 63 | use constant TOK_COMMENT => 'TOK_COMMENT'; | ||
| 64 | use constant TOK_SUBSTITUTION => 'TOK_SUBSTITUTION'; | ||
| 65 | |||
| 66 | use constant TOK_CLASS => 'TOK_CLASS'; | ||
| 67 | use constant TOK_MATCH => 'TOK_MATCH'; | ||
| 68 | |||
| 69 | use constant TOK_LPAREN => 'TOK_LPAREN'; | ||
| 70 | use constant TOK_RPAREN => 'TOK_RPAREN'; | ||
| 71 | use constant TOK_LCUR => 'TOK_LCUR'; | ||
| 72 | use constant TOK_RCUR => 'TOK_RCUR'; | ||
| 73 | use constant TOK_PERIOD => 'TOK_PERIOD'; | ||
| 74 | use constant TOK_COMMA => 'TOK_COMMA'; | ||
| 75 | use constant TOK_COLON => 'TOK_COLON'; | ||
| 76 | use constant TOK_PIPE => 'TOK_PIPE'; | ||
| 77 | use constant TOK_EQ => 'TOK_EQ'; | ||
| 78 | use constant TOK_NEQ => 'TOK_NEQ'; | ||
| 79 | use constant TOK_LE => 'TOK_LE'; | ||
| 80 | use constant TOK_LT => 'TOK_LT'; | ||
| 81 | use constant TOK_GE => 'TOK_GE'; | ||
| 82 | use constant TOK_GT => 'TOK_GT'; | ||
| 83 | |||
| 84 | use constant TOK_SELECT => 'TOK_SELECT'; | ||
| 85 | use constant TOK_WHERE => 'TOK_WHERE'; | ||
| 86 | use constant TOK_FROM => 'TOK_FROM'; | ||
| 87 | use constant TOK_USE => 'TOK_USE'; | ||
| 88 | use constant TOK_FOR => 'TOK_FOR'; | ||
| 89 | use constant TOK_AND => 'TOK_AND'; | ||
| 90 | use constant TOK_OR => 'TOK_OR'; | ||
| 91 | |||
| 92 | #query syntax elements | ||
| 93 | |||
| 94 | use constant Q_QUERY => 'QUERY'; | ||
| 95 | use constant Q_RESULTSET => 'RESULTSET'; | ||
| 96 | use constant Q_SOURCE => 'SOURCE'; | ||
| 97 | use constant Q_SOURCEPATH => 'SOURCEPATH'; | ||
| 98 | use constant Q_HASTARGET => 'HASTARGET'; | ||
| 99 | use constant Q_TARGET => 'TARGET'; | ||
| 100 | use constant Q_CONDITION => 'CONDITION'; | ||
| 101 | use constant Q_NAMESPACE => 'NAMESPACE'; | ||
| 102 | use constant Q_MATCH => 'MATCH'; | ||
| 103 | use constant Q_PATH => 'PATH'; | ||
| 104 | use constant Q_CLASS => 'CLASS'; | ||
| 105 | use constant Q_BINDING => 'BINDING'; | ||
| 106 | use constant Q_ELEMENTS => 'ELEMENTS'; | ||
| 107 | use constant Q_ELEMENTPATH => 'ELEMENTPATH'; | ||
| 108 | use constant Q_ELEMENT => 'ELEMENT'; | ||
| 109 | use constant Q_FUNCTION => 'FUNCTION'; | ||
| 110 | use constant Q_NODE => 'NODE'; | ||
| 111 | use constant Q_VARIABLE => 'VARIABLE'; | ||
| 112 | use constant Q_URIDEF => 'URIDEF'; | ||
| 113 | use constant Q_NAME => 'NAME'; | ||
| 114 | use constant Q_EXPRESSION => 'EXPRESSION'; | ||
| 115 | use constant Q_CONNECTION => 'CONNECTION'; | ||
| 116 | use constant Q_RELATION => 'RELATION'; | ||
| 117 | use constant Q_OPERATION => 'OPERATION'; | ||
| 118 | use constant Q_LITERAL => 'LITERAL'; | ||
| 119 | use constant Q_URI => 'URI'; | ||
| 120 | use constant Q_SUBSTITUTION => 'SUBSTITUTION'; | ||
| 121 | |||
| 122 | sub new { | ||
| 123 | my ($pkg,%options) = @_; | ||
| 124 | $pkg = ref $pkg || $pkg; | ||
| 125 | my $self = {}; | ||
| 126 | $self->{_options} = \%options; | ||
| 127 | bless $self, $pkg; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub getOptions { | ||
| 131 | my $self = shift; | ||
| 132 | return $self->{_options}; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub query { | ||
| 136 | my ($self, $queryString) = @_; | ||
| 137 | my @tokens = $self->_tokenize($queryString); | ||
| 138 | $self->_parse (\@tokens); | ||
| 139 | $self->_syntaxTree($self->{+Q_QUERY}[0],50,'') | ||
| 140 | if $self->getOptions->{Debug}; | ||
| 141 | return $self->getOptions->{Evaluator}->evaluate($self->{+Q_QUERY}[0]); | ||
| 142 | } | ||
| 143 | sub prepare { | ||
| 144 | my ($self, $queryString) = @_; | ||
| 145 | my @tokens = $self->_tokenize($queryString); | ||
| 146 | $self->_parse (\@tokens); | ||
| 147 | $self->_syntaxTree($self->{+Q_QUERY}[0],50,'') | ||
| 148 | if $self->getOptions->{Debug}; | ||
| 149 | return $self->{+Q_QUERY}[0]; | ||
| 150 | } | ||
| 151 | sub execute { | ||
| 152 | my ($self, $substitutions, $query) = @_; | ||
| 153 | $query ||= $self->{+Q_QUERY}[0]; | ||
| 154 | return $self->getOptions->{Evaluator}->evaluate($query, $substitutions); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _tokenize { | ||
| 158 | #Translate query string into an array of tokens | ||
| 159 | #token's properties: type, value | ||
| 160 | |||
| 161 | my ($self, $queryString) = @_; | ||
| 162 | print "Tokenizing $queryString" | ||
| 163 | if $self->getOptions->{Debug}; | ||
| 164 | my @tokens; | ||
| 165 | my $pos = 0; | ||
| 166 | my $lastToken = -1; | ||
| 167 | until ($lastToken eq TOK_END || $lastToken eq TOK_NONE) { | ||
| 168 | my $token = $self->_nextToken(\$queryString,\$pos); | ||
| 169 | unless ($token->{type} eq TOK_COMMENT) { | ||
| 170 | push @tokens, $token; | ||
| 171 | $lastToken = $token->{type}; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | return @tokens; | ||
| 175 | } | ||
| 176 | sub _nextToken { | ||
| 177 | my ($self, $str, $pos) = @_; | ||
| 178 | my $retVal; | ||
| 179 | $retVal->{type} = TOK_NONE; | ||
| 180 | |||
| 181 | #skip blank spaces | ||
| 182 | while (substr($$str, $$pos, 1) =~ /\s/) { | ||
| 183 | $$pos++; | ||
| 184 | } | ||
| 185 | |||
| 186 | my $firstChar = substr($$str, $$pos, 1); | ||
| 187 | my $secondChar = substr($$str, $$pos + 1, 1); | ||
| 188 | |||
| 189 | print "\ngetting next token at pos $$pos, from ",substr ($$str, $$pos, 20) | ||
| 190 | if $self->getOptions->{Debug}; | ||
| 191 | |||
| 192 | if ($firstChar eq '') { | ||
| 193 | $retVal->{type} = TOK_END; | ||
| 194 | } elsif ($firstChar eq '(') { | ||
| 195 | $retVal->{type} = TOK_LPAREN; | ||
| 196 | $retVal->{value} = $firstChar; | ||
| 197 | $$pos++; | ||
| 198 | } elsif ($firstChar eq ')') { | ||
| 199 | $retVal->{type} = TOK_RPAREN; | ||
| 200 | $retVal->{value} = $firstChar; | ||
| 201 | $$pos++; | ||
| 202 | } elsif ($firstChar eq '{') { | ||
| 203 | $retVal->{type} = TOK_LCUR; | ||
| 204 | $retVal->{value} = $firstChar; | ||
| 205 | $$pos++; | ||
| 206 | } elsif ($firstChar eq '}') { | ||
| 207 | $retVal->{type} = TOK_RCUR; | ||
| 208 | $retVal->{value} = $firstChar; | ||
| 209 | $$pos++; | ||
| 210 | } elsif ($firstChar eq ',') { | ||
| 211 | $retVal->{type} = TOK_COMMA; | ||
| 212 | $retVal->{value} = $firstChar; | ||
| 213 | $$pos++; | ||
| 214 | } elsif ($firstChar eq '|') { | ||
| 215 | $retVal->{type} = TOK_PIPE; | ||
| 216 | $retVal->{value} = $firstChar; | ||
| 217 | $$pos++; | ||
| 218 | } elsif ($firstChar eq '=') { | ||
| 219 | if ($secondChar eq '>') { | ||
| 220 | $retVal->{type} = TOK_MATCH; | ||
| 221 | $retVal->{value} = $firstChar.$secondChar; | ||
| 222 | $$pos += 2; | ||
| 223 | } else { | ||
| 224 | $retVal->{type} = TOK_EQ; | ||
| 225 | $retVal->{value} = $firstChar; | ||
| 226 | $$pos++; | ||
| 227 | } | ||
| 228 | } elsif ($firstChar eq '-') { | ||
| 229 | if ($secondChar eq '>') { | ||
| 230 | $retVal->{type} = TOK_PERIOD; | ||
| 231 | $retVal->{value} = $firstChar.$secondChar; | ||
| 232 | $$pos += 2; | ||
| 233 | } elsif ($secondChar eq '-') { | ||
| 234 | $retVal->{type} = TOK_COMMENT; | ||
| 235 | $retVal->{value} = $self->_str2Token($str, $pos, $retVal->{type}) | ||
| 236 | } | ||
| 237 | } elsif ($firstChar eq '<') { | ||
| 238 | if ($secondChar eq '=') { | ||
| 239 | $retVal->{type} = TOK_LE; | ||
| 240 | $retVal->{value} = $firstChar.$secondChar; | ||
| 241 | $$pos += 2; | ||
| 242 | } else { | ||
| 243 | $retVal->{type} = TOK_LT; | ||
| 244 | $retVal->{value} = $firstChar; | ||
| 245 | $$pos++; | ||
| 246 | } | ||
| 247 | } elsif ($firstChar eq '>') { | ||
| 248 | if ($secondChar eq '=') { | ||
| 249 | $retVal->{type} = TOK_GE; | ||
| 250 | $retVal->{value} = $firstChar.$secondChar; | ||
| 251 | $$pos += 2; | ||
| 252 | } else { | ||
| 253 | $retVal->{type} = TOK_GT; | ||
| 254 | $retVal->{value} = $firstChar; | ||
| 255 | $$pos++; | ||
| 256 | } | ||
| 257 | } elsif ($firstChar eq ':') { | ||
| 258 | if ($secondChar eq ':') { | ||
| 259 | $retVal->{type} = TOK_CLASS; | ||
| 260 | $retVal->{value} = $firstChar.$secondChar; | ||
| 261 | $$pos += 2; | ||
| 262 | } else { | ||
| 263 | $retVal->{type} = TOK_COLON; | ||
| 264 | $retVal->{value} = $firstChar; | ||
| 265 | $$pos++; | ||
| 266 | } | ||
| 267 | } elsif (substr ($$str, $$pos, 7) =~ /select\W/i) { | ||
| 268 | $retVal->{type} = TOK_SELECT; | ||
| 269 | $retVal->{value} = substr ($$str, $$pos, 6); | ||
| 270 | $$pos += 6; | ||
| 271 | } elsif (substr ($$str, $$pos, 6) =~ /where\W/i) { | ||
| 272 | $retVal->{type} = TOK_WHERE; | ||
| 273 | $retVal->{value} = substr ($$str, $$pos, 5); | ||
| 274 | $$pos += 5; | ||
| 275 | } elsif (substr ($$str, $$pos, 5) =~ /from\W/i) { | ||
| 276 | $retVal->{type} = TOK_FROM; | ||
| 277 | $retVal->{value} = substr ($$str, $$pos, 4); | ||
| 278 | $$pos += 4; | ||
| 279 | } elsif (substr ($$str, $$pos, 4) =~ /use\W/i) { | ||
| 280 | $retVal->{type} = TOK_USE; | ||
| 281 | $retVal->{value} = substr ($$str, $$pos, 3); | ||
| 282 | $$pos += 3; | ||
| 283 | } elsif (substr ($$str, $$pos, 4) =~ /for\W/i) { | ||
| 284 | $retVal->{type} = TOK_FOR; | ||
| 285 | $retVal->{value} = substr ($$str, $$pos, 3); | ||
| 286 | $$pos += 3; | ||
| 287 | } elsif (substr ($$str, $$pos, 4) =~ /and\W/i) { | ||
| 288 | $retVal->{type} = TOK_AND; | ||
| 289 | $retVal->{value} = substr ($$str, $$pos, 3); | ||
| 290 | $$pos += 3; | ||
| 291 | } elsif (substr ($$str, $$pos, 3) =~ /or\W/i) { | ||
| 292 | $retVal->{type} = TOK_OR; | ||
| 293 | $retVal->{value} = substr ($$str, $$pos, 2); | ||
| 294 | $$pos += 2; | ||
| 295 | } elsif (substr ($$str, $$pos, 2) =~ /!=/i) { | ||
| 296 | $retVal->{type} = TOK_NEQ; | ||
| 297 | $retVal->{value} = substr ($$str, $$pos, 2); | ||
| 298 | $$pos += 2; | ||
| 299 | } elsif ($firstChar eq '"' || $firstChar eq '\'') { | ||
| 300 | $retVal->{type} = TOK_LITERAL; | ||
| 301 | $retVal->{value} = $self->_str2Token($str, $pos, $retVal->{type}); | ||
| 302 | } elsif ($firstChar eq '?' || $firstChar eq '$') { | ||
| 303 | $retVal->{type} = TOK_VAR; | ||
| 304 | $retVal->{value} = $self->_str2Token($str, $pos, $retVal->{type}); | ||
| 305 | } elsif ($firstChar eq '#') { | ||
| 306 | $retVal->{type} = TOK_SUBSTITUTION; | ||
| 307 | $retVal->{value} = $self->_str2Token($str, $pos, TOK_VAR); | ||
| 308 | } elsif ($firstChar eq '[') { | ||
| 309 | $retVal->{type} = TOK_URI; | ||
| 310 | $retVal->{value} = $self->_str2Token($str, $pos, $retVal->{type}); | ||
| 311 | } elsif ($firstChar =~ /\w/) { | ||
| 312 | $retVal->{type} = TOK_NAME; | ||
| 313 | $retVal->{value} = $self->_str2Token($str, $pos, $retVal->{type}); | ||
| 314 | } elsif ($firstChar eq '/' && $secondChar eq '*') { | ||
| 315 | $retVal->{type} = TOK_COMMENT; | ||
| 316 | $retVal->{value} = $self->_str2Token($str, $pos, $retVal->{type}) | ||
| 317 | } else { | ||
| 318 | $retVal->{type} = TOK_NONE; | ||
| 319 | $retVal->{value} = $firstChar; | ||
| 320 | $$pos++; | ||
| 321 | |||
| 322 | } | ||
| 323 | |||
| 324 | return $retVal; | ||
| 325 | } | ||
| 326 | sub _str2Token { | ||
| 327 | my ($self, $str, $pos, $tokenType) = @_; | ||
| 328 | my $retVal; | ||
| 329 | if ($tokenType eq TOK_LITERAL) { | ||
| 330 | my $quote = substr ($$str, $$pos, 1); | ||
| 331 | my $subpos = $$pos ; | ||
| 332 | my $escaped = ''; | ||
| 333 | my $found = 0; | ||
| 334 | while (defined(my $char = substr ($$str, ++$subpos, 1))) { | ||
| 335 | if ($char eq $quote && !$escaped) { | ||
| 336 | $found = $subpos; | ||
| 337 | last; | ||
| 338 | } else { | ||
| 339 | if (!($char eq "\\") || $escaped) { | ||
| 340 | $retVal .= $char; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | if ($char eq "\\") { | ||
| 344 | $escaped = !$escaped; | ||
| 345 | } else { | ||
| 346 | $escaped = ''; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | unless ($found) { | ||
| 350 | croak "Syntax error: Infinite literal at position ".$$pos | ||
| 351 | ."\n".substr ($$str, $$pos,30); | ||
| 352 | } | ||
| 353 | $$pos = ++$subpos; | ||
| 354 | } elsif ($tokenType eq TOK_URI) { | ||
| 355 | my $delim = "]"; | ||
| 356 | my $subpos = $$pos ; | ||
| 357 | my $found = 0; | ||
| 358 | while (defined (my $char = substr ($$str, ++$subpos, 1))) { | ||
| 359 | if ($char eq $delim) { | ||
| 360 | $found = $subpos; | ||
| 361 | last; | ||
| 362 | } else { | ||
| 363 | $retVal .= $char; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | unless ($found) { | ||
| 367 | croak "Syntax error: Infinite URI at position ".$$pos | ||
| 368 | ."\n".substr ($$str, $$pos,30); | ||
| 369 | } | ||
| 370 | $$pos = ++$subpos; | ||
| 371 | } elsif ($tokenType eq TOK_VAR) { | ||
| 372 | $retVal = substr ($$str, $$pos++,1); | ||
| 373 | $retVal .= $self->_str2Token($str, $pos, TOK_NAME); | ||
| 374 | } elsif ($tokenType eq TOK_NAME) { | ||
| 375 | my $subpos = $$pos ; | ||
| 376 | if (substr ($$str, $subpos ,1) =~ /[a-zA-Z_]/ ) { | ||
| 377 | $retVal = substr ($$str, $subpos,1); | ||
| 378 | } else { | ||
| 379 | croak "Syntax error: Invalid name at position ".$$pos | ||
| 380 | ."\n".substr ($$str, $$pos,30); | ||
| 381 | } | ||
| 382 | while (defined (my $char = substr ($$str, ++$subpos, 1))) { | ||
| 383 | if ($char =~ /[a-zA-Z0-9_]/) { | ||
| 384 | $retVal .= $char; | ||
| 385 | } else { | ||
| 386 | last; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | $$pos = $subpos; | ||
| 390 | } elsif ($tokenType eq TOK_COMMENT) { | ||
| 391 | my $delim; | ||
| 392 | my $delimLength; | ||
| 393 | |||
| 394 | if (substr ($$str, $$pos, 2) eq '/*') { | ||
| 395 | $delim = "*/"; | ||
| 396 | $delimLength = 2; | ||
| 397 | } else { | ||
| 398 | $delim = "\n"; | ||
| 399 | $delimLength = 1; | ||
| 400 | } | ||
| 401 | my $subpos = $$pos ; | ||
| 402 | my $found = 0; | ||
| 403 | while (defined (my $char = substr ($$str, ++$subpos, $delimLength))) { | ||
| 404 | if ($char eq $delim) { | ||
| 405 | $found = $subpos; | ||
| 406 | last; | ||
| 407 | } else { | ||
| 408 | $retVal .= $char; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | unless ($found || $delim eq "\n") { | ||
| 412 | croak "Syntax error: Infinite comment at position ".$$pos | ||
| 413 | ."\n".substr ($$str, $$pos,30); | ||
| 414 | } | ||
| 415 | $$pos = $subpos += $delimLength; | ||
| 416 | } | ||
| 417 | return $retVal; | ||
| 418 | } | ||
| 419 | sub _parse { | ||
| 420 | my ($self, $tokens) = @_; | ||
| 421 | |||
| 422 | #init query tree | ||
| 423 | delete $self->{+Q_QUERY}; | ||
| 424 | my @context = ([Q_QUERY, 0]); | ||
| 425 | my @rndParens; #keep track of which parenthesis you are in | ||
| 426 | $self->_treeNode(\@context); | ||
| 427 | |||
| 428 | for (my $i = 0; $i < @$tokens; $i++) { | ||
| 429 | my $token = $tokens->[$i]; | ||
| 430 | #debug############################################################ | ||
| 431 | if ($self->getOptions->{Debug}) { | ||
| 432 | use Data::Dumper; | ||
| 433 | print Dumper @context; | ||
| 434 | print "TOKEN: ",$token->{value}, "\n"; | ||
| 435 | } | ||
| 436 | ################################################################## | ||
| 437 | if ($token->{type} eq TOK_NONE) { | ||
| 438 | _errSyntax ($tokens, $i, \@context, "Token not recognized"); | ||
| 439 | } elsif ($token->{type} eq TOK_END) { | ||
| 440 | } elsif ($token->{type} eq TOK_LITERAL) { | ||
| 441 | until ( @context == 0 || | ||
| 442 | @context[@context - 1]->[0] eq Q_TARGET || | ||
| 443 | @context[@context - 1]->[0] eq Q_ELEMENTPATH || | ||
| 444 | @context[@context - 1]->[0] eq Q_PATH || | ||
| 445 | @context[@context - 1]->[0] eq Q_EXPRESSION) { | ||
| 446 | pop @context; | ||
| 447 | } | ||
| 448 | if (@context > 0) { | ||
| 449 | #we might have expected Q_ELEMENT, let's remove it now | ||
| 450 | my $node = $self->_treeNode(\@context); | ||
| 451 | #TODO: $node should point to an empty structure, raise error | ||
| 452 | # if there is a token value in it | ||
| 453 | # $node->{elements}[0]{element} = [] is ok | ||
| 454 | # $node->{elements}[0]{element} = [{node}...] is not ok | ||
| 455 | |||
| 456 | undef %$node; | ||
| 457 | } else { | ||
| 458 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 459 | } | ||
| 460 | |||
| 461 | if (@context[@context - 1]->[0] eq Q_TARGET || | ||
| 462 | @context[@context - 1]->[0] eq Q_PATH || | ||
| 463 | @context[@context - 1]->[0] eq Q_ELEMENTPATH) { | ||
| 464 | |||
| 465 | my $node = $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 466 | $node = $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 467 | $node->{+Q_LITERAL}->[0] = $token->{value}; | ||
| 468 | pop @context; #Q_EXPRESSION (inner) | ||
| 469 | } elsif (@context[@context - 1]->[0] eq Q_EXPRESSION) { | ||
| 470 | #just add literal | ||
| 471 | my $node = $self->_treeNode(\@context); | ||
| 472 | $node->{+Q_LITERAL}->[0] = $token->{value}; | ||
| 473 | pop @context #Q_EXPRESSION | ||
| 474 | } else { | ||
| 475 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 476 | } | ||
| 477 | } elsif ($token->{type} eq TOK_VAR) { | ||
| 478 | if (@context[@context - 1]->[0] eq Q_BINDING) { | ||
| 479 | #variable binding | ||
| 480 | my $node = $self->_treeNodeAppend(\@context, Q_VARIABLE); | ||
| 481 | $node->{+Q_NAME}->[0] = $token->{value}; | ||
| 482 | pop @context; #Q_VARIABLE | ||
| 483 | } elsif (@context[@context - 1]->[0] eq Q_ELEMENT) { | ||
| 484 | my $node = $self->_treeNodeAppend(\@context, Q_VARIABLE); | ||
| 485 | $node->{+Q_NAME}->[0] = $token->{value}; | ||
| 486 | pop @context; #Q_VARIABLE | ||
| 487 | pop @context; #Q_ELEMENT | ||
| 488 | } else { | ||
| 489 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 490 | } | ||
| 491 | } elsif ($token->{type} eq TOK_URI) { | ||
| 492 | |||
| 493 | if (@context[@context - 1]->[0] eq Q_ELEMENT) { | ||
| 494 | my $node = $self->_treeNodeAppend(\@context, Q_NODE); | ||
| 495 | $node->{+Q_URI}->[0] = $token->{value}; | ||
| 496 | pop @context; #Q_NODE | ||
| 497 | pop @context; #Q_ELEMENT | ||
| 498 | } elsif (@context[@context - 1]->[0] eq Q_NAMESPACE) { | ||
| 499 | my $node = $self->_treeNode(\@context); | ||
| 500 | my $index = defined $node->{+Q_URI} ? @{$node->{+Q_URI}} : 0; | ||
| 501 | $node->{+Q_URI}->[$index] = $token->{value}; | ||
| 502 | } else { | ||
| 503 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 504 | } | ||
| 505 | } elsif ($token->{type} eq TOK_SUBSTITUTION) { | ||
| 506 | |||
| 507 | if (@context[@context - 1]->[0] eq Q_ELEMENT) { | ||
| 508 | my $node = $self->_treeNodeAppend(\@context, Q_SUBSTITUTION); | ||
| 509 | $node->{+Q_NAME}->[0] = $token->{value}; | ||
| 510 | pop @context; #Q_SUBSTITUTION | ||
| 511 | pop @context; #Q_ELEMENT | ||
| 512 | } else { | ||
| 513 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 514 | } | ||
| 515 | } elsif ($token->{type} eq TOK_NAME) { | ||
| 516 | if (@context[@context - 1]->[0] eq Q_ELEMENT) { | ||
| 517 | if ($tokens->[$i+1]->{type} eq TOK_LPAREN) { | ||
| 518 | my $node = $self->_treeNodeAppend(\@context, Q_FUNCTION); | ||
| 519 | $node->{+Q_NAME}->[0] = $token->{value}; | ||
| 520 | } else { | ||
| 521 | my $node = $self->_treeNodeAppend(\@context, Q_NODE); | ||
| 522 | $node->{+Q_NAME}->[0] = $token->{value}; | ||
| 523 | unless ($tokens->[$i+1]->{type} eq TOK_COLON) { | ||
| 524 | pop @context; #Q_NODE | ||
| 525 | pop @context; #Q_ELEMENT | ||
| 526 | } | ||
| 527 | } | ||
| 528 | } elsif (@context[@context - 1]->[0] eq Q_NODE) { | ||
| 529 | my $node = $self->_treeNode(\@context); | ||
| 530 | my $index = @{$node->{+Q_NAME}}; | ||
| 531 | _errSyntax ($tokens, $i, \@context, "Invalid node") | ||
| 532 | if $index > 1; | ||
| 533 | $node->{+Q_NAME}->[$index] = $token->{value}; | ||
| 534 | unless ($tokens->[$i+1]->{type} eq TOK_COLON) { | ||
| 535 | pop @context; #Q_NODE | ||
| 536 | pop @context; #Q_ELEMENT | ||
| 537 | } | ||
| 538 | } elsif (@context[@context - 1]->[0] eq Q_NAMESPACE) { | ||
| 539 | my $node = $self->_treeNode(\@context); | ||
| 540 | my $index = defined $node->{+Q_NAME} ? @{$node->{+Q_NAME}} : 0; | ||
| 541 | $node->{+Q_NAME}->[$index] = $token->{value}; | ||
| 542 | } else { | ||
| 543 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 544 | } | ||
| 545 | } elsif ($token->{type} eq TOK_CLASS) { | ||
| 546 | until ( @context == 0 || | ||
| 547 | @context[@context - 1]->[0] eq Q_PATH || | ||
| 548 | @context[@context - 1]->[0] eq Q_SOURCEPATH) { | ||
| 549 | pop @context; | ||
| 550 | } | ||
| 551 | if (@context > 0) { | ||
| 552 | #move node into Q_CLASS | ||
| 553 | #move one level up in the tree and get parent node | ||
| 554 | my $pathType = pop @context; | ||
| 555 | my $pathNode = $self->_treeNode(\@context); | ||
| 556 | my $node = $pathNode->{$pathType->[0]}[$pathType->[1]]; | ||
| 557 | delete $pathNode->{$pathType->[0]}[$pathType->[1]]; | ||
| 558 | #move back one level down | ||
| 559 | push @context, $pathType; | ||
| 560 | my $class = $self->_treeNodeAppend(\@context, Q_CLASS); | ||
| 561 | %$class = %$node; | ||
| 562 | pop @context; #Q_CLASS | ||
| 563 | $self->_treeNodeAppend(\@context, Q_ELEMENTS) | ||
| 564 | if $pathType->[0] eq Q_PATH; | ||
| 565 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 566 | } else { | ||
| 567 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 568 | } | ||
| 569 | } elsif ($token->{type} eq TOK_MATCH) { | ||
| 570 | until (@context == 0 || @context[@context - 1]->[0] eq Q_SOURCEPATH) { | ||
| 571 | pop @context | ||
| 572 | } | ||
| 573 | if (@context > 0) { | ||
| 574 | my $node = $self->_treeNode(\@context); | ||
| 575 | $node->{+Q_HASTARGET}->[0] = 1; | ||
| 576 | $self->_treeNodeAppend(\@context, Q_TARGET); | ||
| 577 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 578 | } else { | ||
| 579 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 580 | } | ||
| 581 | } elsif ($token->{type} eq TOK_LPAREN) { | ||
| 582 | if (@context[@context - 1]->[0] eq Q_CONDITION) { | ||
| 583 | # look ahead to specify context | ||
| 584 | my $pos = $i; | ||
| 585 | my $depth = 1; | ||
| 586 | my %significant; | ||
| 587 | while (1) { | ||
| 588 | $pos++; | ||
| 589 | my $tok = $tokens->[$pos]; | ||
| 590 | if ($tok->{type} eq TOK_END) { | ||
| 591 | _errSyntax ($tokens, $i, \@context, "Unclosed parenthesis"); | ||
| 592 | last; | ||
| 593 | } | ||
| 594 | if ($depth == 1 && | ||
| 595 | $tok->{type} eq TOK_COMMA || | ||
| 596 | $tok->{type} eq TOK_AND || | ||
| 597 | $tok->{type} eq TOK_OR || | ||
| 598 | $tok->{type} eq TOK_LITERAL || | ||
| 599 | $tok->{type} eq TOK_PIPE || | ||
| 600 | $tok->{type} eq TOK_PERIOD | ||
| 601 | ) { | ||
| 602 | $significant{$tok->{type}} = 1; | ||
| 603 | } | ||
| 604 | $depth++ if $tok->{type} eq TOK_LPAREN; | ||
| 605 | $depth-- if $tok->{type} eq TOK_RPAREN; | ||
| 606 | last unless $depth; | ||
| 607 | } | ||
| 608 | #look one more token ahead | ||
| 609 | if ($tokens->[$pos+1]{type} eq TOK_CLASS) { | ||
| 610 | $significant{+TOK_CLASS} = 1; | ||
| 611 | } | ||
| 612 | if (exists $significant{+TOK_COMMA} || | ||
| 613 | exists $significant{+TOK_CLASS}) { | ||
| 614 | #elements | ||
| 615 | push @rndParens, Q_ELEMENTS; | ||
| 616 | $self->_treeNodeAppend(\@context,Q_MATCH); | ||
| 617 | $self->_treeNodeAppend(\@context,Q_PATH); | ||
| 618 | $self->_treeNodeAppend(\@context,Q_ELEMENTS); | ||
| 619 | $self->_treeNodeAppend(\@context,Q_ELEMENT); | ||
| 620 | } elsif (exists $significant{+TOK_AND} || | ||
| 621 | exists $significant{+TOK_OR}) { | ||
| 622 | #condition | ||
| 623 | push @rndParens, Q_CONDITION; | ||
| 624 | $self->_treeNodeAppend(\@context, Q_CONDITION); | ||
| 625 | unless ($tokens->[$i+1]->{type} eq TOK_LPAREN) { | ||
| 626 | $self->_treeNodeAppend(\@context,Q_MATCH); | ||
| 627 | $self->_treeNodeAppend(\@context,Q_PATH); | ||
| 628 | $self->_treeNodeAppend(\@context,Q_ELEMENTS); | ||
| 629 | $self->_treeNodeAppend(\@context,Q_ELEMENT); | ||
| 630 | } | ||
| 631 | } elsif ( #exists $significant{+TOK_LITERAL} || | ||
| 632 | exists $significant{+TOK_PIPE}) { | ||
| 633 | #expression | ||
| 634 | push @rndParens, Q_EXPRESSION; | ||
| 635 | $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 636 | } else { | ||
| 637 | #condition again | ||
| 638 | push @rndParens, Q_CONDITION; | ||
| 639 | $self->_treeNodeAppend(\@context, Q_CONDITION); | ||
| 640 | unless ($tokens->[$i+1]->{type} eq TOK_LPAREN) { | ||
| 641 | $self->_treeNodeAppend(\@context,Q_MATCH); | ||
| 642 | $self->_treeNodeAppend(\@context,Q_PATH); | ||
| 643 | $self->_treeNodeAppend(\@context,Q_ELEMENTS); | ||
| 644 | $self->_treeNodeAppend(\@context,Q_ELEMENT); | ||
| 645 | } | ||
| 646 | } | ||
| 647 | |||
| 648 | |||
| 649 | ############################################################ | ||
| 650 | |||
| 651 | } elsif (@context[@context - 1]->[0] eq Q_EXPRESSION) { | ||
| 652 | push @rndParens, Q_EXPRESSION; | ||
| 653 | $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 654 | } elsif (@context[@context - 1]->[0] eq Q_FUNCTION) { | ||
| 655 | push @rndParens, Q_FUNCTION; | ||
| 656 | $self->_treeNodeAppend(\@context, Q_ELEMENTPATH); | ||
| 657 | $self->_treeNodeAppend(\@context,Q_ELEMENT); | ||
| 658 | } elsif (@context[@context - 1]->[0] eq Q_ELEMENT) { | ||
| 659 | if (@context[@context - 2]->[0] eq Q_ELEMENTS) { | ||
| 660 | push @rndParens, Q_ELEMENTS; | ||
| 661 | } else { | ||
| 662 | # we expected element but found expression | ||
| 663 | until ( @context == 0 || | ||
| 664 | @context[@context - 1]->[0] eq Q_TARGET || | ||
| 665 | @context[@context - 1]->[0] eq Q_ELEMENTPATH || | ||
| 666 | @context[@context - 1]->[0] eq Q_PATH || | ||
| 667 | @context[@context - 1]->[0] eq Q_EXPRESSION) { | ||
| 668 | pop @context; | ||
| 669 | } | ||
| 670 | if (@context > 0) { | ||
| 671 | my $node = $self->_treeNode(\@context); | ||
| 672 | undef %$node; | ||
| 673 | } else { | ||
| 674 | _errSyntax ($tokens, $i, \@context, | ||
| 675 | "Unexpected token"); | ||
| 676 | } | ||
| 677 | $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 678 | $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 679 | push @rndParens, Q_EXPRESSION; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | } elsif ($token->{type} eq TOK_RPAREN) { | ||
| 683 | #Q_CONDITION, Q_ELEMENTS, Q_FUNCTION, Q_EXPRESSION | ||
| 684 | my $lastIn = pop @rndParens; | ||
| 685 | my $item = pop @context; | ||
| 686 | until ( @context == 0 || | ||
| 687 | $item->[0] eq $lastIn) { | ||
| 688 | $item = pop @context; | ||
| 689 | } | ||
| 690 | if (@context > 0) { | ||
| 691 | pop @context #element | ||
| 692 | if $item->[0] eq Q_FUNCTION; | ||
| 693 | } else { | ||
| 694 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 695 | } | ||
| 696 | |||
| 697 | } elsif ($token->{type} eq TOK_LCUR) { | ||
| 698 | if (@context[@context - 1]->[0] eq Q_SOURCEPATH) { | ||
| 699 | #variable binding | ||
| 700 | my $node = $self->_treeNode(\@context); | ||
| 701 | my $index = @{$node->{+Q_ELEMENT}} -1; | ||
| 702 | push @context, [Q_BINDING, $index]; | ||
| 703 | # } elsif (@context[@context - 2]->[0] eq Q_SOURCEPATH && | ||
| 704 | # @context[@context - 1]->[0] eq Q_CLASS) { | ||
| 705 | # #variable binding in Class expression | ||
| 706 | # my $node = $self->_treeNodeAppend(\@context, Q_BINDING); | ||
| 707 | } else { | ||
| 708 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 709 | } | ||
| 710 | |||
| 711 | } elsif ($token->{type} eq TOK_RCUR) { | ||
| 712 | until (@context == 0 || | ||
| 713 | @context[@context - 1]->[0] eq Q_BINDING | ||
| 714 | ) { | ||
| 715 | pop @context; | ||
| 716 | } | ||
| 717 | ; | ||
| 718 | pop @context; #Q_BINDING | ||
| 719 | |||
| 720 | } elsif ($token->{type} eq TOK_PERIOD) { | ||
| 721 | if (@context[@context - 1]->[0] eq Q_ELEMENTPATH || | ||
| 722 | @context[@context - 1]->[0] eq Q_SOURCEPATH) { | ||
| 723 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 724 | } elsif (@context[@context - 1]->[0] eq Q_ELEMENTS || | ||
| 725 | @context[@context - 1]->[0] eq Q_PATH) { | ||
| 726 | #Q_ELEMENTS is to be removed (or anything up to Q_PATH) | ||
| 727 | until (@context == 0 || @context[@context-1]->[0] eq Q_PATH) { | ||
| 728 | pop @context; | ||
| 729 | } | ||
| 730 | |||
| 731 | $self->_treeNodeAppend(\@context, Q_ELEMENTS); | ||
| 732 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 733 | # } elsif (@context[@context - 1]->[0] eq Q_CLASS) { | ||
| 734 | # #Q_CLASS is to be removed and then decide whether Q_ELEMENTS | ||
| 735 | # #should be added | ||
| 736 | # pop @context; #Q_CLASS | ||
| 737 | # $self->_treeNodeAppend(\@context, Q_ELEMENTS) | ||
| 738 | # if @context[@context - 1]->[0] eq Q_PATH; | ||
| 739 | # $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 740 | } else { | ||
| 741 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 742 | } | ||
| 743 | } elsif ($token->{type} eq TOK_COMMA) { | ||
| 744 | if (@context[@context - 1]->[0] eq Q_EXPRESSION) { | ||
| 745 | pop @context; #Q_EXPRESSION | ||
| 746 | } | ||
| 747 | if (@context[@context - 1]->[0] eq Q_TARGET) { | ||
| 748 | pop @context; #Q_TARGET | ||
| 749 | } | ||
| 750 | # if (@context[@context - 1]->[0] eq Q_CLASS) { | ||
| 751 | # #finish Q_CLASS and continue with some PATH | ||
| 752 | # pop @context; #Q_CLASS | ||
| 753 | # } | ||
| 754 | if (@context[@context - 1]->[0] eq Q_ELEMENTPATH || | ||
| 755 | @context[@context - 1]->[0] eq Q_SOURCEPATH) { | ||
| 756 | my $type = @context[@context - 1]->[0]; | ||
| 757 | pop @context; #$type | ||
| 758 | $self->_treeNodeAppend(\@context, $type); | ||
| 759 | $self->_treeNodeAppend(\@context,Q_ELEMENT); | ||
| 760 | } elsif (@context[@context - 1]->[0] eq Q_ELEMENTS) { | ||
| 761 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 762 | } elsif (@context[@context - 1]->[0] eq Q_NAMESPACE) { | ||
| 763 | } elsif (@context[@context - 1]->[0] eq Q_BINDING) { | ||
| 764 | } else { | ||
| 765 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 766 | } | ||
| 767 | } elsif ($token->{type} eq TOK_PIPE) { | ||
| 768 | if (@context[@context - 1]->[0] eq Q_EXPRESSION) { | ||
| 769 | my $node = $self->_treeNode(\@context); | ||
| 770 | $node->{+Q_OPERATION} = [] unless exists $node->{+Q_OPERATION}; | ||
| 771 | my $index = @{$node->{+Q_OPERATION}}; | ||
| 772 | $node->{+Q_OPERATION}->[$index] = $token->{value}; | ||
| 773 | $self->_treeNodeAppend(\@context, Q_EXPRESSION); | ||
| 774 | } else { | ||
| 775 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 776 | } | ||
| 777 | } elsif ($token->{type} eq TOK_COLON) { | ||
| 778 | unless (@context[@context - 1]->[0] eq Q_NODE) { | ||
| 779 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 780 | } | ||
| 781 | } elsif ($token->{type} eq TOK_EQ || | ||
| 782 | $token->{type} eq TOK_NEQ|| | ||
| 783 | $token->{type} eq TOK_LE || | ||
| 784 | $token->{type} eq TOK_LT || | ||
| 785 | $token->{type} eq TOK_GE || | ||
| 786 | $token->{type} eq TOK_GT) { | ||
| 787 | until (@context == 0 || @context[@context - 1]->[0] eq Q_MATCH) { | ||
| 788 | pop @context | ||
| 789 | } | ||
| 790 | if (@context > 0) { | ||
| 791 | my $node = $self->_treeNode(\@context); | ||
| 792 | $node->{+Q_RELATION}->[0] = $token->{value}; | ||
| 793 | $self->_treeNodeAppend(\@context, Q_PATH); | ||
| 794 | $self->_treeNodeAppend(\@context, Q_ELEMENTS); | ||
| 795 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 796 | } else { | ||
| 797 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 798 | } | ||
| 799 | |||
| 800 | } elsif ($token->{type} eq TOK_SELECT) { | ||
| 801 | if (@context[@context - 1]->[0] eq Q_QUERY) { | ||
| 802 | $self->_treeNodeAppend(\@context, Q_RESULTSET); | ||
| 803 | $self->_treeNodeAppend(\@context, Q_ELEMENTPATH); | ||
| 804 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 805 | } else { | ||
| 806 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 807 | } | ||
| 808 | } elsif ($token->{type} eq TOK_FROM) { | ||
| 809 | until (@context == 0 || @context[@context - 1]->[0] eq Q_QUERY) { | ||
| 810 | pop @context | ||
| 811 | } | ||
| 812 | if (@context > 0) { | ||
| 813 | $self->_treeNodeAppend(\@context, Q_SOURCE); | ||
| 814 | $self->_treeNodeAppend(\@context, Q_SOURCEPATH); | ||
| 815 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 816 | } else { | ||
| 817 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 818 | } | ||
| 819 | } elsif ($token->{type} eq TOK_WHERE) { | ||
| 820 | until (@context == 0 || @context[@context - 1]->[0] eq Q_QUERY) { | ||
| 821 | pop @context | ||
| 822 | } | ||
| 823 | if (@context > 0) { | ||
| 824 | $self->_treeNodeAppend(\@context,Q_CONDITION); | ||
| 825 | $self->_treeNodeAppend(\@context,Q_CONDITION); | ||
| 826 | unless ($tokens->[$i+1]->{type} eq TOK_LPAREN) { | ||
| 827 | $self->_treeNodeAppend(\@context,Q_MATCH); | ||
| 828 | $self->_treeNodeAppend(\@context,Q_PATH); | ||
| 829 | $self->_treeNodeAppend(\@context, Q_ELEMENTS); | ||
| 830 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 831 | } | ||
| 832 | } else { | ||
| 833 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 834 | } | ||
| 835 | } elsif ($token->{type} eq TOK_USE) { | ||
| 836 | until (@context == 0 || @context[@context - 1]->[0] eq Q_QUERY) { | ||
| 837 | pop @context | ||
| 838 | } | ||
| 839 | if (@context > 0) { | ||
| 840 | $self->_treeNodeAppend(\@context, Q_NAMESPACE); | ||
| 841 | } else { | ||
| 842 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 843 | } | ||
| 844 | } elsif ($token->{type} eq TOK_FOR) { | ||
| 845 | } elsif ($token->{type} eq TOK_AND || | ||
| 846 | $token->{type} eq TOK_OR) { | ||
| 847 | |||
| 848 | until (@context == 0 || | ||
| 849 | @context[@context - 1]->[0] eq Q_CONDITION | ||
| 850 | #|| @context[@context - 1]->[0] eq Q_MATCH | ||
| 851 | ) { | ||
| 852 | pop @context; | ||
| 853 | } | ||
| 854 | if (@context > 0) { | ||
| 855 | pop @context; #Q_CONDITION | ||
| 856 | my $node = $self->_treeNode(\@context); | ||
| 857 | $node->{+Q_CONNECTION} = [] unless exists $node->{+Q_CONNECTION}; | ||
| 858 | my $index = @{$node->{+Q_CONNECTION}}; | ||
| 859 | $node->{+Q_CONNECTION}->[$index] = $token->{value}; | ||
| 860 | $self->_treeNodeAppend(\@context, Q_CONDITION); | ||
| 861 | unless ($tokens->[$i+1]->{type} eq TOK_LPAREN) { | ||
| 862 | $self->_treeNodeAppend(\@context,Q_MATCH); | ||
| 863 | $self->_treeNodeAppend(\@context,Q_PATH); | ||
| 864 | $self->_treeNodeAppend(\@context, Q_ELEMENTS); | ||
| 865 | $self->_treeNodeAppend(\@context, Q_ELEMENT); | ||
| 866 | } | ||
| 867 | } else { | ||
| 868 | _errSyntax ($tokens, $i, \@context, "Unexpected token"); | ||
| 869 | } | ||
| 870 | } | ||
| 871 | } | ||
| 872 | } | ||
| 873 | |||
| 874 | sub _treeNode { | ||
| 875 | my ($self, $context) = @_; | ||
| 876 | #@$context example: ([Q_QUERY, 0], | ||
| 877 | # [Q_RESULTSET, 0], | ||
| 878 | # [Q_ELEMENTPATH, 2]) | ||
| 879 | |||
| 880 | my $node = $self; | ||
| 881 | foreach (@$context) { | ||
| 882 | if (exists $node->{$_->[0]}->[$_->[1]]) { | ||
| 883 | $node = $node->{$_->[0]}->[$_->[1]]; | ||
| 884 | } else { | ||
| 885 | $node = $node->{$_->[0]}->[$_->[1]] = {}; | ||
| 886 | } | ||
| 887 | |||
| 888 | } | ||
| 889 | return $node; | ||
| 890 | } | ||
| 891 | |||
| 892 | sub _treeNodeAppend { | ||
| 893 | my ($self, $context, $name) = @_; | ||
| 894 | |||
| 895 | my $node = $self->_treeNode($context); | ||
| 896 | $node->{$name} = [] unless defined $node->{$name}; | ||
| 897 | my $lastIndex = @{$node->{$name}} - 1; | ||
| 898 | push @$context, [$name, ++$lastIndex]; | ||
| 899 | return $self->_treeNode($context); | ||
| 900 | } | ||
| 901 | sub _syntaxTree { | ||
| 902 | #dump parsed query | ||
| 903 | my ($self, $node, $depth, $indent) = @_; | ||
| 904 | if ($depth > 0) { | ||
| 905 | if (ref $node) { | ||
| 906 | foreach (keys %$node) { | ||
| 907 | print "\n",$indent,$_; | ||
| 908 | for (my $i = 0; $i < @{$node->{$_}};$i++) { | ||
| 909 | # print $i; | ||
| 910 | print "\n--$i--" if $i; | ||
| 911 | $self->_syntaxTree($node->{$_}->[$i], $depth-1, $indent.' '); | ||
| 912 | } | ||
| 913 | } | ||
| 914 | } else { | ||
| 915 | print "\n",$indent,$node; | ||
| 916 | } | ||
| 917 | } else { | ||
| 918 | if (ref $node) { | ||
| 919 | foreach (keys %$node) { | ||
| 920 | for (my $i = 0; $i < @{$node->{$_}};$i++) { | ||
| 921 | $self->_syntaxTree($node->{$_}->[$i], $depth-1, $indent.' '); | ||
| 922 | } | ||
| 923 | } | ||
| 924 | } else { | ||
| 925 | print " $node"; | ||
| 926 | } | ||
| 927 | } | ||
| 928 | } | ||
| 929 | ############################################################ | ||
| 930 | # Utils | ||
| 931 | |||
| 932 | sub _errSyntax { | ||
| 933 | my ($tokens, $i, $context, @message) = @_; | ||
| 934 | croak 'Syntax error near ',join (' ', $tokens->[$i-2]->{value}, | ||
| 935 | $tokens->[$i-1]->{value}, | ||
| 936 | "<$tokens->[$i]->{value}>", | ||
| 937 | $tokens->[$i+1]->{value}, | ||
| 938 | $tokens->[$i+2]->{value}), "\n", @message; | ||
| 939 | } | ||
| 940 | |||
| 941 | |||
| 942 | 1; | ||
| 943 | __END__ |