M4/Impl/Regexp.pm

filter: Statementstmt 98.1% 320 / 326 Branchbran 62.2% 66 / 106 Conditioncond 38.1% 8 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 98.5% 69 / 70 Podpod n/a totaltotal 86.4% 464 / 537 CC 61 SCAR 23.9
CC
61
Cov
87%
regexp_substitute
29.3
regexp_exec
18.0
regexp_compile
17.9
CRAP 10.9
« M4/Impl/Default/BaseConversion.pm ↑ Summary M4/Impl/Parser/Actions.pm »
1 4 ▶use Moops;
Subroutine __ANON__: called CC=1 SCAR=0.0
2  
3 # PODNAME: MarpaX::Languages::M4::Impl::Regexp
4  
5 # ABSTRACT: M4 Regexp generic implementation
6 4  
7 4 ▶class MarpaX::Languages::M4::Impl::Regexp {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.020'; # VERSION
10  
11 3 our $AUTHORITY = 'cpan:JDDPAUSE'; # AUTHORITY
12  
13 4 ▶ use MarpaX::Languages::M4::Role::Regexp;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 ▶ use MarpaX::Languages::M4::Type::Regexp -all;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 3 ▶ use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
16 3 ▶ use Types::Common::Numeric -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 3 has _regexp_type => (
19 is => 'rwp',
20 isa => M4RegexpType
21 );
22  
23 3 has _regexp => (
24 is => 'rwp',
25 isa => RegexpRef
26 );
27  
28 3 has regexp_lpos => (
29 is => 'rwp',
30 isa => ArrayRef,
31 handles_via => 'Array',
32 handles => {
33 'regexp_lpos_count' => 'count',
34 'regexp_lpos_get' => 'get'
35 }
36 );
37  
38 3 has regexp_rpos => (
39 is => 'rwp',
40 isa => ArrayRef,
41 handles_via => 'Array',
42 handles => {
43 'regexp_rpos_count' => 'count',
44 'regexp_rpos_get' => 'get'
45 }
46 );
47  
48 3 ▶ method regexp_compile (ConsumerOf['MarpaX::Languages::M4::Role::Impl'] $impl, M4RegexpType $regexpType, Str $regexpString --> Bool) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine regexp_compile: called CC=17 SCAR=28.9
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 11 branches
BranchTrueFalseTotal
@_ ? : 190 0 190
if @_ != 3 0 190 190
$#_ >= 0 ? : 190 0 190
if ($#_ >= 0) 190 0 190
do { builtin::blessed($impl) and do { my $method = $impl->can("DOES") || $impl->can("isa"); $impl->$method("MarpaX::Languages::M4::Role::Impl") } } or _failed_check(108, "ConsumerOf[\"MarpaX::Languages::M4::Role::Impl\"]", $impl, "exception_class", "Error::TypeTiny::Assertion") 190 0 190
$#_ >= 1 ? : 190 0 190
if ($#_ >= 1) 190 0 190
Type::Tiny::XS::AUTO::TC11($regexpType) or _failed_check(122, "M4RegexpType", $regexpType, "exception_class", "Error::TypeTiny::Assertion") 190 0 190
$#_ >= 2 ? : 190 0 190
if ($#_ >= 2) 190 0 190
Type::Tiny::XS::Str($regexpString) or _failed_check(9, "Str", $regexpString, "exception_class", "Error::TypeTiny::Assertion") 190 0 190
Condition33%2/6
builtin::blessed($impl) && do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
!ll&&!rl&&r
00190
$impl->can('DOES') || $impl->can('isa')
l!l&&r!l&&!r
19000
MC/DC0%0/2
builtin::blessed($impl) && do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
builtin::blessed($impl) do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
MC/DC0%0/2
$impl->can('DOES') || $impl->can('isa')
$impl->can('DOES') $impl->can('isa')
Truth table33%1/3
A && B
A= builtin::blessed($impl)B= do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $impl->can('DOES')B= $impl->can('isa')
ABresult
1X1
011
000
49  
50 190 my $regexp;
51  
52 190 my $hasPreviousRegcomp = exists( $^H{regcomp} );
53 190 ▶ my $previousRegcomp = $hasPreviousRegcomp ? $^H{regcomp} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hasPreviousRegcomp ? : 0 190 190
54  
55 190 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
56 #
57 # Some versions of perl warn, some others don't -;
58 # We are only interested by real failures.
59 #
60 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
61 190 ▶ if ( $regexpType eq 'perl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regexpType eq 'perl') { } 154 36 190
62 #
63 # Just make sure this really is perl
64 #
65 154 delete( $^H{regcomp} );
66 #
67 # regexp can be empty and perl have a very special
68 # behaviour in this case. Avoid empty regexp.
69 #
70 154 $regexp = qr/$regexpString(?#)/sm;
71 }
72 else {
73 3 ▶ use re::engine::GNU 0.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
74 36 $regexp = qr/$regexpString/sm;
75 3 ▶ no re::engine::GNU;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
76 }
77  
78 }
79 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 1 $impl->logger_error( '%s: %s',
81 $impl->impl_quote($regexpString), $_ );
82 190 };
83  
84 $hasPreviousRegcomp
85 ? $^H{regcomp}
86 = $previousRegcomp
87 190 ▶ : delete( $^H{regcomp} );
Branch: 1 branch
BranchTrueFalseTotal
$hasPreviousRegcomp ? : 0 190 190
88  
89 190 ▶ if ( defined($regexp) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regexp) { } 189 1 190
90 189 $self->_set__regexp($regexp);
91 189 $self->_set__regexp_type($regexpType);
92 189 return true;
93 }
94 else {
95 1 return false;
96 }
97 }
98  
99 #
100 # Return value is:
101 # -2 if failure (the engine croaked)
102 # -1 if match failed
103 # >=0 Position where it matches
104 #
105 3 ▶ method regexp_exec (ConsumerOf['MarpaX::Languages::M4::Role::Impl'] $impl, Str $string, PositiveOrZeroInt $pos? --> Int) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine regexp_exec: called CC=17 SCAR=28.9
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 12 branches
BranchTrueFalseTotal
@_ ? : 15217 0 15217
if @_ < 2 0 15217 15217
if @_ > 3 0 15217 15217
$#_ >= 0 ? : 15217 0 15217
if ($#_ >= 0) 15217 0 15217
do { builtin::blessed($impl) and do { my $method = $impl->can("DOES") || $impl->can("isa"); $impl->$method("MarpaX::Languages::M4::Role::Impl") } } or _failed_check(108, "ConsumerOf[\"MarpaX::Languages::M4::Role::Impl\"]", $impl, "exception_class", "Error::TypeTiny::Assertion") 15217 0 15217
$#_ >= 1 ? : 15217 0 15217
if ($#_ >= 1) 15217 0 15217
Type::Tiny::XS::Str($string) or _failed_check(9, "Str", $string, "exception_class", "Error::TypeTiny::Assertion") 15217 0 15217
$#_ >= 2 ? : 15190 27 15217
if ($#_ >= 2) 15190 27 15217
Type::Tiny::XS::PositiveOrZeroInt($pos) or _failed_check(48, "PositiveOrZeroInt", $pos, "exception_class", "Error::TypeTiny::Assertion") 15190 0 15190
Condition33%2/6
builtin::blessed($impl) && do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
!ll&&!rl&&r
0015217
$impl->can('DOES') || $impl->can('isa')
l!l&&r!l&&!r
1521700
MC/DC0%0/2
builtin::blessed($impl) && do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
builtin::blessed($impl) do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
MC/DC0%0/2
$impl->can('DOES') || $impl->can('isa')
$impl->can('DOES') $impl->can('isa')
Truth table33%1/3
A && B
A= builtin::blessed($impl)B= do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $impl->can('DOES')B= $impl->can('isa')
ABresult
1X1
011
000
106  
107 15217 pos($string) = $pos; # undef is ok
108 15217 my $rc = -1;
109  
110 #
111 # Just make sure this really is perl
112 #
113 15217 my $hasPreviousRegcomp = exists( $^H{regcomp} );
114 15217 ▶ my $previousRegcomp = $hasPreviousRegcomp ? $^H{regcomp} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hasPreviousRegcomp ? : 0 15217 15217
115  
116 #
117 # Note: this looks like duplicated code, and it is.
118 # But this cannot be avoided because $-/$+ are
119 # lexically scoped, and our scope depend on the engine
120 #
121 15217 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=13.9
122 #
123 # Some versions of perl warn, some others don't -;
124 # We are only interested by real failures.
125 #
126 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
127 15217 my $regexp = $self->_regexp;
128 15217 ▶ if ( $self->_regexp_type eq 'perl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_regexp_type eq 'perl') { } 15073 144 15217
129 #
130 # Just make sure this really is perl
131 #
132 15073 delete( $^H{regcomp} );
133 #
134 # Execute perl engine
135 #
136 15073 ▶ if ( $string =~ m/$regexp/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regexp/cgu) 14742 331 15073
137 #
138 # From profiling point of view this is one of the deepests
139 # method, affecting everything. So we want to have no
140 # penalty whatsoever.
141 #
142 # my @lpos = ();
143 # my @rpos = ();
144 # map { ( $lpos[$_], $rpos[$_] ) = ( $-[$_], $+[$_] ) }
145 # ( 0 .. $#- );
146 #
147 # $self->_set_regexp_lpos( \@lpos );
148 # $self->_set_regexp_rpos( \@rpos );
149 # $rc = $self->regexp_lpos_get(0);
150  
151 14742 $self->{regexp_lpos} = [ @- ];
152 14742 $self->{regexp_rpos} = [ @+ ];
153 14742 $rc = $-[0];
154 }
155 }
156 else {
157 3 ▶ use re::engine::GNU 0.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
158 #
159 # Execute re::engine::GNU engine
160 #
161 144 ▶ if ( $string =~ m/$regexp/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regexp/cgu) 108 36 144
162 #
163 # Same remark as before
164 #
165 # my @lpos = ();
166 # my @rpos = ();
167 # map { ( $lpos[$_], $rpos[$_] ) = ( $-[$_], $+[$_] ) }
168 # ( 0 .. $#- );
169 #
170 # $self->_set_regexp_lpos( \@lpos );
171 # $self->_set_regexp_rpos( \@rpos );
172 # $rc = $self->regexp_lpos_get(0);
173  
174 108 $self->{regexp_lpos} = [ @- ];
175 108 $self->{regexp_rpos} = [ @+ ];
176 108 $rc = $-[0];
177 }
178 3 ▶ no re::engine::GNU;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
179 }
180 }
181 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 0 my $regexp = $self->_regexp;
183 0 $impl->logger_error( '%s =~ %s: %s', $impl->impl_quote($string),
184 "$regexp", $_ );
185 0 $rc = -2;
186 15217 };
187  
188 $hasPreviousRegcomp
189 ? $^H{regcomp}
190 = $previousRegcomp
191 15217 ▶ : delete( $^H{regcomp} );
Branch: 1 branch
BranchTrueFalseTotal
$hasPreviousRegcomp ? : 0 15217 15217
192  
193 15217 return $rc;
194 }
195  
196 #
197 # A perl version of GNU M4's internal
198 # substitute routine
199 #
200 3 ▶ method regexp_substitute (ConsumerOf['MarpaX::Languages::M4::Role::Impl'] $impl, Str $victim, Str $repl --> Str) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine regexp_substitute: called CC=25 SCAR=33.8
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 11 branches
BranchTrueFalseTotal
@_ ? : 45 0 45
if @_ != 3 0 45 45
$#_ >= 0 ? : 45 0 45
if ($#_ >= 0) 45 0 45
do { builtin::blessed($impl) and do { my $method = $impl->can("DOES") || $impl->can("isa"); $impl->$method("MarpaX::Languages::M4::Role::Impl") } } or _failed_check(108, "ConsumerOf[\"MarpaX::Languages::M4::Role::Impl\"]", $impl, "exception_class", "Error::TypeTiny::Assertion") 45 0 45
$#_ >= 1 ? : 45 0 45
if ($#_ >= 1) 45 0 45
Type::Tiny::XS::Str($victim) or _failed_check(9, "Str", $victim, "exception_class", "Error::TypeTiny::Assertion") 45 0 45
$#_ >= 2 ? : 45 0 45
if ($#_ >= 2) 45 0 45
Type::Tiny::XS::Str($repl) or _failed_check(9, "Str", $repl, "exception_class", "Error::TypeTiny::Assertion") 45 0 45
Condition33%2/6
builtin::blessed($impl) && do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
!ll&&!rl&&r
0045
$impl->can('DOES') || $impl->can('isa')
l!l&&r!l&&!r
4500
MC/DC0%0/2
builtin::blessed($impl) && do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
builtin::blessed($impl) do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
MC/DC0%0/2
$impl->can('DOES') || $impl->can('isa')
$impl->can('DOES') $impl->can('isa')
Truth table33%1/3
A && B
A= builtin::blessed($impl)B= do { my $method = $impl->can('DOES') || $impl->can('isa'); $impl->$method('MarpaX::Languages::M4::Role::Impl') }
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $impl->can('DOES')B= $impl->can('isa')
ABresult
1X1
011
000
201 45 my $rc = '';
202 45 my $replPos = 0;
203 45 my $maxReplPos = length($repl) - 1;
204 45 my $maxIndice = $self->regexp_lpos_count - 1;
205 45 my %warned = ();
206  
207 45 ▶ while ( $replPos <= $maxReplPos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replPos <= $maxReplPos) 88 13 101
208 88 my $backslashPos = index( $repl, '\\', $replPos );
209 88 ▶ if ( $backslashPos < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backslashPos < 0) 29 59 88
210 29 $rc .= substr( $repl, $replPos );
211 29 last;
212 }
213 59 $rc .= substr( $repl, $replPos, $backslashPos - $replPos );
214 59 $replPos = $backslashPos;
215 59 my $ch = substr( $repl, ++$replPos, 1 );
216 59 ▶ if ( $replPos > $maxReplPos ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($replPos > $maxReplPos) { } 3 56 59
elsif ($ch eq '0' or $ch eq '&') { } 14 42 56
elsif ($ch =~ /[1-9]/u) { } 33 9 42
217 3 $impl->logger_warn( 'trailing %s ignored in replacement',
218 '\\' );
219 3 $warned{undef} = 1;
220 3 last;
221 }
222 ▶ elsif ( $ch eq '0' || $ch eq '&' ) {
Condition66%2/3
$ch eq 0 or $ch eq "&"
l!l&&r!l&&!r
01442
MC/DC50%1/2
$ch eq 0 or $ch eq "&"
$ch eq 0 $ch eq "&"
Truth table66%2/3
A or B
A= $ch eq 0B= $ch eq "&"
ABresult
1X1
011
000
223 14 ▶ if ( $ch eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq 0) 0 14 14
224 0 ▶ if ( !$warned{$ch} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned{$ch}) 0 0 0
225 0 $impl->logger_warn('\\0 should be replaced by \\&');
226 0 $warned{$ch} = 1;
227 }
228 }
229 14 $rc .= substr(
230 $victim,
231 $self->regexp_lpos_get(0),
232 $self->regexp_rpos_get(0) - $self->regexp_lpos_get(0)
233 );
234 14 ++$replPos;
235 }
236 elsif ( $ch =~ /[1-9]/ ) {
237 33 ▶ if ( $maxIndice < $ch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxIndice < $ch) { } 8 25 33
238 8 ▶ if ( !$warned{$ch} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned{$ch}) 8 0 8
239 8 $impl->logger_warn( 'sub-expression %d not present',
240 $ch );
241 8 $warned{$ch} = 1;
242 }
243 }
244 else {
245 25 my $rpos = $self->regexp_rpos_get($ch);
246 25 ▶ if ( $rpos > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rpos > 0) 23 2 25
247 23 $rc .= substr( $victim, $self->regexp_lpos_get($ch),
248 $self->regexp_rpos_get($ch)
249 - $self->regexp_lpos_get($ch) );
250 }
251 }
252 33 ++$replPos;
253 }
254 else {
255 9 $rc .= $ch;
256 9 ++$replPos;
257 }
258 }
259  
260 45 return $rc;
261 }
262  
263 3 with 'MarpaX::Languages::M4::Role::Regexp';
264 }
265  
266 4 1;
267  
268 __END__
« M4/Impl/Default/BaseConversion.pm ↑ Summary M4/Impl/Parser/Actions.pm »