Parse/Round.pm

filter: Statementstmt 83.9% 89 / 106 Branchbran 55.7% 29 / 52 Conditioncond 54.5% 6 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 94.4% 17 / 18 Podpod 33.3% 3 / 9 totaltotal 71.7% 145 / 202 CC 32 SCAR 20.4
CC
32
Cov
73%
as_array
20.0
parse_line
14.7
as_hash
4.9
CRAP 7.7
« Parse/Util.pm ↑ Summary Parse/Register.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::Parse::Round.pm
4 #
5 # PODNAME: Games::Go::AGA::Parse::Round
6 # ABSTRACT: Parses lines from an AGA Tournament Round file
7 #
8 # AUTHOR: Reid Augustin (REID), <reid@lucidport.com>
9 # COMPANY: LucidPort Technology, Inc.
10 # CREATED: 11/19/2010 03:13:05 PM PST
11 #===============================================================================
12  
13 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 package Games::Go::AGA::Parse::Round;
18 1 ▶use parent 'Games::Go::AGA::Parse';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use String::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Games::Go::AGA::Parse::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Games::Go::AGA::Parse::Util qw( normalize_ID );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = '0.042'; # VERSION
27  
28 2 ▶sub white_id {
Subroutine white_id: called CC=3 SCAR=12.5
Pod undocumented
29 2 my ($self, $new) = @_;
30  
31 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 2 2
32 0 $self->{white_id} = $new;
33 }
34 2 ▶ return $self->{white_id} || '';
Condition50%1/2
$self->{'white_id'} || ''
l!l
20
MC/DC0%0/1
$self->{'white_id'} || ''
$self->{'white_id'}
Truth table50%1/2
A
A= $self->{'white_id'}
Aresult
11
00
35 }
36  
37 1 ▶sub black_id {
Subroutine black_id: called CC=3 SCAR=12.5
Pod undocumented
38 1 my ($self, $new) = @_;
39  
40 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1 1
41 0 $self->{black_id} = $new;
42 }
43 1 ▶ return $self->{black_id} || '';
Condition50%1/2
$self->{'black_id'} || ''
l!l
10
MC/DC0%0/1
$self->{'black_id'} || ''
$self->{'black_id'}
Truth table50%1/2
A
A= $self->{'black_id'}
Aresult
11
00
44 }
45  
46 1 ▶sub result {
Subroutine result: called CC=3 SCAR=12.5
Pod undocumented
47 1 my ($self, $new) = @_;
48  
49 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1 1
50 0 $self->{result} = $new;
51 }
52 1 ▶ return $self->{result} || '';
Condition50%1/2
$self->{'result'} || ''
l!l
10
MC/DC0%0/1
$self->{'result'} || ''
$self->{'result'}
Truth table50%1/2
A
A= $self->{'result'}
Aresult
11
00
53 }
54  
55 1 ▶sub handicap {
Subroutine handicap: called CC=3 SCAR=12.5
Pod undocumented
56 1 my ($self, $new) = @_;
57  
58 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1 1
59 0 $self->{handicap} = $new;
60 }
61 1 ▶ return defined $self->{handicap} ? $self->{handicap} : '0';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'handicap'} ? : 1 0 1
62 }
63  
64 1 ▶sub komi {
Subroutine komi: called CC=3 SCAR=12.5
Pod undocumented
65 1 my ($self, $new) = @_;
66  
67 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1 1
68 0 $self->{komi} = $new;
69 }
70 1 ▶ return defined $self->{komi} ? $self->{komi} : '0';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'komi'} ? : 1 0 1
71 }
72  
73 1 ▶sub comment {
Subroutine comment: called CC=3 SCAR=12.5
Pod undocumented
74 1 my ($self, $new) = @_;
75  
76 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1 1
77 0 $self->{comment} = $new;
78 }
79 1 ▶ return $self->{comment} || '';
Condition50%1/2
$self->{'comment'} || ''
l!l
10
MC/DC0%0/1
$self->{'comment'} || ''
$self->{'comment'}
Truth table50%1/2
A
A= $self->{'comment'}
Aresult
11
00
80 }
81  
82 0 ▶sub as_array {
Subroutine as_array: not called CC=4 SCAR=30.0
Pod documented
83 0 my ($self, $new) = @_;
84  
85 0 my @ret;
86 0 ▶ if ($self->white_id) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->white_id) { } 0 0 0
elsif (exists $self->{'comment'}) { } 0 0 0
87 @ret = map(
88 0 { $self->$_ }
89 # fields, in order
90 qw(
91 white_id
92 black_id
93 result
94 handicap
95 komi
96 comment
97 ),
98 );
99 }
100 elsif (exists $self->{comment}) {
101 0 @ret = ($self->comment);
102 }
103 0 ▶ return wantarray ? @ret : \@ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
104 }
105  
106 1 ▶sub as_hash {
Subroutine as_hash: called CC=4 SCAR=15.9
Pod documented
107 1 my ($self, $new) = @_;
108  
109 1 my %ret;
110 1 ▶ if ($self->white_id) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->white_id) { } 1 0 1
elsif (exists $self->{'comment'}) { } 0 0 0
111 %ret = map(
112 1 { $_, $self->$_ }
113 # fields
114 qw(
115 white_id
116 black_id
117 result
118 handicap
119 komi
120 comment
121 ),
122 );
123 }
124 elsif (exists $self->{comment}) {
125 0 %ret = (comment => $self->comment);
126 }
127 1 ▶ return wantarray ? %ret : \%ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
128 }
129  
130 1 Readonly my $WHITE_ID => 0;
131 1 Readonly my $BLACK_ID => 1;
132 1 Readonly my $RESULT => 2;
133 1 Readonly my $HANDICAP => 3;
134 1 Readonly my $KOMI => 4;
135  
136 1 Readonly my %name_of_state => (
137 $WHITE_ID => 'white_id',
138 $BLACK_ID => 'black_id',
139 $RESULT => 'result',
140 $HANDICAP => 'handicap',
141 $KOMI => 'komi',
142 );
143  
144 1 *parse = \&parse_line; # alias parse to parse_line
145 2 ▶sub parse_line {
Subroutine parse_line: called CC=14 SCAR=26.9
Pod documented
146 2 my ($self, $string) = @_;
147  
148 2 map { delete $self->{$_} }
149 qw(
150 white_id
151 black_id
152 result
153 handicap
154 komi
155 comment
156 );
157 2 ▶ return $self->as_hash if (not $string);
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 2 2
158 2 my $tokenizer = String::Tokenizer->new(
159 $string, # source string
160 # delimiters
161 # "~!@#\$\%^&*()`={}[]:;\"'<>,?/|\\\n",
162 "@#\$\n",
163 String::Tokenizer->RETAIN_WHITESPACE,
164 );
165 2 my $iter = $tokenizer->iterator;
166 2 $self->{source} = $string;
167 2 my $state = $WHITE_ID;
168 TOKEN:
169 2 ▶ while ($iter->hasNextToken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->hasNextToken) 20 0 20
170 20 my $token = $iter->nextToken;
171 #print $self->err("state $state, token $token");
172 20 ▶ if ($token eq "\n") { # a carriage return?
Branch: 3 branches
BranchTrueFalseTotal
if ($token eq "\n") { } 0 20 20
elsif (not $token =~ /\S/ or $token eq '') { } 9 11 20
elsif ($token eq '#') { } 2 9 11
173 0 last TOKEN;
174 }
175 ▶ elsif ($token !~ m/\S/ or # only whitespace
Condition66%2/3
not $token =~ /\S/ or $token eq ""
l!l&&r!l&&!r
9011
MC/DC50%1/2
not $token =~ /\S/ or $token eq ""
not $token =~ /\S/ $token eq ""
Truth table66%2/3
not(A) or B
A= not $token =~ /\S/B= $token eq ""
ABresult
1X1
011
000
176 $token eq '') { # empty
177 9 next TOKEN; # ignore
178 }
179 elsif ($token eq '#') { # comment
180 2 ▶ if ($state != $WHITE_ID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state != $WHITE_ID) 1 1 2
181 $self->_parse_error(
182 error => "got comment, expected $name_of_state{$state}",
183 source => $self->{source},
184 1 );
185 }
186 1 $self->{comment} = join q{}, $iter->collectTokensUntil("\n");
187 1 last TOKEN;
188 }
189 else {
190 #print $self->err("state $state, token $token");
191 9 ▶ if ($state == $WHITE_ID) {
Branch: 5 branches
BranchTrueFalseTotal
if ($state == $WHITE_ID) { } 2 7 9
elsif ($state == $BLACK_ID) { } 2 5 7
elsif ($state == $RESULT) { } 2 3 5
elsif ($state == $HANDICAP) { } 2 1 3
elsif ($state == $KOMI) { } 1 0 1
192 2 $self->{white_id} = normalize_ID($token);
193 2 $state = $BLACK_ID;
194 }
195 elsif ($state == $BLACK_ID) {
196 2 $self->{black_id} = normalize_ID($token);
197 2 $state = $RESULT;
198 }
199 elsif ($state == $RESULT) {
200 2 $self->{result} = $token;
201 2 $state = $HANDICAP;
202 }
203 elsif ($state == $HANDICAP) {
204 2 $self->{handicap} = 0 + $token; # numerify
205 2 $state = $KOMI;
206 }
207 elsif ($state == $KOMI) {
208 1 $self->{komi} = 0 + $token; # numerify
209 1 $state = $WHITE_ID;
210 }
211 else {
212 $self->_parse_error(
213 error => "unknown state: $state",
214 source => $self->{source},
215 0 );
216 }
217 9 next TOKEN;
218 }
219 }
220 1 ▶ if ($state != $WHITE_ID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state != $WHITE_ID) 0 1 1
221 $self->_parse_error(
222 error => "got end of line, expected $name_of_state{$state}",
223 source => $self->{source},
224 0 );
225 }
226 1 return $self->as_hash;
227 }
228  
229 1 1;
230  
231 __END__
« Parse/Util.pm ↑ Summary Parse/Register.pm »