File Coverage

blib/lib/Array/Stream/Transactional/Matcher/Flow.pm
Criterion Covered Total %
statement 53 53 100.0
branch 19 20 95.0
condition 4 6 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 84 87 96.5


line stmt bran cond sub pod time code
1             # $Id: Flow.pm,v 1.14 2004/06/11 20:29:32 claes Exp $
2              
3 5     5   35 use strict;
  5         10  
  5         298  
4              
5             our $VERSION = "1.00";
6              
7             package Array::Stream::Transactional::Matcher::Flow;
8 5     5   26 use Carp qw(croak confess);
  5         10  
  5         1726  
9              
10             our @ISA = qw(Array::Stream::Transactional::Matcher::Rule);
11              
12             sub new {
13 13     13 1 52 my ($class, @args) = @_;
14 13   33     52 $class = ref $class || $class;
15            
16 13 50       35 croak "Can't instansiate abstract class Array::Stream::Transactional::Matcher::Flow" if($class eq "Array::Stream::Transactional::Matcher::Flow");
17            
18 13         48 my $self = bless [@args], $class;
19 13         113 return $self;
20             }
21              
22             package Array::Stream::Transactional::Matcher::Flow::sequence;
23             our @ISA = qw(Array::Stream::Transactional::Matcher::Flow);
24              
25             sub match {
26 46     46   72 my ($self, $stream, @passthru) = @_;
27            
28 46         127 $stream->commit;
29            
30 46         358 my @rules = @$self;
31 46         54 my $match = 0;
32 46         106 TEST: while(defined (my $rule = shift @rules)) {
33 62         274 $match = $rule->match($stream, @passthru);
34 62 100       135 unless($match) {
35 38         111 $stream->rollback;
36 38         412 return 0;
37             }
38 24 100       54 last TEST unless(@rules);
39 16 100       67 $stream->next if($match > 0);
40             }
41            
42 8         26 $stream->regret;
43 8         111 return $match;
44             }
45              
46              
47             package Array::Stream::Transactional::Matcher::Flow::repetition;
48 5     5   28 use Carp qw(croak);
  5         9  
  5         1912  
49             our @ISA = qw(Array::Stream::Transactional::Matcher::Flow);
50              
51             sub match {
52 37     37   53 my ($self, $stream, @passthru) = @_;
53              
54 37         91 $stream->commit;
55              
56 37         314 my ($rule, $min, $max) = @$self;
57              
58             # Take care of 0 as minmum
59 37 100       104 unless($rule->match($stream, @passthru)) {
60 27         62 $stream->rollback;
61 27 100       268 if($min == 0) {
62 3         8 return -1;
63             }
64              
65 24         70 return 0;
66             }
67              
68 10         15 my $match = 0;
69 10         20 my $failure = 0;
70             # Run while we have items in the stream
71 10         30 TEST: while($stream->has_more) {
72 23 100       237 if($rule->match($stream, @passthru)) {
73 17         18 $match++;
74 17 100 100     68 if(defined $max && $match == $max) {
75 2         5 last TEST;
76             }
77 15         37 $stream->next;
78             } else {
79 6         9 $failure = 1;
80 6         12 last TEST;
81             }
82             }
83            
84 10 100       44 if($match >= $min) {
85 9         24 $stream->regret;
86 9 100       91 return $failure ? -1 : 1;
87             }
88              
89             # Report failure
90 1         3 $stream->rollback;
91 1         25 return 0;
92             }
93              
94             package Array::Stream::Transactional::Matcher::Flow::optional;
95             our @ISA = qw(Array::Stream::Transactional::Matcher::Flow::repetition);
96              
97             sub new {
98 1     1   3 my $class = shift;
99 1         53 my $self = $class->SUPER::new(@_, 0, 1);
100 1         6 return $self;
101             }
102              
103             1;
104             __END__