1 #############################################################################
2 # Pod/InputObjects.pm -- package which defines objects for input streams
3 # and paragraphs and commands when parsing POD docs.
5 # Copyright (C) 1996-2000 by Bradford Appleton. All rights reserved.
6 # This file is part of "PodParser". PodParser is free software;
7 # you can redistribute it and/or modify it under the same terms
9 #############################################################################
11 package Pod
::InputObjects
;
13 use vars
qw($VERSION);
14 $VERSION = 1.13; ## Current version of this package
15 require 5.005; ## requires this Perl version or later
17 #############################################################################
21 Pod::InputObjects - objects representing POD input paragraphs, commands, etc.
25 use Pod::InputObjects;
37 This module defines some basic input objects used by B<Pod::Parser> when
38 reading and parsing POD text from an input source. The following objects
45 =item package B<Pod::InputSource>
47 An object corresponding to a source of POD input text. It is mostly a
48 wrapper around a filehandle or C<IO::Handle>-type object (or anything
49 that implements the C<getline()> method) which keeps track of some
50 additional information relevant to the parsing of PODs.
54 =item package B<Pod::Paragraph>
56 An object corresponding to a paragraph of POD input text. It may be a
57 plain paragraph, a verbatim paragraph, or a command paragraph (see
60 =item package B<Pod::InteriorSequence>
62 An object corresponding to an interior sequence command from the POD
63 input text (see L<perlpod>).
65 =item package B<Pod::ParseTree>
67 An object corresponding to a tree of parsed POD text. Each "node" in
68 a parse-tree (or I<ptree>) is either a text-string or a reference to
69 a B<Pod::InteriorSequence> object. The nodes appear in the parse-tree
70 in the order in which they were parsed from left-to-right.
74 Each of these input objects are described in further detail in the
75 sections which follow.
79 #############################################################################
85 #############################################################################
87 package Pod::InputSource;
89 ##---------------------------------------------------------------------------
93 =head1 B<Pod::InputSource>
95 This object corresponds to an input source or stream of POD
96 documentation. When parsing PODs, it is necessary to associate and store
97 certain context information with each input source. All of this
98 information is kept together with the stream itself in one of these
99 C<Pod::InputSource> objects. Each such object is merely a wrapper around
100 an C<IO::Handle> object of some kind (or at least something that
101 implements the C<getline()> method). They have the following
108 ##---------------------------------------------------------------------------
114 my $pod_input1 = Pod::InputSource->new(-handle => $filehandle);
115 my $pod_input2 = new Pod::InputSource(-handle => $filehandle,
117 my $pod_input3 = new Pod::InputSource(-handle => \*STDIN);
118 my $pod_input4 = Pod::InputSource->new(-handle => \*STDIN,
121 This is a class method that constructs a C<Pod::InputSource> object and
122 returns a reference to the new input source object. It takes one or more
123 keyword arguments in the form of a hash. The keyword C<-handle> is
124 required and designates the corresponding input handle. The keyword
125 C<-name> is optional and specifies the name associated with the input
126 handle (typically a file name).
133 ## Determine if we were called via an object-ref or a classname
135 my $class = ref($this) || $this;
137 ## Any remaining arguments are treated as initial values for the
138 ## hash that is used to represent this object. Note that we default
139 ## certain values by specifying them *before* the arguments passed.
140 ## If they are in the argument list, they will override the defaults.
141 my $self = { -name => '(unknown)',
146 ## Bless ourselves into the desired class and perform any initialization
151 ##---------------------------------------------------------------------------
157 my $filename = $pod_input->name();
158 $pod_input->name($new_filename_to_use);
160 This method gets/sets the name of the input source (usually a filename).
161 If no argument is given, it returns a string containing the name of
162 the input source; otherwise it sets the name of the input source to the
163 contents of the given argument.
170 (@_ > 1) and $_[0]->{'-name'} = $_[1];
171 return $_[0]->{'-name'};
174 ## allow 'filename' as an alias for 'name'
177 ##---------------------------------------------------------------------------
183 my $handle = $pod_input->handle();
185 Returns a reference to the handle object from which input is read (the
186 one used to contructed this input source object).
193 return $_[0]->{'-handle'};
196 ##---------------------------------------------------------------------------
200 =head2 B<was_cutting()>
202 print "Yes.\n" if ($pod_input->was_cutting());
204 The value of the C<cutting> state (that the B<cutting()> method would
205 have returned) immediately before any input was read from this input
206 stream. After all input from this stream has been read, the C<cutting>
207 state is restored to this value.
214 (@_ > 1) and $_[0]->{-was_cutting} = $_[1];
215 return $_[0]->{-was_cutting};
218 ##---------------------------------------------------------------------------
220 #############################################################################
222 package Pod::Paragraph;
224 ##---------------------------------------------------------------------------
226 =head1 B<Pod::Paragraph>
228 An object representing a paragraph of POD input text.
229 It has the following methods/attributes:
233 ##---------------------------------------------------------------------------
235 =head2 Pod::Paragraph-E<gt>B<new()>
237 my $pod_para1 = Pod::Paragraph->new(-text => $text);
238 my $pod_para2 = Pod::Paragraph->new(-name => $cmd,
240 my $pod_para3 = new Pod::Paragraph(-text => $text);
241 my $pod_para4 = new Pod::Paragraph(-name => $cmd,
243 my $pod_para5 = Pod::Paragraph->new(-name => $cmd,
246 -line => $line_number);
248 This is a class method that constructs a C<Pod::Paragraph> object and
249 returns a reference to the new paragraph object. It may be given one or
250 two keyword arguments. The C<-text> keyword indicates the corresponding
251 text of the POD paragraph. The C<-name> keyword indicates the name of
252 the corresponding POD command, such as C<head1> or C<item> (it should
253 I<not> contain the C<=> prefix); this is needed only if the POD
254 paragraph corresponds to a command paragraph. The C<-file> and C<-line>
255 keywords indicate the filename and line number corresponding to the
256 beginning of the paragraph
261 ## Determine if we were called via an object-ref or a classname
263 my $class = ref($this) || $this;
265 ## Any remaining arguments are treated as initial values for the
266 ## hash that is used to represent this object. Note that we default
267 ## certain values by specifying them *before* the arguments passed.
268 ## If they are in the argument list, they will override the defaults.
271 -text => (@_ == 1) ? $_[0] : undef,
272 -file => '<unknown-file>',
280 ## Bless ourselves into the desired class and perform any initialization
285 ##---------------------------------------------------------------------------
287 =head2 $pod_para-E<gt>B<cmd_name()>
289 my $para_cmd = $pod_para->cmd_name();
291 If this paragraph is a command paragraph, then this method will return
292 the name of the command (I<without> any leading C<=> prefix).
297 (@_ > 1) and $_[0]->{'-name'} = $_[1];
298 return $_[0]->{'-name'};
301 ## let name() be an alias for cmd_name()
304 ##---------------------------------------------------------------------------
306 =head2 $pod_para-E<gt>B<text()>
308 my $para_text = $pod_para->text();
310 This method will return the corresponding text of the paragraph.
315 (@_ > 1) and $_[0]->{'-text'} = $_[1];
316 return $_[0]->{'-text'};
319 ##---------------------------------------------------------------------------
321 =head2 $pod_para-E<gt>B<raw_text()>
323 my $raw_pod_para = $pod_para->raw_text();
325 This method will return the I<raw> text of the POD paragraph, exactly
326 as it appeared in the input.
331 return $_[0]->{'-text'} unless (defined $_[0]->{'-name'});
332 return $_[0]->{'-prefix'} . $_[0]->{'-name'} .
333 $_[0]->{'-separator'} . $_[0]->{'-text'};
336 ##---------------------------------------------------------------------------
338 =head2 $pod_para-E<gt>B<cmd_prefix()>
340 my $prefix = $pod_para->cmd_prefix();
342 If this paragraph is a command paragraph, then this method will return
343 the prefix used to denote the command (which should be the string "="
349 return $_[0]->{'-prefix'};
352 ##---------------------------------------------------------------------------
354 =head2 $pod_para-E<gt>B<cmd_separator()>
356 my $separator = $pod_para->cmd_separator();
358 If this paragraph is a command paragraph, then this method will return
359 the text used to separate the command name from the rest of the
365 return $_[0]->{'-separator'};
368 ##---------------------------------------------------------------------------
370 =head2 $pod_para-E<gt>B<parse_tree()>
372 my $ptree = $pod_parser->parse_text( $pod_para->text() );
373 $pod_para->parse_tree( $ptree );
374 $ptree = $pod_para->parse_tree();
376 This method will get/set the corresponding parse-tree of the paragraph's text.
381 (@_ > 1) and $_[0]->{'-ptree'} = $_[1];
382 return $_[0]->{'-ptree'};
385 ## let ptree() be an alias for parse_tree()
386 *ptree = \&parse_tree;
388 ##---------------------------------------------------------------------------
390 =head2 $pod_para-E<gt>B<file_line()>
392 my ($filename, $line_number) = $pod_para->file_line();
393 my $position = $pod_para->file_line();
395 Returns the current filename and line number for the paragraph
396 object. If called in a list context, it returns a list of two
397 elements: first the filename, then the line number. If called in
398 a scalar context, it returns a string containing the filename, followed
399 by a colon (':'), followed by the line number.
404 my @loc = ($_[0]->{'-file'} || '<unknown-file>',
405 $_[0]->{'-line'} || 0);
406 return (wantarray) ? @loc : join(':', @loc);
409 ##---------------------------------------------------------------------------
411 #############################################################################
413 package Pod::InteriorSequence;
415 ##---------------------------------------------------------------------------
417 =head1 B<Pod::InteriorSequence>
419 An object representing a POD interior sequence command.
420 It has the following methods/attributes:
424 ##---------------------------------------------------------------------------
426 =head2 Pod::InteriorSequence-E<gt>B<new()>
428 my $pod_seq1 = Pod::InteriorSequence->new(-name => $cmd
429 -ldelim => $delimiter);
430 my $pod_seq2 = new Pod::InteriorSequence(-name => $cmd,
431 -ldelim => $delimiter);
432 my $pod_seq3 = new Pod::InteriorSequence(-name => $cmd,
433 -ldelim => $delimiter,
435 -line => $line_number);
437 my $pod_seq4 = new Pod::InteriorSequence(-name => $cmd, $ptree);
438 my $pod_seq5 = new Pod::InteriorSequence($cmd, $ptree);
440 This is a class method that constructs a C<Pod::InteriorSequence> object
441 and returns a reference to the new interior sequence object. It should
442 be given two keyword arguments. The C<-ldelim> keyword indicates the
443 corresponding left-delimiter of the interior sequence (e.g. 'E<lt>').
444 The C<-name> keyword indicates the name of the corresponding interior
445 sequence command, such as C<I> or C<B> or C<C>. The C<-file> and
446 C<-line> keywords indicate the filename and line number corresponding
447 to the beginning of the interior sequence. If the C<$ptree> argument is
448 given, it must be the last argument, and it must be either string, or
449 else an array-ref suitable for passing to B<Pod::ParseTree::new> (or
450 it may be a reference to an Pod::ParseTree object).
455 ## Determine if we were called via an object-ref or a classname
457 my $class = ref($this) || $this;
459 ## See if first argument has no keyword
460 if (((@_ <= 2) or (@_ % 2)) and $_[0] !~ /^-\w/) {
461 ## Yup - need an implicit '-name' before first parameter
465 ## See if odd number of args
467 ## Yup - need an implicit '-ptree' before the last parameter
468 splice @_, $#_, 0, '-ptree';
471 ## Any remaining arguments are treated as initial values for the
472 ## hash that is used to represent this object. Note that we default
473 ## certain values by specifying them *before* the arguments passed.
474 ## If they are in the argument list, they will override the defaults.
476 -name => (@_ == 1) ? $_[0] : undef,
477 -file => '<unknown-file>',
484 ## Initialize contents if they havent been already
485 my $ptree = $self->{'-ptree'} || new Pod::ParseTree();
486 if ( ref $ptree =~ /^(ARRAY)?$/ ) {
487 ## We have an array-ref, or a normal scalar. Pass it as an
488 ## an argument to the ptree-constructor
489 $ptree = new Pod::ParseTree($1 ? [$ptree] : $ptree);
491 $self->{'-ptree'} = $ptree;
493 ## Bless ourselves into the desired class and perform any initialization
498 ##---------------------------------------------------------------------------
500 =head2 $pod_seq-E<gt>B<cmd_name()>
502 my $seq_cmd = $pod_seq->cmd_name();
504 The name of the interior sequence command.
509 (@_ > 1) and $_[0]->{'-name'} = $_[1];
510 return $_[0]->{'-name'};
513 ## let name() be an alias for cmd_name()
516 ##---------------------------------------------------------------------------
518 ## Private subroutine to set the parent pointer of all the given
519 ## children that are interior-sequences to be $self
521 sub _set_child2parent_links {
522 my ($self, @children) = @_;
523 ## Make sure any sequences know who their parent is
525 next unless (length and ref and ref ne 'SCALAR');
526 if (UNIVERSAL::isa($_, 'Pod::InteriorSequence') or
527 UNIVERSAL::can($_, 'nested'))
534 ## Private subroutine to unset child->parent links
536 sub _unset_child2parent_links {
538 $self->{'-parent_sequence'} = undef;
539 my $ptree = $self->{'-ptree'};
541 next unless (length and ref and ref ne 'SCALAR');
542 $_->_unset_child2parent_links()
543 if UNIVERSAL::isa($_, 'Pod::InteriorSequence');
547 ##---------------------------------------------------------------------------
549 =head2 $pod_seq-E<gt>B<prepend()>
551 $pod_seq->prepend($text);
552 $pod_seq1->prepend($pod_seq2);
554 Prepends the given string or parse-tree or sequence object to the parse-tree
555 of this interior sequence.
561 $self->{'-ptree'}->prepend(@_);
562 _set_child2parent_links($self, @_);
566 ##---------------------------------------------------------------------------
568 =head2 $pod_seq-E<gt>B<append()>
570 $pod_seq->append($text);
571 $pod_seq1->append($pod_seq2);
573 Appends the given string or parse-tree or sequence object to the parse-tree
574 of this interior sequence.
580 $self->{'-ptree'}->append(@_);
581 _set_child2parent_links($self, @_);
585 ##---------------------------------------------------------------------------
587 =head2 $pod_seq-E<gt>B<nested()>
589 $outer_seq = $pod_seq->nested || print "not nested";
591 If this interior sequence is nested inside of another interior
592 sequence, then the outer/parent sequence that contains it is
593 returned. Otherwise C<undef> is returned.
599 (@_ == 1) and $self->{'-parent_sequence'} = shift;
600 return $self->{'-parent_sequence'} || undef;
603 ##---------------------------------------------------------------------------
605 =head2 $pod_seq-E<gt>B<raw_text()>
607 my $seq_raw_text = $pod_seq->raw_text();
609 This method will return the I<raw> text of the POD interior sequence,
610 exactly as it appeared in the input.
616 my $text = $self->{'-name'} . $self->{'-ldelim'};
617 for ( $self->{'-ptree'}->children ) {
618 $text .= (ref $_) ? $_->raw_text : $_;
620 $text .= $self->{'-rdelim'};
624 ##---------------------------------------------------------------------------
626 =head2 $pod_seq-E<gt>B<left_delimiter()>
628 my $ldelim = $pod_seq->left_delimiter();
630 The leftmost delimiter beginning the argument text to the interior
631 sequence (should be "<").
636 (@_ > 1) and $_[0]->{'-ldelim'} = $_[1];
637 return $_[0]->{'-ldelim'};
640 ## let ldelim() be an alias for left_delimiter()
641 *ldelim = \&left_delimiter;
643 ##---------------------------------------------------------------------------
645 =head2 $pod_seq-E<gt>B<right_delimiter()>
647 The rightmost delimiter beginning the argument text to the interior
648 sequence (should be ">").
652 sub right_delimiter {
653 (@_ > 1) and $_[0]->{'-rdelim'} = $_[1];
654 return $_[0]->{'-rdelim'};
657 ## let rdelim() be an alias for right_delimiter()
658 *rdelim = \&right_delimiter;
660 ##---------------------------------------------------------------------------
662 =head2 $pod_seq-E<gt>B<parse_tree()>
664 my $ptree = $pod_parser->parse_text($paragraph_text);
665 $pod_seq->parse_tree( $ptree );
666 $ptree = $pod_seq->parse_tree();
668 This method will get/set the corresponding parse-tree of the interior
674 (@_ > 1) and $_[0]->{'-ptree'} = $_[1];
675 return $_[0]->{'-ptree'};
678 ## let ptree() be an alias for parse_tree()
679 *ptree = \&parse_tree;
681 ##---------------------------------------------------------------------------
683 =head2 $pod_seq-E<gt>B<file_line()>
685 my ($filename, $line_number) = $pod_seq->file_line();
686 my $position = $pod_seq->file_line();
688 Returns the current filename and line number for the interior sequence
689 object. If called in a list context, it returns a list of two
690 elements: first the filename, then the line number. If called in
691 a scalar context, it returns a string containing the filename, followed
692 by a colon (':'), followed by the line number.
697 my @loc = ($_[0]->{'-file'} || '<unknown-file>',
698 $_[0]->{'-line'} || 0);
699 return (wantarray) ? @loc : join(':', @loc);
702 ##---------------------------------------------------------------------------
704 =head2 Pod::InteriorSequence::B<DESTROY()>
706 This method performs any necessary cleanup for the interior-sequence.
707 If you override this method then it is B<imperative> that you invoke
708 the parent method from within your own method, otherwise
709 I<interior-sequence storage will not be reclaimed upon destruction!>
714 ## We need to get rid of all child->parent pointers throughout the
715 ## tree so their reference counts will go to zero and they can be
717 _unset_child2parent_links(@_);
720 ##---------------------------------------------------------------------------
722 #############################################################################
724 package Pod::ParseTree;
726 ##---------------------------------------------------------------------------
728 =head1 B<Pod::ParseTree>
730 This object corresponds to a tree of parsed POD text. As POD text is
731 scanned from left to right, it is parsed into an ordered list of
732 text-strings and B<Pod::InteriorSequence> objects (in order of
733 appearance). A B<Pod::ParseTree> object corresponds to this list of
734 strings and sequences. Each interior sequence in the parse-tree may
735 itself contain a parse-tree (since interior sequences may be nested).
739 ##---------------------------------------------------------------------------
741 =head2 Pod::ParseTree-E<gt>B<new()>
743 my $ptree1 = Pod::ParseTree->new;
744 my $ptree2 = new Pod::ParseTree;
745 my $ptree4 = Pod::ParseTree->new($array_ref);
746 my $ptree3 = new Pod::ParseTree($array_ref);
748 This is a class method that constructs a C<Pod::Parse_tree> object and
749 returns a reference to the new parse-tree. If a single-argument is given,
750 it must be a reference to an array, and is used to initialize the root
751 (top) of the parse tree.
756 ## Determine if we were called via an object-ref or a classname
758 my $class = ref($this) || $this;
760 my $self = (@_ == 1 and ref $_[0]) ? $_[0] : [];
762 ## Bless ourselves into the desired class and perform any initialization
767 ##---------------------------------------------------------------------------
769 =head2 $ptree-E<gt>B<top()>
771 my $top_node = $ptree->top();
772 $ptree->top( $top_node );
773 $ptree->top( @children );
775 This method gets/sets the top node of the parse-tree. If no arguments are
776 given, it returns the topmost node in the tree (the root), which is also
777 a B<Pod::ParseTree>. If it is given a single argument that is a reference,
778 then the reference is assumed to a parse-tree and becomes the new top node.
779 Otherwise, if arguments are given, they are treated as the new list of
780 children for the top node.
787 @{ $self } = (@_ == 1 and ref $_[0]) ? ${ @_ } : @_;
792 ## let parse_tree() & ptree() be aliases for the 'top' method
793 *parse_tree = *ptree = \⊤
795 ##---------------------------------------------------------------------------
797 =head2 $ptree-E<gt>B<children()>
799 This method gets/sets the children of the top node in the parse-tree.
800 If no arguments are given, it returns the list (array) of children
801 (each of which should be either a string or a B<Pod::InteriorSequence>.
802 Otherwise, if arguments are given, they are treated as the new list of
803 children for the top node.
810 @{ $self } = (@_ == 1 and ref $_[0]) ? ${ @_ } : @_;
815 ##---------------------------------------------------------------------------
817 =head2 $ptree-E<gt>B<prepend()>
819 This method prepends the given text or parse-tree to the current parse-tree.
820 If the first item on the parse-tree is text and the argument is also text,
821 then the text is prepended to the first item (not added as a separate string).
822 Otherwise the argument is added as a new string or parse-tree I<before>
827 use vars qw(@ptree); ## an alias used for performance reasons
831 local *ptree = $self;
834 if (@ptree and !(ref $ptree[0]) and !(ref $_)) {
835 $ptree[0] = $_ . $ptree[0];
843 ##---------------------------------------------------------------------------
845 =head2 $ptree-E<gt>B<append()>
847 This method appends the given text or parse-tree to the current parse-tree.
848 If the last item on the parse-tree is text and the argument is also text,
849 then the text is appended to the last item (not added as a separate string).
850 Otherwise the argument is added as a new string or parse-tree I<after>
857 local *ptree = $self;
860 if (@ptree and !(ref $ptree[-1]) and !(ref $_)) {
869 =head2 $ptree-E<gt>B<raw_text()>
871 my $ptree_raw_text = $ptree->raw_text();
873 This method will return the I<raw> text of the POD parse-tree
874 exactly as it appeared in the input.
882 $text .= (ref $_) ? $_->raw_text : $_;
887 ##---------------------------------------------------------------------------
889 ## Private routines to set/unset child->parent links
891 sub _unset_child2parent_links {
893 local *ptree = $self;
895 next unless (length and ref and ref ne 'SCALAR');
896 $_->_unset_child2parent_links()
897 if UNIVERSAL::isa($_, 'Pod::InteriorSequence');
901 sub _set_child2parent_links {
902 ## nothing to do, Pod::ParseTrees cant have parent pointers
905 =head2 Pod::ParseTree::B<DESTROY()>
907 This method performs any necessary cleanup for the parse-tree.
908 If you override this method then it is B<imperative>
909 that you invoke the parent method from within your own method,
910 otherwise I<parse-tree storage will not be reclaimed upon destruction!>
915 ## We need to get rid of all child->parent pointers throughout the
916 ## tree so their reference counts will go to zero and they can be
918 _unset_child2parent_links(@_);
921 #############################################################################
925 See L<Pod::Parser>, L<Pod::Select>
929 Brad Appleton E<lt>bradapp@enteract.comE<gt>