File Coverage

blib/lib/PPIx/Regexp/Token/Recursion.pm
Criterion Covered Total %
statement 25 28 89.2
branch 5 8 62.5
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 44 50 88.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             PPIx::Regexp::Token::Recursion - Represent a recursion
4              
5             =head1 SYNOPSIS
6              
7             use PPIx::Regexp::Dumper;
8             PPIx::Regexp::Dumper->new( 'qr{(foo(?1)?)}smx' )
9             ->print();
10              
11             =head1 INHERITANCE
12              
13             C<PPIx::Regexp::Token::Recursion> is a
14             L<PPIx::Regexp::Token::Reference|PPIx::Regexp::Token::Reference>.
15              
16             C<PPIx::Regexp::Token::Recursion> has no descendants.
17              
18             =head1 DESCRIPTION
19              
20             This class represents a recursion to a named or numbered capture.
21              
22             =head1 METHODS
23              
24             This class provides no public methods beyond those provided by its
25             superclass.
26              
27             =cut
28              
29             package PPIx::Regexp::Token::Recursion;
30              
31 9     9   42 use strict;
  9         16  
  9         244  
32 9     9   28 use warnings;
  9         26  
  9         333  
33              
34 9     9   66 use base qw{ PPIx::Regexp::Token::Reference };
  9         16  
  9         643  
35              
36 9     9   36 use Carp qw{ confess };
  9         14  
  9         340  
37 9     9   31 use PPIx::Regexp::Constant qw{ RE_CAPTURE_NAME @CARP_NOT };
  9         12  
  9         3114  
38              
39             our $VERSION = '0.091';
40              
41             # Return true if the token can be quantified, and false otherwise
42             # sub can_be_quantified { return };
43              
44             sub explain {
45 4     4 1 7 my ( $self ) = @_;
46 4 100       15 $self->is_named()
47             and return sprintf q<Recurse into capture group '%s'>,
48             $self->name();
49 3 50       11 if ( $self->is_relative() ) {
    100          
50 0         0 my $number = $self->number();
51 0 0       0 $number >= 0
52             and return sprintf
53             q<Recurse into %s following capture group (%d in this regexp)>,
54             PPIx::Regexp::Util::__to_ordinal_en( $self->number() ),
55             $self->absolute();
56 0         0 return sprintf
57             q<Back reference to %s previous capture group (%d in this regexp)>,
58             PPIx::Regexp::Util::__to_ordinal_en( - $self->number() ),
59             $self->absolute();
60             } elsif ( my $number = $self->absolute() ) {
61 1         6 return sprintf q<Recurse into capture group %d>, $number;
62             } else {
63 2         4 return q<Recurse to beginning of regular expression>;
64             }
65             }
66              
67             sub perl_version_introduced {
68 8     8 1 1634 return '5.009005';
69             }
70              
71             sub raw_width {
72 1     1 1 3 return ( undef, undef );
73             }
74              
75             sub width {
76 5     5 1 8 return ( undef, undef );
77             }
78              
79             # This must be implemented by tokens which do not recognize themselves.
80             # The return is a list of list references. Each list reference must
81             # contain a regular expression that recognizes the token, and optionally
82             # a reference to a hash to pass to make_token as the class-specific
83             # arguments. The regular expression MUST be anchored to the beginning of
84             # the string.
85             sub __PPIX_TOKEN__recognize {
86             return (
87 16     16   103 [ qr{ \A \( \? (?: ( [-+]? [0-9]+ )) \) }smx, { is_named => 0 } ],
88             [ qr{ \A \( \? (?: R) \) }smx,
89             { is_named => 0, capture => '0' } ],
90 9         1693 [ qr{ \A \( \? (?: & | P> ) ( @{[ RE_CAPTURE_NAME ]} ) \) }smxo,
91             { is_named => 1 } ],
92             );
93             }
94              
95             1;
96              
97             __END__
98              
99             =head1 SUPPORT
100              
101             Support is by the author. Please file bug reports at
102             L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
103             L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in
104             electronic mail to the author.
105              
106             =head1 AUTHOR
107              
108             Thomas R. Wyant, III F<wyant at cpan dot org>
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             Copyright (C) 2009-2023, 2025 by Thomas R. Wyant, III
113              
114             This program is free software; you can redistribute it and/or modify it
115             under the same terms as Perl 5.10.0. For more details, see the full text
116             of the licenses in the directory LICENSES.
117              
118             This program is distributed in the hope that it will be useful, but
119             without any warranty; without even the implied warranty of
120             merchantability or fitness for a particular purpose.
121              
122             =cut
123              
124             # ex: set textwidth=72 :