PPIx::Regexp::Element - Base of the PPIx::Regexp hierarchy.


PPIx-Regexp documentation Contained in the PPIx-Regexp distribution.

Index


Code Index:

NAME

Top

PPIx::Regexp::Element - Base of the PPIx::Regexp hierarchy.

SYNOPSIS

Top

No user-serviceable parts inside.

INHERITANCE

Top

PPIx::Regexp::Element is not descended from any other class.

PPIx::Regexp::Element is the parent of PPIx::Regexp::Node and PPIx::Regexp::Token.

DESCRIPTION

Top

This class is the base of the PPIx::Regexp object hierarchy. It provides the same kind of navigational functionality that is provided by PPI::Element.

METHODS

Top

This class provides the following public methods. Methods not documented here are private, and unsupported in the sense that the author reserves the right to change or remove them without notice.

ancestor_of

This method returns true if the object is an ancestor of the argument, and false otherwise. By the definition of this method, $self is its own ancestor.

can_be_quantified

 $token->can_be_quantified()
     and print "This element can be quantified.\n";

This method returns true if the element can be quantified.

class

This method returns the class name of the element. It is the same as ref $self.

comment

This method returns true if the element is a comment and false otherwise.

content

This method returns the content of the element.

descendant_of

This method returns true if the object is a descendant of the argument, and false otherwise. By the definition of this method, $self is its own descendant.

is_quantifier

 $token->is_quantifier()
     and print "This element is a quantifier.\n";

This method returns true if the element is a quantifier. You can not tell this from the element's class, because a right curly bracket may represent a quantifier for the purposes of figuring out whether a greediness token is possible.

next_sibling

This method returns the element's next sibling, or nothing if there is none.

parent

This method returns the parent of the element, or undef if there is none.

perl_version_introduced

This method returns the version of Perl in which the element was introduced. This will be at least 5.000. Before 5.006 I am relying on the perldelta, perlre, and perlop documentation, since I have been unable to build earlier Perls. Since I have found no documentation before 5.003, I assume that anything found in 5.003 is also in 5.000.

Since this all depends on my ability to read and understand masses of documentation, the results of this method should be viewed with caution, if not downright skepticism.

There are also cases which are ambiguous in various ways. For those see RESTRICTIONS in PPIx::Regexp, and especially Changes in Syntax in PPIx::Regexp.

perl_version_removed

This method returns the version of Perl in which the element was removed. If the element is still valid the return is undef.

All the caveats to perl_version_introduced() apply here also, though perhaps less severely since although many features have been introduced since 5.0, few have been removed.

previous_sibling

This method returns the element's previous sibling, or nothing if there is none.

significant

This method returns true if the element is significant and false otherwise.

snext_sibling

This method returns the element's next significant sibling, or nothing if there is none.

sprevious_sibling

This method returns the element's previous significant sibling, or nothing if there is none.

tokens

This method returns all tokens contained in the element.

top

This method returns the top of the hierarchy.

whitespace

This method returns true if the element is whitespace and false otherwise.

SUPPORT

Top

Support is by the author. Please file bug reports at http://rt.cpan.org, or in electronic mail to the author.

AUTHOR

Top

Thomas R. Wyant, III wyant at cpan dot org

COPYRIGHT AND LICENSE

Top


PPIx-Regexp documentation Contained in the PPIx-Regexp distribution.
package PPIx::Regexp::Element;

use strict;
use warnings;

use 5.006;

use List::MoreUtils qw{ firstidx };
use PPIx::Regexp::Util qw{ __instance };
use Scalar::Util qw{ refaddr weaken };

use PPIx::Regexp::Constant qw{ MINIMUM_PERL };

our $VERSION = '0.020';

sub ancestor_of {
    my ( $self, $elem ) = @_;
    __instance( $elem, __PACKAGE__ ) or return;
    my $addr = refaddr( $self );
    while ( $addr != refaddr( $elem ) ) {
	$elem = $elem->_parent() or return;
    }
    return 1;
}

sub can_be_quantified { return 1; }


sub class {
    my ( $self ) = @_;
    return ref $self;
}

sub comment {
    return;
}

sub content {
    return;
}

sub descendant_of {
    my ( $self, $node ) = @_;
    __instance( $node, __PACKAGE__ ) or return;
    return $node->ancestor_of( $self );
}


sub is_quantifier { return; }

sub next_sibling {
    my ( $self ) = @_;
    my ( $method, $inx ) = $self->_my_inx()
	or return;
    return $self->_parent()->$method( $inx + 1 );
}

sub parent {
    my ( $self ) = @_;
    return $self->_parent();
}

sub perl_version_introduced {
    return MINIMUM_PERL;
}

sub perl_version_removed {
    return undef;	## no critic (ProhibitExplicitReturnUndef)
}

sub previous_sibling {
    my ( $self ) = @_;
    my ( $method, $inx ) = $self->_my_inx()
	or return;
    $inx or return;
    return $self->_parent()->$method( $inx - 1 );
}

sub significant {
    return 1;
}

sub snext_sibling {
    my ( $self ) = @_;
    my $sib = $self;
    while ( defined ( $sib = $sib->next_sibling() ) ) {
	$sib->significant() and return $sib;
    }
    return;
}

sub sprevious_sibling {
    my ( $self ) = @_;
    my $sib = $self;
    while ( defined ( $sib = $sib->previous_sibling() ) ) {
	$sib->significant() and return $sib;
    }
    return;
}

sub tokens {
    my ( $self ) = @_;
    return $self;
}

sub top {
    my ( $self ) = @_;
    my $kid = $self;
    while ( defined ( my $parent = $kid->_parent() ) ) {
	$kid = $parent;
    }
    return $kid;
}

sub whitespace {
    return;
}

sub nav {
    my ( $self ) = @_;
    __instance( $self, __PACKAGE__ ) or return;

    # We do not use $self->parent() here because PPIx::Regexp overrides
    # this to return the (possibly) PPI object that initiated us.
    my $parent = $self->_parent() or return;

    return ( $parent->nav(), $parent->_nav( $self ) );
}

# Find our location and index among the parent's children. If not found,
# just returns.

{
    my %method_map = (
	children => 'child',
    );
    sub _my_inx {
	my ( $self ) = @_;
	my $parent = $self->_parent() or return;
	my $addr = refaddr( $self );
	foreach my $method ( qw{ children start type finish } ) {
	    $parent->can( $method ) or next;
	    my $inx = firstidx { refaddr $_ == $addr } $parent->$method();
	    $inx < 0 and next;
	    return ( $method_map{$method} || $method, $inx );
	}
	return;
    }
}

{
    my %parent;

    # no-argument form returns the parent; one-argument sets it.
    sub _parent {
	my ( $self, @arg ) = @_;
	my $addr = refaddr( $self );
	if ( @arg ) {
	    my $parent = shift @arg;
	    if ( defined $parent ) {
		__instance( $parent, __PACKAGE__ ) or return;
		weaken(
		    $parent{$addr} = $parent );
	    } else {
		delete $parent{$addr};
	    }
	}
	return $parent{$addr};
    }

    sub _parent_keys {
	return scalar keys %parent;
    }

}

# Called by the lexer to record the capture number.
sub __PPIX_LEXER__record_capture_number {
    my ( $self, $number ) = @_;
    return $number;
}

sub DESTROY {
    $_[0]->_parent( undef );
    return;
}

1;

__END__

# ex: set textwidth=72 :