lib/Perl/ToPerl6/Transformer/Regexes/SwapModifiers.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 60.0% 3 / 5 totaltotal 91.4% 64 / 70 CC 4 SCAR 6.0
CC
4
Cov
92%
transform
4.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
« lib/Perl/ToPerl6/Transformer/Variables/RewriteMatchVariables.pm ↑ Summary lib/Perl/ToPerl6/Transformer/Packages/RewriteDeclarations.pm »
1 package Perl::ToPerl6::Transformer::Regexes::SwapModifiers;
2  
3 17 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Perl::ToPerl6::Utils qw{ :characters :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use base 'Perl::ToPerl6::Transformer';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #-----------------------------------------------------------------------------
13  
14 17 Readonly::Scalar my $DESC =>
15 q{Regex modifiers now appear at the start of expresions};
16 17 Readonly::Scalar my $EXPL =>
17 q{Regex modifiers now appear at the start of expresions};
18  
19 #-----------------------------------------------------------------------------
20  
21 38 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
22 28 ▶sub default_necessity { return $NECESSITY_HIGHEST }
Subroutine default_necessity: called CC=1 SCAR=0.0
Pod documented
23 24 ▶sub default_themes { return qw( core ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
24 5 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
25 78 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 78 $_[1]->isa('PPI::Token::Regexp')
27 }
28 5 }
29  
30 #-----------------------------------------------------------------------------
31  
32 17 my %modifier_map = (
33 m => {
34 m => 'm',
35 s => 's', # Changes meaning actually.
36 i => 'i',
37 x => 'x',
38 p => 'p',
39 o => '', # These 5 are not allowed. Others may mean different
40 d => '', # things.
41 u => '',
42 a => '',
43 l => '',
44 g => 'c', # /g --> /c
45 r => 'r',
46 c => '', # /c --> ...
47 e => 'e',
48 },
49 s => {
50 m => 'm',
51 s => 's',
52 i => 'i',
53 x => 'x',
54 p => 'p',
55 o => '', # These 5 are not allowed. Others may mean different
56 d => '', # things.
57 u => '',
58 a => '',
59 l => '',
60 g => 'c',
61 r => 'r',
62 c => '',
63 e => 'e',
64 },
65 tr => {
66 m => 'm',
67 s => 's',
68 i => 'i',
69 x => 'x',
70 p => 'p',
71 o => '', # These 5 are not allowed. Others may mean different
72 d => '', # things.
73 u => '',
74 a => '',
75 l => '',
76 g => 'c',
77 r => 'r',
78 c => '',
79 e => 'e',
80 }
81 );
82  
83 1 ▶sub transform {
Subroutine transform: called CC=4 SCAR=14.0
Pod undocumented
84 1 my ($self, $elem, $doc) = @_;
85 1 my $new_content = $elem->content;
86  
87 1 $new_content =~ s{^/}{m/};
88 1 $new_content =~ s{^y}{tr};
89 1 $new_content =~ m{^(m|s|tr)};
90 1 $elem->{operator} = $1;
91  
92 1 my $num_modifiers = keys %{ $elem->get_modifiers };
93 1 my $modifiers =
94 substr( $elem->content, -$num_modifiers, $num_modifiers, '' );
95  
96 1 my $operator = $elem->{operator};
97 1 my $new_modifiers = '';
98 $new_modifiers = join( '',
99 0 ▶ map { $modifier_map{$operator}{$_} }
Branch: 1 branch
BranchTrueFalseTotal
if $modifiers 0 1 1
100 split //, $modifiers
101 ) if $modifiers;
102  
103 1 my $delta = length($modifiers) - length($new_modifiers);
104 1 for ( @{ $elem->{sections} } ) {
105 1 $_->{position} -= $delta;
106 }
107  
108 1 ▶ if ( $new_modifiers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_modifiers) { } 0 1 1
109 0 $new_content =~ s{^(m|s|tr)}{$elem->{operator}:$new_modifiers:P5};
110 }
111 else {
112 1 $new_content =~ s{^(m|s|tr)}{$elem->{operator}:P5};
113 }
114 1 $new_content =~ s{$modifiers$}{};
115  
116 1 $elem->set_content($new_content);
117  
118 1 return $self->transformation( $DESC, $EXPL, $elem );
119 }
120  
121 17 1;
122  
123 #-----------------------------------------------------------------------------
124  
125 __END__
« lib/Perl/ToPerl6/Transformer/Variables/RewriteMatchVariables.pm ↑ Summary lib/Perl/ToPerl6/Transformer/Packages/RewriteDeclarations.pm »