Regexp/Token/Code.pm

filter: Statementstmt 93.5% 72 / 77 Branchbran 68.7% 22 / 32 Conditioncond 44.4% 8 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 94.4% 17 / 18 Podpod 100% 5 / 5 totaltotal 79.2% 126 / 159 CC 23 SCAR 20.2
CC
23
Cov
80%
ppi
16.5
__new
7.1
content
4.5
CRAP 7.6
« Regexp/Lexer.pm ↑ Summary Regexp/Util.pm »
1 =head1 NAME
2  
3 PPIx::Regexp::Token::Code - Represent a chunk of Perl embedded in a regular expression.
4  
5 =head1 SYNOPSIS
6  
7 use PPIx::Regexp::Dumper;
8 PPIx::Regexp::Dumper->new(
9 'qr{(?{print "hello sailor\n"})}smx')->print;
10  
11 =head1 INHERITANCE
12  
13 C<PPIx::Regexp::Token::Code> is a
14 L<PPIx::Regexp::Token|PPIx::Regexp::Token>.
15  
16 C<PPIx::Regexp::Token::Code> is the parent of
17 L<PPIx::Regexp::Token::Interpolation|PPIx::Regexp::Token::Interpolation>.
18  
19 =head1 DESCRIPTION
20  
21 This class represents a chunk of Perl code embedded in a regular
22 expression. Specifically, it results from parsing things like
23  
24 (?{ code })
25 (??{ code })
26  
27 or from the replacement side of an s///e. Technically, interpolations
28 are also code, but they parse differently and therefore end up in a
29 different token.
30  
31 This token may not appear inside a regex set (i.e. C<(?[ ... ])>. If
32 found, it will become a C<PPIx::Regexp::Token::Unknown>.
33  
34 =head1 METHODS
35  
36 This class provides the following public methods. Methods not documented
37 here are private, and unsupported in the sense that the author reserves
38 the right to change or remove them without notice.
39  
40 =cut
41  
42 package PPIx::Regexp::Token::Code;
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 PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 9 ▶use PPIx::Regexp::Constant qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
51 COOKIE_REGEX_SET
52 LOCATION_COLUMN
53 LOCATION_LOGICAL_LINE
54 LOCATION_LOGICAL_FILE
55 @CARP_NOT
56 9 };
57 9 ▶use PPIx::Regexp::Util qw{ __instance };
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 9 our $VERSION = '0.092';
60  
61 9 ▶use constant TOKENIZER_ARGUMENT_REQUIRED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 9 ▶use constant VERSION_WHEN_IN_REGEX_SET => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 147 ▶sub __new {
Subroutine __new: called CC=7 SCAR=19.7
65 147 my ( $class, $content, %arg ) = @_;
66  
67 defined $arg{perl_version_introduced}
68 147 ▶ or $arg{perl_version_introduced} = '5.005';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{"perl_version_introduced"} or $arg{"perl_version_introduced"} = "5.005" 116 31 147
69  
70 147 my $self = $class->SUPER::__new( $content, %arg );
71  
72 # TODO sort this out, since Token::Interpolation is a subclass, and
73 # those are legal in regex sets
74 147 ▶ if ( $arg{tokenizer}->cookie( COOKIE_REGEX_SET ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"tokenizer"}->cookie("])")) 1 146 147
75 1 ▶ my $ver = $self->VERSION_WHEN_IN_REGEX_SET()
Branch: 1 branch
BranchTrueFalseTotal
my $ver = $self->VERSION_WHEN_IN_REGEX_SET or return $self->__error("Code token not valid in Regex set") 1 0 1
76 or return $self->__error( 'Code token not valid in Regex set' );
77 $self->{perl_version_introduced} < $ver
78 1 ▶ and $self->{perl_version_introduced} = $ver;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"perl_version_introduced"} < $ver and $self->{"perl_version_introduced"} = $ver 1 0 1
79 }
80  
81 $arg{tokenizer}->__recognize_postderef( $self )
82 and $self->{perl_version_introduced} < 5.019005
83 147 ▶ and $self->{perl_version_introduced} = '5.019005';
Branch: 1 branch
BranchTrueFalseTotal
$arg{"tokenizer"}->__recognize_postderef($self) and $self->{"perl_version_introduced"} < "5.019005" and $self->{"perl_version_introduced"} = "5.019005" 9 138 147
Condition66%2/3
$arg{"tokenizer"}->__recognize_postderef($self) and $self->{"perl_version_introduced"} < "5.019005"
!ll&&!rl&&r
13809
MC/DC50%1/2
$arg{"tokenizer"}->__recognize_postderef($self) and $self->{"perl_version_introduced"} < "5.019005"
$arg{"tokenizer"}->__recognize_postderef($self) $self->{"perl_version_introduced"} < "5.019005"
Truth table66%2/3
A and B
A= $arg{"tokenizer"}->__recognize_postderef($self)B= $self->{"perl_version_introduced"} < "5.019005"
ABresult
0X0
100
111
84  
85 147 return $self;
86 }
87  
88 271 ▶sub content {
Subroutine content: called CC=3 SCAR=15.1
Pod documented
89 271 my ( $self ) = @_;
90 271 ▶ if ( exists $self->{content} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'content'}) { } 271 0 271
elsif (exists $self->{'ppi'}) { } 0 0 0
91 271 return $self->{content};
92 } elsif ( exists $self->{ppi} ) {
93 0 return ( $self->{content} = $self->{ppi}->content() );
94 } else {
95 0 return;
96 }
97 }
98  
99 1 ▶sub explain {
Subroutine explain: called CC=1 SCAR=0.0
Pod documented
100 1 return 'Perl expression';
101 }
102  
103 =head2 is_matcher
104  
105 This method returns C<undef> because a static analysis can not in
106 general tell whether an interpolated value matches anything.
107  
108 =cut
109  
110 0 ▶sub is_matcher { return undef; } ## no critic (ProhibitExplicitReturnUndef)
Subroutine is_matcher: not called CC=1 SCAR=6.9
Pod documented
111  
112 =head2 ppi
113  
114 This convenience method returns the L<PPI::Document|PPI::Document>
115 representing the content. This document should be considered read only.
116  
117 B<Note> that if the location of the invocant is available the PPI
118 document will have stuff prefixed to it to make the location of the
119 tokens in the new document consistent with the location. This "stuff"
120 will include at least a C<#line> directive, and maybe leading white
121 space.
122  
123 =cut
124  
125 150 ▶sub ppi {
Subroutine ppi: called CC=13 SCAR=28.0
Pod documented
126 150 my ( $self ) = @_;
127 150 ▶ if ( exists $self->{ppi} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'ppi'}) { } 5 145 150
elsif (exists $self->{'content'}) { } 145 0 145
128 5 return $self->{ppi};
129 } elsif ( exists $self->{content} ) {
130 145 my $content;
131 145 my $location = $self->{location};
132 145 ▶ if ( $location ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location) 2 143 145
133 2 my $fn;
134 2 ▶ if( defined( $fn = $location->[LOCATION_LOGICAL_FILE] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($fn = $location->[4])) { } 2 0 2
135 2 $fn =~ s/ (?= [\\"] ) /\\/smxg;
136 2 $content = qq{#line $location->[LOCATION_LOGICAL_LINE] "$fn"\n};
137 } else {
138 0 $content = qq{#line $location->[LOCATION_LOGICAL_LINE]\n};
139 }
140 2 $content .= ' ' x ( $location->[LOCATION_COLUMN] - 1 );
141 }
142  
143 145 $content .= $self->__ppi_normalize_content();
144  
145 145 $self->{ppi} = PPI::Document->new( \$content );
146  
147 145 ▶ if ( $location ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location) 2 143 145
148 # Generate locations now.
149 2 $self->{ppi}->location();
150 # Remove the stuff we originally injected. NOTE that we can
151 # only get away with doing this if the removal does not
152 # invalidate the locations of the other tokens that we just
153 # generated.
154 2 my $elem;
155 # Remove the '#line' directive if we find it
156 2 ▶ $elem = $self->{ppi}->child( 0 )
Branch: 1 branch
BranchTrueFalseTotal
$elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Comment") and $elem->content =~ / \A \#line\b /msx and $elem->remove 2 0 2
Condition33%2/6
$elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Comment") and $elem->content =~ / \A \#line\b /msx
!ll&&!rl&&r
002
$elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Comment")
!ll&&!rl&&r
002
MC/DC0%0/3
$elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Comment") and $elem->content =~ / \A \#line\b /msx
$elem = $self->{"ppi"}->child(0) $elem->isa("PPI::Token::Comment") $elem->content =~ / \A \#line\b /msx
Truth table25%1/4
A and B and C
A= $elem = $self->{"ppi"}->child(0)B= $elem->isa("PPI::Token::Comment")C= $elem->content =~ / \A \#line\b /msx
ABCresult
0XX0
10X0
1100
1111
157 and $elem->isa( 'PPI::Token::Comment' )
158 and $elem->content() =~ m/ \A \#line\b /smx
159 and $elem->remove();
160 # Remove the white space if we find it, and if it in fact
161 # represents only the white space we injected to get the
162 # column numbers right.
163 2 my $wid = $location->[LOCATION_COLUMN] - 1;
164 $wid
165 2 ▶ and $elem = $self->{ppi}->child( 0 )
Branch: 1 branch
BranchTrueFalseTotal
$wid and $elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Whitespace") and $wid == length $elem->content and $elem->remove 1 1 2
Condition44%4/9
$wid and $elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Whitespace") and $wid == length $elem->content
!ll&&!rl&&r
011
$wid and $elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
002
$wid and $elem = $self->{"ppi"}->child(0)
!ll&&!rl&&r
002
MC/DC25%1/4
$wid and $elem = $self->{"ppi"}->child(0) and $elem->isa("PPI::Token::Whitespace") and $wid == length $elem->content
$wid $elem = $self->{"ppi"}->child(0) $elem->isa("PPI::Token::Whitespace") $wid == length $elem->content
Truth table40%2/5
A and B and C and D
A= $widB= $elem = $self->{"ppi"}->child(0)C= $elem->isa("PPI::Token::Whitespace")D= $wid == length $elem->content
ABCDresult
0XXX0
10XX0
110X0
11100
11111
166 and $elem->isa( 'PPI::Token::Whitespace' )
167 and $wid == length $elem->content()
168 and $elem->remove();
169 }
170  
171 145 return $self->{ppi};
172  
173 } else {
174 0 return;
175 }
176 }
177  
178 21 ▶sub width {
Subroutine width: called CC=1 SCAR=0.0
Pod documented
179 21 return ( undef, undef );
180 }
181  
182 52 ▶sub __ppi_normalize_content {
Subroutine __ppi_normalize_content: called CC=1 SCAR=0.0
183 52 my ( $self ) = @_;
184 52 return $self->{content};
185 }
186  
187 # Return true if the token can be quantified, and false otherwise
188 # sub can_be_quantified { return };
189  
190 {
191 9 ▶ no warnings qw{ qw }; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
192  
193 9 my %accept = map { $_ => 1 } qw{ $ $# @ % & * };
194  
195 # Say what casts are accepted, since not all are in an
196 # interpolation.
197 41 ▶ sub __postderef_accept_cast {
Subroutine __postderef_accept_cast: called CC=1 SCAR=0.0
198 41 return \%accept;
199 }
200 }
201  
202 9 ▶sub __PPIX_TOKENIZER__regexp {
Subroutine __PPIX_TOKENIZER__regexp: called CC=3 SCAR=11.3
203 14 my ( undef, $tokenizer, $character ) = @_;
204  
205 14 ▶ $character eq '{' or return;
Branch: 1 branch
BranchTrueFalseTotal
$character eq "{" or return 14 0 14
206  
207 14 ▶ my $offset = $tokenizer->find_matching_delimiter()
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $tokenizer->find_matching_delimiter or return 14 0 14
208 or return;
209  
210 14 return $offset + 1; # to include the closing delimiter.
211 }
212  
213 9 1;
214  
215 __END__
« Regexp/Lexer.pm ↑ Summary Regexp/Util.pm »