Babble/Match.pm

filter: Statementstmt 100% 167 / 167 Branchbran 90.3% 47 / 52 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 8 totaltotal 91.7% 246 / 268 CC 36 SCAR 14.1
CC
36
Cov
96%
__ANON__
8.0
_build_submatches
8.0
each_match_of
6.0
CRAP 4.1
« Babble/Plugin/SubstituteAndReturn.pm ↑ Summary Babble/PluginChain.pm »
1 package Babble::Match;
2  
3 12 ▶use Babble::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Babble::SymbolGenerator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Mu;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use List::Util 1.45;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ro 'top_rule';
9 12 rwp 'text';
10  
11 168 ▶lazy 'grammar' => sub {
Subroutine _build_grammar: called CC=2 SCAR=6.9
Subroutine __ANON__: called CC=2 SCAR=6.9
12 168 ▶ $_[0]->can('parent')
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->can('parent') ? : 103 65 168
$_[0]->can('parent') ? : 29 71 100
13 ? $_[0]->parent->grammar
14 : Babble::Grammar->new
15 12 } => handles => [ 'grammar_regexp' ];
16  
17 24 ▶lazy 'symbol_generator' => sub {
Subroutine _build_symbol_generator: called CC=2 SCAR=6.9
18 24 ▶ $_[0]->can('parent')
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->can('parent') ? : 14 10 24
$_[0]->can('parent') ? : 56 68 124
19 ? $_[0]->parent->symbol_generator
20 : Babble::SymbolGenerator->new
21 12 } => handles => [ 'gensym' ];
22  
23 63 ▶lazy top_re => sub {
Subroutine _build_top_re: called CC=1 SCAR=0.0
24 63 my ($self) = @_;
25 63 my $top = $self->_rule_to_re($self->top_rule);
26 63 return "\\A${top}\\Z";
27 12 };
28  
29 12 my %SUBMATCHES_COMPILE_CACHE;
30 275 ▶lazy submatches => sub {
Subroutine _build_submatches: called CC=8 SCAR=20.8
31 275 my ($self) = @_;
32 275 ▶ return {} unless ref(my $top = $self->top_rule);
Branch: 2 branches
BranchTrueFalseTotal
unless ref(my $top = $self->top_rule) 77 198 275
unless ref(my $top = $self->top_rule) 49 13 62
33 198 my @subrules;
34 my $re = join '', map {
35 198 ref($_)
36 553 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 293 260 553
ref $_ ? : 0 13 13
37 293 push @subrules, $_;
38 293 my ($name, $rule) = @$_;
39 293 "(${rule})"
40 }
41 : $_
42 } @$top;
43 198 ▶ return {} unless @subrules;
Branch: 2 branches
BranchTrueFalseTotal
unless @subrules 106 92 198
unless @subrules 0 56 56
44 92 my $submatch_re = qq[ \\A${re}\\Z ${\$self->grammar_regexp} ];
45 92 my $_re;
46 my @values = $self->text =~ (
47 Babble::Config::CACHE_RE ? $SUBMATCHES_COMPILE_CACHE{$submatch_re} : $_re ||=
48 92 ▶ do {
Condition66%4/6
$SUBMATCHES_COMPILE_CACHE{$submatch_re} ||= do { '???'; my $re = qr/$submatch_re/x; $re }
l!l&&r!l&&!r
77150
$SUBMATCHES_COMPILE_CACHE{$submatch_re} ||= do { '???'; my $re = qr/$submatch_re/x; $re }
l!l&&r!l&&!r
1030
MC/DC0%0/2
$SUBMATCHES_COMPILE_CACHE{$submatch_re} ||= do { '???'; my $re = qr/$submatch_re/x; $re }
$SUBMATCHES_COMPILE_CACHE{$submatch_re} do { '???'; my $re = qr/$submatch_re/x; $re }
MC/DC0%0/2
$SUBMATCHES_COMPILE_CACHE{$submatch_re} ||= do { '???'; my $re = qr/$submatch_re/x; $re }
$SUBMATCHES_COMPILE_CACHE{$submatch_re} do { '???'; my $re = qr/$submatch_re/x; $re }
Truth table66%2/3
A ||= B
A= $SUBMATCHES_COMPILE_CACHE{$submatch_re}B= do { '???'; my $re = qr/$submatch_re/x; $re }
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $SUBMATCHES_COMPILE_CACHE{$submatch_re}B= do { '???'; my $re = qr/$submatch_re/x; $re }
ABresult
1X1
011
000
49 15 warn "Cache miss submatches\n" if Babble::Config::CACHE_RE && Babble::Config::DEBUG_CACHE_MISS;
50 12 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 15 my $re = qr/$submatch_re/x;
52 12 ▶ no re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 15 $re;
54 });
55 92 ▶ die "Match failed" unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 7 85 92
56 85 my %submatches;
57 85 require Babble::SubMatch;
58 85 foreach my $idx (0 .. $#subrules) {
59 # there may be more than one capture with the same name if there's an
60 # alternation in the rule, or one may be optional, so we skip if that
61 # part of the pattern failed to capture
62 279 ▶ next unless defined $values[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values[$idx] 60 219 279
63 219 my ($name, $rule) = @{$subrules[$idx]};
64 219 $submatches{$name} = Babble::SubMatch->new(
65 top_rule => [ $rule ],
66 start => $-[$idx+1],
67 text => $values[$idx],
68 parent => $self,
69 );
70 }
71 85 return \%submatches;
72 12 };
73  
74 67 ▶sub subtexts {
Subroutine subtexts: called CC=3 SCAR=11.0
Pod undocumented
75 67 my ($self, @names) = @_;
76 67 ▶ unless (@names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 6 61 67
77 6 my %s = %{$self->submatches};
78 6 return +{ map +( $_ => $s{$_}->text ), keys %s };
79 }
80 61 ▶ map +($_ ? $_->text : undef), @{$self->submatches}{@names};
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 108 2 110
81 }
82  
83 383 ▶sub _rule_to_re {
Subroutine _rule_to_re: called CC=3 SCAR=11.0
84 383 my $re = $_[1];
85 383 ▶ return "(?&Perl${re})" unless ref($re);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $re 104 279 383
86 279 ▶ return join '', map +(ref($_) ? $_->[1] : $_), @$re;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 594 460 1054
87 }
88  
89 2 ▶sub is_valid {
Subroutine is_valid: called CC=1 SCAR=0.0
Pod undocumented
90 2 my ($self) = @_;
91 2 return !!$self->text =~ /${\$self->top_re} ${\$self->grammar_regexp}/x;
92 }
93  
94 12 my %MATCH_POS_COMPILE_CACHE;
95 317 ▶sub match_positions_of {
Subroutine match_positions_of: called CC=2 SCAR=6.9
Pod undocumented
96 317 my ($self, $of) = @_;
97 317 our @F;
98 my $wrapped = $self->grammar->clone->extend_rule(
99 317 ▶ $of => sub { '('.$_[0].')'.'(?{ push @Babble::Match::F, [ pos() - length($^N), length($^N) ] })' }
Subroutine __ANON__: called CC=1 SCAR=0.0
100 317 )->grammar_regexp;
101 317 my @found = do {
102 317 local @F;
103 317 local $_ = $self->text;
104 317 my $mp_re = qq/${\$self->top_re} ${wrapped}/;
105 317 my $_re;
106 $_ =~ ( Babble::Config::CACHE_RE ? $MATCH_POS_COMPILE_CACHE{$mp_re} : $_re ||=
107 317 ▶ do {
Condition66%2/3
$MATCH_POS_COMPILE_CACHE{$mp_re} ||= do { '???'; my $re = qr/$mp_re/x; $re }
l!l&&r!l&&!r
285320
MC/DC0%0/2
$MATCH_POS_COMPILE_CACHE{$mp_re} ||= do { '???'; my $re = qr/$mp_re/x; $re }
$MATCH_POS_COMPILE_CACHE{$mp_re} do { '???'; my $re = qr/$mp_re/x; $re }
Truth table66%2/3
A ||= B
A= $MATCH_POS_COMPILE_CACHE{$mp_re}B= do { '???'; my $re = qr/$mp_re/x; $re }
ABresult
1X1
011
000
108 32 warn "Cache miss match_positions_of(): @{[ $self->top_re ]}\n" if Babble::Config::CACHE_RE && Babble::Config::DEBUG_CACHE_MISS;
109 12 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 32 my $re = qr/$mp_re/x;
111 12 ▶ no re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 32 $re;
113 }
114 );
115 317 @F;
116 };
117 176 return map { [ split ',', $_ ] }
118 List::Util::uniqstr
119 317 map { join ",", @$_ } @found;
120 }
121  
122 310 ▶sub each_match_of {
Subroutine each_match_of: called CC=6 SCAR=17.9
Pod undocumented
123 310 my ($self, $of, $call) = @_;
124 310 my @found = $self->match_positions_of($of);
125 310 ▶ return unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 192 118 310
126 118 require Babble::SubMatch;
127 118 ▶ while (my $f = shift @found) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = shift @found) 160 118 278
128 160 my $match = substr($self->text, $f->[0], $f->[1]);
129 160 my $obj = Babble::SubMatch->new(
130 top_rule => $of,
131 start => $f->[0],
132 text => $match,
133 parent => $self,
134 );
135 160 $call->($obj);
136 160 ▶ if (my $len_diff = length($obj->text) - $f->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len_diff = length($obj->text) - $f->[1]) 87 73 160
137 87 foreach my $later (@found) {
138 9 ▶ if ($later->[0] <= $f->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($later->[0] <= $f->[0]) { } 1 8 9
139 1 $later->[1] += $len_diff;
140 } else {
141 8 $later->[0] += $len_diff;
142 }
143 }
144 }
145 }
146 118 return $self;
147 }
148  
149 279 ▶sub each_match_within {
Subroutine each_match_within: called CC=1 SCAR=0.0
Pod undocumented
150 279 my ($self, $within, $rule, $call) = @_;
151 279 my $match_re = $self->_rule_to_re($rule);
152 279 my $extend_grammar = $self->grammar->clone;
153 279 $extend_grammar->add_rule(
154 BabbleInnerMatch => $match_re,
155 )->augment_rule($within => '(?&PerlBabbleInnerMatch)');
156 279 local $self->{grammar} = $extend_grammar;
157 116 ▶ $self->each_match_of(BabbleInnerMatch => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 116 $_[0]->{top_rule} = $rule; # intentionally hacky, should go away (or rwp) later
159 116 $call->($_[0]);
160 279 });
161 279 return $self;
162 }
163  
164 568 ▶sub replace_substring {
Subroutine replace_substring: called CC=4 SCAR=13.9
Pod undocumented
165 568 my ($self, $start, $length, $replace) = @_;
166 568 my $text = $self->text;
167 568 substr($text, $start, $length, $replace);
168 568 $self->_set_text($text);
169 568 foreach my $submatch (values %{$self->submatches}) {
170 625 ▶ next unless defined $submatch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $submatch 2 623 625
171 623 ▶ if ($submatch->start > $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($submatch->start > $start) 230 393 623
172 230 $submatch->{start} += length($replace) - $length;
173 }
174 }
175 568 return $self;
176 }
177  
178 132 ▶sub remove_use_argument {
Subroutine remove_use_argument: called CC=1 SCAR=0.0
Pod undocumented
179 132 my ($self, $use, $argument, $keep_empty) = @_;
180 $self->each_match_within(
181 UseStatement =>
182 [ "use\\s+${use}\\s+", [ explist => '.*?' ], ';' ],
183 3 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
184 3 my ($m) = @_;
185 3 my $explist = $m->submatches->{explist};
186 3 ▶ return unless my @explist_names = eval $explist->text;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@explist_names) = eval $explist->text 0 3 3
187 3 my @remain = grep $_ ne $argument, @explist_names;
188 3 ▶ return unless @remain < @explist_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @remain < @explist_names 0 3 3
189 3 ▶ unless (@remain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@remain) 1 2 3
190 1 ▶ ($keep_empty ? $explist : $m)->replace_text('');
Branch: 1 branch
BranchTrueFalseTotal
$keep_empty ? : 1 0 1
191 1 return;
192 }
193 2 $explist->replace_text('qw('.join(' ', @remain).')');
194 }
195 132 );
196 }
197  
198 3 ▶sub remove_use_statement {
Subroutine remove_use_statement: called CC=1 SCAR=0.0
Pod undocumented
199 3 my ($self, $use) = @_;
200 $self->each_match_within(
201 UseStatement =>
202 [ "use\\s+${use}.*?;" ],
203 1 ▶ sub { shift->replace_text('') },
Subroutine __ANON__: called CC=1 SCAR=0.0
204 3 );
205 }
206  
207 12 1;
« Babble/Plugin/SubstituteAndReturn.pm ↑ Summary Babble/PluginChain.pm »