| 1 | =head1 NAME | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | PPIx::Regexp::Token::Control - Case and quote control. | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||||||||||||
| 8 | PPIx::Regexp::Dumper->new( 'qr{\Ufoo\E}smx' ) | ||||||||||||||||||||
| 9 | ->print(); | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 INHERITANCE | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | C<PPIx::Regexp::Token::Control> is a | ||||||||||||||||||||
| 14 | L<PPIx::Regexp::Token|PPIx::Regexp::Token>. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | C<PPIx::Regexp::Token::Control> has no descendants. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | This class represents the case and quote controls. These apply when the | ||||||||||||||||||||
| 21 | regular expression is compiled, changing the actual expression | ||||||||||||||||||||
| 22 | generated. For example | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | print qr{\Ufoo\E}, "\n" | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | prints | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | (?-xism:FOO) | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | This class provides no public methods beyond those provided by its | ||||||||||||||||||||
| 33 | superclass. | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | package PPIx::Regexp::Token::Control; | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 9 | ▶ | use base qw{ PPIx::Regexp::Token }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 9 | ▶ | use PPIx::Regexp::Constant qw{ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | COOKIE_QUOTE | ||||||||||||||||||||
| 46 | MINIMUM_PERL | ||||||||||||||||||||
| 47 | TOKEN_LITERAL | ||||||||||||||||||||
| 48 | TOKEN_UNKNOWN | ||||||||||||||||||||
| 49 | @CARP_NOT | ||||||||||||||||||||
| 50 | 9 | }; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 9 | our $VERSION = '0.092'; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Return true if the token can be quantified, and false otherwise | ||||||||||||||||||||
| 55 | # sub can_be_quantified { return }; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | { | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 9 | my %explanation = ( | |||||||||||||||||||
| 60 | '\\E' => 'End of interpolation control', | ||||||||||||||||||||
| 61 | '\\F' => 'Fold case until \\E', | ||||||||||||||||||||
| 62 | '\\L' => 'Lowercase until \\E', | ||||||||||||||||||||
| 63 | '\\Q' => 'Quote metacharacters until \\E', | ||||||||||||||||||||
| 64 | '\\U' => 'Uppercase until \\E', | ||||||||||||||||||||
| 65 | '\\l' => 'Lowercase next character', | ||||||||||||||||||||
| 66 | '\\u' => 'Uppercase next character', | ||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 7 | ▶ | sub __explanation { | ||||||||||||||||||
|
Subroutine
__explanation: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 7 | return \%explanation; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | { | ||||||||||||||||||||
| 76 | 9 | my %version_introduced = ( | |||||||||||||||||||
| 77 | '\\F' => '5.015008', | ||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 7 | ▶ | sub perl_version_introduced { | ||||||||||||||||||
|
Subroutine
perl_version_introduced: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 81 | 7 | my ( $self ) = @_; | |||||||||||||||||||
| 82 | 7 | my $content = $self->content(); | |||||||||||||||||||
| 83 | defined $version_introduced{$content} | ||||||||||||||||||||
| 84 | 7 | ▶ | and return $version_introduced{$content}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 6 | return MINIMUM_PERL; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 9 | my %is_control = map { $_ => 1 } qw{ l u L U Q E F }; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 9 | my %cookie_slot = ( | |||||||||||||||||||
| 92 | Q => 'quote', | ||||||||||||||||||||
| 93 | E => 'end', | ||||||||||||||||||||
| 94 | U => 'case', | ||||||||||||||||||||
| 95 | L => 'case', | ||||||||||||||||||||
| 96 | F => 'case', | ||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 9 | ▶ | use constant CONTROL_MASK_QUOTE => 1 << 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 9 | my %cookie_mask = ( | |||||||||||||||||||
| 102 | case => 1 << 0, | ||||||||||||||||||||
| 103 | end => 0, # must be 0. | ||||||||||||||||||||
| 104 | quote => CONTROL_MASK_QUOTE, | ||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1342 | ▶ | sub __PPIX_TOKENIZER__regexp { | ||||||||||||||||||
|
Subroutine
__PPIX_TOKENIZER__regexp: called CC=10 SCAR=23.1
| |||||||||||||||||||||
| 108 | 1342 | my ( undef, $tokenizer, $character ) = @_; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # If we are inside a quote sequence, we want to make literals out of | ||||||||||||||||||||
| 111 | # all the characters we reject; otherwise we just want to return | ||||||||||||||||||||
| 112 | # nothing. | ||||||||||||||||||||
| 113 | 1342 | ▶ | my $in_quote = $tokenizer->cookie( COOKIE_QUOTE ) || do { | ||||||||||||||||||
|
Condition66%2/3
$tokenizer->cookie('\\E') || do {
my(@stack) = {'mask', 0, 'reject', sub {
return;
}
};
$tokenizer->cookie('\\E', sub {
return \@stack;
}
)
}
MC/DC0%0/2
$tokenizer->cookie('\\E') || do {
my(@stack) = {'mask', 0, 'reject', sub {
return;
}
};
$tokenizer->cookie('\\E', sub {
return \@stack;
}
)
}
$tokenizer->cookie('\\E')
do {
my(@stack) = {'mask', 0, 'reject', sub {
return;
}
};
$tokenizer->cookie('\\E', sub {
return \@stack;
}
)
}
Truth table66%2/3
A || B
A= $tokenizer->cookie('\\E')B= do {
my(@stack) = {'mask', 0, 'reject', sub {
return;
}
};
$tokenizer->cookie('\\E', sub {
return \@stack;
}
)
}
| |||||||||||||||||||||
| 114 | 423 | ▶ | my @stack = ( { mask => 0, reject => sub { return; } } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 115 | 423 | ▶ | $tokenizer->cookie( COOKIE_QUOTE, sub { return \@stack } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 116 | }; | ||||||||||||||||||||
| 117 | 1342 | my $cookie_stack = $in_quote->( $tokenizer ); | |||||||||||||||||||
| 118 | 1342 | my $reject = $cookie_stack->[-1]{reject}; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # We are not interested in anything that is not escaped. | ||||||||||||||||||||
| 121 | 1342 | ▶ | $character eq '\\' or return $reject->( 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # We need to see what the next character is to figure out what to | ||||||||||||||||||||
| 124 | # do. If there is no next character, we do not know what to call the | ||||||||||||||||||||
| 125 | # back slash. | ||||||||||||||||||||
| 126 | 227 | ▶ | my $control = $tokenizer->peek( 1 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | or return $reject->( 1, TOKEN_UNKNOWN, { | ||||||||||||||||||||
| 128 | error => 'Trailing back slash' | ||||||||||||||||||||
| 129 | }, | ||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # We reject any escapes that do not represent controls. | ||||||||||||||||||||
| 133 | 227 | ▶ | $is_control{$control} or return $reject->( 2 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # Anything left gets made into a token now, to avoid its processing | ||||||||||||||||||||
| 136 | # by the cookie we may make. | ||||||||||||||||||||
| 137 | 36 | my $token = $tokenizer->make_token( 2 ); | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # \U, \L, and \F supersede each other, but they stack with \Q. So we | ||||||||||||||||||||
| 140 | # need to track that behavior, so that we know what to do when we | ||||||||||||||||||||
| 141 | # hit a \E. | ||||||||||||||||||||
| 142 | # TODO if we wanted we could actually track which (if any) of \U, \L | ||||||||||||||||||||
| 143 | # and \F is in effect, and make that an attribute of any literals | ||||||||||||||||||||
| 144 | # made. | ||||||||||||||||||||
| 145 | 36 | ▶ | if ( my $slot = $cookie_slot{$control} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 30 | ▶ | if ( my $mask = $cookie_mask{$slot} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | # We need another stack entry only if the current slot | ||||||||||||||||||||
| 148 | # ('case' or 'quote') is not occupied | ||||||||||||||||||||
| 149 | 20 | ▶ | unless ( $mask & $cookie_stack->[-1]{mask} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | # Clone the previous entry. | ||||||||||||||||||||
| 151 | 16 | push @{ $cookie_stack }, { %{ $cookie_stack->[-1] } }; | |||||||||||||||||||
| 152 | # Set the mask to show this slot is occupied | ||||||||||||||||||||
| 153 | 16 | $cookie_stack->[-1]{mask} |= $mask; | |||||||||||||||||||
| 154 | # Code to call when this tokenizer rejects a token | ||||||||||||||||||||
| 155 | $cookie_stack->[-1]{reject} = | ||||||||||||||||||||
| 156 | ( $mask & CONTROL_MASK_QUOTE ) ? | ||||||||||||||||||||
| 157 | 7 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 158 | 7 | my ( $size, $class ) = @_; | |||||||||||||||||||
| 159 | 7 | ▶ | return $tokenizer->make_token( | ||||||||||||||||||
|
Condition50%1/2
$class || 'PPIx::Regexp::Token::Literal'
MC/DC0%0/1
$class || 'PPIx::Regexp::Token::Literal'
$class
Truth table50%1/2
A
A= $class
| |||||||||||||||||||||
| 160 | $size, $class || TOKEN_LITERAL ); | ||||||||||||||||||||
| 161 | 16 | ▶ | } : $cookie_stack->[0]{reject}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | # TODO if I want to try to track what controls are in effect | ||||||||||||||||||||
| 164 | # where. | ||||||||||||||||||||
| 165 | # Record the specific content of the current slot | ||||||||||||||||||||
| 166 | # $cookie_stack->[-1]{$slot} = $control; | ||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||
| 168 | # \E - pop data, but don't leave empty. | ||||||||||||||||||||
| 169 | 10 | @{ $cookie_stack } > 1 | |||||||||||||||||||
| 170 | 10 | ▶ | and pop @{ $cookie_stack }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | # Return our token. | ||||||||||||||||||||
| 175 | 36 | return $token; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 13 | ▶ | sub __PPIX_TOKENIZER__repl { | ||||||||||||||||||
|
Subroutine
__PPIX_TOKENIZER__repl: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 179 | 13 | my ( undef, $tokenizer ) = @_; # Invocant, $character unused | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 13 | ▶ | $tokenizer->interpolates() and goto &__PPIX_TOKENIZER__regexp; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 0 | return; | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 9 | 1; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | __END__ | ||||||||||||||||||||