/usr/share/perl5/PPIx/Regexp/Structure.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 | =head1 NAME
PPIx::Regexp::Structure - Represent a structure.
=head1 SYNOPSIS
use PPIx::Regexp::Dumper;
PPIx::Regexp::Dumper->new( 'qr{(foo)}' )->print();
=head1 INHERITANCE
C<PPIx::Regexp::Structure> is a
L<PPIx::Regexp::Node|PPIx::Regexp::Node>.
C<PPIx::Regexp::Structure> is the parent of
L<PPIx::Regexp::Structure::Assertion|PPIx::Regexp::Structure::Assertion>,
L<PPIx::Regexp::Structure::BranchReset|PPIx::Regexp::Structure::BranchReset>,
L<PPIx::Regexp::Structure::Capture|PPIx::Regexp::Structure::Capture>,
L<PPIx::Regexp::Structure::CharClass|PPIx::Regexp::Structure::CharClass>,
L<PPIx::Regexp::Structure::Code|PPIx::Regexp::Structure::Code>,
L<PPIx::Regexp::Structure::Main|PPIx::Regexp::Structure::Main>,
L<PPIx::Regexp::Structure::Modifier|PPIx::Regexp::Structure::Modifier>,
L<PPIx::Regexp::Structure::Quantifier|PPIx::Regexp::Structure::Quantifier>,
L<PPIx::Regexp::Structure::Subexpression|PPIx::Regexp::Structure::Subexpression>,
L<PPIx::Regexp::Structure::Switch|PPIx::Regexp::Structure::Switch> and
L<PPIx::Regexp::Structure::Unknown|PPIx::Regexp::Structure::Unknown>.
=head1 DESCRIPTION
This class represents a bracketed construction of some sort. The
brackets considered part of the structure, but not inside it. So the
C<elements()> method returns the brackets if they are defined, but the
C<children()> method does not.
=head1 METHODS
This class provides the following public methods. Methods not documented
here are private, and unsupported in the sense that the author reserves
the right to change or remove them without notice.
=cut
package PPIx::Regexp::Structure;
use strict;
use warnings;
use base qw{ PPIx::Regexp::Node };
use Carp qw{ confess };
use PPIx::Regexp::Constant qw{ STRUCTURE_UNKNOWN };
use PPIx::Regexp::Util qw{ __instance };
use Scalar::Util qw{ refaddr };
our $VERSION = '0.050';
use constant ELEMENT_UNKNOWN => STRUCTURE_UNKNOWN;
sub __new {
my ( $class, @args ) = @_;
my %brkt;
if ( ref $args[0] eq 'HASH' ) {
%brkt = %{ shift @args };
foreach my $key ( qw{ start type finish } ) {
ref $brkt{$key} eq 'ARRAY' or $brkt{$key} = [ $brkt{$key} ];
}
} else {
$brkt{finish} = [ @args ? pop @args : () ];
$brkt{start} = [ @args ? shift @args : () ];
while ( @args && ! $args[0]->significant() ) {
push @{ $brkt{start} }, shift @args;
}
$brkt{type} = [];
if ( __instance( $args[0], 'PPIx::Regexp::Token::GroupType' ) ) {
push @{ $brkt{type} }, shift @args;
while ( @args && ! $args[0]->significant() ) {
push @{ $brkt{type} }, shift @args;
}
}
}
$class->_check_for_interpolated_match( \%brkt, \@args );
my $self = $class->SUPER::__new( @args )
or return;
if ( __instance( $brkt{type}[0], 'PPIx::Regexp::Token::GroupType' ) ) {
( my $reclass = ref $brkt{type}[0] ) =~
s/ Token::GroupType /Structure/smx;
$reclass->can( 'start' )
or confess "Programming error - $reclass not loaded";
bless $self, $reclass;
}
foreach my $key ( qw{ start type finish } ) {
$self->{$key} = [];
ref $brkt{$key} eq 'ARRAY'
or confess "Programming error - '$brkt{$key}' not an ARRAY";
foreach my $val ( @{ $brkt{$key} } ) {
defined $val or next;
__instance( $val, 'PPIx::Regexp::Element' )
or confess "Programming error - '$val' not a ",
"PPIx::Regexp::Element";
push @{ $self->{$key} }, $val;
$val->_parent( $self );
}
}
@{ $self->{finish} }
or $self->{error} = 'Missing end delimiter';
return $self;
}
sub elements {
my ( $self ) = @_;
if ( wantarray ) {
return (
@{ $self->{start} },
@{ $self->{type} },
@{ $self->{children} },
@{ $self->{finish} },
);
} elsif ( defined wantarray ) {
my $size = scalar @{ $self->{start} };
$size += scalar @{ $self->{type} };
$size += scalar @{ $self->{children} };
$size += scalar @{ $self->{finish} };
return $size;
} else {
return;
}
}
sub explain {
my ( $self ) = @_;
if ( my $type = $self->type() ) {
return $type->explain();
}
if ( my $start = $self->start() ) {
return $start->explain();
}
return $self->__no_explanation();
}
=head2 finish
my $elem = $struct->finish();
my @elem = $struct->finish();
my $elem = $struct->finish( 0 );
Returns the finishing structure element. This is included in the
C<elements> but not in the C<children>.
The finishing element is actually an array, though it should never have
more than one element. Calling C<finish> in list context gets you all
elements of the array. Calling it in scalar context gets you an element
of the array, defaulting to element 0 if no argument is passed.
=cut
sub finish {
my ( $self, $inx ) = @_;
wantarray and return @{ $self->{finish} };
return $self->{finish}[ defined $inx ? $inx : 0 ];
}
sub first_element {
my ( $self ) = @_;
$self->{start}[0] and return $self->{start}[0];
$self->{type}[0] and return $self->{type}[0];
if ( my $elem = $self->SUPER::first_element() ) {
return $elem;
}
$self->{finish}[0] and return $self->{finish}[0];
return;
}
sub last_element {
my ( $self ) = @_;
$self->{finish}[-1] and return $self->{finish}[-1];
if ( my $elem = $self->SUPER::last_element() ) {
return $elem;
}
$self->{type}[-1] and return $self->{type}[-1];
$self->{start}[-1] and return $self->{start}[-1];
return;
}
=head2 start
my $elem = $struct->start();
my @elem = $struct->start();
my $elem = $struct->start( 0 );
Returns the starting structure element. This is included in the
C<elements> but not in the C<children>.
The starting element is actually an array. The first element (element 0)
is the actual starting delimiter. Subsequent elements, if any, are
insignificant elements (comments or white space) absorbed into the start
element for ease of parsing subsequent elements.
Calling C<start> in list context gets you all elements of the array.
Calling it in scalar context gets you an element of the array,
defaulting to element 0 if no argument is passed.
=cut
sub start {
my ( $self, $inx ) = @_;
wantarray and return @{ $self->{start} };
return $self->{start}[ defined $inx ? $inx : 0 ];
}
=head2 type
my $elem = $struct->type();
my @elem = $struct->type();
my $elem = $struct->type( 0 );
Returns the group type if any. This will be the leading
L<PPIx::Regexp::Token::GroupType|PPIx::Regexp::Token::GroupType>
token if any. This is included in C<elements> but not in C<children>.
The type is actually an array. The first element (element 0) is the
actual type determiner. Subsequent elements, if any, are insignificant
elements (comments or white space) absorbed into the type element for
consistency with the way the start element is handled.
Calling C<type> in list context gets you all elements of the array.
Calling it in scalar context gets you an element of the array,
defaulting to element 0 if no argument is passed.
=cut
sub type {
my ( $self, $inx ) = @_;
wantarray and return @{ $self->{type} };
return $self->{type}[ defined $inx ? $inx : 0 ];
}
# Check for things like (?$foo:...) or (?$foo)
sub _check_for_interpolated_match {
my ( undef, $brkt, $args ) = @_; # Invocant unused
# Everything we are interested in begins with a literal '?' followed
# by an interpolation.
__instance( $args->[0], 'PPIx::Regexp::Token::Unknown' )
and $args->[0]->content() eq '?'
and __instance( $args->[1], 'PPIx::Regexp::Token::Interpolation' )
or return;
my $hiwater = 2; # Record how far we got into the arguments for
# subsequent use detecting things like
# (?$foo).
# If we have a literal ':' as the third argument:
# GroupType::Modifier, rebless the ':' so we know not to match
# against it, and splice all three tokens into the type.
if ( __instance( $args->[2], 'PPIx::Regexp::Token::Literal' )
&& $args->[2]->content() eq ':' ) {
# Rebless the '?' as a GroupType::Modifier.
PPIx::Regexp::Token::GroupType::Modifier->__PPIX_ELEM__rebless(
$args->[0] );
# Rebless the ':' as a GroupType, just so it does not look like
# something to match against.
PPIx::Regexp::Token::GroupType->__PPIX_ELEM__rebless(
$args->[2] );
# Shove our three significant tokens into the type.
push @{ $brkt->{type} }, splice @{ $args }, 0, 3;
# Stuff all the immediately-following insignificant tokens into
# the type as well.
while ( @{ $args } && ! $args->[0]->significant() ) {
push @{ $brkt->{type} }, shift @{ $args };
}
# Return to the caller, since we have done all the damage we
# can.
return;
}
# If we have a literal '-' as the third argument, we might have
# something like (?$on-$off:$foo).
if ( __instance( $args->[2], 'PPIx::Regexp::Token::Literal' )
&& $args->[2]->content() eq '-'
&& __instance( $args->[3], 'PPIx::Regexp::Token::Interpolation' )
) {
$hiwater = 4;
if ( __instance( $args->[4], 'PPIx::Regexp::Token::Literal' )
&& $args->[4]->content() eq ':' ) {
# Rebless the '?' as a GroupType::Modifier.
PPIx::Regexp::Token::GroupType::Modifier->__PPIX_ELEM__rebless(
$args->[0] );
# Rebless the '-' and ':' as GroupType, just so they do not
# look like something to match against.
PPIx::Regexp::Token::GroupType->__PPIX_ELEM__rebless(
$args->[2] );
PPIx::Regexp::Token::GroupType->__PPIX_ELEM__rebless(
$args->[4] );
# Shove our five significant tokens into the type.
push @{ $brkt->{type} }, splice @{ $args }, 0, 5;
# Stuff all the immediately-following insignificant tokens
# into the type as well.
while ( @{ $args } && ! $args->[0]->significant() ) {
push @{ $brkt->{type} }, shift @{ $args };
}
# Return to the caller, since we have done all the damage we
# can.
return;
}
}
# If the group contains _any_ significant tokens at this point, we
# do _not_ have something like (?$foo).
foreach my $inx ( $hiwater .. $#$args ) {
$args->[$inx]->significant() and return;
}
# Rebless the '?' as a GroupType::Modifier.
PPIx::Regexp::Token::GroupType::Modifier->__PPIX_ELEM__rebless(
$args->[0] );
# Shove all the contents of $args into type, using splice to leave
# @{ $args } empty after we do this.
push @{ $brkt->{type} }, splice @{ $args };
# We have done all the damage we can.
return;
}
1;
__END__
=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 :
|