Regexp/Node.pm

filter: Statementstmt 91.6% 220 / 240 Branchbran 68.6% 92 / 134 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 95.5% 43 / 45 Podpod 100% 22 / 22 totaltotal 84.4% 381 / 451 CC 85 SCAR 17.5
CC
85
Cov
83%
__raw_width
12.0
schildren
11.8
find_first
10.1
CRAP 5.7
« Regexp/Token/Reference.pm ↑ Summary Regexp/Token/Control.pm »
1 =head1 NAME
2  
3 PPIx::Regexp::Node - Represent a container
4  
5 =head1 SYNOPSIS
6  
7 use PPIx::Regexp::Dumper;
8 PPIx::Regexp::Dumper->new( 'qr{(foo)}' )->print();
9  
10 =head1 INHERITANCE
11  
12 C<PPIx::Regexp::Node> is a
13 L<PPIx::Regexp::Element|PPIx::Regexp::Element>.
14  
15 C<PPIx::Regexp::Node> is the parent of L<PPIx::Regexp|PPIx::Regexp>,
16 L<PPIx::Regexp::Node::Range|PPIx::Regexp::Node::Range> and
17 L<PPIx::Regexp::Structure|PPIx::Regexp::Structure>.
18  
19 =head1 DESCRIPTION
20  
21 This class represents a structural element that contains other classes.
22 It is an abstract class, not instantiated by the lexer.
23  
24 =head1 METHODS
25  
26 This class provides the following public methods. Methods not documented
27 here are private, and unsupported in the sense that the author reserves
28 the right to change or remove them without notice.
29  
30 =cut
31  
32 package PPIx::Regexp::Node;
33  
34 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 9 ▶use base qw{ PPIx::Regexp::Element };
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 9 ▶use List::Util qw{ max min };
Subroutine BEGIN: called CC=1 SCAR=0.0
41 9 ▶use PPIx::Regexp::Constant qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
42 CODE_REF
43 FALSE
44 INFINITY
45 MINIMUM_PERL
46 NODE_UNKNOWN
47 TRUE
48 @CARP_NOT
49 9 };
50 9 ▶use PPIx::Regexp::Util qw{ __instance __merge_perl_requirements width };
Subroutine BEGIN: called CC=1 SCAR=0.0
51 9 ▶use Scalar::Util qw{ refaddr };
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 9 our $VERSION = '0.092';
54  
55 9 ▶use constant ELEMENT_UNKNOWN => NODE_UNKNOWN;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 976 ▶sub __new {
Subroutine __new: called CC=5 SCAR=16.7
58 976 my ( $class, @children ) = @_;
59 976 foreach my $elem ( @children ) {
60 2588 ▶ __instance( $elem, 'PPIx::Regexp::Element' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
__instance($elem, "PPIx::Regexp::Element") or return 2588 0 2588
61 }
62 976 my $self = {
63 children => \@children,
64 };
65 976 ▶ bless $self, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
09760
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
66 976 foreach my $elem ( @children ) {
67 2588 $elem->_parent( $self );
68 }
69 976 return $self;
70 }
71  
72 =head2 child
73  
74 my $kid = $node->child( 0 );
75  
76 This method returns the child at the given index. The indices start from
77 zero, and negative indices are from the end of the list, so that
78 C<< $node->child( -1 ) >> returns the last child of the node.
79  
80 =cut
81  
82 6159 ▶sub child {
Subroutine child: called CC=2 SCAR=7.1
Pod documented
83 6159 my ( $self, $inx ) = @_;
84 6159 ▶ defined $inx or $inx = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $inx or $inx = 0 6159 0 6159
85 6159 return $self->{children}[$inx];
86 }
87  
88 =head2 children
89  
90 This method returns the children of the Node. If called in scalar
91 context it returns the number of children.
92  
93 =cut
94  
95 2485 ▶sub children {
Subroutine children: called CC=1 SCAR=0.0
Pod documented
96 2485 my ( $self ) = @_;
97 2485 return @{ $self->{children} };
98 }
99  
100 =head2 contains
101  
102 print $node->contains( $elem ) ? "yes\n" : "no\n";
103  
104 This method returns true if the given element is contained in the node,
105 or false otherwise.
106  
107 =cut
108  
109 1 ▶sub contains {
Subroutine contains: called CC=4 SCAR=14.5
Pod documented
110 1 my ( $self, $elem ) = @_;
111 1 ▶ __instance( $elem, 'PPIx::Regexp::Element' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
__instance($elem, "PPIx::Regexp::Element") or return 1 0 1
112  
113 1 my $addr = refaddr( $self );
114  
115 1 ▶ while ( $elem = $elem->parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->parent) 3 0 3
116 3 ▶ $addr == refaddr( $elem ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$addr == builtin::refaddr($elem) and return 1 1 2 3
117 }
118  
119 0 return;
120 }
121  
122 516 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
123 516 my ( $self ) = @_;
124 516 return join( '', map{ $_->content() } $self->elements() );
125 }
126  
127 =head2 elements
128  
129 This method returns the elements in the Node. For a
130 C<PPIx::Regexp::Node> proper, it is the same as C<children()>.
131  
132 =cut
133  
134 {
135 9 ▶ no warnings qw{ once };
Subroutine BEGIN: called CC=1 SCAR=0.0
136 9 *elements = \&children; # sub slements
137 }
138  
139 =head2 find
140  
141 my $rslt = $node->find( 'PPIx::Regexp::Token::Literal' );
142 my $rslt = $node->find( 'Token::Literal' );
143 my $rslt = $node->find( sub {
144 return $_[1]->isa( 'PPIx::Regexp::Token::Literal' )
145 && $_[1]->ordinal < ord(' ');
146 } );
147  
148 This method finds things.
149  
150 If given a string as argument, it is assumed to be a class name
151 (possibly without the leading 'PPIx::Regexp::'), and all elements of the
152 given class are found.
153  
154 If given a code reference, that code reference is called once for each
155 element, and passed C<$self> and the element. The code should return
156 true to accept the element, false to reject it, and ( for subclasses of
157 C<PPIx::Regexp::Node>) C<undef> to prevent recursion into the node. If
158 the code throws an exception, you get nothing back from this method.
159  
160 Either way, the return is a reference to the list of things found, a
161 false (but defined) value if nothing was found, or C<undef> if an error
162 occurred.
163  
164 =cut
165  
166 1347 ▶sub _find_routine {
Subroutine _find_routine: called CC=4 SCAR=13.9
167 1347 my ( $want ) = @_;
168 1347 ▶ CODE_REF eq ref $want
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $want and return $want 656 691 1347
169 and return $want;
170 691 ▶ ref $want and return;
Branch: 1 branch
BranchTrueFalseTotal
ref $want and return 1 690 691
171 690 ▶ $want =~ m/ \A PPIx::Regexp:: /smx
Branch: 1 branch
BranchTrueFalseTotal
$want =~ / \A PPIx::Regexp:: /msx or $want = 'PPIx::Regexp::' . $want 687 3 690
172 or $want = 'PPIx::Regexp::' . $want;
173 6198 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
174 6198 ▶ return __instance( $_[1], $want ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
__instance($_[1], $want) ? : 96 6102 6198
175 690 };
176 }
177  
178 1340 ▶sub find {
Subroutine find: called CC=9 SCAR=22.0
Pod documented
179 1340 my ( $self, $want ) = @_;
180  
181 1340 ▶ $want = _find_routine( $want ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$want = _find_routine($want) or return 1339 1 1340
182  
183 1339 my @found;
184  
185 # We use a recursion to find what we want. PPI::Node uses an
186 # iteration.
187 1339 foreach my $elem ( $self->elements() ) {
188 6197 ▶ my $rslt = eval { $want->( $self, $elem ) }
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = eval { do { &$want($self, $elem) } } and push @found, $elem 94 6103 6197
189 and push @found, $elem;
190 6197 ▶ $@ and return;
Branch: 1 branch
BranchTrueFalseTotal
$@ and return 0 6197 6197
191  
192 6197 ▶ __instance( $elem, 'PPIx::Regexp::Node' ) or next;
Branch: 1 branch
BranchTrueFalseTotal
__instance($elem, "PPIx::Regexp::Node") or next 651 5546 6197
193 651 ▶ defined $rslt or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $rslt or next 651 0 651
194 $rslt = $elem->find( $want )
195 651 ▶ and push @found, @{ $rslt };
Branch: 1 branch
BranchTrueFalseTotal
$rslt = $elem->find($want) and push @found, @{$rslt;} 52 599 651
196 }
197  
198 1339 ▶ return @found ? \@found : 0;
Branch: 1 branch
BranchTrueFalseTotal
@found ? : 126 1213 1339
199  
200 }
201  
202 =head2 find_parents
203  
204 my $rslt = $node->find_parents( sub {
205 return $_[1]->isa( 'PPIx::Regexp::Token::Operator' )
206 && $_[1]->content() eq '|';
207 } );
208  
209 This convenience method takes the same arguments as C<find>, but instead
210 of the found objects themselves returns their parents. No parent will
211 appear more than once in the output.
212  
213 This method returns a reference to the array of parents if any were
214 found. If no parents were found the return is false but defined. If an
215 error occurred the return is C<undef>.
216  
217 =cut
218  
219 1 ▶sub find_parents {
Subroutine find_parents: called CC=5 SCAR=16.2
Pod documented
220 1 my ( $self, $want ) = @_;
221  
222 1 my $found;
223 1 ▶ $found = $self->find( $want ) or return $found;
Branch: 1 branch
BranchTrueFalseTotal
$found = $self->find($want) or return $found 1 0 1
224  
225 1 my %parents;
226 my @rslt;
227 1 foreach my $elem ( @{ $found } ) {
228 2 ▶ my $dad = $elem->parent() or next;
Branch: 1 branch
BranchTrueFalseTotal
my $dad = $elem->parent or next 2 0 2
229 2 ▶ $parents{ refaddr( $dad ) }++
Branch: 1 branch
BranchTrueFalseTotal
$parents{builtin::refaddr($dad)}++ or push @rslt, $dad 1 1 2
230 or push @rslt, $dad;
231 }
232  
233 1 return \@rslt;
234 }
235  
236 =head2 find_first
237  
238 This method has the same arguments as L</find>, but returns either a
239 reference to the first element found, a false (but defined) value if no
240 elements were found, or C<undef> if an error occurred.
241  
242 =cut
243  
244 7 ▶sub find_first {
Subroutine find_first: called CC=9 SCAR=23.1
Pod documented
245 7 my ( $self, $want ) = @_;
246  
247 7 ▶ $want = _find_routine( $want ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$want = _find_routine($want) or return 7 0 7
248  
249 # We use a recursion to find what we want. PPI::Node uses an
250 # iteration.
251 7 foreach my $elem ( $self->elements() ) {
252 16 ▶ my $rslt = eval { $want->( $self, $elem ) }
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = eval { do { &$want($self, $elem) } } and return $elem 3 13 16
253 and return $elem;
254 13 ▶ $@ and return;
Branch: 1 branch
BranchTrueFalseTotal
$@ and return 0 13 13
255  
256 13 ▶ __instance( $elem, 'PPIx::Regexp::Node' ) or next;
Branch: 1 branch
BranchTrueFalseTotal
__instance($elem, "PPIx::Regexp::Node") or next 4 9 13
257 4 ▶ defined $rslt or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $rslt or next 4 0 4
258  
259 4 ▶ defined( $rslt = $elem->find_first( $want ) )
Branch: 1 branch
BranchTrueFalseTotal
defined($rslt = $elem->find_first($want)) or return 4 0 4
260 or return;
261 4 ▶ $rslt and return $rslt;
Branch: 1 branch
BranchTrueFalseTotal
$rslt and return $rslt 4 0 4
262 }
263  
264 0 return 0;
265  
266 }
267  
268 =head2 first_element
269  
270 This method returns the first element in the node.
271  
272 =cut
273  
274 3 ▶sub first_element {
Subroutine first_element: called CC=1 SCAR=0.0
Pod documented
275 3 my ( $self ) = @_;
276 3 return $self->{children}[0];
277 }
278  
279 =head2 first_token
280  
281 This method returns the first token in the node. If there is none, it
282 returns nothing.
283  
284 =cut
285  
286 4 ▶sub first_token {
Subroutine first_token: called CC=4 SCAR=16.4
Pod documented
287 4 my ( $self ) = @_;
288 4 ▶ my $elem = $self->first_element()
Branch: 1 branch
BranchTrueFalseTotal
my $elem = $self->first_element or return 4 0 4
289 or return;
290 4 my $token;
291 4 ▶ while ( ! ( $token = $elem->first_token() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $elem->first_token) 0 4 4
292 0 ▶ $elem = $elem->next_element()
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->next_element or return 0 0 0
293 or return;
294 }
295 4 return $token;
296 }
297  
298 =head2 last_element
299  
300 This method returns the last element in the node.
301  
302 =cut
303  
304 2 ▶sub last_element {
Subroutine last_element: called CC=1 SCAR=0.0
Pod documented
305 2 my ( $self ) = @_;
306 2 return $self->{children}[-1];
307 }
308  
309 =head2 last_token
310  
311 This method returns the last token in the node. If there is none, it
312 returns nothing.
313  
314 =cut
315  
316 45 ▶sub last_token {
Subroutine last_token: called CC=4 SCAR=16.4
Pod documented
317 45 my ( $self ) = @_;
318 45 ▶ my $elem = $self->last_element()
Branch: 1 branch
BranchTrueFalseTotal
my $elem = $self->last_element or return 45 0 45
319 or return;
320 45 my $token;
321 45 ▶ while ( ! ( $token = $elem->last_token() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $elem->last_token) 0 45 45
322 0 ▶ $elem = $elem->previous_element()
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->previous_element or return 0 0 0
323 or return;
324 }
325 45 return $token;
326 }
327  
328 1 ▶sub location {
Subroutine location: called CC=2 SCAR=7.1
Pod documented
329 1 my ( $self ) = @_;
330 1 ▶ my $token = $self->first_token()
Branch: 1 branch
BranchTrueFalseTotal
my $token = $self->first_token or return undef 1 0 1
331 or return undef; ## no critic (ProhibitExplicitReturnUndef)
332 1 return $token->location();
333 }
334  
335 =head2 is_matcher
336  
337 This method returns a true value if any of the node's children does.
338 Otherwise it returns C<undef> if any of the node's children does.
339 Otherwise it returns a false (but defined) value.
340  
341 =cut
342  
343 5 ▶sub is_matcher {
Subroutine is_matcher: called CC=4 SCAR=17.1
Pod documented
344 5 my ( $self ) = @_;
345 5 my $rslt = 0;
346 5 foreach my $kid ( @{ $self->{children} } ) {
347 5 ▶ my $kid_rslt = $kid->is_matcher()
Branch: 1 branch
BranchTrueFalseTotal
my $kid_rslt = $kid->is_matcher and return 1 5 0 5
348 and return 1;
349 0 ▶ defined $kid_rslt
Branch: 1 branch
BranchTrueFalseTotal
defined $kid_rslt or $rslt = $kid_rslt 0 0 0
350 or $rslt = $kid_rslt;
351 }
352 0 return $rslt;
353 }
354  
355 =head2 perl_version_introduced
356  
357 This method returns the maximum value of C<perl_version_introduced>
358 returned by any of its elements. In other words, it returns the minimum
359 version of Perl under which this node is valid. If there are no
360 elements, 5.000 is returned, since that is the minimum value of Perl
361 supported by this package.
362  
363 =cut
364  
365 176 ▶sub perl_version_introduced {
Subroutine perl_version_introduced: called CC=1 SCAR=0.0
Pod documented
366 176 my ( $self ) = @_;
367 1038 return max( grep { defined $_ } MINIMUM_PERL,
368 $self->{perl_version_introduced},
369 176 map { $_->perl_version_introduced() } $self->elements() );
370 }
371  
372 =head2 perl_version_removed
373  
374 This method returns the minimum defined value of C<perl_version_removed>
375 returned by any of the node's elements. In other words, it returns the
376 lowest version of Perl in which this node is C<not> valid. If there are
377 no elements, or if no element has a defined C<perl_version_removed>,
378 C<undef> is returned.
379  
380 =cut
381  
382 182 ▶sub perl_version_removed {
Subroutine perl_version_removed: called CC=5 SCAR=17.2
Pod documented
383 182 my ( $self ) = @_;
384 182 my $max;
385 182 foreach my $elem ( $self->elements() ) {
386 697 ▶ if ( defined ( my $ver = $elem->perl_version_removed() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ver = $elem->perl_version_removed)) 15 682 697
387 15 ▶ if ( defined $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) { } 0 15 15
388 0 ▶ $ver < $max and $max = $ver;
Branch: 1 branch
BranchTrueFalseTotal
$ver < $max and $max = $ver 0 0 0
389 } else {
390 15 $max = $ver;
391 }
392 }
393 }
394 182 return $max;
395 }
396  
397 0 ▶sub remove_insignificant {
Subroutine remove_insignificant: not called CC=1 SCAR=6.9
Pod documented
398 0 my ( $self ) = @_;
399 0 return $self->__new( map { $_->remove_insignificant() }
400 $self->children() );
401 }
402  
403 =head2 schild
404  
405 This method returns the significant child at the given index; that is,
406 C<< $node->schild(0) >> returns the first significant child,
407 C<< $node->schild(1) >> returns the second significant child, and so on.
408 Negative indices count from the end.
409  
410 =cut
411  
412 22 ▶sub schild {
Subroutine schild: called CC=9 SCAR=22.1
Pod documented
413 22 my ( $self, $inx ) = @_;
414 22 ▶ defined $inx or $inx = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $inx or $inx = 0 22 0 22
415  
416 22 my $kids = $self->{children};
417  
418 22 ▶ if ( $inx >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inx >= 0) { } 20 2 22
419  
420 20 my $loc = 0;
421  
422 20 ▶ while ( exists $kids->[$loc] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $kids->[$loc]) 22 0 22
423 22 ▶ $kids->[$loc]->significant() or next;
Branch: 1 branch
BranchTrueFalseTotal
$kids->[$loc]->significant or next 21 1 22
424 21 ▶ --$inx >= 0 and next;
Branch: 1 branch
BranchTrueFalseTotal
--$inx >= 0 and next 1 20 21
425 20 return $kids->[$loc];
426 } continue {
427 2 $loc++;
428 }
429  
430 } else {
431  
432 2 my $loc = -1;
433
434 2 ▶ while ( exists $kids->[$loc] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $kids->[$loc]) 5 0 5
435 5 ▶ $kids->[$loc]->significant() or next;
Branch: 1 branch
BranchTrueFalseTotal
$kids->[$loc]->significant or next 3 2 5
436 3 ▶ $inx++ < -1 and next;
Branch: 1 branch
BranchTrueFalseTotal
$inx++ < -1 and next 1 2 3
437 2 return $kids->[$loc];
438 } continue {
439 3 --$loc;
440 }
441  
442 }
443  
444 0 return;
445 }
446  
447 =head2 schildren
448  
449 This method returns the significant children of the Node. If called in
450 scalar context it returns the number of significant children.
451  
452 =cut
453  
454 4 ▶sub schildren {
Subroutine schildren: called CC=5 SCAR=24.7
Pod documented
455 4 my ( $self ) = @_;
456 4 ▶ if ( wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 4 0 4
elsif (defined wantarray) { } 0 0 0
457 4 return ( grep { $_->significant() } @{ $self->{children} } );
458 } elsif ( defined wantarray ) {
459 0 my $kids = 0;
460 0 foreach ( @{ $self->{children} } ) {
461 0 ▶ $_->significant() and $kids++;
Branch: 1 branch
BranchTrueFalseTotal
$_->significant and ++$kids 0 0 0
462 }
463 0 return $kids;
464 } else {
465 0 return;
466 }
467 }
468  
469 9 ▶sub scontent {
Subroutine scontent: called CC=2 SCAR=7.0
Pod documented
470 9 my ( $self ) = @_;
471 # As of the invention of this method all nodes are significant, so
472 # the following statement is pure paranoia on my part. -- TRW
473 9 ▶ $self->significant()
Branch: 1 branch
BranchTrueFalseTotal
$self->significant or return 9 0 9
474 or return;
475 # This needs to be elements(), not children() or schildren() -- or
476 # selements() if that is ever invented. Not children() or
477 # schildren() because those ignore the delimiters. Not selements()
478 # (if that ever comes to pass) because scontent() has to make the
479 # significance check, so selements() would be wasted effort.
480 9 return join( '', map{ $_->scontent() } $self->elements() );
481 }
482  
483 35 ▶sub tokens {
Subroutine tokens: called CC=1 SCAR=0.0
Pod documented
484 35 my ( $self ) = @_;
485 35 return ( map { $_->tokens() } $self->elements() );
486 }
487  
488 0 ▶sub unescaped_content {
Subroutine unescaped_content: not called CC=1 SCAR=6.9
Pod documented
489 0 my ( $self ) = @_;
490 0 return join '', map { $_->unescaped_content() } $self->elements();
491 }
492  
493 9 ▶use constant ALTERNATION => q<|>;
Subroutine BEGIN: called CC=1 SCAR=0.0
494  
495 {
496 9 my $obj;
497 390 ▶ sub _alternation_object {
Subroutine _alternation_object: called CC=2 SCAR=6.9
498 390 ▶ unless ( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 2 388 390
499  
500 =begin comment
501  
502 # This is a pain because PPIx::Regexp::Token requires a
503 # tokenizer object.
504 require PPIx::Regexp::Tokenizer;
505 require PPIx::Regexp::Token::Operator;
506 $obj = PPIx::Regexp::Token::Operator->__new(
507 ALTERNATION,
508 tokenizer => PPIx::Regexp::Tokenizer->new( ALTERNATION ),
509 );
510  
511 =end comment
512  
513 =cut
514  
515 # DANGER WILL ROBINSON!
516 # This is a horrible encapsulation violation, which I get
517 # away with because I am using the object as a sentinel.
518  
519 2 $obj = bless {
520 content => ALTERNATION,
521 }, 'PPIx::Regexp::Token::Operator';
522 }
523 390 return $obj;
524 }
525 }
526  
527 9 ▶sub raw_width {
Subroutine raw_width: called CC=1 SCAR=0.0
Pod documented
528 396 my ( $self ) = @_;
529 396 return ( $self->__raw_width() )[ 0, 1 ];
530 }
531  
532 # PRIVATE TO THIS PACKAGE.
533 # This is the machinery for raw_width(), but because the datum is needed
534 # internally it also returns the number of alternatives found.
535 390 ▶sub __raw_width {
Subroutine __raw_width: called CC=12 SCAR=24.9
536 390 my ( $self ) = @_;
537 390 my ( $node_min, $node_max ) = ( INFINITY, 0 );
538 390 my ( $raw_min, $raw_max ) = ( 0, 0 );
539 390 my $alternatives = 0;
540 390 foreach my $elem ( $self->elements(), _alternation_object() ) {
541 1926 ▶ if ( $elem->isa( 'PPIx::Regexp::Token::Operator' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPIx::Regexp::Token::Operator') and $elem->content eq '|') { } 428 1498 1926
Condition66%2/3
$elem->isa("PPIx::Regexp::Token::Operator") and $elem->content eq "|"
!ll&&!rl&&r
14980428
MC/DC50%1/2
$elem->isa("PPIx::Regexp::Token::Operator") and $elem->content eq "|"
$elem->isa("PPIx::Regexp::Token::Operator") $elem->content eq "|"
Truth table66%2/3
A and B
A= $elem->isa("PPIx::Regexp::Token::Operator")B= $elem->content eq "|"
ABresult
0X0
100
111
542 $elem->content() eq ALTERNATION
543 ) {
544 428 $alternatives++;
545 428 ▶ defined $node_min
Branch: 2 branches
BranchTrueFalseTotal
defined $node_min and $node_min = defined $raw_min ? &min($node_min, $raw_min) : undef 423 5 428
defined $raw_min ? : 390 33 423
546 and $node_min = defined $raw_min ?
547 min( $node_min, $raw_min ) :
548 undef;
549 428 $raw_min = 0;
550 428 ▶ defined $node_max
Branch: 2 branches
BranchTrueFalseTotal
defined $node_max and $node_max = defined $raw_max ? &max($node_max, $raw_max) : undef 423 5 428
defined $raw_max ? : 378 45 423
551 and $node_max = defined $raw_max ?
552 max( $node_max, $raw_max ) :
553 undef;
554 428 $raw_max = 0;
555 } else {
556 1498 my ( $e_min, $e_max ) = $elem->width();
557 1498 ▶ defined $raw_min
Branch: 2 branches
BranchTrueFalseTotal
defined $raw_min and $raw_min = defined $e_min ? $raw_min + $e_min : undef 1469 29 1498
defined $e_min ? : 1435 34 1469
558 and $raw_min = defined $e_min ? $raw_min + $e_min : undef;
559 1498 ▶ defined $raw_max
Branch: 2 branches
BranchTrueFalseTotal
defined $raw_max and $raw_max = defined $e_max ? $raw_max + $e_max : undef 1449 49 1498
defined $e_max ? : 1403 46 1449
560 and $raw_max = defined $e_max ? $raw_max + $e_max : undef;
561 }
562 }
563 390 return ( $node_min, $node_max, $alternatives );
564 }
565  
566 # Help for nav();
567 23 ▶sub __nav {
Subroutine __nav: called CC=3 SCAR=11.4
568 23 my ( $self, $child ) = @_;
569 23 ▶ refaddr( $child->parent() ) == refaddr( $self )
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($child->parent) == builtin::refaddr($self) or return 23 0 23
570 or return;
571 23 ▶ my ( $method, $inx ) = $child->__my_nav()
Branch: 1 branch
BranchTrueFalseTotal
() = $child->__my_nav or return 23 0 23
572 or return;
573  
574 23 return ( $method => [ $inx ] );
575 }
576  
577 3 ▶sub __error {
Subroutine __error: called CC=3 SCAR=11.0
578 3 my ( $self, $msg, %arg ) = @_;
579 3 ▶ defined $msg
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or $msg = 'Was class ' . ref($self) 3 0 3
580 or $msg = 'Was class ' . ref $self;
581 3 $self->ELEMENT_UNKNOWN()->__PPIX_ELEM__rebless( $self, error => $msg );
582 3 foreach my $key ( keys %arg ) {
583 2 $self->{$key} = $arg{$key};
584 }
585 3 return 1;
586 }
587  
588 23 ▶sub __perl_requirements {
Subroutine __perl_requirements: called CC=3 SCAR=11.0
589 23 my ( $self ) = @_;
590 23 ▶ unless ( $self->{perl_requirements} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"perl_requirements"}) 9 14 23
591 9 my @req = $self->__perl_requirements_setup();
592 9 foreach my $kid ( $self->children() ) {
593 19 push @req, $kid->__perl_requirements();
594 }
595 9 $self->{perl_requirements} = [ __merge_perl_requirements( @req ) ];
596 }
597 23 return @{ $self->{perl_requirements} };
598 }
599  
600 5 ▶sub _token_order {
Subroutine _token_order: called CC=2 SCAR=6.9
601 5 my ( $self ) = @_;
602 5 my $order = 0;
603 5 delete $self->{_token_order};
604 5 foreach my $elem ( $self->tokens() ) {
605 58 $self->{_token_order}{ refaddr $elem } = $order++;
606 }
607 5 return;
608 }
609  
610 # Order two elements according to the position of their last tokens. The
611 # elements must both be descendants of the invocant or an exception is
612 # thrown. The return is equivalent to the space ship operator (<=>).
613 #
614 # For the moment at least this is private to the PPIx-Regexp package.
615 # It is needed by the width() functionality to (try to) determine which
616 # capture group a back reference refers to.
617 42 ▶sub __token_post_order {
Subroutine __token_post_order: called CC=5 SCAR=16.2
618 42 my ( $self, $left, $right ) = @_;
619 $self->{_token_order}
620 42 ▶ or $self->_token_order();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_token_order"} or $self->_token_order 37 5 42
621 42 my @order;
622 42 foreach ( $left, $right ) {
623 84 ▶ ref $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ or confess("Bug - Operand must be a PPIx::Regexp::Element") 84 0 84
624 or confess 'Bug - Operand must be a PPIx::Regexp::Element';
625 84 ▶ defined( my $inx = $self->{_token_order}{ refaddr( $_->last_token() ) } )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $inx = $self->{"_token_order"}{builtin::refaddr($_->last_token)}) or confess("Bug - Operand not descendant of invocant") 84 0 84
626 or confess 'Bug - Operand not descendant of invocant';
627 84 push @order, $inx;
628 }
629 42 return $order[0] <=> $order[1];
630 }
631  
632 # Called by the lexer once it has done its worst to all the tokens.
633 # Called as a method with the lexer as argument. The return is the
634 # number of parse failures discovered when finalizing.
635 265 ▶sub __PPIX_LEXER__finalize {
Subroutine __PPIX_LEXER__finalize: called CC=2 SCAR=6.9
636 265 my ( $self, $lexer ) = @_;
637 265 my $rslt = 0;
638 265 foreach my $elem ( $self->elements() ) {
639 1213 $rslt += $elem->__PPIX_LEXER__finalize( $lexer );
640 }
641 265 return $rslt;
642 }
643  
644 # Called by the lexer to record the capture number.
645 520 ▶sub __PPIX_LEXER__record_capture_number {
Subroutine __PPIX_LEXER__record_capture_number: called CC=2 SCAR=6.9
646 520 my ( $self, $number ) = @_;
647 520 foreach my $kid ( $self->children() ) {
648 1288 $number = $kid->__PPIX_LEXER__record_capture_number( $number );
649 }
650 520 return $number;
651 }
652  
653 9 1;
654  
655 __END__
« Regexp/Token/Reference.pm ↑ Summary Regexp/Token/Control.pm »