| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | PPIx::Regexp::Token::Quantifier - Represent an atomic quantifier. | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||
| 8 | PPIx::Regexp::Dumper->new( 'qr{\w+}smx' ) | ||||||||||
| 9 | ->print(); | ||||||||||
| 10 | |||||||||||
| 11 | =head1 INHERITANCE | ||||||||||
| 12 | |||||||||||
| 13 | C<PPIx::Regexp::Token::Quantifier> is a | ||||||||||
| 14 | L<PPIx::Regexp::Token|PPIx::Regexp::Token>. | ||||||||||
| 15 | |||||||||||
| 16 | C<PPIx::Regexp::Token::Quantifier> has no descendants. | ||||||||||
| 17 | |||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||
| 19 | |||||||||||
| 20 | This class represents an atomic quantifier; that is, one of the | ||||||||||
| 21 | characters C<*>, C<+>, or C<?>. | ||||||||||
| 22 | |||||||||||
| 23 | B<Note> that if they occur inside a variable-length look-behind, C<'?'> | ||||||||||
| 24 | implies a minimum Perl version of C<5.29.9>, and C<'+'> and C<'*'> are | ||||||||||
| 25 | regarded as parse errors and reblessed into the unknown token. | ||||||||||
| 26 | |||||||||||
| 27 | =head1 METHODS | ||||||||||
| 28 | |||||||||||
| 29 | This class provides the following public methods. Methods not documented | ||||||||||
| 30 | here are private, and unsupported in the sense that the author reserves | ||||||||||
| 31 | the right to change or remove them without notice. | ||||||||||
| 32 | |||||||||||
| 33 | =cut | ||||||||||
| 34 | |||||||||||
| 35 | package PPIx::Regexp::Token::Quantifier; | ||||||||||
| 36 | |||||||||||
| 37 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | 9 | ▶ | use base qw{ PPIx::Regexp::Token }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 9 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 9 | ▶ | use PPIx::Regexp::Constant qw{ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | INFINITY | ||||||||||
| 45 | LITERAL_LEFT_CURLY_ALLOWED | ||||||||||
| 46 | MSG_LOOK_BEHIND_TOO_LONG | ||||||||||
| 47 | TOKEN_UNKNOWN | ||||||||||
| 48 | VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED | ||||||||||
| 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 | 33 | ▶ | sub can_be_quantified { return }; | ||||||||
|
Subroutine
can_be_quantified: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | |||||||||||
| 57 | # Return true if the token is a quantifier. | ||||||||||
| 58 | 33 | ▶ | sub is_quantifier { return 1 }; | ||||||||
|
Subroutine
is_quantifier: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | |||||||||||
| 60 | 9 | my %quantifier = map { $_ => 1 } qw{ * + ? }; | |||||||||
| 61 | |||||||||||
| 62 | =head2 could_be_quantifier | ||||||||||
| 63 | |||||||||||
| 64 | PPIx::Regexp::Token::Quantifier->could_be_quantifier( '*' ); | ||||||||||
| 65 | |||||||||||
| 66 | This method returns true if the given string could be a quantifier; that | ||||||||||
| 67 | is, if it is '*', '+', or '?'. | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | 2 | ▶ | sub could_be_quantifier { | ||||||||
|
Subroutine
could_be_quantifier: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 72 | 2 | my ( undef, $string ) = @_; # Invocant unused | |||||||||
| 73 | 2 | return $quantifier{$string}; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | { | ||||||||||
| 77 | |||||||||||
| 78 | 9 | my %explanation = ( | |||||||||
| 79 | '*' => 'match zero or more times', | ||||||||||
| 80 | '+' => 'match one or more times', | ||||||||||
| 81 | '?' => 'match zero or one time', | ||||||||||
| 82 | ); | ||||||||||
| 83 | |||||||||||
| 84 | 3 | ▶ | sub __explanation { | ||||||||
|
Subroutine
__explanation: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 3 | return \%explanation; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 9 | ▶ | sub __following_literal_left_curly_disallowed_in { | ||||||||
|
Subroutine
__following_literal_left_curly_disallowed_in: not called CC=1 SCAR=1.2
| |||||||||||
| 91 | 0 | return LITERAL_LEFT_CURLY_ALLOWED; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | { | ||||||||||
| 95 | 9 | my $variable_look_behind_introduced = { | |||||||||
| 96 | '*' => undef, | ||||||||||
| 97 | '+' => undef, | ||||||||||
| 98 | '?' => VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED, | ||||||||||
| 99 | }; | ||||||||||
| 100 | |||||||||||
| 101 | 51 | ▶ | sub __PPIX_LEXER__finalize { | ||||||||
|
Subroutine
__PPIX_LEXER__finalize: called CC=3 SCAR=11.0
| |||||||||||
| 102 | 51 | my ( $self ) = @_; | |||||||||
| 103 | 51 | ▶ | if ( $self->__in_look_behind() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | $self->{perl_version_introduced} = | ||||||||||
| 105 | 2 | ▶ | $variable_look_behind_introduced->{$self->content()} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | and return 0; | ||||||||||
| 107 | 1 | TOKEN_UNKNOWN->__PPIX_ELEM__rebless( $self, | |||||||||
| 108 | error => MSG_LOOK_BEHIND_TOO_LONG, | ||||||||||
| 109 | ); | ||||||||||
| 110 | 1 | return 1; | |||||||||
| 111 | } | ||||||||||
| 112 | 49 | return 0; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | { | ||||||||||
| 117 | 9 | my %width = ( | |||||||||
| 118 | '*' => [ 0, INFINITY ], | ||||||||||
| 119 | '+' => [ 1, INFINITY ], | ||||||||||
| 120 | '?' => [ 0, 1 ], | ||||||||||
| 121 | ); | ||||||||||
| 122 | |||||||||||
| 123 | 56 | ▶ | sub __quantified_width { | ||||||||
|
Subroutine
__quantified_width: called CC=4 SCAR=14.3
| |||||||||||
| 124 | 56 | my ( $self, $raw_min, $raw_max ) = @_; | |||||||||
| 125 | 56 | ▶ | my $info = $width{$self->content()} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | or croak sprintf q<Bug - Quantifier '%s' width unknown>, | ||||||||||
| 127 | $self->content(); | ||||||||||
| 128 | 56 | my ( $my_min, $my_max ) = @{ $info }; | |||||||||
| 129 | 56 | ▶ | defined $raw_min | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | and $raw_min *= $my_min; | ||||||||||
| 131 | 56 | ▶ | defined $raw_max | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | and $raw_max *= $my_max; | ||||||||||
| 133 | 56 | return ( $raw_min, $raw_max ); | |||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 9 | ▶ | sub __PPIX_TOKENIZER__regexp { | ||||||||
|
Subroutine
__PPIX_TOKENIZER__regexp: called CC=2 SCAR=6.9
| |||||||||||
| 138 | 1171 | my ( undef, $tokenizer, $character ) = @_; | |||||||||
| 139 | |||||||||||
| 140 | 1171 | ▶ | $tokenizer->prior_significant_token( 'can_be_quantified' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | or return; | ||||||||||
| 142 | |||||||||||
| 143 | 721 | return $quantifier{$character}; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 9 | 1; | |||||||||
| 147 | |||||||||||
| 148 | __END__ | ||||||||||