ScottAdams/Parse.pm

filter: Statementstmt 85.7% 174 / 203 Branchbran 72.6% 77 / 106 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 1 totaltotal 81.1% 275 / 339 CC 59 SCAR 23.7
CC
59
Cov
81%
parse
26.5
_parse_item
6.2
_parse_room
5.0
CRAP 10.7
« ScottAdams/Compile.pm ↑ Summary ScottAdams/Action.pm »
1 # $Id: Parse.pm,v 1.3 2006-11-03 21:00:13 mike Exp $
2  
3 # Parse.pm - parsing functions for Scott Adams game files.
4  
5 package Games::ScottAdams::Game;
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Games::ScottAdams::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 5 ▶sub parse {
Subroutine parse: called CC=25 SCAR=32.8
Pod undocumented
12 5 my $this = shift();
13 5 my($filename) = @_;
14  
15 #warn "parsing '$filename'";
16 5 ▶ my $fh = new Games::ScottAdams::File($filename)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "Games::ScottAdams::File"->new($filename) or die "can't open '${filename}': $!" 5 0 5
17 or die "can't open '$filename': $!";
18  
19 5 ▶ while (defined ($_ = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $fh->getline(1))) 252 5 257
20 252 s/^\s+%/%/; # Skip any whitespace before leading %
21 252 ▶ if (/^%room (.*)/i) {
Branch: 21 branches
BranchTrueFalseTotal
if (/^%room (.*)/i) { } 19 233 252
elsif (/^%exit\s+(.*)\s+(.*)/i) { } 30 203 233
elsif (/^%item\s+(.*)/i) { } 33 170 203
elsif (/^%getdrop\s+(.*)/i) { } 14 156 170
elsif (/^%(at|nowhere)\s*(.*)/i) { } 11 145 156
elsif (/^%start\s+(.*)/i) { } 1 144 145
elsif (/^%treasury\s+(.*)/i) { } 1 143 144
elsif (/^%maxload\s+(.*)/i) { } 1 142 143
elsif (/^%lighttime\s+(.*)/i) { } 1 141 142
elsif (/^%ident\s+(.*)/i) { } 1 140 141
elsif (/^%version\s+(.*)/i) { } 1 139 140
elsif (/^%wordlen\s+(.*)/i) { } 1 138 139
elsif (/^%lightsource\s+(.*)/i) { } 0 138 138
elsif (/^%action\s+(.*)\s+(.*)/i) { } 39 99 138
elsif (/^%action\s+(.*)/i) { } 8 91 99
elsif (/^%occur\s*(.*)/i) { } 18 73 91
elsif (/^%result/i) { } 65 8 73
elsif (/^%comment\s+(.*)/i) { } 4 4 8
elsif (/^%([nv])alias\s+(.*)\s+(.*)/i) { } 0 4 4
elsif (/^%include\s+(.*)/i) { } 4 0 4
elsif (not /^%/) { } 0 0 0
22 19 $this->_parse_room($fh, $1);
23 } elsif (/^%exit\s+(.*)\s+(.*)/i) {
24 30 $this->_parse_exit($fh, $1, $2);
25 } elsif (/^%item\s+(.*)/i) {
26 33 $this->_parse_item($fh, $1);
27 } elsif (/^%getdrop\s+(.*)/i) {
28 14 $this->_parse_getdrop($fh, $1);
29 } elsif (/^%(at|nowhere)\s*(.*)/i) {
30 11 $this->_parse_at($fh, $2);
31 } elsif (/^%start\s+(.*)/i) {
32 1 $this->_parse_start($fh, $1);
33 } elsif (/^%treasury\s+(.*)/i) {
34 1 $this->_parse_treasury($fh, $1);
35 } elsif (/^%maxload\s+(.*)/i) {
36 1 $this->_parse_maxload($fh, $1);
37 } elsif (/^%lighttime\s+(.*)/i) {
38 1 $this->_parse_lighttime($fh, $1);
39 } elsif (/^%ident\s+(.*)/i) {
40 1 $this->_parse_ident($fh, $1);
41 } elsif (/^%version\s+(.*)/i) {
42 1 $this->_parse_version($fh, $1);
43 } elsif (/^%wordlen\s+(.*)/i) {
44 1 $this->_parse_wordlen($fh, $1);
45 } elsif (/^%lightsource\s+(.*)/i) {
46 0 $this->_parse_lightsource($fh, $1);
47 } elsif (/^%action\s+(.*)\s+(.*)/i) {
48 39 $this->_parse_action($fh, $1, $2);
49 } elsif (/^%action\s+(.*)/i) {
50 8 $this->_parse_action($fh, $1, undef);
51 } elsif (/^%occur\s*(.*)/i) {
52 18 $this->_parse_action($fh, undef, $1);
53 } elsif (/^%result/i) {
54 65 $this->_parse_result($fh);
55 } elsif (/^%comment\s+(.*)/i) {
56 4 $this->_parse_comment($fh, $1);
57 } elsif (/^%([nv])alias\s+(.*)\s+(.*)/i) {
58 0 $this->_parse_alias($fh, lc($1), $2, $3);
59 } elsif (/^%include\s+(.*)/i) {
60 4 my $newfile = $1;
61 #warn "%include '$newfile'";
62 # Interpret filenames relative to directory of current file
63 4 ▶ if ($filename =~ m@/@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[/]) 4 0 4
64 4 my $prefix = $filename;
65 4 $prefix =~ s@(.*)/.*@$1@;
66 4 $newfile = "$prefix/$newfile";
67 }
68 4 $this->parse($newfile);
69 } elsif (!/^%/) {
70 0 $fh->warn("expected directive, got '$_' (ignored)");
71 } else {
72 0 $fh->warn("unrecognised directive (ignored): '$_'");
73 }
74 }
75  
76 5 $this->_coalesce_aliases();
77 5 return 1;
78 }
79  
80  
81 # PRIVATE to the parse() method
82 19 ▶sub _parse_room {
Subroutine _parse_room: called CC=5 SCAR=16.2
83 19 my $this = shift();
84 19 my($fh, $name) = @_;
85  
86 19 my $desc = '';
87 19 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 43 0 43
88 43 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 19 24 43
89 19 $fh->ungetline($line);
90 19 last;
91 }
92  
93 24 $desc .= "$line\n";
94 }
95  
96 19 my $num = @{ $this->{rooms} }; # 0-based index of room to be added
97 19 my $room = new Games::ScottAdams::Room($name, $desc, $num);
98 19 push @{ $this->{rooms} }, $room;
99 19 ▶ if (defined $this->{roomname}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"roomname"}{$name}) 0 19 19
100 0 $fh->warn("discarding old room '$name'");
101 }
102  
103 19 $this->{roomname}->{$name} = $room;
104 19 $this->{_room} = $room;
105 $this->{_roomname1} = $name
106 19 ▶ if !defined $this->{_roomname1};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $this->{"_roomname1"} 1 18 19
107 }
108  
109  
110 # PRIVATE to the parse() method
111 30 ▶sub _parse_exit {
Subroutine _parse_exit: called CC=4 SCAR=15.9
112 30 my $this = shift();
113 30 my($fh, $dir, $dest) = @_;
114  
115 30 my $room = $this->{_room};
116 30 ▶ if (!defined $room) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $room) 0 30 30
117 0 $fh->warn("ignoring %exit '$dir'->'$dest' before first room");
118 0 return;
119 }
120  
121 30 my $roomname = $room->name();
122 30 $dir = lc(substr($dir, 0, 1));
123 30 ▶ if ($dir !~ /^[nsewud]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir =~ /^[nsewud]$/) 0 30 30
124 0 $fh->warn("ignoring %exit '$dir'->'$dest' at '$roomname'");
125 0 return;
126 }
127  
128 30 my $old = $room->exit($dir);
129 30 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 30 30
130 0 $fh->warn("discarding old exit '$dir'->'$old' at '$roomname'");
131 }
132  
133 30 $room->exit($dir, $dest);
134 }
135  
136  
137 # PRIVATE to the parse() method
138 33 ▶sub _parse_item {
Subroutine _parse_item: called CC=6 SCAR=18.2
139 33 my $this = shift();
140 33 my($fh, $name) = @_;
141  
142 33 my $where = undef; # item is initially nowhere
143 33 my $room = $this->{_room};
144 33 ▶ if (defined $room) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $room) 33 0 33
145 33 $where = $room->name();
146 }
147  
148 33 my $desc = $fh->getline(1);
149 33 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 33 0 33
150 33 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 33 0 33
151 33 $fh->ungetline($line);
152 33 last;
153 }
154  
155 0 $desc .= "\n$line";
156 }
157  
158 33 my $num = @{ $this->{items} }; # 0-based index of item to be added
159 33 if (0) {
160 ### No need to do this, is there?
161 if ($num == 9) {
162 # Leave slot 9 free for the light-source
163 my $nothing = new Games::ScottAdams::Item('', '', $num++);
164 push @{ $this->{items} }, $nothing;
165 }
166 }
167  
168 33 my $item = new Games::ScottAdams::Item($name, $desc, $num, $where);
169 33 push @{ $this->{items} }, $item;
170 33 ▶ if (defined $this->{itemname}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"itemname"}{$name}) 0 33 33
171 0 $fh->warn("discarding old item '$name'");
172 }
173  
174 33 $this->{itemname}->{$name} = $item;
175 33 $this->{_item} = $item;
176 33 ▶ if ($desc =~ /^\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /^\*/) 3 30 33
177 3 $this->{ntreasures}++;
178 }
179 }
180  
181  
182 # PRIVATE to the parse() method
183 14 ▶sub _parse_getdrop {
Subroutine _parse_getdrop: called CC=3 SCAR=11.9
184 14 my $this = shift();
185 14 my($fh, $name) = @_;
186  
187 14 my $item = $this->{_item};
188 14 ▶ if (!defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 0 14 14
189 0 $fh->warn("ignoring %getdrop '$name' before first item");
190 0 return;
191 }
192  
193 14 my $itemname = $item->name();
194 14 my $old = $item->getdrop();
195 14 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 14 14
196 0 $fh->warn("discarding old getdrop '$old' for '$itemname'");
197 }
198  
199 14 $item->getdrop($name);
200 14 $this->_parse_alias($fh, 'n', $name);
201 }
202  
203  
204 # PRIVATE to the parse() method
205 11 ▶sub _parse_at {
Subroutine _parse_at: called CC=4 SCAR=15.5
206 11 my $this = shift();
207 11 my($fh, $where) = @_;
208  
209 11 my $item = $this->{_item};
210 11 ▶ if (!defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 0 11 11
211 0 $fh->warn("ignoring %at '$where' before first item");
212 0 return;
213 }
214  
215 11 my $itemname = $item->name();
216 11 my $old = $item->where();
217 11 ▶ if (defined $old && $where ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old and $where ne "") 0 11 11
Condition33%1/3
defined $old and $where ne ""
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $old and $where ne ""
defined $old $where ne ""
Truth table33%1/3
A and B
A= defined $oldB= $where ne ""
ABresult
0X0
100
111
218 #$fh->warn("replacing location '$old' with '$where' for '$itemname'");
219 }
220  
221 11 $item->where($where);
222 }
223  
224  
225 # All the following wrappers are PRIVATE to the parse() method
226 1 ▶sub _parse_start {
Subroutine _parse_start: called CC=1 SCAR=0.0
227 1 return _parse_param(@_, 'start', 'start room'); }
228 1 ▶sub _parse_treasury {
Subroutine _parse_treasury: called CC=1 SCAR=0.0
229 1 return _parse_param(@_, 'treasury', 'treasury room'); }
230 1 ▶sub _parse_maxload {
Subroutine _parse_maxload: called CC=1 SCAR=0.0
231 1 return _parse_param(@_, 'maxload', 'maximum load'); }
232 1 ▶sub _parse_lighttime {
Subroutine _parse_lighttime: called CC=1 SCAR=0.0
233 1 return _parse_param(@_, 'lighttime', 'light duration'); }
234 1 ▶sub _parse_ident {
Subroutine _parse_ident: called CC=1 SCAR=0.0
235 1 return _parse_param(@_, 'ident', 'adventure identifier'); }
236 1 ▶sub _parse_version {
Subroutine _parse_version: called CC=1 SCAR=0.0
237 1 return _parse_param(@_, 'version', 'version number'); }
238 1 ▶sub _parse_wordlen {
Subroutine _parse_wordlen: called CC=1 SCAR=0.0
239 1 return _parse_param(@_, 'wordlen', 'word length'); }
240 0 ▶sub _parse_lightsource {
Subroutine _parse_lightsource: not called CC=1 SCAR=6.9
241 0 return _parse_param(@_, 'lightsource', 'light source'); }
242  
243  
244 # PRIVATE to the _parse_{start...wordlen}() methods
245 7 ▶sub _parse_param {
Subroutine _parse_param: called CC=2 SCAR=7.4
246 7 my $this = shift();
247 7 my($fh, $value, $param, $caption) = @_;
248  
249 7 ▶ if (defined $this->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{$param}) 0 7 7
250 0 $fh->warn("discarding old $caption '", $this->{$param}, "'");
251 }
252  
253 7 $this->{$param} = $value;
254 }
255  
256  
257 # PRIVATE to the parse() method
258 65 ▶sub _parse_action {
Subroutine _parse_action: called CC=5 SCAR=16.1
259 65 my $this = shift();
260 65 my($fh, $verb, $noun) = @_;
261  
262 65 my $num = @{ $this->{actions} }; # 0-based index of action to be added
263 65 my $action = new Games::ScottAdams::Action($verb, $noun, $num);
264 65 push @{ $this->{actions} }, $action;
265 65 $this->{_action} = $action;
266  
267 # Register noun and verb
268 65 ▶ $this->_parse_alias($fh, 'v', $verb) if $verb;
Branch: 1 branch
BranchTrueFalseTotal
if $verb 47 18 65
269 65 ▶ $this->_parse_alias($fh, 'n', $noun) if $noun;
Branch: 1 branch
BranchTrueFalseTotal
if $noun 39 26 65
270  
271 65 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 124 0 124
272 124 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 65 59 124
273 65 $fh->ungetline($line);
274 65 last;
275 }
276 59 $action->add_cond($line);
277 }
278  
279 65 return;
280 }
281  
282  
283 # PRIVATE to the parse() method
284 65 ▶sub _parse_result {
Subroutine _parse_result: called CC=4 SCAR=14.1
285 65 my $this = shift();
286 65 my($fh) = @_;
287  
288 65 my $action = $this->{_action};
289 65 ▶ if (!defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 0 65 65
290 0 $fh->warn("ignoring %result before first action");
291 0 return;
292 }
293  
294 65 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 185 1 186
295 185 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 64 121 185
296 64 $fh->ungetline($line);
297 64 last;
298 }
299 121 $action->add_result($line);
300 }
301 }
302  
303  
304 # PRIVATE to the parse() method
305 4 ▶sub _parse_comment {
Subroutine _parse_comment: called CC=3 SCAR=12.3
306 4 my $this = shift();
307 4 my($fh, $comment) = @_;
308  
309 4 my $action = $this->{_action};
310 4 ▶ if (!defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 0 4 4
311 0 $fh->warn("ignoring %comment before first %action");
312 0 return;
313 }
314  
315 4 my $old = $action->comment();
316 4 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 4 4
317 0 $fh->warn("discarding old comment '$old'");
318 }
319  
320 4 $action->comment($comment);
321 }
322  
323  
324 # PRIVATE to the parse() method
325 100 ▶sub _parse_alias {
Subroutine _parse_alias: called CC=3 SCAR=11.0
326 100 my $this = shift();
327 100 my $fh = shift();
328 100 my $type = lc(shift());
329 100 my @words = map { uc() } @_;
330  
331 100 my $href = $this->{$type . 'vocab'};
332 100 for my $word (@words) {
333 100 ▶ $fh->fatal("empty word") if !$word;
Branch: 1 branch
BranchTrueFalseTotal
unless $word 0 100 100
334 100 push @{ $href->{$word} }, grep { $_ ne $word } @words;
335 }
336 }
337  
338  
339 # PRIVATE to the parse() method
340 #
341 # At this point, we have a bunch of equivalence classes for each
342 # vocabulary set, we we need to coalesce them. For example, if we
343 # have the following in the source --
344 # %valias enter go
345 # %valias run go
346 # %valias walk go
347 # We'll have a {vvocab} hash that looks like this --
348 # RUN -> GO
349 # GO -> ENTER,RUN,WALK
350 # ENTER -> GO
351 # WALK -> GO
352 # But we want a single list of all four words
353  
354 5 ▶sub _coalesce_aliases {
Subroutine _coalesce_aliases: called CC=1 SCAR=0.0
355 5 my $this = shift();
356  
357 5 $this->{vvocab} = _extend_lists($this->{vvocab});
358 5 $this->{nvocab} = _extend_lists($this->{nvocab});
359 }
360  
361 # PRIVATE to the _coalesce_aliases() method
362 10 ▶sub _extend_lists {
Subroutine _extend_lists: called CC=4 SCAR=14.1
363 10 my($vocab) = @_;
364  
365 10 my @keys = keys %$vocab; # we're going to change this
366 10 foreach my $key (@keys) {
367 #warn "considering aliases for '$key'";
368 89 ▶ next if !exists $vocab->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $vocab->{$key} 0 89 89
369 #warn "thinking about '$key'";
370 89 my @list = _equivalents($vocab, $key, { $key => 1});
371 #warn "\t" . join(' ', @list);
372 89 foreach my $used (@list) {
373 0 delete $vocab->{$used};
374 #warn "deleted '$used'";
375 }
376 89 $vocab->{$key} = [ @list ];
377 }
378  
379 10 return $vocab;
380 }
381  
382 # PRIVATE to the _extend_lists method()
383 89 ▶sub _equivalents {
Subroutine _equivalents: called CC=3 SCAR=15.0
384 89 my($vocab, $word, $seen) = @_;
385  
386 89 my @equivalents;
387 89 my $cref = $vocab->{$word};
388 89 foreach my $candidate (@$cref) {
389 0 ▶ next if exists $seen->{$candidate};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen->{$candidate} 0 0 0
390 0 $seen->{$candidate} = 1;
391 0 my @sub = _equivalents($vocab, $candidate, $seen);
392 0 push @equivalents, $candidate, @sub;
393 }
394  
395 89 return @equivalents;
396 }
397  
398  
399 1 1;
« ScottAdams/Compile.pm ↑ Summary ScottAdams/Action.pm »