blib/lib/PPIx/Regexp/Token/Quantifier.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 93.3% 70 / 75 CC 7 SCAR 7.6
CC
7
Cov
93%
__quantified_width
4.2
__PPIX_LEXER__finalize
3.0
__following_literal_left_curly_disallowed_in
2.0
CRAP 2.1
« blib/lib/PPIx/Regexp/Structure/Code.pm ↑ Summary blib/lib/PPIx/Regexp/Token/Unknown.pm »
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{
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 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 9 our $VERSION = '0.091_01';
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 sub could_be_quantifier {
72 2 ▶ my ( undef, $string ) = @_; # Invocant unused
Subroutine could_be_quantifier: called CC=1 SCAR=0.0
Pod documented
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 sub __explanation {
85 3 ▶ return \%explanation;
Subroutine __explanation: called CC=1 SCAR=0.0
86 }
87  
88 }
89  
90 9 sub __following_literal_left_curly_disallowed_in {
91 0 ▶ return LITERAL_LEFT_CURLY_ALLOWED;
Subroutine __following_literal_left_curly_disallowed_in: not called CC=1 SCAR=6.9
92 }
93  
94 {
95 9 my $variable_look_behind_introduced = {
96 '*' => undef,
97 '+' => undef,
98 '?' => VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED,
99 };
100  
101 sub __PPIX_LEXER__finalize {
102 51 ▶ my ( $self ) = @_;
Subroutine __PPIX_LEXER__finalize: called CC=3 SCAR=11.0
103 51 ▶ if ( $self->__in_look_behind() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__in_look_behind) 2 49 51
104 $self->{perl_version_introduced} =
105 2 ▶ $variable_look_behind_introduced->{$self->content()}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"perl_version_introduced"} = $$variable_look_behind_introduced{$self->content} and return 0 1 1 2
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 sub __quantified_width {
124 56 ▶ my ( $self, $raw_min, $raw_max ) = @_;
Subroutine __quantified_width: called CC=4 SCAR=14.3
125 56 ▶ my $info = $width{$self->content()}
Branch: 1 branch
BranchTrueFalseTotal
my $info = $width{$self->content} or croak(sprintf("Bug - Quantifier '%s' width unknown", $self->content)) 0 56 56
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
BranchTrueFalseTotal
defined $raw_min and $raw_min *= $my_min 56 0 56
130 and $raw_min *= $my_min;
131 56 ▶ defined $raw_max
Branch: 1 branch
BranchTrueFalseTotal
defined $raw_max and $raw_max *= $my_max 56 0 56
132 and $raw_max *= $my_max;
133 56 return ( $raw_min, $raw_max );
134 }
135 }
136  
137 9 sub __PPIX_TOKENIZER__regexp {
138 1171 ▶ my ( undef, $tokenizer, $character ) = @_;
Subroutine __PPIX_TOKENIZER__regexp: called CC=2 SCAR=6.9
139  
140 1171 ▶ $tokenizer->prior_significant_token( 'can_be_quantified' )
Branch: 1 branch
BranchTrueFalseTotal
$tokenizer->prior_significant_token("can_be_quantified") or return 450 721 1171
141 or return;
142  
143 721 return $quantifier{$character};
144 }
145  
146 9 1;
147  
148 __END__
« blib/lib/PPIx/Regexp/Structure/Code.pm ↑ Summary blib/lib/PPIx/Regexp/Token/Unknown.pm »