File Coverage

blib/lib/Perl/ToPerl6/Transformer/Regexes/SwapModifiers.pm
Criterion Covered Total %
statement 19 45 42.2
branch 0 4 0.0
condition n/a
subroutine 8 12 66.6
pod 3 5 60.0
total 30 66 45.4


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Regexes::SwapModifiers;
2              
3 1     1   752 use 5.006001;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         17  
5 1     1   5 use warnings;
  1         4  
  1         21  
6 1     1   4 use Readonly;
  1         2  
  1         43  
7              
8 1     1   4 use Perl::ToPerl6::Utils qw{ :characters :severities };
  1         2  
  1         47  
9              
10 1     1   305 use base 'Perl::ToPerl6::Transformer';
  1         3  
  1         632  
11              
12             #-----------------------------------------------------------------------------
13              
14             Readonly::Scalar my $DESC =>
15             q{Regex modifiers now appear at the start of expresions};
16             Readonly::Scalar my $EXPL =>
17             q{Regex modifiers now appear at the start of expresions};
18              
19             #-----------------------------------------------------------------------------
20              
21 1     1 0 4 sub supported_parameters { return () }
22 1     1 1 6 sub default_necessity { return $NECESSITY_HIGHEST }
23 0     0 1   sub default_themes { return qw( core ) }
24             sub applies_to {
25             return sub {
26 0     0     $_[1]->isa('PPI::Token::Regexp')
27             }
28 0     0 1   }
29              
30             #-----------------------------------------------------------------------------
31              
32             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             sub transform {
84 0     0 0   my ($self, $elem, $doc) = @_;
85 0           my $new_content = $elem->content;
86              
87 0           $new_content =~ s{^/}{m/};
88 0           $new_content =~ s{^y}{tr};
89 0           $new_content =~ m{^(m|s|tr)};
90 0           $elem->{operator} = $1;
91              
92 0           my $num_modifiers = keys %{ $elem->get_modifiers };
  0            
93 0           my $modifiers =
94             substr( $elem->content, -$num_modifiers, $num_modifiers, '' );
95              
96 0           my $operator = $elem->{operator};
97 0           my $new_modifiers = '';
98             $new_modifiers = join( '',
99 0 0         map { $modifier_map{$operator}{$_} }
  0            
100             split //, $modifiers
101             ) if $modifiers;
102              
103 0           my $delta = length($modifiers) - length($new_modifiers);
104 0           for ( @{ $elem->{sections} } ) {
  0            
105 0           $_->{position} -= $delta;
106             }
107              
108 0 0         if ( $new_modifiers ) {
109 0           $new_content =~ s{^(m|s|tr)}{$elem->{operator}:$new_modifiers:P5};
110             }
111             else {
112 0           $new_content =~ s{^(m|s|tr)}{$elem->{operator}:P5};
113             }
114 0           $new_content =~ s{$modifiers$}{};
115              
116 0           $elem->set_content($new_content);
117              
118 0           return $self->transformation( $DESC, $EXPL, $elem );
119             }
120              
121             1;
122              
123             #-----------------------------------------------------------------------------
124              
125             __END__
126              
127             =pod
128              
129             =head1 NAME
130              
131             Perl::ToPerl6::Transformer::Regexex::SwapModifiers
132              
133              
134             =head1 AFFILIATION
135              
136             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
137             distribution.
138              
139              
140             =head1 DESCRIPTION
141              
142             In Perl6, modifiers have moved to the start of the regular expression declaration, and some are no longer needed:
143              
144             m/foo/ --> m/foo/
145             m/foo/x --> m/foo/
146             m/foo/gi --> m:gi/foo/
147              
148             Transforms regular expressions outside of comments, heredocs, strings and POD.
149              
150             =head1 CONFIGURATION
151              
152             This Transformer is not configurable except for the standard options.
153              
154             =head1 AUTHOR
155              
156             Jeffrey Goff <drforr@pobox.com>
157              
158             =head1 COPYRIGHT
159              
160             Copyright (c) 2015 Jeffrey Goff
161              
162             This program is free software; you can redistribute it and/or modify
163             it under the same terms as Perl itself.
164              
165             =cut
166              
167             ##############################################################################
168             # Local Variables:
169             # mode: cperl
170             # cperl-indent-level: 4
171             # fill-column: 78
172             # indent-tabs-mode: nil
173             # c-indentation-style: bsd
174             # End:
175             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :