/usr/share/perl5/PPIx/Regexp/Token/Interpolation.pm is in libppix-regexp-perl 0.050-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | =head1 NAME
PPIx::Regexp::Token::Interpolation - Represent an interpolation in the PPIx::Regexp package.
=head1 SYNOPSIS
use PPIx::Regexp::Dumper;
PPIx::Regexp::Dumper->new('qr{$foo}smx')->print();
=head1 INHERITANCE
C<PPIx::Regexp::Token::Interpolation> is a
L<PPIx::Regexp::Token::Code|PPIx::Regexp::Token::Code>.
C<PPIx::Regexp::Token::Interpolation> has no descendants.
=head1 DESCRIPTION
This class represents a variable interpolation into a regular
expression. In the L</SYNOPSIS> the C<$foo> would be represented by an
object of this class.
=head1 METHODS
This class provides the following public methods beyond those provided
by its superclass.
=cut
package PPIx::Regexp::Token::Interpolation;
use strict;
use warnings;
use base qw{ PPIx::Regexp::Token::Code };
use Carp qw{ confess };
use PPI::Document;
use PPIx::Regexp::Constant qw{
COOKIE_CLASS COOKIE_REGEX_SET TOKEN_LITERAL MINIMUM_PERL
};
our $VERSION = '0.050';
use constant VERSION_WHEN_IN_REGEX_SET => '5.017009';
sub __new {
my ( $class, $content, %arg ) = @_;
defined $arg{perl_version_introduced}
or $arg{perl_version_introduced} = MINIMUM_PERL;
my $self = $class->SUPER::__new( $content, %arg );
return $self;
}
# Return true if the token can be quantified, and false otherwise
# This can be quantified because it might interpolate a quantifiable
# token. Of course, it might not, but we need to be permissive here.
# sub can_be_quantified { return };
# We overrode this in PPIx::Regexp::Token::Code, since (?{...}) did not
# appear until Perl 5.5. But interpolation has been there since the
# beginning, so we have to override again. This turns out to be OK,
# though, because while Regex Sets were introduced in 5.17.8,
# interpolation inside them was not introduced until 5.17.9.
sub perl_version_introduced {
my ( $self ) = @_;
return $self->{perl_version_introduced};
}
=head2 ppi
This convenience method returns the L<PPI::Document|PPI::Document>
representing the content. This document should be considered read only.
Note that the content of the returned L<PPI::Document|PPI::Document> may
not be the same as the content of the original
C<PPIx::Regexp::Token::Interpolation>. This can happen because
interpolated variable names may be enclosed in curly brackets, but this
does not happen in normal code. For example, in C</${foo}bar/>, the
content of the C<PPIx::Regexp::Token::Interpolation> object will be
C<'${foo}'>, but the content of the C<PPI::Document> will be C<'$foo'>.
=cut
sub ppi {
my ( $self ) = @_;
if ( exists $self->{ppi} ) {
return $self->{ppi};
} elsif ( exists $self->{content} ) {
( my $code = $self->{content} ) =~
s/ \A ( [\@\$] ) [{] ( .* ) [}] \z /$1$2/smx;
return ( $self->{ppi} = PPI::Document->new(
\$code, readonly => 1 ) );
} else {
return;
}
}
# Match the beginning of an interpolation.
my $interp_re =
qr{ \A (?= [\@\$]? \$ [-\w&`'+^./\\";%=~:?!\@\$<>\[\]\{\},#] |
\@ [\w\{] )
}smx;
# Match bracketed interpolation
my $brkt_interp_re =
qr{ \A (?: [\@\$] \$* [#]? \$* [\{] (?: [][\-&`'+,^./\\";%=:?\@\$<>,#] |
\^? \w+ (?: :: \w+ )* ) [\}] |
\@ [\{] \w+ (?: :: \w+ )* [\}] )
}smx;
# We pull out the logic of finding and dealing with the interpolation
# into a separate subroutine because if we fail to find an interpolation
# we want to do something with the sigils.
my %allow_subscript_based_on_cast_symbol = (
q<$#> => 0,
q<$> => 1,
q<@> => 1,
);
sub _interpolation {
my ( $class, $tokenizer, undef, $in_regexp ) = @_; # $character unused
# If the regexp does not interpolate, bail now.
$tokenizer->interpolates() or return;
# If we're a bracketed interpolation, just accept it
if ( my $len = $tokenizer->find_regexp( $brkt_interp_re ) ) {
return $len;
}
# Make sure we start off plausibly
defined $tokenizer->find_regexp( $interp_re )
or return;
# See if PPI can figure out what we have
my $doc = $tokenizer->ppi_document()
or return;
# Get the first statement to work on.
my $stmt = $doc->find_first( 'PPI::Statement' )
or return;
my @accum; # The elements of the interpolation
my $allow_subscript; # Assume no subscripts allowed
# Find the beginning of the interpolation
my $next = $stmt->schild( 0 ) or return;
# The interpolation should start with
if ( $next->isa( 'PPI::Token::Symbol' ) ) {
# A symbol
push @accum, $next;
$allow_subscript = 1; # Subscripts are allowed
} elsif ( $next->isa( 'PPI::Token::Cast' ) ) {
# Or a cast followed by a block
push @accum, $next;
$next = $next->next_sibling() or return;
if ( $next->isa( 'PPI::Token::Symbol' ) ) {
defined (
$allow_subscript =
$allow_subscript_based_on_cast_symbol{
$accum[-1]->content()
}
) or return;
push @accum, $next;
} elsif ( $next->isa( 'PPI::Structure::Block' ) ) {
push @accum, $next;
} else {
return;
}
} elsif ( $next->isa( 'PPI::Token::ArrayIndex' ) ) {
# Or an array index
push @accum, $next;
} else {
# None others need apply.
return;
}
# The interpolation _may_ be subscripted. If so ...
{
# Only accept a subscript if wanted and available
$allow_subscript and $next = $next->snext_sibling() or last;
# Accept an optional dereference operator.
my @subscr;
if ( $next->isa( 'PPI::Token::Operator' ) ) {
$next->content() eq '->' or last;
push @subscr, $next;
$next = $next->next_sibling() or last;
# postderef was introduced in 5.19.5, per perl5195delta.
if ( my $deref = $tokenizer->__recognize_postderef(
__PACKAGE__, $next ) ) {
push @accum, @subscr, $deref;
last;
}
}
# Accept only a subscript
$next->isa( 'PPI::Structure::Subscript' ) or last;
# The subscript must have a closing delimiter.
$next->finish() or last;
# If we are in a regular expression rather than a replacement
# string, screen the subscript for content, since [] could be a
# character class, and {} could be a quantifier. The perlop docs
# say that Perl applies undocumented heuristics subject to
# change without notice to figure this out. So we do our poor
# best to be heuristical and undocumented.
not $in_regexp or $class->_subscript( $next ) or last;
# If we got this far, accept the subscript and try for another
# one.
push @accum, @subscr, $next;
redo;
}
# Compute the length of all the PPI elements accumulated, and return
# it.
my $length = 0;
foreach ( @accum ) {
$length += ref $_ ? length $_->content() : $_;
}
return $length;
}
{
no warnings qw{ qw }; ## no critic (ProhibitNoWarnings)
my %accept = map { $_ => 1 } qw{ $ $# @ };
sub __postderef_accept_cast {
return \%accept;
}
}
{
my %allowed = (
'[' => '_square',
'{' => '_curly',
);
sub _subscript {
my ( $class, $struct ) = @_;
# We expect to have a left delimiter, which is either a '[' or a
# '{'.
my $left = $struct->start() or return;
my $lc = $left->content();
my $handler = $allowed{$lc} or return;
# We expect a single child, which is a PPI::Statement
( my @kids = $struct->schildren() ) == 1 or return;
$kids[0]->isa( 'PPI::Statement' ) or return;
# We expect the statement to have at least one child.
( @kids = $kids[0]->schildren() ) or return;
return $class->$handler( @kids );
}
}
# Return true if we think a curly-bracketed subscript is really a
# subscript, rather than a quantifier.
# Called as $class->$handler( ... ) above
sub _curly { ## no critic (ProhibitUnusedPrivateSubroutines)
my ( undef, @kids ) = @_; # Invocant unused
# If the first child is a word, and either it is an only child or
# the next child is the fat comma operator, we accept it as a
# subscript.
if ( $kids[0]->isa( 'PPI::Token::Word' ) ) {
@kids == 1 and return 1;
$kids[1]->isa( 'PPI::Token::Operator' )
and $kids[1]->content() eq '=>'
and return 1;
}
# If the first child is a symbol,
if ( @kids && $kids[0]->isa( 'PPI::Token::Symbol' ) ) {
# Accept it if it is the only child
@kids == 1
and return 1;
# Accept it if there are exactly two children and the second is
# a subscript.
@kids == 2
and $kids[1]->isa( 'PPI::Structure::Subscript' )
and return 1;
}
# We reject anything else.
return;
}
# Return true if we think a square-bracketed subscript is really a
# subscript, rather than a character class.
# Called as $class->$handler( ... ) above
sub _square { ## no critic (ProhibitUnusedPrivateSubroutines)
my ( undef, @kids ) = @_; # Invocant unused
# We expect to have either a number or a symbol as the first
# element.
$kids[0]->isa( 'PPI::Token::Number' ) and return 1;
$kids[0]->isa( 'PPI::Token::Symbol' ) and return 1;
# Anything else is rejected.
return;
}
# Alternate classes for the sigils, depending on whether we are in a
# character class (index 1) or not (index 0).
my %sigil_alternate = (
'$' => [ 'PPIx::Regexp::Token::Assertion', TOKEN_LITERAL ],
'@' => [ TOKEN_LITERAL, TOKEN_LITERAL ],
);
sub __PPIX_TOKENIZER__regexp {
my ( $class, $tokenizer, $character ) = @_;
exists $sigil_alternate{$character} or return;
if ( my $accept = $class->_interpolation( $tokenizer, $character, 1 ) ) {
return $accept;
}
my $alternate = $sigil_alternate{$character} or return;
return $tokenizer->make_token(
1, $alternate->[$tokenizer->cookie( COOKIE_CLASS ) ? 1 : 0 ] );
}
sub __PPIX_TOKENIZER__repl {
my ( $class, $tokenizer, $character ) = @_;
exists $sigil_alternate{$character} or return;
if ( my $accept = $class->_interpolation( $tokenizer, $character, 0 ) ) {
return $accept;
}
return $tokenizer->make_token( 1, TOKEN_LITERAL );
}
1;
__END__
=begin comment
Interpolation notes:
$ perl -E '$foo = "\\w"; $bar = 3; say qr{$foo{$bar}}'
(?-xism:)
white2:~/Code/perl/PPIx-Regexp.new tom 22:50:33
$ perl -E '$foo = "\\w"; $bar = 3; say qr{foo{$bar}}'
(?-xism:foo{3})
white2:~/Code/perl/PPIx-Regexp.new tom 22:50:59
$ perl -E '$foo = "\\w"; $bar = 3; %foo = {baz => 42}; say qr{$foo{$bar}}'
(?-xism:)
white2:~/Code/perl/PPIx-Regexp.new tom 22:51:38
$ perl -E '$foo = "\\w"; $bar = 3; %foo = {baz => 42}; say qr{$foo}'
(?-xism:\w)
white2:~/Code/perl/PPIx-Regexp.new tom 22:51:50
$ perl -E '$foo = "\\w"; $bar = 3; %foo = {baz => 42}; say qr{$foo{baz}}'
(?-xism:)
white2:~/Code/perl/PPIx-Regexp.new tom 22:52:49
$ perl -E '$foo = "\\w"; $bar = 3; %foo = {baz => 42}; say qr{${foo}{baz}}'
(?-xism:\w{baz})
white2:~/Code/perl/PPIx-Regexp.new tom 22:54:07
$ perl -E '$foo = "\\w"; $bar = 3; %foo = {baz => 42}; say qr{${foo}{$bar}}'
(?-xism:\w{3})
The above makes me think that Perl is extremely reluctant to understand
an interpolation followed by curlys as a hash dereference. In fact, only
when the interpolation was what PPI calls a block was it understood at
all.
$ perl -E '$foo = { bar => 42 }; say qr{$foo->{bar}};'
(?-xism:42)
$ perl -E '$foo = { bar => 42 }; say qr{$foo->{baz}};'
(?-xism:)
On the other hand, Perl seems to be less reluctant to accept an explicit
dereference as a hash dereference.
$ perl -E '$foo = "\\w"; $bar = 3; @foo = (42); say qr{$foo}'
(?-xism:\w)
white2:~/Code/perl/PPIx-Regexp.new tom 22:58:20
$ perl -E '$foo = "\\w"; $bar = 3; @foo = (42); say qr{$foo[0]}'
(?-xism:42)
white2:~/Code/perl/PPIx-Regexp.new tom 22:58:28
$ perl -E '$foo = "\\w"; $bar = 3; @foo = (42); say qr{$foo[$bar]}'
(?-xism:)
white2:~/Code/perl/PPIx-Regexp.new tom 22:58:43
$ perl -E '$foo = "\\w"; $bar = 0; @foo = (42); say qr{$foo[$bar]}'
(?-xism:42)
The above makes it somewhat easier to get $foo[$bar] interpreted as an
array dereference, but it appears to make use of information that is not
available to a static analysis, such as whether $foo[$bar] exists.
Actually, the above suggests a strategy: a subscript of any kind is to
be accepted as a subscript if it looks like \[\d+\], \[\$foo\], \{\w+\},
or \{\$foo\}. Otherwise, accept it as a character class or a quantifier
depending on the delimiter. Obviously when I bring PPI to bear I will
have to keep track of '->' operators before subscripts, and shed them
from the interpolation as well if the purported subscript does not pass
muster.
=end comment
=head1 SUPPORT
Support is by the author. Please file bug reports at
L<http://rt.cpan.org>, or in electronic mail to the author.
=head1 AUTHOR
Thomas R. Wyant, III F<wyant at cpan dot org>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2009-2016 by Thomas R. Wyant, III
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.
This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.
=cut
# ex: set textwidth=72 :
|