| 1 | # Games::Checkers, Copyright (C) 1996-2012 Mikhael Goikhman, migo@cpan.org | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | package Games::Checkers::PDNParser; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Games::Checkers::Board; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Games::Checkers::Rules; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub new ($$;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my $filename = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $variant = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | -r "$filename.$_" and $filename .= ".$_" | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | for qw(pdn.gz pdn.xz pdn.bz2 pdn gz xz bz2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $file_to_open = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | $filename =~ /\.gz$/ ? "zcat $filename |" : | ||||||||||||||||||||||||||||||||||||||||||
| 34 | $filename =~ /\.xz$/ ? "xzcat $filename |" : | ||||||||||||||||||||||||||||||||||||||||||
| 35 | $filename =~ /\.bz2$/ ? "bzcat $filename |" : | ||||||||||||||||||||||||||||||||||||||||||
| 36 | $filename; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $fd = new IO::File $file_to_open; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | die "Can't open PDN for reading ($filename)\n" unless $fd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $self = { fn => $filename, fd => $fd, lineno => 0, variant => $variant }; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub error_prefix { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_prefix: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | "Error parsing $self->{fn}, line $self->{lineno}, corrupted record:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub next_record ($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_record: not called CC=22 SCAR=62.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $values = {}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $line; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $not_end = 0; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | while ($line = $self->{fd}->getline) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $self->{lineno}++; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | next if $line =~ /^\s*(([#;]|{.*}|\(.*\))\s*)?$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $not_end = 1; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($line =~ /\[(\w+)\s+"?(.*?)"?\]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $values->{$1} = $2; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return undef unless $not_end; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | for (qw(White Black)) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | next unless $values->{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $values->{$_} = join(' ', reverse split(/\s*,\s*/, $values->{$_})); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $result = $values->{Result}; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | die $self->error_prefix . "\tNon empty named value 'Result' is missing\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | unless $result; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $lineno = $self->{lineno}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $variant = $values->{GameType} || $self->{variant}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$values->{'GameType'} || $self->{'variant'}
MC/DC0%0/2
$values->{'GameType'} || $self->{'variant'}
$values->{'GameType'}
$self->{'variant'}
Truth table0%0/3
A || B
A= $values->{'GameType'}B= $self->{'variant'}
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | Games::Checkers::Rules::set_variant($variant); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $board = Games::Checkers::Board->new($values->{FEN}); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $move_string = ""; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | while (!$move_string || ($line = $self->{fd}->getline) && $self->{lineno}++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $move_string or $line = $self->{"fd"}->getline and $self->{"lineno"}++
$line = $self->{"fd"}->getline and $self->{"lineno"}++
MC/DC0%0/3
not $move_string or $line = $self->{"fd"}->getline and $self->{"lineno"}++
not $move_string
$line = $self->{"fd"}->getline
$self->{"lineno"}++
Truth table0%0/4
not(A) or B and C
A= not $move_stringB= $line = $self->{"fd"}->getlineC= $self->{"lineno"}++
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $line =~ s/[\r\n]+$/ /; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $move_string .= $line; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | last if $line =~ /\Q$result\E\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # tolerate some broken PDNs without trailing result separator | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $next_char = $self->{fd}->getc; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | last unless defined $next_char; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $self->{fd}->ungetc(ord($next_char)); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | last if $next_char eq "["; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # tolerate some broken PDNs without result separator | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # warn $self->error_prefix . "\tSeparator ($result) from line $lineno is not found, continuing anyway\n" | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # unless $line =~ /\Q$result\E\s*$/; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $move_string =~ s/\Q$result\E\s*$//; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $move_string =~ s/{[^}]*}//g; # remove comments | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $move_string =~ s/\([^\)]*(\)[^(]*)?\)//g; # remove comments | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $move_string =~ s/([x:*-])\s+(\d|\w)/$1$2/gi; # remove alignment spaces | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my @move_verge_strings = split(/(?:\s+|\d+\.+\s*)+/, $move_string); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | shift @move_verge_strings while @move_verge_strings && !$move_verge_strings[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@move_verge_strings and not $move_verge_strings[0]
MC/DC0%0/2
@move_verge_strings and not $move_verge_strings[0]
@move_verge_strings
not $move_verge_strings[0]
Truth table0%0/3
A and not(B)
A= @move_verge_stringsB= not $move_verge_strings[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # tolerate some broken PDNs with no real moves, like: 1. - - 2. - - | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | pop @move_verge_strings while @move_verge_strings && $move_verge_strings[0] eq '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@move_verge_strings and $move_verge_strings[0] eq "-"
MC/DC0%0/2
@move_verge_strings and $move_verge_strings[0] eq "-"
@move_verge_strings
$move_verge_strings[0] eq "-"
Truth table0%0/3
A and B
A= @move_verge_stringsB= $move_verge_strings[0] eq "-"
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | my @move_verges = map { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | die $self->error_prefix . "\tIncorrect move notation ($_)\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | unless /^(\d+|\w\d)([x:*-])(\d+|\w\d)((?!-)\2(\d+|\w\d))*$/i; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 114 | $2 eq "-" ? 0 : 1, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | /^\d+$/ ? $board->num_to_loc($_) : $board->str_to_loc($_) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } split(/[x:*-]/) | ||||||||||||||||||||||||||||||||||||||||||
| 118 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } @move_verge_strings; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return [ \@move_verges, $values, $variant, $board ]; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||