| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
@pos and $pos[0][0] == 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||