« lib/Perl/ToPerl6/TransformerConfig.pm
↑ Summary
lib/Perl/ToPerl6/Transformer/CompoundStatements/SwapForArguments.pm »
| 1 | package Perl::ToPerl6::Theme; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 13 | ▶ | use 5.006001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 13 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 13 | ▶ | use Readonly; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 13 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 13 | ▶ | use List::MoreUtils qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 13 | ▶ | use Perl::ToPerl6::Utils qw{ :characters :data_conversion }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 13 | ▶ | use Perl::ToPerl6::Exception::Fatal::Internal qw{ &throw_internal }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | use Perl::ToPerl6::Exception::Configuration::Option::Global::ParameterValue | ||||||||||||||||||||
| 16 | 13 | ▶ | qw{ &throw_global_value }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 13 | Readonly::Array our @EXPORT_OK => qw{ | |||||||||||||||||||
| 21 | $RULE_INVALID_CHARACTER_REGEX | ||||||||||||||||||||
| 22 | cook_rule | ||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 13 | Readonly::Scalar our $RULE_INVALID_CHARACTER_REGEX => | |||||||||||||||||||
| 28 | qr/ ( [^()\s\w\d+\-*&|!] ) /xms; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 13 | Readonly::Scalar my $CONFIG_KEY => 'theme'; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 112 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 112 | my ( $class, %args ) = @_; | |||||||||||||||||||
| 39 | 112 | my $self = bless {}, $class; | |||||||||||||||||||
| 40 | 112 | $self->_init( %args ); | |||||||||||||||||||
| 41 | 104 | return $self; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 112 | ▶ | sub _init { | ||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 112 | my ($self, %args) = @_; | |||||||||||||||||||
| 49 | 112 | ▶ | my $rule = $args{-rule} || $EMPTY; | ||||||||||||||||||
|
Condition66%2/3
$args{'-rule'} || $Perl::ToPerl6::Theme::EMPTY
MC/DC50%1/2
$args{'-rule'} || $Perl::ToPerl6::Theme::EMPTY
$args{'-rule'}
$Perl::ToPerl6::Theme::EMPTY
Truth table66%2/3
A || B
A= $args{'-rule'}B= $Perl::ToPerl6::Theme::EMPTY
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 112 | ▶ | if ( $rule =~ m/$RULE_INVALID_CHARACTER_REGEX/xms ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 8 | throw_global_value | |||||||||||||||||||
| 53 | option_name => $CONFIG_KEY, | ||||||||||||||||||||
| 54 | option_value => $rule, | ||||||||||||||||||||
| 55 | message_suffix => qq{contains an invalid character: "$1".}; | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 104 | $self->{_rule} = cook_rule( $rule ); | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 104 | return $self; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 5 | ▶ | sub rule { | ||||||||||||||||||
|
Subroutine
rule: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 66 | 5 | my $self = shift; | |||||||||||||||||||
| 67 | 5 | return $self->{_rule}; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1597 | ▶ | sub transformer_is_thematic { | ||||||||||||||||||
|
Subroutine
transformer_is_thematic: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1597 | my ($self, %args) = @_; | |||||||||||||||||||
| 75 | my $transformer = $args{-transformer} | ||||||||||||||||||||
| 76 | 1597 | ▶ | || throw_internal 'The -transformer argument is required'; | ||||||||||||||||||
|
Condition33%1/3
$args{'-transformer'} || throw_internal('The -transformer argument is required')
MC/DC0%0/2
$args{'-transformer'} || throw_internal('The -transformer argument is required')
$args{'-transformer'}
throw_internal('The -transformer argument is required')
Truth table33%1/3
A || B
A= $args{'-transformer'}B= throw_internal('The -transformer argument is required')
| |||||||||||||||||||||
| 77 | 1597 | ▶ | ref $transformer | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | || throw_internal 'The -transformer must be an object'; | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 1597 | ▶ | my $rule = $self->{_rule} or return 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 799 | my %themes = hashify( $transformer->get_themes() ); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | # This bit of magic turns the rule into a perl expression that can be | ||||||||||||||||||||
| 84 | # eval-ed for truth. Each theme name in the rule is translated to 1 or 0 | ||||||||||||||||||||
| 85 | # if the $transformer belongs in that theme. For example: | ||||||||||||||||||||
| 86 | # | ||||||||||||||||||||
| 87 | # 'bugs && (pbp || core)' ...could become... '1 && (0 || 1)' | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 799 | my $as_code = $rule; #Making a copy, so $rule is preserved | |||||||||||||||||||
| 90 | 799 | ▶ | $as_code =~ s/ ( [\w\d]+ ) /exists $themes{$1} || 0/gexms; | ||||||||||||||||||
|
Condition100%2/2
exists $themes{$1} || 0
MC/DC100%1/1
exists $themes{$1} || 0
exists $themes{$1}
Truth table100%2/2
A
A= exists $themes{$1}
| |||||||||||||||||||||
| 91 | 799 | my $is_thematic = eval $as_code; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 799 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 1 | throw_global_value | |||||||||||||||||||
| 95 | option_name => $CONFIG_KEY, | ||||||||||||||||||||
| 96 | option_value => $rule, | ||||||||||||||||||||
| 97 | message_suffix => q{contains a syntax error.}; | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 798 | return $is_thematic; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 191 | ▶ | sub cook_rule { | ||||||||||||||||||
|
Subroutine
cook_rule: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 106 | 191 | my ($raw_rule) = @_; | |||||||||||||||||||
| 107 | 191 | ▶ | return if not defined $raw_rule; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | #Translate logical operators | ||||||||||||||||||||
| 110 | 191 | $raw_rule =~ s{\b not \b}{!}ixmsg; # "not" -> "!" | |||||||||||||||||||
| 111 | 191 | $raw_rule =~ s{\b and \b}{&&}ixmsg; # "and" -> "&&" | |||||||||||||||||||
| 112 | 191 | $raw_rule =~ s{\b or \b}{||}ixmsg; # "or" -> "||" | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | #Translate algebra operators (for backward compatibility) | ||||||||||||||||||||
| 115 | 191 | $raw_rule =~ s{\A [-] }{!}ixmsg; # "-" -> "!" e.g. difference | |||||||||||||||||||
| 116 | 191 | $raw_rule =~ s{ [-] }{&& !}ixmsg; # "-" -> "&& !" e.g. difference | |||||||||||||||||||
| 117 | 191 | $raw_rule =~ s{ [*] }{&&}ixmsg; # "*" -> "&&" e.g. intersection | |||||||||||||||||||
| 118 | 191 | $raw_rule =~ s{ [+] }{||}ixmsg; # "+" -> "||" e.g. union | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 191 | my $cooked_rule = lc $raw_rule; #Is now cooked! | |||||||||||||||||||
| 121 | 191 | return $cooked_rule; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 13 | 1; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||