File Coverage

blib/lib/Parse/RandGen/Subrule.pm
Criterion Covered Total %
statement 38 52 73.0
branch 14 32 43.7
condition 6 9 66.6
subroutine 9 12 75.0
pod 4 7 57.1
total 71 112 63.3


line stmt bran cond sub pod time code
1             # $Revision: #4 $$Date: 2005/08/31 $$Author: jd150722 $
2             ######################################################################
3             #
4             # This program is Copyright 2003-2005 by Jeff Dutton.
5             #
6             # This program is free software; you can redistribute it and/or modify
7             # it under the terms of either the GNU General Public License or the
8             # Perl Artistic License.
9             #
10             # This program is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13             # GNU General Public License for more details.
14             #
15             # If you do not have a copy of the GNU General Public License write to
16             # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17             # MA 02139, USA.
18             ######################################################################
19              
20             package Parse::RandGen::Subrule;
21              
22             require 5.006_001;
23 4     4   22 use Carp;
  4         6  
  4         302  
24 4     4   24 use Parse::RandGen qw($Debug);
  4         6  
  4         425  
25 4     4   23 use strict;
  4         6  
  4         173  
26 4     4   20 use vars qw(@ISA $Debug);
  4         7  
  4         3198  
27             @ISA = ('Parse::RandGen::Condition');
28              
29 3     3   7 sub _newDerived { } # Nothing to do
30              
31 0     0 1 0 sub isSubrule { return 1; }
32 0     0 1 0 sub isTerminal { return 0; }
33 104     104 0 554 sub isQuantSupported { return 1; }
34              
35             sub subrule {
36 404 50   404 1 1630 my $self = shift or confess ("%Error: Cannot call without a valid object!");
37 404         1592 my $subrule = $self->element();
38 404 50       1193 if (ref($subrule)) {
39 404 50       1619 UNIVERSAL::isa($subrule, "Parse::RandGen::Rule") or confess("subrule() contains a reference ($subrule), but it is not a Rule!");
40 404         1658 return($subrule);
41             } else {
42 0 0       0 defined($self->grammar()) or confess ("%Error: Parse::RandGen::Subrule::subrule() called, but the \"$subrule\" rule cannot be found, because grammar() is undef!");
43 0         0 return($self->grammar()->rule($subrule));
44             }
45             }
46              
47             sub dump {
48 0 0   0 0 0 my $self = shift or confess ("%Error: Cannot call without a valid object!");
49 0         0 my $subrule = $self->element();
50 0 0       0 if (ref($subrule)) {
51 0         0 return ($subrule->dumpHeir() . $self->quant());
52             } else {
53             # Named non-reference rule
54 0         0 my $output = $subrule;
55 0 0 0     0 if (defined($self->max()) && ($self->min() == $self->max())) {
56 0 0       0 $output .= "(" . $self->min() . ")" unless ($self->min() == 1);
57             } else {
58 0 0       0 my $max = defined($self->max()) ? $self->max() : "";
59 0         0 $output .= "(" . $self->min() . ".." . $max . ")";
60             }
61 0         0 return $output;
62             }
63             }
64              
65             sub pick {
66 202 50   202 1 504 my $self = shift or confess ("%Error: Cannot call without a valid object!");
67 202         793 my %args = ( match=>1, # Default is to pick matching data
68             @_ );
69 202         392 my $rule = $self->subrule();
70 202         519 my $ruleName = $self->element();
71 202 50       992 defined($rule) or confess("Subrule::pick(): $ruleName subrule cannot be found in the grammar!\n");
72              
73 202         757 my %result = $self->pickRepetitions(%args);
74 202         408 my $matchCnt = $result{matchCnt};
75 202         269 my $badOne = $result{badOne};
76              
77 202         938 my $val = "";
78 202         802 for (my $i=0; $i<$matchCnt; $i++) {
79 603 100 100     2651 my $matchThis = (defined($badOne) && ($i==$badOne))?0:1; # Only don't match for corrupted data
80 603 100 100     3906 my $specifiedVals = ($matchThis && ($i==($matchCnt-1))) ? $args{vals} : { }; # Only specify rules for last capture value
81 603         3258 $val .= $rule->pick(%args, match=>$matchThis, vals => $specifiedVals);
82             }
83 202 100       753 if ($Debug) {
84 200 100       1763 print("Parse::RandGen::Subrule::pick(match=>$args{match}, matchCnt=>$matchCnt, badOne=>".(defined($badOne)?$badOne:"undef")
85             .") on the rule \"".$rule->dumpHeir()."\" has a value of ".$self->dumpVal($val)."\n");
86             }
87 202         17087 return ($val);
88             }
89              
90             # Returns true (1) if this subrule contains any of the rules specified by the "vals" argument
91             sub containsVals {
92 202 50   202 0 575 my $self = shift or confess("%Error: Cannot call without a valid object!");
93 202         1118 my %args = ( vals => { }, # Hash of values of various hard-coded sub-rules (by name)
94             @_ );
95 202         480 return $self->subrule()->containsVals(%args);
96             }
97              
98             ######################################################################
99             #### Package return
100             1;
101             __END__