| 1 | =head1 NAME | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | PPIx::Regexp::Token::Operator - Represent an operator. | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||||||||||||
| 8 | PPIx::Regexp::Dumper->new( 'qr{foo|bar}smx' ) | ||||||||||||||||||||
| 9 | ->print(); | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 INHERITANCE | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | C<PPIx::Regexp::Token::Operator> is a | ||||||||||||||||||||
| 14 | L<PPIx::Regexp::Token|PPIx::Regexp::Token>. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | C<PPIx::Regexp::Token::Operator> has no descendants. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | This class represents an operator. In a character class, it represents | ||||||||||||||||||||
| 21 | the negation (C<^>) and range (C<->) operators. Outside a character | ||||||||||||||||||||
| 22 | class, it represents the alternation (C<|>) operator. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 METHODS | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | This class provides no public methods beyond those provided by its | ||||||||||||||||||||
| 27 | superclass. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | package PPIx::Regexp::Token::Operator; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 9 | ▶ | use base qw{ PPIx::Regexp::Token }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 9 | ▶ | use PPIx::Regexp::Constant qw{ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | COOKIE_CLASS COOKIE_REGEX_SET | ||||||||||||||||||||
| 40 | LITERAL_LEFT_CURLY_ALLOWED | ||||||||||||||||||||
| 41 | TOKEN_LITERAL | ||||||||||||||||||||
| 42 | @CARP_NOT | ||||||||||||||||||||
| 43 | 9 | }; | |||||||||||||||||||
| 44 | 9 | ▶ | use PPIx::Regexp::Util qw{ __instance }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 9 | our $VERSION = '0.092'; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 9 | ▶ | use constant TOKENIZER_ARGUMENT_REQUIRED => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 64 | ▶ | sub __new { | ||||||||||||||||||
|
Subroutine
__new: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 51 | 64 | my ( $class, $content, %arg ) = @_; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 64 | ▶ | my $self = $class->SUPER::__new( $content, %arg ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | or return; | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | $self->{operation} = $self->_compute_operation_name( | ||||||||||||||||||||
| 57 | 64 | ▶ | $arg{tokenizer} ) || 'unknown'; | ||||||||||||||||||
|
Condition100%2/2
$self->_compute_operation_name($arg{'tokenizer'}) || 'unknown'
MC/DC100%1/1
$self->_compute_operation_name($arg{'tokenizer'}) || 'unknown'
$self->_compute_operation_name($arg{'tokenizer'})
Truth table100%2/2
A
A= $self->_compute_operation_name($arg{'tokenizer'})
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 64 | return $self; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # Return true if the token can be quantified, and false otherwise | ||||||||||||||||||||
| 63 | # sub can_be_quantified { return }; | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 5 | ▶ | sub explain { | ||||||||||||||||||
|
Subroutine
explain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 66 | 5 | my ( $self ) = @_; | |||||||||||||||||||
| 67 | 5 | my $expl = ucfirst "$self->{operation} operator"; | |||||||||||||||||||
| 68 | 5 | $expl =~ s/ _ / /smxg; | |||||||||||||||||||
| 69 | 5 | return $expl; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head2 operation | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | This method returns the name of the operation performed by the operator. | ||||||||||||||||||||
| 75 | This depends not only on the operator itself but its context: | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =over | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =item In a bracketed character class | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | '-' => 'range', | ||||||||||||||||||||
| 82 | '^' => 'inversion', | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =item In an extended bracketed character class | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | '&' => 'intersection', | ||||||||||||||||||||
| 87 | '+' => 'union', | ||||||||||||||||||||
| 88 | '|' => 'union', | ||||||||||||||||||||
| 89 | '-' => 'subtraction', | ||||||||||||||||||||
| 90 | '^' => 'symmetric_difference', | ||||||||||||||||||||
| 91 | '!' => 'complement', | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =item Outside any sort of bracketed character class | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | '|' => 'alternation', | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =back | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | sub operation { | ||||||||||||||||||
|
Subroutine
operation: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 102 | 0 | my ( $self ) = @_; | |||||||||||||||||||
| 103 | 0 | return $self->{operation}; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # These will be intercepted by PPIx::Regexp::Token::Literal if they are | ||||||||||||||||||||
| 107 | # really literals, so here we may process them unconditionally. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | # Note that if we receive a '-' we unconditionally make it an operator, | ||||||||||||||||||||
| 110 | # relying on the lexer to turn it back into a literal if necessary. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 9 | my %operator = map { $_ => 1 } qw{ | - }; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 36 | ▶ | sub _treat_as_literal { | ||||||||||||||||||
|
Subroutine
_treat_as_literal: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 115 | 36 | my ( $token ) = @_; | |||||||||||||||||||
| 116 | 36 | ▶ | return __instance( $token, 'PPIx::Regexp::Token::Literal' ) || | ||||||||||||||||||
|
Condition66%2/3
__instance($token, 'PPIx::Regexp::Token::Literal') || __instance($token, 'PPIx::Regexp::Token::Interpolation')
MC/DC50%1/2
__instance($token, 'PPIx::Regexp::Token::Literal') || __instance($token, 'PPIx::Regexp::Token::Interpolation')
__instance($token, 'PPIx::Regexp::Token::Literal')
__instance($token, 'PPIx::Regexp::Token::Interpolation')
Truth table66%2/3
A || B
A= __instance($token, 'PPIx::Regexp::Token::Literal')B= __instance($token, 'PPIx::Regexp::Token::Interpolation')
| |||||||||||||||||||||
| 117 | __instance( $token, 'PPIx::Regexp::Token::Interpolation' ); | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | { | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 9 | my %operation = ( | |||||||||||||||||||
| 123 | COOKIE_CLASS() => { | ||||||||||||||||||||
| 124 | '-' => 'range', | ||||||||||||||||||||
| 125 | '^' => 'inversion', | ||||||||||||||||||||
| 126 | }, | ||||||||||||||||||||
| 127 | COOKIE_REGEX_SET() => { | ||||||||||||||||||||
| 128 | '&' => 'intersection', | ||||||||||||||||||||
| 129 | '+' => 'union', | ||||||||||||||||||||
| 130 | '|' => 'union', | ||||||||||||||||||||
| 131 | '-' => 'subtraction', | ||||||||||||||||||||
| 132 | '^' => 'symmetric_difference', | ||||||||||||||||||||
| 133 | '!' => 'complement', | ||||||||||||||||||||
| 134 | }, | ||||||||||||||||||||
| 135 | '' => { | ||||||||||||||||||||
| 136 | '|' => 'alternation', | ||||||||||||||||||||
| 137 | }, | ||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 64 | ▶ | sub _compute_operation_name { | ||||||||||||||||||
|
Subroutine
_compute_operation_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 141 | 64 | my ( $self, $tokenizer ) = @_; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 64 | my $content = $self->content(); | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 64 | ▶ | if ( $tokenizer->cookie( COOKIE_CLASS ) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 146 | 24 | return $operation{ COOKIE_CLASS() }{$content}; | |||||||||||||||||||
| 147 | } elsif ( $tokenizer->cookie( COOKIE_REGEX_SET ) ) { | ||||||||||||||||||||
| 148 | 13 | return $operation{ COOKIE_REGEX_SET() }{$content}; | |||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||
| 150 | 27 | return $operation{''}{$content}; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | { | ||||||||||||||||||||
| 157 | 9 | my $removed_in = { | |||||||||||||||||||
| 158 | '|' => LITERAL_LEFT_CURLY_ALLOWED, # Allowed after alternation | ||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | ▶ | sub __following_literal_left_curly_disallowed_in { | ||||||||||||||||||
|
Subroutine
__following_literal_left_curly_disallowed_in: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 162 | 0 | my ( $self ) = @_; | |||||||||||||||||||
| 163 | 0 | my $content = $self->content(); | |||||||||||||||||||
| 164 | exists $removed_in->{$content} | ||||||||||||||||||||
| 165 | 0 | ▶ | and return $removed_in->{$content}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | return $self->SUPER::__following_literal_left_curly_disallowed_in(); | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 9 | ▶ | sub __PPIX_TOKENIZER__regexp { | ||||||||||||||||||
|
Subroutine
__PPIX_TOKENIZER__regexp: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 171 | 62 | my ( undef, $tokenizer, $character ) = @_; | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | # We only receive the '-' if we are inside a character class. But it | ||||||||||||||||||||
| 174 | # is only an operator if it is preceded and followed by literals. We | ||||||||||||||||||||
| 175 | # can use prior() because there are no insignificant tokens inside a | ||||||||||||||||||||
| 176 | # character class. | ||||||||||||||||||||
| 177 | 62 | ▶ | if ( $character eq '-' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 19 | ▶ | _treat_as_literal( $tokenizer->prior_significant_token() ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | or return $tokenizer->make_token( 1, TOKEN_LITERAL ); | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 17 | my @tokens = ( $tokenizer->make_token( 1 ) ); | |||||||||||||||||||
| 183 | 17 | push @tokens, $tokenizer->get_token(); | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 17 | ▶ | _treat_as_literal( $tokens[1] ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | or TOKEN_LITERAL->__PPIX_ELEM__rebless( $tokens[0] ); | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 17 | return ( @tokens ); | |||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 43 | return $operator{$character}; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 9 | 1; | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||