| 1 | =head1 NAME | ||||||||||||||
| 2 | |||||||||||||||
| 3 | PPIx::Regexp::Token::Backreference - Represent a back reference | ||||||||||||||
| 4 | |||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||
| 6 | |||||||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||||||
| 8 | PPIx::Regexp::Dumper->new( 'qr{(foo|bar)baz\1}smx' ) | ||||||||||||||
| 9 | ->print(); | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 INHERITANCE | ||||||||||||||
| 12 | |||||||||||||||
| 13 | C<PPIx::Regexp::Token::Backreference> is a | ||||||||||||||
| 14 | L<PPIx::Regexp::Token::Reference|PPIx::Regexp::Token::Reference>. | ||||||||||||||
| 15 | |||||||||||||||
| 16 | C<PPIx::Regexp::Token::Backreference> has no descendants. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||
| 19 | |||||||||||||||
| 20 | This class represents back references of all sorts, both the traditional | ||||||||||||||
| 21 | numbered variety and the Perl 5.010 named kind. | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | This class provides no public methods beyond those provided by its | ||||||||||||||
| 26 | superclass. | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =cut | ||||||||||||||
| 29 | |||||||||||||||
| 30 | package PPIx::Regexp::Token::Backreference; | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | 9 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 9 | ▶ | use base qw{ PPIx::Regexp::Token::Reference }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | 9 | ▶ | use Carp qw{ confess }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 9 | ▶ | use PPIx::Regexp::Constant qw{ | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | MINIMUM_PERL | ||||||||||||||
| 40 | RE_CAPTURE_NAME | ||||||||||||||
| 41 | TOKEN_LITERAL | ||||||||||||||
| 42 | TOKEN_UNKNOWN | ||||||||||||||
| 43 | @CARP_NOT | ||||||||||||||
| 44 | 9 | }; | |||||||||||||
| 45 | 9 | ▶ | use PPIx::Regexp::Util qw{ __to_ordinal_en width }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 9 | our $VERSION = '0.092'; | |||||||||||||
| 48 | |||||||||||||||
| 49 | # Return true if the token can be quantified, and false otherwise | ||||||||||||||
| 50 | # sub can_be_quantified { return }; | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 3 | ▶ | sub explain { | ||||||||||||
|
Subroutine
explain: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 53 | 3 | my ( $self ) = @_; | |||||||||||||
| 54 | 3 | ▶ | $self->is_named() | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | and return sprintf q<Back reference to capture group '%s'>, | ||||||||||||||
| 56 | $self->name(); | ||||||||||||||
| 57 | 2 | ▶ | $self->is_relative() | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | and return sprintf | ||||||||||||||
| 59 | q<Back reference to %s previous capture group (%d in this regexp)>, | ||||||||||||||
| 60 | __to_ordinal_en( - $self->number() ), | ||||||||||||||
| 61 | $self->absolute(); | ||||||||||||||
| 62 | 1 | return sprintf q<Back reference to capture group %d>, | |||||||||||||
| 63 | $self->absolute(); | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | { | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 9 | my %perl_version_introduced = ( | |||||||||||||
| 69 | g => '5.009005', # \g1 \g-1 \g{1} \g{-1} | ||||||||||||||
| 70 | k => '5.009005', # \k<name> \k'name' | ||||||||||||||
| 71 | '?' => '5.009005', # (?P=name) (PCRE/Python) | ||||||||||||||
| 72 | ); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 21 | ▶ | sub perl_version_introduced { | ||||||||||||
|
Subroutine
perl_version_introduced: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 75 | 21 | my ( $self ) = @_; | |||||||||||||
| 76 | 21 | ▶ | return $perl_version_introduced{substr( $self->content(), 1, 1 )} || | ||||||||||||
|
Condition100%2/2
$perl_version_introduced{substr $self->content, 1, 1} || '5.000'
MC/DC100%1/1
$perl_version_introduced{substr $self->content, 1, 1} || '5.000'
$perl_version_introduced{substr $self->content, 1, 1}
Truth table100%2/2
A
A= $perl_version_introduced{substr $self->content, 1, 1}
| |||||||||||||||
| 77 | MINIMUM_PERL; | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 9 | ▶ | sub raw_width { | ||||||||||||
|
Subroutine
raw_width: called CC=19 SCAR=30.8
Pod
documented
| |||||||||||||||
| 83 | 30 | my ( $self ) = @_; | |||||||||||||
| 84 | 30 | ▶ | my $re = $self->top() | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | or return ( undef, undef ); # Shouldn't happen. | ||||||||||||||
| 86 | 30 | my @capture; | |||||||||||||
| 87 | 30 | ▶ | if ( $self->is_named() ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 6 | my $name = $self->name(); | |||||||||||||
| 89 | 6 | ▶ | foreach my $elem ( @{ $re->find( | ||||||||||||
|
Condition50%1/2
$re->find('PPIx::Regexp::Structure::NamedCapture') || []
MC/DC0%0/1
$re->find('PPIx::Regexp::Structure::NamedCapture') || []
$re->find('PPIx::Regexp::Structure::NamedCapture')
Truth table50%1/2
A
A= $re->find('PPIx::Regexp::Structure::NamedCapture')
| |||||||||||||||
| 90 | 'PPIx::Regexp::Structure::NamedCapture' ) || [] } ) { | ||||||||||||||
| 91 | 6 | ▶ | $elem->name() eq $name | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | or next; | ||||||||||||||
| 93 | 6 | ▶ | $re->__token_post_order( $elem, $self ) < 0 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | or last; | ||||||||||||||
| 95 | 6 | push @capture, $elem; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | } else { | ||||||||||||||
| 98 | 24 | my $number = $self->absolute(); | |||||||||||||
| 99 | 24 | ▶ | foreach my $elem ( @{ $re->find( | ||||||||||||
|
Condition50%1/2
$re->find('PPIx::Regexp::Structure::Capture') || []
MC/DC0%0/1
$re->find('PPIx::Regexp::Structure::Capture') || []
$re->find('PPIx::Regexp::Structure::Capture')
Truth table50%1/2
A
A= $re->find('PPIx::Regexp::Structure::Capture')
| |||||||||||||||
| 100 | 'PPIx::Regexp::Structure::Capture' ) || [] } ) { | ||||||||||||||
| 101 | 36 | ▶ | $elem->number() == $number | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | or next; | ||||||||||||||
| 103 | 36 | ▶ | $re->__token_post_order( $elem, $self ) < 0 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | or last; | ||||||||||||||
| 105 | 36 | push @capture, $elem; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | } | ||||||||||||||
| 108 | 30 | ▶ | @capture == 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | and return $capture[0]->raw_width(); | ||||||||||||||
| 110 | 12 | my ( $base_min, $base_max ) = $capture[0]->raw_width(); | |||||||||||||
| 111 | 12 | foreach my $elem ( @capture[ 1 .. $#capture ] ) { | |||||||||||||
| 112 | 12 | my ( $ele_min, $ele_max ) = $elem->raw_width(); | |||||||||||||
| 113 | 12 | ▶ | defined $ele_min | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | or $base_min = undef; | ||||||||||||||
| 115 | 12 | ▶ | defined $base_min | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 116 | and $base_min = $base_min == $ele_min ? $base_min : undef; | ||||||||||||||
| 117 | 12 | ▶ | defined $ele_max | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | or $base_max = undef; | ||||||||||||||
| 119 | 12 | ▶ | defined $base_max | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 120 | and $base_max = $base_max == $ele_max ? $base_max : undef; | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | 12 | return ( $base_min, $base_max ); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 9 | my @external = ( # Recognition used externally | |||||||||||||
| 126 | 9 | [ qr{ \A \( \? P = ( @{[ RE_CAPTURE_NAME ]} ) \) }smxo, | |||||||||||||
| 127 | { is_named => 1 }, | ||||||||||||||
| 128 | ], | ||||||||||||||
| 129 | ); | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 9 | my @recognize_regexp = ( # recognition used internally | |||||||||||||
| 132 | [ | ||||||||||||||
| 133 | qr{ \A \\ (?: # numbered (including relative) | ||||||||||||||
| 134 | ( [0-9]+ ) | | ||||||||||||||
| 135 | g (?: ( -? [0-9]+ ) | \{ ( -? [0-9]+ ) \} ) | ||||||||||||||
| 136 | ) | ||||||||||||||
| 137 | }smx, { is_named => 0 }, ], | ||||||||||||||
| 138 | [ | ||||||||||||||
| 139 | qr{ \A \\ (?: # named | ||||||||||||||
| 140 | 9 | g [{] ( @{[ RE_CAPTURE_NAME ]} ) [}] | | |||||||||||||
| 141 | 9 | k (?: \< ( @{[ RE_CAPTURE_NAME ]} ) \> | # named with angles | |||||||||||||
| 142 | 9 | ' ( @{[ RE_CAPTURE_NAME ]} ) ' ) # or quotes | |||||||||||||
| 143 | ) | ||||||||||||||
| 144 | }smxo, { is_named => 1 }, ], | ||||||||||||||
| 145 | ); | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 9 | my %recognize = ( | |||||||||||||
| 148 | regexp => \@recognize_regexp, | ||||||||||||||
| 149 | repl => [ | ||||||||||||||
| 150 | [ qr{ \A \\ ( [0-9]+ ) }smx, { is_named => 0 } ], | ||||||||||||||
| 151 | ], | ||||||||||||||
| 152 | ); | ||||||||||||||
| 153 | |||||||||||||||
| 154 | # This must be implemented by tokens which do not recognize themselves. | ||||||||||||||
| 155 | # The return is a list of list references. Each list reference must | ||||||||||||||
| 156 | # contain a regular expression that recognizes the token, and optionally | ||||||||||||||
| 157 | # a reference to a hash to pass to make_token as the class-specific | ||||||||||||||
| 158 | # arguments. The regular expression MUST be anchored to the beginning of | ||||||||||||||
| 159 | # the string. | ||||||||||||||
| 160 | 18 | ▶ | sub __PPIX_TOKEN__recognize { | ||||||||||||
|
Subroutine
__PPIX_TOKEN__recognize: called CC=2 SCAR=6.9
| |||||||||||||||
| 161 | 18 | ▶ | return __PACKAGE__->isa( scalar caller ) ? | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | ( @external, @recognize_regexp ) : | ||||||||||||||
| 163 | ( @external ); | ||||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | 110 | ▶ | sub __PPIX_TOKENIZER__regexp { | ||||||||||||
|
Subroutine
__PPIX_TOKENIZER__regexp: called CC=4 SCAR=13.9
| |||||||||||||||
| 167 | 110 | my ( undef, $tokenizer, $character ) = @_; | |||||||||||||
| 168 | |||||||||||||||
| 169 | # PCRE/Python back references are handled in | ||||||||||||||
| 170 | # PPIx::Regexp::Token::Structure, because they are parenthesized. | ||||||||||||||
| 171 | |||||||||||||||
| 172 | # All the other styles are escaped. | ||||||||||||||
| 173 | 110 | ▶ | $character eq '\\' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | or return; | ||||||||||||||
| 175 | |||||||||||||||
| 176 | 46 | foreach ( @{ $recognize{$tokenizer->get_mode()} } ) { | |||||||||||||
| 177 | 58 | my ( $re, $arg ) = @{ $_ }; | |||||||||||||
| 178 | 58 | ▶ | my $accept = $tokenizer->find_regexp( $re ) or next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 179 | 39 | my %arg = ( %{ $arg }, tokenizer => $tokenizer ); | |||||||||||||
| 180 | 39 | return $tokenizer->make_token( $accept, __PACKAGE__, \%arg ); | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | |||||||||||||||
| 183 | 7 | return; | |||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | 13 | ▶ | sub __PPIX_TOKENIZER__repl { | ||||||||||||
|
Subroutine
__PPIX_TOKENIZER__repl: called CC=2 SCAR=7.1
| |||||||||||||||
| 187 | 13 | my ( undef, $tokenizer ) = @_; # Invocant, $character unused | |||||||||||||
| 188 | |||||||||||||||
| 189 | 13 | ▶ | $tokenizer->interpolates() | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | or return; | ||||||||||||||
| 191 | |||||||||||||||
| 192 | 13 | goto &__PPIX_TOKENIZER__regexp; | |||||||||||||
| 193 | } | ||||||||||||||
| 194 | |||||||||||||||
| 195 | # Called by the lexer to disambiguate between captures, literals, and | ||||||||||||||
| 196 | # whatever. We have to return the number of tokens reblessed to | ||||||||||||||
| 197 | # TOKEN_UNKNOWN (i.e. either 0 or 1) because we get called after the | ||||||||||||||
| 198 | # parse is finalized. | ||||||||||||||
| 199 | 25 | ▶ | sub __PPIX_LEXER__rebless { | ||||||||||||
|
Subroutine
__PPIX_LEXER__rebless: called CC=6 SCAR=18.6
| |||||||||||||||
| 200 | 25 | my ( $self, %arg ) = @_; | |||||||||||||
| 201 | |||||||||||||||
| 202 | # Handle named back references | ||||||||||||||
| 203 | 25 | ▶ | if ( $self->is_named() ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 204 | 8 | ▶ | $arg{capture_name}{$self->name()} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | and return 0; | ||||||||||||||
| 206 | 0 | return $self->__error(); | |||||||||||||
| 207 | } | ||||||||||||||
| 208 | |||||||||||||||
| 209 | # Get the absolute capture group number. | ||||||||||||||
| 210 | 17 | my $absolute = $self->absolute(); | |||||||||||||
| 211 | |||||||||||||||
| 212 | # If it is zero or negative, we have a relateive reference to a | ||||||||||||||
| 213 | # non-existent capture group. | ||||||||||||||
| 214 | 17 | ▶ | $absolute <= 0 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 215 | and return $self->__error(); | ||||||||||||||
| 216 | |||||||||||||||
| 217 | # If the absolute number is less than or equal to the maximum | ||||||||||||||
| 218 | # capture group number, we are good. | ||||||||||||||
| 219 | $absolute <= $arg{max_capture} | ||||||||||||||
| 220 | 17 | ▶ | and return 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 221 | |||||||||||||||
| 222 | # It's not a valid capture. If it's an octal literal, rebless it so. | ||||||||||||||
| 223 | # Note that we can't rebless single-digit numbers, since they can't | ||||||||||||||
| 224 | # be octal literals. | ||||||||||||||
| 225 | 1 | my $content = $self->content(); | |||||||||||||
| 226 | 1 | ▶ | if ( $content =~ m/ \A \\ [0-7]{2,} \z /smx ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 227 | 1 | TOKEN_LITERAL->__PPIX_ELEM__rebless( $self ); | |||||||||||||
| 228 | 1 | return 0; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | |||||||||||||||
| 231 | # Anything else is an error. | ||||||||||||||
| 232 | 0 | return $self->__error(); | |||||||||||||
| 233 | } | ||||||||||||||
| 234 | |||||||||||||||
| 235 | 0 | ▶ | sub __error { | ||||||||||||
|
Subroutine
__error: not called CC=2 SCAR=17.9
| |||||||||||||||
| 236 | 0 | my ( $self, $msg ) = @_; | |||||||||||||
| 237 | 0 | ▶ | defined $msg | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 238 | or $msg = 'No corresponding capture group'; | ||||||||||||||
| 239 | 0 | TOKEN_UNKNOWN->__PPIX_ELEM__rebless( $self, error => $msg ); | |||||||||||||
| 240 | 0 | return 1; | |||||||||||||
| 241 | } | ||||||||||||||
| 242 | |||||||||||||||
| 243 | 9 | 1; | |||||||||||||
| 244 | |||||||||||||||
| 245 | __END__ | ||||||||||||||