File Coverage

blib/lib/Perl/ToPerl6/Transformer/CompoundStatements/RewriteLoops.pm
Criterion Covered Total %
statement 22 43 51.1
branch 0 8 0.0
condition 0 3 0.0
subroutine 9 15 60.0
pod 3 5 60.0
total 34 74 45.9


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::CompoundStatements::RewriteLoops;
2              
3 1     1   804 use 5.006001;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         18  
5 1     1   4 use warnings;
  1         3  
  1         21  
6 1     1   4 use Readonly;
  1         2  
  1         42  
7              
8 1     1   4 use Perl::ToPerl6::Utils qw{ :severities };
  1         2  
  1         47  
9 1         41 use Perl::ToPerl6::Utils::PPI qw{
10             insert_trailing_whitespace
11 1     1   107 };
  1         1  
12              
13 1     1   4 use base 'Perl::ToPerl6::Transformer';
  1         2  
  1         421  
14              
15             #-----------------------------------------------------------------------------
16              
17             Readonly::Scalar my $DESC => q{Transform C-style 'for()' to 'loop ()'};
18             Readonly::Scalar my $EXPL => q{C-style for() is now 'loop'};
19              
20             #-----------------------------------------------------------------------------
21              
22 1     1 0 3 sub supported_parameters { return () }
23 1     1 1 5 sub default_necessity { return $NECESSITY_HIGHEST }
24 0     0 1   sub default_themes { return qw( core ) }
25              
26             #-----------------------------------------------------------------------------
27              
28             my %conditional = (
29             for => 'loop',
30             foreach => 'loop',
31             );
32              
33             sub _structure_has_semicolon {
34 0     0     my ($elem) = @_;
35 0           $elem = $elem->child(1);
36 0           while ( $elem ) {
37 0 0         return 1 if $elem->isa('PPI::Token::Structure');
38 0           $elem = $elem->next_sibling;
39             }
40 0           return;
41             }
42              
43             sub _is_c_style {
44 0     0     my ($elem) = @_;
45 0           $elem = $elem->child(1);
46 0           while ( $elem->next_sibling ) {
47 0 0         return 1 if $elem->isa('PPI::Statement::Null');
48 0 0 0       return 1 if $elem->isa('PPI::Statement') and
49             _structure_has_semicolon($elem);
50 0           $elem = $elem->next_sibling;
51             }
52 0           return;
53             }
54              
55             #
56             # There's a tradeoff at work here.
57             #
58             # While the PPI::Structure::For object is the correct type to look for
59             # (specifically, it accurately matches the 'for ( ) ...' construct)
60             # it matches the '(...)' element, not the structure containing the emtire 'for'
61             # block.
62             #
63             # So we search for the PPI::Structure::For object, and when the time comes
64             # to transform the object, we just move our "pointer" up to the parent,
65             # which contains the entire 'for () {}' construct.
66             #
67              
68             sub applies_to {
69             return sub {
70 0 0   0     $_[1]->isa('PPI::Structure::For') and
71             _is_c_style($_[1])
72             }
73 0     0 1   }
74              
75             #-----------------------------------------------------------------------------
76              
77             # Note to reader: (after moving $elem up one layer) the structure looks like
78             #
79             # $elem (PPI::Statement::Compound)
80             # \
81             # \ 0 1 # count by child()
82             # \0 1 # count by schild()
83             # +----+
84             # | |
85             # for ( )
86             #
87             # After changing child(0):
88             #
89             # $elem (PPI::Statement::Compound)
90             # \
91             # \ 0 1 # count by child()
92             # \0 1 # count by schild()
93             # +----+
94             # | |
95             # loop ( )
96             #
97             # After inserting ' ' after schild(0):
98             #
99             # $elem (PPI::Statement::Compound)
100             # \
101             # \ 0 1 2 # count by child()
102             # \0 1 # count by schild()
103             # +----+----+
104             # | | |
105             # loop ' ' ( )
106              
107             sub transform {
108 0     0 0   my ($self, $elem, $doc) = @_;
109 0           $elem = $elem->parent;
110              
111 0           $elem->schild(0)->set_content('loop');
112              
113 0           insert_trailing_whitespace($elem->schild(0));
114              
115 0           return $self->transformation( $DESC, $EXPL, $elem );
116             }
117              
118             1;
119              
120             #-----------------------------------------------------------------------------
121              
122             __END__
123              
124             =pod
125              
126             =head1 NAME
127              
128             Perl::ToPerl6::Transformer::CompoundStatements::RewriteLoops - Format for(;;) loops
129              
130              
131             =head1 AFFILIATION
132              
133             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
134             distribution.
135              
136              
137             =head1 DESCRIPTION
138              
139             Perl6 changes C-style C<for> loops to use the name C<loop>:
140              
141             for(@a) --> for (@a)
142             for($i=0;$i<1;$i++) --> loop ($i=0;$i<1;$i++)
143              
144             =head1 CONFIGURATION
145              
146             This Transformer is not configurable except for the standard options.
147              
148             =head1 AUTHOR
149              
150             Jeffrey Goff <drforr@pobox.com>
151              
152             =head1 COPYRIGHT
153              
154             Copyright (c) 2015 Jeffrey Goff
155              
156             This program is free software; you can redistribute it and/or modify
157             it under the same terms as Perl itself.
158              
159             =cut
160              
161             ##############################################################################
162             # Local Variables:
163             # mode: cperl
164             # cperl-indent-level: 4
165             # fill-column: 78
166             # indent-tabs-mode: nil
167             # c-indentation-style: bsd
168             # End:
169             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :