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