| 1 | ############################################################################## | ||
| 2 | # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic-Deprecated/lib/Perl/Critic/Utils/PPIRegexp.pm $ | ||
| 3 | # $Date: 2013-10-29 09:11:44 -0700 (Tue, 29 Oct 2013) $ | ||
| 4 | # $Author: thaljef $ | ||
| 5 | # $Revision: 4214 $ | ||
| 6 | ############################################################################## | ||
| 7 | |||
| 8 | package Perl::Critic::Utils::PPIRegexp; | ||
| 9 | |||
| 10 | use 5.006001; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use English qw(-no_match_vars); | ||
| 15 | use Readonly; | ||
| 16 | use Carp qw(croak); | ||
| 17 | |||
| 18 | use PPI::Node; | ||
| 19 | |||
| 20 | use base 'Exporter'; | ||
| 21 | |||
| 22 | our $VERSION = '1.119'; | ||
| 23 | |||
| 24 | #----------------------------------------------------------------------------- | ||
| 25 | |||
| 26 | our @EXPORT_OK = qw( | ||
| 27 | parse_regexp | ||
| 28 | get_match_string | ||
| 29 | get_substitute_string | ||
| 30 | get_modifiers | ||
| 31 | get_delimiters | ||
| 32 | ppiify | ||
| 33 | regexp_interpolates | ||
| 34 | ); | ||
| 35 | |||
| 36 | our %EXPORT_TAGS = ( | ||
| 37 | all => \@EXPORT_OK, | ||
| 38 | ); | ||
| 39 | |||
| 40 | #----------------------------------------------------------------------------- | ||
| 41 | |||
| 42 | sub parse_regexp { | ||
| 43 | my ($elem) = @_; | ||
| 44 | |||
| 45 | eval { require Regexp::Parser; } or return; | ||
| 46 | |||
| 47 | my $re = get_match_string($elem); | ||
| 48 | return if !defined $re; | ||
| 49 | |||
| 50 | # Are there any external regexp modifiers? If so, embed the ones | ||
| 51 | # that matter before parsing. | ||
| 52 | my %modifiers = get_modifiers($elem); | ||
| 53 | my $mods = join q{}, map {$modifiers{$_} ? $_ : q{}} qw(i m x s); | ||
| 54 | if ($mods) { | ||
| 55 | $re = "(?$mods:$re)"; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $parser = Regexp::Parser->new; | ||
| 59 | # If we can't parse the regexp, don't return a parse tree | ||
| 60 | { | ||
| 61 | local $SIG{__WARN__} = sub {}; # blissful silence... | ||
| 62 | return if ! $parser->regex($re); | ||
| 63 | } | ||
| 64 | |||
| 65 | return $parser; | ||
| 66 | } | ||
| 67 | |||
| 68 | #----------------------------------------------------------------------------- | ||
| 69 | |||
| 70 | sub get_match_string { | ||
| 71 | my ($elem) = @_; | ||
| 72 | $elem->can( 'get_match_string' ) or return; | ||
| 73 | return $elem->get_match_string(); | ||
| 74 | } | ||
| 75 | |||
| 76 | #----------------------------------------------------------------------------- | ||
| 77 | |||
| 78 | sub get_substitute_string { | ||
| 79 | my ($elem) = @_; | ||
| 80 | $elem->can( 'get_substitute_string' ) or return; | ||
| 81 | return $elem->get_substitute_string(); | ||
| 82 | } | ||
| 83 | |||
| 84 | #----------------------------------------------------------------------------- | ||
| 85 | |||
| 86 | sub get_modifiers { | ||
| 87 | my ($elem) = @_; | ||
| 88 | $elem->can( 'get_modifiers' ) or return; | ||
| 89 | return $elem->get_modifiers(); | ||
| 90 | } | ||
| 91 | |||
| 92 | #----------------------------------------------------------------------------- | ||
| 93 | |||
| 94 | sub get_delimiters { | ||
| 95 | my ($elem) = @_; | ||
| 96 | return if !$elem->{sections}; | ||
| 97 | my @delimiters; | ||
| 98 | if (!$elem->{sections}->[0]->{type}) { | ||
| 99 | # PPI v1.118 workaround: the delimiters were not recorded in some cases | ||
| 100 | # hack: pull them out ourselves | ||
| 101 | # limitation: this regexp fails on s{foo}<bar> | ||
| 102 | my $operator = defined $elem->{operator} ? $elem->{operator} : q{}; | ||
| 103 | @delimiters = join q{}, $elem =~ m/\A $operator (.).*?(.) (?:[xmsocgie]*) \z/xms; | ||
| 104 | } else { | ||
| 105 | @delimiters = ($elem->{sections}->[0]->{type}); | ||
| 106 | if ($elem->{sections}->[1]) { | ||
| 107 | push @delimiters, $elem->{sections}->[1]->{type} || $delimiters[0]; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return @delimiters; | ||
| 111 | } | ||
| 112 | |||
| 113 | #----------------------------------------------------------------------------- | ||
| 114 | |||
| 115 | { | ||
| 116 | ## This nastiness is to auto-vivify PPI packages from Regexp::Parser classes | ||
| 117 | |||
| 118 | # Track which ones are already created | ||
| 119 | my %seen = ('Regexp::Parser::__object__' => 1); | ||
| 120 | |||
| 121 | sub _get_ppi_package { | ||
| 122 | my ($src_class, $re_node) = @_; | ||
| 123 | (my $dest_class = $src_class) =~ s/\A Regexp::Parser::/Perl::Critic::PPIRegexp::/xms; | ||
| 124 | if (!$seen{$src_class}) { | ||
| 125 | $seen{$src_class} = 1; | ||
| 126 | croak 'Regexp node which is not in the Regexp::Parser namespace' | ||
| 127 | if $dest_class eq $src_class; | ||
| 128 | my $src_isa_name = $src_class . '::ISA'; | ||
| 129 | my $dest_isa_name = $dest_class . '::ISA'; | ||
| 130 | my @isa; | ||
| 131 | for my $isa (eval "\@$src_isa_name") { ## no critic (StringyEval) | ||
| 132 | my $dest_isa = _get_ppi_package($isa, $re_node); | ||
| 133 | push @isa, $dest_isa; | ||
| 134 | } | ||
| 135 | eval "\@$dest_isa_name = qw(@isa)"; ## no critic (Eval) | ||
| 136 | croak $EVAL_ERROR if $EVAL_ERROR; | ||
| 137 | } | ||
| 138 | return $dest_class; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | Readonly::Scalar my $NO_DEPTH_USED => -1; | ||
| 143 | |||
| 144 | sub ppiify { | ||
| 145 | my ($re) = @_; | ||
| 146 | return if !$re; | ||
| 147 | |||
| 148 | # walk the Regexp::Parser tree, converting to PPI nodes as we go | ||
| 149 | |||
| 150 | my $ppire = PPI::Node->new; | ||
| 151 | my @stack = ($ppire); | ||
| 152 | my $iter = $re->walker; | ||
| 153 | my $last_depth = $NO_DEPTH_USED; | ||
| 154 | while (my ($node, $depth) = $iter->()) { | ||
| 155 | if ($last_depth > $depth) { # -> parent | ||
| 156 | # walker() creates pseudo-closing nodes for reasons I don't understand | ||
| 157 | while ($last_depth-- > $depth) { | ||
| 158 | pop @stack; | ||
| 159 | } | ||
| 160 | } else { | ||
| 161 | my $src_class = ref $node; | ||
| 162 | my $ppipkg = _get_ppi_package($src_class, $node); | ||
| 163 | my $ppinode = $ppipkg->new($node); | ||
| 164 | if ($last_depth == $depth) { # -> sibling | ||
| 165 | $stack[-1] = $ppinode; | ||
| 166 | } else { # -> child | ||
| 167 | push @stack, $ppinode; | ||
| 168 | } | ||
| 169 | $stack[-2]->add_element($ppinode); ## no critic qw(MagicNumbers) | ||
| 170 | } | ||
| 171 | $last_depth = $depth; | ||
| 172 | } | ||
| 173 | return $ppire; | ||
| 174 | } | ||
| 175 | |||
| 176 | { | ||
| 177 | package ## no critic (ProhibitMultiplePackages, NamingConventions::Capitalization) # hide from PAUSE | ||
| 178 | Perl::Critic::PPIRegexp::__object__; | ||
| 179 | use base 'PPI::Node'; | ||
| 180 | |||
| 181 | # Base wrapper class for PPI versions of Regexp::Parser classes | ||
| 182 | |||
| 183 | # This is a hack because we call everything PPI::Node instances instead of | ||
| 184 | # PPI::Token instances. One downside is that PPI::Dumper doesn't work on | ||
| 185 | # regexps. | ||
| 186 | |||
| 187 | sub new { | ||
| 188 | my ($class, $re_node) = @_; | ||
| 189 | my $self = $class->SUPER::new(); | ||
| 190 | $self->{_re} = $re_node; | ||
| 191 | return $self; | ||
| 192 | } | ||
| 193 | sub content { | ||
| 194 | my ($self) = @_; | ||
| 195 | return $self->{_re}->visual; | ||
| 196 | } | ||
| 197 | sub re { | ||
| 198 | my ($self) = @_; | ||
| 199 | return $self->{_re}; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | #----------------------------------------------------------------------------- | ||
| 204 | |||
| 205 | sub regexp_interpolates { | ||
| 206 | my ( $elem ) = @_; | ||
| 207 | my @delim = get_delimiters( $elem ) or return; | ||
| 208 | return q{'} ne substr $delim[0], 0, 1; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ |