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
|
package PPIx::Regexp::Util;
use 5.006;
use strict;
use warnings;
use Carp;
use PPIx::Regexp::Constant qw{
INFINITY
MINIMUM_PERL
@CARP_NOT
};
use Scalar::Util qw{ blessed };
use base qw{ Exporter };
our @EXPORT_OK = qw{
is_ppi_regexp_element
__choose_tokenizer_class
__instance
__is_ppi_regexp_element
__merge_perl_requirements
__ns_can
__post_rebless_error
raw_width
__to_ordinal_en
width
};
our %EXPORT_TAGS = (
all => \@EXPORT_OK,
width_one => [ qw{ raw_width width } ],
);
our $VERSION = '0.090';
sub is_ppi_regexp_element {
my ( $elem ) = @_;
__instance( $elem, 'PPI::Element' )
or return;
return $elem->isa( 'PPI::Token::Regexp' ) ||
$elem->isa( 'PPI::Token::QuoteLike::Regexp' );
}
sub __is_ppi_regexp_element {
Carp::cluck(
'__is_ppi_regexp_element is deprecated. Use is_ppi_regexp_element'
);
goto &is_ppi_regexp_element;
}
# TODO ditch this once the deprecation period ends
sub __choose_tokenizer_class {
# my ( $content, $arg ) = @_;
my ( undef, $arg ) = @_;
if ( defined $arg->{parse} ) {
my $warning = q<The 'parse' argument is deprecated.>;
{ guess => 1, string => 1 }->{$arg->{parse}}
and $warning = join ' ', $warning,
q<You should use PPIx::QuoteLike on quotish things>;
croak $warning;
}
return 'PPIx::Regexp::Tokenizer';
}
sub __instance {
my ( $object, $class ) = @_;
blessed( $object ) or return;
return $object->isa( $class );
}
sub __merge_perl_requirements { ## no critic (RequireArgUnpacking)
my @work =
sort { $a->[0] <=> $b->[0] || $b->[1] <=> $a->[1] }
map { ( [ $_->[0], 1 ], [ $_->[1], 0 ] ) }
map { [ $_->{introduced}, defined $_->{removed} ? $_->{removed} : INFINITY ] } @_;
my @rslt;
while ( @work ) {
my ( $intro, $rem );
$intro = ( shift @work )->[0] while @work && $work[0][1];
if ( @work ) {
$rem = $work[0][0];
shift @work while @work && ! $work[0][1];
}
defined $intro
or $intro = MINIMUM_PERL;
defined $rem
or $rem = INFINITY;
$intro != $rem
and push @rslt, {
introduced => $intro,
removed => $rem,
};
}
@rslt
and $rslt[-1]{removed} == INFINITY
and delete $rslt[-1]{removed};
return @rslt;
}
sub __ns_can {
my ( $class, $name ) = @_;
my $fqn = join '::', ref $class || $class, $name;
no strict qw{ refs };
return defined &$fqn ? \&$fqn : undef;
}
sub __post_rebless_error {
my ( $self, %arg ) = @_;
my $rslt = 0;
unless ( defined( $self->{error} = $arg{error} ) ) {
my $class = ref $self;
Carp::cluck( "Making $class with no error message" );
$self->{error} = 'Unspecified error';
$rslt++;
}
$self->{explanation} = defined $arg{explanation} ?
$arg{explanation} :
$arg{error};
return $rslt;
}
# Unquantified number of characters matched.
sub raw_width {
return ( 1, 1 );
}
sub __to_ordinal_en {
my ( $num ) = @_;
$num += 0;
1 == int( ( $num % 100 ) / 10 ) # teens
and return "${num}th";
1 == $num % 10
and return "${num}st";
2 == $num % 10
and return "${num}nd";
3 == $num % 10
and return "${num}rd";
return "${num}th";
}
sub width {
my ( $self ) = @_;
my @raw_width = $self->raw_width();
my ( $code, $next_sib );
$next_sib = $self->snext_sibling()
and $code = $next_sib->can( '__quantified_width' )
or return @raw_width;
return $code->( $next_sib, @raw_width );
}
1;
__END__
=head1 NAME
PPIx::Regexp::Util - Utility functions for PPIx::Regexp;
=head1 SYNOPSIS
use PPIx::Regexp::Util qw{ __instance };
.
.
.
__instance( $foo, 'Bar' )
or die '$foo is not a Bar';
=head1 DESCRIPTION
This module contains utility functions for L<PPIx::Regexp|PPIx::Regexp>
which it is convenient to centralize.
Double-underscore subroutines are B<private> to the C<PPIx-Regexp>
package. Their documentation is provided for the author's convenience
only, and they are subject to change without notice. I<Caveat user.>
This module exports nothing by default.
=head1 SUBROUTINES
This module can export the following subroutines:
=head2 is_ppi_regexp_element
is_ppi_regexp_element( $elem )
and print "$elem is a regexp of some sort\n";
This subroutine is public and supported.
This subroutine takes as its argument a L<PPI::Element|PPI::Element>. It
returns a true value if the argument represents a regular expression of
some sort, and a false value otherwise.
=head2 __instance
__instance( $foo, 'Bar' )
and print '$foo isa Bar', "\n";
This subroutine is B<private> to the C<PPIx-Regexp> package.
This subroutine returns true if its first argument is an instance of the
class specified by its second argument. Unlike C<UNIVERSAL::isa>, the
result is always false unless the first argument is a reference.
=head2 __is_ppi_regexp_element
__is_ppi_regexp_element( $elem )
and print "$elem is a regexp of some sort\n";
This subroutine is B<private> to the C<PPIx-Regexp> package.
This is a synonym for L<is_ppi_regexp_element()|/is_ppi_regexp_element>,
and is deprecated in favor of it. If called, it will complain via
C<Carp::cluck()> and then C<goto &is_ppi_regexp_element>.
=head2 __merge_perl_requirements
This subroutine is B<private> to the C<PPIx-Regexp> package.
This subroutine merges perl requirements as returned by the various
C<__perl_requirements()> methods.
=head2 __ns_can
This subroutine is B<private> to the C<PPIx-Regexp> package.
This method is analogous to C<can()>, but returns a reference to the
code only if it is actually implemented by the invoking name space.
=head2 __post_rebless_error
This method is B<private> to the C<PPIx-Regexp> package. The intended
use is to alias it to C<__PPIX_ELEM__post_reblessing()>.
It takes arguments as name/value pairs. Argument C<{error}> is the error
message; if it is omitted you get a warning with stack trace. Argument
C<{explanation}> defaults to C<{error}>.
It returns the number of errors to add to the parse.
=head2 raw_width
This public method returns the minimum and maximum width matched by the
element before taking into account such details as what the element
actually is and how it is quantified.
This implementation is appropriate to things that match exactly one
character -- i.e. it returns C<( 1, 1 )>.
=head2 __to_ordinal_en
This subroutine is B<private> to the C<PPIx-Regexp> package.
This subroutine takes as its argument an integer and returns a string
representing its ordinal in English. For example
say __to_ordinal_en( 17 );
# 17th
=head2 width
my ( $min_wid, $max_wid ) = $self->width();
This public method (well, mixin) returns the minimum and maximum width
of the text matched by the element.
Elements which import this method must also implement a C<raw_width()>
method which returns the unquantified width of the element.
=head1 EXPORT TAGS
The following export tags are defined by this module. All are private to
the C<PPIx-Regexp> package unless otherwise documented.
=head2 all
This tag exports everything exportable by this module.
=head2 width_one
This tag is appropriate to an element which, when unquantified, matches
exactly one character. It exports C<raw_width()> and C<width()>.
=head1 SEE ALSO
L<Params::Util|Params::Util>, which I recommend, but in the case of
C<PPIx::Regexp> I did not want to introduce a dependency on an XS module
when all I really wanted was the function of that module's
C<_INSTANCE()> subroutine.
=head1 SUPPORT
Support is by the author. Please file bug reports at
L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, 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) 2010-2023, 2025 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 :
|