TPath/Forester.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« TPath/Concatenation.pm ↑ Summary TPath/Selector/Quantified.pm »
1 package TPath::Forester;
2 $TPath::Forester::VERSION = '1.007';
3 # ABSTRACT: a generator of TPath expressions for a particular class of nodes
4  
5  
6 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶use TPath::Compiler qw(compile);
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use TPath::Grammar qw(parse);
13 use TPath::Index;
14 use TPath::StderrLog;
15 use TPath::Test::Node::True;
16  
17  
18 with qw(TPath::Attributes::Standard TPath::TypeCheck);
19  
20  
21 requires qw(children tag);
22  
23  
24 has log_stream => (
25 is => 'rw',
26 isa => 'TPath::LogStream',
27 default => sub { TPath::StderrLog->new }
28 );
29  
30  
31 has one_based => ( is => 'ro', isa => 'Bool', default => 0 );
32  
33  
34 has case_insensitive => ( is => 'ro', isa => 'Bool', default => 0 );
35  
36 has _cr => (
37 is => 'rw',
38 isa => 'CodeRef',
39 );
40  
41 has _cf => (
42 is => 'rw',
43 isa => 'CodeRef',
44 );
45  
46 sub _build_cf {
47 my $self = shift;
48 return sub { $_[0] eq $_[1] }
49 unless $self->case_insensitive;
50 return eval 'sub { use feature "fc"; fc($_[0]) eq fc($_[1]) }' if $] > 5.016;
51 my $sub = eval
52 'require Unicode::CaseFold; sub { Unicode::CaseFold::fc($_[0]) eq Unicode::CaseFold::fc($_[1])}';
53 return $sub unless $@;
54 return sub { lc( $_[0] ) eq lc( $_[1] ) };
55 }
56  
57  
58 has _tests => (
59 is => 'ro',
60 isa => 'ArrayRef[CodeRef]',
61 default => sub { [] },
62 traits => ['Array'],
63 handles => {
64 add_test => 'push',
65 has_tests => 'count',
66 clear_tests => 'clear',
67 },
68 auto_deref => 1
69 );
70  
71 # map from attribute names to their implementations
72 has _attributes => (
73 is => 'ro',
74 isa => 'HashRef[CodeRef]',
75 lazy => 1,
76 builder => '_collect_attributes'
77 );
78  
79 sub _collect_attributes {
80 my $self = shift;
81 my $class = ref $self;
82 my %attributes = ();
83 for my $method ( $class->meta->get_all_methods ) {
84 next unless $method->can('attributes');
85 my @annotations = grep /^Attr\b/, @{ $method->attributes };
86 if (@annotations) {
87 my $annotation = shift @annotations;
88 if ( $annotation eq 'Attr' ) {
89 $attributes{ $method->name } = $method->body;
90 }
91 else {
92 if ( $annotation =~ /^Attr\((.*)\)$/ ) {
93 $attributes{$1} = $method->body;
94 }
95 else {
96 confess "malformed annotation $annotation on method "
97 . $method->name;
98 }
99 }
100 }
101 }
102 return \%attributes;
103 }
104  
105  
106 sub add_attribute {
107 my ( $self, $name, $attr, %options ) = @_;
108 die 'attributes must have non-empty names' unless $name;
109 die "attribute $name but be a code reference" unless ref $attr eq 'CODE';
110 if ( $options{-force} ) {
111 $self->_attributes->{$name} = $attr;
112 }
113 elsif ( $options{-override} ) {
114 carp("override used by attribute $name does not exist")
115 unless exists $self->_attributes->{$name};
116 $self->_attributes->{$name} = $attr;
117 }
118 else {
119 croak("attribute $name already exists")
120 if exists $self->_attributes->{$name};
121 $self->_attributes->{$name} = $attr;
122 }
123 }
124  
125  
126 sub attribute {
127 my ( $self, $ctx, $aname, @params ) = @_;
128 $self->_attributes->{$aname}->( $self, $ctx, @params );
129 }
130  
131  
132 sub path {
133 my ( $self, $expr ) = @_;
134 my $ast = parse($expr);
135 return compile( $ast, $self );
136 }
137  
138  
139 sub index {
140 my ( $self, $node ) = @_;
141 TPath::Index->new(
142 f => $self,
143 root => $node,
144 node_type => $self->node_type
145 );
146 }
147  
148  
149 sub parent {
150 my ( $self, $original, $ctx ) = @_;
151 my $p = $ctx->i->parent( $ctx->n );
152 return undef unless $p;
153 return $original->bud($p);
154 }
155  
156  
157 sub id { }
158  
159  
160 sub autoload_attribute { }
161  
162 sub _kids {
163 my ( $self, $original, $ctx ) = @_;
164 return [ map { $original->bud($_) }
165 @{ $self->_decontextualized_kids( $ctx->n, $ctx->i ) } ];
166 }
167  
168 sub _decontextualized_descendants {
169 my ( $self, $n, $i ) = @_;
170 my $ra = refaddr $n;
171 my $cached = $i->d_index->{$ra};
172 return $cached if $cached;
173 my @descendants;
174 for my $kid ( @{ $self->_decontextualized_kids( $n, $i ) } ) {
175 push @descendants, $kid;
176 push @descendants,
177 @{ $self->_decontextualized_descendants( $kid, $i ) };
178 }
179 return $i->d_index->{$ra} = \@descendants;
180 }
181  
182 sub _decontextualized_kids {
183 my ( $self, $n, $i ) = @_;
184 my $ra = refaddr $n;
185 my $cached = $i->k_index->{$ra};
186 return $cached if $cached;
187 my @children = $self->children( $n, $i );
188 return $i->k_index->{$ra} = \@children unless $self->has_tests;
189 return $i->k_index->{$ra} = [
190 grep {
191 my $good = 1;
192 for my $t ( $self->_tests ) {
193 $good &= $t->( $self, $_, $i );
194 last unless $good;
195 }
196 $good;
197 } @children
198 ];
199 }
200  
201 sub axis_ancestor {
202 my ( $self, $ctx, $t ) = @_;
203 @{ $self->_ancestors( $ctx, $ctx, $t ) };
204 }
205  
206 sub axis_adjacent {
207 my ( $self, $ctx, $t ) = @_;
208 @{ $self->_adjacent( $ctx, $ctx, $t ) };
209 }
210  
211 sub axis_ancestor_or_self {
212 my ( $self, $ctx, $t ) = @_;
213 my $nodes = $self->_ancestors( $ctx, $ctx, $t );
214 push @$nodes, $ctx if $t->passes($ctx);
215 return @$nodes;
216 }
217  
218 sub axis_child {
219 my ( $self, $ctx, $t ) = @_;
220 @{ $self->_children( $ctx, $ctx, $t ) };
221 }
222  
223 sub axis_descendant {
224 my ( $self, $ctx, $t ) = @_;
225 @{ $self->_descendants( $ctx, $ctx, $t ) };
226 }
227  
228 sub axis_descendant_or_self {
229 my ( $self, $ctx, $t ) = @_;
230 my @descendants = @{ $self->_descendants( $ctx, $ctx, $t ) };
231 unshift @descendants, $ctx if $t->passes($ctx);
232 return @descendants;
233 }
234  
235 sub axis_following {
236 my ( $self, $ctx, $t ) = @_;
237 @{ $self->_following( $ctx, $ctx, $t ) };
238 }
239  
240 sub axis_following_sibling {
241 my ( $self, $ctx, $t ) = @_;
242 @{ $self->_following_siblings( $ctx, $ctx, $t ) };
243 }
244  
245 sub axis_leaf {
246 my ( $self, $ctx, $t ) = @_;
247 @{ $self->_leaves( $ctx, $ctx, $t ) };
248 }
249  
250 sub axis_parent {
251 my ( $self, $ctx, $t ) = @_;
252 my $parent = $self->parent( $ctx, $ctx );
253 return () unless $parent;
254 return $t->passes($parent) ? $parent : ();
255 }
256  
257 sub axis_preceding {
258 my ( $self, $ctx, $t ) = @_;
259 @{ $self->_preceding( $ctx, $ctx, $t ) };
260 }
261  
262 sub axis_previous {
263 my ( $self, $ctx, $t ) = @_;
264 my @previous;
265 while ( my $previous = $ctx->previous ) {
266 push @previous, $previous if $t->passes($previous);
267 $ctx = $previous;
268 }
269 return @previous;
270 }
271  
272 sub axis_preceding_sibling {
273 my ( $self, $ctx, $t ) = @_;
274 @{ $self->_preceding_siblings( $ctx, $ctx, $t ) };
275 }
276  
277 sub axis_self {
278 my ( $self, $ctx, $t ) = @_;
279 return $t->passes($ctx) ? $ctx->bud( $ctx->n ) : ();
280 }
281  
282 sub axis_sibling {
283 my ( $self, $ctx, $t ) = @_;
284 @{ $self->_siblings( $ctx, $ctx, $t ) };
285 }
286  
287 sub axis_sibling_or_self {
288 my ( $self, $ctx, $t ) = @_;
289 @{ $self->_siblings_or_self( $ctx, $ctx, $t ) };
290 }
291  
292 sub closest {
293 my ( $self, $ctx, $t, $first ) = @_;
294 return $ctx if !$first && $t->passes($ctx);
295 my $children = $self->_kids( $ctx, $ctx );
296 my @closest;
297 for my $c (@$children) {
298 push @closest, $self->closest( $c, $t, 0 );
299 }
300 return @closest;
301 }
302  
303 sub _siblings_or_self {
304 my ( $self, $original, $ctx, $t ) = @_;
305 [ grep { $t->passes($_) }
306 @{ $self->_kids( $original, $self->parent( $original, $ctx ) ) } ];
307 }
308  
309 sub _siblings {
310 my ( $self, $original, $ctx, $t ) = @_;
311 my $siblings = $self->_untested_siblings( $original, $ctx );
312 [ grep { $t->passes($_) } @$siblings ];
313 }
314  
315 sub _untested_siblings {
316 my ( $self, $original, $ctx ) = @_;
317 return [] if $self->is_root($ctx);
318 my $ra = refaddr $ctx->n;
319 [ grep { refaddr $_->n ne $ra }
320 @{ $self->_kids( $original, $self->parent( $original, $ctx ) ) } ];
321 }
322  
323 sub _preceding {
324 my ( $self, $original, $ctx, $t ) = @_;
325 return [] if $self->is_root($ctx);
326 my @preceding;
327 state $tt = TPath::Test::Node::True->new;
328 my $ancestors = $self->_ancestors( $original, $ctx, $tt );
329 for my $a ( @$ancestors[ 1 .. $#$ancestors ] ) {
330 for my $p ( @{ $self->_preceding_siblings( $original, $a, $tt ) } ) {
331 push @preceding, $p if $t->passes($p);
332 push @preceding, @{ $self->_descendants( $original, $p, $t ) };
333 }
334 }
335 for my $p ( @{ $self->_preceding_siblings( $original, $ctx, $tt ) } ) {
336 push @preceding, $p if $t->passes($p);
337 push @preceding, @{ $self->_descendants( $original, $p, $t ) };
338 }
339 return \@preceding;
340 }
341  
342 sub _preceding_siblings {
343 my ( $self, $original, $ctx, $t ) = @_;
344 return [] if $self->is_root($ctx);
345 my $siblings = $self->_kids( $original, $self->parent( $original, $ctx ) );
346 return [] if @$siblings == 1;
347 my @preceding_siblings;
348 my $ra = refaddr $ctx->n;
349 for my $s (@$siblings) {
350 last if refaddr $s->n == $ra;
351 push @preceding_siblings, $s if $t->passes($s);
352 }
353 return \@preceding_siblings;
354 }
355  
356 sub _leaves {
357 my ( $self, $original, $ctx, $t ) = @_;
358 my $children = $self->_kids( $original, $ctx );
359 return $t->passes($ctx) ? [$ctx] : [] unless @$children;
360 my @leaves;
361 push @leaves, @{ $self->_leaves( $original, $_, $t ) } for @$children;
362 return \@leaves;
363 }
364  
365 sub _following {
366 my ( $self, $original, $ctx, $t ) = @_;
367 return [] if $self->is_root($ctx);
368 my @following;
369 state $tt = TPath::Test::Node::True->new;
370 my $ancestors = $self->_ancestors( $original, $ctx, $tt );
371 for my $a ( @$ancestors[ 1 .. $#$ancestors ] ) {
372 for my $p ( @{ $self->_following_siblings( $original, $a, $tt ) } ) {
373 push @following, $p if $t->passes($p);
374 push @following, @{ $self->_descendants( $original, $p, $t ) };
375 }
376 }
377 for my $p ( @{ $self->_following_siblings( $original, $ctx, $tt ) } ) {
378 push @following, $p if $t->passes($p);
379 push @following, @{ $self->_descendants( $original, $p, $t ) };
380 }
381 return \@following;
382 }
383  
384 sub _following_siblings {
385 my ( $self, $original, $ctx, $t ) = @_;
386 return [] if $self->is_root($ctx);
387 my $siblings = $self->_kids( $original, $self->parent( $original, $ctx ) );
388 return [] if @$siblings == 1;
389 my ( @following_siblings, $add );
390 my $ra = refaddr $ctx->n;
391 for my $s (@$siblings) {
392 if ($add) {
393 push @following_siblings, $s if $t->passes($s);
394 }
395 else {
396 $add = $ra == refaddr $s->n;
397 }
398 }
399 return \@following_siblings;
400 }
401  
402 sub _descendants {
403 my ( $self, $original, $ctx, $t ) = @_;
404 my @descendants;
405 for my $d ( @{ $self->_decontextualized_descendants( $ctx->n, $ctx->i ) } )
406 {
407 my $c = $original->bud($d);
408 push @descendants, $c if $t->passes($c);
409 }
410 return \@descendants;
411 }
412  
413  
414 sub is_leaf {
415 my ( $self, $ctx ) = @_;
416 my $children = $self->_kids( $ctx, $ctx );
417 return !@$children;
418 }
419  
420  
421 sub is_root {
422 my ( $self, $ctx ) = @_;
423 $ctx->i->is_root( $ctx->n );
424 }
425  
426 sub _ancestors {
427 my ( $self, $original, $ctx, $t ) = @_;
428 my @nodes;
429 while ( !$self->is_root($ctx) ) {
430 my $parent = $self->parent( $original, $ctx );
431 unshift @nodes, $parent if $t->passes($parent);
432 $ctx = $parent;
433 }
434 return \@nodes;
435 }
436  
437 sub _children {
438 my ( $self, $original, $ctx, $t ) = @_;
439 my $children = $self->_kids( $original, $ctx );
440 return [] unless @$children;
441 [ grep { $t->passes($_) ? $_ : () } @$children ];
442 }
443  
444 sub _adjacent {
445 my ( $self, $original, $ctx, $t ) = @_;
446 return [] if $self->is_root($ctx);
447 my $siblings = $self->_kids( $original, $self->parent( $original, $ctx ) );
448 my $lim = $#$siblings;
449 return [] if $lim == 1;
450 my $idx;
451 my $ra = refaddr $original->n;
452 for my $i ( 0 .. $lim ) {
453 $idx = $i;
454 last if refaddr $siblings->[$idx]->n == $ra;
455 }
456 my @adjacent;
457 if ($idx) {
458 for ( my $i = $idx - 1 ; $i >= 0 ; $i-- ) {
459 my $c = $siblings->[$i];
460 if ( $t->passes($c) ) {
461 push @adjacent, $c;
462 last;
463 }
464 }
465 }
466 if ( $idx < $lim ) {
467 for my $i ( $idx + 1 .. $lim ) {
468 my $c = $siblings->[$i];
469 if ( $t->passes($c) ) {
470 push @adjacent, $c;
471 last;
472 }
473 }
474 }
475 return \@adjacent;
476 }
477  
478  
479 sub has_tag {
480  
481 (
482 $_[0]->_cr // do {
483 my $tag = $_[0]->can('tag');
484 my $cf = $_[0]->_build_cf;
485 $_[0]->_cr(
486 sub {
487 my $t = $tag->( $_[0], $_[1] );
488 return undef unless defined $t;
489 return $cf->( $t, $_[2] );
490 }
491 );
492 }
493 )->(@_);
494  
495 # optimized from
496 # my ( $self, $n, $tag ) = @_;
497 # my $t = $_[0]->tag( $_[1] );
498 # return undef unless defined $t;
499 # $_[0]->_cf->( $t, $_[2] );
500 }
501  
502  
503 sub matches_tag {
504  
505 # my ( $self, $n, $rx ) = @_;
506 my $t = $_[0]->tag( $_[1] );
507 return undef unless defined $t;
508 $t =~ $_[2];
509 }
510  
511  
512 sub wrap { return $_[1] }
513  
514 1;
515  
516 __END__
« TPath/Concatenation.pm ↑ Summary TPath/Selector/Quantified.pm »