Regexp/Parser.pm

filter: Statementstmt 96.0% 220 / 229 Branchbran 81.1% 73 / 90 Conditioncond 79.4% 27 / 34 MC/DCmcdc 68.4% 13 / 19 Subroutinesub 94.1% 48 / 51 Podpod 6.2% 2 / 32 totaltotal 84.1% 383 / 455 CC 53 SCAR 13.0
CC
53
Cov
91%
next
8.0
__ANON__
7.0
regex
7.0
CRAP 3.7
« Regexp/Parser/Handlers.pm ↑ Summary Regexp/Parser/Diagnostics.pm »
1 package Regexp::Parser;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 our $VERSION = '0.27';
7  
8 19 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use charnames ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 19 our %loaded;
14 19 our @EXPORT = qw( Rx RxPOS RxCUR RxLEN Rf SIZE_ONLY LATEST );
15  
16 21830 ▶sub Rx :lvalue { $_[0]{regex} }
Subroutine Rx: called CC=1 SCAR=0.0
Pod undocumented
17 3874 ▶sub RxPOS :lvalue { pos ${&Rx} }
Subroutine RxPOS: called CC=1 SCAR=0.0
Pod undocumented
18 104 ▶sub RxCUR { substr ${&Rx}, &RxPOS }
Subroutine RxCUR: called CC=1 SCAR=0.0
Pod undocumented
19 4879 ▶sub RxLEN { $_[0]{len} }
Subroutine RxLEN: called CC=1 SCAR=0.0
Pod undocumented
20  
21 6667 ▶sub Rf :lvalue { $_[0]{flags}[-1] }
Subroutine Rf: called CC=1 SCAR=0.0
Pod undocumented
22  
23 5669 ▶sub SIZE_ONLY { ! $_[0]{tree} }
Subroutine SIZE_ONLY: called CC=1 SCAR=0.0
Pod undocumented
24 0 ▶sub LATEST :lvalue { $_[0]{tree}[-1] }
Subroutine LATEST: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 19 ▶use Regexp::Parser::Diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 19 ▶use Regexp::Parser::Objects;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 19 ▶use Regexp::Parser::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30  
31 # this handles 'use base/parent "Regexp::Parser"'
32 # which wouldn't call 'import'
33 {
34 19 my ($level, $prev, $pkg);
35 19 ▶ while (my ($curr) = caller $level++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($curr) = caller $level++) 57 18 75
36 57 ▶ $pkg = $curr, last if $prev and ($prev eq "base" or $prev eq "parent") and $curr ne "base" and $curr ne "parent";
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev eq "base" || $prev eq "parent" and $curr ne "base" and $curr ne "parent" 1 56 57
Condition83%10/12
$prev and $prev eq "base" || $prev eq "parent" and $curr ne "base" and $curr ne "parent"
!ll&&!rl&&r
5601
$prev and $prev eq "base" || $prev eq "parent" and $curr ne "base"
!ll&&!rl&&r
5511
$prev and $prev eq "base" || $prev eq "parent"
!ll&&!rl&&r
19362
$prev eq "base" or $prev eq "parent"
l!l&&r!l&&!r
2036
MC/DC60%3/5
$prev and $prev eq "base" || $prev eq "parent" and $curr ne "base" and $curr ne "parent"
$prev $prev eq "base" $prev eq "parent" $curr ne "base" $curr ne "parent"
Truth table50%4/8
A and B or C and D and E
A= $prevB= $prev eq "base"C= $prev eq "parent"D= $curr ne "base"E= $curr ne "parent"
ABCDEresult
0XXXX0
100XX0
11X0X0
1010X0
11X100
101100
11X111
101111
37 56 $prev = $curr;
38 }
39 19 ▶ Regexp::Parser->export_to_level($level, $pkg, @EXPORT) if $pkg;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg 1 18 19
40 }
41  
42  
43 359 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
44 359 my ($class, $rx) = @_;
45 359 my $self = bless {}, $class;
46 359 $self->init;
47 359 ▶ $self->regex($rx) if defined $rx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rx 1 358 359
48 359 return $self;
49 }
50  
51  
52 557 ▶sub regex {
Subroutine regex: called CC=7 SCAR=19.5
Pod documented
53 557 my ($self, $rx, $flags) = @_;
54 557 my $init_flags = 0;
55 557 ▶ if (defined $flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags) 7 550 557
56 7 for my $ch (split //, $flags) {
57 9 my $method = "FLAG_$ch";
58 9 ▶ if ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) 9 0 9
59 9 my $v = $self->$method;
60 # /xx: if x is already on, set the xx bit (Perl 5.26+)
61 9 ▶ if ($ch eq 'x' && ($init_flags & $v)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "x" and $init_flags & $v) 1 8 9
Condition100%3/3
$ch eq "x" and $init_flags & $v
!ll&&!rl&&r
531
MC/DC100%2/2
$ch eq "x" and $init_flags & $v
$ch eq "x" $init_flags & $v
Truth table100%3/3
A and B
A= $ch eq "x"B= $init_flags & $v
ABresult
0X0
100
111
62 1 $init_flags |= 0x200;
63 }
64 9 $init_flags |= $v;
65 }
66 }
67 }
68 557 %$self = (
69 regex => \"$rx",
70 len => length $rx,
71 tree => undef,
72 stack => [],
73 maxpar => 0,
74 nparen => 0,
75 captures => [],
76 named_captures => {},
77 flags => [$init_flags],
78 next => ['atom'],
79 quotemeta => 0,
80 );
81  
82 # do the initial scan (populates maxpar)
83 # because tree is undef, nothing gets built
84 557 &RxPOS = 0;
85 557 eval { $self->parse };
86 557 ▶ $self->{errmsg} = $@, return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 59 498 557
87  
88 # reset things, define tree as []
89 498 &RxPOS = 0;
90 498 $self->{tree} = [];
91 498 $self->{flags} = [$init_flags];
92 498 $self->{next} = ['atom'];
93 498 $self->{quotemeta} = 0;
94  
95 498 return 1;
96 }
97  
98  
99 992 ▶sub parse {
Subroutine parse: called CC=5 SCAR=17.8
Pod undocumented
100 992 my ($self, $rx) = @_;
101 992 ▶ $self->regex($rx) or return if defined $rx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rx 0 992 992
Condition25%1/4
defined $rx && ($self->regex($rx) || (return))
!ll
9920
$self->regex($rx) or return
l!l
00
MC/DC0%0/2
defined $rx && ($self->regex($rx) || (return))
defined $rx $self->regex($rx)
Truth table33%1/3
A && B
A= defined $rxB= $self->regex($rx)
ABresult
0X0
100
111
102 992 ▶ croak "no regex defined" unless &RxLEN;
Branch: 1 branch
BranchTrueFalseTotal
unless &RxLEN 0 992 992
103 992 ▶ 1 while $self->next;
Branch: 1 branch
BranchTrueFalseTotal
$self->next and do { '???' } 1952 920 2872
104 920 return 1;
105 }
106  
107  
108 55 ▶sub root {
Subroutine root: called CC=2 SCAR=6.9
Pod undocumented
109 55 my ($self) = @_;
110 55 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 44 11 55
111 55 return $self->{tree};
112 }
113  
114  
115 2 ▶sub nparen {
Subroutine nparen: called CC=2 SCAR=7.1
Pod undocumented
116 2 my ($self) = @_;
117 2 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 0 2 2
118 2 return $self->{nparen};
119 }
120  
121  
122 1 ▶sub captures {
Subroutine captures: called CC=3 SCAR=11.4
Pod undocumented
123 1 my ($self, $n) = @_;
124 1 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 1 0 1
125 1 ▶ return $self->{captures}[--$n] if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 1 1
126 1 return $self->{captures};
127 }
128  
129  
130 1 ▶sub named_captures {
Subroutine named_captures: called CC=3 SCAR=11.4
Pod undocumented
131 1 my ($self, $name) = @_;
132 1 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 0 1 1
133 1 ▶ return $self->{named_captures}{$name} if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 1 1
134 1 return $self->{named_captures};
135 }
136  
137  
138 7 ▶sub nchar {
Subroutine nchar: called CC=2 SCAR=7.2
Pod undocumented
139 7 my $self = shift;
140 7 ▶ return map chr(/^\^(\S)/ ? (64 ^ ord $1) : charnames::vianame($_)), @_;
Branch: 1 branch
BranchTrueFalseTotal
/^\^(\S)/ ? : 0 7 7
141 }
142  
143  
144 2 ▶sub error_is {
Subroutine error_is: called CC=2 SCAR=7.1
Pod undocumented
145 2 my ($self, $enum) = @_;
146 2 ▶ return $self->{errnum} && $self->{errnum} == $enum;
Condition66%2/3
$self->{'errnum'} && $self->{'errnum'} == $enum
!ll&&!rl&&r
011
MC/DC50%1/2
$self->{'errnum'} && $self->{'errnum'} == $enum
$self->{'errnum'} $self->{'errnum'} == $enum
Truth table66%2/3
A && B
A= $self->{'errnum'}B= $self->{'errnum'} == $enum
ABresult
0X0
100
111
147 }
148  
149  
150 2 ▶sub errmsg {
Subroutine errmsg: called CC=1 SCAR=0.0
Pod undocumented
151 2 my ($self) = @_;
152 2 return $self->{errmsg};
153 }
154  
155  
156 109 ▶sub errnum {
Subroutine errnum: called CC=1 SCAR=0.0
Pod undocumented
157 109 my ($self) = @_;
158 109 return $self->{errnum};
159 }
160  
161  
162 72 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
163 72 my ($self, $enum, $err, @args) = @_;
164 72 $err .= "; marked by <-- HERE in m/%s <-- HERE %s/";
165 72 push @args, substr(${&Rx}, 0, &RxPOS), &RxCUR;
166 72 $self->{errnum} = $enum;
167 72 $self->{errmsg} = sprintf $err, @args;
168 72 croak $self->{errmsg};
169 }
170  
171  
172 23 ▶sub warn {
Subroutine warn: called CC=2 SCAR=7.0
Pod undocumented
173 23 my ($self, $enum, $err, @args) = @_;
174 23 $err .= "; marked by <-- HERE in m/%s <-- HERE %s/";
175 23 push @args, substr(${&Rx}, 0, &RxPOS), &RxCUR;
176 23 ▶ carp sprintf $err, @args if &SIZE_ONLY;
Branch: 1 branch
BranchTrueFalseTotal
if &SIZE_ONLY 23 0 23
177 }
178  
179  
180 2 ▶sub awarn {
Subroutine awarn: called CC=1 SCAR=0.0
Pod undocumented
181 2 my ($self, $enum, $err, @args) = @_;
182 2 local $self->{tree};
183 2 $self->warn($enum, $err, @args);
184 }
185  
186  
187 2944 ▶sub next {
Subroutine next: called CC=8 SCAR=20.8
Pod undocumented
188 2944 my ($self) = @_;
189 2944 ▶ croak "no regex defined" unless &RxLEN;
Branch: 1 branch
BranchTrueFalseTotal
unless &RxLEN 0 2944 2944
190  
191 2944 ▶ while (my $try = pop @{ $self->{next} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $try = pop @{$$self{"next"};}) 6532 921 7453
192 6532 ▶ if (defined(my $r = $self->$try)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $r = $self->$try)) 1959 4509 6468
193 1959 $r->insert($self->{tree});
194 1952 return $r;
195 }
196 }
197  
198 921 ▶ $self->error(RPe_RPAREN) if ${&Rx} =~ m{ \G \) }xgc;
Branch: 1 branch
BranchTrueFalseTotal
if ${&Rx;} =~ / \G \) /cgx 1 920 921
199 920 ▶ $self->error(0, "PANIC! %d %s", &RxPOS, &RxCUR) if &RxPOS != &RxLEN;
Branch: 1 branch
BranchTrueFalseTotal
if &RxPOS != &RxLEN 0 920 920
200  
201 920 ▶ if (! &SIZE_ONLY) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&SIZE_ONLY) 422 498 920
202 422 ▶ $self->{tree} = pop @{ $self->{stack} } while @{ $self->{stack} };
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"stack"};} and do { $self->{"tree"} = pop @{$$self{"stack"};} } 9 422 431
203 422 delete $self->{stack};
204 }
205  
206 920 return;
207 }
208  
209  
210 23 ▶sub walker {
Subroutine walker: called CC=4 SCAR=13.9
Pod undocumented
211 23 my ($self, $depth) = @_;
212 23 ▶ croak "no regex defined" unless &RxLEN;
Branch: 1 branch
BranchTrueFalseTotal
unless &RxLEN 0 23 23
213 23 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 20 3 23
214  
215 23 ▶ $depth = -1 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 19 4 23
216 23 my $d = $depth;
217 23 my @stack = @{ $self->{tree} };
218 23 my $next;
219  
220 188 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
221 188 ▶ return $depth if @_ and $_[0] eq -depth;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $_[0] eq "-depth" 4 184 188
Condition66%2/3
@_ and $_[0] eq "-depth"
!ll&&!rl&&r
18404
MC/DC50%1/2
@_ and $_[0] eq "-depth"
@_ $_[0] eq "-depth"
Truth table66%2/3
A and B
A= @_B= $_[0] eq "-depth"
ABresult
0X0
100
111
222 184 ▶ carp "unexpected argument ($_[0]) to iterator" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 184 184
223  
224 {
225 184 $next = shift @stack;
226 342 ▶ $d += $next->(), redo if ref($next) eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $next eq "CODE" 158 184 342
227 }
228  
229 184 ▶ @stack = @{ $self->{tree} }, return unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 19 165 184
230 165 $next->walk(\@stack, $d);
231 165 ▶ return wantarray ? ($next, $depth-$d) : $next;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 164 1 165
232 }
233 23 }
234  
235  
236 354 ▶sub visual {
Subroutine visual: called CC=2 SCAR=7.0
Pod undocumented
237 354 my ($self) = @_;
238 354 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 354 0 354
239 341 my $vis = join "", map($_->visual, @{ $self->{tree} });
240 341 return $vis;
241 }
242  
243  
244 31 ▶sub qr {
Subroutine qr: called CC=4 SCAR=13.9
Pod undocumented
245 31 my ($self) = @_;
246 31 ▶ $self->parse if $self->{stack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stack"} 5 26 31
247 31 my $rx = $self->{tree};
248 19 ▶ no warnings 'regexp';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 19 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
250  
251 31 ▶ if (@$rx == 1 and $rx->[0]->family eq 'group') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rx == 1 and $rx->[0]->family eq "group") 6 25 31
Condition100%3/3
@$rx == 1 and $rx->[0]->family eq "group"
!ll&&!rl&&r
2326
MC/DC100%2/2
@$rx == 1 and $rx->[0]->family eq "group"
@$rx == 1 $rx->[0]->family eq "group"
Truth table100%3/3
A and B
A= @$rx == 1B= $rx->[0]->family eq "group"
ABresult
0X0
100
111
252 6 my $vis = join "", map $_->qr, @{ $rx->[0]->{data} };
253 6 my $flags = $rx->[0]->on;
254 6 $flags =~ s/^\^//; # strip caret prefix for qr// modifier
255 6 return eval('qr/$vis/' . $flags);
256 }
257  
258 25 $rx = join "", map($_->qr, @$rx);
259 25 return qr/$rx/;
260 }
261  
262  
263 5650 ▶sub nextchar {
Subroutine nextchar: called CC=5 SCAR=16.1
Pod undocumented
264 5650 my ($self) = @_;
265  
266 {
267 5650 ▶ if (${&Rx} =~ m{ \G \(\?\# [^)]* }xgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (${&Rx;} =~ / \G \(\?\# [^)]* /cgx) 7 5660 5667
268 7 ▶ ${&Rx} =~ m{ \G \) }xgc and redo;
Branch: 1 branch
BranchTrueFalseTotal
${&Rx;} =~ / \G \) /cgx and redo 5 2 7
269 2 $self->error(RPe_NOTERM);
270 }
271 5660 ▶ &Rf & $self->FLAG_x and ${&Rx} =~ m{ \G (?: \s+ | \# .* )+ }xgc and redo;
Branch: 1 branch
BranchTrueFalseTotal
&Rf & $self->FLAG_x and ${&Rx;} =~ / \G (?: \s+ | \# .* )+ /cgx and redo 12 5648 5660
Condition100%3/3
&Rf & $self->FLAG_x and ${&Rx;} =~ / \G (?: \s+ | \# .* )+ /cgx
!ll&&!rl&&r
55628612
MC/DC100%2/2
&Rf & $self->FLAG_x and ${&Rx;} =~ / \G (?: \s+ | \# .* )+ /cgx
&Rf & $self->FLAG_x ${&Rx;} =~ / \G (?: \s+ | \# .* )+ /cgx
Truth table100%3/3
A and B
A= &Rf & $self->FLAG_xB= ${&Rx;} =~ / \G (?: \s+ | \# .* )+ /cgx
ABresult
0X0
100
111
272 }
273 }
274  
275  
276 4161 ▶sub object {
Subroutine object: called CC=2 SCAR=6.9
Pod undocumented
277 4161 ▶ return if &SIZE_ONLY;
Branch: 1 branch
BranchTrueFalseTotal
if &SIZE_ONLY 2183 1978 4161
278 1978 my $self = shift;
279 1978 $self->force_object(@_);
280 }
281  
282  
283 2235 ▶sub force_object {
Subroutine force_object: called CC=6 SCAR=17.9
Pod undocumented
284 2235 ▶ Carp::croak("class name passed where object required") unless ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 2235 2235
285 2235 my $type = splice @_, 1, 1;
286 2235 my $ref = ref $_[0];
287 2235 my $class = "${ref}::$type";
288  
289 2235 ▶ if ($ref ne __PACKAGE__ and !$loaded{$class}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne __PACKAGE__ and not $Regexp::Parser::loaded{$class}++) 11 2224 2235
Condition100%3/3
$ref ne __PACKAGE__ and not $Regexp::Parser::loaded{$class}++
!ll&&!rl&&r
21982611
MC/DC100%2/2
$ref ne __PACKAGE__ and not $Regexp::Parser::loaded{$class}++
$ref ne __PACKAGE__ not $Regexp::Parser::loaded{$class}++
Truth table100%3/3
A and not(B)
A= $ref ne __PACKAGE__B= not $Regexp::Parser::loaded{$class}++
ABresult
0X0
100
111
290 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 11 my $orig_base = $Regexp::Parser::{$type . '::'};
292 11 my $user_base = ${"${ref}::"}{'__object__::'};
293  
294 11 ▶ push @{ "${class}::ISA" }, $ref . "::__object__" if $user_base;
Branch: 1 branch
BranchTrueFalseTotal
if $user_base 11 0 11
295 11 ▶ push @{ "${class}::ISA" }, __PACKAGE__ . "::$type" if $orig_base;
Branch: 1 branch
BranchTrueFalseTotal
if $orig_base 10 1 11
296 }
297  
298 2235 return $class->new(@_);
299 }
300  
301  
302 4308 ▶sub add_flag {
Subroutine add_flag: called CC=1 SCAR=0.0
Pod undocumented
303 4308 my ($self, $seq, $func) = @_;
304 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
305 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
306 4308 *{ ref($self) . "::FLAG_$seq" } = $func;
307 }
308  
309  
310 0 ▶sub del_flag {
Subroutine del_flag: not called CC=2 SCAR=10.1
Pod undocumented
311 0 my ($self, @flags) = @_;
312 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
313 0 my $stash = \%{ ref($self) . "::" };
314 0 undef $stash->{"FLAG_$_"} for @flags;
315 }
316  
317  
318 34468 ▶sub add_handler {
Subroutine add_handler: called CC=1 SCAR=0.0
Pod undocumented
319 34468 my ($self, $seq, $func) = @_;
320 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
321 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
322 34468 *{ ref($self) . "::$seq" } = $func;
323 }
324  
325  
326 0 ▶sub del_handler {
Subroutine del_handler: not called CC=2 SCAR=10.1
Pod undocumented
327 0 my ($self, @handles) = @_;
328 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
329 0 my $stash = \%{ ref($self) . "::" };
330 0 undef $stash->{$_} for @handles;
331 }
332  
333  
334 19 1;
335  
336 __END__
« Regexp/Parser/Handlers.pm ↑ Summary Regexp/Parser/Diagnostics.pm »