TPath/Expression.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« TPath/Selector/Test/Anywhere.pm ↑ Summary TPath/Predicate/Attribute.pm »
1 package TPath::Expression;
2 $TPath::Expression::VERSION = '1.007';
3 # ABSTRACT: a compiled TPath expression
4  
5  
6 1 ▶use TPath::TypeCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use TPath::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use TPath::Context;
9 use Scalar::Util qw(refaddr);
10 use Moose;
11  
12 use overload '""' => \&to_string;
13  
14  
15 with qw(TPath::Test TPath::Numifiable);
16  
17  
18 has f => ( is => 'ro', does => 'TPath::Forester', required => 1 );
19  
20 has _selectors =>
21 ( is => 'ro', isa => 'ArrayRef[ArrayRef[TPath::Selector]]', required => 1 );
22  
23 has string =>
24 ( is => 'ro', isa => 'Str', lazy => 1, builder => '_stringify_exp' );
25  
26 has needs_uniq => (
27 is => 'ro',
28 isa => 'Bool',
29 lazy => 1,
30 default => sub {
31 my $self = shift;
32 return 1 if @{ $self->_selectors } > 1;
33 return 1 if @{ $self->_selectors->[0] } > 1;
34 return 0;
35 },
36 );
37  
38  
39 has 'vars' => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
40  
41  
42 sub select {
43 my ( $self, $n, $i, %opts ) = @_;
44 confess 'select called on a null node' unless defined $n;
45 $n = $self->f->wrap( $n, %opts );
46 $self->f->_typecheck($n);
47 $i //= $self->f->index($n);
48 $i->index;
49 my $ctx = TPath::Context->new( n => $n, i => $i );
50 my $sel = $self->_select( $ctx, 1 );
51 return wantarray ? map { $_->n } @$sel : @$sel ? $sel->[0]->n : undef;
52 }
53  
54 # select minus the initialization steps
55 sub _select {
56 my ( $self, $ctx, $first ) = @_;
57  
58 my @sel;
59 for my $fork ( @{ $self->_selectors } ) {
60 push @sel, @{ _sel( $ctx, $fork, 0, $first ) };
61 }
62 @sel = uniq(@sel) if $self->needs_uniq;
63  
64 return \@sel;
65 }
66  
67  
68 sub to_num {
69 my ( $self, $ctx ) = @_;
70 my $nodes = $self->_select( $ctx, 1 );
71 return scalar @$nodes;
72 }
73  
74 # required by TPath::Test
75 sub test {
76 my ( $self, $ctx ) = @_;
77 !!$self->select( $ctx->n, $ctx->i );
78 }
79  
80 # goes down steps of path
81 sub _sel {
82 my ( $ctx, $fork, $idx, $first ) = @_;
83 my $selector = $fork->[ $idx++ ];
84 my @c = uniq( $selector->select( $ctx, $first ) );
85 return \@c if $idx == @$fork;
86  
87 my @sel;
88 push @sel, @{ _sel( $_, $fork, $idx, 0 ) } for @c;
89 return \@sel;
90 }
91  
92 # a substitute for List::MoreUtils::uniq which uses reference address rather than stringification to establish identity
93 # the list filtered is assumed to be of TPath::Context objects
94 sub uniq {
95 return @_ if @_ < 2;
96 my %seen = ();
97 grep { not $seen{ refaddr $_->n }++ } @_;
98 }
99  
100 sub to_string { $_[0]->string }
101  
102 sub _stringify_exp {
103 my $self = shift;
104 my $s = '';
105 for my $selectors ( @{ $self->_selectors } ) {
106 $s .= '|' if $s;
107 my $non_first = 0;
108 for my $sel (@$selectors) {
109 $s .= $sel->to_string( !$non_first++ );
110 }
111 }
112 return $s;
113 }
114  
115  
116 sub case_insensitive { shift->f->case_insensitive }
117  
118 no Moose;
119 __PACKAGE__->meta->make_immutable;
120  
121 1;
122  
123 __END__
« TPath/Selector/Test/Anywhere.pm ↑ Summary TPath/Predicate/Attribute.pm »