File Coverage

blib/lib/PPIx/Regexp/Element.pm
Criterion Covered Total %
statement 222 245 90.6
branch 87 108 80.5
condition 16 23 69.5
subroutine 62 75 82.6
pod 49 49 100.0
total 436 500 87.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             PPIx::Regexp::Element - Base of the PPIx::Regexp hierarchy.
4              
5             =head1 SYNOPSIS
6              
7             No user-serviceable parts inside.
8              
9             =head1 INHERITANCE
10              
11             C is not descended from any other class.
12              
13             C is the parent of
14             L and
15             L.
16              
17             =head1 DESCRIPTION
18              
19             This class is the base of the L
20             object hierarchy. It provides the same kind of navigational
21             functionality that is provided by L.
22              
23             =head1 METHODS
24              
25             This class provides the following public methods. Methods not documented
26             here are private, and unsupported in the sense that the author reserves
27             the right to change or remove them without notice.
28              
29             =cut
30              
31             package PPIx::Regexp::Element;
32              
33 9     9   72 use strict;
  9         19  
  9         235  
34 9     9   58 use warnings;
  9         17  
  9         222  
35              
36 9     9   174 use 5.006;
  9         31  
37              
38 9     9   49 use Carp;
  9         18  
  9         703  
39 9     9   54 use List::Util qw{ first max min };
  9         16  
  9         1284  
40 9     9   3580 use PPIx::Regexp::Util qw{ __instance };
  9         23  
  9         541  
41 9     9   65 use Scalar::Util qw{ refaddr weaken };
  9         16  
  9         536  
42              
43 9         30303 use PPIx::Regexp::Constant qw{
44             FALSE
45             LITERAL_LEFT_CURLY_REMOVED_PHASE_1
46             LOCATION_LINE
47             LOCATION_CHARACTER
48             LOCATION_COLUMN
49             LOCATION_LOGICAL_LINE
50             LOCATION_LOGICAL_FILE
51             MINIMUM_PERL
52             TOKEN_UNKNOWN
53             TRUE
54             @CARP_NOT
55 9     9   52 };
  9         42  
56              
57             our $VERSION = '0.087_01';
58              
59             =head2 accepts_perl
60              
61             $token->accepts_perl( '5.020' )
62             and say 'This works under Perl 5.20';
63              
64             This method returns a true value if the token is acceptable under the
65             specified version of Perl, and a false value otherwise. Unless the token
66             (or its contents) have been equivocated on, the result is simply what
67             you would expect based on testing the results of
68             L and
69             L versus the given Perl
70             version number.
71              
72             This method was added in version 0.051_01.
73              
74             =cut
75              
76             sub accepts_perl {
77 12     12 1 27 my ( $self, $version ) = @_;
78 12         38 foreach my $check ( $self->__perl_requirements() ) {
79             $version < $check->{introduced}
80 15 100       54 and next;
81             defined $check->{removed}
82             and $version >= $check->{removed}
83 11 100 100     44 and next;
84 8         26 return TRUE;
85             }
86 4         12 return FALSE;
87             }
88              
89             # Return the Perl requirements, constructing if necessary. The
90             # requirements are simply an array of hashes containing keys:
91             # {introduced} - The Perl version introduced;
92             # {removed} - The Perl version removed (or undef)
93             # The requirements are evaluated by iterating through the array,
94             # returning a true value if the version of Perl being tested falls
95             # inside any of the half-open (on the right) intervals.
96             sub __perl_requirements {
97 18     18   63 my ( $self ) = @_;
98 18         26 return @{ $self->{perl_requirements} ||=
99 18   100     85 [ $self->__perl_requirements_setup() ] };
100             }
101              
102             # Construct the array returned by __perl_requirements().
103             sub __perl_requirements_setup {
104 22     22   56 my ( $self ) = @_;
105             return {
106 22         54 introduced => $self->perl_version_introduced(),
107             removed => $self->perl_version_removed(),
108             };
109             }
110              
111             =head2 ancestor_of
112              
113             This method returns true if the object is an ancestor of the argument,
114             and false otherwise. By the definition of this method, C<$self> is its
115             own ancestor.
116              
117             =cut
118              
119             sub ancestor_of {
120 5     5 1 13 my ( $self, $elem ) = @_;
121 5 100       12 __instance( $elem, __PACKAGE__ ) or return;
122 4         19 my $addr = refaddr( $self );
123 4         12 while ( $addr != refaddr( $elem ) ) {
124 14 100       43 $elem = $elem->_parent() or return;
125             }
126 2         6 return 1;
127             }
128              
129             =head2 can_be_quantified
130              
131             $token->can_be_quantified()
132             and print "This element can be quantified.\n";
133              
134             This method returns true if the element can be quantified.
135              
136             =cut
137              
138 680     680 1 2233 sub can_be_quantified { return 1; }
139              
140             =head2 class
141              
142             This method returns the class name of the element. It is the same as
143             C.
144              
145             =cut
146              
147             sub class {
148 50     50 1 133 my ( $self ) = @_;
149 50         307 return ref $self;
150             }
151              
152             =head2 column_number
153              
154             This method returns the column number of the first character in the
155             element, or C if that can not be determined.
156              
157             =cut
158              
159             sub column_number {
160 1     1 1 5 my ( $self ) = @_;
161 1   50     4 return ( $self->location() || [] )->[LOCATION_CHARACTER];
162             }
163              
164             =head2 comment
165              
166             This method returns true if the element is a comment and false
167             otherwise.
168              
169             =cut
170              
171             sub comment {
172 2     2 1 5 return;
173             }
174              
175             =head2 content
176              
177             This method returns the content of the element.
178              
179             =cut
180              
181             sub content {
182 0     0 1 0 return;
183             }
184              
185             =head2 descendant_of
186              
187             This method returns true if the object is a descendant of the argument,
188             and false otherwise. By the definition of this method, C<$self> is its
189             own descendant.
190              
191             =cut
192              
193             sub descendant_of {
194 3     3 1 8 my ( $self, $node ) = @_;
195 3 100       10 __instance( $node, __PACKAGE__ ) or return;
196 2         10 return $node->ancestor_of( $self );
197             }
198              
199             =head2 explain
200              
201             This method returns a brief explanation of what the element does. The
202             return will be either a string or C in scalar context, but may be
203             multiple values or an empty array in list context.
204              
205             This method should be considered experimental. What it returns may
206             change without notice as my understanding of what all the pieces/parts
207             of a Perl regular expression evolves. The worst case is that it will
208             prove entirely infeasible to implement satisfactorily, in which case it
209             will be put through a deprecation cycle and retracted.
210              
211             =cut
212              
213             sub explain {
214 83     83 1 171 my ( $self ) = @_;
215             defined $self->{explanation}
216 83 100       209 and return $self->{explanation};
217 82         247 my $explanation = $self->__explanation();
218 82         231 my $content = $self->content();
219 82 100       221 if ( my $main = $self->main_structure() ) {
220 81         266 my $delim = $main->delimiters();
221 81         505 $delim = qr{ \\ (?= [\Q$delim\E] ) }smx;
222 81         442 $content =~ s/$delim//smxg;
223             }
224 82 50       338 if ( defined( my $splain = $explanation->{$content} ) ) {
225 82         255 return $splain;
226             }
227 0         0 return $self->__no_explanation();
228             }
229              
230             # Return explanation hash
231             sub __explanation {
232 0 0   0   0 $PPIx::Regexp::NO_EXPLANATION_FATAL
233             and confess 'Neither explain() nor __explanation() overridden';
234 0         0 return {};
235             }
236              
237             # Called if no explanation available
238             sub __no_explanation {
239             ## my ( $self ) = @_; # Invocant unused
240 0     0   0 my $msg = sprintf q;
241 0 0       0 $PPIx::Regexp::NO_EXPLANATION_FATAL
242             and confess $msg;
243 0         0 return $msg;
244             }
245              
246             =head2 error
247              
248             say $token->error();
249              
250             If an element is one of the classes that represents a parse error, this
251             method B return a brief message saying why. Otherwise it will
252             return C.
253              
254             =cut
255              
256             sub error {
257 73     73 1 148 my ( $self ) = @_;
258 73         236 return $self->{error};
259             }
260              
261             =begin comment
262              
263             =head2 first_element
264              
265             This method throws an exception saying that it must be overridden.
266              
267             =end comment
268              
269             =cut
270              
271             sub first_element {
272 0     0 1 0 confess 'Bug - first_element must be overridden';
273             }
274              
275             =begin comment
276              
277             =head2 first_token
278              
279             This method throws an exception saying that it must be overridden.
280              
281             =end comment
282              
283             =cut
284              
285             sub first_token {
286 0     0 1 0 confess 'Bug - first_token must be overridden';
287             }
288              
289             =head2 is_matcher
290              
291             This method reports on whether the element potentially matches
292             something. Possible returns are a true value if it does, a false (but
293             defined) value if it does not, or C if this can not be
294             determined.
295              
296             The idea is to classify elements based on whether they potentially match
297             something in the target string.
298              
299             This method is overridden to return C in
300             L,
301             L, and
302             L.
303              
304             This method is overridden to return a true value in
305             L,
306             L,
307             L,
308             and
309             L.
310              
311             For L, this method is
312             overridden to return a value computed from the node's children.
313              
314             For anything else this method returns a false (but defined) value.
315              
316             =cut
317              
318 3     3 1 7 sub is_matcher { return 0; }
319              
320             # NOTE retracted this as a public method until I can investigate whether
321             # the tokenizer can actually produce nested assertions.
322              
323             #=head2 in_assertion
324             #
325             #This method returns an array of assertions that contain the element,
326             #most-local first. For the purpose of this method, a look-around
327             #structure does not contain itself. If called in scalar context you get
328             #the size of the array.
329             #
330             #This method was added in version 0.075_01.
331             #
332             #=cut
333              
334             sub __in_assertion {
335 80     80   201 my ( $self ) = @_;
336 80         144 my $elem = $self;
337 80         134 my @assertions;
338 80         321 while ( $elem = $elem->parent() ) {
339 98 100       661 $elem->isa( 'PPIx::Regexp::Structure::Assertion' )
340             and push @assertions, $elem;
341             }
342 80         341 return @assertions;
343             }
344              
345             # Convenience method that returns the number of look-behind
346             # assertions that contain the current element. This is really only
347             # here so it can be shared between PPIx::Regexp::Token::Quantifier
348             # and PPIx::Regexp::Structure::Quantifier
349              
350             sub __in_look_behind {
351 80     80   211 my ( $self ) = @_;
352 80         147 my @look_behind;
353 80         302 foreach my $assertion ( $self->__in_assertion() ) {
354 6 100       37 $assertion->is_look_ahead()
355             and next;
356 4         16 push @look_behind, $assertion;
357             }
358 80         389 return @look_behind;
359             }
360              
361             =head2 in_regex_set
362              
363             This method returns a true value if the invocant is contained in an
364             extended bracketed character class (also known as a regex set), and a
365             false value otherwise. This method returns true if the invocant is a
366             L.
367              
368             =cut
369              
370             sub in_regex_set {
371 2     2 1 7 my ( $self ) = @_;
372 2         4 my $ele = $self;
373 2         5 while ( 1 ) {
374 5 100       19 $ele->isa( 'PPIx::Regexp::Structure::RegexSet' )
375             and return 1;
376 4 100       12 $ele = $ele->parent()
377             or last;
378             }
379 1         15 return 0;
380             }
381              
382             =head2 is_quantifier
383              
384             $token->is_quantifier()
385             and print "This element is a quantifier.\n";
386              
387             This method returns true if the element is a quantifier. You can not
388             tell this from the element's class, because a right curly bracket may
389             represent a quantifier for the purposes of figuring out whether a
390             greediness token is possible.
391              
392             =cut
393              
394 597     597 1 2111 sub is_quantifier { return; }
395              
396             =begin comment
397              
398             =head2 last_element
399              
400             This method throws an exception saying that it must be overridden.
401              
402             =end comment
403              
404             =cut
405              
406             sub last_element {
407 0     0 1 0 confess 'Bug - last_element must be overridden';
408             }
409              
410             =begin comment
411              
412             =head2 last_token
413              
414             This method throws an exception saying that it must be overridden.
415              
416             =end comment
417              
418             =cut
419              
420             sub last_token {
421 0     0 1 0 confess 'Bug - last_token must be overridden';
422             }
423              
424             =head2 line_number
425              
426             This method returns the line number of the first character in the
427             element, or C if that can not be determined.
428              
429             =cut
430              
431             sub line_number {
432 1     1 1 3 my ( $self ) = @_;
433 1   50     4 return ( $self->location() || [] )->[LOCATION_LINE];
434             }
435              
436             =head2 location
437              
438             This method returns a reference to an array describing the position of
439             the element in the regular expression, or C if locations were not
440             indexed.
441              
442             The array is compatible with the corresponding
443             L method.
444              
445             =cut
446              
447             sub location {
448 54     54 1 2245 my ( $self ) = @_;
449 54 50       138 return $self->{location} ? [ @{ $self->{location} } ] : undef;
  54         461  
450             }
451              
452             =pod
453              
454             =head2 logical_filename
455              
456             This method returns the logical file name (taking C<#line> directives
457             into account) of the file containing first character in the element, or
458             C if that can not be determined.
459              
460             =cut
461              
462             sub logical_filename {
463 1     1 1 4 my ( $self ) = @_;
464 1   50     4 return ( $self->location() || [] )->[LOCATION_LOGICAL_FILE];
465             }
466              
467             =head2 logical_line_number
468              
469             This method returns the logical line number (taking C<#line> directives
470             into account) of the first character in the element, or C if that
471             can not be determined.
472              
473             =cut
474              
475             sub logical_line_number {
476 1     1 1 6 my ( $self ) = @_;
477 1   50     4 return ( $self->location() || [] )->[LOCATION_LOGICAL_LINE];
478             }
479              
480             =head2 main_structure
481              
482             This method returns the
483             L that
484             contains the element. In practice this will be a
485             L or a
486             L,
487              
488             If the element is not contained in any such structure, C is
489             returned. This will happen if the element is a
490             L or one of its immediate children.
491              
492             =cut
493              
494             sub main_structure {
495 258     258 1 454 my ( $self ) = @_;
496 258   100     650 while ( $self = $self->parent()
497             and not $self->isa( 'PPIx::Regexp::Structure::Main' ) ) {
498             }
499 258         695 return $self;
500             }
501              
502             =head2 modifier_asserted
503              
504             $token->modifier_asserted( 'i' )
505             and print "Matched without regard to case.\n";
506              
507             This method returns true if the given modifier is in effect for the
508             element, and false otherwise.
509              
510             What it does is to walk backwards from the element until it finds a
511             modifier object that specifies the modifier, whether asserted or
512             negated. and returns the specified value. If nobody specifies the
513             modifier, it returns C.
514              
515             This method will not work reliably if called on tokenizer output.
516              
517             =cut
518              
519             sub modifier_asserted {
520 6     6 1 23 my ( $self, $modifier ) = @_;
521              
522 6 50       19 defined $modifier
523             or croak 'Modifier must be defined';
524              
525 6         13 my $elem = $self;
526              
527 6         14 while ( $elem ) {
528 19 100       116 if ( $elem->can( '__ducktype_modifier_asserted' ) ) {
529 6         10 my $val;
530 6 50       34 defined( $val = $elem->__ducktype_modifier_asserted( $modifier ) )
531             and return $val;
532             }
533 13 100       50 if ( my $prev = $elem->sprevious_sibling() ) {
534 5         19 $elem = $prev;
535             } else {
536 8         31 $elem = $elem->parent();
537             }
538             }
539              
540 0         0 return;
541             }
542              
543             =head2 next_element
544              
545             This method returns the next element, or nothing if there is none.
546              
547             Unlike L, this will cross from the content
548             of a structure into the elements that define the structure, or vice
549             versa.
550              
551             =cut
552              
553             sub next_element {
554 16     16 1 28 my ( $self ) = @_;
555 16 100       41 my $parent = $self->_parent()
556             or return;
557 15         56 my $inx = $self->__my_inx();
558 15         73 return ( $parent->elements() )[ $inx + 1 ];
559             }
560              
561             =head2 next_sibling
562              
563             This method returns the element's next sibling, or nothing if there is
564             none.
565              
566             =cut
567              
568             sub next_sibling {
569 739     739 1 1194 my ( $self ) = @_;
570 739 100       1507 my ( $method, $inx ) = $self->__my_nav()
571             or return;
572 703         1601 return $self->_parent()->$method( $inx + 1 );
573             }
574              
575             =head2 next_token
576              
577             This method returns the next token, or nothing if there is none.
578              
579             Unlike L, this will walk the parse tree.
580              
581             =cut
582              
583             sub next_token {
584 15     15 1 30 my ( $self ) = @_;
585 15 100       52 if ( my $next = $self->next_element() ) {
    100          
586 11         64 return $next->first_token();
587             } elsif ( my $parent = $self->parent() ) {
588 3         20 return $parent->next_token();
589             } else {
590 1         18 return;
591             }
592             }
593              
594             =head2 parent
595              
596             This method returns the parent of the element, or undef if there is
597             none.
598              
599             =cut
600              
601             sub parent {
602 719     719 1 1312 my ( $self ) = @_;
603 719         1405 return $self->_parent();
604             }
605              
606             =head2 perl_version_introduced
607              
608             This method returns the version of Perl in which the element was
609             introduced. This will be at least 5.000. Before 5.006 I am relying on
610             the F, F, and F documentation, since I have
611             been unable to build earlier Perls. Since I have found no documentation
612             before 5.003, I assume that anything found in 5.003 is also in 5.000.
613              
614             Since this all depends on my ability to read and understand masses of
615             documentation, the results of this method should be viewed with caution,
616             if not downright skepticism.
617              
618             There are also cases which are ambiguous in various ways. For those see
619             the L documentation, particularly
620             L.
621              
622             Very occasionally, a construct will be removed and then added back. If
623             this happens, this method will return the B version in which the
624             construct appeared. For the known instances of this, see
625             the L documentation, particularly
626             L.
627              
628             =cut
629              
630             sub perl_version_introduced {
631 0     0 1 0 return MINIMUM_PERL;
632             }
633              
634             =head2 perl_version_removed
635              
636             This method returns the version of Perl in which the element was
637             removed. If the element is still valid the return is C.
638              
639             All the I to
640             L apply here also,
641             though perhaps less severely since although many features have been
642             introduced since 5.0, few have been removed.
643              
644             Very occasionally, a construct will be removed and then added back. If
645             this happens, this method will return the C if the construct is
646             present in the highest-numbered version of Perl (whether production or
647             development), or the version after the highest-numbered version in which
648             it appeared otherwise. For the known instances of this, see the
649             L documentation, particularly
650             L.
651              
652             =cut
653              
654             sub perl_version_removed {
655 0     0 1 0 return undef; ## no critic (ProhibitExplicitReturnUndef)
656             }
657              
658             =head2 previous_element
659              
660             This method returns the previous element, or nothing if there is none.
661              
662             Unlike L, this will cross from
663             the content of a structure into the elements that define the structure,
664             or vice versa.
665              
666             =cut
667              
668             sub previous_element {
669 16     16 1 39 my ( $self ) = @_;
670 16 100       36 my $parent = $self->_parent()
671             or return;
672 15 100       41 my $inx = $self->__my_inx()
673             or return;
674 12         60 return ( $parent->elements() )[ $inx - 1 ];
675             }
676              
677             =head2 previous_sibling
678              
679             This method returns the element's previous sibling, or nothing if there
680             is none.
681              
682             This method is analogous to the same-named L
683             method, in that it will not cross from the content of a structure into
684             the elements that define the structure.
685              
686             =cut
687              
688             sub previous_sibling {
689 33     33 1 67 my ( $self ) = @_;
690 33 100       107 my ( $method, $inx ) = $self->__my_nav()
691             or return;
692 30 100       112 $inx or return;
693 20         50 return $self->_parent()->$method( $inx - 1 );
694             }
695              
696             =head2 previous_token
697              
698             This method returns the previous token, or nothing if there is none.
699              
700             Unlike L, this will walk the parse tree.
701              
702             =cut
703              
704             sub previous_token {
705 15     15 1 33 my ( $self ) = @_;
706 15 100       60 if ( my $previous = $self->previous_element() ) {
    100          
707 11         69 return $previous->last_token();
708             } elsif ( my $parent = $self->parent() ) {
709 3         22 return $parent->previous_token();
710             } else {
711 1         18 return;
712             }
713             }
714              
715             =head2 raw_width
716              
717             my ( $raw_min, $raw_max ) = $self->raw_width();
718              
719             This public method returns the minimum and maximum width matched by the
720             element before taking into account such details as what the element
721             actually is and how it is quantified. Either or both elements can be
722             C if the width can not be determined, and the maximum can be
723             C.
724              
725             This method was added in version 0.085_01.
726              
727             =cut
728              
729             # This implementation is appropriate to a structural element -- i.e. it
730             # returns C<( 0, 0 )>.
731              
732             sub raw_width {
733 99     99 1 341 return ( 0, 0 );
734             }
735              
736             =head2 remove_insignificant
737              
738             This method returns a new object manufactured from the invocant, but
739             containing only elements for which C<< $elem->significant() >> returns a
740             true value.
741              
742             If you call this method on a L
743             you will get back a deep clone, but without the insignificant elements.
744              
745             If you call this method on any other L class
746             you will get back either the invocant or nothing. This may change to a
747             clone of the invocant or nothing if unforeseen problems arise with
748             returning the invocant, or if objects become mutable (unlikely, but not
749             impossible.)
750              
751             =cut
752              
753             sub remove_insignificant {
754 0     0 1 0 my ( $self ) = @_;
755 0 0       0 $self->significant()
756             and return $self;
757 0         0 return;
758             }
759              
760             =head2 requirements_for_perl
761              
762             say $token->requirements_for_perl();
763              
764             This method returns a string representing the Perl requirements for a
765             given module. This should only be used for informational purposes, as
766             the format of the string may be subject to change.
767              
768             At the moment, the returns may be:
769              
770             version <= $]
771             version <= $] < version
772             two or more of the above joined by '||'
773             ! $]
774              
775             The last means that, although all the components of the regular
776             expression can be compiled by B version of Perl, there is no
777             version that will compile all of them.
778              
779             I reiterate: the returned string may be subject to change, maybe without
780             warning.
781              
782             This method was added in version 0.051_01.
783              
784             =cut
785              
786             sub requirements_for_perl {
787 10     10 1 26 my ( $self ) = @_;
788 10         16 my @req;
789 10         32 foreach my $r ( $self->__perl_requirements() ) {
790             push @req, defined $r->{removed} ?
791 11 100       46 "$r->{introduced} <= \$] < $r->{removed}" :
792             "$r->{introduced} <= \$]";
793             }
794             @req
795 10 50       25 or return '! $]';
796 10         36 return join ' || ', @req;
797             }
798              
799             =head2 scontent
800              
801             This method returns the significant content of the element. That is, if
802             called on the parse of C<'/ f u b a r /x'>, it returns C<'/fubar/x'>. If
803             the invocant contains no insignificant elements, it is the same as
804             L. If called on an insignificant element, it returns
805             nothing -- that is, C in scalar context, and an empty list in
806             list context.
807              
808             This method was inspired by jb's question on Perl Monks about stripping
809             comments and white space from a regular expression:
810             L
811              
812             This method was added in version 0.053_01
813              
814             =cut
815              
816             sub scontent {
817 0     0 1 0 return;
818             }
819              
820             =head2 significant
821              
822             This method returns true if the element is significant and false
823             otherwise.
824              
825             =cut
826              
827             sub significant {
828 11374     11374 1 33565 return 1;
829             }
830              
831             =head2 snext_element
832              
833             This method returns the next significant element, or nothing if
834             there is none.
835              
836             Unlike L, this will cross from
837             the content of a structure into the elements that define the structure,
838             or vice versa.
839              
840             =cut
841              
842             sub snext_element {
843 1     1 1 3 my ( $self ) = @_;
844 1         6 my $inx = $self->__my_inx();
845 1 50       9 my $parent = $self->_parent()
846             or return;
847 1         6 my @elem = $parent->elements();
848 1         5 while ( 1 ) {
849 2         8 $inx++;
850 2 50       6 $elem[$inx]
851             or last;
852 2 100       6 $elem[$inx]->significant()
853             and return $elem[$inx];
854             }
855 0         0 return;
856             }
857              
858             =head2 snext_sibling
859              
860             This method returns the element's next significant sibling, or nothing
861             if there is none.
862              
863             This method is analogous to the same-named L
864             method, in that it will not cross from the content of a structure into
865             the elements that define the structure.
866              
867             =cut
868              
869             sub snext_sibling {
870 730     730 1 1325 my ( $self ) = @_;
871 730         1047 my $sib = $self;
872 730         1433 while ( defined ( $sib = $sib->next_sibling() ) ) {
873 434 100       1276 $sib->significant() and return $sib;
874             }
875 300         1384 return;
876             }
877              
878             =head2 sprevious_element
879              
880             This method returns the previous significant element, or nothing if
881             there is none.
882              
883             Unlike L, this will cross from
884             the content of a structure into the elements that define the structure,
885             or vice versa.
886              
887             =cut
888              
889             sub sprevious_element {
890 2     2 1 16 my ( $self ) = @_;
891 2 100       9 my $inx = $self->__my_inx()
892             or return;
893 1 50       7 my $parent = $self->_parent()
894             or return;
895 1         6 my @elem = $parent->elements();
896 1         15 while ( $inx ) {
897 2 100       9 $elem[--$inx]->significant()
898             and return $elem[$inx];
899             }
900 0         0 return;
901             }
902              
903             =head2 sprevious_sibling
904              
905             This method returns the element's previous significant sibling, or
906             nothing if there is none.
907              
908             This method is analogous to the same-named L
909             method, in that it will not cross from the content of a structure into
910             the elements that define the structure.
911              
912             =cut
913              
914             sub sprevious_sibling {
915 28     28 1 62 my ( $self ) = @_;
916 28         48 my $sib = $self;
917 28         86 while ( defined ( $sib = $sib->previous_sibling() ) ) {
918 18 100       83 $sib->significant() and return $sib;
919             }
920 11         49 return;
921             }
922              
923             =head2 statement
924              
925             This method returns the L that contains
926             this element, or nothing if the statement can not be determined.
927              
928             In general this method will return something only under the following
929             conditions:
930              
931             =over
932              
933             =item * The element is contained in a L object;
934              
935             =item * That object was initialized from a L;
936              
937             =item * The L is contained in a statement.
938              
939             =back
940              
941             =cut
942              
943             sub statement {
944 2     2 1 7 my ( $self ) = @_;
945 2 50       8 my $top = $self->top()
946             or return;
947 2 50       10 $top->can( 'source' )
948             or return;
949 2 50       7 my $source = $top->source()
950             or return;
951 2 100       23 $source->can( 'statement' )
952             or return;
953 1         5 return $source->statement();
954             }
955              
956             =head2 tokens
957              
958             This method returns all tokens contained in the element.
959              
960             =cut
961              
962             sub tokens {
963 119     119 1 199 my ( $self ) = @_;
964 119         329 return $self;
965             }
966              
967             =head2 top
968              
969             This method returns the top of the hierarchy.
970              
971             =cut
972              
973             sub top {
974 36     36 1 98 my ( $self ) = @_;
975 36         67 my $kid = $self;
976 36         95 while ( defined ( my $parent = $kid->_parent() ) ) {
977 64         160 $kid = $parent;
978             }
979 36         187 return $kid;
980             }
981              
982             =head2 unescaped_content
983              
984             This method returns the content of the element, unescaped.
985              
986             =cut
987              
988             sub unescaped_content {
989 0     0 1 0 return;
990             }
991              
992             =head2 visual_column_number
993              
994             This method returns the visual column number (taking tabs into account)
995             of the first character in the element, or C if that can not be
996             determined.
997              
998             =cut
999              
1000             sub visual_column_number {
1001 1     1 1 4 my ( $self ) = @_;
1002 1   50     5 return ( $self->location() || [] )->[LOCATION_COLUMN];
1003             }
1004              
1005             =head2 whitespace
1006              
1007             This method returns true if the element is whitespace and false
1008             otherwise.
1009              
1010             =cut
1011              
1012             sub whitespace {
1013 2     2 1 5 return;
1014             }
1015              
1016             =head2 width
1017              
1018             my ( $min, $max ) = $self->width();
1019              
1020             This method returns the minimum and maximum number of characters this
1021             element can match.
1022              
1023             Either element can be C if it cannot be determined. For example,
1024             for C both elements will be C. Recursions will return
1025             C because they can not be analyzed statically -- or at least I am
1026             not smart enough to do so. Back references B return C if the
1027             referred-to group can not be uniquely determined.
1028              
1029             It is possible for C<$max> to be C. For example, for C
1030             C<$max> will be C.
1031              
1032             Elements that do not actually match anything will return zeroes.
1033              
1034             B This method was added because I wanted better detection of
1035             variable-length look-behinds. Both it and L
1036             (above) should be considered somewhat experimental.
1037              
1038             This method was added in version 0.085_01.
1039              
1040             =cut
1041              
1042             sub width {
1043 987     987 1 2232 return ( 0, 0 );
1044             }
1045              
1046             =head2 nav
1047              
1048             This method returns navigation information from the top of the hierarchy
1049             to this node. The return is a list of names of methods and references to
1050             their argument lists. The idea is that given C<$elem> which is somewhere
1051             under C<$top>,
1052              
1053             my @nav = $elem->nav();
1054             my $obj = $top;
1055             while ( @nav ) {
1056             my $method = shift @nav;
1057             my $args = shift @nav;
1058             $obj = $obj->$method( @{ $args } ) or die;
1059             }
1060             # At this point, $obj should contain the same object
1061             # as $elem.
1062              
1063             =cut
1064              
1065             sub nav {
1066 33     33 1 58 my ( $self ) = @_;
1067 33 50       67 __instance( $self, __PACKAGE__ ) or return;
1068              
1069             # We do not use $self->parent() here because PPIx::Regexp overrides
1070             # this to return the (possibly) PPI object that initiated us.
1071 33 100       72 my $parent = $self->_parent() or return;
1072              
1073 23         72 return ( $parent->nav(), $parent->__nav( $self ) );
1074             }
1075              
1076             # Find our index among the parents children. If not found, just return.
1077             # Unlike __my_nav(), this just returns an index, which is appropriate
1078             # for ->element( $inx ), or would be if element() existed.
1079              
1080             sub __my_inx {
1081 33     33   62 my ( $self ) = @_;
1082 33 100       66 my $parent = $self->_parent() or return;
1083 32         69 my $addr = refaddr( $self );
1084 32         99 my @elem = $parent->elements();
1085 32     91   172 return first { refaddr( $elem[$_] ) == $addr } 0 .. $#elem;
  91         225  
1086             }
1087              
1088             # Find our location and index among the parent's children. If not found,
1089             # just returns.
1090              
1091             {
1092             my %method_map = (
1093             children => 'child',
1094             );
1095              
1096             sub __my_nav {
1097 795     795   1252 my ( $self ) = @_;
1098 795 100       1594 my $parent = $self->_parent() or return;
1099 756         1588 my $addr = refaddr( $self );
1100 756         1373 foreach my $method ( qw{ children start type finish } ) {
1101 758 50       2521 $parent->can( $method ) or next;
1102 758         1932 my @elem = $parent->$method();
1103 1328     1328   3556 defined( my $inx = first { refaddr( $elem[$_] ) == $addr }
1104 758 100       3980 0 .. $#elem )
1105             or next;
1106 756   66     4715 return ( $method_map{$method} || $method, $inx );
1107             }
1108 0         0 return;
1109             }
1110             }
1111              
1112             {
1113             my %parent;
1114              
1115             # no-argument form returns the parent; one-argument sets it.
1116             sub _parent {
1117 12796     12796   22662 my ( $self, @arg ) = @_;
1118 12796         22228 my $addr = refaddr( $self );
1119 12796 100       24344 if ( @arg ) {
1120 10345         14344 my $parent = shift @arg;
1121 10345 100       17528 if ( defined $parent ) {
1122 3936 50       8131 __instance( $parent, __PACKAGE__ ) or return;
1123             weaken(
1124 3936         20865 $parent{$addr} = $parent );
1125             } else {
1126 6409         12891 delete $parent{$addr};
1127             }
1128             }
1129 12796         30242 return $parent{$addr};
1130             }
1131              
1132             sub __parent_keys {
1133 2     2   29 return scalar keys %parent;
1134             }
1135              
1136             }
1137              
1138             # Bless into TOKEN_UNKNOWN, record error message, return 1.
1139             sub __error {
1140 2     2   12 my ( $self, $msg ) = @_;
1141 2 50       7 defined $msg
1142             or $msg = 'Was ' . ref $self;
1143 2         13 TOKEN_UNKNOWN->__PPIX_ELEM__rebless( $self, error => $msg );
1144 2         5 return 1;
1145             }
1146              
1147             # This huge kluge is required by
1148             # https://rt.perl.org/Ticket/Display.html?id=128213 which means the
1149             # deprecation will be done in at least two separate phases. It exists
1150             # for the use of PPIx::Regexp::Token::Literal->perl_version_removed, and
1151             # MUST NOT be called by any other code.
1152             # Note that the perldelta for 5.25.1 and 5.26.0 do not acknowledge tha
1153             # phased deprecation, and pretend that everything was done on the phase
1154             # 1 schedule. This appears to be deliberate per
1155             # https://rt.perl.org/Ticket/Display.html?id=131352
1156             sub __following_literal_left_curly_disallowed_in {
1157 0     0   0 return LITERAL_LEFT_CURLY_REMOVED_PHASE_1;
1158             }
1159              
1160             # Called by the lexer to record the capture number.
1161             sub __PPIX_LEXER__record_capture_number {
1162 968     968   1793 my ( undef, $number ) = @_; # Invocant unused
1163 968         2002 return $number;
1164             }
1165              
1166             # Called by the lexer to rebless
1167             sub __PPIX_ELEM__rebless {
1168 46     46   160 my ( $class, $self, %arg ) = @_;
1169 46   50     134 $self ||= {};
1170 46         117 bless $self, $class;
1171 46         135 delete $self->{error};
1172 46         264 return $self->__PPIX_ELEM__post_reblessing( %arg );
1173             }
1174              
1175             sub __PPIX_ELEM__post_reblessing {
1176 38     38   110 return 0;
1177             }
1178              
1179             sub DESTROY {
1180 6409     6409   73295 $_[0]->_parent( undef );
1181 6409         22244 return;
1182             }
1183              
1184             1;
1185              
1186             __END__