blib/lib/Pugs/Runtime/Regex.pm

filter: Statementstmt 42.3% 125 / 295 Branchbran 43.0% 43 / 100 Conditioncond 39.2% 22 / 56 MC/DCmcdc 25.0% 8 / 32 Subroutinesub 41.7% 33 / 79 Podpod 0.0% 0 / 29 totaltotal 39.0% 231 / 591 CC 76 SCAR 29.7
CC
76
Cov
42%
__ANON__
81.9
preprocess_hash
30.0
__ANON__
20.0
CRAP 19.5
« blib/lib/Pugs/Emitter/Rule/Perl5/Regex.pm ↑ Summary blib/lib/Pugs/Runtime/Match.pm »
1 package Pugs::Runtime::Regex;
2  
3 # documentation after __END__
4  
5 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶no warnings qw(recursion);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #use Smart::Comments; #for debugging, look also at Filtered-Comments.pm
10 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 36 ▶use Pugs::Runtime::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 36 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # note: alternation is first match (not longest).
15 # note: the list in @$nodes can be modified at runtime
16 18 ▶sub alternation {
Subroutine alternation: called CC=1 SCAR=0.0
Pod undocumented
17 18 my $nodes = shift;
18 106 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
19 106 ▶ my @state = $_[1] ? @{$_[1]} : ( 0, undef );
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 13 93 106
20 106 ▶ while ( $state[0] <= $#$nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state[0] <= $nodes->$#*) 137 6 143
21 #print "alternation $state[0] ",Dumper($nodes->[ $state[0] ]);
22 137 $nodes->[ $state[0] ]->( $_[0], $state[1], @_[2..7] );
23 137 ▶ last unless defined $_[3]; # test case ???
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[3] 0 137 137
24 137 $state[1] = $_[3]->state;
25 137 ▶ $state[0]++ unless $state[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $state[1] 124 13 137
26 137 ▶ if ( $_[3] || $_[3]->data->{abort} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3] or $_[3]->data->{"abort"}) 100 37 137
Condition66%2/3
$_[3] or $_[3]->data->{"abort"}
l!l&&r!l&&!r
100037
MC/DC50%1/2
$_[3] or $_[3]->data->{"abort"}
$_[3] $_[3]->data->{"abort"}
Truth table66%2/3
A or B
A= $_[3]B= $_[3]->data->{"abort"}
ABresult
1X1
011
000
27 100 ▶ $_[3]->data->{state} = $state[0] > $#$nodes
Branch: 1 branch
BranchTrueFalseTotal
$state[0] > $nodes->$#* ? : 34 66 100
28 ? undef
29 : \@state;
30 100 return;
31 }
32 }
33 6 $_[3] = failed()->(@_);
34 }
35 18 }
36  
37 59 ▶sub concat {
Subroutine concat: called CC=5 SCAR=18.2
Pod undocumented
38 59 my $nodes = shift;
39 59 ▶ $nodes = [ $nodes, @_ ] unless ref($nodes) eq 'ARRAY'; # backwards compat
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodes eq "ARRAY" 13 46 59
40 59 ▶ return null() if ! @$nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$nodes 1 58 59
41 58 ▶ return $nodes->[0] if @$nodes == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$nodes == 1 9 49 58
42 49 ▶ if ( @$nodes > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$nodes > 2) 4 45 49
43 4 return concat(
44 concat( [ $nodes->[0], $nodes->[1] ] ),
45 @$nodes[ 2 .. $#$nodes ],
46 );
47 }
48 101 ▶ return sub {
Subroutine __ANON__: called CC=27 SCAR=44.1
49 101 ▶ my @state = $_[1] ? @{$_[1]} : ( undef, undef );
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 11 90 101
50 #print "enter state ",Dumper(\@state);
51 101 my $m2;
52 101 my $redo_count = 0;
53 # XXX - workaround for t/regex/from_perl6_rules/capture.t test #38:
54 # regex single { o | k | e };
55 # # ...
56 # ok(!( "bokeper" ~~ m/(<?single>) ($0)/ ), 'Failed positional backref');
57  
58 do {
59  
60 114 ▶ my %param1 = defined $_[7] ? %{$_[7]} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $_[7] ? : 76 38 114
61 #print "concat 1: @{[ %param1 ]} \n";
62  
63 114 $nodes->[0]->( $_[0], $state[0], @_[2..7] );
64 return if ! $_[3]
65 114 ▶ || $_[3]->data->{abort};
Branch: 1 branch
BranchTrueFalseTotal
if not $_[3] or $_[3]->data->{"abort"} 24 90 114
Condition66%2/3
not $_[3] or $_[3]->data->{"abort"}
l!l&&r!l&&!r
24090
MC/DC50%1/2
not $_[3] or $_[3]->data->{"abort"}
not $_[3] $_[3]->data->{"abort"}
Truth table66%2/3
not(A) or B
A= not $_[3]B= $_[3]->data->{"abort"}
ABresult
1X1
011
000
66  
67 90 my $is_empty = ( $_[3]->from == $_[3]->to );
68 # && ( $param1{was_empty} )
69 # ; # fix a problem with '^'
70 90 ▶ if ( $is_empty && $param1{was_empty} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_empty and $param1{"was_empty"}) 0 90 90
Condition33%1/3
$is_empty and $param1{"was_empty"}
!ll&&!rl&&r
9000
MC/DC0%0/2
$is_empty and $param1{"was_empty"}
$is_empty $param1{"was_empty"}
Truth table33%1/3
A and B
A= $is_emptyB= $param1{"was_empty"}
ABresult
0X0
100
111
71 # # perl5 perlre says "the following match after a zero-length match
72 # is prohibited to have a length of zero"
73 0 ▶ return unless $_[3]->from == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[3]->from == 0 0 0 0
74 }
75  
76 90 ▶ my $param = { ( defined $_[7] ? %{$_[7]} : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $_[7] ? : 90 0 90
77 p => $_[3]->to,
78 was_empty => $is_empty,
79 };
80 # TODO - retry the second submatch only, until it fails
81 90 my $next_state = $_[3]->state;
82 #print "next_state ",Dumper($next_state);
83 #print "concat 2: "," \n";
84 90 $nodes->[1]->( $_[0], $state[1], $_[2], $m2,
85 $_[4], $_[3]->to, $_[6], $param );
86
87 #return if $is_empty && $m2->from == $m2->to;
88 90 $state[1] = $m2->state;
89 90 ▶ $state[0] = $next_state unless $state[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $state[1] 50 40 90
90 #print "concat 3: "," \n";
91 #print "return state ",Dumper(\@state);
92  
93 } while ! $m2
94 && ! $m2->data->{abort}
95 101 ▶ && defined $state[0]
Branch: 1 branch
BranchTrueFalseTotal
not $m2 || $m2->data->{"abort"} and defined $state[0] and $redo_count++ < 512 and do { do { my(%param1) = defined $_[7] ? %{$_[7];} : (); $nodes->[0]->($_[0], $state[0], @_[2..7]); return if not $_[3] or $_[3]->data->{"abort"}; my $is_empty = $_[3]->from == $_[3]->to; if ($is_empty and $param1{"was_empty"}) { return unless $_[3]->from == 0; } ; my $param = {defined $_[7] ? %{$_[7];} : (), "p", $_[3]->to, "was_empty", $is_empty}; my $next_state = $_[3]->state; $nodes->[1]->($_[0], $state[1], $_[2], $m2, $_[4], $_[3]->to, $_[6], $param); $state[1] = $m2->state; $state[0] = $next_state unless $state[1] } } 13 77 90
Condition77%7/9
not $m2 || $m2->data->{"abort"} and defined $state[0] and $redo_count++ < 512
!ll&&!rl&&r
77013
not $m2 || $m2->data->{"abort"} and defined $state[0]
!ll&&!rl&&r
76113
$m2 || $m2->data->{'abort'}
l!l&&r!l&&!r
76014
MC/DC50%2/4
not $m2 || $m2->data->{"abort"} and defined $state[0] and $redo_count++ < 512
$m2 $m2->data->{'abort'} defined $state[0] $redo_count++ < 512
Truth table60%3/5
not(A || B) and C and D
A= $m2B= $m2->data->{'abort'}C= defined $state[0]D= $redo_count++ < 512
ABCDresult
1XXX0
01XX0
000X0
00100
00111
96 && $redo_count++ < 512
97 ;
98  
99 # push capture data
100 # print "Concat positional: ", Dumper( $_[3]->data->{match}, $m2->data->{match} );
101 77 for ( 0 .. $#{ $m2 } ) {
102 0 ▶ if ( ref $m2->[$_] eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $m2->[$_] eq 'ARRAY') { } 0 0 0
elsif (defined $m2->[$_]) { } 0 0 0
103 # TODO - fully static count
104 # push @{ $_[3]->data->{match}[$_] }, @{ $m2->[$_] };
105 $_[3]->data->{match}[$_] = [
106 ( ref( $_[3]->data->{match}[$_] ) eq 'ARRAY'
107 0 ▶ ? @{ $_[3]->data->{match}[$_] }
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3]->data->{'match'}[$_] ? : 0 0 0
108 : defined( $_[3]->data->{match}[$_] )
109 ? $_[3]->data->{match}[$_]
110 : ()
111 ),
112 0 ▶ @{ $m2->[$_] },
Branch: 1 branch
BranchTrueFalseTotal
ref $_[3]->data->{'match'}[$_] eq 'ARRAY' ? : 0 0 0
113 ];
114 }
115 elsif ( defined $m2->[$_] ) {
116 0 $_[3]->data->{match}[$_] = $m2->[$_];
117 }
118 }
119 #print "Concat named: ", Dumper( $_[3]->data->{named}, $m2->data->{named} );
120 77 for ( keys %{$m2} ) {
121 0 ▶ if ( ref $m2->{$_} eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $m2->{$_} eq 'ARRAY') { } 0 0 0
elsif (defined $m2->{$_}) { } 0 0 0
122 # TODO - fully static count
123 #push @{ $_[3]->data->{named}{$_} }, @{ $m2->{$_} };
124 $_[3]->data->{named}{$_} = [
125 ( ref( $_[3]->data->{named}{$_} ) eq 'ARRAY'
126 0 ▶ ? @{ $_[3]->data->{named}{$_} }
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3]->data->{'named'}{$_} ? : 0 0 0
127 : defined( $_[3]->data->{named}{$_} )
128 ? $_[3]->data->{named}{$_}
129 : ()
130 ),
131 0 ▶ @{ $m2->{$_} },
Branch: 1 branch
BranchTrueFalseTotal
ref $_[3]->data->{'named'}{$_} eq 'ARRAY' ? : 0 0 0
132 ];
133 }
134 elsif ( defined $m2->{$_} ) {
135 0 $_[3]->data->{named}{$_} = $m2->{$_};
136 }
137 }
138 # /push capture data
139  
140 77 %{$_[3]->data} = (
141 77 ▶ %{$_[3]->data},
Branch: 1 branch
BranchTrueFalseTotal
defined $state[0] || defined $state[1] ? : 56 21 77
Condition66%4/6
$m2->data->{'capture'} || $_[3]->data->{'capture'}
l!l&&r!l&&!r
0077
defined $state[0] or defined $state[1]
l!l&&r!l&&!r
173921
MC/DC0%0/2
$m2->data->{'capture'} || $_[3]->data->{'capture'}
$m2->data->{'capture'} $_[3]->data->{'capture'}
MC/DC100%2/2
defined $state[0] or defined $state[1]
defined $state[0] defined $state[1]
Truth table33%1/3
A || B
A= $m2->data->{'capture'}B= $_[3]->data->{'capture'}
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= defined $state[0]B= defined $state[1]
ABresult
1X1
011
000
142 bool => \($m2->bool),
143 to => \($m2->to),
144 capture => $m2->data->{capture} || $_[3]->data->{capture},
145 abort => $m2->data->{abort},
146 77 state => ( defined $state[0] || defined $state[1]
147 ? \@state
148 : undef ),
149 );
150 }
151 45 }
152  
153 0 ▶sub try_method {
Subroutine try_method: not called CC=1 SCAR=1.7
Pod undocumented
154 0 my $method = shift;
155 0 my $param_list = shift; # XXX
156 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
157 # XXX method call must be inlined, due to inheritance problems
158 0 my $sub = 'sub {
159 my $bool = $_[0]->'.$method.'( '.$param_list.' ) ? 1 : 0;
160 $_[3] = Pugs::Runtime::Match->new({
161 bool => \$bool,
162 str => \$_[0],
163 from => \(0 + $_[5]),
164 to => \(0 + $_[5]),
165 named => {},
166 match => [],
167 });
168 }';
169 #print "sub: $sub\n";
170 0 return eval $sub;
171 }
172  
173  
174 0 ▶sub ignorecase {
Subroutine ignorecase: not called CC=1 SCAR=2.9
Pod undocumented
175 0 my $sub = shift;
176 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
177 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
178 0 ▶ my %param = ( ( defined $_[7] ? %{$_[7]} : () ), ignorecase => 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[7] ? : 0 0 0
179 0 $sub->( @_[0..6], \%param );
180 }
181 0 }
182  
183 32 ▶sub constant {
Subroutine constant: called CC=1 SCAR=0.0
Pod undocumented
184 32 my $const = shift;
185 32 my $lconst = length( $const );
186 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
187 157 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
188 my $bool = $_[7]{ignorecase}
189 157 ▶ ? lc( $const ) eq lc( substr( $_[0], $_[5], $lconst ) )
Branch: 1 branch
BranchTrueFalseTotal
$_[7]{'ignorecase'} ? : 0 157 157
190 : $const eq substr( $_[0], $_[5], $lconst );
191 157 $_[3] = Pugs::Runtime::Match->new({
192 bool => \$bool,
193 str => \$_[0],
194 from => \(0 + $_[5]),
195 to => \($_[5] + $lconst),
196 named => {},
197 match => [],
198 });
199 }
200 32 }
201  
202 0 ▶sub perl5 {
Subroutine perl5: not called CC=2 SCAR=15.2
Pod undocumented
203 0 my $rx;
204 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
205 {
206 0 local $@;
207 0 $rx = eval " use charnames ':full'; qr(^($_[0]))s ";
208 #print "regex perl5<< $_[0] >>\n";
209 0 ▶ print "Error in perl5 regex: << $_[0] >> \n$@\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
210 if $@;
211 #die "Error in perl5 regex: $_[0]"
212 # if $@;
213 }
214 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
215 #use charnames ':full';
216 0 my $bool;
217 eval {
218 $bool = $_[7]{ignorecase}
219 0 ▶ ? substr( $_[0], $_[5] ) =~ m/(?i)$rx/
Branch: 1 branch
BranchTrueFalseTotal
$_[7]{'ignorecase'} ? : 0 0 0
220 : substr( $_[0], $_[5] ) =~ m/$rx/;
221 0 $_[3] = Pugs::Runtime::Match->new({
222 bool => \$bool,
223 str => \$_[0],
224 from => \(0 + $_[5]),
225 to => \($_[5] + length $1),
226 named => {},
227 match => [],
228 });
229 0 1;
230 }
231 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $bool = $_[7]{"ignorecase"} ? substr($_[0], $_[5]) =~ /(?i)$rx/ : substr($_[0], $_[5]) =~ /$rx/; $_[3] = "Pugs::Runtime::Match"->new({"bool", \$bool, "str", \$_[0], "from", \(0 + $_[5]), "to", \($_[5] + length($1)), "named", {}, "match", []}); 1 } } or do { die "$@ in perl5 regex: /$rx/" } 0 0 0
232 0 die "$@ in perl5 regex: /$rx/";
233 };
234 0 $_[3];
235 0 };
236 }
237  
238 9 ▶sub null {
Subroutine null: called CC=1 SCAR=0.0
Pod undocumented
239 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
240 13 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
241 13 $_[3] = Pugs::Runtime::Match->new({
242 bool => \1,
243 str => \$_[0],
244 from => \(0 + $_[5]),
245 to => \(0 + $_[5]),
246 named => {},
247 match => [],
248 });
249 }
250 9 };
251  
252 7 ▶sub failed {
Subroutine failed: called CC=1 SCAR=0.0
Pod undocumented
253 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
254 7 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
255 7 $_[3] = Pugs::Runtime::Match->new({
256 bool => \0,
257 str => \$_[0],
258 from => \(0 + $_[5]),
259 to => \(0 + $_[5]),
260 named => {},
261 match => [],
262 state => undef,
263 });
264 }
265 7 };
266  
267 0 ▶sub failed_abort {
Subroutine failed_abort: not called CC=1 SCAR=0.6
Pod undocumented
268 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
269 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
270 0 $_[3] = Pugs::Runtime::Match->new({
271 bool => \0,
272 str => \$_[0],
273 from => \(0 + $_[5]),
274 to => \(0 + $_[5]),
275 named => {},
276 match => [],
277 abort => 1,
278 });
279 }
280 0 };
281  
282 0 ▶sub named {
Subroutine named: not called CC=1 SCAR=6.9
Pod undocumented
283 # return a named capture
284 0 my $label = shift;
285 0 my $capture_to_array = shift;
286 0 my $node = shift;
287 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
288 0 my $match;
289 0 $node->( @_[0,1,2], $match, @_[4,5,6,7] );
290 0 my %matches;
291 0 ▶ $matches{ $label } = $capture_to_array ? [ $match ] : $match;
Branch: 1 branch
BranchTrueFalseTotal
$capture_to_array ? : 0 0 0
292 $_[3] = Pugs::Runtime::Match->new({
293 bool => \( $match->bool ),
294 str => \$_[0],
295 from => \( $match->from ),
296 to => \( $match->to ),
297 named => \%matches,
298 match => [],
299 capture => $match->data->{capture},
300 0 state => $match->state,
301 });
302 }
303 0 }
304 0 ▶sub capture { named(@_) } # backwards compat
Subroutine capture: not called CC=1 SCAR=6.9
Pod undocumented
305  
306 0 ▶sub positional {
Subroutine positional: not called CC=1 SCAR=6.9
Pod undocumented
307 # return a positional capture
308 0 my $num = shift;
309 0 my $capture_to_array = shift;
310 0 my $node = shift;
311 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
312 0 my $match;
313 0 $node->( @_[0,1,2], $match, @_[4,5,6,7] );
314 0 my @matches;
315 0 ▶ $matches[ $num ] = $capture_to_array ? [ $match ] : $match;
Branch: 1 branch
BranchTrueFalseTotal
$capture_to_array ? : 0 0 0
316 $_[3] = Pugs::Runtime::Match->new({
317 bool => \( $match->bool ),
318 str => \$_[0],
319 from => \( $match->from ),
320 to => \( $match->to ),
321 named => {},
322 match => \@matches,
323 capture => $match->data->{capture},
324 0 state => $match->state,
325 });
326 }
327 0 }
328  
329 0 ▶sub capture_as_result {
Subroutine capture_as_result: not called CC=1 SCAR=6.9
Pod undocumented
330 # return a capture as the result object
331 0 my $node = shift;
332 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
333 0 my $match;
334 0 $node->( @_[0,1,2], $match, @_[4,5,6,7] );
335 $_[3] = Pugs::Runtime::Match->new({
336 bool => \( $match->bool ),
337 str => \$_[0],
338 from => \( $match->from ),
339 to => \( $match->to ),
340 named => {},
341 match => [],
342 capture => (
343 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
344 # print "Match: ", Dumper( $match );
345 0 '' . $match
346 }
347 0 ),
348 state => $match->state,
349 });
350 }
351 0 }
352  
353 0 ▶sub ___abort {
Subroutine ___abort: not called CC=1 SCAR=6.9
354 0 my $op = shift;
355 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 print "ABORTING\n";
357 0 $op->( @_ );
358 0 print "ABORT: [0] ",Dumper(@_); #$_[3]->perl;
359 0 $_[3]->data->{abort} = 1;
360 0 print "ABORT: ",$_[3]->perl;
361 0 };
362 };
363  
364 0 ▶sub ___fail {
Subroutine ___fail: not called CC=1 SCAR=6.9
365 0 my $op = shift;
366 return abort(
367 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 print "FAILING\n";
369 0 $op->( @_ );
370 0 $_[3]->data->{bool} = \0;
371 0 print "FAIL: ",Dumper( $_[3] );
372 }
373 0 );
374 };
375  
376 0 ▶sub before {
Subroutine before: not called CC=1 SCAR=6.9
Pod undocumented
377 0 my $op = shift;
378 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 0 my $match;
380 0 $op->( @_[0,1,2], $match, @_[4,5,6,7] );
381 0 $_[3] = Pugs::Runtime::Match->new({
382 bool => \( $match->bool ),
383 str => \$_[0],
384 from => \( $match->from ),
385 to => \( $match->from ),
386 named => {},
387 match => [],
388 state => $match->state,
389 });
390 0 };
391 }
392  
393 0 ▶sub at_start {
Subroutine at_start: not called CC=1 SCAR=0.6
Pod undocumented
394 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
395 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
396 0 $_[3] = Pugs::Runtime::Match->new({
397 bool => \( $_[5] == 0 ),
398 str => \$_[0],
399 from => \(0 + $_[5]),
400 to => \(0 + $_[5]),
401 named => {},
402 match => [],
403 abort => 0,
404 });
405 }
406 0 };
407  
408 0 ▶sub at_line_start {
Subroutine at_line_start: not called CC=1 SCAR=2.6
Pod undocumented
409 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
410 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
411 0 ▶ my $bool = $_[5] == 0
Condition0%0/3
$_[5] == 0 || substr($_[0], 0, $_[5]) =~ /\n$/s
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[5] == 0 || substr($_[0], 0, $_[5]) =~ /\n$/s
$_[5] == 0 substr($_[0], 0, $_[5]) =~ /\n$/s
Truth table0%0/3
A || B
A= $_[5] == 0B= substr($_[0], 0, $_[5]) =~ /\n$/s
ABresult
1X1
011
000
412 || substr( $_[0], 0, $_[5] ) =~ /\n$/s;
413 0 $_[3] = Pugs::Runtime::Match->new({
414 bool => \$bool,
415 str => \$_[0],
416 from => \(0 + $_[5]),
417 to => \(0 + $_[5]),
418 named => {},
419 match => [],
420 abort => 0,
421 });
422 }
423 0 };
424  
425 0 ▶sub at_line_end {
Subroutine at_line_end: not called CC=1 SCAR=2.6
Pod undocumented
426 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
427 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
428 0 ▶ my $bool = $_[5] >= length( $_[0] )
Condition0%0/3
$_[5] >= length $_[0] || substr($_[0], $_[5]) =~ /^\n/s
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[5] >= length $_[0] || substr($_[0], $_[5]) =~ /^\n/s
$_[5] >= length $_[0] substr($_[0], $_[5]) =~ /^\n/s
Truth table0%0/3
A || B
A= $_[5] >= length $_[0]B= substr($_[0], $_[5]) =~ /^\n/s
ABresult
1X1
011
000
429 || substr( $_[0], $_[5] ) =~ /^\n/s;
430 0 $_[3] = Pugs::Runtime::Match->new({
431 bool => \$bool,
432 str => \$_[0],
433 from => \(0 + $_[5]),
434 to => \(0 + $_[5]),
435 named => {},
436 match => [],
437 abort => 0,
438 });
439 }
440 0 };
441  
442 0 ▶sub at_end_of_string {
Subroutine at_end_of_string: not called CC=1 SCAR=0.6
Pod undocumented
443 36 ▶ no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
444 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
445 0 $_[3] = Pugs::Runtime::Match->new({
446 bool => \( $_[5] == length( $_[0] ) ),
447 str => \$_[0],
448 from => \(0 + $_[5]),
449 to => \(0 + $_[5]),
450 named => {},
451 match => [],
452 abort => 0,
453 });
454 }
455 0 };
456  
457 # experimental!
458 0 ▶sub negate {
Subroutine negate: not called CC=1 SCAR=6.9
Pod undocumented
459 0 my $op = shift;
460 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
461 #my $str = $_[0];
462 0 my $match = $op->( @_ );
463 0 my $bool = ! $match;
464  
465 0 $_[3] = Pugs::Runtime::Match->new({
466 bool => \( $bool ),
467 str => \$_[0],
468 from => \(0 + $_[5]),
469 to => \(0 + $_[5]),
470 named => {},
471 match => [],
472 abort => 0,
473 });
474  
475 0 };
476 };
477  
478 # ------- higher-order ruleops
479  
480 8 ▶sub optional {
Subroutine optional: called CC=1 SCAR=0.0
Pod undocumented
481 8 my $node = shift;
482 8 alternation( [ $node, null() ] );
483 }
484  
485 0 ▶sub null_or_optional {
Subroutine null_or_optional: not called CC=1 SCAR=6.9
Pod undocumented
486 0 my $node = shift;
487 0 alternation( [ null(), $node ] );
488 }
489  
490 2 ▶sub greedy_star {
Subroutine greedy_star: called CC=2 SCAR=7.6
Pod undocumented
491 2 ▶ greedy_plus( $_[0], $_[1] || 0, $_[2] )
Condition50%1/2
$_[1] || 0
l!l
02
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
492 }
493  
494 0 ▶sub non_greedy_star {
Subroutine non_greedy_star: not called CC=2 SCAR=17.9
Pod undocumented
495 0 ▶ non_greedy_plus( $_[0], $_[1] || 0, $_[2] )
Condition0%0/2
$_[1] || 0
l!l
00
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
496 }
497  
498 # XXX - needs optimization for faster backtracking, less stack usage
499 # TODO - run-time ranges (iterator)
500 5 ▶sub greedy_plus {
Subroutine greedy_plus: called CC=5 SCAR=16.6
Pod undocumented
501 5 my $node = shift;
502 5 ▶ my $min_count = defined( $_[0] ) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 2 5
503 5 my $max_count = $_[1];
504 5 ▶ if ( defined $max_count
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_count and $max_count < "1e+99") 0 5 5
Condition33%1/3
defined $max_count and $max_count < "1e+99"
!ll&&!rl&&r
500
MC/DC0%0/2
defined $max_count and $max_count < "1e+99"
defined $max_count $max_count < "1e+99"
Truth table33%1/3
A and B
A= defined $max_countB= $max_count < "1e+99"
ABresult
0X0
100
111
505 && $max_count < 1e99
506 ) {
507 0 return concat( [
508 ( $node ) x $min_count,
509 ( optional( $node ) ) x ($max_count - $min_count)
510 ] );
511 }
512 # $max_count == infinity
513 5 my $alt;
514 $alt = concat( [
515 $node,
516 5 ▶ optional( sub{ goto $alt } ),
Subroutine __ANON__: called CC=1 SCAR=0.0
517 ] );
518 5 ▶ return optional( $alt ) if $min_count < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min_count < 1 2 3 5
519 3 return concat( [ ( $node ) x ($min_count - 1), $alt ] );
520 }
521  
522 # XXX - needs optimization for faster backtracking, less stack usage
523 # TODO - run-time ranges (iterator)
524 6 ▶sub non_greedy_plus {
Subroutine non_greedy_plus: called CC=3 SCAR=11.0
Pod undocumented
525 6 my $node = shift;
526 6 ▶ my $min_count = defined( $_[0] ) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 3 6
527 6 ▶ my $max_count = $_[1] || 1e99;
Condition100%2/2
$_[1] || 1e+99
l!l
33
MC/DC100%1/1
$_[1] || 1e+99
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
528 21 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
529 21 ▶ my $state = $_[1]
Condition100%2/2
$_[1] || {'node', concat([() x $min_count]), 'count', $min_count}
l!l
912
MC/DC100%1/1
$_[1] || {'node', concat([() x $min_count]), 'count', $min_count}
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
530 || { node => concat( [ ( $node ) x $min_count ] ),
531 count => $min_count };
532 return failed()->(@_)
533 21 ▶ if $state->{count} > $max_count;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"count"} > $max_count 1 20 21
534 20 $state->{node}->( $_[0], undef, @_[2..7] );
535 $_[3]->data->{state} =
536 { node => concat( [ $node, $state->{node} ] ),
537 20 count => $state->{count} + 1 };
538 }
539 6 }
540  
541 0 ▶sub range {
Subroutine range: not called CC=1 SCAR=6.9
Pod undocumented
542 0 my $node = shift;
543 0 my $min_count = shift;
544 0 my $max_count = shift;
545 0 my $greedy = not shift;
546 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
547  
548 0 my $continuation = $_[1]; #XXX how do optional continuations work?
549  
550 # Forward declarations
551  
552 0 my $try_getting_more;
553  
554 my $default_behavior;
555 0 my $fallback_behavior;
556  
557 # Loop variables
558  
559 0 my $count = 0;
560 0 my $previous_pos = -1;
561  
562 # Loop 1 - getting to min_count
563  
564 0 my $continue_towards_min;
565 0 ▶ my $get_minimum = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
566 0 ▶ if ( $count < $min_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $min_count) { } 0 0 0
567 0 $count++;
568 0 goto &$continue_towards_min;
569 } else {
570 0 goto &$try_getting_more;
571 }
572 0 };
573 0 $continue_towards_min = concat( [ $node, $get_minimum ] );
574  
575 # Loop 2 - beyond the minimum
576  
577 0 ▶ $try_getting_more = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
578  
579 0 my $current_pos = $_[5];
580  
581 # (1) Stop when max_count is reached, or if pos does not move.
582  
583 0 ▶ if ( !( $count < $max_count ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($count < $max_count and $previous_pos < $current_pos) 0 0 0
Condition0%0/3
$count < $max_count and $previous_pos < $current_pos
!ll&&!rl&&r
000
MC/DC0%0/2
$count < $max_count and $previous_pos < $current_pos
$count < $max_count $previous_pos < $current_pos
Truth table0%0/3
A and B
A= $count < $max_countB= $previous_pos < $current_pos
ABresult
0X0
100
111
584 !( $previous_pos < $current_pos ) )
585 {
586 0 goto &$continuation;
587 }
588 0 $count++;
589 0 $previous_pos = $current_pos;
590  
591 # (2) Attempt the default behavior.
592  
593 # XXX - This section needs to be filled in.
594 # try $default_behavior
595 # if successful, return.
596 # if abort, do whatever is needed.
597 # if fail, we need to backtrack:
598 # undo any side-effects from trying the $default_behavior,
599 # so we can do the $fallback_behavior.
600  
601 # (3) Since the default behavior failed, do the fall-back beharvior.
602  
603 0 goto &$fallback_behavior;
604  
605 0 };
606 0 my $get_one_and_maybe_more = concat( [ $node, $try_getting_more ] );
607  
608 # Final preparations.
609  
610 0 ▶ if ( $greedy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($greedy) { } 0 0 0
611 0 $default_behavior = $get_one_and_maybe_more;
612 0 $fallback_behavior = $continuation;
613 } else { # non-greedy
614 0 $default_behavior = $continuation;
615 0 $fallback_behavior = $get_one_and_maybe_more;
616 }
617  
618 # Start.
619  
620 0 goto &$get_minimum;
621 0 };
622 }
623  
624  
625 0 ▶sub preprocess_hash {
Subroutine preprocess_hash: not called CC=5 SCAR=34.0
Pod undocumented
626 # TODO - move to Pugs::Runtime::Regex
627 0 my ( $h, $key ) = @_;
628 # returns AST depending on $h
629 #print "preprocess_hash: ", Dumper( \@_ );
630 0 ▶ if ( ref( $h->{$key} ) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h->{$key} eq "CODE") 0 0 0
631 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
632 0 my ( $str, $grammar, $args ) = @_;
633 #print "data: ", Dumper( \@_ );
634 0 my $ret = $h->{$key}->( @_ );
635 #print "ret: ", Dumper( $ret );
636
637 0 ▶ return $ret
Branch: 1 branch
BranchTrueFalseTotal
if ref $ret eq "Pugs::Runtime::Match" 0 0 0
638 if ref( $ret ) eq 'Pugs::Runtime::Match';
639
640 Pugs::Runtime::Match->new( {
641 bool => \1,
642 str => \$str,
643 from => \( 0 + ( $args->{p} || 0 ) ),
644 0 ▶ to => \( 0 + ( $args->{p} || 0 ) ),
Condition0%0/4
$args->{'p'} || 0
l!l
00
$args->{'p'} || 0
l!l
00
MC/DC0%0/1
$args->{'p'} || 0
$args->{'p'}
MC/DC0%0/1
$args->{'p'} || 0
$args->{'p'}
Truth table0%0/2
A
A= $args->{'p'}
Aresult
11
00
Truth table0%0/2
A
A= $args->{'p'}
Aresult
11
00
645 named => {},
646 match => [],
647 } ) }
648 0 }
649 0 ▶ if ( ref( $h->{$key} ) =~ /Pugs::Compiler::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($h->{$key}) =~ /Pugs::Compiler::/) 0 0 0
650 0 ▶ return sub { $h->{$key}->match( @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
651 }
652 # fail is number != 1
653 0 ▶ if ( $h->{$key} =~ /^(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{$key} =~ /^(\d+)$/) 0 0 0
654 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
655 0 my ( $str, $grammar, $args ) = @_;
656 Pugs::Runtime::Match->new( {
657 bool => \0,
658 str => \$str,
659 from => \( 0 + ( $args->{p} || 0 ) ),
660 0 ▶ to => \( 0 + ( $args->{p} || 0 ) ),
Condition0%0/4
$args->{'p'} || 0
l!l
00
$args->{'p'} || 0
l!l
00
MC/DC0%0/1
$args->{'p'} || 0
$args->{'p'}
MC/DC0%0/1
$args->{'p'} || 0
$args->{'p'}
Truth table0%0/2
A
A= $args->{'p'}
Aresult
11
00
Truth table0%0/2
A
A= $args->{'p'}
Aresult
11
00
661 named => {},
662 match => [],
663 0 ▶ } ) } unless $1 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 == 1 0 0 0
664 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
665 0 my ( $str, $grammar, $args ) = @_;
666 Pugs::Runtime::Match->new( {
667 bool => \1,
668 str => \$str,
669 from => \( 0 + ( $args->{p} || 0 ) ),
670 0 ▶ to => \( 0 + ( $args->{p} || 0 ) ),
Condition0%0/4
$args->{'p'} || 0
l!l
00
$args->{'p'} || 0
l!l
00
MC/DC0%0/1
$args->{'p'} || 0
$args->{'p'}
MC/DC0%0/1
$args->{'p'} || 0
$args->{'p'}
Truth table0%0/2
A
A= $args->{'p'}
Aresult
11
00
Truth table0%0/2
A
A= $args->{'p'}
Aresult
11
00
671 named => {},
672 match => [],
673 0 } ) };
674 }
675 # subrule
676 #print "compile: ",$h->{$key}, "\n";
677  
678 # XXX - compile to Token or to Regex ? (v6.pm needs Token)
679 0 my $r = Pugs::Compiler::Token->compile( $h->{$key} );
680 0 $h->{$key} = $r;
681 0 ▶ return sub { $r->match( @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
682 # return sub { warn "uncompiled subrule: $h->{$key} - not implemented " };
683 }
684  
685 # see commit #9783 for an alternate implementation
686 0 ▶sub hash {
Subroutine hash: not called CC=2 SCAR=17.9
Pod undocumented
687 0 my %hash = %{shift()};
688 #print "HASH: @{[ %hash ]}\n";
689 0 my @keys = sort {length $b <=> length $a } keys %hash;
690 #print "hash keys [ @keys ]\n";
691 0 for ( @keys ) {
692 0 my $h = preprocess_hash( \%hash, $_ );
693 0 my $key = $_;
694 $_ =
695 concat( [
696 constant( $key ),
697 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
698 # print "hash param: ",Dumper(\@_);
699 # TODO - add $<KEY> to $_[7]
700 0 $_[3] = $h->( $_[0], $_[4], $_[7], $_[1] );
701 # print "result: ",Dumper($_[3]);
702 }
703 0 ] );
704 }
705 0 return alternation( \@keys );
706 }
707  
708 # not a 'rule node'
709 # gets a variable from the user's pad
710 # this is used by the <$var> rule
711 0 ▶sub get_variable {
Subroutine get_variable: not called CC=4 SCAR=30.0
Pod undocumented
712 0 my $name = shift;
713
714 0 local $@;
715 0 my($idx, $pad) = 0;
716 0 ▶ while(eval { require PadWalker; $pad = PadWalker::peek_my($idx) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require PadWalker; $pad = PadWalker::peek_my($idx) } }) 0 0 0
717 $idx++, next
718 0 ▶ unless exists $pad->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pad->{$name} 0 0 0
719  
720 #print "NAME $name $pad->{$name}\n";
721 0 ▶ return ${ $pad->{$name} } if $name =~ /^\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\$/ 0 0 0
722 0 return $pad->{$name}; # arrayref/hashref
723 }
724 0 croak "Couldn't find '$name' in surrounding lexical scope.";
725 }
726  
727  
728 36 1;
729  
730 __END__
« blib/lib/Pugs/Emitter/Rule/Perl5/Regex.pm ↑ Summary blib/lib/Pugs/Runtime/Match.pm »