| 1 | ############################################################################## | ||
| 2 | # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/PPIx-Utilities/lib/PPIx/Utilities/Statement.pm $ | ||
| 3 | # $Date: 2010-11-13 14:25:12 -0600 (Sat, 13 Nov 2010) $ | ||
| 4 | # $Author: clonezone $ | ||
| 5 | # $Revision: 3990 $ | ||
| 6 | ############################################################################## | ||
| 7 | |||
| 8 | package PPIx::Utilities::Statement; | ||
| 9 | |||
| 10 | use 5.006001; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | our $VERSION = '1.001000'; | ||
| 15 | |||
| 16 | use Readonly; | ||
| 17 | |||
| 18 | |||
| 19 | use PPI 1.208 qw< >; # Just for the version check. | ||
| 20 | |||
| 21 | use base 'Exporter'; | ||
| 22 | |||
| 23 | our @EXPORT_OK = qw( | ||
| 24 | get_constant_name_elements_from_declaring_statement | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | Readonly::Hash my %IS_COMMA => ( q[,] => 1, q[=>] => 1 ); | ||
| 29 | |||
| 30 | |||
| 31 | sub get_constant_name_elements_from_declaring_statement { | ||
| 32 | my ($element) = @_; | ||
| 33 | |||
| 34 | return if not $element; | ||
| 35 | return if not $element->isa('PPI::Statement'); | ||
| 36 | |||
| 37 | if ( $element->isa('PPI::Statement::Include') ) { | ||
| 38 | my $pragma; | ||
| 39 | if ( $pragma = $element->pragma() and $pragma eq 'constant' ) { | ||
| 40 | return _get_constant_names_from_constant_pragma($element); | ||
| 41 | } # end if | ||
| 42 | } elsif ( not $element->specialized() and $element->schildren() > 2 ) { | ||
| 43 | my $supposed_constant_function = $element->schild(0)->content(); | ||
| 44 | my $declaring_scope = $element->schild(1)->content(); | ||
| 45 | |||
| 46 | if ( | ||
| 47 | ( | ||
| 48 | $supposed_constant_function eq 'const' | ||
| 49 | or $supposed_constant_function =~ m< \A Readonly \b >xms | ||
| 50 | ) | ||
| 51 | and ($declaring_scope eq 'our' or $declaring_scope eq 'my') | ||
| 52 | ) { | ||
| 53 | return $element->schild(2); | ||
| 54 | } # end if | ||
| 55 | } # end if | ||
| 56 | |||
| 57 | return; | ||
| 58 | } # end get_constant_name_elements_from_declaring_statement() | ||
| 59 | |||
| 60 | |||
| 61 | sub _get_constant_names_from_constant_pragma { | ||
| 62 | my ($include) = @_; | ||
| 63 | |||
| 64 | my @arguments = $include->arguments() or return; | ||
| 65 | |||
| 66 | my $follower = $arguments[0]; | ||
| 67 | return if not defined $follower; | ||
| 68 | |||
| 69 | # We test for a 'PPI::Structure::Block' in the following because some | ||
| 70 | # versions of PPI parse the last element of 'use constant { ONE => 1, TWO | ||
| 71 | # => 2 }' as a block rather than a constructor. As of PPI 1.206, PPI | ||
| 72 | # handles the above correctly, but still blows it on 'use constant 1.16 { | ||
| 73 | # ONE => 1, TWO => 2 }'. | ||
| 74 | if ( | ||
| 75 | $follower->isa( 'PPI::Structure::Constructor' ) | ||
| 76 | or $follower->isa( 'PPI::Structure::Block' ) | ||
| 77 | ) { | ||
| 78 | my $statement = $follower->schild( 0 ) or return; | ||
| 79 | $statement->isa( 'PPI::Statement' ) or return; | ||
| 80 | |||
| 81 | my @elements; | ||
| 82 | my $inx = 0; | ||
| 83 | foreach my $child ( $statement->schildren() ) { | ||
| 84 | if (not $inx % 2) { | ||
| 85 | push @{ $elements[ $inx ] ||= [] }, $child; | ||
| 86 | } # end if | ||
| 87 | |||
| 88 | if ( $IS_COMMA{ $child->content() } ) { | ||
| 89 | $inx++; | ||
| 90 | } # end if | ||
| 91 | } # end foreach | ||
| 92 | |||
| 93 | return map | ||
| 94 | { | ||
| 95 | ( | ||
| 96 | $_ | ||
| 97 | and @{$_} == 2 | ||
| 98 | and '=>' eq $_->[1]->content() | ||
| 99 | and $_->[0]->isa( 'PPI::Token::Word' ) | ||
| 100 | ) | ||
| 101 | ? $_->[0] | ||
| 102 | : () | ||
| 103 | } | ||
| 104 | @elements; | ||
| 105 | } else { | ||
| 106 | return $follower; | ||
| 107 | } # end if | ||
| 108 | |||
| 109 | return $follower; | ||
| 110 | } # end _get_constant_names_from_constant_pragma() | ||
| 111 | |||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | __END__ |