| 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::Production; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | require 5.006_001; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Parse::RandGen qw($Debug); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use vars qw($Debug); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #### Creators | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 11 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 34 | _conditions => [ ], # Ordered list of Conditions that must be satisfied for the production to be true | ||||||||||||||||||||||||||||||||||||||||||
| 35 | _action => undef, # Action to take if the production is satisfied | ||||||||||||||||||||||||||||||||||||||||||
| 36 | _rule => undef, # The Rule that this Production belongs to | ||||||||||||||||||||||||||||||||||||||||||
| 37 | _name => undef, # The name of the Production (most are anonymous, but Productions can be named if they need to be accessed later) | ||||||||||||||||||||||||||||||||||||||||||
| 38 | _number => undef, # The number of the Production in the Rule (which production is this 0...X) | ||||||||||||||||||||||||||||||||||||||||||
| 39 | #@_, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 11 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Optional named arguments can be passed. Any unknown named arguments are turned into object data members. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 | my @args = @_; # Arguments can override defaults or create new attributes in the object | |||||||||||||||||||||||||||||||||||||||||
| 45 | 11 | my $numArgs = $#args + 1; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 11 | ▶ | if ($numArgs == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $self->addCond(shift(@args)); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } elsif ($numArgs) { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | ($numArgs % 2) and confess("%Error: new Production called with an odd number of arguments ($numArgs); arguments must be in named pairs (or a single Condition argument)!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | $self->set(@args); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | my $rule = $self->{_rule}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | (!defined($self->{_rule}) || UNIVERSAL::isa($rule, "Parse::RandGen::Rule")) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $self->{"_rule"} or UNIVERSAL::isa($rule, "Parse::RandGen::Rule")
MC/DC0%0/2
not defined $self->{"_rule"} or UNIVERSAL::isa($rule, "Parse::RandGen::Rule")
not defined $self->{"_rule"}
UNIVERSAL::isa($rule, "Parse::RandGen::Rule")
Truth table33%1/3
not(A) or B
A= not defined $self->{"_rule"}B= UNIVERSAL::isa($rule, "Parse::RandGen::Rule")
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | or confess("%Error: new Production was passed an unknown \"rule\" argument \"$rule\"!\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | return($self); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 61 | #### Methods | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=9 SCAR=30.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | my $numArgs = $#args + 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | ($numArgs) or confess("%Error: Production::set() called with no arguments!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | ($numArgs % 2) and confess("%Error: Production::set() called with an odd number of arguments ($numArgs); arguments must be in named pairs!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | while ($#args >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my ($arg, $val) = (shift(@args), shift(@args)); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | if ($arg eq "cond") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | $self->addCond($val); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } elsif ($arg eq "action") { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{_action} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } elsif ($arg eq "rule") { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | UNIVERSAL::isa($val, "Parse::RandGen::Rule") or confess("%Error: Production::set() called with a bad \"rule\" argument ($val)!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $self->{_rule} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # Unknown arguments become data members | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $self->{$arg} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 11 | ▶ | sub addCond { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
addCond: called CC=16 SCAR=44.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 11 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 11 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 11 | ▶ | while ($#args >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 11 | my $val = shift(@args); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 11 | ▶ | defined($val) or confess("%Error: Production::addCond(): condition is undefined!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 11 | my $element = $val; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 11 | my $cond = undef; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 11 | my $valRef = ref($val); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 11 | ▶ | if ($valRef) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 11 | ▶ | if ($valRef eq "Regexp") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | # Regular expression | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | $cond = Parse::RandGen::Regexp->new($element); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 10 | ▶ | (UNIVERSAL::isa($val, "Parse::RandGen::Condition")) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | or confess("%Error: The Production condition is a reference (ref=\"$valRef\"), but not a supported type!"); | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 10 | $cond = $val; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } elsif ($val =~ m/^\s* (\w+) (?: \( (.*?) \) )? \s*$/x) { # subrule(subargs) | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my ($min, $max) = (1, 1); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $element = $1; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $subargs = $2; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (defined($subargs) && $subargs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $subargs and $subargs
MC/DC0%0/2
defined $subargs and $subargs
defined $subargs
$subargs
Truth table0%0/3
A and B
A= defined $subargsB= $subargs
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($subargs eq "?" ) { $min = 0; $max = 1; } # ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | elsif ($subargs =~ m/^(s|\+)$/ ) { $min = 1; $max = undef; } # s or + | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | elsif ($subargs =~ m/^((s\?)|\*)$/ ) { $min = 0; $max = undef; } # s? or * | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | elsif ($subargs =~ /(\d+)(?:\.\.(\d+))?/ ) { $min = $1; $max = ($2 || $min); } # 2..3 or ..4 or 5.. | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$2 || $min
MC/DC0%0/2
$2 || $min
$2
$min
Truth table0%0/3
A || B
A= $2B= $min
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | confess("%Error: The Production condition \"${val}\" has decoded to be a subrule, but the subargs are not understood (${subargs})!"); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $cond = Parse::RandGen::Subrule->new($element, min=>$min, max=>$max); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } elsif ($element =~ $Parse::RandGen::Literal::ValidLiteralRE) { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # Must be a literal surrounded by single or double quotes | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $element = Parse::RandGen::Literal::stripLiteral($element); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $cond = Parse::RandGen::Literal->new($element); | |||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | confess("%Error: The Production condition \"${val}\" has decoded to be a literal, but it doesn't look good!"); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 10 | $cond->{_production} = $self; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 10 | push @{$self->{_conditions}}, $cond; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return "%Error: Production has no RandGen object!\n" unless $self->grammar(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my $grammarName = $self->grammar()->name(); | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $err = ""; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | foreach my $cond (@{$self->{_conditions}}) { | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | next unless $cond->isSubrule(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $subrule = $cond->subrule(); # Will be undef if there is a problem | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $subruleName = $cond->element(); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | next unless defined($subruleName); # Anonymous subrule | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $rule = $self->grammar()->rule($subruleName); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | next if (defined($rule) && ($rule == $subrule)); # Everything is OK! | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $rule and $rule == $subrule
MC/DC0%0/2
defined $rule and $rule == $subrule
defined $rule
$rule == $subrule
Truth table0%0/3
A and B
A= defined $ruleB= $rule == $subrule
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $ruleName = $self->rule()->name(); # The name of the rule that this production belongs to... | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $err .= "%Error: The \"${ruleName}\" rule references the subrule \"${subruleName}\", which is not defined in the \"${grammarName}\" grammar!\n" unless (defined($subrule)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return $err; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $output = ""; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | foreach my $cond (@{$self->{_conditions}}) { | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $output .= " " if $output; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $output .= $cond->dump(); | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $output .= $self->_dumpParseFunction(); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 400 | ▶ | sub dumpHeir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dumpHeir: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 400 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 400 | my $output = ""; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 400 | foreach my $cond (@{$self->{_conditions}}) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 400 | ▶ | $output .= " " if $output; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 400 | $output .= $cond->dump(); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 400 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 814 | ▶ | sub pick { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 814 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 814 | my %args = ( match=>1, # Default is to pick matching data | |||||||||||||||||||||||||||||||||||||||||
| 175 | vals => { }, # Hash of values of various hard-coded sub-rules (by name) | ||||||||||||||||||||||||||||||||||||||||||
| 176 | @_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 814 | my @conds = $self->conditions(); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 814 | my $badCond; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 814 | my $val = ""; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 814 | ▶ | if (!$args{match}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 120 | my @badConds; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 120 | foreach my $cond (@conds) { | |||||||||||||||||||||||||||||||||||||||||
| 184 | 221 | ▶ | next if ($cond->isQuantSupported() && $cond->zeroOrMore()); # Cannot corrupt | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cond->isQuantSupported and $cond->zeroOrMore
MC/DC0%0/2
$cond->isQuantSupported and $cond->zeroOrMore
$cond->isQuantSupported
$cond->zeroOrMore
Truth table66%2/3
A and B
A= $cond->isQuantSupportedB= $cond->zeroOrMore
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 221 | push(@badConds, $cond); | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 120 | my $i = int(rand($#badConds+1)); | |||||||||||||||||||||||||||||||||||||||||
| 188 | 120 | $badCond = $badConds[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 814 | ▶ | for (my $i=0; $i <= $#conds; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 1016 | ▶ | $val .= $conds[$i]->pick(%args, match=>($args{match} || ((defined($badCond) && ($badCond==$conds[$i]))?0:1)) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$args{'match'} || (defined $badCond && $badCond == $conds[$i] ? 0 : 1)
defined $badCond and $badCond == $conds[$i]
MC/DC66%2/3
$args{'match'} || (defined $badCond && $badCond == $conds[$i] ? 0 : 1)
$args{'match'}
defined $badCond
$badCond == $conds[$i]
Truth table75%3/4
A || B and C
A= $args{'match'}B= defined $badCondC= $badCond == $conds[$i]
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 814 | return( $val ); | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | # Returns true (1) if this production contains any of the rules specified by the "vals" argument | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 1830 | ▶ | sub containsVals { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
containsVals: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 1830 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1830 | my %args = ( vals => { }, # Hash of values of various hard-coded sub-rules (by name) | |||||||||||||||||||||||||||||||||||||||||
| 202 | @_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 1830 | foreach my $cond ($self->conditions()) { | |||||||||||||||||||||||||||||||||||||||||
| 204 | 2032 | ▶ | return 1 if $cond->containsVals(%args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 1830 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 210 | #### Accessors | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub action { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
action: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call name() without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | return $self->{_action}; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub rule { # Rule that this Production belongs to | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rule: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call rule() without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | return $self->{_rule}; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub name { # Name of the Production (optional) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call rule() without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | return $self->{_name}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub number { # Production number on its Rule (required if defined(rule())) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
number: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call rule() without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | return $self->{_number}; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub grammar { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
grammar: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call grammar() without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | my $grammar = $self->rule()->grammar() if defined($self->rule()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | return $grammar; | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 2644 | ▶ | sub conditions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
conditions: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 2644 | ▶ | my $self = shift or confess("%Error: Cannot call conditions() without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 2644 | return (@{$self->{_conditions}}); | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 244 | #### Private Functions | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | sub _dumpParseFunction { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dumpParseFunction: not called CC=21 SCAR=61.4
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $output = "\n\t\t\t {\t"; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $indent = "\n\t\t\t\t"; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if (defined($self->{_action})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $output .= $self->{_action} . " }"; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # Determine whether this is a single terminal production or not | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my @conds = @{$self->{_conditions}}; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $ind = 1; # Index 0 is the rule name | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $output .= 'my $val=""; my $obj={val=>undef,offset=>$itempos[1]{offset}{from},len=>0,rules=>{}};'; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | foreach my $cond (@conds) { | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $output .= $indent; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my $sName = $cond->isSubrule() ? $cond->subrule()->name() : ""; # Name of subrule, if subrule... | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | my $sKeep = $sName ? $cond->subrule()->{keep}||"" : ""; # Keep this subrule? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$cond->subrule->{"keep"} or ""
MC/DC0%0/1
$cond->subrule->{"keep"} or ""
$cond->subrule->{"keep"}
Truth table0%0/2
A
A= $cond->subrule->{"keep"}
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | my $sParse = $sName ? $cond->subrule()->{parse}||"" : ""; # Parse this subrule (preserve heirarchy beneath it) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$cond->subrule->{"parse"} or ""
MC/DC0%0/1
$cond->subrule->{"parse"} or ""
$cond->subrule->{"parse"}
Truth table0%0/2
A
A= $cond->subrule->{"parse"}
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if ($cond->once()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | #$output .= "if (ref(\$item[$ind])) { \$val.=\$item[$ind]->{val}; } else { \$val.=\$item[$ind]; }"; | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ($sName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $output .= "\$val.=\$item[$ind]->{val};"; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if ($sKeep eq "once") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | $output .= " \$obj->{rules}{$sName}=\$item[$ind];"; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } elsif ($sKeep eq "all") { | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $output .= " \$obj->{rules}{$sName}||=[]; push(\@{\$obj->{rules}{$sName}}, \$item[$ind]);"; | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if (!$sParse) { # Not adding a new level of parse, so flatten rules | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | $output .= "${indent}foreach my \$j (keys \%{\$item[$ind]->{rules}}) {" | |||||||||||||||||||||||||||||||||||||||||
| 275 | ."${indent}\tmy \$o=\$item[$ind]->{rules}{\$j};" | ||||||||||||||||||||||||||||||||||||||||||
| 276 | ."${indent}\tif (ref(\$o) eq \"ARRAY\") { \$obj->{rules}{\$j}||=[]; push(\@{\$obj->{rules}{\$j}}, \$o); }" | ||||||||||||||||||||||||||||||||||||||||||
| 277 | ."${indent}\telse { \$obj->{rules}{\$j}=\$o; } }"; | ||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | $output .= "\$val.=\$item[$ind];"; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 283 | #$output .= "foreach my \$i (\@{\$item[$ind]}) { if(ref(\$i)){ \$val.=\$i->{val}; } else { \$val.=\$i; }"; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if ($sName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | $output .= "foreach my \$i (\@{\$item[$ind]}) { \$val.=\$i->{val};"; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if ($sKeep eq "once") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | $output .= " \$obj->{rules}{$sName}=\$i;"; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } elsif ($sKeep eq "all") { | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $output .= " \$obj->{rules}{$sName} ||= []; push(\@{\$obj->{rules}{$sName}}, \$i);"; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | if (!$sParse) { # Not adding a new level of parse, so flatten rules | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | $output .= "${indent}\tforeach my \$j (keys \%{\$i->{rules}}) {" | |||||||||||||||||||||||||||||||||||||||||
| 293 | ."${indent}\t\tmy \$o=\$i->{rules}{\$j};" | ||||||||||||||||||||||||||||||||||||||||||
| 294 | ."${indent}\t\tif (ref(\$o) eq \"ARRAY\") { \$obj->{rules}{\$j}||=[]; push(\@{\$obj->{rules}{\$j}}, \$o); }" | ||||||||||||||||||||||||||||||||||||||||||
| 295 | ."${indent}\t\telse { \$obj->{rules}{\$j}=\$o; } }"; | ||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $output .= " }"; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $output .= "foreach my \$i (\@{\$item[$ind]}) { \$val.=\$i; }"; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $ind++; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | #$output .= " print(\$item[0],\" [\",\$itempos[1]{offset}{from},\"..\${thisoffset}]\\n\");"; | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | (defined($self->rule()) and $self->rule()->name()) or confess("%Error: _dumpParseFunction(): Rule is not defined or the Rule is anonymous (no name)!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->rule and $self->rule->name
MC/DC0%0/2
defined $self->rule and $self->rule->name
defined $self->rule
$self->rule->name
Truth table0%0/3
A and B
A= defined $self->ruleB= $self->rule->name
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | my $ruleName = $self->rule()->name(); | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | my $prodNum = $self->number(); | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | ($self->grammar()->rule($ruleName) == $self->rule()) or confess("%Error: Internal error! Cannot find our Rule \"$ruleName\" on our RandGen!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | #$output .= " \$thisparser->{local}{grammar}->rule(\"$ruleName\")->production($prodNum);"; | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | $output .= $indent.'$obj->{val}=$val; $obj->{len}=length($val);'; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | $output .= ' $return=$obj; }'; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 316 | #### Package return | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 318 | __END__ | ||||||||||||||||||||||||||||||||||||||||||