ScottAdams/Parse.pm

filter: Statementstmt 5.4% 11 / 203 Branchbran 0.9% 1 / 106 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 12.5% 3 / 24 Podpod 0.0% 0 / 1 totaltotal 4.4% 15 / 339 CC 59 SCAR 51.8
CC
59
Cov
4%
parse
543.7
_parse_item
42.0
_parse_room
30.0
CRAP 178.5
« 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 sub parse {
12 1 ▶ my $this = shift();
Subroutine parse: called CC=25 SCAR=63.0
Pod undocumented
13 1 my($filename) = @_;
14  
15 #warn "parsing '$filename'";
16 1 ▶ my $fh = new Games::ScottAdams::File($filename)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "Games::ScottAdams::File"->new($filename) or die "can't open '${filename}': $!" 0 1 1
17 or die "can't open '$filename': $!";
18  
19 1 ▶ while (defined ($_ = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $fh->getline(1))) 0 0 0
20 0 s/^\s+%/%/; # Skip any whitespace before leading %
21 0 ▶ if (/^%room (.*)/i) {
Branch: 21 branches
BranchTrueFalseTotal
if (/^%room (.*)/i) { } 0 0 0
elsif (/^%exit\s+(.*)\s+(.*)/i) { } 0 0 0
elsif (/^%item\s+(.*)/i) { } 0 0 0
elsif (/^%getdrop\s+(.*)/i) { } 0 0 0
elsif (/^%(at|nowhere)\s*(.*)/i) { } 0 0 0
elsif (/^%start\s+(.*)/i) { } 0 0 0
elsif (/^%treasury\s+(.*)/i) { } 0 0 0
elsif (/^%maxload\s+(.*)/i) { } 0 0 0
elsif (/^%lighttime\s+(.*)/i) { } 0 0 0
elsif (/^%ident\s+(.*)/i) { } 0 0 0
elsif (/^%version\s+(.*)/i) { } 0 0 0
elsif (/^%wordlen\s+(.*)/i) { } 0 0 0
elsif (/^%lightsource\s+(.*)/i) { } 0 0 0
elsif (/^%action\s+(.*)\s+(.*)/i) { } 0 0 0
elsif (/^%action\s+(.*)/i) { } 0 0 0
elsif (/^%occur\s*(.*)/i) { } 0 0 0
elsif (/^%result/i) { } 0 0 0
elsif (/^%comment\s+(.*)/i) { } 0 0 0
elsif (/^%([nv])alias\s+(.*)\s+(.*)/i) { } 0 0 0
elsif (/^%include\s+(.*)/i) { } 0 0 0
elsif (not /^%/) { } 0 0 0
22 0 $this->_parse_room($fh, $1);
23 } elsif (/^%exit\s+(.*)\s+(.*)/i) {
24 0 $this->_parse_exit($fh, $1, $2);
25 } elsif (/^%item\s+(.*)/i) {
26 0 $this->_parse_item($fh, $1);
27 } elsif (/^%getdrop\s+(.*)/i) {
28 0 $this->_parse_getdrop($fh, $1);
29 } elsif (/^%(at|nowhere)\s*(.*)/i) {
30 0 $this->_parse_at($fh, $2);
31 } elsif (/^%start\s+(.*)/i) {
32 0 $this->_parse_start($fh, $1);
33 } elsif (/^%treasury\s+(.*)/i) {
34 0 $this->_parse_treasury($fh, $1);
35 } elsif (/^%maxload\s+(.*)/i) {
36 0 $this->_parse_maxload($fh, $1);
37 } elsif (/^%lighttime\s+(.*)/i) {
38 0 $this->_parse_lighttime($fh, $1);
39 } elsif (/^%ident\s+(.*)/i) {
40 0 $this->_parse_ident($fh, $1);
41 } elsif (/^%version\s+(.*)/i) {
42 0 $this->_parse_version($fh, $1);
43 } elsif (/^%wordlen\s+(.*)/i) {
44 0 $this->_parse_wordlen($fh, $1);
45 } elsif (/^%lightsource\s+(.*)/i) {
46 0 $this->_parse_lightsource($fh, $1);
47 } elsif (/^%action\s+(.*)\s+(.*)/i) {
48 0 $this->_parse_action($fh, $1, $2);
49 } elsif (/^%action\s+(.*)/i) {
50 0 $this->_parse_action($fh, $1, undef);
51 } elsif (/^%occur\s*(.*)/i) {
52 0 $this->_parse_action($fh, undef, $1);
53 } elsif (/^%result/i) {
54 0 $this->_parse_result($fh);
55 } elsif (/^%comment\s+(.*)/i) {
56 0 $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 0 my $newfile = $1;
61 #warn "%include '$newfile'";
62 # Interpret filenames relative to directory of current file
63 0 ▶ if ($filename =~ m@/@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[/]) 0 0 0
64 0 my $prefix = $filename;
65 0 $prefix =~ s@(.*)/.*@$1@;
66 0 $newfile = "$prefix/$newfile";
67 }
68 0 $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 0 $this->_coalesce_aliases();
77 0 return 1;
78 }
79  
80  
81 # PRIVATE to the parse() method
82 sub _parse_room {
83 0 ▶ my $this = shift();
Subroutine _parse_room: not called CC=5 SCAR=34.0
84 0 my($fh, $name) = @_;
85  
86 0 my $desc = '';
87 0 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 0 0 0
88 0 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 0 0 0
89 0 $fh->ungetline($line);
90 0 last;
91 }
92  
93 0 $desc .= "$line\n";
94 }
95  
96 0 my $num = @{ $this->{rooms} }; # 0-based index of room to be added
97 0 my $room = new Games::ScottAdams::Room($name, $desc, $num);
98 0 push @{ $this->{rooms} }, $room;
99 0 ▶ if (defined $this->{roomname}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"roomname"}{$name}) 0 0 0
100 0 $fh->warn("discarding old room '$name'");
101 }
102  
103 0 $this->{roomname}->{$name} = $room;
104 0 $this->{_room} = $room;
105 $this->{_roomname1} = $name
106 0 ▶ if !defined $this->{_roomname1};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $this->{"_roomname1"} 0 0 0
107 }
108  
109  
110 # PRIVATE to the parse() method
111 sub _parse_exit {
112 0 ▶ my $this = shift();
Subroutine _parse_exit: not called CC=4 SCAR=30.0
113 0 my($fh, $dir, $dest) = @_;
114  
115 0 my $room = $this->{_room};
116 0 ▶ if (!defined $room) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $room) 0 0 0
117 0 $fh->warn("ignoring %exit '$dir'->'$dest' before first room");
118 0 return;
119 }
120  
121 0 my $roomname = $room->name();
122 0 $dir = lc(substr($dir, 0, 1));
123 0 ▶ if ($dir !~ /^[nsewud]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir =~ /^[nsewud]$/) 0 0 0
124 0 $fh->warn("ignoring %exit '$dir'->'$dest' at '$roomname'");
125 0 return;
126 }
127  
128 0 my $old = $room->exit($dir);
129 0 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 0 0
130 0 $fh->warn("discarding old exit '$dir'->'$old' at '$roomname'");
131 }
132  
133 0 $room->exit($dir, $dest);
134 }
135  
136  
137 # PRIVATE to the parse() method
138 sub _parse_item {
139 0 ▶ my $this = shift();
Subroutine _parse_item: not called CC=6 SCAR=37.4
140 0 my($fh, $name) = @_;
141  
142 0 my $where = undef; # item is initially nowhere
143 0 my $room = $this->{_room};
144 0 ▶ if (defined $room) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $room) 0 0 0
145 0 $where = $room->name();
146 }
147  
148 0 my $desc = $fh->getline(1);
149 0 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 0 0 0
150 0 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 0 0 0
151 0 $fh->ungetline($line);
152 0 last;
153 }
154  
155 0 $desc .= "\n$line";
156 }
157  
158 0 my $num = @{ $this->{items} }; # 0-based index of item to be added
159 0 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 0 my $item = new Games::ScottAdams::Item($name, $desc, $num, $where);
169 0 push @{ $this->{items} }, $item;
170 0 ▶ if (defined $this->{itemname}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"itemname"}{$name}) 0 0 0
171 0 $fh->warn("discarding old item '$name'");
172 }
173  
174 0 $this->{itemname}->{$name} = $item;
175 0 $this->{_item} = $item;
176 0 ▶ if ($desc =~ /^\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /^\*/) 0 0 0
177 0 $this->{ntreasures}++;
178 }
179 }
180  
181  
182 # PRIVATE to the parse() method
183 sub _parse_getdrop {
184 0 ▶ my $this = shift();
Subroutine _parse_getdrop: not called CC=3 SCAR=24.8
185 0 my($fh, $name) = @_;
186  
187 0 my $item = $this->{_item};
188 0 ▶ if (!defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 0 0 0
189 0 $fh->warn("ignoring %getdrop '$name' before first item");
190 0 return;
191 }
192  
193 0 my $itemname = $item->name();
194 0 my $old = $item->getdrop();
195 0 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 0 0
196 0 $fh->warn("discarding old getdrop '$old' for '$itemname'");
197 }
198  
199 0 $item->getdrop($name);
200 0 $this->_parse_alias($fh, 'n', $name);
201 }
202  
203  
204 # PRIVATE to the parse() method
205 sub _parse_at {
206 0 ▶ my $this = shift();
Subroutine _parse_at: not called CC=4 SCAR=30.0
207 0 my($fh, $where) = @_;
208  
209 0 my $item = $this->{_item};
210 0 ▶ if (!defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 0 0 0
211 0 $fh->warn("ignoring %at '$where' before first item");
212 0 return;
213 }
214  
215 0 my $itemname = $item->name();
216 0 my $old = $item->where();
217 0 ▶ if (defined $old && $where ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old and $where ne "") 0 0 0
Condition0%0/3
defined $old and $where ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $old and $where ne ""
defined $old $where ne ""
Truth table0%0/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 0 $item->where($where);
222 }
223  
224  
225 # All the following wrappers are PRIVATE to the parse() method
226 sub _parse_start {
227 0 ▶ return _parse_param(@_, 'start', 'start room'); }
Subroutine _parse_start: not called CC=1 SCAR=6.9
228 sub _parse_treasury {
229 0 ▶ return _parse_param(@_, 'treasury', 'treasury room'); }
Subroutine _parse_treasury: not called CC=1 SCAR=6.9
230 sub _parse_maxload {
231 0 ▶ return _parse_param(@_, 'maxload', 'maximum load'); }
Subroutine _parse_maxload: not called CC=1 SCAR=6.9
232 sub _parse_lighttime {
233 0 ▶ return _parse_param(@_, 'lighttime', 'light duration'); }
Subroutine _parse_lighttime: not called CC=1 SCAR=6.9
234 sub _parse_ident {
235 0 ▶ return _parse_param(@_, 'ident', 'adventure identifier'); }
Subroutine _parse_ident: not called CC=1 SCAR=6.9
236 sub _parse_version {
237 0 ▶ return _parse_param(@_, 'version', 'version number'); }
Subroutine _parse_version: not called CC=1 SCAR=6.9
238 sub _parse_wordlen {
239 0 ▶ return _parse_param(@_, 'wordlen', 'word length'); }
Subroutine _parse_wordlen: not called CC=1 SCAR=6.9
240 sub _parse_lightsource {
241 0 ▶ return _parse_param(@_, 'lightsource', 'light source'); }
Subroutine _parse_lightsource: not called CC=1 SCAR=6.9
242  
243  
244 # PRIVATE to the _parse_{start...wordlen}() methods
245 sub _parse_param {
246 0 ▶ my $this = shift();
Subroutine _parse_param: not called CC=2 SCAR=17.9
247 0 my($fh, $value, $param, $caption) = @_;
248  
249 0 ▶ if (defined $this->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{$param}) 0 0 0
250 0 $fh->warn("discarding old $caption '", $this->{$param}, "'");
251 }
252  
253 0 $this->{$param} = $value;
254 }
255  
256  
257 # PRIVATE to the parse() method
258 sub _parse_action {
259 0 ▶ my $this = shift();
Subroutine _parse_action: not called CC=5 SCAR=34.0
260 0 my($fh, $verb, $noun) = @_;
261  
262 0 my $num = @{ $this->{actions} }; # 0-based index of action to be added
263 0 my $action = new Games::ScottAdams::Action($verb, $noun, $num);
264 0 push @{ $this->{actions} }, $action;
265 0 $this->{_action} = $action;
266  
267 # Register noun and verb
268 0 ▶ $this->_parse_alias($fh, 'v', $verb) if $verb;
Branch: 1 branch
BranchTrueFalseTotal
if $verb 0 0 0
269 0 ▶ $this->_parse_alias($fh, 'n', $noun) if $noun;
Branch: 1 branch
BranchTrueFalseTotal
if $noun 0 0 0
270  
271 0 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 0 0 0
272 0 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 0 0 0
273 0 $fh->ungetline($line);
274 0 last;
275 }
276 0 $action->add_cond($line);
277 }
278  
279 0 return;
280 }
281  
282  
283 # PRIVATE to the parse() method
284 sub _parse_result {
285 0 ▶ my $this = shift();
Subroutine _parse_result: not called CC=4 SCAR=30.0
286 0 my($fh) = @_;
287  
288 0 my $action = $this->{_action};
289 0 ▶ if (!defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 0 0 0
290 0 $fh->warn("ignoring %result before first action");
291 0 return;
292 }
293  
294 0 ▶ while (defined (my $line = $fh->getline(1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline(1))) 0 0 0
295 0 ▶ if ($line =~ /^\s*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*%/) 0 0 0
296 0 $fh->ungetline($line);
297 0 last;
298 }
299 0 $action->add_result($line);
300 }
301 }
302  
303  
304 # PRIVATE to the parse() method
305 sub _parse_comment {
306 0 ▶ my $this = shift();
Subroutine _parse_comment: not called CC=3 SCAR=24.8
307 0 my($fh, $comment) = @_;
308  
309 0 my $action = $this->{_action};
310 0 ▶ if (!defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 0 0 0
311 0 $fh->warn("ignoring %comment before first %action");
312 0 return;
313 }
314  
315 0 my $old = $action->comment();
316 0 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 0 0 0
317 0 $fh->warn("discarding old comment '$old'");
318 }
319  
320 0 $action->comment($comment);
321 }
322  
323  
324 # PRIVATE to the parse() method
325 sub _parse_alias {
326 0 ▶ my $this = shift();
Subroutine _parse_alias: not called CC=3 SCAR=24.8
327 0 my $fh = shift();
328 0 my $type = lc(shift());
329 0 my @words = map { uc() } @_;
330  
331 0 my $href = $this->{$type . 'vocab'};
332 0 for my $word (@words) {
333 0 ▶ $fh->fatal("empty word") if !$word;
Branch: 1 branch
BranchTrueFalseTotal
unless $word 0 0 0
334 0 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 sub _coalesce_aliases {
355 0 ▶ my $this = shift();
Subroutine _coalesce_aliases: not called CC=1 SCAR=6.9
356  
357 0 $this->{vvocab} = _extend_lists($this->{vvocab});
358 0 $this->{nvocab} = _extend_lists($this->{nvocab});
359 }
360  
361 # PRIVATE to the _coalesce_aliases() method
362 sub _extend_lists {
363 0 ▶ my($vocab) = @_;
Subroutine _extend_lists: not called CC=4 SCAR=30.0
364  
365 0 my @keys = keys %$vocab; # we're going to change this
366 0 foreach my $key (@keys) {
367 #warn "considering aliases for '$key'";
368 0 ▶ next if !exists $vocab->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $vocab->{$key} 0 0 0
369 #warn "thinking about '$key'";
370 0 my @list = _equivalents($vocab, $key, { $key => 1});
371 #warn "\t" . join(' ', @list);
372 0 foreach my $used (@list) {
373 0 delete $vocab->{$used};
374 #warn "deleted '$used'";
375 }
376 0 $vocab->{$key} = [ @list ];
377 }
378  
379 0 return $vocab;
380 }
381  
382 # PRIVATE to the _extend_lists method()
383 sub _equivalents {
384 0 ▶ my($vocab, $word, $seen) = @_;
Subroutine _equivalents: not called CC=3 SCAR=24.8
385  
386 0 my @equivalents;
387 0 my $cref = $vocab->{$word};
388 0 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 0 return @equivalents;
396 }
397  
398  
399 1 1;
« ScottAdams/Compile.pm ↑ Summary ScottAdams/Action.pm »