blib/lib/Babble/Match.pm

filter: Statementstmt 99.2% 135 / 136 Branchbran 84.0% 42 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 8 totaltotal 92.1% 199 / 216 CC 31 SCAR 13.8
CC
31
Cov
95%
__ANON__
7.0
_build_submatches
7.0
each_match_of
6.0
CRAP 4.0
« blib/lib/App/sigfix.pm ↑ Summary blib/lib/Babble/Plugin/PostfixDeref.pm »
1 package Babble::Match;
2  
3 8 ▶use Babble::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Babble::SymbolGenerator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Mu;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ro 'top_rule';
9 8 rwp 'text';
10  
11 42 ▶lazy 'grammar' => sub { Babble::Grammar->new }
Subroutine _build_grammar: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12 8 => handles => [ 'grammar_regexp' ];
13  
14 lazy 'symbol_generator' => sub {
15 2 ▶ $_[0]->can('parent')
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->can('parent') ? : 1 1 2
$_[0]->can('parent') ? : 5 3 8
16 ? $_[0]->parent->symbol_generator
17 : Babble::SymbolGenerator->new
18 8 } => handles => [ 'gensym' ];
19  
20 lazy top_re => sub {
21 40 ▶ my ($self) = @_;
Subroutine _build_top_re: called CC=1 SCAR=0.0
22 40 my $top = $self->_rule_to_re($self->top_rule);
23 40 return "\\A${top}\\Z";
24 8 };
25  
26 lazy submatches => sub {
27 137 ▶ my ($self) = @_;
Subroutine _build_submatches: called CC=7 SCAR=19.5
28 137 ▶ return {} unless ref(my $top = $self->top_rule);
Branch: 2 branches
BranchTrueFalseTotal
unless ref(my $top = $self->top_rule) 43 94 137
unless ref(my $top = $self->top_rule) 2 1 3
29 94 my @subrules;
30 my $re = join '', map {
31 94 ref($_)
32 354 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 173 181 354
ref $_ ? : 0 1 1
33 173 push @subrules, $_;
34 173 my ($name, $rule) = @$_;
35 173 "(${rule})"
36 }
37 : $_
38 } @$top;
39 94 ▶ return {} unless @subrules;
Branch: 2 branches
BranchTrueFalseTotal
unless @subrules 54 40 94
unless @subrules 0 1 1
40 40 my @values = $self->text =~ /\A${re}\Z ${\$self->grammar_regexp}/x;
41 40 ▶ die "Match failed" unless @values;
Branch: 2 branches
BranchTrueFalseTotal
unless @values 0 40 40
unless @values 0 2 2
42 40 my %submatches;
43 40 require Babble::SubMatch;
44 40 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 173 ▶ next unless defined $values[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values[$idx] 54 119 173
49 119 my ($name, $rule) = @{$subrules[$idx]};
50 119 $submatches{$name} = Babble::SubMatch->new(
51 top_rule => [ $rule ],
52 start => $-[$idx+1],
53 text => $values[$idx],
54 parent => $self,
55 );
56 }
57 40 return \%submatches;
58 8 };
59  
60 sub subtexts {
61 18 ▶ my ($self, @names) = @_;
Subroutine subtexts: called CC=3 SCAR=11.0
Pod undocumented
62 18 ▶ unless (@names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@names) 6 12 18
63 6 my %s = %{$self->submatches};
64 6 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 124 ▶ my $re = $_[1];
Subroutine _rule_to_re: called CC=3 SCAR=11.0
71 124 ▶ return "(?&Perl${re})" unless ref($re);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $re 43 81 124
72 81 ▶ return join '', map +(ref($_) ? $_->[1] : $_), @$re;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 242 202 444
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 102 ▶ my ($self, $of) = @_;
Subroutine match_positions_of: called CC=1 SCAR=0.0
Pod undocumented
82 102 our @F;
83 my $wrapped = $self->grammar->clone->extend_rule(
84 102 ▶ $of => sub { '('.$_[0].')'.'(?{ push @Babble::Match::F, [ pos() - length($^N), length($^N) ] })' }
Subroutine __ANON__: called CC=1 SCAR=0.0
85 102 )->grammar_regexp;
86 102 my @found = do {
87 102 local @F;
88 102 local $_ = $self->text;
89 102 /${\$self->top_re} ${wrapped}/x;
90 102 @F;
91 };
92 102 return @found;
93 }
94  
95 sub each_match_of {
96 102 ▶ my ($self, $of, $call) = @_;
Subroutine each_match_of: called CC=6 SCAR=18.0
Pod undocumented
97 102 my @found = $self->match_positions_of($of);
98 102 ▶ return unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 52 50 102
99 50 require Babble::SubMatch;
100 50 ▶ while (my $f = shift @found) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = shift @found) 64 50 114
101 64 my $match = substr($self->text, $f->[0], $f->[1]);
102 64 my $obj = Babble::SubMatch->new(
103 top_rule => $of,
104 start => $f->[0],
105 text => $match,
106 parent => $self,
107 );
108 64 $call->($obj);
109 64 ▶ if (my $len_diff = length($obj->text) - $f->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len_diff = length($obj->text) - $f->[1]) 46 18 64
110 46 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 50 return $self;
120 }
121  
122 sub each_match_within {
123 81 ▶ my ($self, $within, $rule, $call) = @_;
Subroutine each_match_within: called CC=1 SCAR=0.0
Pod undocumented
124 81 my $match_re = $self->_rule_to_re($rule);
125 81 my $extend_grammar = $self->grammar->clone;
126 81 $extend_grammar->add_rule(
127 BabbleInnerMatch => $match_re,
128 )->augment_rule($within => '(?&PerlBabbleInnerMatch)');
129 81 local $self->{grammar} = $extend_grammar;
130 $self->each_match_of(BabbleInnerMatch => sub {
131 44 ▶ $_[0]->{top_rule} = $rule; # intentionally hacky, should go away (or rwp) later
Subroutine __ANON__: called CC=1 SCAR=0.0
132 44 $call->($_[0]);
133 81 });
134 81 return $self;
135 }
136  
137 sub replace_substring {
138 233 ▶ my ($self, $start, $length, $replace) = @_;
Subroutine replace_substring: called CC=4 SCAR=13.9
Pod undocumented
139 233 my $text = $self->text;
140 233 substr($text, $start, $length, $replace);
141 233 $self->_set_text($text);
142 233 foreach my $submatch (values %{$self->submatches}) {
143 235 ▶ next unless defined $submatch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $submatch 2 233 235
144 233 ▶ if ($submatch->start > $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($submatch->start > $start) 101 132 233
145 101 $submatch->{start} += length($replace) - $length;
146 }
147 }
148 233 return $self;
149 }
150  
151 sub remove_use_argument {
152 26 ▶ 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 26 );
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 8 1;
« blib/lib/App/sigfix.pm ↑ Summary blib/lib/Babble/Plugin/PostfixDeref.pm »