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
|
=head1 NAME
PPIx::Regexp::Token::Control - Case and quote control.
=head1 SYNOPSIS
use PPIx::Regexp::Dumper;
PPIx::Regexp::Dumper->new( 'qr{\Ufoo\E}smx' )
->print();
=head1 INHERITANCE
C<PPIx::Regexp::Token::Control> is a
L<PPIx::Regexp::Token|PPIx::Regexp::Token>.
C<PPIx::Regexp::Token::Control> has no descendants.
=head1 DESCRIPTION
This class represents the case and quote controls. These apply when the
regular expression is compiled, changing the actual expression
generated. For example
print qr{\Ufoo\E}, "\n"
prints
(?-xism:FOO)
=head1 METHODS
This class provides no public methods beyond those provided by its
superclass.
=cut
package PPIx::Regexp::Token::Control;
use strict;
use warnings;
use base qw{ PPIx::Regexp::Token };
use PPIx::Regexp::Constant qw{
COOKIE_QUOTE
MINIMUM_PERL
TOKEN_LITERAL
TOKEN_UNKNOWN
@CARP_NOT
};
our $VERSION = '0.090';
# Return true if the token can be quantified, and false otherwise
# sub can_be_quantified { return };
{
my %explanation = (
'\\E' => 'End of interpolation control',
'\\F' => 'Fold case until \\E',
'\\L' => 'Lowercase until \\E',
'\\Q' => 'Quote metacharacters until \\E',
'\\U' => 'Uppercase until \\E',
'\\l' => 'Lowercase next character',
'\\u' => 'Uppercase next character',
);
sub __explanation {
return \%explanation;
}
}
{
my %version_introduced = (
'\\F' => '5.015008',
);
sub perl_version_introduced {
my ( $self ) = @_;
my $content = $self->content();
defined $version_introduced{$content}
and return $version_introduced{$content};
return MINIMUM_PERL;
}
}
my %is_control = map { $_ => 1 } qw{ l u L U Q E F };
my %cookie_slot = (
Q => 'quote',
E => 'end',
U => 'case',
L => 'case',
F => 'case',
);
use constant CONTROL_MASK_QUOTE => 1 << 1;
my %cookie_mask = (
case => 1 << 0,
end => 0, # must be 0.
quote => CONTROL_MASK_QUOTE,
);
sub __PPIX_TOKENIZER__regexp {
my ( undef, $tokenizer, $character ) = @_;
# If we are inside a quote sequence, we want to make literals out of
# all the characters we reject; otherwise we just want to return
# nothing.
my $in_quote = $tokenizer->cookie( COOKIE_QUOTE ) || do {
my @stack = ( { mask => 0, reject => sub { return; } } );
$tokenizer->cookie( COOKIE_QUOTE, sub { return \@stack } );
};
my $cookie_stack = $in_quote->( $tokenizer );
my $reject = $cookie_stack->[-1]{reject};
# We are not interested in anything that is not escaped.
$character eq '\\' or return $reject->( 1 );
# We need to see what the next character is to figure out what to
# do. If there is no next character, we do not know what to call the
# back slash.
my $control = $tokenizer->peek( 1 )
or return $reject->( 1, TOKEN_UNKNOWN, {
error => 'Trailing back slash'
},
);
# We reject any escapes that do not represent controls.
$is_control{$control} or return $reject->( 2 );
# Anything left gets made into a token now, to avoid its processing
# by the cookie we may make.
my $token = $tokenizer->make_token( 2 );
# \U, \L, and \F supersede each other, but they stack with \Q. So we
# need to track that behavior, so that we know what to do when we
# hit a \E.
# TODO if we wanted we could actually track which (if any) of \U, \L
# and \F is in effect, and make that an attribute of any literals
# made.
if ( my $slot = $cookie_slot{$control} ) {
if ( my $mask = $cookie_mask{$slot} ) {
# We need another stack entry only if the current slot
# ('case' or 'quote') is not occupied
unless ( $mask & $cookie_stack->[-1]{mask} ) {
# Clone the previous entry.
push @{ $cookie_stack }, { %{ $cookie_stack->[-1] } };
# Set the mask to show this slot is occupied
$cookie_stack->[-1]{mask} |= $mask;
# Code to call when this tokenizer rejects a token
$cookie_stack->[-1]{reject} =
( $mask & CONTROL_MASK_QUOTE ) ?
sub {
my ( $size, $class ) = @_;
return $tokenizer->make_token(
$size, $class || TOKEN_LITERAL );
} : $cookie_stack->[0]{reject};
}
# TODO if I want to try to track what controls are in effect
# where.
# Record the specific content of the current slot
# $cookie_stack->[-1]{$slot} = $control;
} else {
# \E - pop data, but don't leave empty.
@{ $cookie_stack } > 1
and pop @{ $cookie_stack };
}
}
# Return our token.
return $token;
}
sub __PPIX_TOKENIZER__repl {
my ( undef, $tokenizer ) = @_; # Invocant, $character unused
$tokenizer->interpolates() and goto &__PPIX_TOKENIZER__regexp;
return;
}
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 :
|