Regexp/Token/Assertion.pm

filter: Statementstmt 96.5% 56 / 58 Branchbran 84.6% 22 / 26 Conditioncond 71.4% 10 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 87.7% 107 / 122 CC 21 SCAR 19.1
CC
21
Cov
90%
__PPIX_TOKENIZER__regexp
13.4
_perl_version_removed
4.0
_perl_version_introduced
4.0
CRAP 6.8
« Regexp/Structure/Switch.pm ↑ Summary Regexp/Token/Reference.pm »
1 =head1 NAME
2  
3 PPIx::Regexp::Token::Assertion - Represent a simple assertion.
4  
5 =head1 SYNOPSIS
6  
7 use PPIx::Regexp::Dumper;
8 PPIx::Regexp::Dumper->new( 'qr{\bfoo\b}smx' )
9 ->print();
10  
11 =head1 INHERITANCE
12  
13 C<PPIx::Regexp::Token::Assertion> is a
14 L<PPIx::Regexp::Token|PPIx::Regexp::Token>.
15  
16 C<PPIx::Regexp::Token::Assertion> has no descendants.
17  
18 =head1 DESCRIPTION
19  
20 This class represents one of the simple assertions; that is, those that
21 are not defined via parentheses. This includes the zero-width assertions
22 C<^>, C<$>, C<\b>, C<\B>, C<\A>, C<\Z>, C<\z> and C<\G>, as well as:
23  
24 =over
25  
26 =item * The C<\z> assertion added in Perl 5.005,
27  
28 =item * The C<\K> assertion added in Perl 5.009005,
29  
30 =item * The C<\b{gcb}> assertion (and friends) added in Perl 5.021009.
31 Similar braced constructions (like C<\b{foo}>) are unknown tokens.
32  
33 =back
34  
35 =head1 METHODS
36  
37 This class provides no public methods beyond those provided by its
38 superclass.
39  
40 =cut
41  
42 package PPIx::Regexp::Token::Assertion;
43  
44 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 9 ▶use base qw{ PPIx::Regexp::Token };
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 9 ▶use PPIx::Regexp::Constant qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
50 COOKIE_CLASS
51 COOKIE_LOOKAROUND_ASSERTION
52 LITERAL_LEFT_CURLY_ALLOWED
53 MINIMUM_PERL
54 TOKEN_LITERAL
55 TOKEN_UNKNOWN
56 @CARP_NOT
57 9 };
58  
59 9 ▶use constant KEEP_EXPLANATION => 'In s///, keep everything before the \\K';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 9 our $VERSION = '0.092';
62  
63 # Return true if the token can be quantified, and false otherwise
64 # sub can_be_quantified { return };
65  
66 9 my @braced_assertions = (
67 [ qr< \\ [bB] [{] (?: g | gcb | wb | sb ) [}] >smx, '5.021009' ],
68 [ qr< \\ [bB] [{] (?: lb ) [}] >smx, '5.023007' ],
69 [ qr< \\ [bB] [{] .*? [}] >smx, undef, TOKEN_UNKNOWN,
70 { error => 'Unknown bound type' },
71 ],
72 );
73  
74 =head2 is_matcher
75  
76 This method returns a true value because an assertion actually matches
77 something.
78  
79 =cut
80  
81 0 ▶sub is_matcher { return 1; }
Subroutine is_matcher: not called CC=1 SCAR=6.9
Pod documented
82  
83 24 ▶sub perl_version_introduced {
Subroutine perl_version_introduced: called CC=2 SCAR=8.1
Pod documented
84 24 my ( $self ) = @_;
85 return ( $self->{perl_version_introduced} ||=
86 24 ▶ $self->_perl_version_introduced() );
Condition33%1/3
$self->{"perl_version_introduced"} ||= $self->_perl_version_introduced
l!l&&r!l&&!r
0240
MC/DC0%0/2
$self->{"perl_version_introduced"} ||= $self->_perl_version_introduced
$self->{"perl_version_introduced"} $self->_perl_version_introduced
Truth table33%1/3
A ||= B
A= $self->{"perl_version_introduced"}B= $self->_perl_version_introduced
ABresult
1X1
011
000
87 }
88  
89 {
90  
91 9 my %perl_version_introduced = (
92 '\\K' => '5.009005',
93 '\\z' => '5.005',
94 );
95  
96 24 ▶ sub _perl_version_introduced {
Subroutine _perl_version_introduced: called CC=4 SCAR=13.9
97 24 my ( $self ) = @_;
98 24 my $content = $self->content();
99 24 foreach my $item ( @braced_assertions ) {
100 54 ▶ $content =~ m/ \A $item->[0] \z /smx
Branch: 1 branch
BranchTrueFalseTotal
$content =~ / \A $item->[0] \z /msx and return $item->[1] 10 44 54
101 and return $item->[1];
102 }
103 14 ▶ return $perl_version_introduced{ $content } || MINIMUM_PERL;
Condition100%2/2
$perl_version_introduced{$content} || '5.000'
l!l
68
MC/DC100%1/1
$perl_version_introduced{$content} || '5.000'
$perl_version_introduced{$content}
Truth table100%2/2
A
A= $perl_version_introduced{$content}
Aresult
11
00
104 }
105  
106 }
107  
108 9 ▶sub perl_version_removed {
Subroutine perl_version_removed: called CC=2 SCAR=7.0
Pod documented
109 24 my ( $self ) = @_;
110 return ( $self->{perl_version_removed} ||=
111 24 ▶ $self->_perl_version_removed() );
Condition66%2/3
$self->{"perl_version_removed"} ||= $self->_perl_version_removed
l!l&&r!l&&!r
3021
MC/DC50%1/2
$self->{"perl_version_removed"} ||= $self->_perl_version_removed
$self->{"perl_version_removed"} $self->_perl_version_removed
Truth table66%2/3
A ||= B
A= $self->{"perl_version_removed"}B= $self->_perl_version_removed
ABresult
1X1
011
000
112 }
113  
114 21 ▶sub _perl_version_removed {
Subroutine _perl_version_removed: called CC=4 SCAR=13.9
115 21 my ( $self ) = @_;
116 21 ▶ if ( '\\K' eq $self->content() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("\\K" eq $self->content) 3 18 21
117 3 my $parent = $self;
118 3 ▶ while ( $parent = $parent->parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent = $parent->parent) 5 3 8
119 5 ▶ $parent->isa( 'PPIx::Regexp::Structure::Assertion' )
Branch: 1 branch
BranchTrueFalseTotal
$parent->isa("PPIx::Regexp::Structure::Assertion") and return "5.031003" 0 5 5
120 and return '5.031003';
121 }
122 }
123 21 return $self->SUPER::perl_version_removed();
124 }
125  
126 {
127 9 my %explanation = (
128 '$' => 'Assert position is at end of string or newline',
129 '\\A' => 'Assert position is at beginning of string',
130 '\\B' => 'Assert position is not at word/nonword boundary',
131 '\\B{gcb}' => 'Assert position is not at grapheme cluster boundary',
132 '\\B{g}' => 'Assert position is not at grapheme cluster boundary',
133 '\\B{lb}' => 'Assert position is not at line boundary',
134 '\\B{sb}' => 'Assert position is not at sentence boundary',
135 '\\B{wb}' => 'Assert position is not at word boundary',
136 '\\G' => 'Assert position is at pos()',
137 '\\K' => KEEP_EXPLANATION,
138 '\\Z' => 'Assert position is at end of string, or newline before end',
139 '\\b' => 'Assert position is at word/nonword boundary',
140 '\\b{gcb}' => 'Assert position is at grapheme cluster boundary',
141 '\\b{g}' => 'Assert position is at grapheme cluster boundary',
142 '\\b{lb}' => 'Assert position is at line boundary',
143 '\\b{sb}' => 'Assert position is at sentence boundary',
144 '\\b{wb}' => 'Assert position is at word boundary',
145 '\\z' => 'Assert position is at end of string',
146 '^' => 'Assert position is at beginning of string or after newline',
147 );
148  
149 20 ▶ sub __explanation {
Subroutine __explanation: called CC=1 SCAR=0.0
150 20 return \%explanation;
151 }
152 }
153  
154 # An un-escaped literal left curly bracket can always follow this
155 # element.
156 9 ▶sub __following_literal_left_curly_disallowed_in {
Subroutine __following_literal_left_curly_disallowed_in: called CC=1 SCAR=0.0
157 1 return LITERAL_LEFT_CURLY_ALLOWED;
158 }
159  
160 # By logic we should handle '$' here. But
161 # PPIx::Regexp::Token::Interpolation needs to process it to see if it is
162 # a sigil. If it is not, that module is expected to make it into an
163 # assertion. This is to try to keep the order in which the tokenizers
164 # are called non-critical, and try to keep all processing for a
165 # character in one place. Except for the back slash, which gets in
166 # everywhere.
167 #
168 ## my %assertion = map { $_ => 1 } qw{ ^ $ };
169 9 my %assertion = map { $_ => 1 } qw{ ^ };
170 9 my %escaped = map { $_ => 1 } qw{ b B A Z z G K };
171  
172 153 ▶sub __PPIX_TOKENIZER__regexp {
Subroutine __PPIX_TOKENIZER__regexp: called CC=13 SCAR=25.9
173 153 my ( undef, $tokenizer, $character ) = @_;
174  
175 # Inside a character class, these are all literals.
176 153 ▶ my $make = $tokenizer->cookie( COOKIE_CLASS ) ?
Branch: 1 branch
BranchTrueFalseTotal
$tokenizer->cookie(']') ? : 23 130 153
177 TOKEN_LITERAL :
178 __PACKAGE__;
179  
180 # '^' and '$'. Or at least '^'. See note above for '$'.
181 153 ▶ $assertion{$character}
Branch: 1 branch
BranchTrueFalseTotal
$assertion{$character} and return $tokenizer->make_token(1, $make) 11 142 153
182 and return $tokenizer->make_token( 1, $make );
183  
184 142 ▶ $character eq '\\' or return;
Branch: 1 branch
BranchTrueFalseTotal
$character eq "\\" or return 82 60 142
185  
186 82 ▶ defined ( my $next = $tokenizer->peek( 1 ) ) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $next = $tokenizer->peek(1)) or return 82 0 82
187  
188 # Handle assertions of the form \b{gcb} and friends, introduced in
189 # Perl 5.21.9. These are not recognized inside square bracketed
190 # character classes, where \b is not an assertion but a backspace
191 # character.
192 82 ▶ if ( __PACKAGE__ eq $make ) { # Only outside [...]
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__ eq $make) 82 0 82
193 82 foreach my $item ( @braced_assertions ) {
194 228 ▶ my $end = $tokenizer->find_regexp( qr/ \A $item->[0] /smx )
Branch: 1 branch
BranchTrueFalseTotal
my $end = $tokenizer->find_regexp(qr/ \A $item->[0] /msx) or next 10 218 228
195 or next;
196 10 ▶ $item->[2]
Branch: 1 branch
BranchTrueFalseTotal
$item->[2] or return $end 0 10 10
197 or return $end;
198 0 return $tokenizer->make_token( $end, $item->[2], $item->[3] );
199 }
200 }
201  
202 # We special-case '\K' because it was retracted inside look-around
203 # assertions in 5.31.3.
204 72 ▶ if ( 'K' eq $next && __PACKAGE__ eq $make &&
Branch: 1 branch
BranchTrueFalseTotal
if ("K" eq $next and __PACKAGE__ eq $make and $tokenizer->__cookie_exists("lookaround")) 3 69 72
Condition83%5/6
"K" eq $next and __PACKAGE__ eq $make and $tokenizer->__cookie_exists("lookaround")
!ll&&!rl&&r
6363
"K" eq $next and __PACKAGE__ eq $make
!ll&&!rl&&r
6309
MC/DC66%2/3
"K" eq $next and __PACKAGE__ eq $make and $tokenizer->__cookie_exists("lookaround")
"K" eq $next __PACKAGE__ eq $make $tokenizer->__cookie_exists("lookaround")
Truth table75%3/4
A and B and C
A= "K" eq $nextB= __PACKAGE__ eq $makeC= $tokenizer->__cookie_exists("lookaround")
ABCresult
0XX0
10X0
1100
1111
205 $tokenizer->__cookie_exists( COOKIE_LOOKAROUND_ASSERTION ) ) {
206 3 return $tokenizer->make_token( 2, $make, {
207 perl_version_removed => '5.031003',
208 explanation => KEEP_EXPLANATION .
209 '; retracted inside look-around assertion',
210 },
211 );
212 }
213  
214 69 ▶ $escaped{$next}
Branch: 1 branch
BranchTrueFalseTotal
$escaped{$next} and return $tokenizer->make_token(2, $make) 32 37 69
215 and return $tokenizer->make_token( 2, $make );
216  
217 37 return;
218 }
219  
220 9 1;
221  
222 __END__
« Regexp/Structure/Switch.pm ↑ Summary Regexp/Token/Reference.pm »