| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | PPIx::Regexp::Structure::Quantifier - Represent curly bracket quantifiers | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||||||||||||||||||||
| 8 | PPIx::Regexp::Dumper->new( 'qr{fo{2,}}smx' ) | ||||||||||||||||||||||||||||
| 9 | ->print(); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | C<PPIx::Regexp::Structure::Quantifier> is a | ||||||||||||||||||||||||||||
| 14 | L<PPIx::Regexp::Structure|PPIx::Regexp::Structure>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | C<PPIx::Regexp::Structure::Quantifier> has no descendants. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This class represents curly bracket quantifiers such as C<{3}>, C<{3,}> | ||||||||||||||||||||||||||||
| 21 | and C<{3,5}>. The contents are left as literals or interpolations. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | B<Note> that if they occur inside a variable-length look-behind, | ||||||||||||||||||||||||||||
| 24 | quantifiers with different low and high limits (such as C<'{1,3}'> imply | ||||||||||||||||||||||||||||
| 25 | a minimum Perl version of C<5.29.9>. Quantifiers specifying more than | ||||||||||||||||||||||||||||
| 26 | 255 characters are regarded as parse errors and reblessed into the | ||||||||||||||||||||||||||||
| 27 | unknown structure. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 METHODS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This class provides no public methods beyond those provided by its | ||||||||||||||||||||||||||||
| 32 | superclass. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | package PPIx::Regexp::Structure::Quantifier; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 9 | ▶ | use base qw{ PPIx::Regexp::Structure }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | ▶ | use Scalar::Util qw{ looks_like_number }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 9 | ▶ | use PPIx::Regexp::Constant qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | INFINITY | ||||||||||||||||||||||||||||
| 47 | LITERAL_LEFT_CURLY_ALLOWED | ||||||||||||||||||||||||||||
| 48 | MINIMUM_PERL | ||||||||||||||||||||||||||||
| 49 | MSG_LOOK_BEHIND_TOO_LONG | ||||||||||||||||||||||||||||
| 50 | STRUCTURE_UNKNOWN | ||||||||||||||||||||||||||||
| 51 | VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED | ||||||||||||||||||||||||||||
| 52 | @CARP_NOT | ||||||||||||||||||||||||||||
| 53 | 9 | }; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | our $VERSION = '0.092'; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub can_be_quantified { | ||||||||||||||||||||||||||
|
Subroutine
can_be_quantified: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 0 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub explain { | ||||||||||||||||||||||||||
|
Subroutine
explain: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 4 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =begin comment | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my $content = $self->content(); | ||||||||||||||||||||||||||||
| 67 | if ( $content =~ m/ \A [{] ( .*? ) [}] \z /smx ) { | ||||||||||||||||||||||||||||
| 68 | my $quant = $1; | ||||||||||||||||||||||||||||
| 69 | my ( $lo, $hi ) = split qr{ , }smx, $quant; | ||||||||||||||||||||||||||||
| 70 | foreach ( $lo, $hi ) { | ||||||||||||||||||||||||||||
| 71 | defined | ||||||||||||||||||||||||||||
| 72 | or next; | ||||||||||||||||||||||||||||
| 73 | s/ \A \s+ //smx; | ||||||||||||||||||||||||||||
| 74 | s/ \s+ \z //smx; | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | defined $lo | ||||||||||||||||||||||||||||
| 77 | and '' ne $lo | ||||||||||||||||||||||||||||
| 78 | or $lo = '0'; | ||||||||||||||||||||||||||||
| 79 | defined $hi | ||||||||||||||||||||||||||||
| 80 | and '' ne $hi | ||||||||||||||||||||||||||||
| 81 | and return "match $lo to $hi times"; | ||||||||||||||||||||||||||||
| 82 | $quant =~ m/ , \z /smx | ||||||||||||||||||||||||||||
| 83 | and return "match $lo or more times"; | ||||||||||||||||||||||||||||
| 84 | $lo =~ m/ [^0-9] /smx | ||||||||||||||||||||||||||||
| 85 | and return "match $lo times"; | ||||||||||||||||||||||||||||
| 86 | return "match exactly $lo times"; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | return $self->SUPER::explain(); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =end comment | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | my ( $lo, $hi ) = $self->_min_max(); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | ▶ | if ( looks_like_number( $hi ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | $hi == INFINITY | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | and return "match $lo or more times"; | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | looks_like_number( $lo ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&looks_like_number($lo) and $lo == $hi
MC/DC50%1/2
&looks_like_number($lo) and $lo == $hi
&looks_like_number($lo)
$lo == $hi
Truth table66%2/3
A and B
A= &looks_like_number($lo)B= $lo == $hi
| |||||||||||||||||||||||||||||
| 100 | and $lo == $hi | ||||||||||||||||||||||||||||
| 101 | and return "match exactly $lo times"; | ||||||||||||||||||||||||||||
| 102 | } elsif ( $lo eq $hi ) { | ||||||||||||||||||||||||||||
| 103 | 1 | return "match $lo times"; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | return "match $lo to $hi times"; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 39 | ▶ | sub _min_max { | ||||||||||||||||||||||||||
|
Subroutine
_min_max: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 109 | 39 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 110 | 39 | my $content = $self->content(); | |||||||||||||||||||||||||||
| 111 | 39 | ▶ | if ( $content =~ m/ \A [{] ( .*? ) [}] \z /smx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 39 | my $quant = $1; | |||||||||||||||||||||||||||
| 113 | 39 | my ( $lo, $hi ) = split qr{ , }smx, $quant; | |||||||||||||||||||||||||||
| 114 | 39 | foreach ( $lo, $hi ) { | |||||||||||||||||||||||||||
| 115 | defined | ||||||||||||||||||||||||||||
| 116 | 78 | ▶ | or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 71 | s/ \A \s+ //smx; | |||||||||||||||||||||||||||
| 118 | 71 | s/ \s+ \z //smx; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 39 | ▶ | defined $lo | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $lo and "" ne $lo
MC/DC50%1/2
defined $lo and "" ne $lo
defined $lo
"" ne $lo
Truth table66%2/3
A and B
A= defined $loB= "" ne $lo
| |||||||||||||||||||||||||||||
| 121 | and '' ne $lo | ||||||||||||||||||||||||||||
| 122 | or $lo = 0; | ||||||||||||||||||||||||||||
| 123 | 39 | ▶ | defined $hi | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $hi and "" ne $hi
MC/DC100%2/2
defined $hi and "" ne $hi
defined $hi
"" ne $hi
Truth table100%3/3
A and B
A= defined $hiB= "" ne $hi
| |||||||||||||||||||||||||||||
| 124 | and '' ne $hi | ||||||||||||||||||||||||||||
| 125 | and return ( $lo, $hi ); | ||||||||||||||||||||||||||||
| 126 | 13 | ▶ | $quant =~ m/ , \z /smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | and return ( $lo, INFINITY ); | ||||||||||||||||||||||||||||
| 128 | 7 | return ( $lo, $lo ); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub is_quantifier { | ||||||||||||||||||||||||||
|
Subroutine
is_quantifier: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | return 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 35 | ▶ | sub width { | ||||||||||||||||||||||||||
|
Subroutine
width: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 35 | return ( 0, 0 ); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 35 | ▶ | sub __quantified_width { | ||||||||||||||||||||||||||
|
Subroutine
__quantified_width: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 141 | 35 | my ( $self, $raw_min, $raw_max ) = @_; | |||||||||||||||||||||||||||
| 142 | 35 | my ( $my_min, $my_max ) = $self->_min_max(); | |||||||||||||||||||||||||||
| 143 | 35 | foreach ( $my_min, $my_max ) { | |||||||||||||||||||||||||||
| 144 | 70 | ▶ | looks_like_number( $_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | or $_ = undef; | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 35 | ▶ | defined $raw_min | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 148 | and $raw_min = defined $my_min ? $raw_min * $my_min : undef; | ||||||||||||||||||||||||||||
| 149 | 35 | ▶ | defined $raw_max | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 150 | and $raw_max = defined $my_max ? $raw_max * $my_max : undef; | ||||||||||||||||||||||||||||
| 151 | 35 | return ( $raw_min, $raw_max ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub __following_literal_left_curly_disallowed_in { | ||||||||||||||||||||||||||
|
Subroutine
__following_literal_left_curly_disallowed_in: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 0 | return LITERAL_LEFT_CURLY_ALLOWED; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | ▶ | sub _too_big { | ||||||||||||||||||||||||||
|
Subroutine
_too_big: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 160 | 1 | STRUCTURE_UNKNOWN->__PPIX_ELEM__rebless( $self, | |||||||||||||||||||||||||||
| 161 | error => MSG_LOOK_BEHIND_TOO_LONG, | ||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||
| 163 | 1 | return 1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 29 | ▶ | sub __PPIX_LEXER__finalize { | ||||||||||||||||||||||||||
|
Subroutine
__PPIX_LEXER__finalize: called CC=14 SCAR=27.9
| |||||||||||||||||||||||||||||
| 167 | 29 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 29 | my $content = $self->content(); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 29 | ▶ | if ( $self->__in_look_behind() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 2 | ▶ | if ( $content =~ m/ \A [{] ( .*? ) [}] \z /smx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 2 | my $quant = $1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | ▶ | $quant =~ m/ , \z /smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | and return $self->_too_big(); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | my ( $lo, $hi ) = split qr{ , }smx, $quant; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | defined $hi | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or $hi = $lo; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1 | my $numeric = 1; | |||||||||||||||||||||||||||
| 184 | 1 | foreach ( $lo, $hi ) { | |||||||||||||||||||||||||||
| 185 | 2 | ▶ | if ( m/ \A [0-9]+ \z /smx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 2 | ▶ | $_ >= 256 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | and return $self->_too_big(); | ||||||||||||||||||||||||||||
| 188 | } else { | ||||||||||||||||||||||||||||
| 189 | 0 | $numeric = 0; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | ▶ | if ( $numeric && $lo != $hi ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$numeric and $lo != $hi
MC/DC0%0/2
$numeric and $lo != $hi
$numeric
$lo != $hi
Truth table33%1/3
A and B
A= $numericB= $lo != $hi
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | if ( my $finish = $self->finish() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | $finish->perl_version_introduced() lt | ||||||||||||||||||||||||||||
| 197 | VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED | ||||||||||||||||||||||||||||
| 198 | and $finish->{perl_version_introduced} = | ||||||||||||||||||||||||||||
| 199 | 1 | ▶ | VARIABLE_LENGTH_LOOK_BEHIND_INTRODUCED; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | ( $content =~ m/ \s /smx or $content =~ m/ \A \{ , /smx ) | ||||||||||||||||||||||||||||
| 207 | 28 | ▶ | and $self->finish()->{perl_version_introduced} = '5.033006'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$content =~ / \s /msx or $content =~ / \A \{ , /msx
MC/DC100%2/2
$content =~ / \s /msx or $content =~ / \A \{ , /msx
$content =~ / \s /msx
$content =~ / \A \{ , /msx
Truth table100%3/3
A or B
A= $content =~ / \s /msxB= $content =~ / \A \{ , /msx
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 28 | return 0; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # Called by the lexer to record the capture number. | ||||||||||||||||||||||||||||
| 213 | 28 | ▶ | sub __PPIX_LEXER__record_capture_number { | ||||||||||||||||||||||||||
|
Subroutine
__PPIX_LEXER__record_capture_number: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 28 | my ( undef, $number ) = @_; # Invocant unused | |||||||||||||||||||||||||||
| 215 | 28 | return $number; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 9 | 1; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | __END__ | ||||||||||||||||||||||||||||