| 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
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my $rule = shift or confess("%Error: addRule takes a required $expType object!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | confess("%Error: Passed a ".ref($rule)." argument instead of a $expType reference argument!") unless (ref($rule) eq $expType); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | confess("%Error: Overwriting the existing rule for ", $rule->name(), "!") if exists($self->{_rules}{$rule->name()}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $rule->grammar and $rule->grammar != $self
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $ruleName = shift or confess("%Error: Cannot call without a rule name!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | (ref($ruleName) eq "") or confess("%Error: Argument given for a rule name is actually a ".ref($ruleName)." reference!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | ($self->rule($ruleName)) or confess("%Error: Cannot find the $ruleName rule on this grammar!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | my @examples = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (!defined($self->{_examples}{$ruleName})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | (defined($example->{stat}) && defined($example->{val})) or confess("%Error: Example hash does not contain both \"stat\" and \"val\" entries!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $example->{"stat"} and defined $example->{"val"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | 0 | my $output = ""; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($Debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $name = shift or confess("%Error: Cannot call rule() without the name of the Rule to find!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}
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}
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $rule = $self->{_rules}{$name} if exists($self->{_rules}{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | my $name = shift or confess("%Error: Cannot call defineRule() without the name of the Rule to find!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $self->{"_rules"}{$name} and not defined $self->{"_rules"}{$name}
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}
| |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | exists($self->{_rules}{$name}) and confess($self->name() . "Grammar already has a definition for the \"$name\" rule!\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | if (!exists($self->{_rules}{$name})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $ruleName = shift or confess("%Error: Cannot call without a rule name!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | ($self->rule($ruleName)) or confess("%Error: Cannot find the $ruleName rule on this grammar!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | my @examples = ( ); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if (defined($self->{_examples}{$ruleName})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | @examples = @{$self->{_examples}{$ruleName}}; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | return @examples; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | ###################################################################### | ||||||||||||||||||||||||||||
| 160 | #### Package return | ||||||||||||||||||||||||||||
| 161 | 4 | 1; | |||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||