YAX/Query.pm

filter: Statementstmt 88.5% 131 / 148 Branchbran 65.9% 58 / 88 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 92.8% 13 / 14 Podpod 75.0% 9 / 12 totaltotal 78.3% 221 / 282 CC 56 SCAR 34.6
CC
56
Cov
79%
compile
62.0
descendants
14.2
select
6.0
CRAP 31.7
« YAX/Parser.pm ↑ Summary YAX/Node.pm »
1 package YAX::Query;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use YAX::Constants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $rx_iden = "[a-zA-Z0-9-\\:_]+|\\*";
8 3 our $rx_item = "\\[(?:(?:-?\\d+)|(?:\\d+\\s*\\.\\.\\s*-?\\d+))\\]";
9 3 our $rx_func = "\\b(?:parent|document|id)\\b\\(\\)";
10 3 our $rx_type = "#(?:text|processing-instruction|comment|cdata|node)";
11 3 our $rx_filt = "\\(.+?\\)(?:$rx_item)?(?=(?:\\.|\$))";
12 3 our $rx_attr = "@(?:$rx_iden)(?:$rx_item)?";
13 3 our $rx_elmt = "(?:$rx_iden)(?:$rx_item)?";
14 3 our $rx_term = "(?:$rx_type)(?:$rx_item)?|(?:$rx_func)(?:$rx_item)?";
15 3 our $rx_frag = "(?:$rx_attr)|(?:$rx_term)|(?:$rx_elmt)|(?:$rx_filt)";
16 3 our $rx_chld = "\\.(?:$rx_frag)";
17 3 our $rx_desc = "\\.\\.(?:$rx_frag)";
18 3 our $rx_expr = "$rx_desc|$rx_chld";
19  
20 3 our $RX_TEST = "^(?:$rx_expr)+\$";
21 3 our $RX_EXEC = $rx_expr;
22 3 our $RX_ITEM = '\[(-?\d+)\]$';
23 3 our $RX_SLCE = '\[(-?\d+)\s*\.\.\s*(-?\d+)\]$';
24  
25 3 our %CACHE;
26  
27 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28 14 my ( $class, $node ) = @_;
29 14 my $self = bless [ $node ], $class;
30 14 $self;
31 }
32  
33 14 ▶sub tokenize {
Subroutine tokenize: called CC=1 SCAR=0.0
Pod undocumented
34 14 my ( $self, $expr ) = @_;
35 14 $expr =~ /$RX_EXEC/g;
36 }
37  
38 14 ▶sub compile {
Subroutine compile: called CC=31 SCAR=41.3
Pod undocumented
39 14 my ( $self, $expr ) = @_;
40 14 ▶ $expr = ".$expr" unless substr( $expr, 0, 1 ) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($expr, 0, 1) eq "." 0 14 14
41 14 ▶ die "failed to parse `$expr'" unless $expr =~ /$RX_TEST/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr =~ /$YAX::Query::RX_TEST/g 0 14 14
42  
43 0 ▶ return @{ $CACHE{ $expr } } if exists $CACHE{ $expr };
Branch: 1 branch
BranchTrueFalseTotal
if exists $YAX::Query::CACHE{$expr} 0 14 14
44  
45 14 my @exec;
46 14 my @tokens = $self->tokenize( $expr );
47  
48 14 my ( $index, $start, $end, $seen_flat );
49 14 foreach my $token ( @tokens ) {
50 28 $token = substr( $token, 1 );
51  
52 28 undef( $index );
53 28 undef( $start );
54 28 undef( $end );
55  
56 28 ▶ if ( $token =~ /$RX_ITEM/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /$YAX::Query::RX_ITEM/) { } 6 22 28
elsif ($token =~ /$YAX::Query::RX_SLCE/) { } 1 21 22
57 6 $token =~ s/$RX_ITEM//;
58 6 $index = $1;
59 } elsif ( $token =~ /$RX_SLCE/ ) {
60 1 $token =~ s/$RX_SLCE//;
61 1 $start = $1;
62 1 $end = $2;
63 }
64  
65 28 ▶ if ( substr( $token, 0, 1 ) eq '.' ) {
Branch: 11 branches
BranchTrueFalseTotal
if (substr($token, 0, 1) eq '.') { } 14 14 28
elsif ($token eq '#node') { } 1 13 14
elsif ($token eq '*') { } 0 13 13
elsif ($token eq '#text') { } 1 12 13
elsif ($token eq '#cdata') { } 0 12 12
elsif ($token eq '#processing-instruction') { } 0 12 12
elsif ($token eq '#comment') { } 0 12 12
elsif ($token eq '@*') { } 1 11 12
elsif (substr($token, 0, 1) eq '@') { } 0 11 11
elsif (substr($token, 0, 1) eq '(') { } 3 8 11
elsif ($token eq 'parent()') { } 1 7 8
66 14 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 14 14
67 14 push @exec, [ 'descendants', substr( $token, 1 ) ];
68 }
69 elsif ( $token eq '#node' ) {
70 1 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 1 1
71 1 push @exec, [ 'children' ];
72 }
73 elsif ( $token eq '*' ) {
74 0 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 0 0
75 0 push @exec, [ 'children', ELEMENT_NODE ];
76 }
77 elsif ( $token eq '#text' ) {
78 1 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 1 1
79 1 push @exec, [ 'children', TEXT_NODE ];
80 1 $seen_flat = $token;
81 }
82 elsif ( $token eq '#cdata' ) {
83 0 $seen_flat = $token;
84 0 push @exec, [ 'children', CDATA_SECTION_NODE ];
85 }
86 elsif ( $token eq '#processing-instruction' ) {
87 0 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 0 0
88 0 push @exec, [ 'children', PROCESSING_INSTRUCTION_NODE ];
89 }
90 elsif ( $token eq '#comment' ) {
91 0 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 0 0
92 0 push @exec, [ 'children', COMMENT_NODE ];
93 }
94 elsif ( $token eq '@*' ) {
95 1 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 1 1
96 1 push @exec, [ 'attributes' ];
97 1 $seen_flat = $token;
98 }
99 elsif ( substr( $token, 0, 1 ) eq '@' ) {
100 0 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 0 0
101 0 push @exec, [ 'attribute', substr( $token, 1 ) ];
102 0 $seen_flat = $token;
103 }
104 elsif ( substr( $token, 0, 1 ) eq '(' ) {
105 3 push @exec, [ 'filter', substr( $token, 1, -1 ) ];
106 }
107 elsif ( $token eq 'parent()' ) {
108 1 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 1 1
109 1 push @exec, [ 'parent' ];
110 }
111 else {
112 7 ▶ $seen_flat && die "cannot select `$token' following `$seen_flat'";
Branch: 1 branch
BranchTrueFalseTotal
$seen_flat and die "cannot select `${token}' following `${seen_flat}'" 0 7 7
113 7 push @exec, [ 'child', $token ]
114 }
115  
116 28 ▶ if ( defined $index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $index) { } 6 22 28
elsif (defined $start and defined $end) { } 1 42 43
117 6 push @exec, [ 'item', 0+$index ];
118 }
119 ▶ elsif ( defined $start and defined $end ) {
Condition66%2/3
defined $start and defined $end
!ll&&!rl&&r
2101
MC/DC50%1/2
defined $start and defined $end
defined $start defined $end
Truth table66%2/3
A and B
A= defined $startB= defined $end
ABresult
0X0
100
111
120 1 push @exec, [ 'slice', 0+$start, 0+$end ];
121 }
122 }
123  
124 14 $CACHE{ $expr } = [ @exec ];
125 14 return @exec;
126 }
127  
128 14 ▶sub select {
Subroutine select: called CC=6 SCAR=17.9
Pod documented
129 14 my ( $self, $expr ) = @_;
130 14 my @exec = $self->compile( $expr );
131 14 my ( $meth, @list );
132 14 foreach my $exec ( @exec ) {
133 35 $meth = shift @$exec;
134 35 ▶ if ( $meth eq 'item' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($meth eq 'item') { } 6 29 35
elsif ($meth eq 'slice') { } 1 28 29
elsif ($meth eq 'filter') { } 3 25 28
135 6 @$self = ( $self->[ $exec->[0] ] );
136 }
137 elsif ( $meth eq 'slice' ) {
138 1 @$self = @$self[ $exec->[0] .. $exec->[1] ];
139 }
140 elsif ( $meth eq 'filter' ) {
141 3 $self->filter( $exec->[0] );
142 }
143 else {
144 25 @list = @$self;
145 25 @$self = ( );
146 25 foreach my $node ( @list ) {
147 37 $self->$meth( $node, @$exec );
148 }
149 }
150 }
151 14 $self;
152 }
153  
154 3 ▶sub parent {
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
155 3 my ( $self, $node ) = @_;
156 3 push @$self, $node->parent;
157 3 $self;
158 }
159  
160 4 ▶sub children {
Subroutine children: called CC=5 SCAR=16.1
Pod documented
161 4 my ( $self, $node, $type ) = @_;
162 4 ▶ if ( UNIVERSAL::can( $node, 'children' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($node, "children")) 4 0 4
163 4 foreach my $child ( @{ $node->children } ) {
164 14 ▶ next if defined $type and ( $child->type != $type );
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $child->type != $type 1 13 14
Condition100%3/3
defined $type and $child->type != $type
!ll&&!rl&&r
1121
MC/DC100%2/2
defined $type and $child->type != $type
defined $type $child->type != $type
Truth table100%3/3
A and B
A= defined $typeB= $child->type != $type
ABresult
0X0
100
111
165 13 push @$self, $child;
166 }
167 }
168 4 $self;
169 }
170  
171 15 ▶sub child {
Subroutine child: called CC=4 SCAR=13.9
Pod documented
172 15 my ( $self, $node, $name ) = @_;
173 15 ▶ if ( UNIVERSAL::can( $node, 'children' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($node, "children")) 15 0 15
174 15 foreach my $child ( @{ $node->children } ) {
175 48 ▶ next unless $child->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $child->name eq $name 32 16 48
176 16 push @$self, $child;
177 }
178 }
179 15 $self;
180 }
181  
182 1 ▶sub attributes {
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
183 1 my ( $self, $node ) = @_;
184 1 push @$self, $node->attributes;
185 1 $self;
186 }
187  
188 0 ▶sub attribute {
Subroutine attribute: not called CC=1 SCAR=6.9
Pod documented
189 0 my ( $self, $node, $name ) = @_;
190 0 push @$self, $node->attributes->{ $name };
191 0 $self;
192 }
193  
194 14 ▶sub descendants {
Subroutine descendants: called CC=12 SCAR=26.6
Pod documented
195 14 my ( $self, $node, $name ) = @_;
196 14 ▶ $name = '*' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 14 14
197 14 ▶ if ( UNIVERSAL::can( $node, 'children' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($node, "children")) 14 0 14
198 14 my @stack;
199 14 my $count = 0;
200 14 foreach my $child ( reverse @{ $node->children } ) {
201 30 $stack[ $count++ ] = $child;
202 }
203 14 ▶ while ( --$count >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$count >= 0) 624 14 638
204 624 my $n = $stack[ $count ];
205 624 ▶ if ( $name eq '*' and $n->type == ELEMENT_NODE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq '*' and $n->type == 1) { } 0 624 624
elsif ($name eq '#processing-instruction' and $n->type == 7) { } 0 624 624
elsif ($n->name eq $name) { } 53 571 624
Condition33%1/3
$name eq "*" and $n->type == 1
!ll&&!rl&&r
62400
MC/DC0%0/2
$name eq "*" and $n->type == 1
$name eq "*" $n->type == 1
Truth table33%1/3
A and B
A= $name eq "*"B= $n->type == 1
ABresult
0X0
100
111
206 0 push @$self, $n;
207 }
208 ▶ elsif ( $name eq '#processing-instruction' and
Condition33%1/3
$name eq "#processing-instruction" and $n->type == 7
!ll&&!rl&&r
62400
MC/DC0%0/2
$name eq "#processing-instruction" and $n->type == 7
$name eq "#processing-instruction" $n->type == 7
Truth table33%1/3
A and B
A= $name eq "#processing-instruction"B= $n->type == 7
ABresult
0X0
100
111
209 ( $n->type == PROCESSING_INSTRUCTION_NODE ) ) {
210 0 push @$self, $n;
211 }
212 elsif ( $n->name eq $name ) {
213 53 push @$self, $n;
214 }
215 624 ▶ if ( UNIVERSAL::can( $n, 'children' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($n, "children")) 214 410 624
216 214 foreach my $child ( reverse @{ $n->children } ) {
217 594 $stack[ $count++ ] = $child;
218 }
219 }
220 }
221 14 undef( @stack );
222 }
223 14 $self;
224 }
225  
226 3 ▶sub filter {
Subroutine filter: called CC=3 SCAR=11.1
Pod documented
227 3 my ( $self, $test ) = @_;
228 3 ▶ unless ( ref $test eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $test eq "CODE") 3 0 3
229 3 my $orig = $test;
230 3 $test =~ s/@([a-zA-Z\-:._]+)\b/\$_->{$1}/g;
231 3 $orig = $test;
232 3 $test = mk_code( $test );
233 3 ▶ die "$@ while compiling filter: `$orig'" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
234 }
235 3 @$self = grep { &$test } @$self;
236 3 $self;
237 }
238  
239 3 ▶sub mk_code { eval 'sub { '.$_[-1].' }' }
Subroutine mk_code: called CC=1 SCAR=0.0
Pod undocumented
240  
241 3 1;
242 __END__
« YAX/Parser.pm ↑ Summary YAX/Node.pm »