| 1 | package XML::XPath::Parser; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak); | ||
| 6 | use XML::XPath::XMLParser; | ||
| 7 | use XML::XPath::Step; | ||
| 8 | use XML::XPath::Expr; | ||
| 9 | use XML::XPath::Function; | ||
| 10 | use XML::XPath::LocationPath; | ||
| 11 | use XML::XPath::Variable; | ||
| 12 | use XML::XPath::Literal; | ||
| 13 | use XML::XPath::Number; | ||
| 14 | use XML::XPath::NodeSet; | ||
| 15 | |||
| 16 | our $VERSION = '1.49'; | ||
| 17 | |||
| 18 | # Axis name to principal node type mapping | ||
| 19 | my %AXES = ( | ||
| 20 | 'ancestor' => 'element', | ||
| 21 | 'ancestor-or-self' => 'element', | ||
| 22 | 'attribute' => 'attribute', | ||
| 23 | 'namespace' => 'namespace', | ||
| 24 | 'child' => 'element', | ||
| 25 | 'descendant' => 'element', | ||
| 26 | 'descendant-or-self' => 'element', | ||
| 27 | 'following' => 'element', | ||
| 28 | 'following-sibling' => 'element', | ||
| 29 | 'parent' => 'element', | ||
| 30 | 'preceding' => 'element', | ||
| 31 | 'preceding-sibling' => 'element', | ||
| 32 | 'self' => 'element', | ||
| 33 | ); | ||
| 34 | |||
| 35 | my $NameStartCharClassBody = "a-zA-Z_\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}"; | ||
| 36 | my $NameCharClassBody = "${NameStartCharClassBody}\\-.0-9\\xB7\\x{300}-\\x{36F}\\x{203F}-\\x{2040}"; | ||
| 37 | my $Name = "(?:[$NameStartCharClassBody][$NameCharClassBody]*)"; | ||
| 38 | |||
| 39 | my $NCName = $Name; | ||
| 40 | my $QName = "$NCName(?::$NCName)?"; | ||
| 41 | my $NCWild = "${NCName}:\\*"; | ||
| 42 | my $QNWild = "\\*"; | ||
| 43 | my $NODE_TYPE = '((text|comment|processing-instruction|node)\\(\\))'; | ||
| 44 | my $AXIS_NAME = '(' . join('|', keys %AXES) . ')::'; | ||
| 45 | my $NUMBER_RE = '\d+(\\.\d*)?|\\.\d+'; | ||
| 46 | my $LITERAL = '\\"[^\\"]*\\"|\\\'[^\\\']*\\\''; | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $class = shift; | ||
| 50 | my $self = bless {}, $class; | ||
| 51 | debug("New Parser being created.\n"); | ||
| 52 | $self->{context_set} = XML::XPath::NodeSet->new(); | ||
| 53 | $self->{context_pos} = undef; # 1 based position in array context | ||
| 54 | $self->{context_size} = 0; # total size of context | ||
| 55 | $self->clear_namespaces(); | ||
| 56 | $self->{vars} = {}; | ||
| 57 | $self->{direction} = 'forward'; | ||
| 58 | $self->{cache} = {}; | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub cleanup { | ||
| 63 | my $self = shift; | ||
| 64 | $self->{cache} = {}; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub get_var { | ||
| 68 | my $self = shift; | ||
| 69 | my $var = shift; | ||
| 70 | $self->{vars}->{$var}; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub set_var { | ||
| 74 | my $self = shift; | ||
| 75 | my $var = shift; | ||
| 76 | my $val = shift; | ||
| 77 | $self->{vars}->{$var} = $val; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub set_namespace { | ||
| 81 | my $self = shift; | ||
| 82 | my ($prefix, $expanded) = @_; | ||
| 83 | $self->{namespaces}{$prefix} = $expanded; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub clear_namespaces { | ||
| 87 | my $self = shift; | ||
| 88 | $self->{namespaces} = {}; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub get_namespace { | ||
| 92 | my $self = shift; | ||
| 93 | my ($prefix, $node) = @_; | ||
| 94 | if (my $ns = $self->{namespaces}{$prefix}) { | ||
| 95 | return $ns; | ||
| 96 | } | ||
| 97 | if (my $nsnode = $node->getNamespace($prefix)) { | ||
| 98 | return $nsnode->getValue(); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub get_context_set { $_[0]->{context_set}; } | ||
| 103 | sub set_context_set { $_[0]->{context_set} = $_[1]; } | ||
| 104 | sub get_context_pos { $_[0]->{context_pos}; } | ||
| 105 | sub set_context_pos { $_[0]->{context_pos} = $_[1]; } | ||
| 106 | sub get_context_size { $_[0]->{context_set}->size; } | ||
| 107 | sub get_context_node { $_[0]->{context_set}->get_node($_[0]->{context_pos}); } | ||
| 108 | |||
| 109 | sub my_sub { | ||
| 110 | return (caller(1))[3]; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub parse { | ||
| 114 | my $self = shift; | ||
| 115 | my $path = shift; | ||
| 116 | |||
| 117 | if ($self->{cache}->{$path}) { | ||
| 118 | return $self->{cache}->{$path}; | ||
| 119 | } | ||
| 120 | |||
| 121 | my $tokens = $self->tokenize($path); | ||
| 122 | |||
| 123 | $self->{_tokpos} = 0; | ||
| 124 | my $tree = $self->analyze($tokens); | ||
| 125 | |||
| 126 | if ($self->{_tokpos} < scalar(@$tokens)) { | ||
| 127 | # didn't manage to parse entire expression - throw an exception | ||
| 128 | die "Parse of expression $path failed - junk after end of expression: $tokens->[$self->{_tokpos}]"; | ||
| 129 | } | ||
| 130 | |||
| 131 | $self->{cache}->{$path} = $tree; | ||
| 132 | |||
| 133 | debug("PARSED Expr to:\n", $tree->as_string, "\n") if $XML::XPath::Debug; | ||
| 134 | |||
| 135 | return $tree; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub tokenize { | ||
| 139 | my $self = shift; | ||
| 140 | my $path = shift; | ||
| 141 | study $path; | ||
| 142 | |||
| 143 | my @tokens; | ||
| 144 | |||
| 145 | debug("Parsing: $path\n"); | ||
| 146 | |||
| 147 | # Bug: We don't allow "'@' NodeType" which is in the grammar, but I think is just plain stupid. | ||
| 148 | |||
| 149 | while($path =~ m/\G | ||
| 150 | \s* # ignore all whitespace | ||
| 151 | ( # tokens | ||
| 152 | $LITERAL| | ||
| 153 | $NUMBER_RE| # Match digits | ||
| 154 | \.\.| # match parent | ||
| 155 | \.| # match current | ||
| 156 | ($AXIS_NAME)?$NODE_TYPE| # match tests | ||
| 157 | processing-instruction| | ||
| 158 | \@($NCWild|$QName|$QNWild)| # match attrib | ||
| 159 | \$$QName| # match variable reference | ||
| 160 | ($AXIS_NAME)?($NCWild|$QName|$QNWild)| # match NCName,NodeType,Axis::Test | ||
| 161 | \!=|<=|\-|>=|\/\/|and|or|mod|div| # multi-char seps | ||
| 162 | [,\+=\|<>\/\(\[\]\)]| # single char seps | ||
| 163 | (?<!(\@|\(|\[))\*| # multiply operator rules (see xpath spec) | ||
| 164 | (?<!::)\*| | ||
| 165 | $ # match end of query | ||
| 166 | ) | ||
| 167 | \s* # ignore all whitespace | ||
| 168 | /gcxso) { | ||
| 169 | |||
| 170 | my ($token) = ($1); | ||
| 171 | |||
| 172 | if (length($token)) { | ||
| 173 | debug("TOKEN: $token\n"); | ||
| 174 | push @tokens, $token; | ||
| 175 | } | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | my $path_pos = pos($path); | ||
| 180 | my $path_length = length($path); | ||
| 181 | if (defined $path_pos | ||
| 182 | && defined $path_length | ||
| 183 | && ($path_pos < $path_length)) { | ||
| 184 | my $marker = ("." x ($path_pos-1)); | ||
| 185 | $path = substr($path, 0, $path_pos + 8) . "..."; | ||
| 186 | $path =~ s/\n/ /g; | ||
| 187 | $path =~ s/\t/ /g; | ||
| 188 | die "Query:\n", | ||
| 189 | "$path\n", | ||
| 190 | $marker, "^^^\n", | ||
| 191 | "Invalid query somewhere around here (I think)\n"; | ||
| 192 | } | ||
| 193 | |||
| 194 | return \@tokens; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub analyze { | ||
| 198 | my $self = shift; | ||
| 199 | my $tokens = shift; | ||
| 200 | # lexical analysis | ||
| 201 | |||
| 202 | return Expr($self, $tokens); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub match { | ||
| 206 | my ($self, $tokens, $match, $fatal) = @_; | ||
| 207 | |||
| 208 | $self->{_curr_match} = ''; | ||
| 209 | return 0 unless $self->{_tokpos} < @$tokens; | ||
| 210 | |||
| 211 | local $^W; | ||
| 212 | |||
| 213 | # debug ("match: $match\n"); | ||
| 214 | |||
| 215 | if ($tokens->[$self->{_tokpos}] =~ /^$match$/) { | ||
| 216 | $self->{_curr_match} = $tokens->[$self->{_tokpos}]; | ||
| 217 | $self->{_tokpos}++; | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | if ($fatal) { | ||
| 222 | die "Invalid token: ", $tokens->[$self->{_tokpos}], "\n"; | ||
| 223 | } | ||
| 224 | else { | ||
| 225 | return 0; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | sub Expr { | ||
| 231 | my ($self, $tokens) = @_; | ||
| 232 | |||
| 233 | debug("in SUB\n"); | ||
| 234 | |||
| 235 | return OrExpr($self, $tokens); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub OrExpr { | ||
| 239 | my ($self, $tokens) = @_; | ||
| 240 | |||
| 241 | debug("in SUB\n"); | ||
| 242 | |||
| 243 | my $expr = AndExpr($self, $tokens); | ||
| 244 | while (match($self, $tokens, 'or')) { | ||
| 245 | my $or_expr = XML::XPath::Expr->new($self); | ||
| 246 | $or_expr->set_lhs($expr); | ||
| 247 | $or_expr->set_op('or'); | ||
| 248 | |||
| 249 | my $rhs = AndExpr($self, $tokens); | ||
| 250 | |||
| 251 | $or_expr->set_rhs($rhs); | ||
| 252 | $expr = $or_expr; | ||
| 253 | } | ||
| 254 | |||
| 255 | return $expr; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub AndExpr { | ||
| 259 | my ($self, $tokens) = @_; | ||
| 260 | |||
| 261 | debug("in SUB\n"); | ||
| 262 | |||
| 263 | my $expr = EqualityExpr($self, $tokens); | ||
| 264 | while (match($self, $tokens, 'and')) { | ||
| 265 | my $and_expr = XML::XPath::Expr->new($self); | ||
| 266 | $and_expr->set_lhs($expr); | ||
| 267 | $and_expr->set_op('and'); | ||
| 268 | |||
| 269 | my $rhs = EqualityExpr($self, $tokens); | ||
| 270 | |||
| 271 | $and_expr->set_rhs($rhs); | ||
| 272 | $expr = $and_expr; | ||
| 273 | } | ||
| 274 | |||
| 275 | return $expr; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub EqualityExpr { | ||
| 279 | my ($self, $tokens) = @_; | ||
| 280 | |||
| 281 | debug("in SUB\n"); | ||
| 282 | |||
| 283 | my $expr = RelationalExpr($self, $tokens); | ||
| 284 | while (match($self, $tokens, '!?=')) { | ||
| 285 | my $eq_expr = XML::XPath::Expr->new($self); | ||
| 286 | $eq_expr->set_lhs($expr); | ||
| 287 | $eq_expr->set_op($self->{_curr_match}); | ||
| 288 | |||
| 289 | my $rhs = RelationalExpr($self, $tokens); | ||
| 290 | |||
| 291 | $eq_expr->set_rhs($rhs); | ||
| 292 | $expr = $eq_expr; | ||
| 293 | } | ||
| 294 | |||
| 295 | return $expr; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub RelationalExpr { | ||
| 299 | my ($self, $tokens) = @_; | ||
| 300 | |||
| 301 | debug("in SUB\n"); | ||
| 302 | |||
| 303 | my $expr = AdditiveExpr($self, $tokens); | ||
| 304 | while (match($self, $tokens, '(<|>|<=|>=)')) { | ||
| 305 | my $rel_expr = XML::XPath::Expr->new($self); | ||
| 306 | $rel_expr->set_lhs($expr); | ||
| 307 | $rel_expr->set_op($self->{_curr_match}); | ||
| 308 | |||
| 309 | my $rhs = AdditiveExpr($self, $tokens); | ||
| 310 | |||
| 311 | $rel_expr->set_rhs($rhs); | ||
| 312 | $expr = $rel_expr; | ||
| 313 | } | ||
| 314 | |||
| 315 | return $expr; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub AdditiveExpr { | ||
| 319 | my ($self, $tokens) = @_; | ||
| 320 | |||
| 321 | debug("in SUB\n"); | ||
| 322 | |||
| 323 | my $expr = MultiplicativeExpr($self, $tokens); | ||
| 324 | while (match($self, $tokens, '[\\+\\-]')) { | ||
| 325 | my $add_expr = XML::XPath::Expr->new($self); | ||
| 326 | $add_expr->set_lhs($expr); | ||
| 327 | $add_expr->set_op($self->{_curr_match}); | ||
| 328 | |||
| 329 | my $rhs = MultiplicativeExpr($self, $tokens); | ||
| 330 | |||
| 331 | $add_expr->set_rhs($rhs); | ||
| 332 | $expr = $add_expr; | ||
| 333 | } | ||
| 334 | |||
| 335 | return $expr; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub MultiplicativeExpr { | ||
| 339 | my ($self, $tokens) = @_; | ||
| 340 | |||
| 341 | debug("in SUB\n"); | ||
| 342 | |||
| 343 | my $expr = UnaryExpr($self, $tokens); | ||
| 344 | while (match($self, $tokens, '(\\*|div|mod)')) { | ||
| 345 | my $mult_expr = XML::XPath::Expr->new($self); | ||
| 346 | $mult_expr->set_lhs($expr); | ||
| 347 | $mult_expr->set_op($self->{_curr_match}); | ||
| 348 | |||
| 349 | my $rhs = UnaryExpr($self, $tokens); | ||
| 350 | |||
| 351 | $mult_expr->set_rhs($rhs); | ||
| 352 | $expr = $mult_expr; | ||
| 353 | } | ||
| 354 | |||
| 355 | return $expr; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub UnaryExpr { | ||
| 359 | my ($self, $tokens) = @_; | ||
| 360 | |||
| 361 | debug("in SUB\n"); | ||
| 362 | |||
| 363 | if (match($self, $tokens, '-')) { | ||
| 364 | my $expr = XML::XPath::Expr->new($self); | ||
| 365 | $expr->set_lhs(XML::XPath::Number->new(0)); | ||
| 366 | $expr->set_op('-'); | ||
| 367 | $expr->set_rhs(UnaryExpr($self, $tokens)); | ||
| 368 | return $expr; | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | return UnionExpr($self, $tokens); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | sub UnionExpr { | ||
| 376 | my ($self, $tokens) = @_; | ||
| 377 | |||
| 378 | debug("in SUB\n"); | ||
| 379 | |||
| 380 | my $expr = PathExpr($self, $tokens); | ||
| 381 | while (match($self, $tokens, '\\|')) { | ||
| 382 | my $un_expr = XML::XPath::Expr->new($self); | ||
| 383 | $un_expr->set_lhs($expr); | ||
| 384 | $un_expr->set_op('|'); | ||
| 385 | |||
| 386 | my $rhs = PathExpr($self, $tokens); | ||
| 387 | |||
| 388 | $un_expr->set_rhs($rhs); | ||
| 389 | $expr = $un_expr; | ||
| 390 | } | ||
| 391 | |||
| 392 | return $expr; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub PathExpr { | ||
| 396 | my ($self, $tokens) = @_; | ||
| 397 | |||
| 398 | debug("in SUB\n"); | ||
| 399 | |||
| 400 | # PathExpr is LocationPath | FilterExpr | FilterExpr '//?' RelativeLocationPath | ||
| 401 | |||
| 402 | # Since we are being predictive we need to find out which function to call next, then. | ||
| 403 | |||
| 404 | # LocationPath either starts with "/", "//", ".", ".." or a proper Step. | ||
| 405 | |||
| 406 | my $expr = XML::XPath::Expr->new($self); | ||
| 407 | |||
| 408 | my $test = $tokens->[$self->{_tokpos}]; | ||
| 409 | |||
| 410 | # Test for AbsoluteLocationPath and AbbreviatedRelativeLocationPath | ||
| 411 | if (defined $test && ($test =~ /^(\/\/?|\.\.?)$/)) { | ||
| 412 | # LocationPath | ||
| 413 | $expr->set_lhs(LocationPath($self, $tokens)); | ||
| 414 | } | ||
| 415 | # Test for AxisName::... | ||
| 416 | elsif (is_step($self, $tokens)) { | ||
| 417 | $expr->set_lhs(LocationPath($self, $tokens)); | ||
| 418 | } | ||
| 419 | else { | ||
| 420 | # Not a LocationPath | ||
| 421 | # Use FilterExpr instead: | ||
| 422 | |||
| 423 | $expr = FilterExpr($self, $tokens); | ||
| 424 | if (match($self, $tokens, '//?')) { | ||
| 425 | my $loc_path = XML::XPath::LocationPath->new(); | ||
| 426 | push @$loc_path, $expr; | ||
| 427 | if ($self->{_curr_match} eq '//') { | ||
| 428 | push @$loc_path, XML::XPath::Step->new($self, 'descendant-or-self', | ||
| 429 | XML::XPath::Step::test_nt_node); | ||
| 430 | } | ||
| 431 | push @$loc_path, RelativeLocationPath($self, $tokens); | ||
| 432 | my $new_expr = XML::XPath::Expr->new($self); | ||
| 433 | $new_expr->set_lhs($loc_path); | ||
| 434 | return $new_expr; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | return $expr; | ||
| 439 | } | ||
| 440 | |||
| 441 | sub FilterExpr { | ||
| 442 | my ($self, $tokens) = @_; | ||
| 443 | |||
| 444 | debug("in SUB\n"); | ||
| 445 | |||
| 446 | my $expr = PrimaryExpr($self, $tokens); | ||
| 447 | while (match($self, $tokens, '\\[')) { | ||
| 448 | # really PredicateExpr... | ||
| 449 | $expr->push_predicate(Expr($self, $tokens)); | ||
| 450 | match($self, $tokens, '\\]', 1); | ||
| 451 | } | ||
| 452 | |||
| 453 | return $expr; | ||
| 454 | } | ||
| 455 | |||
| 456 | sub PrimaryExpr { | ||
| 457 | my ($self, $tokens) = @_; | ||
| 458 | |||
| 459 | debug("in SUB\n"); | ||
| 460 | |||
| 461 | my $expr = XML::XPath::Expr->new($self); | ||
| 462 | |||
| 463 | if (match($self, $tokens, $LITERAL)) { | ||
| 464 | # new Literal with $self->{_curr_match}... | ||
| 465 | $self->{_curr_match} =~ m/^(["'])(.*)\1$/; | ||
| 466 | $expr->set_lhs(XML::XPath::Literal->new($2)); | ||
| 467 | } | ||
| 468 | elsif (match($self, $tokens, $NUMBER_RE)) { | ||
| 469 | # new Number with $self->{_curr_match}... | ||
| 470 | $expr->set_lhs(XML::XPath::Number->new($self->{_curr_match})); | ||
| 471 | } | ||
| 472 | elsif (match($self, $tokens, '\\(')) { | ||
| 473 | $expr->set_lhs(Expr($self, $tokens)); | ||
| 474 | match($self, $tokens, '\\)', 1); | ||
| 475 | } | ||
| 476 | elsif (match($self, $tokens, "\\\$$QName")) { | ||
| 477 | # new Variable with $self->{_curr_match}... | ||
| 478 | $self->{_curr_match} =~ /^\$(.*)$/; | ||
| 479 | $expr->set_lhs(XML::XPath::Variable->new($self, $1)); | ||
| 480 | } | ||
| 481 | elsif (match($self, $tokens, $QName)) { | ||
| 482 | # check match not Node_Type - done in lexer... | ||
| 483 | # new Function | ||
| 484 | my $func_name = $self->{_curr_match}; | ||
| 485 | match($self, $tokens, '\\(', 1); | ||
| 486 | $expr->set_lhs( | ||
| 487 | XML::XPath::Function->new( | ||
| 488 | $self, | ||
| 489 | $func_name, | ||
| 490 | Arguments($self, $tokens) | ||
| 491 | ) | ||
| 492 | ); | ||
| 493 | match($self, $tokens, '\\)', 1); | ||
| 494 | } | ||
| 495 | else { | ||
| 496 | croak("Not a PrimaryExpr at " . ($tokens->[$self->{_tokpos}] ||'')); | ||
| 497 | } | ||
| 498 | |||
| 499 | return $expr; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub Arguments { | ||
| 503 | my ($self, $tokens) = @_; | ||
| 504 | |||
| 505 | debug("in SUB\n"); | ||
| 506 | |||
| 507 | my @args; | ||
| 508 | |||
| 509 | if($tokens->[$self->{_tokpos}] eq ')') { | ||
| 510 | return \@args; | ||
| 511 | } | ||
| 512 | |||
| 513 | push @args, Expr($self, $tokens); | ||
| 514 | while (match($self, $tokens, ',')) { | ||
| 515 | push @args, Expr($self, $tokens); | ||
| 516 | } | ||
| 517 | |||
| 518 | return \@args; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub LocationPath { | ||
| 522 | my ($self, $tokens) = @_; | ||
| 523 | |||
| 524 | debug("in SUB\n"); | ||
| 525 | |||
| 526 | my $loc_path = XML::XPath::LocationPath->new(); | ||
| 527 | |||
| 528 | if (match($self, $tokens, '/')) { | ||
| 529 | # root | ||
| 530 | debug("SUB: Matched root\n"); | ||
| 531 | push @$loc_path, XML::XPath::Root->new(); | ||
| 532 | if (is_step($self, $tokens)) { | ||
| 533 | debug("Next is step\n"); | ||
| 534 | push @$loc_path, RelativeLocationPath($self, $tokens); | ||
| 535 | } | ||
| 536 | } | ||
| 537 | elsif (match($self, $tokens, '//')) { | ||
| 538 | # root | ||
| 539 | push @$loc_path, XML::XPath::Root->new(); | ||
| 540 | my $optimised = optimise_descendant_or_self($self, $tokens); | ||
| 541 | if (!$optimised) { | ||
| 542 | push @$loc_path, XML::XPath::Step->new($self, 'descendant-or-self', | ||
| 543 | XML::XPath::Step::test_nt_node); | ||
| 544 | push @$loc_path, RelativeLocationPath($self, $tokens); | ||
| 545 | } | ||
| 546 | else { | ||
| 547 | push @$loc_path, $optimised, RelativeLocationPath($self, $tokens); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | else { | ||
| 551 | push @$loc_path, RelativeLocationPath($self, $tokens); | ||
| 552 | } | ||
| 553 | |||
| 554 | return $loc_path; | ||
| 555 | } | ||
| 556 | |||
| 557 | sub optimise_descendant_or_self { | ||
| 558 | my ($self, $tokens) = @_; | ||
| 559 | |||
| 560 | debug("in SUB\n"); | ||
| 561 | |||
| 562 | my $tokpos = $self->{_tokpos}; | ||
| 563 | |||
| 564 | # // must be followed by a Step. | ||
| 565 | if ($tokens->[$tokpos+1] && $tokens->[$tokpos+1] eq '[') { | ||
| 566 | # next token is a predicate | ||
| 567 | return; | ||
| 568 | } | ||
| 569 | elsif ($tokens->[$tokpos] =~ /^\.\.?$/) { | ||
| 570 | # abbreviatedStep - can't optimise. | ||
| 571 | return; | ||
| 572 | } | ||
| 573 | else { | ||
| 574 | debug("Trying to optimise //\n"); | ||
| 575 | my $step = Step($self, $tokens); | ||
| 576 | if ($step->{axis} ne 'child') { | ||
| 577 | # can't optimise axes other than child for now... | ||
| 578 | $self->{_tokpos} = $tokpos; | ||
| 579 | return; | ||
| 580 | } | ||
| 581 | $step->{axis} = 'descendant'; | ||
| 582 | $step->{axis_method} = 'axis_descendant'; | ||
| 583 | $self->{_tokpos}--; | ||
| 584 | $tokens->[$self->{_tokpos}] = '.'; | ||
| 585 | return $step; | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | sub RelativeLocationPath { | ||
| 590 | my ($self, $tokens) = @_; | ||
| 591 | |||
| 592 | debug("in SUB\n"); | ||
| 593 | |||
| 594 | my @steps; | ||
| 595 | |||
| 596 | push @steps, Step($self, $tokens); | ||
| 597 | while (match($self, $tokens, '//?')) { | ||
| 598 | if ($self->{_curr_match} eq '//') { | ||
| 599 | my $optimised = optimise_descendant_or_self($self, $tokens); | ||
| 600 | if (!$optimised) { | ||
| 601 | push @steps, XML::XPath::Step->new($self, 'descendant-or-self', | ||
| 602 | XML::XPath::Step::test_nt_node); | ||
| 603 | } | ||
| 604 | else { | ||
| 605 | push @steps, $optimised; | ||
| 606 | } | ||
| 607 | } | ||
| 608 | push @steps, Step($self, $tokens); | ||
| 609 | if ((scalar(@steps) > 1) | ||
| 610 | && | ||
| 611 | (defined $steps[-1]->{axis} && ($steps[-1]->{axis} eq 'self')) | ||
| 612 | && | ||
| 613 | (defined $steps[-1]->{test} && ($steps[-1]->{test} == XML::XPath::Step::test_nt_node))) { | ||
| 614 | pop @steps; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | return @steps; | ||
| 619 | } | ||
| 620 | |||
| 621 | sub Step { | ||
| 622 | my ($self, $tokens) = @_; | ||
| 623 | |||
| 624 | debug("in SUB\n"); | ||
| 625 | |||
| 626 | if (match($self, $tokens, '\\.')) { | ||
| 627 | # self::node() | ||
| 628 | return XML::XPath::Step->new($self, 'self', XML::XPath::Step::test_nt_node); | ||
| 629 | } | ||
| 630 | elsif (match($self, $tokens, '\\.\\.')) { | ||
| 631 | # parent::node() | ||
| 632 | return XML::XPath::Step->new($self, 'parent', XML::XPath::Step::test_nt_node); | ||
| 633 | } | ||
| 634 | else { | ||
| 635 | # AxisSpecifier NodeTest Predicate(s?) | ||
| 636 | my $token = $tokens->[$self->{_tokpos}]; | ||
| 637 | |||
| 638 | debug("SUB: Checking $token\n") if defined $token; | ||
| 639 | |||
| 640 | my $step; | ||
| 641 | if (defined $token) { | ||
| 642 | if ($token eq 'processing-instruction') { | ||
| 643 | $self->{_tokpos}++; | ||
| 644 | match($self, $tokens, '\\(', 1); | ||
| 645 | match($self, $tokens, $LITERAL); | ||
| 646 | $self->{_curr_match} =~ /^["'](.*)["']$/; | ||
| 647 | $step = XML::XPath::Step->new( | ||
| 648 | $self, 'child', | ||
| 649 | XML::XPath::Step::test_nt_pi, | ||
| 650 | XML::XPath::Literal->new($1)); | ||
| 651 | match($self, $tokens, '\\)', 1); | ||
| 652 | } | ||
| 653 | elsif ($token =~ /^\@($NCWild|$QName|$QNWild)$/o) { | ||
| 654 | $self->{_tokpos}++; | ||
| 655 | if ($token eq '@*') { | ||
| 656 | $step = XML::XPath::Step->new( | ||
| 657 | $self, | ||
| 658 | 'attribute', | ||
| 659 | XML::XPath::Step::test_attr_any, | ||
| 660 | '*'); | ||
| 661 | } | ||
| 662 | elsif ($token =~ /^\@($NCName):\*$/o) { | ||
| 663 | $step = XML::XPath::Step->new( | ||
| 664 | $self, | ||
| 665 | 'attribute', | ||
| 666 | XML::XPath::Step::test_attr_ncwild, | ||
| 667 | $1); | ||
| 668 | } | ||
| 669 | elsif ($token =~ /^\@($QName)$/o) { | ||
| 670 | $step = XML::XPath::Step->new( | ||
| 671 | $self, | ||
| 672 | 'attribute', | ||
| 673 | XML::XPath::Step::test_attr_qname, | ||
| 674 | $1); | ||
| 675 | } | ||
| 676 | } | ||
| 677 | elsif ($token =~ /^($NCName):\*$/o) { # ns:* | ||
| 678 | $self->{_tokpos}++; | ||
| 679 | $step = XML::XPath::Step->new( | ||
| 680 | $self, 'child', | ||
| 681 | XML::XPath::Step::test_ncwild, | ||
| 682 | $1); | ||
| 683 | } | ||
| 684 | elsif ($token =~ /^$QNWild$/o) { # * | ||
| 685 | $self->{_tokpos}++; | ||
| 686 | $step = XML::XPath::Step->new( | ||
| 687 | $self, 'child', | ||
| 688 | XML::XPath::Step::test_any, | ||
| 689 | $token); | ||
| 690 | } | ||
| 691 | elsif ($token =~ /^$QName$/o) { # name:name | ||
| 692 | $self->{_tokpos}++; | ||
| 693 | $step = XML::XPath::Step->new( | ||
| 694 | $self, 'child', | ||
| 695 | XML::XPath::Step::test_qname, | ||
| 696 | $token); | ||
| 697 | } | ||
| 698 | elsif ($token eq 'comment()') { | ||
| 699 | $self->{_tokpos}++; | ||
| 700 | $step = XML::XPath::Step->new( | ||
| 701 | $self, 'child', | ||
| 702 | XML::XPath::Step::test_nt_comment); | ||
| 703 | } | ||
| 704 | elsif ($token eq 'text()') { | ||
| 705 | $self->{_tokpos}++; | ||
| 706 | $step = XML::XPath::Step->new( | ||
| 707 | $self, 'child', | ||
| 708 | XML::XPath::Step::test_nt_text); | ||
| 709 | } | ||
| 710 | elsif ($token eq 'node()') { | ||
| 711 | $self->{_tokpos}++; | ||
| 712 | $step = XML::XPath::Step->new( | ||
| 713 | $self, 'child', | ||
| 714 | XML::XPath::Step::test_nt_node); | ||
| 715 | } | ||
| 716 | elsif ($token eq 'processing-instruction()') { | ||
| 717 | $self->{_tokpos}++; | ||
| 718 | $step = XML::XPath::Step->new( | ||
| 719 | $self, 'child', | ||
| 720 | XML::XPath::Step::test_nt_pi); | ||
| 721 | } | ||
| 722 | elsif ($token =~ /^$AXIS_NAME($NCWild|$QName|$QNWild|$NODE_TYPE)$/o) { | ||
| 723 | my $axis = $1; | ||
| 724 | $self->{_tokpos}++; | ||
| 725 | $token = $2; | ||
| 726 | if ($token eq 'processing-instruction') { | ||
| 727 | match($self, $tokens, '\\(', 1); | ||
| 728 | match($self, $tokens, $LITERAL); | ||
| 729 | $self->{_curr_match} =~ /^["'](.*)["']$/; | ||
| 730 | $step = XML::XPath::Step->new( | ||
| 731 | $self, $axis, | ||
| 732 | XML::XPath::Step::test_nt_pi, | ||
| 733 | XML::XPath::Literal->new($1)); | ||
| 734 | match($self, $tokens, '\\)', 1); | ||
| 735 | } | ||
| 736 | elsif ($token =~ /^($NCName):\*$/o) { # ns:* | ||
| 737 | $step = XML::XPath::Step->new( | ||
| 738 | $self, $axis, | ||
| 739 | (($axis eq 'attribute') ? | ||
| 740 | XML::XPath::Step::test_attr_ncwild | ||
| 741 | : | ||
| 742 | XML::XPath::Step::test_ncwild), | ||
| 743 | $1); | ||
| 744 | } | ||
| 745 | elsif ($token =~ /^$QNWild$/o) { # * | ||
| 746 | $step = XML::XPath::Step->new( | ||
| 747 | $self, $axis, | ||
| 748 | (($axis eq 'attribute') ? | ||
| 749 | XML::XPath::Step::test_attr_any | ||
| 750 | : | ||
| 751 | XML::XPath::Step::test_any), | ||
| 752 | $token); | ||
| 753 | } | ||
| 754 | elsif ($token =~ /^$QName$/o) { # name:name | ||
| 755 | $step = XML::XPath::Step->new( | ||
| 756 | $self, $axis, | ||
| 757 | (($axis eq 'attribute') ? | ||
| 758 | XML::XPath::Step::test_attr_qname | ||
| 759 | : | ||
| 760 | XML::XPath::Step::test_qname), | ||
| 761 | $token); | ||
| 762 | } | ||
| 763 | elsif ($token eq 'comment()') { | ||
| 764 | $step = XML::XPath::Step->new( | ||
| 765 | $self, $axis, | ||
| 766 | XML::XPath::Step::test_nt_comment); | ||
| 767 | } | ||
| 768 | elsif ($token eq 'text()') { | ||
| 769 | $step = XML::XPath::Step->new( | ||
| 770 | $self, $axis, | ||
| 771 | XML::XPath::Step::test_nt_text); | ||
| 772 | } | ||
| 773 | elsif ($token eq 'node()') { | ||
| 774 | $step = XML::XPath::Step->new( | ||
| 775 | $self, $axis, | ||
| 776 | XML::XPath::Step::test_nt_node); | ||
| 777 | } | ||
| 778 | elsif ($token eq 'processing-instruction()') { | ||
| 779 | $step = XML::XPath::Step->new( | ||
| 780 | $self, $axis, | ||
| 781 | XML::XPath::Step::test_nt_pi); | ||
| 782 | } | ||
| 783 | else { | ||
| 784 | die "Shouldn't get here"; | ||
| 785 | } | ||
| 786 | } | ||
| 787 | else { | ||
| 788 | die "token $token doesn't match format of a 'Step'\n"; | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | while (match($self, $tokens, '\\[')) { | ||
| 793 | push @{$step->{predicates}}, Expr($self, $tokens); | ||
| 794 | match($self, $tokens, '\\]', 1); | ||
| 795 | } | ||
| 796 | |||
| 797 | return $step; | ||
| 798 | } | ||
| 799 | } | ||
| 800 | |||
| 801 | sub is_step { | ||
| 802 | my ($self, $tokens) = @_; | ||
| 803 | |||
| 804 | my $token = $tokens->[$self->{_tokpos}]; | ||
| 805 | |||
| 806 | return unless defined $token; | ||
| 807 | |||
| 808 | debug("SUB: Checking if '$token' is a step\n"); | ||
| 809 | |||
| 810 | local $^W; | ||
| 811 | |||
| 812 | if ($token eq 'processing-instruction') { | ||
| 813 | return 1; | ||
| 814 | } | ||
| 815 | elsif ($token =~ /^\@($NCWild|$QName|$QNWild)$/o) { | ||
| 816 | return 1; | ||
| 817 | } | ||
| 818 | elsif ($token =~ /^($NCWild|$QName|$QNWild)$/o | ||
| 819 | && (!defined $tokens->[$self->{_tokpos}+1] || ($tokens->[$self->{_tokpos}+1] ne '('))) { | ||
| 820 | return 1; | ||
| 821 | } | ||
| 822 | elsif ($token =~ /^$NODE_TYPE$/o) { | ||
| 823 | return 1; | ||
| 824 | } | ||
| 825 | elsif ($token =~ /^$AXIS_NAME($NCWild|$QName|$QNWild|$NODE_TYPE)$/o) { | ||
| 826 | return 1; | ||
| 827 | } | ||
| 828 | elsif ($token =~ /^\.\.?$/) { | ||
| 829 | return 1; | ||
| 830 | } | ||
| 831 | |||
| 832 | debug("SUB: '$token' not a step\n"); | ||
| 833 | |||
| 834 | return; | ||
| 835 | } | ||
| 836 | |||
| 837 | sub debug { | ||
| 838 | return unless $XML::XPath::Debug; | ||
| 839 | |||
| 840 | my ($pkg, $file, $line, $sub) = caller(1); | ||
| 841 | |||
| 842 | $sub =~ s/^$pkg\:://; | ||
| 843 | |||
| 844 | while (@_) { | ||
| 845 | my $x = shift; | ||
| 846 | $x =~ s/\bPKG\b/$pkg/g; | ||
| 847 | $x =~ s/\bLINE\b/$line/g; | ||
| 848 | $x =~ s/\bSUB\b/$sub/g; | ||
| 849 | print STDERR $x; | ||
| 850 | } | ||
| 851 | } | ||
| 852 | |||
| 853 | 1; |