| 1 | package App::BoolFindGrep::Bool; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use common::sense; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use charnames q(:full); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use English qw[-no_match_vars]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use List::Util qw[first]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Text::Balanced qw[extract_delimited extract_multiple]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 5 | our $VERSION = '0.06'; # VERSION | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | has slash_as_delim => ( | ||||||||||||||||||||||||||||||||
| 14 | is => q(rw), | ||||||||||||||||||||||||||||||||
| 15 | 44 | ▶ | isa => sub { ( $_[0] == 0 || $_[0] == 1 ) or die }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
Condition66%2/3
$_[0] == 0 or $_[0] == 1
MC/DC0%0/2
$_[0] == 0 or $_[0] == 1
$_[0] == 0
$_[0] == 1
Truth table66%2/3
A or B
A= $_[0] == 0B= $_[0] == 1
| |||||||||||||||||||||||||||||||||
| 16 | 5 | default => 0, | |||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||
| 18 | has operators => ( | ||||||||||||||||||||||||||||||||
| 19 | is => q(ro), | ||||||||||||||||||||||||||||||||
| 20 | 12 | ▶ | default => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 12 | { AND => q(&&), # | |||||||||||||||||||||||||||||||
| 22 | OR => q(||), # | ||||||||||||||||||||||||||||||||
| 23 | NOT => q(!), # | ||||||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||||||
| 26 | 5 | ); | |||||||||||||||||||||||||||||||
| 27 | 5 | has expression => ( is => q(rw), default => undef ); | |||||||||||||||||||||||||||||||
| 28 | 12 | ▶ | has operands => ( is => q(rw), default => sub { []; }, ); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 12 | ▶ | has parse => ( is => q(rw), default => sub { []; }, ); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 43 | ▶ | sub parse_expr { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_expr: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 32 | 43 | my $self = shift; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 43 | ▶ | return 1 unless defined $self->expression(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 43 | ▶ | return 1 if $self->expression() eq q(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 43 | $self->operands(undef); | |||||||||||||||||||||||||||||||
| 38 | 43 | $self->parse(undef); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 43 | my @token = $self->tokenizer( $self->expression() ); | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 39 | ▶ | return unless $self->lazy_checker(@token); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 36 | $self->operands_collector(@token); | |||||||||||||||||||||||||||||||
| 45 | 34 | my @expression = @token; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 34 | $self->parse( [@expression] ); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 34 | return 1; | |||||||||||||||||||||||||||||||
| 50 | } ## end sub parse_expr | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 43 | ▶ | sub tokenizer { | ||||||||||||||||||||||||||||||
|
Subroutine
tokenizer: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 53 | 43 | my $self = shift; | |||||||||||||||||||||||||||||||
| 54 | 43 | my $expression = shift; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 43 | my $op = join qq(\N{VERTICAL LINE}), keys %{ $self->operators() }; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 43 | my @expression; | |||||||||||||||||||||||||||||||
| 59 | 43 | ▶ | if ( $self->slash_as_delim() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | @expression = extract_multiple( | ||||||||||||||||||||||||||||||||
| 61 | $expression, # | ||||||||||||||||||||||||||||||||
| 62 | 32 | ▶ | [ sub { extract_delimited( $_[0], '/' ) } ], # | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||
| 66 | 11 | @expression = $expression; | |||||||||||||||||||||||||||||||
| 67 | 11 | $expression[0] =~ s{\N{SOLIDUS}}{\N{REVERSE SOLIDUS}\N{SOLIDUS}}gmsx; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 43 | foreach (@expression) { | |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 47 | s{\A\p{IsSpace}}{}msx; | |||||||||||||||||||||||||||||||
| 73 | 47 | s{\p{IsSpace}\z}{}msx; | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 47 | ▶ | if ( m{\A\N{SOLIDUS}}msx | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/\A\N{U+2F}/msux and /(?<!\N{U+5C})\N{U+2F}\z/msux
MC/DC50%1/2
/\A\N{U+2F}/msux and /(?<!\N{U+5C})\N{U+2F}\z/msux
/\A\N{U+2F}/msux
/(?<!\N{U+5C})\N{U+2F}\z/msux
Truth table66%2/3
A and B
A= /\A\N{U+2F}/msuxB= /(?<!\N{U+5C})\N{U+2F}\z/msux
| |||||||||||||||||||||||||||||||||
| 76 | && m{(?<!\N{REVERSE SOLIDUS})\N{SOLIDUS}\z}msx ) | ||||||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||||||
| 78 | 8 | ▶ | croak sprintf q(Syntax Error in expression: '%s'), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | $self->expression() | ||||||||||||||||||||||||||||||||
| 80 | if length() < 3; | ||||||||||||||||||||||||||||||||
| 81 | 4 | next; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | s{(?<!\\)([()])} # PARENTHESIS | ||||||||||||||||||||||||||||||||
| 85 | 39 | {\N{LINE FEED}$1\N{LINE FEED}}gmsx; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 39 | s{(?:\A|\s)(${op})(?=\s|\z)} # OPERATORS | |||||||||||||||||||||||||||||||
| 88 | {\N{LINE FEED}$1\N{LINE FEED}}gimsx; | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 39 | s{\A\p{IsSpace}+}{}msx; | |||||||||||||||||||||||||||||||
| 91 | 39 | s{\p{IsSpace}+\z}{}msx; | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 39 | s{\N{SPACE}*\N{LINE FEED}+\N{SPACE}*} | |||||||||||||||||||||||||||||||
| 94 | {\N{LINE FEED}}gmsx; | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | } ## end foreach (@expression) | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 39 | my @token = map { split m{\N{LINE FEED}}msx } @expression; | |||||||||||||||||||||||||||||||
| 99 | 39 | ▶ | @token = grep { defined && $_ ne q() } @token; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && $_ ne ''
MC/DC50%1/2
defined $_ && $_ ne ''
defined $_
$_ ne ''
Truth table66%2/3
A && B
A= defined $_B= $_ ne ''
| |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 39 | foreach my $token (@token) { | |||||||||||||||||||||||||||||||
| 102 | 161 | ▶ | if ( $token eq qq(\N{LEFT PARENTHESIS}) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$token eq "(" or $token eq ")"
MC/DC100%2/2
$token eq "(" or $token eq ")"
$token eq "("
$token eq ")"
Truth table100%3/3
A or B
A= $token eq "("B= $token eq ")"
| |||||||||||||||||||||||||||||||||
| 103 | || $token eq qq(\N{RIGHT PARENTHESIS}) ) | ||||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||
| 105 | 40 | $token = [ q(PARENTHESIS), $token ]; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | elsif ( exists $self->operators->{uc $token} ) { | ||||||||||||||||||||||||||||||||
| 108 | 48 | $token = [ q(OPERATOR), uc $token ],; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||||||
| 111 | 73 | ▶ | if ($token =~ # | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | m{\A\N{SOLIDUS} | ||||||||||||||||||||||||||||||||
| 113 | (?<token>.*?) | ||||||||||||||||||||||||||||||||
| 114 | (?<!\N{REVERSE SOLIDUS})\N{SOLIDUS}\z | ||||||||||||||||||||||||||||||||
| 115 | }msx | ||||||||||||||||||||||||||||||||
| 116 | ) | ||||||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||||||
| 118 | 4 | $token = $LAST_PAREN_MATCH{token}; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 73 | $token =~ s{\N{REVERSE SOLIDUS}\N{SOLIDUS}} | |||||||||||||||||||||||||||||||
| 121 | {\N{SOLIDUS}}gmsx; | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 73 | $token = [ q(OPERAND), $token ]; | |||||||||||||||||||||||||||||||
| 124 | } ## end else [ if ( $token eq qq(\N{LEFT PARENTHESIS})...)] | ||||||||||||||||||||||||||||||||
| 125 | } ## end foreach my $token (@token) | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 39 | return @token; | |||||||||||||||||||||||||||||||
| 128 | } ## end sub tokenizer | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 39 | ▶ | sub lazy_checker { | ||||||||||||||||||||||||||||||
|
Subroutine
lazy_checker: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 131 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||
| 132 | 39 | my @token = splice @_; | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 39 | my $status; | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 39 | foreach my $token (@token) { | |||||||||||||||||||||||||||||||
| 137 | 161 | my ( $name, $value ) = @$token; | |||||||||||||||||||||||||||||||
| 138 | 161 | ▶ | if ( $name eq q(OPERAND) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 139 | 73 | $token = 1; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | elsif ( $name eq q(OPERATOR) ) { | ||||||||||||||||||||||||||||||||
| 142 | 48 | $token = $self->operators->{$value}; | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | 40 | else { $token = $value; } | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 39 | my $expression = join qq(\N{SPACE}), @token; | |||||||||||||||||||||||||||||||
| 148 | 39 | $EVAL_ERROR = q(); | |||||||||||||||||||||||||||||||
| 149 | 39 | eval $expression; | |||||||||||||||||||||||||||||||
| 150 | 39 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | 3 | croak sprintf q(Syntax Error in expression: '%s'), | |||||||||||||||||||||||||||||||
| 152 | $self->expression(); | ||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | 36 | else { $status = 1; } | |||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 36 | return $status; | |||||||||||||||||||||||||||||||
| 157 | } ## end sub lazy_checker | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 36 | ▶ | sub operands_collector { | ||||||||||||||||||||||||||||||
|
Subroutine
operands_collector: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 160 | 36 | my $self = shift; | |||||||||||||||||||||||||||||||
| 161 | 36 | my @token = splice @_; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 36 | my %operand; | |||||||||||||||||||||||||||||||
| 164 | 36 | foreach my $token (@token) { | |||||||||||||||||||||||||||||||
| 165 | 149 | my ( $name, $value ) = @$token; | |||||||||||||||||||||||||||||||
| 166 | 149 | ▶ | next if $name ne q(OPERAND); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 69 | $operand{$value} = 1; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 36 | ▶ | unless (%operand) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | 2 | croak sprintf q(Syntax Error in expression: '%s'), | |||||||||||||||||||||||||||||||
| 172 | $self->expression(); | ||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 34 | $self->operands( [ keys %operand ] ); | |||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 34 | return 1; | |||||||||||||||||||||||||||||||
| 178 | } ## end sub operands_collector | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 3960 | ▶ | sub lazy_solver { | ||||||||||||||||||||||||||||||
|
Subroutine
lazy_solver: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 181 | 3960 | my $self = shift; | |||||||||||||||||||||||||||||||
| 182 | 3960 | my %operand = splice @_; | |||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 3960 | my @expression; | |||||||||||||||||||||||||||||||
| 185 | 3960 | foreach my $token ( @{ $self->parse() } ) { | |||||||||||||||||||||||||||||||
| 186 | 3960 | my ( $name, $value ) = @$token; | |||||||||||||||||||||||||||||||
| 187 | 3960 | ▶ | if ( $name eq q(OPERAND) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 188 | 3960 | $value = $operand{$value}; | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | elsif ( $name eq q(OPERATOR) ) { | ||||||||||||||||||||||||||||||||
| 191 | 0 | $value = $self->operators->{$value}; | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | 3960 | push @expression, $value; | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | 3960 | my $expression = join qq(\N{SPACE}), @expression; | |||||||||||||||||||||||||||||||
| 196 | 3960 | my $result = eval $expression; | |||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 3960 | return $result; | |||||||||||||||||||||||||||||||
| 199 | } ## end sub lazy_solver | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 5 | ▶ | no Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 202 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 5 | 1; | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||||||