line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Babble::Plugin::SubstituteAndReturn; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
69583
|
use Moo; |
|
1
|
|
|
|
|
7287
|
|
|
1
|
|
|
|
|
5
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our ($REGMARK, $REGERROR); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $OP_TYPE_RE = qr{ |
19
|
|
|
|
|
|
|
\A |
20
|
|
|
|
|
|
|
(?: |
21
|
|
|
|
|
|
|
s (*MARK:s) |
22
|
|
|
|
|
|
|
| (?: y|tr ) (*MARK:y) |
23
|
|
|
|
|
|
|
) |
24
|
|
|
|
|
|
|
}x; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _get_flags { |
27
|
40
|
|
|
40
|
|
104
|
my ($text) = @_; |
28
|
|
|
|
|
|
|
|
29
|
40
|
50
|
|
|
|
379
|
if ( $text =~ $OP_TYPE_RE ) { |
30
|
40
|
|
|
|
|
424
|
return $OP_TYPE_DATA{$REGMARK}{flags}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
0
|
return ''; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _transform_binary { |
37
|
8
|
|
|
8
|
|
30
|
my ($self, $top) = @_; |
38
|
|
|
|
|
|
|
|
39
|
8
|
|
|
|
|
25
|
my $chained_re = qr{ |
40
|
|
|
|
|
|
|
\G |
41
|
|
|
|
|
|
|
( |
42
|
|
|
|
|
|
|
(?>(?&PerlOWS)) =~ (?>(?&PerlOWS)) |
43
|
|
|
|
|
|
|
((?> |
44
|
|
|
|
|
|
|
(?&PerlSubstitution) |
45
|
|
|
|
|
|
|
| (?&PerlTransliteration) |
46
|
|
|
|
|
|
|
)) |
47
|
|
|
|
|
|
|
) |
48
|
8
|
|
|
|
|
156
|
@{[ $top->grammar_regexp ]} |
49
|
|
|
|
|
|
|
}x; |
50
|
|
|
|
|
|
|
|
51
|
8
|
|
|
|
|
1293
|
my $replaced; |
52
|
8
|
|
|
|
|
32
|
do { |
53
|
18
|
|
|
|
|
42
|
$replaced = 0; |
54
|
|
|
|
|
|
|
$top->each_match_within(BinaryExpression => [ |
55
|
|
|
|
|
|
|
[ 'left' => '(?>(?&PerlPrefixPostfixTerm))' ], |
56
|
|
|
|
|
|
|
'(?>(?&PerlOWS)) =~ (?>(?&PerlOWS))', |
57
|
|
|
|
|
|
|
[ 'right' => '(?> |
58
|
|
|
|
|
|
|
(?&PerlSubstitution) |
59
|
|
|
|
|
|
|
| (?&PerlTransliteration) |
60
|
|
|
|
|
|
|
)' ], |
61
|
|
|
|
|
|
|
] => sub { |
62
|
29
|
|
|
29
|
|
83
|
my ($m) = @_; |
63
|
29
|
|
|
|
|
71
|
my ($left, $right); |
64
|
29
|
100
|
|
|
|
63
|
eval { |
65
|
29
|
|
|
|
|
121
|
($left, $right) = $m->subtexts(qw(left right)); |
66
|
22
|
|
|
|
|
106
|
1 |
67
|
|
|
|
|
|
|
} or return; |
68
|
22
|
|
|
|
|
86
|
my ($flags) = $right =~ _get_flags($right); |
69
|
22
|
100
|
|
|
|
150
|
return unless (my $newflags = $flags) =~ s/r//g; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# find chained substitutions |
72
|
|
|
|
|
|
|
# ... =~ s///r =~ s///r =~ s///r |
73
|
10
|
|
|
|
|
44
|
my $top_text = $top->text; |
74
|
10
|
|
|
|
|
60
|
pos( $top_text ) = $m->start + length $m->text; |
75
|
10
|
|
|
|
|
29
|
my $chained_subs_length = 0; |
76
|
10
|
|
|
|
|
21
|
my @chained_subs; |
77
|
10
|
|
|
|
|
2180
|
while( $top_text =~ /$chained_re/g ) { |
78
|
11
|
|
|
|
|
3578
|
$chained_subs_length += length $1; |
79
|
11
|
|
|
|
|
336
|
push @chained_subs, $2; |
80
|
|
|
|
|
|
|
} |
81
|
10
|
|
|
|
|
43
|
for my $subst_c (@chained_subs) { |
82
|
11
|
|
|
|
|
29
|
my ($f_c) = $subst_c =~ _get_flags($subst_c); |
83
|
11
|
50
|
|
|
|
63
|
die "Chained substitution must use the /r modifier" |
84
|
|
|
|
|
|
|
unless (my $nf_c = $f_c) =~ s/r//g; |
85
|
11
|
|
|
|
|
132
|
$subst_c =~ s/\Q${f_c}\E$/${nf_c}/; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
10
|
|
|
|
|
116
|
$right =~ s/\Q${flags}\E$/${newflags}/; |
89
|
10
|
|
|
|
|
43
|
$left =~ s/\s+$//; |
90
|
10
|
|
|
|
|
250
|
my $genlex = '$'.$m->gensym; |
91
|
|
|
|
|
|
|
|
92
|
10
|
100
|
|
|
|
40
|
if( @chained_subs ) { |
93
|
7
|
|
|
|
|
39
|
my $chained_for = 'for ('.$genlex.') { ' |
94
|
|
|
|
|
|
|
. join("; ", @chained_subs) |
95
|
|
|
|
|
|
|
. ' }'; |
96
|
7
|
|
|
|
|
69
|
$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
|
|
|
|
|
28
|
$m->replace_text( |
104
|
|
|
|
|
|
|
'(map { (my '.$genlex.' = $_) =~ '.$right.'; '.$genlex.' }' |
105
|
|
|
|
|
|
|
.' '.$left.')[0]' |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
10
|
|
|
|
|
45
|
$replaced++; |
110
|
18
|
|
|
|
|
331
|
}); |
111
|
|
|
|
|
|
|
} while( $replaced ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _transform_contextualise { |
115
|
8
|
|
|
8
|
|
21
|
my ($self, $top) = @_; |
116
|
|
|
|
|
|
|
|
117
|
8
|
|
|
|
|
19
|
do { |
118
|
8
|
|
|
|
|
19
|
my @subst_pos; # sorted positions |
119
|
|
|
|
|
|
|
# Look for substitution without binding operator: |
120
|
|
|
|
|
|
|
# First look for an expression that begins with Substitution. |
121
|
|
|
|
|
|
|
$top->each_match_of( Expression => sub { |
122
|
20
|
|
|
20
|
|
49
|
my ($m) = @_; |
123
|
20
|
|
|
|
|
55
|
my $expr_text = $m->text; |
124
|
20
|
|
|
|
|
39
|
my @start_pos = do { |
125
|
20
|
100
|
|
|
|
152
|
if( $expr_text =~ $OP_TYPE_RE ) { |
126
|
7
|
|
|
|
|
24
|
my $rule = $OP_TYPE_DATA{$REGMARK}{rule}; |
127
|
7
|
|
|
|
|
31
|
my @pos = $m->match_positions_of($rule); |
128
|
7
|
50
|
33
|
|
|
65
|
return unless @pos && $pos[0][0] == 0; |
129
|
7
|
|
|
|
|
19
|
@{ $pos[0] }; |
|
7
|
|
|
|
|
34
|
|
130
|
|
|
|
|
|
|
} else { |
131
|
13
|
|
|
|
|
37
|
return; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
}; |
134
|
7
|
|
|
|
|
29
|
my $text = substr($expr_text, $start_pos[0], $start_pos[1]); |
135
|
7
|
|
|
|
|
22
|
my ($flags) = $text =~ _get_flags($text); |
136
|
7
|
50
|
|
|
|
37
|
return unless $flags =~ /r/; |
137
|
7
|
|
|
|
|
52
|
push @subst_pos, $m->start; |
138
|
8
|
|
|
|
|
85
|
}); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Insert context variable and binding operator |
141
|
8
|
|
|
|
|
84
|
my $diff = 0; |
142
|
8
|
|
|
|
|
21
|
my $replace = '$_ =~ '; |
143
|
8
|
|
|
|
|
46
|
while( my $pos = shift @subst_pos ) { |
144
|
7
|
|
|
|
|
43
|
$top->replace_substring($pos + $diff, 0, $replace); |
145
|
7
|
|
|
|
|
30
|
$diff += length $replace; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
}; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub transform_to_plain { |
151
|
8
|
|
|
8
|
0
|
10952
|
my ($self, $top) = @_; |
152
|
|
|
|
|
|
|
|
153
|
8
|
|
|
|
|
40
|
$self->_transform_contextualise($top); |
154
|
|
|
|
|
|
|
|
155
|
8
|
|
|
|
|
33
|
$self->_transform_binary($top); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub check_bail_out_early { |
159
|
0
|
|
|
0
|
0
|
|
my ($self, $top) = @_; |
160
|
0
|
|
|
|
|
|
$top->text !~ m/ \b (?: s|y|tr ) \b /xs; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
__END__ |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 NAME |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Babble::Plugin::SubstituteAndReturn - Plugin for /r flag for substitution and transliteration |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 SYNOPSIS |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Converts usage of the C<s///r> and C<tr///r> syntax to substitution and |
173
|
|
|
|
|
|
|
transliteration without the C</r> flag. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 SEE ALSO |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
L<E<sol>r flag|Syntax::Construct/"/r"> |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |