home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / perl5 / Glib / ParseXSDoc.pm < prev    next >
Encoding:
Perl POD Document  |  2006-04-03  |  21.8 KB  |  843 lines

  1. package Glib::ParseXSDoc;
  2.  
  3. # vim: set ts=4 :
  4.  
  5. use strict;
  6. use Data::Dumper;
  7. use Storable qw(store_fd);
  8. use Exporter;
  9. use Carp;
  10.  
  11. our @ISA = qw(Exporter);
  12. our @EXPORT = qw(
  13.     xsdocparse
  14. );
  15.  
  16. our $VERSION = '1.002';
  17.  
  18. our $NOISY = $ENV{NOISYDOC};
  19.  
  20. =head1 NAME
  21.  
  22. Glib::ParseXSDoc - Parse POD and XSub declarations from XS files.
  23.  
  24. =head1 DESCRIPTION
  25.  
  26. This is the heart of an automatic API reference documentation system for
  27. XS-based Perl modules.  FIXME more info here!!
  28.  
  29. FIXME document recognized POD directives and the output data structures
  30.  
  31. =head1 FUNCTIONS
  32.  
  33. =over
  34.  
  35. =item xsdocparse (@filenames)
  36.  
  37. Parse xs files for xsub signatures and pod.  Writes to standard output a
  38. data structure suitable for eval'ing in another Perl script, describing
  39. all the stuff found.  The output contains three variables:
  40.  
  41. =over
  42.  
  43. =item $xspods = ARRAYREF
  44.  
  45. array of pods found in the verbatim C portion of the XS file, listed in the
  46. order found.  These are assumed to pertain to the XS/C api, not the Perl api.
  47. Any C<=for apidoc> paragraphs following an C<=object> paragraphs in the
  48. verbatim sections are stripped (as are the C<=object> paragraphs), and will
  49. appear instead in C<< $data->{$package}{pods} >>.
  50.  
  51. =item $data = HASHREF
  52.  
  53. big hash keyed by package name (as found in the MODULE line), containing under
  54. each key a hash with all the xsubs and pods in that package, in the order
  55. found.  Packages are consolidated across multiple files.
  56.  
  57. =back
  58.  
  59. FYI, this creates a new parser and calls C<parse_file> on it for each
  60. input filename; then calls C<swizzle_pods> to ensure that any
  61. C<=for apidoc name> pods are matched up with their target xsubs; and
  62. finally calls Data::Dumper to write the data to stdout.  So, if you want
  63. to get finer control over how the output is created, or keep all the data
  64. in-process, now you know how.  :-)
  65.  
  66. =cut
  67.  
  68. sub xsdocparse {
  69.     my @filenames = @_;
  70.  
  71.     my $parser = Glib::ParseXSDoc->new;
  72.     foreach my $filename (@filenames) {
  73.         $parser->parse_file ($filename);
  74.     }
  75.     $parser->canonicalize_xsubs;
  76.     $parser->swizzle_pods;
  77.     $parser->preprocess_pods;
  78.     $parser->clean_out_empty_pods;
  79.  
  80.     print "# THIS FILE IS AUTOMATICALLY GENERATED - ANY CHANGES WILL BE LOST\n";
  81.     print "# generated by $0 ".scalar (localtime)."\n";
  82.     print "# input files:\n";
  83.     map { print "#   $_\n" } @filenames;
  84.     print "#\n\n";
  85.  
  86.     # Data::Dumper converts the whole output to a string, and consequently
  87.     # uses an obscene amount of ram on Gtk2's nearly 200 xs files.  Use
  88.     # Storable unless the user really really wants to force us to fall
  89.     # back to Data::Dumper.
  90.     if ($ENV{FORCE_DATA_DUMPER}) {
  91.         $Data::Dumper::Purity = 1;
  92.         print Data::Dumper->Dump([$parser->{xspods}, $parser->{data}],
  93.                                [qw($xspods            $data)]);
  94.         print "\n1;\n";
  95.     } else {
  96.         print "use Storable qw(fd_retrieve);\n";
  97.         print "\$xspods = fd_retrieve \\*DATA;\n";
  98.         print "\$data = fd_retrieve \\*DATA;\n";
  99.  
  100.         print "\n1;\n";
  101.         print "__DATA__\n";
  102.  
  103.         # NOTE: don't assume STDOUT, because other code may have select'd
  104.         # a different file handle.
  105.         store_fd $parser->{xspods}, select;
  106.         store_fd $parser->{data}, select;
  107.     }
  108.  
  109.     return [ keys %{$parser->{data}} ];
  110. }
  111.  
  112.  
  113. =back
  114.  
  115. =cut
  116.  
  117. # =========================================================================
  118.  
  119. =head1 METHODS
  120.  
  121. =over
  122.  
  123. =item $Glib::ParseXSDoc::verbose
  124.  
  125. If true, this causes the parser to be verbose.
  126.  
  127. =cut
  128.  
  129. our $verbose = undef;
  130.  
  131.  
  132. =item $parser = Glib::ParseXSDoc->new
  133.  
  134. Create a new xsub parser.
  135.  
  136. =cut
  137. sub new {
  138.     my $class = shift;
  139.     return bless {
  140.         # state
  141.         module => undef,
  142.         package => undef,
  143.         prefix => undef,
  144.         # data
  145.         xspods => [],    #pods for the exported xs interface, e.g. the C stuff
  146.         data => {},    # all the shizzle, by package name
  147.     }, $class;
  148. }
  149.  
  150. =item string = $parser->package
  151.  
  152. Get the current package name.  Falls back to the module name.  Will be undef
  153. if the parser hasn't reached the first MODULE line.
  154.  
  155. =cut
  156. sub package {
  157.         my $self = shift;
  158.         return ($self->{package} || $self->{module})
  159. }
  160.  
  161. =item HASHREF = $parser->pkgdata
  162.  
  163. The data hash corresponding to the current package, honoring the most recently
  164. encountered C<=for object> directive.  Ensures that it exists.
  165. Returns a reference to the member of the main data structure, so modifications
  166. are permanent and useful.
  167.  
  168. =cut
  169. sub pkgdata {
  170.         my $self = shift;
  171.         my $pkg = $self->{object} || $self->package;
  172.         my $pkgdata = $self->{data}{$pkg};
  173.         if (not defined $pkgdata) {
  174.                 $pkgdata = {};
  175.                 $self->{data}{$pkg} = $pkgdata;
  176.         }
  177.         return $pkgdata;
  178. }
  179.  
  180.  
  181. =item $parser->parse_file (filename)
  182.  
  183. Parse one xs file.  Stores all the collected data in I<$parser>'s internal
  184. data structures.
  185.  
  186. =cut
  187. sub parse_file {
  188.     my $self = shift;
  189.     my $filename = shift;
  190.  
  191.     local *IN;
  192.     open IN, $filename or die "can't open $filename: $!\n";
  193.     print STDERR "scanning $filename\n" if $verbose;
  194.     $self->{filehandle} = \*IN;
  195.     $self->{filename} = $filename;
  196.  
  197.     # there was once a single state machine to parse an entire
  198.     # file, but it turned into a bi-level state machine because
  199.     # of the two-part nature of XS files.  that's silly, so i've
  200.     # broken it into two loops: the part that scans up to the
  201.     # first MODULE line, and the part that scans the rest of the
  202.     # file.
  203.  
  204.     my $lastpod = undef;    # most recently-read pod (for next xsub)
  205.     my @thesepackages = ();    # packages seen in this file
  206.  
  207.     # In the verbatim C portion of the file:
  208.     # seek the first MODULE line *outside* comments.
  209.     # collect any pod we encounter; only certain ones are 
  210.     # precious to us...  my... preciousssss... ahem.
  211.     $self->{module}  = undef;
  212.     $self->{package} = undef;
  213.     $self->{prefix}  = undef;
  214.     $self->{object}  = undef;
  215.     while (<IN>) {
  216.         chomp;
  217.         # in the verbatim C section before the first MODULE line,
  218.         # we need to be on the lookout for a few things...
  219.         # we need the first MODULE line, of course...
  220.         if ($self->is_module_line ($_)) {
  221.             last; # go to the next state machine.
  222.  
  223.         # mostly we want pods.
  224.         } elsif (/^=/) {
  225.             my $thispod = $self->slurp_pod_paragraph ($_);
  226.             # we're only interested in certain pod directives here.
  227.             if (/^=for\s+(apidoc|object)\b/) {
  228.                 my $which = $1;
  229.                 warn "$filename:".($.-@{$thispod->{lines}}+1).":"
  230.                    . " =for $which found before "
  231.                    . "MODULE directive\n";
  232.             }
  233.             push @{ $self->{xspods} }, $thispod;
  234.  
  235. ##        # we also need to track whether we're in a C comment, because
  236. ##        # MODULE directives are ignore in multiline comments.
  237. ##        } elsif (m{/\*}) {
  238. ##            # there was an open comment marker on this line.
  239. ##            # see if it's alone.
  240. ##            s{/\*.*\*/}{}g;
  241. ##            if (m{/\*}) {
  242. ##                # look for the end...
  243. ##                while (<IN>) {
  244. ##                }
  245. ##            }
  246.         }
  247.     }
  248.  
  249.     # preprocessor conditionals
  250.     my @cond;
  251.  
  252.     $lastpod = undef;
  253.     while (<IN>) {
  254.         #
  255.         # we're seeking xsubs and pods to document the Perl interface.
  256.         #
  257.         if ($self->is_module_line ($_)) {
  258.             # xsubs cannot steal pods across MODULE lines.
  259.             $lastpod = undef;
  260.  
  261.         } elsif (/^\s*$/) {
  262.             # ignore blank lines; but a blank line after a pod
  263.             # means it can't be associated with an xsub.
  264.             $lastpod = undef;
  265.  
  266.         } elsif (/^\s*#\s*(if|ifdef|ifndef)\s*(\s.*)$/) {
  267.             #warn "conditional $1 $2\n";
  268.             push @cond, $2;
  269.             #print Dumper(\@cond);
  270.         } elsif (/^\s*#\s*else\s*(\s.*)?$/) {
  271.             #warn "else $cond[-1]\n";
  272.         } elsif (/^\s*#\s*endif\s*(\s.*)?$/) {
  273.             #warn "endif $cond[-1]\n";
  274.             pop @cond;
  275.         } elsif (/^\s*#/) {
  276.             # ignore comments.  we've already determined that 
  277.             # this isn't a preprocessor directive (or at least
  278.             # not one in which we're interested).
  279.  
  280.         } elsif (/^(BOOT|PROTOTYPES)/) {
  281.             # ignore keyword lines in which we aren't interested
  282.  
  283.         } elsif (/^=/) {
  284.             # slurp in pod, up to and including the next =cut.
  285.             # put it in $lastpod so that the next-discovered
  286.             # xsub can claim it.
  287.             $lastpod = $self->slurp_pod_paragraph ($_);
  288.  
  289.             # we're interested in certain pod directives at
  290.             # this point...
  291.             if (/^=for\s+object(?:\s+([\w\:]*))?(.*)/) {
  292.                 $self->{object} = $1;
  293.                 if ($2) {
  294.                     $self->pkgdata->{blurb} = $2;
  295.                     if ($self->pkgdata->{blurb} =~ s/\((.*)\)//)
  296.                     {
  297.                         print STDERR "Documenting object $1 in file "
  298.                                     .$self->{object}."\n";
  299.                         $self->pkgdata->{object} = $1;
  300.                     }
  301.                     $self->pkgdata->{blurb} =~ s/^\s*-\s*//;
  302.                 }
  303.             } elsif (/^=for\s+(enum|flags)\s+([\w:]+)/) {
  304.                 push @{ $self->pkgdata->{enums} }, {
  305.                     type => $1,
  306.                     name => $2,
  307.                     pod => $lastpod,
  308.                 };
  309.                 # claim this pod now!
  310.                 $lastpod = undef;
  311.             } elsif (/^=for\s+see_also\s+(.+)$/) {
  312.                 push @{ $self->pkgdata->{see_alsos} }, $1;
  313.                 # claim this pod now!
  314.                 $lastpod = undef;
  315.             }
  316.             push @{ $self->pkgdata->{pods} }, $lastpod
  317.                 if defined $lastpod;
  318.  
  319.         } elsif (/^\w+/) {
  320.             # there's something at the beginning of the line!
  321.             # we've ruled out everything else, so this must be
  322.             # an xsub.  slurp in everything up to the next
  323.             # blank line (or end of file).   i know that's not
  324.             # *really* an entire XSUB body, but we don't care
  325.             # -- we only need the return value, name, arg types,
  326.             # and body type, and there aren't supposed to be 
  327.             # blank lines in all of that.
  328.             my @thisxsub = ($_);
  329.             while (<IN>) {
  330.                 chomp;
  331.                 last if /^\s*$/;
  332.                 push @thisxsub, $_;
  333.             }
  334.             my $xsub = $self->parse_xsub (\@thisxsub);
  335.             if ($lastpod) {
  336.                 # aha! we'll lay claim to that...
  337.                 pop @{ $self->pkgdata->{pods} };
  338.                 $xsub->{pod} = $lastpod;
  339.                 $lastpod = undef;
  340.             }
  341.             push @{ $self->pkgdata->{xsubs} }, $xsub;
  342.  
  343.         } else {
  344.             # this is probably xsub function body, comment, or
  345.             # some other stuff we don't care about.
  346.         }
  347.     }
  348.  
  349.     # that's it for this file...
  350.     close IN;
  351.     delete $self->{filehandle};
  352.     delete $self->{filename};
  353. }
  354.  
  355.  
  356. =item $parser->swizzle_pods
  357.  
  358. Match C<=for apidoc> pods to xsubs.
  359.  
  360. =cut
  361. sub swizzle_pods {
  362.     my $self = shift;
  363.     foreach my $package (keys %{$self->{data}}) {
  364.         my $pkgdata = $self->{data}{$package};
  365.         next unless $pkgdata->{pods};
  366.         next unless $pkgdata->{xsubs};
  367.         my $pods = $pkgdata->{pods};
  368.         for (my $i = @$pods-1 ; $i >= 0 ; $i--) {
  369.             my $firstline = $pods->[$i]{lines}[0];
  370.             next unless $firstline =~ /=for\s+apidoc\s+([:\w]+)\s*/;
  371.             my $name = $1;
  372.             foreach my $xsub (@{ $pkgdata->{xsubs} }) {
  373.                 if ($name eq $xsub->{symname}) {
  374.                     $xsub->{pod} = $pods->[$i];
  375.                     splice @$pods, $i, 1;
  376.                     last;
  377.                 }
  378.             }
  379.         }
  380.     }
  381. }
  382.  
  383.  
  384. =item $parser->preprocess_pods
  385.  
  386. Honor the C<__hide__> and C<__function__> directives in C<=for apidoc> lines.
  387.  
  388. We look for the strings anywhere, but you'll typically have it at the end of
  389. the line, e.g.:
  390.  
  391.   =for apidoc symname __hide__        for detached blocks
  392.   =for apidoc __hide__                for attached blocks
  393.  
  394.   =for apidoc symname __function__    for functions rather than methods
  395.   =for apidoc __function__            for functions rather than methods
  396.  
  397. =cut
  398. sub preprocess_pods {
  399.     my $self = shift;
  400.     foreach my $package (keys %{$self->{data}}) {
  401.         my $pkgdata = $self->{data}{$package};
  402.  
  403.         foreach (@{$pkgdata->{pods}})
  404.         {
  405.             my $firstline = $_->{lines}[0];
  406.             if ($firstline) {
  407.                 $_->{position} = $1 if ($firstline =~ /=for\s+position\s+(\w+)/);
  408.             }
  409.         }
  410.  
  411.         next unless $pkgdata->{xsubs};
  412.  
  413.         # look for magic keywords in the =for apidoc
  414.         foreach (@{$pkgdata->{xsubs}})
  415.         {
  416.             my $firstline = $_->{pod}{lines}[0];
  417.             if ($firstline) {
  418.                 $_->{function} = ($firstline =~ /__function__/);
  419.                 $_->{hidden} = ($firstline =~ /__hide__/);
  420.                 $_->{gerror} = ($firstline =~ /__gerror__/);
  421.             }
  422.         }
  423.     }
  424. }
  425.  
  426.  
  427. # ===============================================================
  428.  
  429. =item bool = $parser->is_module_line ($line)
  430.  
  431. Analyze I<$line> to see if it contains an XS MODULE directive.  If so, returns
  432. true after setting the I<$parser>'s I<module>, I<package>, and I<prefix>
  433. accordingly.
  434.  
  435. =cut
  436. sub is_module_line {
  437.     my $self = shift;
  438.     my $l = shift;
  439.     if ($l =~ /^MODULE\s*=\s*([:\w]+)
  440.                 (?:\s+PACKAGE\s*=\s*([:\w]+)
  441.                 (?:\s+PREFIX\s*=\s*([:\w]+))?)?
  442.                 /x) {
  443.         $self->{module}  = $1;
  444.         $self->{package} = $2 || $self->{module};
  445.         $self->{prefix}  = $3;
  446.         $self->{object}  = undef;
  447.         return 1;
  448.     } else {
  449.         return 0;
  450.     }
  451. }
  452.  
  453.  
  454. =item $pod = $parser->slurp_pod_paragraph ($firstline, $term_regex=/^=cut\s*/)
  455.  
  456. Slurp up POD lines from I<$filehandle> from here to the next
  457. I<$term_regex> or EOF.  Since you probably already read a
  458. line to determine that we needed to start a pod, you can pass
  459. that first line to be included.
  460.  
  461. =cut
  462. sub slurp_pod_paragraph {
  463.     my $parser     = shift;
  464.     my $firstline  = shift;
  465.     my $term_regex = shift || qr/^=cut\s*/o;
  466.     my $filehandle = $parser->{filehandle};
  467.  
  468.     # just in case.
  469.     chomp $firstline;
  470.  
  471.     my @lines = $firstline ? ($firstline) : ();
  472.     while (my $line = <$filehandle>) {
  473.         chomp $line;
  474.         push @lines, $line;
  475.         last if $line =~ m/$term_regex/;
  476.     }
  477.  
  478.     return {
  479.         filename => $parser->{filename},
  480.         line => $. - @lines,
  481.         lines => \@lines,
  482.     };
  483. }
  484.  
  485.  
  486. =item $xsub = $parser->parse_xsub (\@lines)
  487.  
  488. =item $xsub = $parser->parse_xsub (@lines)
  489.  
  490. Parse an xsub header, in the form of a list of lines,
  491. into a data structure describing the xsub.  That includes
  492. pulling out the argument types, aliases, and code type.
  493.  
  494. Without artificial intelligence, we cannot reliably 
  495. determine anything about the types or number of parameters
  496. returned from xsubs with PPCODE bodies.
  497.  
  498. OUTLIST parameters are pulled from the args list and put
  499. into an "outlist" key.  IN_OUTLIST parameters are put into
  500. both.
  501.  
  502. Data type names are not mangled at all.
  503.  
  504. Note that the method can take either a list of lines or a reference to a
  505. list of lines.  The flat list form is provided for compatibility; the
  506. reference form is preferred, to avoid duplicating a potentially large list
  507. of strings.
  508.  
  509. =cut
  510. sub parse_xsub {
  511.     my ($self, @thisxsub) = @_;
  512.  
  513.     # allow for pass-by-reference.
  514.     @thisxsub = @{ $thisxsub[0] }
  515.         if @thisxsub == 1 && 'ARRAY' eq ref $thisxsub[0];
  516.  
  517.     map { s/#.*$// } @thisxsub;
  518.  
  519.     my $filename = $self->{filename};
  520.     my $oldwarn = $SIG{__WARN__};
  521. #$SIG{__WARN__} = sub {
  522. #        warn "$self->{filename}:$.:  "
  523. #           . join(" / ", $self->{module}||"", $self->{package}||"")
  524. #           . "\n    $_[0]\n   ".Dumper(\@thisxsub)
  525. #};
  526.  
  527.     my $lineno = $. - @thisxsub;
  528.     my %xsub = (
  529.         'filename' => $filename,
  530.         'line' => ($.-@thisxsub),
  531.         'module' => $self->{module},
  532.         'package' => $self->package, # to be overwritten as needed
  533.     );
  534.     my $args;
  535.  
  536.     #warn Dumper(\@thisxsub);
  537.  
  538.     # merge continuation lines.  xsubpp allows continuation lines in the
  539.     # xsub arguments list and barfs on them in other spots, but with xsubpp
  540.     # providing such validation, we'll just cheat and merge any that we find.
  541.     # this will bork the line counting logic we have below, but i don't see
  542.     # a fix for it without major tearup of the code here.
  543.     my @foo = @thisxsub;
  544.     @thisxsub = shift @foo;
  545.     while (my $s = shift @foo) {
  546.         if ($thisxsub[$#thisxsub] =~ s/\\$//) {
  547.             chomp $thisxsub[$#thisxsub];
  548.             $thisxsub[$#thisxsub] .= $s;
  549.         } else {
  550.             push @thisxsub, $s;
  551.         }
  552.     }
  553.  
  554.     if ($thisxsub[0] =~ /^([^(]+\s+\*?)   # return type, possibly with a *
  555.                           \b([:\w]+)\s*   # symbol name
  556.                           \(              # open paren
  557.                             (.*)          # whatever's inside, if anything
  558.                           \)              # close paren, maybe with space
  559.                           \s*;?\s*$/x) {  # and maybe other junk at the end
  560.         # all on one line
  561.         $xsub{symname} = $2;
  562.         $args = $3;
  563.         my $r = $1;
  564.         $xsub{return_type} = [$r]
  565.             unless $r =~ /^void\s*$/;
  566.         shift @thisxsub; $lineno++;
  567.  
  568.     } elsif ($thisxsub[1] =~ /^(\S+)\s*\((.*)\);?\s*$/) {
  569.         # multiple lines
  570.         $xsub{symname} = $1;
  571.         $args = $2;
  572.         # return type is on line 0
  573.         $thisxsub[0] =~ s/\s*$//;
  574.         $xsub{return_type} = [$thisxsub[0]]
  575.             unless $thisxsub[0] =~ /^void\s*$/;
  576.         shift @thisxsub; $lineno++;
  577.         shift @thisxsub; $lineno++;
  578.     }
  579.  
  580.     # eat padding spaces from the arg string.  i tried several ways of
  581.     # building this into the regexen above, but found nothing that still
  582.     # allowed the arg string to be empty, which we'll have for functions
  583.     # (not methods) without resorting to extremely arcane negatory
  584.     # lookbeside assertiveness operators.
  585.     $args =~ s/^\s*//;
  586.     $args =~ s/\s*$//;
  587.  
  588.     # we can get empty arg strings on non-methods.
  589.     #warn "$filename:$lineno: WTF : args string is empty\n"
  590.     #    if not defined $args;
  591.  
  592.     my %args = ();
  593.     my @argstr = split /\s*,\s*/, $args;
  594.     #warn Dumper([$args, \%args, \@argstr]);
  595.     for (my $i = 0 ; $i < @argstr ; $i++) {
  596.         # the last one can be an ellipsis, let's handle that specially
  597.         if ($i == $#argstr and $argstr[$i] eq '...') {
  598.             $args{'...'} = { name => '...', };
  599.             push @{ $xsub{args} }, $args{'...'};
  600.             last;
  601.         }
  602.         if ($argstr[$i] =~
  603.                        /^(?:(IN_OUTLIST|OUTLIST)\s+)? # OUTLIST would be 1st
  604.                          ([^=]+(?:\b|\s))?  # arg type is optional, too
  605.                          (\w+)              # arg name
  606.                          (?:\s*=\s*(.+))?   # possibly a default value
  607.                          $/x) {
  608.             if (defined $1) {
  609.                 push @{ $xsub{outlist} }, {
  610.                     type => $2,
  611.                     name => $3,
  612.                 };
  613.                 if ($1 eq 'IN_OUTLIST') {
  614.                     # also an arg
  615.                     $args{$3} = {
  616.                         type => $2,
  617.                         name => $3,
  618.                     };
  619.                     $args{$3}{default} = $4 if defined $4;
  620.                     push @{ $xsub{args} }, $args{$3};
  621.                 }
  622.             
  623.             } else {
  624.                 $args{$3} = {
  625.                     type => $2,
  626.                     name => $3,
  627.                 };
  628.                 $args{$3}{default} = $4 if defined $4;
  629.                 push @{ $xsub{args} }, $args{$3};
  630.             }
  631.         } elsif ($argstr[$i] =~ /^g?int\s+length\((\w+)\)$/) {
  632.             #warn " ******* $i is string length of $1 *****\n";
  633.         } else {
  634.             warn "$filename:$lineno: ($xsub{symname}) don't know how to"
  635.                . " parse arg $i, '$argstr[$i]'\n";
  636.         }
  637.     }
  638.  
  639.     
  640.  
  641.     my $xstate = 'args';
  642.     while ($_ = shift @thisxsub) {
  643.         if (/^\s*ALIAS:/) {
  644.             $xstate = 'alias';
  645.         } elsif (/\s*(PREINIT|CLEANUP|OUTPUT|C_ARGS):/) {
  646.             $xstate = 'code';
  647.         } elsif (/\s*(PPCODE|CODE):/) {
  648.             $xsub{codetype} = $1;
  649.             last;
  650.         } elsif ($xstate eq 'alias') {
  651.             /^\s*([:\w]+)\s*=\s*(\d+)\s*$/;
  652.             if (defined $2) {
  653.                 $xsub{alias}{$1} = $2;
  654.             } else {
  655.                 warn "$filename:$lineno: WTF : seeking alias on line $_\n";
  656.             }
  657.         } elsif ($xstate eq 'args') {
  658.             if (/^\s*
  659.                   (.+(?:\b|\s))      # datatype
  660.                   (\w+)              # arg name
  661.                   ;?                 # optional trailing semicolon
  662.                   \s*$/x)
  663.             {
  664.                 if (exists $args{$2}) {
  665.                     $args{$2}{type} = $1
  666.                 } else {
  667.                     warn "$filename:$lineno: unused arg $2\n";
  668.                     warn "  line was '$_'\n";
  669.                 }
  670.             } elsif (/^\s*/) {
  671.                 # must've stripped a comment.
  672.             } else {
  673.                 warn "$filename:$lineno: WTF : seeking args on line $_\n";
  674.             }
  675.         }
  676.         $lineno++;
  677.     }
  678.  
  679.     # mangle the symbol name from an xsub into its actual perl name.
  680.     $xsub{original_name} = $xsub{symname};
  681.     if (defined $self->{prefix}) {
  682.         my $pkg = $self->package;
  683.         $xsub{symname} =~ s/^($self->{prefix})?/$pkg\::/;
  684.     } else {
  685.         $xsub{symname} = ($self->package)."::".$xsub{symname};
  686.     }
  687.  
  688.     # sanitize all the C type declarations, which we have 
  689.     # collected in the arguments, outlist, and return types.
  690.     if ($xsub{args}) {
  691.         foreach my $a (@{ $xsub{args} }) {
  692.             $a->{type} = sanitize_type ($a->{type})
  693.                 if defined $a->{type};
  694.         }
  695.     }
  696.     if ($xsub{outlist}) {
  697.         foreach my $a (@{ $xsub{outlist} }) {
  698.             $a->{type} = sanitize_type ($a->{type})
  699.                 if defined $a->{type};
  700.         }
  701.     }
  702.     if ($xsub{return_type}) {
  703.         for (my $i = 0 ; $i < @{ $xsub{return_type} } ; $i++) {
  704.             $xsub{return_type}[$i] =
  705.                 sanitize_type ($xsub{return_type}[$i]);
  706.         }
  707.     }
  708.  
  709.     $SIG{__WARN__} = $oldwarn;
  710.  
  711.     return \%xsub;
  712. }
  713.  
  714.  
  715.  
  716. sub sanitize_type {
  717.         local $_ = shift;
  718.         s/\s+/ /g;        # squash all whitespace
  719.         s/^\s//;          # zap leading space
  720.         s/\s$//;          # zap trailing space
  721.         s/(?<=\S)\*$/ */; # stars may not be glued to the name
  722.         return $_;
  723. }
  724.  
  725.  
  726. sub canonicalize_xsubs {
  727.     my $self = shift;
  728.  
  729.     return undef unless 'HASH' eq ref $self->{data};
  730.  
  731.     # make sure that each package contains an xsub hash for each
  732.     # xsub, whether an alias or not.
  733.     foreach my $package (keys %{$self->{data}}) {
  734.         my $pkgdata = $self->{data}{$package};
  735.         next unless $pkgdata or $pkgdata->{xsubs};
  736.         my $xsubs = $pkgdata->{xsubs};
  737.         @$xsubs = map { split_aliases ($_) } @$xsubs;
  738.     }
  739. }
  740.  
  741. sub split_aliases {
  742.     my $xsub = shift;
  743.     return $xsub unless exists $xsub->{alias};
  744.     return $xsub unless 'HASH' eq ref $xsub->{alias};
  745.     my %aliases = %{ $xsub->{alias} };
  746.     my @xsubs = ();
  747.     my %seen = ();
  748.     foreach my $a (sort { $aliases{$a} <=> $aliases{$b} } keys %aliases) {
  749.         push @xsubs, {
  750.             %$xsub,
  751.             symname => $a,
  752.             pod => undef,
  753.             # we do a deep copy on the args, so that changes to one do not
  754.             # affect another.  in particular, adding docs or hiding an arg
  755.             # in one xsub shouldn't affect another.
  756.             args => deep_copy_ref ($xsub->{args}),
  757.         };
  758.         $seen{ $aliases{$a} }++;
  759.     }
  760.     if (! $seen{0}) {
  761.         unshift @xsubs, $xsub;
  762.     }
  763.  
  764.     return @xsubs;
  765. }
  766.  
  767.  
  768. sub deep_copy_ref {
  769.         my $ref = shift;
  770.         return undef if not $ref;
  771.         my $reftype = ref $ref;
  772.         if ('ARRAY' eq $reftype) {
  773.                 my @newary = map { deep_copy_ref ($_) } @$ref;
  774.                 return \@newary;
  775.         } elsif ('HASH' eq $reftype) {
  776.                 my %newhash = map { $_, deep_copy_ref ($ref->{$_}) } keys %$ref;
  777.                 return \%newhash;
  778.         } else {
  779.                 return $ref;
  780.         }
  781. }
  782.  
  783. =item $parser->clean_out_empty_pods
  784.  
  785. Looks through the data member of the parser and removes any keys (and
  786. associated values) when no pod, enums, and xsubs exist for the package.
  787.  
  788. =cut
  789.  
  790. sub clean_out_empty_pods
  791. {
  792.     my $data = shift;
  793.     return unless (exists ($data->{data}));
  794.     $data = $data->{data};
  795.  
  796.     my $pod;
  797.     my $xsub;
  798.     foreach (keys %$data)    
  799.     {
  800.         $pod = $data->{$_};
  801.         next if ((exists $pod->{pods} and scalar @{$pod->{pods}}) or
  802.                  exists $pod->{enums} or 
  803.                  scalar (grep (!/DESTROY/, 
  804.                                  map { $_->{hidden} 
  805.                                        ? ()
  806.                                        : $_->{symname} }
  807.                                      @{$pod->{xsubs}})));
  808.         print STDERR "Deleting $_ from doc.pl's \$data\n";
  809.         delete $data->{$_}; 
  810.     }
  811. }
  812.  
  813.  
  814. 1;
  815.  
  816. __END__
  817.  
  818. =back
  819.  
  820. =head1 AUTHOR
  821.  
  822. muppet E<lt>scott at asofyet dot orgE<gt>
  823.  
  824. =head1 COPYRIGHT AND LICENSE
  825.  
  826. Copyright (C) 2003, 2004 by muppet
  827.  
  828. This library is free software; you can redistribute it and/or modify it under
  829. the terms of the GNU Library General Public License as published by the Free
  830. Software Foundation; either version 2.1 of the License, or (at your option) any
  831. later version.
  832.  
  833. This library is distributed in the hope that it will be useful, but WITHOUT ANY
  834. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  835. PARTICULAR PURPOSE.  See the GNU Library General Public License for more
  836. details.
  837.  
  838. You should have received a copy of the GNU Library General Public License along
  839. with this library; if not, write to the Free Software Foundation, Inc., 59
  840. Temple Place - Suite 330, Boston, MA  02111-1307  USA.
  841.  
  842. =cut
  843.