File Coverage

blib/lib/Language/Zcode/Parser.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 4 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 32 34 94.1


line stmt bran cond sub pod time code
1             package Language::Zcode::Parser;
2            
3 2     2   31623 use strict;
  2         5  
  2         171  
4 2     2   11 use warnings;
  2         6  
  2         73  
5            
6 2     2   1471 use Language::Zcode::Parser::Routine;
  2         8  
  2         59  
7 2     2   2083 use Language::Zcode::Parser::Opcode;
  2         7  
  2         637  
8            
9             =head1 NAME
10            
11             Language::Zcode::Parser - reads and parses a Z-code file into a big Perl hash
12            
13             =head1 SYNOPSIS
14            
15             # Create a Pure Perl Parser
16             my $pParser = new Language::Zcode::Parser "Perl";
17            
18             # If they didn't put ".z5" at the end, find it anyway
19             $infile = $pParser->find_zfile($infile) || exit;
20            
21             # Read in the file, store it in memory
22             $pParser->read_memory($infile);
23            
24             # Parse header of the Z-file
25             $pParser->parse_header();
26            
27             # Get the subroutines in the file (LZ::Parser::Routine objects)
28             my @subs = $pParser->find_subs($infile);
29            
30             =head1 DESCRIPTION
31            
32             For finding where the subroutines start and end, you can either depend on
33             an external call to txd, a 1992 C program available at ifarchive.org, or
34             a pure Perl version.
35            
36             Everything else is done in pure Perl.
37            
38             =cut
39            
40             =head2 new (class, how to find subs, args...)
41            
42             This is a factory method. Called with 'Perl' or 'TXD' (or 'txd') as arguments,
43             it will create Parsers of LZ::Parser::Perl or LZ::Parser::TXD, which
44             are subclasses of LZ::Parser::Generic.
45            
46             That class' 'new' method will be called with any other passed-in args.
47            
48             =cut
49            
50             sub new {
51 2     2 1 514 my ($class, $sub_finder, @arg) = @_;
52             # XXX I'll bet there's some fancy way of telling if a class exists.
53             # E.g., test $class->can("new")
54 2 50       29 die "Arg for how to find subs must be 'Perl' or 'txd', not '$sub_finder\n'"
55             unless $sub_finder =~ /^(perl|txd)$/i;
56 2         11 $sub_finder =~ s/perl/Perl/i;
57 2         6 $sub_finder =~ s/txd/TXD/i;
58 2         6 my $new_class = "Language::Zcode::Parser::${sub_finder}";
59 2 50   2   1685 eval "use $new_class"; die $@ if $@;
  2         7  
  2         51  
  2         202  
  2         14  
60 2         35 return new $new_class @arg;
61             }
62            
63             1;