blib/lib/Data/Stag/BaseGenerator.pm

filter: Statementstmt 42.7% 76 / 178 Branchbran 29.0% 18 / 62 Conditioncond 22.2% 6 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 41.6% 15 / 36 Podpod 30.0% 9 / 30 totaltotal 35.5% 124 / 349 CC 42 SCAR 24.4
CC
42
Cov
37%
handler
36.8
parse_err
30.0
errlist
20.0
CRAP 11.5
« blib/lib/Data/Stag/GraphHandler.pm ↑ Summary blib/script/stag-splitter.pl »
1 # $Id: BaseGenerator.pm,v 1.15 2004/12/21 02:26:25 cmungall Exp $
2 #
3 # Copyright (C) 2002 Chris Mungall <cjm@fruitfly.org>
4 #
5 # See also - http://stag.sourceforge.net
6 #
7 # This module is free software.
8 # You may distribute this module under the same terms as perl itself
9  
10 package Data::Stag::BaseGenerator;
11  
12 =head1 NAME
13  
14 Data::Stag::BaseGenerator - base class for parsers and other event generators
15  
16 =head1 SYNOPSIS
17  
18 # writing the parser
19 package MyParser;
20 use base qw(Data::Stag::BaseGenerator);
21
22 sub parse_fh {
23 my ($self, $fh) = shift;
24  
25 my $lnum = 0;
26 $self->start_event('data');
27 while (<$fh>) {
28 ++$lnum;
29 $self->line_no($lnum);
30 # do stuff
31 $self->start_event('foo');
32  
33 # ...
34 $self->event(blah=>5);
35  
36 #
37 if (/incorrect_line/) {
38 $self->parse_err('line not in correct format');
39 }
40  
41 # ...
42 $self->end_event('foo');
43 }
44 $self->pop_stack_to_depth(0);
45 }
46 1;
47  
48 # using the parser
49 my $p = MyParser->new;
50 my $h = MyHandler->new; # see Data::Stag::BaseHandler
51 my $eh = Data::Stag->makehandler;
52 $p->handler($h);
53 $p->errhandler($eh);
54 $p->parse($file);
55  
56 # result tree
57 print $h->stag->xml;
58  
59 # write parse errs on standard err
60 printf \*STDERR $p->errhandler->stag->xml;
61  
62 # using the parser from the command line
63 unix> stag-parse.pl -p MyParser -w xml -e err.xml > out.xml
64  
65 # using the parser from the command line via intermediate handler
66 unix> stag-handle.pl -p MyParser -m MyHandler -w xml -e err.xml > out.xml
67  
68 =cut
69  
70 =head1 DESCRIPTION
71  
72 This is the base class for all parsers and event generators
73  
74 parsers/generators take some input (usually a filehandle, but a
75 generator could be a socket listener, for example) and fire stag
76 events
77  
78 stag events are
79  
80 =over
81  
82 =item start_event NODENAME
83  
84 =item evbody DATA
85  
86 =item end_event NODENAME {optional}
87  
88 =item event NODENAME DATA
89  
90 =back
91  
92 These events can be nested/hierarchical
93  
94 If uncaught, these events are stacked into a stag tree, which can be
95 written as xml or one of the other stag formats
96  
97 specialised handlers can be written to catch the events your parser
98 throws
99  
100 For example, you may wish to write a pod parser that generates nested
101 events like this:
102  
103 <pod>
104 <section>
105 <type>head1</type>
106 <name>NAME</name>
107 <text>Data::Stag - Structured Tags datastructures</text>
108 </section>
109 ...
110 </pod>
111  
112 (see the source for Data::Stag::PodParser for details)
113  
114 You can write handlers that take the pod-xml and generate something -
115 for example HTML
116  
117 parsers may encounter unexpected things along the way - they may throw
118 an exception, and fall over - or they may choose to fire an error
119 event. by default, error event streams are diverted to STDERR. You can
120 create your own error handlers
121  
122 =head1 PUBLIC METHODS
123  
124 =head3 new
125  
126 Title: new
127  
128 Args:
129 Return: L<Data::Stag::BaseGenerator>
130 Example:
131  
132 CONSTRUCTOR
133  
134 =head3 handler
135  
136 Title: handler
137 Function: GET/SET ACCESSOR METHOD
138 Args: handler L<Data::Stag::BaseHandler> optional
139 Return: L<Data::Stag::BaseHandler>
140 Example: $p->handler(MyHandler->new);
141  
142 each parser has a handler - all events generated are passed onto the
143 handler; the default handler simply sits there collecting events
144  
145 =head3 errhandler
146  
147 Title: errhandler
148 Function: GET/SET ACCESSOR METHOD
149 Args: handler L<Data::Stag::BaseHandler> optional
150 Return: L<Data::Stag::BaseHandler>
151 Example: $p->errhandler(Data::Stag->makehandler);
152  
153 each parser has an error handler - if the parser encounters things it
154 does not expect, it can pass errors to the errorhandler
155  
156 if no errorhandler is set, an XML event handler that writes to STDERR is used
157  
158 =head3 cache_errors
159  
160 Title: cache_errors
161 Args:
162 Return:
163 Example: $p->cache_errors
164  
165 If this is called, all errors will be cached rather than written to STDERR
166  
167 The error list can be accessed like this
168  
169 $p->parse($fn);
170 @errs = $p->errhandler->stag->get_error;
171  
172 =head2 parse
173  
174 Example - $parser->parse($file1, $file2);
175 Returns -
176 Args - filenames str-LIST
177  
178 parses a file
179  
180 =head2 parse
181  
182 Example - $parser->parse_fh($fh)
183 Returns -
184 Args - fh FILEHANDLE
185  
186 parses an open filehandle
187  
188 =cut
189  
190 =head1 PROTECTED METHODS
191  
192 These methods are only of interest if you are making your own
193 parser/generator class
194  
195 =over
196  
197 =item start_event NODENAME
198  
199 =item evbody DATA
200  
201 =item end_event NODENAME {optional}
202  
203 =item event NODENAME DATA
204  
205 =back
206  
207 =head1 SEE ALSO
208  
209 L<Data::Stag>
210 L<Data::Stag::BaseHandler>
211  
212 =cut
213  
214 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 11 @ISA = qw(Exporter);
216  
217 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 11 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 11 ▶use Data::Stag::Util qw(rearrange);
Subroutine BEGIN: called CC=1 SCAR=0.0
220 11 ▶use Data::Stag::null;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 11 ▶use strict qw(subs vars refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
222  
223 # Exceptions
224  
225 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
226 0 my $self = shift;
227 0 confess("@_");
228 }
229  
230 0 ▶sub warn {
Subroutine warn: not called CC=1 SCAR=6.9
Pod undocumented
231 0 my $self = shift;
232 0 warn("@_");
233 }
234  
235 #sub last_evcall_type {
236 # my $self = shift;
237 # $self->{_last_evcall_type} = shift if @_;
238 # return $self->{_last_evcall_type};
239 #}
240  
241  
242 0 ▶sub stack {
Subroutine stack: not called CC=3 SCAR=24.8
Pod undocumented
243 0 my $self = shift;
244 0 ▶ $self->{_stack} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
245 0 ▶ $self->{_stack} = [] unless $self->{_stack};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_stack"} 0 0 0
246 0 return $self->{_stack};
247 }
248  
249 0 ▶sub stack_top {
Subroutine stack_top: not called CC=1 SCAR=6.9
Pod undocumented
250 0 my $self = shift;
251 0 $self->stack->[-1];
252 }
253  
254 0 ▶sub push_stack {
Subroutine push_stack: not called CC=1 SCAR=6.9
Pod undocumented
255 0 my $self = shift;
256 0 push(@{$self->stack}, @_);
257 }
258  
259 0 ▶sub pop_stack {
Subroutine pop_stack: not called CC=1 SCAR=6.9
Pod undocumented
260 0 my $self = shift;
261 0 my $top = $self->stack_top;
262 0 $self->end_event($top);
263 0 $top;
264
265 }
266  
267 0 ▶sub pop_stack_to_depth {
Subroutine pop_stack_to_depth: not called CC=2 SCAR=17.9
Pod undocumented
268 0 my $self = shift;
269 0 my $depth = shift;
270 0 ▶ while ($depth < $self->stack_depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < $self->stack_depth) 0 0 0
271 0 $self->end_event;
272 }
273 }
274  
275 0 ▶sub pop_all {
Subroutine pop_all: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my $self = shift;
277 0 $self->pop_stack_to_depth(0);
278 }
279  
280 0 ▶sub stack_depth {
Subroutine stack_depth: not called CC=1 SCAR=6.9
Pod undocumented
281 0 my $self = shift;
282 0 scalar(@{$self->stack});
283 }
284  
285 11 *error_list = \&messages;
286  
287 0 ▶sub message {
Subroutine message: not called CC=2 SCAR=17.9
Pod undocumented
288 0 my $self = shift;
289 0 my $msg = shift;
290 0 ▶ unless (ref($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $msg) 0 0 0
291 0 $msg =
292 {msg=>$msg,
293 line=>$self->line,
294 line_no=>$self->line_no,
295 file=>$self->file};
296 }
297 0 push(@{$self->messages},
298 $msg);
299 }
300  
301  
302  
303 17 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
304 17 my ($class, $init_h) = @_;
305 17 my $self = {};
306 17 $self->{handler} = Data::Stag::null->new;
307 17 ▶ if ($init_h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init_h) 0 17 17
308 0 map {$self->{$_} = $init_h->{$_}} keys %$init_h;
309 }
310 17 bless $self, $class;
311 17 ▶ $self->init if $self->can("init");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("init") 0 17 17
312 17 $self;
313 }
314  
315 17 ▶sub load_module {
Subroutine load_module: called CC=2 SCAR=7.0
Pod undocumented
316  
317 17 my $self = shift;
318 17 my $classname = shift;
319 17 my $mod = $classname;
320 17 $mod =~ s/::/\//g;
321  
322 17 ▶ if ($main::{"_<$mod.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::main::{"_<$mod.pm"}) { } 0 17 17
323 }
324 else {
325 17 require "$mod.pm";
326 }
327 }
328  
329 0 ▶sub modulemap {
Subroutine modulemap: not called CC=2 SCAR=17.9
Pod undocumented
330 0 my $self = shift;
331 0 ▶ $self->{_modulemap} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
332 0 return $self->{_modulemap};
333 }
334  
335 17 ▶sub handler {
Subroutine handler: called CC=9 SCAR=36.1
Pod documented
336 17 my $self = shift;
337 17 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 0 17
338 17 my $h = shift;
339 17 ▶ if ($h && !ref($h)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h and not ref $h) 0 17 17
Condition33%1/3
$h and not ref $h
!ll&&!rl&&r
0170
MC/DC0%0/2
$h and not ref $h
$h not ref $h
Truth table33%1/3
A and not(B)
A= $hB= not ref $h
ABresult
0X0
100
111
340 0 my $base = "Data::Stag:";
341 0 my $mm = $self->modulemap;
342 0 ▶ if ($mm && $mm->{$h}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mm and $mm->{$h}) 0 0 0
Condition0%0/3
$mm and $mm->{$h}
!ll&&!rl&&r
000
MC/DC0%0/2
$mm and $mm->{$h}
$mm $mm->{$h}
Truth table0%0/3
A and B
A= $mmB= $mm->{$h}
ABresult
0X0
100
111
343 0 $h = $mm->{$h};
344 }
345 0 $h =~ s/^xml$/$base:XMLWriter/;
346 0 $h =~ s/^perl$/$base:PerlWriter/;
347 0 $h =~ s/^sxpr$/$base:SxprWriter/;
348 0 $h =~ s/^itext$/$base:ITextWriter/;
349 0 $h =~ s/^graph$/$base:GraphWriter/;
350 0 $self->load_module($h);
351 0 $h = $h->new;
352 0 ▶ if ($h->can("fh")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->can("fh")) 0 0 0
353 0 $h->fh(\*STDOUT);
354 }
355 }
356 17 $self->{_handler} = $h;
357 17 ▶ if (!$h->errhandler && $self->errhandler) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $h->errhandler and $self->errhandler) 0 17 17
Condition33%1/3
not $h->errhandler and $self->errhandler
!ll&&!rl&&r
0170
MC/DC0%0/2
not $h->errhandler and $self->errhandler
not $h->errhandler $self->errhandler
Truth table33%1/3
not(A) and B
A= not $h->errhandlerB= $self->errhandler
ABresult
0X0
100
111
358 0 $h->errhandler($self->errhandler);
359 }
360 }
361 # return $self->{_handler} || Data::Stag::null->new();
362 17 return $self->{_handler};
363 }
364  
365 0 ▶sub cache_errors {
Subroutine cache_errors: not called CC=1 SCAR=6.9
Pod documented
366 0 my $self = shift;
367 0 return $self->errhandler(Data::Stag->makehandler);
368 }
369  
370 34 ▶sub errhandler {
Subroutine errhandler: called CC=2 SCAR=6.9
Pod documented
371 34 my $self = shift;
372 34 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 17 34
373 17 $self->{errhandler} = shift;
374 }
375 34 return $self->{errhandler};
376 }
377  
378 0 ▶sub err_event {
Subroutine err_event: not called CC=3 SCAR=24.8
Pod undocumented
379 0 my $self = shift;
380 0 ▶ if (!$self->errhandler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errhandler) 0 0 0
381 0 $self->errhandler(Data::Stag->getformathandler('xml'));
382 0 $self->errhandler->fh(\*STDERR);
383
384 # my $estag = Data::Stag->new(@_);
385 # eval {
386 # confess;
387 # };
388 # $estag->set_stacktrace($@);
389 # print STDERR $estag->xml;
390 # exit 1;
391 }
392 0 ▶ if (!$self->errhandler->depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errhandler->depth) 0 0 0
393 0 $self->errhandler->start_event("error_eventset");
394 }
395 0 $self->errhandler->event(@_);
396 0 return;
397 }
398  
399 0 ▶sub err {
Subroutine err: not called CC=2 SCAR=17.9
Pod undocumented
400 0 my $self = shift;
401 0 my $err = shift;
402 0 ▶ if (ref($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err) 0 0 0
403 0 $self->throw("Bad error msg $err - must not by ref");
404 }
405 0 $self->err_event(message=>$err);
406 0 return;
407 }
408  
409 0 ▶sub parse_err {
Subroutine parse_err: not called CC=5 SCAR=34.0
Pod undocumented
410 0 my $self = shift;
411 0 ▶ my $err = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
412 0 ▶ if (ref($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err) 0 0 0
413 0 $self->throw("Bad error msg $err - must not by ref");
414 }
415 0 my @tags = ([message=>$err],[file=>$self->file]);
416 0 my $line = $self->line;
417 0 ▶ push(@tags, [line=>$line]) if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
418 0 my $line_no = $self->line_no;
419 0 ▶ push(@tags, [line_no=>$line_no]) if $line_no;
Branch: 1 branch
BranchTrueFalseTotal
if $line_no 0 0 0
420 0 my $pclass = ref($self);
421 0 push(@tags, [parse_class=>"$pclass"]);
422 0 $self->err_event(error=>[@tags]);
423 0 return;
424 }
425  
426  
427 0 ▶sub line_no {
Subroutine line_no: not called CC=2 SCAR=17.9
Pod undocumented
428 0 my $self = shift;
429 0 ▶ $self->{_line_no} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
430 0 return $self->{_line_no};
431 }
432  
433 0 ▶sub line {
Subroutine line: not called CC=2 SCAR=17.9
Pod undocumented
434 0 my $self = shift;
435 0 ▶ $self->{_line} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
436 0 return $self->{_line};
437 }
438 26 ▶sub file {
Subroutine file: called CC=2 SCAR=7.1
Pod undocumented
439 26 my $self = shift;
440 26 ▶ $self->{_file} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 26 0 26
441 26 return $self->{_file};
442 }
443  
444 0 ▶sub finish {
Subroutine finish: not called CC=3 SCAR=24.8
Pod undocumented
445 0 my $self = shift;
446 0 ▶ if ($self->errhandler && $self->errhandler->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->errhandler and $self->errhandler->depth) 0 0 0
Condition0%0/3
$self->errhandler and $self->errhandler->depth
!ll&&!rl&&r
000
MC/DC0%0/2
$self->errhandler and $self->errhandler->depth
$self->errhandler $self->errhandler->depth
Truth table0%0/3
A and B
A= $self->errhandlerB= $self->errhandler->depth
ABresult
0X0
100
111
447 0 $self->errhandler->end_event;
448 }
449 }
450  
451  
452 17 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.9
Pod documented
453 17 my $self = shift;
454 17 my ($file, $str, $fh) =
455 rearrange([qw(file str fh)], @_);
456  
457 17 ▶ $self->file($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 13 4 17
458 17 ▶ if ($str) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str) { } 4 13 17
elsif ($file) { } 13 0 13
459 4 $self->load_module("IO::String");
460 4 ▶ $fh = IO::String->new($str) || confess($str);
Condition50%1/2
'IO::String'->new($str) || confess($str)
l!l
40
MC/DC0%0/1
'IO::String'->new($str) || confess($str)
'IO::String'->new($str)
Truth table50%1/2
A
A= 'IO::String'->new($str)
Aresult
11
00
461 }
462 elsif ($file) {
463 13 ▶ if ($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 13 13
464 0 $fh = \*STDIN;
465 }
466 else {
467 13 $self->load_module("FileHandle");
468 13 ▶ $fh = FileHandle->new($file) || confess("cannot open file: $file");
Condition50%1/2
'FileHandle'->new($file) || confess("cannot open file: $file")
l!l
130
MC/DC0%0/1
'FileHandle'->new($file) || confess("cannot open file: $file")
'FileHandle'->new($file)
Truth table50%1/2
A
A= 'FileHandle'->new($file)
Aresult
11
00
469 }
470 }
471 else {
472 }
473 17 ▶ if (!$fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 17 17
474 0 confess("no filehandle");
475 }
476 17 $self->parse_fh($fh);
477 # problem with IO::String closing in perl5.6.1
478 17 ▶ unless ($str) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str) 13 4 17
479 #$fh->close || confess("cannot close file: $file"); // problem stdout
480 13 $fh->close;
481 }
482 17 return;
483 }
484  
485 0 ▶sub handler_err {
Subroutine handler_err: not called CC=1 SCAR=6.9
Pod undocumented
486 0 my $self = shift;
487 0 $self->err_event(error=>[[message=>'handler problem'],
488 [stack=>shift]]);
489 }
490  
491 0 ▶sub errlist {
Subroutine errlist: not called CC=4 SCAR=30.0
Pod undocumented
492 0 my $self = shift;
493 0 $self->finish;
494 0 my $eh = $self->errhandler;
495 0 ▶ if ($eh && $eh->stag && $eh->stag->data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eh and $eh->stag and $eh->stag->data) 0 0 0
Condition0%0/6
$eh and $eh->stag and $eh->stag->data
!ll&&!rl&&r
000
$eh and $eh->stag
!ll&&!rl&&r
000
MC/DC0%0/3
$eh and $eh->stag and $eh->stag->data
$eh $eh->stag $eh->stag->data
Truth table0%0/4
A and B and C
A= $ehB= $eh->stagC= $eh->stag->data
ABCresult
0XX0
10X0
1100
1111
496 0 return ($eh->stag->get_error);
497 }
498 0 return ();
499 }
500  
501  
502 # MAIN EVENT HANDLING
503 # start/end/event/body
504  
505 480 ▶sub start_event {
Subroutine start_event: called CC=1 SCAR=0.0
Pod documented
506 480 my $self = shift;
507 480 push(@{$self->{_stack}}, $_[0]);
508 480 $self->{_handler}->start_event(@_);
509 480 return;
510 }
511 480 ▶sub end_event {
Subroutine end_event: called CC=2 SCAR=7.0
Pod documented
512 480 my $self = shift;
513 480 ▶ my $ev = shift || $self->{_stack}->[-1];
Condition66%2/3
shift() || $self->{'_stack'}[-1]
l!l&&r!l&&!r
3701100
MC/DC0%0/2
shift() || $self->{'_stack'}[-1]
shift() $self->{'_stack'}[-1]
Truth table66%2/3
A || B
A= shift()B= $self->{'_stack'}[-1]
ABresult
1X1
011
000
514 480 $self->{_handler}->end_event($ev);
515  
516 480 my $out = pop(@{$self->{_stack}});
517 480 return;
518 }
519  
520 0 ▶sub event {
Subroutine event: not called CC=1 SCAR=6.9
Pod documented
521 0 my $self = shift;
522 0 $self->{_handler}->event(@_);
523 0 return;
524 }
525 306 ▶sub evbody {
Subroutine evbody: called CC=1 SCAR=0.0
Pod documented
526 306 my $self = shift;
527 306 $self->{_handler}->evbody(@_);
528 # my $lc = $self->last_evcall_type;
529 # if ($lc && $lc eq 'evbody') {
530 # confess("attempting to event_body illegally (body already defined)");
531 # }
532  
533 # eval {
534 # $self->handler->evbody(@_);
535 # };
536 # if ($@) {
537 # $self->handler_err($@);
538 # }
539 # $self->last_evcall_type('evbody');
540 306 return;
541 }
542  
543 11 1;
« blib/lib/Data/Stag/GraphHandler.pm ↑ Summary blib/script/stag-splitter.pl »