blib/lib/Babble/Match.pm

filter: Statementstmt 97.0% 132 / 136 Branchbran 84.0% 42 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 8 totaltotal 90.3% 197 / 218 CC 31 SCAR 13.8
CC
31
Cov
94%
__ANON__
7.0
_build_submatches
7.0
each_match_of
6.0
CRAP 4.0
« blib/lib/Babble/Plugin/State.pm ↑ Summary blib/lib/Babble/Plugin/PostfixDeref.pm »
1 package Babble::Match;
2  
3 7 ▶use Babble::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Babble::SymbolGenerator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Mu;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ro 'top_rule';
9 7 rwp 'text';
10  
11 33 ▶lazy 'grammar' => sub { Babble::Grammar->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_grammar: called CC=1 SCAR=0.0
12 7 => handles => [ 'grammar_regexp' ];
13  
14 lazy 'symbol_generator' => sub {
15 2 ▶ $_[0]->can('parent')
Subroutine _build_symbol_generator: called CC=2 SCAR=6.9
Subroutine __ANON__: not called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->can('parent') ? : 1 1 2
$_[0]->can('parent') ? : 3 6 9
16 ? $_[0]->parent->symbol_generator
17 : Babble::SymbolGenerator->new
18 7 } => handles => [ 'gensym' ];
19  
20 lazy top_re => sub {
21 18 ▶ my ($self) = @_;
Subroutine _build_top_re: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
22 18 my $top = $self->_rule_to_re($self->top_rule);
23 18 return "\\A${top}\\Z";
24 7 };
25  
26 lazy submatches => sub {
27 106 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.5
28 106 ▶ return {} unless ref(my $top = $self->top_rule);
Branch: 2 branches
BranchTrueFalseTotal
unless ref(my $top = $self->top_rule) 27 79 106
unless ref(my $top = $self->top_rule) 4 16 20
29 79 my @subrules;
30 my $re = join '', map {
31 79 ref($_)
32 279 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 145 134 279
ref $_ ? : 4 2 6
33 145 push @subrules, $_;
34 145 my ($name, $rule) = @$_;
35 145 "(${rule})"
36 }
37 : $_
38 } @$top;
39 79 ▶ return {} unless @subrules;
Branch: 2 branches
BranchTrueFalseTotal
unless @subrules 48 31 79
unless @subrules 0 2 2
40 31 my @values = $self->text =~ /\A${re}\Z ${\$self->grammar_regexp}/x;
41 31 ▶ die "Match failed" unless @values;
Branch: 2 branches
BranchTrueFalseTotal
unless @values 0 31 31
unless @values 0 4 4
42 31 my %submatches;
43 31 require Babble::SubMatch;
44 31 foreach my $idx (0 .. $#subrules) {
45 # there may be more than one capture with the same name if there's an
46 # alternation in the rule, or one may be optional, so we skip if that
47 # part of the pattern failed to capture
48 145 ▶ next unless defined $values[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values[$idx] 50 95 145
49 95 my ($name, $rule) = @{$subrules[$idx]};
50 95 $submatches{$name} = Babble::SubMatch->new(
51 top_rule => [ $rule ],
52 start => $-[$idx+1],
53 text => $values[$idx],
54 parent => $self,
55 );
56 }
57 31 return \%submatches;
58 7 };
59  
60 sub subtexts {
61 12 ▶ my ($self, @names) = @_;
Subroutine subtexts: called CC=3 SCAR=12.3
Pod undocumented
62 12 ▶ unless (@names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 0 12 12
63 0 my %s = %{$self->submatches};
64 0 return +{ map +( $_ => $s{$_}->text ), keys %s };
65 }
66 12 ▶ map +($_ ? $_->text : undef), @{$self->submatches}{@names};
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 24 2 26
67 }
68  
69 sub _rule_to_re {
70 94 ▶ my $re = $_[1];
Subroutine _rule_to_re: called CC=3 SCAR=11.0
71 94 ▶ return "(?&Perl${re})" unless ref($re);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $re 29 65 94
72 65 ▶ return join '', map +(ref($_) ? $_->[1] : $_), @$re;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 208 154 362
73 }
74  
75 sub is_valid {
76 2 ▶ my ($self) = @_;
Subroutine is_valid: called CC=1 SCAR=0.0
Pod undocumented
77 2 return !!$self->text =~ /${\$self->top_re} ${\$self->grammar_regexp}/x;
78 }
79  
80 sub match_positions_of {
81 74 ▶ my ($self, $of) = @_;
Subroutine match_positions_of: called CC=1 SCAR=0.0
Pod undocumented
82 74 our @F;
83 my $wrapped = $self->grammar->clone->extend_rule(
84 74 ▶ $of => sub { '('.$_[0].')'.'(?{ push @Babble::Match::F, [ pos() - length($^N), length($^N) ] })' }
Subroutine __ANON__: called CC=1 SCAR=0.0
85 74 )->grammar_regexp;
86 74 my @found = do {
87 74 local @F;
88 74 local $_ = $self->text;
89 74 /${\$self->top_re} ${wrapped}/x;
90 74 @F;
91 };
92 74 return @found;
93 }
94  
95 sub each_match_of {
96 74 ▶ my ($self, $of, $call) = @_;
Subroutine each_match_of: called CC=6 SCAR=18.0
Pod undocumented
97 74 my @found = $self->match_positions_of($of);
98 74 ▶ return unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 38 36 74
99 36 require Babble::SubMatch;
100 36 ▶ while (my $f = shift @found) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = shift @found) 50 36 86
101 50 my $match = substr($self->text, $f->[0], $f->[1]);
102 50 my $obj = Babble::SubMatch->new(
103 top_rule => $of,
104 start => $f->[0],
105 text => $match,
106 parent => $self,
107 );
108 50 $call->($obj);
109 50 ▶ if (my $len_diff = length($obj->text) - $f->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len_diff = length($obj->text) - $f->[1]) 32 18 50
110 32 foreach my $later (@found) {
111 7 ▶ if ($later->[0] <= $f->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($later->[0] <= $f->[0]) { } 0 7 7
112 0 $later->[1] += $len_diff;
113 } else {
114 7 $later->[0] += $len_diff;
115 }
116 }
117 }
118 }
119 36 return $self;
120 }
121  
122 sub each_match_within {
123 65 ▶ my ($self, $within, $rule, $call) = @_;
Subroutine each_match_within: called CC=1 SCAR=0.0
Pod undocumented
124 65 my $match_re = $self->_rule_to_re($rule);
125 65 my $extend_grammar = $self->grammar->clone;
126 65 $extend_grammar->add_rule(
127 BabbleInnerMatch => $match_re,
128 )->augment_rule($within => '(?&PerlBabbleInnerMatch)');
129 65 local $self->{grammar} = $extend_grammar;
130 $self->each_match_of(BabbleInnerMatch => sub {
131 36 ▶ $_[0]->{top_rule} = $rule; # intentionally hacky, should go away (or rwp) later
Subroutine __ANON__: called CC=1 SCAR=0.0
132 36 $call->($_[0]);
133 65 });
134 65 return $self;
135 }
136  
137 sub replace_substring {
138 197 ▶ my ($self, $start, $length, $replace) = @_;
Subroutine replace_substring: called CC=4 SCAR=13.9
Pod undocumented
139 197 my $text = $self->text;
140 197 substr($text, $start, $length, $replace);
141 197 $self->_set_text($text);
142 197 foreach my $submatch (values %{$self->submatches}) {
143 213 ▶ next unless defined $submatch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $submatch 2 211 213
144 211 ▶ if ($submatch->start > $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($submatch->start > $start) 81 130 211
145 81 $submatch->{start} += length($replace) - $length;
146 }
147 }
148 197 return $self;
149 }
150  
151 sub remove_use_argument {
152 20 ▶ my ($self, $use, $argument, $keep_empty) = @_;
Subroutine remove_use_argument: called CC=1 SCAR=0.0
Pod undocumented
153 $self->each_match_within(
154 UseStatement =>
155 [ "use\\s+${use}\\s+", [ explist => '.*?' ], ';' ],
156 sub {
157 3 ▶ my ($m) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.4
158 3 my $explist = $m->submatches->{explist};
159 3 ▶ return unless my @explist_names = eval $explist->text;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@explist_names) = eval $explist->text 0 3 3
160 3 my @remain = grep $_ ne $argument, @explist_names;
161 3 ▶ return unless @remain < @explist_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @remain < @explist_names 0 3 3
162 3 ▶ unless (@remain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@remain) 1 2 3
163 1 ▶ ($keep_empty ? $explist : $m)->replace_text('');
Branch: 1 branch
BranchTrueFalseTotal
$keep_empty ? : 1 0 1
164 1 return;
165 }
166 2 $explist->replace_text('qw('.join(' ', @remain).')');
167 }
168 20 );
169 }
170  
171 sub remove_use_statement {
172 3 ▶ my ($self, $use) = @_;
Subroutine remove_use_statement: called CC=1 SCAR=0.0
Pod undocumented
173 $self->each_match_within(
174 UseStatement =>
175 [ "use\\s+${use}.*?;" ],
176 1 ▶ sub { shift->replace_text('') },
Subroutine __ANON__: called CC=1 SCAR=0.0
177 3 );
178 }
179  
180 7 1;
« blib/lib/Babble/Plugin/State.pm ↑ Summary blib/lib/Babble/Plugin/PostfixDeref.pm »