1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
package PPI::Normal;
=pod
=head1 NAME
PPI::Normal - Normalize Perl Documents
=head2 DESCRIPTION
Perl Documents, as created by PPI, are typically filled with all sorts of
mess such as whitespace and comments and other things that don't effect
the actual meaning of the code.
In addition, because there is more than one way to do most things, and the
syntax of Perl itself is quite flexible, there are many ways in which the
"same" code can look quite different.
PPI::Normal attempts to resolve this by providing a variety of mechanisms
and algorithms to "normalize" Perl Documents, and determine a sort of base
form for them (although this base form will be a memory structure, and
not something that can be turned back into Perl source code).
The process itself is quite complex, and so for convenience and
extensibility it has been separated into a number of layers. At a later
point, it will be possible to write Plugin classes to insert additional
normalization steps into the various different layers.
In addition, you can choose to do the normalization only as deep as a
particular layer, depending on aggressively you want the normalization
process to be.
=head2 Layer 1 - Insignificant Data Removal
The basic step common to all normalization, layer 1 scans through the
Document and removes all whitespace, comments, POD, and anything else
that returns false for its C<significant> method.
It also checks each Element and removes known-useless sub-element
metadata such as the Elements physical position in the file.
=head1 Layer 2 - NOT IMPLEMENTED
This initial forward-port of the original Perl::Compare functionality to
the 0.900+ API of PPI only implements Layer 1 at this time.
=head1 METHODS
=cut
use strict;
use UNIVERSAL 'isa';
use List::MoreUtils ();
use PPI::Document::Normalized ();
use vars qw{$VERSION $errstr %LAYER};
BEGIN {
$VERSION = '0.903';
$errstr = '';
# Registered function store
%LAYER = (
1 => [],
);
}
#####################################################################
# Configuration
=pod
=head2 register $function => $layer, ...
The C<register> method is used by normalization method providers to
tell the normalization engines which functions need to be run, and
in which layer they apply.
Provide a set of key/value pairs, where the key is the full name of the
function (in string form), and the value is the layer (see description
of the layers above) in which it should be run.
Returns true if all functions are registered, or C<undef> on error.
=cut
sub register {
my $class = shift;
while ( @_ ) {
# Check the function
my $function = shift;
{ no strict 'refs';
defined $function and defined &{"$function"} or return undef;
}
# Has it already been added?
if ( List::MoreUtils::any { $_ eq $function } ) {
return 1;
}
# Check the layer to add it to
my $layer = shift;
defined $layer and $layer == 1 or return undef;
# Add to the layer data store
push @{ $LAYER{$layer} }, $function;
}
1;
}
# With the registration mechanism in place, load in the main set of
# normalization methods to initialize the store.
use PPI::Normal::Standard;
#####################################################################
# Constructor and Accessors
=pod
=head2 new
Creates a new normalization level 1 object, to which Document objects
can be passed to be normalized.
Of course, what you probably REALLY want is just to call
L<PPI::Document>'s C<normalize> method.
Returns a new PPI::Normal object, or C<undef> on error.
B<Note>
Unless it can be shown there's absolutely a could reason to make this
instantiable, this may degrade into a non-instantiable class at a
later date (although the PPI::Document C<normalize> will stay as is).
=cut
sub new {
my $class = shift->_clear;
# Create the object
my $object = bless {
layer => 1,
}, $class;
$object;
}
sub layer { $_[0]->{layer} }
#####################################################################
# Main Methods
sub process {
my $self = ref $_[0] ? shift->_clear : shift->new;
my $Document = isa(ref $_[0], 'PPI::Document') ? shift : return undef;
# Fail if object is already in use
return undef if $self->{Document};
# Set up for processing run
$self->{Document} = $Document;
# Work out what functions we need to call
my @functions = ();
foreach ( 1 .. $self->layer ) {
push @functions, @{ $LAYER{$_} };
}
# Execute each function
foreach my $function ( @functions ) {
no strict 'refs';
&{"$function"}( $Document );
}
# Create the normalized Document object
my $Normalized = PPI::Document::Normalized->new(
Document => $Document,
version => $VERSION,
functions => \@functions,
) or return undef;
$Normalized;
}
#####################################################################
# Error Handling
sub errstr { $errstr }
sub _error { $errstr = $_[1]; undef }
sub _clear { $errstr = ''; $_[0] }
1;
=pod
=head1 TO DO
- Write the other 5 layers :)
=head1 SUPPORT
See the L<support section|PPI/SUPPORT> in the main module
=head1 AUTHOR
Adam Kennedy (Maintainer), L<http://ali.as/>, cpan@ali.as
=head1 COPYRIGHT
Copyright (c) 2005 Adam Kennedy. All rights reserved.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|