Parse/Register.pm

filter: Statementstmt 87.2% 151 / 173 Branchbran 69.7% 53 / 76 Conditioncond 84.0% 21 / 25 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 96.3% 26 / 27 Podpod 25.0% 3 / 12 totaltotal 80.6% 263 / 326 CC 50 SCAR 18.6
CC
50
Cov
82%
as_array
20.0
parse_line
15.6
_get_flags
5.1
CRAP 6.4
« Parse/Round.pm ↑ Summary Parse.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::Parse::Register.pm
4 #
5 # PODNAME: Games::Go::AGA::Parse::Register
6 # ABSTRACT: models AGA register.tde file information
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::Register;
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 Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use String::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Games::Go::AGA::Parse::Util qw( is_ID is_Rating is_Rank_or_Rating normalize_ID );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Games::Go::AGA::Parse::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 our $VERSION = '0.042'; # VERSION
28  
29 3 ▶sub last_name {
Subroutine last_name: called CC=3 SCAR=12.5
Pod undocumented
30 3 my ($self, $new) = @_;
31  
32 3 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
33 0 $self->{last_name} = $new;
34 }
35 3 ▶ return $self->{last_name} || '';
Condition50%1/2
$self->{'last_name'} || ''
l!l
30
MC/DC0%0/1
$self->{'last_name'} || ''
$self->{'last_name'}
Truth table50%1/2
A
A= $self->{'last_name'}
Aresult
11
00
36 }
37  
38 3 ▶sub first_name {
Subroutine first_name: called CC=3 SCAR=11.4
Pod undocumented
39 3 my ($self, $new) = @_;
40  
41 3 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
42 0 $self->{first_name} = $new;
43 }
44 3 ▶ return $self->{first_name} || '';
Condition100%2/2
$self->{'first_name'} || ''
l!l
21
MC/DC100%1/1
$self->{'first_name'} || ''
$self->{'first_name'}
Truth table100%2/2
A
A= $self->{'first_name'}
Aresult
11
00
45 }
46  
47 9 ▶sub id {
Subroutine id: called CC=3 SCAR=11.4
Pod undocumented
48 9 my ($self, $new) = @_;
49  
50 9 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 9 9
51 0 $self->{id} = $new;
52 }
53 9 ▶ return $self->{id} || '';
Condition100%2/2
$self->{'id'} || ''
l!l
63
MC/DC100%1/1
$self->{'id'} || ''
$self->{'id'}
Truth table100%2/2
A
A= $self->{'id'}
Aresult
11
00
54 }
55  
56 6 ▶sub rank {
Subroutine rank: called CC=4 SCAR=13.9
Pod undocumented
57 6 my ($self, $new) = @_;
58  
59 6 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 3 3 6
60 3 $self->{rank} = $new;
61 3 ▶ $self->{rank} += 0 if (is_Rating($new)); # numify
Branch: 1 branch
BranchTrueFalseTotal
if is_Rating($new) 1 2 3
62 }
63 6 ▶ return $self->{rank} || '';
Condition50%1/2
$self->{'rank'} || ''
l!l
60
MC/DC0%0/1
$self->{'rank'} || ''
$self->{'rank'}
Truth table50%1/2
A
A= $self->{'rank'}
Aresult
11
00
64 }
65  
66 3 ▶sub flags {
Subroutine flags: called CC=3 SCAR=11.4
Pod undocumented
67 3 my ($self, $new) = @_;
68  
69 3 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
70 0 $self->{flags} = $new;
71 }
72 3 ▶ return $self->{flags} || [];
Condition100%2/2
$self->{'flags'} || []
l!l
12
MC/DC100%1/1
$self->{'flags'} || []
$self->{'flags'}
Truth table100%2/2
A
A= $self->{'flags'}
Aresult
11
00
73 }
74  
75 3 ▶sub club {
Subroutine club: called CC=3 SCAR=11.4
Pod undocumented
76 3 my ($self, $new) = @_;
77  
78 3 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
79 0 $self->{club} = $new;
80 }
81 3 ▶ return $self->{club} || '';
Condition100%2/2
$self->{'club'} || ''
l!l
12
MC/DC100%1/1
$self->{'club'} || ''
$self->{'club'}
Truth table100%2/2
A
A= $self->{'club'}
Aresult
11
00
82 }
83  
84 5 ▶sub comment {
Subroutine comment: called CC=3 SCAR=11.4
Pod undocumented
85 5 my ($self, $new) = @_;
86  
87 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 5 5
88 0 $self->{comment} = $new;
89 }
90 5 ▶ return $self->{comment} || '';
Condition100%2/2
$self->{'comment'} || ''
l!l
41
MC/DC100%1/1
$self->{'comment'} || ''
$self->{'comment'}
Truth table100%2/2
A
A= $self->{'comment'}
Aresult
11
00
91 }
92  
93 4 ▶sub directive {
Subroutine directive: called CC=3 SCAR=11.4
Pod undocumented
94 4 my ($self, $new) = @_;
95  
96 4 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 4 4
97 0 $self->{directive} = $new;
98 }
99 4 ▶ return $self->{directive} || '';
Condition100%2/2
$self->{'directive'} || ''
l!l
22
MC/DC100%1/1
$self->{'directive'} || ''
$self->{'directive'}
Truth table100%2/2
A
A= $self->{'directive'}
Aresult
11
00
100 }
101  
102 1 ▶sub value {
Subroutine value: called CC=3 SCAR=12.5
Pod undocumented
103 1 my ($self, $new) = @_;
104  
105 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1 1
106 0 $self->{value} = $new;
107 }
108 1 ▶ return defined $self->{value} ? $self->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'value'} ? : 1 0 1
109 }
110  
111 0 ▶sub as_array {
Subroutine as_array: not called CC=4 SCAR=30.0
Pod documented
112 0 my ($self, $new) = @_;
113  
114 0 my @ret;
115 0 ▶ if ($self->id) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->id) { } 0 0 0
elsif ($self->directive) { } 0 0 0
116 @ret = map
117 0 { $self->$_ }
118 # fields, in order
119 qw(
120 id
121 last_name
122 first_name
123 rank
124 flags
125 club
126 comment
127 );
128 }
129 elsif ($self->directive) {
130 0 @ret = ($self->directive, $self->value);
131 }
132 else {
133 0 @ret = ($self->comment);
134 }
135 0 ▶ return wantarray ? @ret : \@ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
136 }
137  
138 6 ▶sub as_hash {
Subroutine as_hash: called CC=4 SCAR=13.9
Pod documented
139 6 my ($self, $new) = @_;
140  
141 6 my %ret;
142 6 ▶ if ($self->id) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->id) { } 3 3 6
elsif ($self->directive) { } 1 2 3
143 %ret = map
144 3 { $_, $self->$_ }
145 # fields, in order
146 qw(
147 id
148 last_name
149 first_name
150 rank
151 flags
152 club
153 comment
154 );
155 }
156 elsif ($self->directive) {
157 1 %ret = (
158 directive => $self->directive,
159 value => $self->value,
160 );
161 }
162 else {
163 2 %ret = (
164 comment => $self->comment,
165 );
166 }
167 6 ▶ return wantarray ? %ret : \%ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
168 }
169  
170  
171 1 Readonly my $ID => 0;
172 1 Readonly my $LAST_NAME => 1;
173 1 Readonly my $FIRST_NAME => 2;
174 1 Readonly my $FLAGS => 3;
175  
176 1 Readonly my %name_of_state => (
177 $ID => 'ID',
178 $LAST_NAME => 'LAST_NAME',
179 $FIRST_NAME => 'FIRST_NAME',
180 $FLAGS => 'FLAGS',
181 );
182  
183 1 Readonly my %state_functions => (
184 $ID => \&_get_id,
185 $LAST_NAME => \&_get_last_name,
186 $FIRST_NAME => \&_get_first_name,
187 $FLAGS => \&_get_flags,
188 );
189  
190 1 *parse = \&parse_line; # alias parse to parse_line
191 8 ▶sub parse_line {
Subroutine parse_line: called CC=15 SCAR=27.5
Pod documented
192 8 my ($self, $string) = @_;
193  
194 # initialize
195 8 $self->{source} = $string;
196 8 map { delete $self->{$_} } qw(
197 id
198 last_name
199 first_name
200 rank
201 flags
202 club
203 comment
204 directive
205 value
206 comment
207 ); # empty arrays
208  
209 8 ▶ return $self->as_hash if (not $string);
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 8 8
210  
211 8 my $tokenizer = String::Tokenizer->new(
212 $string, # source string
213 #"~!@#\$\%^&*()`={}[]:;\"'<>,?/|\\\n", # delimiters (doesn't include +-.)
214 "#,=\n",
215 String::Tokenizer->RETAIN_WHITESPACE,
216 );
217 8 my $iter = $tokenizer->iterator;
218  
219 8 my $state = $ID; # assume we will see ID first
220  
221 TOKEN:
222 8 ▶ while ($iter->hasNextToken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->hasNextToken) 47 0 47
223 47 my $token = $iter->nextToken;
224 # warn "state $name_of_state{$state}: token=<$token>\n";
225  
226 47 ▶ if ($token eq "\n") { # a carriage return
Branch: 4 branches
BranchTrueFalseTotal
if ($token eq "\n") { } 1 46 47
elsif (not $token =~ /\S/ or $token eq '') { } 16 30 46
elsif ($token eq '#') { } 6 24 30
elsif (exists $state_functions{$state}) { } 24 0 24
227 1 last TOKEN;
228 }
229 ▶ elsif ($token !~ m/\S/ or # only whitespace
Condition66%2/3
not $token =~ /\S/ or $token eq ""
l!l&&r!l&&!r
16030
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
230 $token eq '') { # empty
231 16 next TOKEN;
232 }
233 elsif ($token eq '#') { # comment
234 6 my @remainder;
235 6 ▶ push (@remainder, $iter->nextToken) while $iter->hasNextToken;
Branch: 1 branch
BranchTrueFalseTotal
$iter->hasNextToken and do { push @remainder, $iter->nextToken } 37 6 43
236 6 $token = join('', @remainder);
237 6 $state = $self->_get_comment($token);
238 6 last TOKEN;
239 }
240 elsif (exists $state_functions{$state}) {
241 24 $state = $state_functions{$state}($self, $token);
242 23 next TOKEN;
243 }
244 else {
245 0 $self->_parse_error(
246 error => "Unknown state: $state",
247 source => $token,
248 );
249 }
250 }
251 7 ▶ if (exists $self->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"id"}) 4 3 7
252 4 ▶ $self->{last_name} = join(' ', @{$self->{last_name}} ) if exists $self->{last_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"last_name"} 4 0 4
253 4 ▶ $self->{first_name} = join(' ', @{$self->{first_name}}) if exists $self->{first_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"first_name"} 3 1 4
254 my @missing = grep
255 4 { not $self->{$_} }
256 ( qw( id last_name rank ) );
257 4 ▶ if (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 1 3 4
258 1 $self->_parse_error(
259 error => "missing: @missing",
260 source => $string,
261 );
262 }
263  
264 # transfer dropN from comment to flags
265 3 ▶ if (exists $self->{comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"comment"}) 2 1 3
266 2 ▶ while ($self->{comment} =~ s/\b(drop\d+)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"comment"} =~ s/\b(drop\d+)\s*//) 0 2 2
267 0 push @{$self->{flags}}, $1;
268 }
269 }
270 }
271 6 return $self->as_hash;
272 }
273  
274 5 ▶sub _get_id {
Subroutine _get_id: called CC=2 SCAR=7.2
275 5 my ($self, $token) = @_;
276  
277 5 my $id = normalize_ID($token);
278 5 ▶ if (not is_ID($id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_ID($id)) 0 5 5
279 $self->_parse_error(
280 error => "<$token> is not a valid AGA ID",
281 source => $self->{source},
282 0 );
283 }
284 5 $self->{id} = $id;
285 5 return $LAST_NAME; # next state
286 }
287  
288 10 ▶sub _get_last_name {
Subroutine _get_last_name: called CC=4 SCAR=13.9
289 10 my ($self, $token) = @_;
290  
291 10 ▶ if ($token eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq ",") 4 6 10
292 4 ▶ if (exists $self->{last_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'last_name'}) { } 3 1 4
293 3 return $FIRST_NAME;
294 } else {
295 $self->_parse_error(
296 error => 'missing ID or last name before comma',
297 source => $self->{source},
298 1 );
299 }
300 }
301 6 ▶ if (is_Rank_or_Rating($token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_Rank_or_Rating($token)) 1 5 6
302 1 $self->rank(uc $token);
303 1 return $FLAGS; # either flag or trailing comments is next
304 }
305 5 push(@{$self->{last_name}}, $token);
306 5 return $LAST_NAME;
307 }
308  
309 6 ▶sub _get_first_name {
Subroutine _get_first_name: called CC=2 SCAR=6.9
310 6 my ($self, $token) = @_;
311  
312 6 ▶ if (is_Rank_or_Rating($token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_Rank_or_Rating($token)) 2 4 6
313 2 $self->rank(uc $token);
314 2 return $FLAGS; # either flag or trailing comments is next
315 }
316 4 push(@{$self->{first_name}}, $token);
317 4 return $FIRST_NAME;
318 }
319  
320 3 ▶sub _get_flags {
Subroutine _get_flags: called CC=5 SCAR=16.3
321 3 my ($self, $token) = @_;
322  
323 3 my $flags_ref = $self->{flags};
324 3 ▶ if ($flags_ref and
Branch: 1 branch
BranchTrueFalseTotal
if ($flags_ref and @{$flags_ref;} and $flags_ref->[-1] eq '=') { } 1 2 3
Condition83%5/6
$flags_ref and @{$flags_ref;} and $flags_ref->[-1] eq "="
!ll&&!rl&&r
111
$flags_ref and @{$flags_ref;}
!ll&&!rl&&r
102
MC/DC66%2/3
$flags_ref and @{$flags_ref;} and $flags_ref->[-1] eq "="
$flags_ref @{$flags_ref;} $flags_ref->[-1] eq "="
Truth table75%3/4
A and B and C
A= $flags_refB= @{$flags_ref;}C= $flags_ref->[-1] eq "="
ABCresult
0XX0
10X0
1100
1111
325 2 @{$flags_ref} and
326 ($flags_ref->[-1] eq '=')) { # last entry was '='
327  
328 # Flags formed like Bar = Foo got turned into
329 # three seperate tokens: 'Bar', '=', and 'Foo'.
330 # Combine them into one array element here, and upper-case the
331 # key (i.e: BAR)
332  
333 1 pop @{$flags_ref}; # remove equals sign
334 1 my $key = uc(pop(@{$flags_ref})); # remove key and upper-case it
335 1 ▶ if ($key eq 'CLUB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'CLUB') { } 1 0 1
336 1 $self->{club} = $token; # turn CLUB= flags into the club
337 }
338 else {
339 # concatenate with equal sign and current token
340 0 push @{$flags_ref}, "$key=$token";
341 }
342 }
343 else {
344 2 push( @{$self->{flags}}, $token );
345 }
346 3 return $FLAGS;
347 }
348  
349 # $token is the rest of the line following the #
350 6 ▶sub _get_comment {
Subroutine _get_comment: called CC=2 SCAR=6.9
351 6 my ($self, $token) = @_;
352  
353 6 ▶ if ($token =~ m/\A#\s*(\w+)\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A#\s*(\w+)\s*(.*)/) { } 1 5 6
354 # lines starting with ## are directives
355 1 $self->{directive} = $1;
356 1 $self->{value} = $2;
357 1 $self->{value} =~ s/\s*$//; # trim trailing whitespace
358 }
359 else {
360 5 $self->{comment} = $token;
361 5 $self->{comment} =~ s/\s*$//; # trim trailing whitespace
362 }
363 6 return $ID; # comments go to end of line, wrap back to start
364 }
365  
366 1 1;
367  
368 __END__
« Parse/Round.pm ↑ Summary Parse.pm »