Babble/Plugin/SubstituteAndReturn.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 81.8% 18 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 2 totaltotal 87.1% 95 / 109 CC 14 SCAR 14.7
CC
14
Cov
91%
__ANON__
7.0
__ANON__
5.1
_get_flags
2.1
CRAP 4.3
« Babble/Config.pm ↑ Summary Babble/Match.pm »
1 package Babble::Plugin::SubstituteAndReturn;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our ($REGMARK, $REGERROR);
6  
7 1 my %OP_TYPE_DATA = (
8 s => {
9 rule => 'QuotelikeS',
10 flags => qr/([msixpodualgcern]*+)$/,
11 },
12 y => {
13 rule => 'QuotelikeTR',
14 flags => qr/([cdsr]*+)$/,
15 },
16 );
17  
18 1 my $OP_TYPE_RE = qr{
19 \A
20 (?:
21 s (*MARK:s)
22 | (?: y|tr ) (*MARK:y)
23 )
24 }x;
25  
26 40 ▶sub _get_flags {
Subroutine _get_flags: called CC=2 SCAR=7.6
27 40 my ($text) = @_;
28  
29 40 ▶ if ( $text =~ $OP_TYPE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$OP_TYPE_RE/x) 40 0 40
30 40 return $OP_TYPE_DATA{$REGMARK}{flags};
31 }
32  
33 0 return '';
34 }
35  
36 8 ▶sub _transform_binary {
Subroutine _transform_binary: called CC=2 SCAR=6.9
37 8 my ($self, $top) = @_;
38  
39 8 my $chained_re = qr{
40 \G
41 (
42 (?>(?&PerlOWS)) =~ (?>(?&PerlOWS))
43 ((?>
44 (?&PerlSubstitution)
45 | (?&PerlTransliteration)
46 ))
47 )
48 8 @{[ $top->grammar_regexp ]}
49 }x;
50  
51 8 my $replaced;
52 8 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$replaced and do { do { $replaced = 0; $top->each_match_within("BinaryExpression", [["left", "(?>(?&PerlPrefixPostfixTerm))"], "(?>(?&PerlOWS)) =~ (?>(?&PerlOWS))", ["right", "(?>\n (?&PerlSubstitution)\n | (?&PerlTransliteration)\n )"]], sub { my($m) = @_; my($left, $right); eval { do { ($left, $right) = $m->subtexts("left", "right"); 1 } } or return; my($flags) = $right =~ _get_flags($right); return unless (my $newflags = $flags) =~ s/r//g; my $top_text = $top->text; pos $top_text = $m->start + length($m->text); my $chained_subs_length = 0; my @chained_subs; while ($top_text =~ /$chained_re/g) { $chained_subs_length += length $1; push @chained_subs, $2; } ; foreach my $subst_c (@chained_subs) { my($f_c) = $subst_c =~ _get_flags($subst_c); die "Chained substitution must use the /r modifier" unless (my $nf_c = $f_c) =~ s/r//g; $subst_c =~ s/\Q$f_c\E$/$nf_c/; } ; $right =~ s/\Q$flags\E$/$newflags/; $left =~ s/\s+$//; my $genlex = '$' . $m->gensym; if (@chained_subs) { my $chained_for = 'for (' . $genlex . ') { ' . join("; ", @chained_subs) . ' }'; $top->replace_substring($m->start, length($m->text) + $chained_subs_length, '(map { (my ' . $genlex . ' = $_) =~ ' . $right . '; ' . $chained_for . ' ' . $genlex . ' }' . " " . $left . ')[0]'); } else { $m->replace_text('(map { (my ' . $genlex . ' = $_) =~ ' . $right . '; ' . $genlex . ' }' . " " . $left . ')[0]'); } ; $replaced++; } ) } } 10 8 18
53 18 $replaced = 0;
54 $top->each_match_within(BinaryExpression => [
55 [ 'left' => '(?>(?&PerlPrefixPostfixTerm))' ],
56 '(?>(?&PerlOWS)) =~ (?>(?&PerlOWS))',
57 [ 'right' => '(?>
58 (?&PerlSubstitution)
59 | (?&PerlTransliteration)
60 )' ],
61 29 ▶ ] => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
62 29 my ($m) = @_;
63 29 my ($left, $right);
64 29 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { () = $m->subtexts("left", "right"); 1 } } or return 22 7 29
65 29 ($left, $right) = $m->subtexts(qw(left right));
66 22 1
67 } or return;
68 22 my ($flags) = $right =~ _get_flags($right);
69 22 ▶ return unless (my $newflags = $flags) =~ s/r//g;
Branch: 1 branch
BranchTrueFalseTotal
unless (my $newflags = $flags) =~ s/r//g 12 10 22
70  
71 # find chained substitutions
72 # ... =~ s///r =~ s///r =~ s///r
73 10 my $top_text = $top->text;
74 10 pos( $top_text ) = $m->start + length $m->text;
75 10 my $chained_subs_length = 0;
76 10 my @chained_subs;
77 10 ▶ while( $top_text =~ /$chained_re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top_text =~ /$chained_re/g) 11 10 21
78 11 $chained_subs_length += length $1;
79 11 push @chained_subs, $2;
80 }
81 10 for my $subst_c (@chained_subs) {
82 11 my ($f_c) = $subst_c =~ _get_flags($subst_c);
83 11 ▶ die "Chained substitution must use the /r modifier"
Branch: 1 branch
BranchTrueFalseTotal
unless (my $nf_c = $f_c) =~ s/r//g 0 11 11
84 unless (my $nf_c = $f_c) =~ s/r//g;
85 11 $subst_c =~ s/\Q${f_c}\E$/${nf_c}/;
86 }
87  
88 10 $right =~ s/\Q${flags}\E$/${newflags}/;
89 10 $left =~ s/\s+$//;
90 10 my $genlex = '$'.$m->gensym;
91  
92 10 ▶ if( @chained_subs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chained_subs) { } 7 3 10
93 7 my $chained_for = 'for ('.$genlex.') { '
94 . join("; ", @chained_subs)
95 . ' }';
96 7 $top->replace_substring(
97 $m->start,
98 length($m->text) + $chained_subs_length,
99 '(map { (my '.$genlex.' = $_) =~ '.$right.'; '.$chained_for.' '.$genlex.' }'
100 .' '.$left.')[0]'
101 );
102 } else {
103 3 $m->replace_text(
104 '(map { (my '.$genlex.' = $_) =~ '.$right.'; '.$genlex.' }'
105 .' '.$left.')[0]'
106 );
107 }
108  
109 10 $replaced++;
110 18 });
111 } while( $replaced );
112 }
113  
114 8 ▶sub _transform_contextualise {
Subroutine _transform_contextualise: called CC=2 SCAR=7.0
115 8 my ($self, $top) = @_;
116  
117 8 do {
118 8 my @subst_pos; # sorted positions
119 # Look for substitution without binding operator:
120 # First look for an expression that begins with Substitution.
121 20 ▶ $top->each_match_of( Expression => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
122 20 my ($m) = @_;
123 20 my $expr_text = $m->text;
124 20 my @start_pos = do {
125 20 ▶ if( $expr_text =~ $OP_TYPE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr_text =~ /$OP_TYPE_RE/x) { } 7 13 20
126 7 my $rule = $OP_TYPE_DATA{$REGMARK}{rule};
127 7 my @pos = $m->match_positions_of($rule);
128 7 ▶ return unless @pos && $pos[0][0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @pos and $pos[0][0] == 0 0 7 7
Condition33%1/3
@pos and $pos[0][0] == 0
!ll&&!rl&&r
007
MC/DC0%0/2
@pos and $pos[0][0] == 0
@pos $pos[0][0] == 0
Truth table33%1/3
A and B
A= @posB= $pos[0][0] == 0
ABresult
0X0
100
111
129 7 @{ $pos[0] };
130 } else {
131 13 return;
132 }
133 };
134 7 my $text = substr($expr_text, $start_pos[0], $start_pos[1]);
135 7 my ($flags) = $text =~ _get_flags($text);
136 7 ▶ return unless $flags =~ /r/;
Branch: 1 branch
BranchTrueFalseTotal
unless $flags =~ /r/ 0 7 7
137 7 push @subst_pos, $m->start;
138 8 });
139  
140 # Insert context variable and binding operator
141 8 my $diff = 0;
142 8 my $replace = '$_ =~ ';
143 8 ▶ while( my $pos = shift @subst_pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pos = shift @subst_pos) 7 8 15
144 7 $top->replace_substring($pos + $diff, 0, $replace);
145 7 $diff += length $replace;
146 }
147 };
148 }
149  
150 8 ▶sub transform_to_plain {
Subroutine transform_to_plain: called CC=1 SCAR=0.0
Pod undocumented
151 8 my ($self, $top) = @_;
152  
153 8 $self->_transform_contextualise($top);
154  
155 8 $self->_transform_binary($top);
156 }
157  
158 0 ▶sub check_bail_out_early {
Subroutine check_bail_out_early: not called CC=1 SCAR=6.9
Pod undocumented
159 0 my ($self, $top) = @_;
160 0 $top->text !~ m/ \b (?: s|y|tr ) \b /xs;
161 }
162  
163 1 1;
164 __END__
« Babble/Config.pm ↑ Summary Babble/Match.pm »