| 1 | package Zoidberg::StringParser; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # Hic sunt leones. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 65 | our $VERSION = '0.981'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 65 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 65 | ▶ | no warnings; # can't stand the nagging | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 65 | ▶ | use Zoidberg::Utils qw/debug error bug/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 65 | our $ERROR_CALLER = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # TODO : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # esc per type ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # how bout more general state machine approach, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # making QUOTE and NEST operations like CUT, POP and RECURS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # grammar can be big hash (sort keys on length) .. how to deal with regexes than ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # ... optimise for normal string tokens, regexes are the exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # need seperate hashes for overloading | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # how bout ->for(gram, string, int, sub) ? exec sub on token with most parser vars in scope | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # %state ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 68 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 68 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 68 | ▶ | my $self = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
shift() || {}
shift() || {}
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
MC/DC100%1/1
shift() || {}
shift()
MC/DC100%1/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Truth table100%2/2
A
A= shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | base_gram => shift || {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | collection => shift || {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | settings => shift || {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 68 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 68 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 10783 | ▶ | sub split { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split: called CC=49 SCAR=39.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 10783 | my ($self, $gram, $input, $int) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 10783 | $int--; # 1 based => 0 based | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 10783 | $$self{broken} = undef; # reset error | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 10783 | debug "splitting with $gram"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 10783 | ▶ | unless (ref $gram) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 10780 | ▶ | error "No such grammar: $gram" unless $$self{collection}{$gram}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 10780 | $gram = [$$self{collection}{$gram}] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | elsif (ref($gram) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | $gram = [ map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | ref($_) ? $_ : ($$self{collection}{$_} || $error++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"collection"}{$_} or $error++
MC/DC0%0/2
$self->{"collection"}{$_} or $error++
$self->{"collection"}{$_}
$error++
Truth table0%0/3
A or B
A= $self->{"collection"}{$_}B= $error++
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } @$gram ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | error "No such grammar: $_" if $error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | else { $gram = [$gram] } # hash or regex | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10783 | unshift @$gram, $$self{base_gram}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 10783 | my ($expr, $types); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 10783 | ($gram, $expr, $types) = $self->_prepare_gram($gram); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # use Data::Dumper; print STDERR Dumper $gram, $expr, $types; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 10783 | my $string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 10783 | ▶ | if (ref($input) eq 'ARRAY') { $string = shift @$input } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 10773 | else { ($string, $input) = ("$input", []) } # quotes in case of overload | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 10783 | ▶ | return unless length $string or @$input; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $string or @$input
MC/DC50%1/2
length $string or @$input
length $string
@$input
Truth table66%2/3
A or B
A= length $stringB= @$input
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 10782 | my ($block, @parts, @open, $i, $s_i); # $i counts splitted parts, $s_i the stack size | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 15293 | PARSE_TOKEN: | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | debug 'splitting string: '.$string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 15293 | my ($token, $type, $sign); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 15293 | ▶ | while ( !$token && $string =~ s{\A(.*?)($expr\z)}{}s ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $token and $string =~ s/\A(.*?)($expr\z)//s
MC/DC50%1/2
not $token and $string =~ s/\A(.*?)($expr\z)//s
not $token
$string =~ s/\A(.*?)($expr\z)//s
Truth table66%2/3
not(A) and B
A= not $tokenB= $string =~ s/\A(.*?)($expr\z)//s
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 19735 | ▶ | $block .= $1 if length $1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 19735 | $sign = $2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 19735 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 19735 | ▶ | ($_ eq $2) ? last : $i++ for ($3, $4, $5); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 19735 | $type = $$types[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 19735 | ▶ | last unless length $sign or length $string; # catch the \z | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $sign or length $string
MC/DC50%1/2
length $sign or length $string
length $sign
length $string
Truth table66%2/3
A or B
A= length $signB= length $string
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 11223 | ▶ | if ($type eq 'd_esc') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 9 | debug "block: ==>$block<== token: ==>$sign<== type: $type"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | $block .= $sign; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 9 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # fetch token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 11214 | my $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 11214 | my ($slice) = grep exists($$_{$type}), reverse @$gram; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 11214 | ▶ | if (ref($$slice{$type}[1]) eq 'ARRAY') { # for loop probably faster | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ($item) = map $$_[1], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 15483 | ▶ | grep {ref($$_[0]) ? ($sign =~ $$_[0]) : ($sign eq $$_[0])} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 5629 | @{$$slice{$type}[1]} | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 5585 | else { $item = $$slice{$type}[1]{$sign} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 11214 | debug "block: ==>$block<== token: ==>$sign<== type: $type item: $item"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 11214 | ▶ | $item = $sign if $item eq '_SELF'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 11214 | ▶ | if (exists $$slice{s_esc} and $1 =~ /$$slice{s_esc}$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $slice->{"s_esc"} and $1 =~ /$slice->{"s_esc"}$/
MC/DC50%1/2
exists $slice->{"s_esc"} and $1 =~ /$slice->{"s_esc"}$/
exists $slice->{"s_esc"}
$1 =~ /$slice->{"s_esc"}$/
Truth table66%2/3
A and B
A= exists $slice->{"s_esc"}B= $1 =~ /$slice->{"s_esc"}$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 154 | debug 'escaped token s_esc: '.$$slice{s_esc}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | $block =~ s/$$slice{s_esc}$// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 154 | ▶ | if $type eq 'tokens' and ! $$self{settings}{no_esc_rm}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "tokens" and not $self->{"settings"}{"no_esc_rm"}
MC/DC100%2/2
$type eq "tokens" and not $self->{"settings"}{"no_esc_rm"}
$type eq "tokens"
not $self->{"settings"}{"no_esc_rm"}
Truth table100%3/3
A and not(B)
A= $type eq "tokens"B= not $self->{"settings"}{"no_esc_rm"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 154 | $block .= $sign; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 154 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 11060 | ▶ | if ($type eq 'tokens') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 8355 | ▶ | unless ($s_i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 5627 | ▶ | if (ref $item) { # for $() matching tactics | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 25 | debug 'push stack (tokens)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 25 | push @$gram, $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 25 | $s_i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 25 | ($gram, $expr, $types) = $self->_prepare_gram($gram); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 25 | @open = ($sign, $type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 25 | $token = $$gram[-1]{token}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 5602 | else { $token = $item } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2728 | ▶ | if ($item eq '_POP') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2703 | $block .= $sign; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 2703 | debug "pop stack ($item)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2703 | pop @$gram; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 2703 | $s_i--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | elsif ($item eq '_CUT') { # for $() matching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 25 | $token = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 25 | debug "cut stack ($item)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 25 | splice @$gram, -$s_i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 25 | $s_i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | else { bug "what to do with $item !?" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 2728 | ($gram, $expr, $types) = $self->_prepare_gram($gram); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | else { # open nest or quote | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2705 | $block .= $sign; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 2705 | ▶ | unless (ref $item) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 2705 | ▶ | if ($item eq '_REC') { $item = {} } # recurs UGLY | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | else { # generate a grammar on the fly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2510 | ▶ | $item = ($type eq 'nests') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | ? { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | tokens => {$item => '_POP'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | nests => {$sign => '_REC'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } : { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | tokens => {$item => '_POP'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | quotes => {$sign => '_REC'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | nests => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # else if item is ref => item is grammar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 2705 | debug "push stack ($type)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 2705 | push @$gram, $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 2705 | $s_i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 2705 | ($gram, $expr, $types) = $self->_prepare_gram($gram); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 2705 | @open = ($sign, $type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 11060 | ▶ | last unless length $string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 15293 | ▶ | if (length $block) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 14646 | my $part = $block; # force copy | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 14646 | push @parts, \$part; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 15293 | ▶ | if ($token and $token ne '_CUT') { push @parts, $token } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$token and $token ne "_CUT"
MC/DC100%2/2
$token and $token ne "_CUT"
$token
$token ne "_CUT"
Truth table100%3/3
A and B
A= $tokenB= $token ne "_CUT"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 15293 | $block = $token = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 15293 | ▶ | if (($s_i or ++$i != $int) and length($string) || scalar(@$input)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%8/8
$s_i || ++$i != $int and length $string || scalar @$input
$s_i or ++$i != $int
length $string or scalar @$input
MC/DC100%3/3
$s_i || ++$i != $int and length $string || scalar @$input
$s_i
++$i != $int
length $string
Truth table100%5/5
A or B and C
A= $s_iB= ++$i != $intC= length $string
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 4511 | ▶ | $string = shift @$input unless length $string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 4511 | goto PARSE_TOKEN; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | elsif ($i == $int) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | my $part = join '', $string, @$input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | push @parts, \$part; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 10782 | ▶ | if ($s_i) { # broken | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | debug 'stack not empty'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | $open[1] =~ s/s$// ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $$self{broken} = "Unmatched $open[1] at end of input: $open[0]"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | ▶ | error $$self{broken} unless $$self{settings}{allow_broken}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | pop @$gram for 1 .. $s_i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 4701 | ▶ | return grep defined($_), map {ref($_) ? $$_ : $_} @parts | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 10781 | ▶ | if $$gram[-1]{was_regexp} && ! $$self{settings}{no_split_intel}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$gram->[-1]{"was_regexp"} and not $self->{"settings"}{"no_split_intel"}
MC/DC100%2/2
$gram->[-1]{"was_regexp"} and not $self->{"settings"}{"no_split_intel"}
$gram->[-1]{"was_regexp"}
not $self->{"settings"}{"no_split_intel"}
Truth table100%3/3
A and not(B)
A= $gram->[-1]{"was_regexp"}B= not $self->{"settings"}{"no_split_intel"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 9338 | return grep defined($_), @parts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 16241 | ▶ | sub _prepare_gram { # index immediatly here | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_gram: called CC=18 SCAR=28.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 16241 | my ($self, $gram) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 16241 | my %index; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 16241 | for my $ref (@$gram) { # prepare grammars for usage | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 36660 | ▶ | if (ref($ref) eq 'Regexp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1444 | $ref = {tokens => [[$ref, '_CUT']], was_regexp => 1}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | elsif (ref($ref) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | error 'Grammar has wrong data type: '.ref($ref)."\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 36660 | ▶ | unless ($$ref{prepared}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 4486 | ▶ | if (exists $$ref{esc}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | $$ref{s_esc} = ref($$ref{esc}) ? $$ref{esc} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 66 | ▶ | : quotemeta $$ref{esc}; # single esc regexp | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 66 | $$ref{d_esc} = '('.($$ref{s_esc}x2).')|'; # double esc regexp | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | ▶ | elsif (! exists $$ref{s_esc} and exists $index{s_esc}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
not exists $ref->{"s_esc"} and exists $index{"s_esc"}
MC/DC100%2/2
not exists $ref->{"s_esc"} and exists $index{"s_esc"}
not exists $ref->{"s_esc"}
exists $index{"s_esc"}
Truth table100%3/3
not(A) and B
A= not exists $ref->{"s_esc"}B= exists $index{"s_esc"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 4355 | $$ref{s_esc} = $index{s_esc}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 4486 | for (qw/tokens nests quotes/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 13458 | ▶ | next unless exists $$ref{$_}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | my $expr = (ref($$ref{$_}) eq 'ARRAY') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | ? join( '|', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 2686 | ▶ | ref($$_[0]) ? $$_[0] : quotemeta($$_[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 1631 | } @{$$ref{$_}} ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 8589 | ▶ | : join( '|', map { quotemeta($_) } keys %{$$ref{$_}} ) ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 8589 | ▶ | $expr = $expr ? '('.$expr.')|' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 8589 | $$ref{$_} = [$expr, $$ref{$_}]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 4486 | $$ref{prepared}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 36660 | $index{$_} = $$ref{$_}[0] for grep exists($$ref{$_}), qw/tokens nests quotes/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 36660 | $index{$_} = $$ref{$_} for grep exists($$ref{$_}), qw/s_esc d_esc/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 16241 | my ($expr, @types) = (''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 16241 | for (qw/d_esc tokens nests quotes/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 64964 | ▶ | next unless length $index{$_}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 63108 | push @types, $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 63108 | $expr .= $index{$_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 16241 | return $gram, $expr, \@types; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 65 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||