XPath/Parser.pm

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