blib/lib/Zoidberg/StringParser.pm

filter: Statementstmt 95.8% 139 / 145 Branchbran 88.0% 81 / 92 Conditioncond 80.4% 33 / 41 MC/DCmcdc 70.8% 17 / 24 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 89.6% 278 / 310 CC 69 SCAR 36.6
CC
69
Cov
91%
split
51.7
_prepare_gram
18.1
new
4.0
CRAP 38.9
« blib/lib/Zoidberg/Contractor.pm ↑ Summary blib/lib/Zoidberg/Utils/Error.pm »
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() || {}
l!l
680
shift() || {}
l!l
671
shift() || {}
l!l
644
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()
Aresult
11
00
Truth table100%2/2
A
A= shift()
Aresult
11
00
Truth table100%2/2
A
A= shift()
Aresult
11
00
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
BranchTrueFalseTotal
if (ref $gram) { } 3 10780 10783
if (do { ref $gram eq 'ARRAY' }) { } 0 3 3
45 10780 ▶ error "No such grammar: $gram" unless $$self{collection}{$gram};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"collection"}{$gram} 0 10780 10780
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
BranchTrueFalseTotal
ref $_ ? : 0 0 0
Condition0%0/3
$self->{"collection"}{$_} or $error++
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"collection"}{$_} or $error++
$self->{"collection"}{$_} $error++
Truth table0%0/3
A or B
A= $self->{"collection"}{$_}B= $error++
ABresult
1X1
011
000
52 } @$gram ];
53 0 ▶ error "No such grammar: $_" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
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
BranchTrueFalseTotal
if (ref $input eq 'ARRAY') { } 10 10773 10783
64 10773 else { ($string, $input) = ("$input", []) } # quotes in case of overload
65  
66 10783 ▶ return unless length $string or @$input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string or @$input 1 10782 10783
Condition66%2/3
length $string or @$input
l!l&&r!l&&!r
1078201
MC/DC50%1/2
length $string or @$input
length $string @$input
Truth table66%2/3
A or B
A= length $stringB= @$input
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (not $token and $string =~ s/\A(.*?)($expr\z)//s) 19735 4501 24236
Condition66%2/3
not $token and $string =~ s/\A(.*?)($expr\z)//s
!ll&&!rl&&r
4501019735
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
ABresult
0X0
100
111
75 19735 ▶ $block .= $1 if length $1;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 16942 2793 19735
76 19735 $sign = $2;
77  
78 19735 my $i = 0;
79 19735 ▶ ($_ eq $2) ? last : $i++ for ($3, $4, $5);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $2 ? : 18099 15626 33725
80 19735 $type = $$types[$i];
81  
82 19735 ▶ last unless length $sign or length $string; # catch the \z
Branch: 1 branch
BranchTrueFalseTotal
unless length $sign or length $string 8512 11223 19735
Condition66%2/3
length $sign or length $string
l!l&&r!l&&!r
1122308512
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
ABresult
1X1
011
000
83  
84 11223 ▶ if ($type eq 'd_esc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "d_esc") 9 11214 11223
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
BranchTrueFalseTotal
if (ref $slice->{$type}[1] eq 'ARRAY') { } 5629 5585 11214
94 ($item) = map $$_[1],
95 15483 ▶ grep {ref($$_[0]) ? ($sign =~ $$_[0]) : ($sign eq $$_[0])}
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[0] ? : 5833 9650 15483
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
BranchTrueFalseTotal
if $item eq "_SELF" 207 11007 11214
101  
102 11214 ▶ if (exists $$slice{s_esc} and $1 =~ /$$slice{s_esc}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $slice->{"s_esc"} and $1 =~ /$slice->{"s_esc"}$/) 154 11060 11214
Condition66%2/3
exists $slice->{"s_esc"} and $1 =~ /$slice->{"s_esc"}$/
!ll&&!rl&&r
011060154
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"}$/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $type eq "tokens" and not $self->{"settings"}{"no_esc_rm"} 1 153 154
Condition100%3/3
$type eq "tokens" and not $self->{"settings"}{"no_esc_rm"}
!ll&&!rl&&r
71821
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"}
ABresult
0X0
100
111
106 154 $block .= $sign;
107 154 next;
108 }
109  
110 11060 ▶ if ($type eq 'tokens') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'tokens') { } 8355 2705 11060
111 8355 ▶ unless ($s_i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_i) { } 2728 5627 8355
112 5627 ▶ if (ref $item) { # for $() matching tactics
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 25 5602 5627
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
BranchTrueFalseTotal
if ($item eq '_POP') { } 2703 25 2728
elsif ($item eq '_CUT') { } 25 0 25
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
BranchTrueFalseTotal
unless (ref $item) 2705 0 2705
142 2705 ▶ if ($item eq '_REC') { $item = {} } # recurs UGLY
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq '_REC') { } 195 2510 2705
143 else { # generate a grammar on the fly
144 2510 ▶ $item = ($type eq 'nests')
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'nests' ? : 909 1601 2510
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
BranchTrueFalseTotal
unless length $string 2280 8780 11060
163 }
164  
165 15293 ▶ if (length $block) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $block) 14646 647 15293
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
BranchTrueFalseTotal
if ($token and $token ne "_CUT") 1548 13745 15293
Condition100%3/3
$token and $token ne "_CUT"
!ll&&!rl&&r
964141041548
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"
ABresult
0X0
100
111
170 15293 $block = $token = undef;
171  
172 15293 ▶ if (($s_i or ++$i != $int) and length($string) || scalar(@$input)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s_i || ++$i != $int and length $string || scalar @$input) { } 4511 10782 15293
elsif ($i == $int) { } 1 10781 10782
Condition100%8/8
$s_i || ++$i != $int and length $string || scalar @$input
!ll&&!rl&&r
1107814511
$s_i or ++$i != $int
l!l&&r!l&&!r
27152651
length $string or scalar @$input
l!l
450010792
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
ABCresult
00X0
1X00
0100
1X11
0111
173 4511 ▶ $string = shift @$input unless length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 11 4500 4511
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
BranchTrueFalseTotal
if ($s_i) 2 10780 10782
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
BranchTrueFalseTotal
unless $self->{"settings"}{"allow_broken"} 1 1 2
186 1 pop @$gram for 1 .. $s_i;
187 }
188  
189 4701 ▶ return grep defined($_), map {ref($_) ? $$_ : $_} @parts
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 4701 0 4701
190 10781 ▶ if $$gram[-1]{was_regexp} && ! $$self{settings}{no_split_intel};
Branch: 1 branch
BranchTrueFalseTotal
if $gram->[-1]{"was_regexp"} and not $self->{"settings"}{"no_split_intel"} 1443 9338 10781
Condition100%3/3
$gram->[-1]{"was_regexp"} and not $self->{"settings"}{"no_split_intel"}
!ll&&!rl&&r
933711443
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (ref $ref eq 'Regexp') { } 1444 35216 36660
elsif (ref $ref ne 'HASH') { } 0 35216 35216
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
BranchTrueFalseTotal
unless ($ref->{"prepared"}) 4486 32174 36660
206 4486 ▶ if (exists $$ref{esc}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ref->{'esc'}) { } 66 4420 4486
elsif (not exists $ref->{'s_esc'} and exists $index{'s_esc'}) { } 4355 127 4482
207 $$ref{s_esc} = ref($$ref{esc}) ? $$ref{esc}
208 66 ▶ : quotemeta $$ref{esc}; # single esc regexp
Branch: 1 branch
BranchTrueFalseTotal
ref $ref->{'esc'} ? : 0 66 66
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"}
!ll&&!rl&&r
6234355
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless exists $ref->{$_} 4869 8589 13458
217 my $expr = (ref($$ref{$_}) eq 'ARRAY')
218 ? join( '|', map {
219 2686 ▶ ref($$_[0]) ? $$_[0] : quotemeta($$_[0])
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[0] ? : 1694 992 2686
220 1631 } @{$$ref{$_}} )
221 8589 ▶ : join( '|', map { quotemeta($_) } keys %{$$ref{$_}} ) ;
Branch: 1 branch
BranchTrueFalseTotal
ref $ref->{$_} eq 'ARRAY' ? : 1631 6958 8589
222 8589 ▶ $expr = $expr ? '('.$expr.')|' : '';
Branch: 1 branch
BranchTrueFalseTotal
$expr ? : 6926 1663 8589
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
BranchTrueFalseTotal
unless length $index{$_} 1856 63108 64964
235 63108 push @types, $_;
236 63108 $expr .= $index{$_};
237 }
238 16241 return $gram, $expr, \@types;
239 }
240  
241 65 1;
242  
243 __END__
« blib/lib/Zoidberg/Contractor.pm ↑ Summary blib/lib/Zoidberg/Utils/Error.pm »