RandGen/Grammar.pm

filter: Statementstmt 42.2% 38 / 90 Branchbran 18.1% 12 / 66 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.3% 8 / 15 Podpod 50.0% 5 / 10 totaltotal 32.0% 66 / 206 CC 42 SCAR 35.1
CC
42
Cov
31%
addExamples
110.0
rule
42.0
dump
30.0
CRAP 33.5
« RandGen/Regexp.pm ↑ Summary RandGen/CharClass.pm »
1 # $Revision: #3 $$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::Grammar;
21  
22 4 require 5.006_001;
23 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Parse::RandGen qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use vars qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 ######################################################################
30 #### Creators
31  
32 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
33 1 my $class = shift;
34 1 my $self = {
35 _name => undef, # Name of the grammar
36 _rules => { }, # Rules of the grammar
37 _examples => { }, # Examples for various rules in the grammar
38 #@_,
39 };
40 1 ▶ bless $self, ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
41  
42 1 ▶ $self->{_name} = shift or confess("%Error: Cannot call new without a name for the new grammer (only required argument)!");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_name"} = shift() or confess("%Error: Cannot call new without a name for the new grammer (only required argument)!") 1 0 1
43 1 return($self);
44 }
45  
46 ######################################################################
47 #### Methods
48  
49 # Add Rules to the Grammar
50 1 ▶sub addRule {
Subroutine addRule: called CC=7 SCAR=21.8
Pod undocumented
51 1 my $expType = "Parse::RandGen::Rule";
52 1 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 1 0 1
53 1 ▶ my $rule = shift or confess("%Error: addRule takes a required $expType object!");
Branch: 1 branch
BranchTrueFalseTotal
my $rule = shift() or confess("%Error: addRule takes a required $expType object!") 1 0 1
54 1 ▶ confess("%Error: Passed a ".ref($rule)." argument instead of a $expType reference argument!") unless (ref($rule) eq $expType);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq $expType 0 1 1
55 1 ▶ confess("%Error: Overwriting the existing rule for ", $rule->name(), "!") if exists($self->{_rules}{$rule->name()});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_rules"}{$rule->name} 0 1 1
56 1 ▶ confess("%Error: Passed a Rule that already belongs to a different Grammar object!\n") if (defined($rule->grammar()) && ($rule->grammar() != $self));
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->grammar and $rule->grammar != $self 0 1 1
Condition33%1/3
defined $rule->grammar and $rule->grammar != $self
!ll&&!rl&&r
100
MC/DC0%0/2
defined $rule->grammar and $rule->grammar != $self
defined $rule->grammar $rule->grammar != $self
Truth table33%1/3
A and B
A= defined $rule->grammarB= $rule->grammar != $self
ABresult
0X0
100
111
57 1 $self->{_rules}{$rule->name()} = $rule; # Save the rule in the _rule hash
58 1 $rule->{_grammar} = $self; # Set the rule's grammar to self
59 }
60  
61 # Add examples for a particular Rule to the Grammar
62 0 ▶sub addExamples {
Subroutine addExamples: not called CC=10 SCAR=47.0
Pod undocumented
63 0 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 0 0 0
64 0 ▶ my $ruleName = shift or confess("%Error: Cannot call without a rule name!");
Branch: 1 branch
BranchTrueFalseTotal
my $ruleName = shift() or confess("%Error: Cannot call without a rule name!") 0 0 0
65 0 ▶ (ref($ruleName) eq "") or confess("%Error: Argument given for a rule name is actually a ".ref($ruleName)." reference!");
Branch: 1 branch
BranchTrueFalseTotal
ref $ruleName eq "" or confess('%Error: Argument given for a rule name is actually a ' . ref($ruleName) . ' reference!') 0 0 0
66 0 ▶ ($self->rule($ruleName)) or confess("%Error: Cannot find the $ruleName rule on this grammar!");
Branch: 1 branch
BranchTrueFalseTotal
$self->rule($ruleName) or confess("%Error: Cannot find the $ruleName rule on this grammar!") 0 0 0
67 0 my @examples = @_;
68  
69 0 ▶ if (!defined($self->{_examples}{$ruleName})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_examples"}{$ruleName}) 0 0 0
70 0 $self->{_examples}{$ruleName} = [ ]; # List of examples for the given rule
71 }
72 0 my $exList = $self->{_examples}{$ruleName};
73 0 foreach my $example (@examples) {
74 0 ▶ (ref($example) eq "HASH") or confess("%Error: Example argument should be a HASH reference with \"stat\" and \"val\" entries, but is actually a ".ref($example)." reference!");
Branch: 1 branch
BranchTrueFalseTotal
ref $example eq "HASH" or confess('%Error: Example argument should be a HASH reference with "stat" and "val" entries, but is actually a ' . ref($example) . ' reference!') 0 0 0
75 0 ▶ (defined($example->{stat}) && defined($example->{val})) or confess("%Error: Example hash does not contain both \"stat\" and \"val\" entries!");
Branch: 1 branch
BranchTrueFalseTotal
defined $example->{"stat"} and defined $example->{"val"} or confess("%Error: Example hash does not contain both \"stat\" and \"val\" entries!") 0 0 0
Condition0%0/3
defined $example->{"stat"} and defined $example->{"val"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $example->{"stat"} and defined $example->{"val"}
defined $example->{"stat"} defined $example->{"val"}
Truth table0%0/3
A and B
A= defined $example->{"stat"}B= defined $example->{"val"}
ABresult
0X0
100
111
76 0 push @$exList, $example;
77 }
78 }
79  
80 # Check the Grammar for completeness/errors
81 0 ▶sub check {
Subroutine check: not called CC=3 SCAR=24.8
Pod undocumented
82 0 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 0 0 0
83 0 my $grammarName = $self->name();
84  
85 0 my $err = "";
86 0 foreach my $ruleName (keys %{$self->{_rules}}) {
87 0 my $rule = $self->rule($ruleName);
88 0 $err .= $rule->check();
89 }
90 0 return $err;
91 }
92  
93 # Dump the Grammar
94 0 ▶sub dump {
Subroutine dump: not called CC=5 SCAR=34.0
Pod documented
95 0 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 0 0 0
96 0 my $output = "";
97 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::RandGen::Grammar::Debug) { } 0 0 0
98 0 my $d = Data::Dumper->new([$self]);
99 0 $d->Terse(1);
100 0 $output .= $self->name() . " = " . $d->Dump();
101 } else {
102 0 $output .= "#" . $self->name() . " Grammar specification:\n";
103 #$output .= "<autotree>\n";
104 0 my @ruleNames = sort keys %{$self->{_rules}};
105 0 foreach my $ruleName (@ruleNames) {
106 0 $output .= $self->rule($ruleName)->dump();
107 }
108 0 ▶ $output .= "# No rules defined...\n" if ($#ruleNames < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#ruleNames < 0 0 0 0
109 }
110 0 return $output;
111 }
112  
113 ######################################################################
114 #### Accessors
115  
116 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
117 0 ▶ my $self = shift or confess("%Error: Cannot call name() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call name() without a valid object!") 0 0 0
118 0 return $self->{_name};
119 }
120  
121 0 ▶sub rule { # Access the named rule (no side effects: undef is returned if the rule is not found)
Subroutine rule: not called CC=6 SCAR=37.4
Pod documented
122 0 ▶ my $self = shift or confess("%Error: Cannot call rule() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call rule() without a valid object!") 0 0 0
123 0 ▶ my $name = shift or confess("%Error: Cannot call rule() without the name of the Rule to find!");
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or confess("%Error: Cannot call rule() without the name of the Rule to find!") 0 0 0
124 0 ▶ if (exists($self->{_rules}{$name}) && !defined($self->{_rules}{$name})) { die "Grammar has a rule \"$name\", which references an undefined Rule object!\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}) 0 0 0
Condition0%0/3
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}
exists $self->{"_rules"}{$name} not defined $self->{"_rules"}{$name}
Truth table0%0/3
A and not(B)
A= exists $self->{"_rules"}{$name}B= not defined $self->{"_rules"}{$name}
ABresult
0X0
100
111
125 0 ▶ my $rule = $self->{_rules}{$name} if exists($self->{_rules}{$name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_rules"}{$name} 0 0 0
126 0 return $rule;
127 }
128  
129 1 ▶sub defineRule { # Access the named rule (if it does not exist, create the rule)
Subroutine defineRule: called CC=8 SCAR=23.7
Pod documented
130 1 ▶ my $self = shift or confess("%Error: Cannot call defineRule() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call defineRule() without a valid object!") 1 0 1
131 1 ▶ my $name = shift or confess("%Error: Cannot call defineRule() without the name of the Rule to find!");
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or confess("%Error: Cannot call defineRule() without the name of the Rule to find!") 1 0 1
132 1 ▶ exists($self->{_rules}{$name}) and not defined($self->{_rules}{$name}) and die ($self->name() . " Grammar has a rule \"$name\", which references an undefined Rule object!\n");
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name} and die $self->name . qq[ Grammar has a rule "$name", which references an undefined Rule object!\n] 0 1 1
Condition33%1/3
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}
exists $self->{"_rules"}{$name} not defined $self->{"_rules"}{$name}
Truth table33%1/3
A and not(B)
A= exists $self->{"_rules"}{$name}B= not defined $self->{"_rules"}{$name}
ABresult
0X0
100
111
133 1 ▶ exists($self->{_rules}{$name}) and confess($self->name() . "Grammar already has a definition for the \"$name\" rule!\n");
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"_rules"}{$name} and confess($self->name . qq[Grammar already has a definition for the "$name" rule!\n]) 0 1 1
134 1 ▶ if (!exists($self->{_rules}{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_rules"}{$name}) 1 0 1
135 1 $self->addRule(Parse::RandGen::Rule->new($name));
136 }
137 1 ▶ my $rule = $self->{_rules}{$name} or die "%Error: Failed to create the \"$name\" rule!";
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $self->{"_rules"}{$name} or die qq[%Error: Failed to create the "$name" rule!] 1 0 1
138 1 return $rule;
139 }
140  
141 0 ▶sub ruleNames {
Subroutine ruleNames: not called CC=2 SCAR=17.9
Pod undocumented
142 0 ▶ my $self = shift or confess("%Error: Cannot call rules() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call rules() without a valid object!") 0 0 0
143 0 return (sort keys %{$self->{_rules}});
144 }
145  
146 0 ▶sub examples {
Subroutine examples: not called CC=5 SCAR=34.0
Pod undocumented
147 0 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 0 0 0
148 0 ▶ my $ruleName = shift or confess("%Error: Cannot call without a rule name!");
Branch: 1 branch
BranchTrueFalseTotal
my $ruleName = shift() or confess("%Error: Cannot call without a rule name!") 0 0 0
149 0 ▶ ($self->rule($ruleName)) or confess("%Error: Cannot find the $ruleName rule on this grammar!");
Branch: 1 branch
BranchTrueFalseTotal
$self->rule($ruleName) or confess("%Error: Cannot find the $ruleName rule on this grammar!") 0 0 0
150 0 my @examples = ( );
151  
152 0 ▶ if (defined($self->{_examples}{$ruleName})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_examples"}{$ruleName}) 0 0 0
153 0 @examples = @{$self->{_examples}{$ruleName}};
154 }
155  
156 0 return @examples;
157 }
158  
159 ######################################################################
160 #### Package return
161 4 1;
162 __END__
« RandGen/Regexp.pm ↑ Summary RandGen/CharClass.pm »