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
|
=head1 NAME
PPIx::Regexp::Token::Operator - Represent an operator.
=head1 SYNOPSIS
use PPIx::Regexp::Dumper;
PPIx::Regexp::Dumper->new( 'qr{foo|bar}smx' )
->print();
=head1 INHERITANCE
C<PPIx::Regexp::Token::Operator> is a
L<PPIx::Regexp::Token|PPIx::Regexp::Token>.
C<PPIx::Regexp::Token::Operator> has no descendants.
=head1 DESCRIPTION
This class represents an operator. In a character class, it represents
the negation (C<^>) and range (C<->) operators. Outside a character
class, it represents the alternation (C<|>) operator.
=head1 METHODS
This class provides no public methods beyond those provided by its
superclass.
=cut
package PPIx::Regexp::Token::Operator;
use strict;
use warnings;
use base qw{ PPIx::Regexp::Token };
use PPIx::Regexp::Constant qw{
COOKIE_CLASS COOKIE_REGEX_SET
LITERAL_LEFT_CURLY_ALLOWED
TOKEN_LITERAL
@CARP_NOT
};
use PPIx::Regexp::Util qw{ __instance };
our $VERSION = '0.091';
use constant TOKENIZER_ARGUMENT_REQUIRED => 1;
sub __new {
my ( $class, $content, %arg ) = @_;
my $self = $class->SUPER::__new( $content, %arg )
or return;
$self->{operation} = $self->_compute_operation_name(
$arg{tokenizer} ) || 'unknown';
return $self;
}
# Return true if the token can be quantified, and false otherwise
# sub can_be_quantified { return };
sub explain {
my ( $self ) = @_;
my $expl = ucfirst "$self->{operation} operator";
$expl =~ s/ _ / /smxg;
return $expl;
}
=head2 operation
This method returns the name of the operation performed by the operator.
This depends not only on the operator itself but its context:
=over
=item In a bracketed character class
'-' => 'range',
'^' => 'inversion',
=item In an extended bracketed character class
'&' => 'intersection',
'+' => 'union',
'|' => 'union',
'-' => 'subtraction',
'^' => 'symmetric_difference',
'!' => 'complement',
=item Outside any sort of bracketed character class
'|' => 'alternation',
=back
=cut
sub operation {
my ( $self ) = @_;
return $self->{operation};
}
# These will be intercepted by PPIx::Regexp::Token::Literal if they are
# really literals, so here we may process them unconditionally.
# Note that if we receive a '-' we unconditionally make it an operator,
# relying on the lexer to turn it back into a literal if necessary.
my %operator = map { $_ => 1 } qw{ | - };
sub _treat_as_literal {
my ( $token ) = @_;
return __instance( $token, 'PPIx::Regexp::Token::Literal' ) ||
__instance( $token, 'PPIx::Regexp::Token::Interpolation' );
}
{
my %operation = (
COOKIE_CLASS() => {
'-' => 'range',
'^' => 'inversion',
},
COOKIE_REGEX_SET() => {
'&' => 'intersection',
'+' => 'union',
'|' => 'union',
'-' => 'subtraction',
'^' => 'symmetric_difference',
'!' => 'complement',
},
'' => {
'|' => 'alternation',
},
);
sub _compute_operation_name {
my ( $self, $tokenizer ) = @_;
my $content = $self->content();
if ( $tokenizer->cookie( COOKIE_CLASS ) ) {
return $operation{ COOKIE_CLASS() }{$content};
} elsif ( $tokenizer->cookie( COOKIE_REGEX_SET ) ) {
return $operation{ COOKIE_REGEX_SET() }{$content};
} else {
return $operation{''}{$content};
}
}
}
{
my $removed_in = {
'|' => LITERAL_LEFT_CURLY_ALLOWED, # Allowed after alternation
};
sub __following_literal_left_curly_disallowed_in {
my ( $self ) = @_;
my $content = $self->content();
exists $removed_in->{$content}
and return $removed_in->{$content};
return $self->SUPER::__following_literal_left_curly_disallowed_in();
}
}
sub __PPIX_TOKENIZER__regexp {
my ( undef, $tokenizer, $character ) = @_;
# We only receive the '-' if we are inside a character class. But it
# is only an operator if it is preceded and followed by literals. We
# can use prior() because there are no insignificant tokens inside a
# character class.
if ( $character eq '-' ) {
_treat_as_literal( $tokenizer->prior_significant_token() )
or return $tokenizer->make_token( 1, TOKEN_LITERAL );
my @tokens = ( $tokenizer->make_token( 1 ) );
push @tokens, $tokenizer->get_token();
_treat_as_literal( $tokens[1] )
or TOKEN_LITERAL->__PPIX_ELEM__rebless( $tokens[0] );
return ( @tokens );
}
return $operator{$character};
}
1;
__END__
=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) 2009-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 :
|