| 1 | package Text::Scigen::scigen; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # This file is part of SCIgen. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # SCIgen is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 6 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 7 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 8 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # SCIgen 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 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 16 | # along with SCIgen; if not, write to the Free Software | ||||||||||||||||||||||||||||
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use vars qw($SCIGEND_PORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | #### daemon settings #### | ||||||||||||||||||||||||||||
| 24 | 1 | $SCIGEND_PORT = 4724; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | sub dup_name { | ||||||||||||||||||||||||||
|
Subroutine
dup_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 9 | my $name = shift; | |||||||||||||||||||||||||||
| 28 | 9 | return $name . "!!!"; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub file_name { | ||||||||||||||||||||||||||
|
Subroutine
file_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 2 | my $name = shift; | |||||||||||||||||||||||||||
| 33 | 2 | return $name . ".file"; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub read_rules { | ||||||||||||||||||||||||||
|
Subroutine
read_rules: called CC=19 SCAR=30.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 2 | my ($fh, $rules, $RE, $debug, $self) = @_; | |||||||||||||||||||||||||||
| 38 | 2 | my $line; | |||||||||||||||||||||||||||
| 39 | 2 | ▶ | while ($line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3979 | ▶ | next if $line =~ /^#/ ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 3850 | ▶ | next if $line !~ /\S/ ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3176 | my @words = split /\s+/, $line; | |||||||||||||||||||||||||||
| 44 | 3176 | my $name = shift @words; | |||||||||||||||||||||||||||
| 45 | 3176 | my $rule = ""; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # non-duplicate rule | ||||||||||||||||||||||||||||
| 48 | 3176 | ▶ | if( $name =~ /([^\+]*)\!$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 3 | $name = $1; | |||||||||||||||||||||||||||
| 50 | 3 | push @{$rules->{dup_name("$name")}}, ""; | |||||||||||||||||||||||||||
| 51 | 3 | next; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # include rule | ||||||||||||||||||||||||||||
| 55 | 3173 | ▶ | if( $name =~ /\.include$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | my $file = $words[0]; | |||||||||||||||||||||||||||
| 57 | # make sure we haven't already included this file | ||||||||||||||||||||||||||||
| 58 | # NOTE: this allows the main file to be included at most twice | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | if( defined $rules->{&file_name($file)} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if( $debug > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | print "Skipping duplicate included file $file\n"; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | next; | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 1 | $rules->{&file_name($file)} = 1; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if( $debug > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | print "Opening included file $file\n"; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 1 | my $path = $self->_find( $file ); | |||||||||||||||||||||||||||
| 71 | 1 | ▶ | open(my $inc_fh, "<:utf8", $path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | or die( "Couldn't open included file $path" ); | ||||||||||||||||||||||||||||
| 73 | 1 | &read_rules( $inc_fh, $rules, undef, $debug ); | |||||||||||||||||||||||||||
| 74 | 1 | next; # we don't want to have .include itself be a rule | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3172 | ▶ | if ($#words == 0 && $words[0] eq '{') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$#words == 0 and $words[0] eq "{"
MC/DC100%2/2
$#words == 0 and $words[0] eq "{"
$#words == 0
$words[0] eq "{"
Truth table100%3/3
A and B
A= $#words == 0B= $words[0] eq "{"
| |||||||||||||||||||||||||||||
| 78 | 124 | my $end = 0; | |||||||||||||||||||||||||||
| 79 | 124 | ▶ | while ($line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1147 | ▶ | if ($line =~ /^}[\r\n]+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 124 | $end = 1; | |||||||||||||||||||||||||||
| 82 | 124 | last; | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 1023 | $rule .= $line; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 124 | ▶ | if (! $end) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | die "$name: EOF found before close rule\n"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 3048 | $line =~ s/^\S+\s+//; | |||||||||||||||||||||||||||
| 92 | 3048 | chomp ($line); | |||||||||||||||||||||||||||
| 93 | 3048 | $rule = $line; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # look for the weight | ||||||||||||||||||||||||||||
| 97 | 3172 | my $weight = 1; | |||||||||||||||||||||||||||
| 98 | 3172 | ▶ | if( $name =~ /([^\+]*)\+(\d+)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 196 | $name = $1; | |||||||||||||||||||||||||||
| 100 | 196 | $weight = $2; | |||||||||||||||||||||||||||
| 101 | 196 | ▶ | if( $debug > 10 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | warn "weighting rule by $weight: $name -> $rule\n"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3172 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 4885 | push @{$rules->{$name}}, $rule; | |||||||||||||||||||||||||||
| 108 | } while( --$weight > 0 ); | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if( defined $RE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | compute_re( $rules, $RE ); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub compute_re { | ||||||||||||||||||||||||||
|
Subroutine
compute_re: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # must sort; order matters, and we want to make sure that we get | ||||||||||||||||||||||||||||
| 120 | # the longest matches first | ||||||||||||||||||||||||||||
| 121 | 1 | my ($rules, $RE) = @_; | |||||||||||||||||||||||||||
| 122 | 1 | my $in = join "|", sort { length ($b) <=> length ($a) } keys %$rules; | |||||||||||||||||||||||||||
| 123 | 1 | $$RE = qr/^(.*?)(${in})/s ; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | sub generate { | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 1 | my ($rules, $start, $RE, $debug, $pretty) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my $s = expand ($rules, $start, $RE, $debug); | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | if( $pretty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | $s = pretty_print($s); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 1 | return $s; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 6 | ▶ | sub pick_rand { | ||||||||||||||||||||||||||
|
Subroutine
pick_rand: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 6 | my ($set) = @_; | |||||||||||||||||||||||||||
| 140 | 6 | my $n = $#$set + 1; | |||||||||||||||||||||||||||
| 141 | 6 | my $v = @$set[int (rand () * $n)]; | |||||||||||||||||||||||||||
| 142 | 6 | return $v; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 11 | ▶ | sub pop_first_rule { | ||||||||||||||||||||||||||
|
Subroutine
pop_first_rule: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 11 | my ($rules, $preamble, $input, $rule, $RE) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 11 | $$preamble = undef; | |||||||||||||||||||||||||||
| 149 | 11 | $$rule = undef; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 11 | my $ret = undef; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 11 | ▶ | if ($$input =~ s/$RE//s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 5 | $$preamble = $1; | |||||||||||||||||||||||||||
| 155 | 5 | $$rule = $2; | |||||||||||||||||||||||||||
| 156 | 5 | return 1; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 6 | return 0; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub pretty_print { | ||||||||||||||||||||||||||
|
Subroutine
pretty_print: called CC=9 SCAR=32.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1 | my ($s) = shift; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | my $news = ""; | |||||||||||||||||||||||||||
| 167 | 1 | my @lines = split( /\n/, $s ); | |||||||||||||||||||||||||||
| 168 | 1 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 1 | my $newline = ""; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | $line =~ s/(\s+)([\.\,\?\;\:])/$2/g; | |||||||||||||||||||||||||||
| 173 | 1 | $line =~ s/(\b)(a)\s+([aeiou])/$1$2n $3/gi; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | ▶ | if( $line =~ /\\section(\*?){(.*)}/ ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 176 | 0 | $newline = "\\section${1}{" . | |||||||||||||||||||||||||||
| 177 | Text::Autoformat::autoformat( $2, { case => 'highlight', | ||||||||||||||||||||||||||||
| 178 | squeeze => 0 } ); | ||||||||||||||||||||||||||||
| 179 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 180 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 181 | 0 | $newline .= "}"; | |||||||||||||||||||||||||||
| 182 | ▶ | } elsif( $line =~ /(\\subsection){(.*)}/ or | |||||||||||||||||||||||||||
|
Condition33%1/3
$line =~ /(\\subsection){(.*)}/ or $line =~ /(\\slideheading){(.*)}/
MC/DC0%0/2
$line =~ /(\\subsection){(.*)}/ or $line =~ /(\\slideheading){(.*)}/
$line =~ /(\\subsection){(.*)}/
$line =~ /(\\slideheading){(.*)}/
Truth table33%1/3
A or B
A= $line =~ /(\\subsection){(.*)}/B= $line =~ /(\\slideheading){(.*)}/
| |||||||||||||||||||||||||||||
| 183 | $line =~ /(\\slideheading){(.*)}/ ) { | ||||||||||||||||||||||||||||
| 184 | 0 | $newline = $1 . "{" . | |||||||||||||||||||||||||||
| 185 | Text::Autoformat::autoformat( $2, { case => 'highlight', | ||||||||||||||||||||||||||||
| 186 | squeeze => 0 } ); | ||||||||||||||||||||||||||||
| 187 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 188 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 189 | 0 | $newline .= "}"; | |||||||||||||||||||||||||||
| 190 | } elsif( $line =~ /\\title{(.*)}/ ) { | ||||||||||||||||||||||||||||
| 191 | 0 | $newline = "\\title{" . | |||||||||||||||||||||||||||
| 192 | Text::Autoformat::autoformat( $1, { case => 'highlight', | ||||||||||||||||||||||||||||
| 193 | squeeze => 0 } ); | ||||||||||||||||||||||||||||
| 194 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 195 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 196 | 0 | $newline .= "}"; | |||||||||||||||||||||||||||
| 197 | } elsif( $line =~ /(.*) = {(.*)}\,/ ) { | ||||||||||||||||||||||||||||
| 198 | 0 | my $label = $1; | |||||||||||||||||||||||||||
| 199 | 0 | my $curr = $2; | |||||||||||||||||||||||||||
| 200 | # place brackets around any words containing capital letters | ||||||||||||||||||||||||||||
| 201 | 0 | $curr =~ s/\b([^\s]*[A-Z]+[^\s\:]*)\b/\{$1\}/g; | |||||||||||||||||||||||||||
| 202 | 0 | $newline = "$label = {" . | |||||||||||||||||||||||||||
| 203 | Text::Autoformat::autoformat( $curr, { case => 'highlight', | ||||||||||||||||||||||||||||
| 204 | squeeze => 0 } ); | ||||||||||||||||||||||||||||
| 205 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 206 | 0 | chomp $newline; | |||||||||||||||||||||||||||
| 207 | 0 | $newline .= "},"; | |||||||||||||||||||||||||||
| 208 | } elsif( $line =~ /\S/ ) { | ||||||||||||||||||||||||||||
| 209 | 1 | $newline = | |||||||||||||||||||||||||||
| 210 | Text::Autoformat::autoformat( $line, { case => 'sentence', | ||||||||||||||||||||||||||||
| 211 | squeeze => 0, | ||||||||||||||||||||||||||||
| 212 | break => break_latex(), | ||||||||||||||||||||||||||||
| 213 | ignore => qr/^\\/ } ); | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | $newline =~ s/\\Em/\\em/g; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | ▶ | if( $newline !~ /\n$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | $newline .= "\n"; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 1 | $news .= $newline; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | return $news; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1 | ▶ | sub break_latex($$$) { | ||||||||||||||||||||||||||
|
Subroutine
break_latex: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 229 | 1 | my ($text, $reqlen, $fldlen) = @_; | |||||||||||||||||||||||||||
| 230 | 1 | ▶ | if( !defined $text ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 1 | $text = ""; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | 1 | return { $text, "" }; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 6 | ▶ | sub expand { | ||||||||||||||||||||||||||
|
Subroutine
expand: called CC=16 SCAR=40.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 237 | 6 | my ($rules, $start, $RE, $debug) = @_; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # check for special rules ending in + and # | ||||||||||||||||||||||||||||
| 240 | # Rules ending in + generate a sequential integer | ||||||||||||||||||||||||||||
| 241 | # The same rule ending in # chooses a random # from among preiously | ||||||||||||||||||||||||||||
| 242 | # generated integers | ||||||||||||||||||||||||||||
| 243 | 6 | ▶ | if( $start =~ /(.*)\+$/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 244 | 0 | my $rule = $1; | |||||||||||||||||||||||||||
| 245 | 0 | my $i = $rules->{$rule}; | |||||||||||||||||||||||||||
| 246 | 0 | ▶ | if( !defined $i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 0 | $i = 0; | |||||||||||||||||||||||||||
| 248 | 0 | $rules->{$rule} = 1; | |||||||||||||||||||||||||||
| 249 | } else { | ||||||||||||||||||||||||||||
| 250 | 0 | $rules->{$rule} = $i+1; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 0 | return $i; | |||||||||||||||||||||||||||
| 253 | } elsif( $start =~ /(.*)\#$/ ) { | ||||||||||||||||||||||||||||
| 254 | 0 | my $rule = $1; | |||||||||||||||||||||||||||
| 255 | 0 | my $i = $rules->{$rule}; | |||||||||||||||||||||||||||
| 256 | 0 | ▶ | if( !defined $i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 0 | $i = 0; | |||||||||||||||||||||||||||
| 258 | } else { | ||||||||||||||||||||||||||||
| 259 | 0 | $i = int rand $i; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | 0 | return $i; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 6 | my $full_token; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 6 | my $repeat = 0; | |||||||||||||||||||||||||||
| 266 | 6 | my $count = 0; | |||||||||||||||||||||||||||
| 267 | 6 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 6 | my $input = pick_rand ($rules->{$start}); | |||||||||||||||||||||||||||
| 270 | 6 | $count++; | |||||||||||||||||||||||||||
| 271 | 6 | ▶ | if ($debug >= 5) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | warn "$start -> $input\n"; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 6 | my ($pre, $rule); | |||||||||||||||||||||||||||
| 276 | 6 | my @components; | |||||||||||||||||||||||||||
| 277 | 6 | $repeat = 0; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 6 | ▶ | while (pop_first_rule ($rules, \$pre, \$input, \$rule, $RE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 5 | my $ex = expand ($rules, $rule, $RE, $debug); | |||||||||||||||||||||||||||
| 281 | 5 | ▶ | push @components, $pre if length ($pre); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 5 | ▶ | push @components, $ex if length ($ex); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 6 | ▶ | push @components, $input if length ($input); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 6 | $full_token = join "", @components ; | |||||||||||||||||||||||||||
| 286 | 6 | my $ref = $rules->{dup_name("$start")}; | |||||||||||||||||||||||||||
| 287 | 6 | ▶ | if( defined $ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 0 | my @dups = @{$ref}; | |||||||||||||||||||||||||||
| 289 | # make sure we haven't generated this exact token yet | ||||||||||||||||||||||||||||
| 290 | 0 | foreach my $d (@dups) { | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | if( $d eq $full_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | $repeat = 1; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | if( !$repeat ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 297 | 0 | push @{$rules->{dup_name("$start")}}, $full_token; | |||||||||||||||||||||||||||
| 298 | } elsif( $count > 50 ) { | ||||||||||||||||||||||||||||
| 299 | 0 | $repeat = 0; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | } while( $repeat ); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 6 | return $full_token; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 1 | 1; | |||||||||||||||||||||||||||