| 1 | package App::Greple::Pattern::Holder; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 171 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 171 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 171 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 171 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 171 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 171 | our @EXPORT = (); | |||||||||||||||||||||||||||
| 10 | 171 | our %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 11 | 171 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 171 | ▶ | use App::Greple::Pattern; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 170 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 170 | my $class = shift; | |||||||||||||||||||||||||||
| 17 | 170 | my $obj = bless [], $class; | |||||||||||||||||||||||||||
| 18 | 170 | $obj; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 274 | ▶ | sub append { | ||||||||||||||||||||||||||
|
Subroutine
append: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 274 | my $obj = shift; | |||||||||||||||||||||||||||
| 23 | 274 | ▶ | my $arg = ref $_[0] eq 'HASH' ? shift : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 274 | ▶ | return $obj unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 274 | ▶ | $arg->{type} //= 'pattern'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{"type"} //= "pattern"
MC/DC100%1/1
$arg->{"type"} //= "pattern"
$arg->{"type"}
Truth table100%2/2
A
A= $arg->{"type"}
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 274 | ▶ | if ($arg->{type} eq 'file') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 9 | $obj->load_file($arg, @_); | |||||||||||||||||||||||||||
| 31 | 9 | return $obj; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 265 | ▶ | if ($arg->{flag} & FLAG_LEXICAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 33 | for (@_) { | |||||||||||||||||||||||||||
| 36 | 33 | $obj->lexical_opt($arg, $_); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 33 | return $obj; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 232 | ▶ | if ($arg->{flag} & FLAG_OR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 9 | $arg->{flag} &= ~FLAG_OR; | |||||||||||||||||||||||||||
| 43 | my @p = map { | ||||||||||||||||||||||||||||
| 44 | 9 | App::Greple::Pattern->new | |||||||||||||||||||||||||||
| 45 | 40 | ($_, flag => $arg->{flag} & ~FLAG_IGNORECASE) | |||||||||||||||||||||||||||
| 46 | ->cooked; | ||||||||||||||||||||||||||||
| 47 | } @_; | ||||||||||||||||||||||||||||
| 48 | 9 | my $p = "(?x)\n " . join("\n| ", map qr/$_/m, @p); | |||||||||||||||||||||||||||
| 49 | 9 | $arg->{flag} |= FLAG_REGEX; | |||||||||||||||||||||||||||
| 50 | 9 | $arg->{flag} &= ~FLAG_COOK; | |||||||||||||||||||||||||||
| 51 | 9 | push @$obj, App::Greple::Pattern->new($p, flag => $arg->{flag}); | |||||||||||||||||||||||||||
| 52 | 9 | return $obj; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 223 | for (@_) { | |||||||||||||||||||||||||||
| 56 | 223 | push @$obj, App::Greple::Pattern->new($_, flag => $arg->{flag}); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 221 | $obj; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub optimize { | ||||||||||||||||||||||||||
|
Subroutine
optimize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my $obj = shift; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # collect required pattern at the top of list | ||||||||||||||||||||||||||||
| 66 | 0 | @$obj = ( grep( { $_->is_required } @$obj ), | |||||||||||||||||||||||||||
| 67 | 0 | grep( { ! $_->is_required } @$obj ) ); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $obj; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 33 | ▶ | sub lexical_opt { | ||||||||||||||||||||||||||
|
Subroutine
lexical_opt: called CC=10 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 33 | my($obj, $arg, $opt) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 33 | ▶ | unless ($arg->{flag} & FLAG_LEXICAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | die "Unexpected flag value ($arg->{flag})"; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 33 | my $orig_flag = $arg->{flag} & ~FLAG_LEXICAL; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 33 | my $or; | |||||||||||||||||||||||||||
| 81 | my @pattern; | ||||||||||||||||||||||||||||
| 82 | 33 | for (split /(?<!\\)\s+/, $opt) { | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 44 | ▶ | next if $_ eq ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 44 | my $flag = $orig_flag; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 44 | ▶ | if (s/^\+//) { # +pattern | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 89 | 3 | $flag |= FLAG_REQUIRED; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | elsif (s/^-//) { # -pattern | ||||||||||||||||||||||||||||
| 92 | 2 | $flag |= FLAG_NEGATIVE; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif (s/^\?//) { # ?pattern | ||||||||||||||||||||||||||||
| 95 | 2 | $flag |= FLAG_OPTIONAL; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 44 | ▶ | if (s/^\&//) { # &func(...) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $flag |= FLAG_FUNCTION; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 44 | $flag |= FLAG_REGEX; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 44 | ▶ | push @pattern, [ { flag => $flag }, $_ ] if $_ ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 33 | for (@pattern) { | |||||||||||||||||||||||||||
| 109 | 44 | $obj->append(@$_); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 171 | ▶ | use Getopt::EX::Numbers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 9 | ▶ | sub load_file { | ||||||||||||||||||||||||||
|
Subroutine
load_file: called CC=14 SCAR=33.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 9 | my $obj = shift; | |||||||||||||||||||||||||||
| 117 | 9 | ▶ | my $arg = ref $_[0] eq 'HASH' ? shift : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 9 | $arg->{type} = 'pattern'; | |||||||||||||||||||||||||||
| 120 | 9 | ▶ | my $flag = ( $arg->{flag} // 0 ) | FLAG_REGEX | FLAG_OR; | ||||||||||||||||||||||||||
|
Condition50%1/2
$arg->{"flag"} // 0
MC/DC0%0/1
$arg->{"flag"} // 0
$arg->{"flag"}
Truth table50%1/2
A
A= $arg->{"flag"}
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 9 | for my $file (@_) { | |||||||||||||||||||||||||||
| 123 | 9 | my $select; | |||||||||||||||||||||||||||
| 124 | 9 | ▶ | if (!-f $file and $file =~ s/\@ (?<n>[\d:,]+) $//x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not -f $file and $file =~ s/\@ (?<n>[\d:,]+) $//ux
MC/DC50%1/2
not -f $file and $file =~ s/\@ (?<n>[\d:,]+) $//ux
not -f $file
$file =~ s/\@ (?<n>[\d:,]+) $//ux
Truth table66%2/3
not(A) and B
A= not -f $fileB= $file =~ s/\@ (?<n>[\d:,]+) $//ux
| |||||||||||||||||||||||||||||
| 125 | 1 | $select = $+{n}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 9 | ▶ | open my $fh, '<:encoding(utf8)', $file or die "$file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 9 | my @p = map s/\\(?=\R)//gr, split /(?<!\\)\R/, do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 129 | 9 | ▶ | if ($select //= $arg->{select}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$select //= $arg->{"select"}
MC/DC100%2/2
$select //= $arg->{"select"}
$select
$arg->{"select"}
Truth table100%3/3
A //= B
A= $selectB= $arg->{"select"}
| |||||||||||||||||||||||||||||
| 130 | 5 | my $numbers = Getopt::EX::Numbers->new(min => 1, max => int @p); | |||||||||||||||||||||||||||
| 131 | 5 | my @select = do { | |||||||||||||||||||||||||||
| 132 | 25 | map { $_ - 1 } | |||||||||||||||||||||||||||
| 133 | 37 | sort { $a <=> $b } | |||||||||||||||||||||||||||
| 134 | 25 | grep { $_ <= @p } | |||||||||||||||||||||||||||
| 135 | 5 | map { $numbers->parse($_)->sequence } | |||||||||||||||||||||||||||
| 136 | split /,/, $select; | ||||||||||||||||||||||||||||
| 137 | }; | ||||||||||||||||||||||||||||
| 138 | 5 | @p = @p[@select]; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | ## | ||||||||||||||||||||||||||||
| 141 | ## Collect DEFINE patterns | ||||||||||||||||||||||||||||
| 142 | ## | ||||||||||||||||||||||||||||
| 143 | 9 | my %DEFINE; | |||||||||||||||||||||||||||
| 144 | 9 | for (@p) { | |||||||||||||||||||||||||||
| 145 | 46 | ▶ | if (/^\Q(?(DEFINE)(?<\E(?<name>[^>]+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $DEFINE{$+{name}} = $_; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 9 | @p = do { | |||||||||||||||||||||||||||
| 150 | 40 | map { chomp; s{\s*//.*$}{}r } | |||||||||||||||||||||||||||
| 151 | 46 | grep { not m{^\s*(?:#|//|$)} } | |||||||||||||||||||||||||||
| 152 | 9 | grep { not m{^\Q(?(DEFINE)\E} } | |||||||||||||||||||||||||||
| 153 | @p; | ||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | ## | ||||||||||||||||||||||||||||
| 156 | ## Append DEFINE to each pattern that references it | ||||||||||||||||||||||||||||
| 157 | ## | ||||||||||||||||||||||||||||
| 158 | 9 | ▶ | if (%DEFINE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | for my $p (@p) { | |||||||||||||||||||||||||||
| 160 | 0 | my %define; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | (sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 162 | 0 | my($str, $seen) = @_; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | $seen //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$seen //= {}
MC/DC0%0/1
$seen //= {}
$seen
Truth table0%0/2
A
A= $seen
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | while ($str =~ /\(\?&(?<name>[^)]+)\)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | my $name = $+{name}; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | next if $seen->{$name}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my $def = $DEFINE{$name} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $define{$name} //= $def; | ||||||||||||||||||||||||||
|
Condition0%0/3
$define{$name} //= $def
MC/DC0%0/2
$define{$name} //= $def
$define{$name}
$def
Truth table0%0/3
A //= B
A= $define{$name}B= $def
| |||||||||||||||||||||||||||||
| 169 | 0 | __SUB__->($def, $seen); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | })->($p); | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | $p .= "(?x:\n" . join("\n", values %define) . ")" if %define; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 9 | $obj->append({ flag => $flag }, @p); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 829 | ▶ | sub patterns { | ||||||||||||||||||||||||||
|
Subroutine
patterns: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 829 | my $obj = shift; | |||||||||||||||||||||||||||
| 181 | 829 | @{ $obj }; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 171 | 1; | |||||||||||||||||||||||||||