| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | PPIx::Regexp::Token::Interpolation - Represent an interpolation in the PPIx::Regexp package. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | PPIx::Regexp::Dumper->new('qr{$foo}smx')->print(); | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 INHERITANCE | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | C<PPIx::Regexp::Token::Interpolation> is a | ||||||||||||||||||||||||||||||||||||||||||
| 13 | L<PPIx::Regexp::Token::Code|PPIx::Regexp::Token::Code>. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | C<PPIx::Regexp::Token::Interpolation> has no descendants. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | This class represents a variable interpolation into a regular | ||||||||||||||||||||||||||||||||||||||||||
| 20 | expression. In the L</SYNOPSIS> the C<$foo> would be represented by an | ||||||||||||||||||||||||||||||||||||||||||
| 21 | object of this class. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head2 Incompatible Change: Parse of '@{[ ... ]}' | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | In versions 0.083 and earlier, C<'@{[ ... ]}'> parsed as a member of | ||||||||||||||||||||||||||||||||||||||||||
| 26 | this class. In 0.084 and later, it parses as a member of | ||||||||||||||||||||||||||||||||||||||||||
| 27 | C<PPIx::Regexp::Token::Code>. This more accurately reflects the actual | ||||||||||||||||||||||||||||||||||||||||||
| 28 | contents of the token, and enables the recognition of the full range of | ||||||||||||||||||||||||||||||||||||||||||
| 29 | postfix dereference operators for versioning purposes, not just those | ||||||||||||||||||||||||||||||||||||||||||
| 30 | valid in interpolations. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | This class provides the following public methods beyond those provided | ||||||||||||||||||||||||||||||||||||||||||
| 35 | by its superclass. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | package PPIx::Regexp::Token::Interpolation; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 9 | ▶ | use base qw{ PPIx::Regexp::Token::Code }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | ▶ | use Carp qw{ confess }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | use PPIx::Regexp::Constant qw{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | COOKIE_CLASS | ||||||||||||||||||||||||||||||||||||||||||
| 49 | COOKIE_REGEX_SET | ||||||||||||||||||||||||||||||||||||||||||
| 50 | MINIMUM_PERL | ||||||||||||||||||||||||||||||||||||||||||
| 51 | TOKEN_LITERAL | ||||||||||||||||||||||||||||||||||||||||||
| 52 | @CARP_NOT | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | our $VERSION = '0.092'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 9 | ▶ | use constant VERSION_WHEN_IN_REGEX_SET => '5.017009'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 105 | ▶ | sub __new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__new: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 105 | my ( $class, $content, %arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | defined $arg{perl_version_introduced} | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 105 | ▶ | or $arg{perl_version_introduced} = MINIMUM_PERL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 105 | my $self = $class->SUPER::__new( $content, %arg ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 105 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =head2 is_matcher | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | This method returns C<undef> because a static analysis can not in | ||||||||||||||||||||||||||||||||||||||||||
| 73 | general tell whether a piece of code matches anything. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub is_matcher { return undef; } ## no critic (ProhibitExplicitReturnUndef) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_matcher: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # Return true if the token can be quantified, and false otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # This can be quantified because it might interpolate a quantifiable | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # token. Of course, it might not, but we need to be permissive here. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # sub can_be_quantified { return }; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # We overrode this in PPIx::Regexp::Token::Code, since (?{...}) did not | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # appear until Perl 5.5. But interpolation has been there since the | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # beginning, so we have to override again. This turns out to be OK, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # though, because while Regex Sets were introduced in 5.17.8, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # interpolation inside them was not introduced until 5.17.9. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 17 | ▶ | sub perl_version_introduced { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
perl_version_introduced: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 17 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 17 | return $self->{perl_version_introduced}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # Normalize the content of an interpolation object before making it into | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # a PPI document. The issue here is that things like ${x} are at least | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # warnings outside strings, but are normal inside them. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 115 | ▶ | sub __ppi_normalize_content { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ppi_normalize_content: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 115 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 115 | my $content; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 115 | ▶ | defined( $content = $self->content() ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | or return $content; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # NOTE: perldata gives a regexp for this, but it requires Perl 5.10. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # I believe the following caputures the intent, except possibly for | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # various weird combinations of '::' and "'". | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 115 | $content =~ | |||||||||||||||||||||||||||||||||||||||||
| 106 | s/ \A | ||||||||||||||||||||||||||||||||||||||||||
| 107 | ( \$ \# \$* | [\@\$] \$* ) # Sigil and possible casts | ||||||||||||||||||||||||||||||||||||||||||
| 108 | [{] \s* (?: :: )* '? # per perldata | ||||||||||||||||||||||||||||||||||||||||||
| 109 | ( ^? (?: \w+ (?: (?: :: | ' ) \w+ )* (?: :: )? | [[:punct:]] ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | \s* [}] \z | ||||||||||||||||||||||||||||||||||||||||||
| 111 | /$1$2/smx; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 115 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # Match the beginning of an interpolation. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | my $interp_re = | |||||||||||||||||||||||||||||||||||||||||
| 118 | qr{ \A (?= [\@\$]? \$ [-\w&`'+^./\\";%=~:?!\@\$<>\[\]\{\},#] | | ||||||||||||||||||||||||||||||||||||||||||
| 119 | \@ [\w\{] ) | ||||||||||||||||||||||||||||||||||||||||||
| 120 | }smx; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | # Match bracketed interpolation | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 9 | my $brkt_interp_re = | |||||||||||||||||||||||||||||||||||||||||
| 125 | qr{ \A (?: [\@\$] \$* [#]? \$* [\{] (?: [][\-&`'+,^./\\";%=:?\@\$<>,#] | | ||||||||||||||||||||||||||||||||||||||||||
| 126 | \^? \w+ (?: :: \w+ )* ) [\}] | | ||||||||||||||||||||||||||||||||||||||||||
| 127 | \@ [\{] \w+ (?: :: \w+ )* [\}] ) | ||||||||||||||||||||||||||||||||||||||||||
| 128 | }smx; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # We pull out the logic of finding and dealing with the interpolation | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # into a separate subroutine because if we fail to find an interpolation | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # we want to do something with the sigils. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | my %allow_subscript_based_on_cast_symbol = ( | |||||||||||||||||||||||||||||||||||||||||
| 135 | q<$#> => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | q<$> => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | q<@> => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 110 | ▶ | sub _interpolation { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_interpolation: called CC=31 SCAR=36.1
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 110 | my ( $class, $tokenizer, undef, $in_regexp ) = @_; # $character unused | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # If the regexp does not interpolate, bail now. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 110 | ▶ | $tokenizer->interpolates() or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | # If we're a bracketed interpolation, just accept it | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 108 | ▶ | if ( my $len = $tokenizer->find_regexp( $brkt_interp_re ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 8 | return $len; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | # Make sure we start off plausibly | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 100 | ▶ | defined $tokenizer->find_regexp( $interp_re ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # See if PPI can figure out what we have | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 83 | ▶ | my $doc = $tokenizer->ppi_document() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # Get the first statement to work on. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 83 | ▶ | my $stmt = $doc->find_first( 'PPI::Statement' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 83 | my @accum; # The elements of the interpolation | |||||||||||||||||||||||||||||||||||||||||
| 164 | my $allow_subscript; # Assume no subscripts allowed | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 83 | my $want_class = __PACKAGE__; # Assume we want an interpolation. | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | # Find the beginning of the interpolation | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 83 | ▶ | my $next = $stmt->schild( 0 ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | # The interpolation should start with | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 83 | ▶ | if ( $next->isa( 'PPI::Token::Symbol' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # A symbol | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 73 | push @accum, $next; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 73 | $allow_subscript = 1; # Subscripts are allowed | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | } elsif ( $next->isa( 'PPI::Token::Cast' ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # Or a cast followed by a block | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | push @accum, $next; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | ▶ | $next = $next->next_sibling() or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | ▶ | if ( $next->isa( 'PPI::Token::Symbol' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | defined ( | ||||||||||||||||||||||||||||||||||||||||||
| 184 | $allow_subscript = | ||||||||||||||||||||||||||||||||||||||||||
| 185 | $allow_subscript_based_on_cast_symbol{ | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 6 | ▶ | $accum[-1]->content() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | ) or return; | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 6 | push @accum, $next; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } elsif ( $next->isa( 'PPI::Structure::Block' ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # We want @{[ ... ]} to parse as a PPIx::Regexp::Token::Code. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # PPI parses this as a cast followed by a block. The block | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # contains a single statement, which contains a single | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # constructor. So: | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | my @kids = $next->schildren(); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | ▶ | if ( @kids == 1 && $kids[0]->isa( 'PPI::Statement' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@kids == 1 and $kids[0]->isa("PPI::Statement")
MC/DC0%0/2
@kids == 1 and $kids[0]->isa("PPI::Statement")
@kids == 1
$kids[0]->isa("PPI::Statement")
Truth table33%1/3
A and B
A= @kids == 1B= $kids[0]->isa("PPI::Statement")
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | @kids = $kids[0]->schildren(); | |||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | if ( @kids == 1 && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@kids == 1 and $kids[0]->isa("PPI::Structure::Constructor") and $kids[0]->start eq "["
@kids == 1 and $kids[0]->isa("PPI::Structure::Constructor")
MC/DC0%0/3
@kids == 1 and $kids[0]->isa("PPI::Structure::Constructor") and $kids[0]->start eq "["
@kids == 1
$kids[0]->isa("PPI::Structure::Constructor")
$kids[0]->start eq "["
Truth table25%1/4
A and B and C
A= @kids == 1B= $kids[0]->isa("PPI::Structure::Constructor")C= $kids[0]->start eq "["
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | $kids[0]->isa( 'PPI::Structure::Constructor' ) && | ||||||||||||||||||||||||||||||||||||||||||
| 200 | $kids[0]->start() eq '[' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 2 | $want_class = 'PPIx::Regexp::Token::Code'; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 2 | push @accum, $next; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | } elsif ( $next->isa( 'PPI::Token::ArrayIndex' ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # Or an array index | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | push @accum, $next; | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | # None others need apply. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | # The interpolation _may_ be subscripted. If so ... | ||||||||||||||||||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | # Only accept a subscript if wanted and available | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 83 | ▶ | $allow_subscript and $next = $next->snext_sibling() or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$allow_subscript and $next = $next->snext_sibling
MC/DC100%2/2
$allow_subscript and $next = $next->snext_sibling
$allow_subscript
$next = $next->snext_sibling
Truth table100%3/3
A and B
A= $allow_subscriptB= $next = $next->snext_sibling
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | # Accept an optional dereference operator. | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 63 | my @subscr; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 63 | ▶ | if ( $next->isa( 'PPI::Token::Operator' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 18 | ▶ | $next->content() eq '->' or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 4 | push @subscr, $next; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 4 | ▶ | $next = $next->next_sibling() or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | # postderef was introduced in 5.19.5, per perl5195delta. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 4 | ▶ | if ( my $deref = $tokenizer->__recognize_postderef( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | __PACKAGE__, $next ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 4 | push @accum, @subscr, $deref; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | # Accept only a subscript | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 45 | ▶ | $next->isa( 'PPI::Structure::Subscript' ) or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | # The subscript must have a closing delimiter. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 25 | ▶ | $next->finish() or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # If we are in a regular expression rather than a replacement | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # string, screen the subscript for content, since [] could be a | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # character class, and {} could be a quantifier. The perlop docs | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # say that Perl applies undocumented heuristics subject to | ||||||||||||||||||||||||||||||||||||||||||
| 252 | # change without notice to figure this out. So we do our poor | ||||||||||||||||||||||||||||||||||||||||||
| 253 | # best to be heuristical and undocumented. | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 25 | ▶ | not $in_regexp or $class->_subscript( $next ) or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $in_regexp or $class->_subscript($next)
MC/DC100%2/2
not $in_regexp or $class->_subscript($next)
not $in_regexp
$class->_subscript($next)
Truth table100%3/3
not(A) or B
A= not $in_regexpB= $class->_subscript($next)
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | # If we got this far, accept the subscript and try for another | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # one. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 23 | push @accum, @subscr, $next; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 23 | redo; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # Compute the length of all the PPI elements accumulated, and return | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # it. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 83 | my $length = 0; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 83 | foreach ( @accum ) { | |||||||||||||||||||||||||||||||||||||||||
| 266 | 122 | ▶ | $length += ref $_ ? length $_->content() : $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 83 | return ( $length, $want_class ); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 9 | ▶ | no warnings qw{ qw }; ## no critic (ProhibitNoWarnings) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 9 | my %accept = map { $_ => 1 } qw{ $ $# @ }; | |||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 108 | ▶ | sub __postderef_accept_cast { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__postderef_accept_cast: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 108 | return \%accept; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 9 | my %allowed = ( | |||||||||||||||||||||||||||||||||||||||||
| 284 | '[' => '_square', | ||||||||||||||||||||||||||||||||||||||||||
| 285 | '{' => '_curly', | ||||||||||||||||||||||||||||||||||||||||||
| 286 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 21 | ▶ | sub _subscript { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_subscript: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 21 | my ( $class, $struct ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # We expect to have a left delimiter, which is either a '[' or a | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # '{'. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 21 | ▶ | my $left = $struct->start() or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 21 | my $lc = $left->content(); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 21 | ▶ | my $handler = $allowed{$lc} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | # We expect a single child, which is a PPI::Statement | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 21 | ▶ | ( my @kids = $struct->schildren() ) == 1 or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 21 | ▶ | $kids[0]->isa( 'PPI::Statement' ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | # We expect the statement to have at least one child. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 21 | ▶ | ( @kids = $kids[0]->schildren() ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 21 | return $class->$handler( @kids ); | |||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | # Return true if we think a curly-bracketed subscript is really a | ||||||||||||||||||||||||||||||||||||||||||
| 311 | # subscript, rather than a quantifier. | ||||||||||||||||||||||||||||||||||||||||||
| 312 | # Called as $class->$handler( ... ) above | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 9 | ▶ | sub _curly { ## no critic (ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_curly: called CC=10 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 17 | my ( undef, @kids ) = @_; # Invocant unused | |||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | # If the first child is a word, and either it is an only child or | ||||||||||||||||||||||||||||||||||||||||||
| 317 | # the next child is the fat comma operator, we accept it as a | ||||||||||||||||||||||||||||||||||||||||||
| 318 | # subscript. | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 17 | ▶ | if ( $kids[0]->isa( 'PPI::Token::Word' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 14 | ▶ | @kids == 1 and return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 4 | ▶ | $kids[1]->isa( 'PPI::Token::Operator' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$kids[1]->isa("PPI::Token::Operator") and $kids[1]->content eq "=>"
MC/DC0%0/2
$kids[1]->isa("PPI::Token::Operator") and $kids[1]->content eq "=>"
$kids[1]->isa("PPI::Token::Operator")
$kids[1]->content eq "=>"
Truth table33%1/3
A and B
A= $kids[1]->isa("PPI::Token::Operator")B= $kids[1]->content eq "=>"
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | and $kids[1]->content() eq '=>' | ||||||||||||||||||||||||||||||||||||||||||
| 323 | and return 1; | ||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | # If the first child is a symbol, | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 3 | ▶ | if ( @kids && $kids[0]->isa( 'PPI::Token::Symbol' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@kids and $kids[0]->isa("PPI::Token::Symbol")
MC/DC50%1/2
@kids and $kids[0]->isa("PPI::Token::Symbol")
@kids
$kids[0]->isa("PPI::Token::Symbol")
Truth table66%2/3
A and B
A= @kidsB= $kids[0]->isa("PPI::Token::Symbol")
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | # Accept it if it is the only child | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 1 | ▶ | @kids == 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | and return 1; | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # Accept it if there are exactly two children and the second is | ||||||||||||||||||||||||||||||||||||||||||
| 332 | # a subscript. | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | ▶ | @kids == 2 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@kids == 2 and $kids[1]->isa("PPI::Structure::Subscript")
MC/DC0%0/2
@kids == 2 and $kids[1]->isa("PPI::Structure::Subscript")
@kids == 2
$kids[1]->isa("PPI::Structure::Subscript")
Truth table33%1/3
A and B
A= @kids == 2B= $kids[1]->isa("PPI::Structure::Subscript")
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | and $kids[1]->isa( 'PPI::Structure::Subscript' ) | ||||||||||||||||||||||||||||||||||||||||||
| 335 | and return 1; | ||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | # We reject anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | # Return true if we think a square-bracketed subscript is really a | ||||||||||||||||||||||||||||||||||||||||||
| 343 | # subscript, rather than a character class. | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # Called as $class->$handler( ... ) above | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 4 | ▶ | sub _square { ## no critic (ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_square: called CC=3 SCAR=16.5
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 4 | my ( undef, @kids ) = @_; # Invocant unused | |||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | # We expect to have either a number or a symbol as the first | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # element. | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 4 | ▶ | $kids[0]->isa( 'PPI::Token::Number' ) and return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | $kids[0]->isa( 'PPI::Token::Symbol' ) and return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | # Anything else is rejected. | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | # Alternate classes for the sigils, depending on whether we are in a | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # character class (index 1) or not (index 0). | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 9 | my %sigil_alternate = ( | |||||||||||||||||||||||||||||||||||||||||
| 360 | '$' => [ 'PPIx::Regexp::Token::Assertion', TOKEN_LITERAL ], | ||||||||||||||||||||||||||||||||||||||||||
| 361 | '@' => [ TOKEN_LITERAL, TOKEN_LITERAL ], | ||||||||||||||||||||||||||||||||||||||||||
| 362 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 1434 | ▶ | sub __PPIX_TOKENIZER__regexp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__PPIX_TOKENIZER__regexp: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | 1434 | my ( $class, $tokenizer, $character ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 1434 | ▶ | exists $sigil_alternate{$character} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 105 | ▶ | if ( my ( $accept, $want_class ) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | $class->_interpolation( $tokenizer, $character, 1 ) | ||||||||||||||||||||||||||||||||||||||||||
| 371 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 86 | return $tokenizer->make_token( $accept, $want_class ); | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 19 | ▶ | my $alternate = $sigil_alternate{$character} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 19 | ▶ | return $tokenizer->make_token( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 1, $alternate->[$tokenizer->cookie( COOKIE_CLASS ) ? 1 : 0 ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 18 | ▶ | sub __PPIX_TOKENIZER__repl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__PPIX_TOKENIZER__repl: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 18 | my ( $class, $tokenizer, $character ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | 18 | ▶ | exists $sigil_alternate{$character} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 5 | ▶ | if ( my ( $accept, $want_class ) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | $class->_interpolation( $tokenizer, $character, 0 ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 5 | return $tokenizer->make_token( $accept, $want_class ); | |||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | return $tokenizer->make_token( 1, TOKEN_LITERAL ); | |||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | __END__ | ||||||||||||||||||||||||||||||||||||||||||