PPI::Structure - The base class for Perl braced structures


PPI documentation Contained in the PPI distribution.

Index


Code Index:

NAME

Top

PPI::Structure - The base class for Perl braced structures

INHERITANCE

Top

  PPI::Structure
  isa PPI::Node
      isa PPI::Element

DESCRIPTION

Top

PPI::Structure is the root class for all Perl bracing structures. This covers all forms of [ ... ] , { ... } , and ( ... ) brace types, and includes cases where only one half of the pair exist.

The class PPI::Structure itself is full abstract and no objects of that type should actually exist in the tree.

Elements vs Children

A PPI::Structure has an unusual existance. Unlike a PPI::Document or PPI::Statement, which both simply contain other elements, a structure both contains and consists of content.

That is, the brace tokens are not considered to be "children" of the structure, but are part of it.

In practice, this will mean that while the ->elements and ->tokens methods (and related) will return a list with the brace tokens at either end, the ->children method explicitly will not return the brace.

STRUCTURE CLASSES

Top

Excluding the transient PPI::Structure::Unknown that exists briefly inside the parser, there are eight types of structure.

PPI::Structure::List

This covers all round braces used for function arguments, in foreach loops, literal lists, and braces used for precedence-ordering purposes.

PPI::Structure::For

Although not used for the foreach loop list, this is used for the special case of the round-brace three-part semicolon-seperated for loop expression (the traditional C style for loop).

PPI::Structure::Given

This is for the expression being matched in switch statements.

PPI::Structure::When

This is for the matching expression in "when" statements.

PPI::Structure::Condition

This round-brace structure covers boolean conditional braces, such as for if and while blocks.

PPI::Structure::Block

This curly-brace and common structure is used for all form of code blocks. This includes those for if, do and similar, as well as grep, map, sort, sub and (labelled or anonymous) scoping blocks.

PPI::Structure::Constructor

This class covers brace structures used for the construction of anonymous ARRAY and HASH references.

PPI::Structure::Subscript

This class covers square-braces and curly-braces used after a -> pointer to access the subscript of an ARRAY or HASH.

METHODS

Top

PPI::Structure itself has very few methods. Most of the time, you will be working with the more generic PPI::Element or PPI::Node methods, or one of the methods that are subclass-specific.

start

For lack of better terminology (like "open" and "close") that has not already in use for some other more important purpose, the two individual braces for the structure are known within PPI as the "start" and "finish" braces (at least for method purposes).

The start method returns the start brace for the structure (i.e. the opening brace).

Returns the brace as a PPI::Token::Structure or undef if the structure does not have a starting brace.

Under normal parsing circumstances this should never occur, but may happen due to manipulation of the PDOM tree.

finish

The finish method returns the finish brace for the structure (i.e. the closing brace).

Returns the brace as a PPI::Token::Structure or undef if the structure does not have a finishing brace. This can be quite common if the document is not complete (for example, from an editor where the user may be halfway through typeing a subroutine).

braces

The braces method is a utility method which returns the brace type, regardless of whether has both braces defined, or just the starting brace, or just the ending brace.

Returns on of the three strings '[]', '{}', or '()', or undef on error (primarily not having a start brace, as mentioned above).

complete

Top

The complete method is a convenience method that returns true if the both braces are defined for the structure, or false if only one brace is defined.

Unlike the top level complete method which checks for completeness in depth, the structure complete method ONLY confirms completeness for the braces, and does not recurse downwards.

SUPPORT

Top

See the support section in the main module.

AUTHOR

Top

Adam Kennedy <adamk@cpan.org>

COPYRIGHT

Top


PPI documentation Contained in the PPI distribution.
package PPI::Structure;

use strict;
use Scalar::Util   ();
use Params::Util   qw{_INSTANCE};
use PPI::Node      ();
use PPI::Exception ();

use vars qw{$VERSION @ISA *_PARENT};
BEGIN {
	$VERSION = '1.215';
	@ISA     = 'PPI::Node';
	*_PARENT = *PPI::Element::_PARENT;
}

use PPI::Structure::Block       ();
use PPI::Structure::Condition   ();
use PPI::Structure::Constructor ();
use PPI::Structure::For         ();
use PPI::Structure::Given       ();
use PPI::Structure::List        ();
use PPI::Structure::Subscript   ();
use PPI::Structure::Unknown     ();
use PPI::Structure::When        ();





#####################################################################
# Constructor

sub new {
	my $class = shift;
	my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;

	# Create the object
	my $self = bless {
		children => [],
		start    => $Token,
		}, $class;

	# Set the start braces parent link
	Scalar::Util::weaken(
		$_PARENT{Scalar::Util::refaddr $Token} = $self
	);

	$self;
}





#####################################################################
# PPI::Structure API methods

sub start  { $_[0]->{start}  }

sub finish { $_[0]->{finish} }

sub braces {
	my $self = $_[0]->{start} ? shift : return undef;
	return {
		'[' => '[]',
		'(' => '()',
		'{' => '{}',
	}->{ $self->{start}->{content} };
}

sub complete {
	!! ($_[0]->{start} and $_[0]->{finish});
}





#####################################################################
# PPI::Node overloaded methods

# For us, the "elements" concept includes the brace tokens
sub elements {
	my $self = shift;

	if ( wantarray ) {
		# Return a list in array context
		return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
	} else {
		# Return the number of elements in scalar context.
		# This is memory-cheaper than creating another big array
		return scalar(@{$self->{children}})
			+ ($self->{start}  ? 1 : 0)
			+ ($self->{finish} ? 1 : 0);
	}
}

# For us, the first element is probably the opening brace
sub first_element {
	# Technically, if we have no children and no opening brace,
	# then the first element is the closing brace.
	$_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
}

# For us, the last element is probably the closing brace
sub last_element {
	# Technically, if we have no children and no closing brace,
	# then the last element is the opening brace
	$_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
}

# Location is same as the start token, if any
sub location {
	my $self  = shift;
	my $first = $self->first_element or return undef;
	$first->location;
}





#####################################################################
# PPI::Element overloaded methods

# Get the full set of tokens, including start and finish
sub tokens {
	my $self = shift;
	my @tokens = (
		$self->{start}  || (),
		$self->SUPER::tokens(@_),
		$self->{finish} || (),
		);
	@tokens;
}

# Like the token method ->content, get our merged contents.
# This will recurse downwards through everything
### Reimplement this using List::Utils stuff
sub content {
	my $self = shift;
	my $content = $self->{start} ? $self->{start}->content : '';
	foreach my $child ( @{$self->{children}} ) {
		$content .= $child->content;
	}
	$content .= $self->{finish}->content if $self->{finish};
	$content;
}

# Is the structure completed
sub _complete {
	!! ( defined $_[0]->{finish} );
}

# You can insert either another structure, or a token
sub insert_before {
	my $self    = shift;
	my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
	if ( $Element->isa('PPI::Structure') ) {
		return $self->__insert_before($Element);
	} elsif ( $Element->isa('PPI::Token') ) {
		return $self->__insert_before($Element);
	}
	'';
}

# As above, you can insert either another structure, or a token
sub insert_after {
	my $self    = shift;
	my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
	if ( $Element->isa('PPI::Structure') ) {
		return $self->__insert_after($Element);
	} elsif ( $Element->isa('PPI::Token') ) {
		return $self->__insert_after($Element);
	}
	'';
}

1;