PPIx::Regexp::Node - Represent a container


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

Index


Code Index:

NAME

Top

PPIx::Regexp::Node - Represent a container

SYNOPSIS

Top

 use PPIx::Regexp::Dumper;
 PPIx::Regexp::Dumper->new( 'qr{(foo)}' )->print();

INHERITANCE

Top

PPIx::Regexp::Node is a PPIx::Regexp::Element.

PPIx::Regexp::Node is the parent of PPIx::Regexp, PPIx::Regexp::Node::Range and PPIx::Regexp::Structure.

DESCRIPTION

Top

This class represents a structural element that contains other classes. It is an abstract class, not instantiated by the lexer.

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.

child

 my $kid = $node->child( 0 );

This method returns the child at the given index. The indices start from zero, and negative indices are from the end of the list, so that $node->child( -1 ) returns the last child of the node.

children

This method returns the children of the Node. If called in scalar context it returns the number of children.

contains

 print $node->contains( $elem ) ? "yes\n" : "no\n";

This method returns true if the given element is contained in the node, or false otherwise.

elements

This method returns the elements in the Node. For a PPIx::Regexp::Node proper, it is the same as children().

find

 my $rslt = $node->find( 'PPIx::Regexp::Token::Literal' );
 my $rslt = $node->find( 'Token::Literal' );
 my $rslt = $node->find( sub {
     return $_[1]->isa( 'PPIx::Regexp::Token::Literal' )
	 && $_[1]->ordinal < ord(' ');
     } );

This method finds things.

If given a string as argument, it is assumed to be a class name (possibly without the leading 'PPIx::Regexp::'), and all elements of the given class are found.

If given a code reference, that code reference is called once for each element, and passed $self and the element. The code should return true to accept the element, false to reject it, and ( for subclasses of PPIx::Regexp::Node) undef to prevent recursion into the node. If the code throws an exception, you get nothing back from this method.

Either way, the return is a reference to the list of things found, a false (but defined) value if nothing was found, or undef if an error occurred.

find_parents

 my $rslt = $node->find_parents( sub {
     return $_[1]->isa( 'PPIx::Regexp::Token::Operator' )
         && $_[1]->content() eq '|';
     } );

This convenience method takes the same arguments as find, but instead of the found objects themselves returns their parents. No parent will appear more than once in the output.

The return is a reference to the array of parents if any were found. If none were found the return is false but defined. If an error occurred the return is undef.

find_first

This method has the same arguments as find, but returns either a reference to the first element found, a false (but defined) value if no elements were found, or undef if an error occurred.

first_element

This method returns the first element in the node.

last_element

This method returns the last element in the node.

perl_version_introduced

This method returns the maximum value of perl_version_introduced returned by any of its elements. In other words, it returns the minimum version of Perl under which this node is valid. If there are no elements, 5.006 is returned, since that is the minimum value of Perl supported by this package.

perl_version_removed

This method returns the minimum defined value of perl_version_removed returned by any of the node's elements. In other words, it returns the lowest version of Perl in which this node is not valid. If there are no elements, or if no element has a defined perl_version_removed, undef is returned.

schild

This method returns the significant child at the given index; that is, $node->schild(0) returns the first significant child, $node->schild(1) returns the second significant child, and so on. Negative indices count from the end.

schildren

This method returns the significant children of the node.

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::Node;

use strict;
use warnings;

use base qw{ PPIx::Regexp::Element };

use List::Util qw{ max };
use PPIx::Regexp::Constant qw{ MINIMUM_PERL };
use PPIx::Regexp::Util qw{ __instance };
use Scalar::Util qw{ refaddr };

our $VERSION = '0.020';

sub _new {
    my ( $class, @children ) = @_;
    ref $class and $class = ref $class;
    foreach my $elem ( @children ) {
	__instance( $elem, 'PPIx::Regexp::Element' ) or return;
    }
    my $self = {
	children => \@children,
    };
    bless $self, $class;
    foreach my $elem ( @children ) {
	$elem->_parent( $self );
    }
    return $self;
}

sub child {
    my ( $self, $inx ) = @_;
    defined $inx or $inx = 0;
    return $self->{children}[$inx];
}

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

sub contains {
    my ( $self, $elem ) = @_;
    __instance( $elem, 'PPIx::Regexp::Element' ) or return;

    my $addr = refaddr( $self );

    while ( $elem = $elem->parent() ) {
	$addr == refaddr( $elem ) and return 1;
    }

    return;
}

sub content {
    my ( $self ) = @_;
    return join( '', map{ $_->content() } $self->elements() );
}

{
    no warnings qw{ once };
    *elements = \&children;
}

sub _find_routine {
    my ( $want ) = @_;
    ref $want eq 'CODE' and return $want;
    ref $want and return;
    $want =~ m/ \A PPIx::Regexp:: /smx
	or $want = 'PPIx::Regexp::' . $want;
    return sub {
	return __instance( $_[1], $want ) ? 1 : 0;
    };
}

sub find {
    my ( $self, $want ) = @_;

    $want = _find_routine( $want ) or return;

    my @found;

    # We use a recursion to find what we want. PPI::Node uses an
    # iteration.
    foreach my $elem ( $self->elements() ) {
	my $rslt = eval { $want->( $self, $elem ) }
	    and push @found, $elem;
	$@ and return;

	__instance( $elem, 'PPIx::Regexp::Node' ) or next;
	defined $rslt or next;
	$rslt = $elem->find( $want )
	    and push @found, @{ $rslt };
    }

    return @found ? \@found : 0;

}

sub find_parents {
    my ( $self, $want ) = @_;

    my $found;
    $found = $self->find( $want ) or return $found;

    my %parents;
    my @rslt;
    foreach my $elem ( @{ $found } ) {
	my $dad = $elem->parent() or next;
	$parents{ refaddr( $dad ) }++
	    or push @rslt, $dad;
    }

    return \@rslt;
}

sub find_first {
    my ( $self, $want ) = @_;

    $want = _find_routine( $want ) or return;

    # We use a recursion to find what we want. PPI::Node uses an
    # iteration.
    foreach my $elem ( $self->elements() ) {
	my $rslt = eval { $want->( $self, $elem ) }
	    and return $elem;
	$@ and return;

	__instance( $elem, 'PPIx::Regexp::Node' ) or next;
	defined $rslt or next;

	defined( $rslt = $elem->find_first( $want ) )
	    or return;
	$rslt and return $rslt;
    }

    return 0;

}

sub first_element {
    my ( $self ) = @_;
    return $self->{children}[0];
}

sub last_element {
    my ( $self ) = @_;
    return $self->{children}[-1];
}

sub perl_version_introduced {
    my ( $self ) = @_;
    return max( MINIMUM_PERL,
	map { $_->perl_version_introduced() } $self->elements() );
}

sub perl_version_removed {
    my ( $self ) = @_;
    my $max;
    foreach my $elem ( $self->elements() ) {
	if ( defined ( my $ver = $elem->perl_version_removed() ) ) {
	    if ( defined $max ) {
		$ver < $max and $max = $ver;
	    } else {
		$max = $ver;
	    }
	}
    }
    return $max;
}

sub schild {
    my ( $self, $inx ) = @_;
    defined $inx or $inx = 0;

    my $kids = $self->{children};

    if ( $inx >= 0 ) {

	my $loc = 0;

	while ( exists $kids->[$loc] ) {
	    $kids->[$loc]->significant() or next;
	    --$inx >= 0 and next;
	    return $kids->[$loc];
	} continue {
	    $loc++;
	}

    } else {

	my $loc = -1;
	
	while ( exists $kids->[$loc] ) {
	    $kids->[$loc]->significant() or next;
	    $inx++ < -1 and next;
	    return $kids->[$loc];
	} continue {
	    --$loc;
	}

    }

    return;
}

sub schildren {
    my ( $self ) = @_;
    if ( wantarray ) {
	return ( grep { $_->significant() } @{ $self->{children} } );
    } elsif ( defined wantarray ) {
	my $kids = 0;
	foreach ( @{ $self->{children} } ) {
	    $_->significant() and $kids++;
	}
	return $kids;
    } else {
	return;
    }
}

sub tokens {
    my ( $self ) = @_;
    return ( map { $_->tokens() } $self->elements() );
}

# Help for nav();
sub _nav {
    my ( $self, $child ) = @_;
    refaddr( $child->parent() ) == refaddr( $self )
	or return;
    my ( $method, $inx ) = $child->_my_inx()
	or return;

    return ( $method => [ $inx ] );
}

# Called by the lexer once it has done its worst to all the tokens.
# Called as a method with no arguments. The return is the number of
# parse failures discovered when finalizing.
sub __PPIX_LEXER__finalize {
    my ( $self ) = @_;
    my $rslt = 0;
    foreach my $elem ( $self->elements() ) {
	$rslt += $elem->__PPIX_LEXER__finalize();
    }
    return $rslt;
}

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

1;

__END__

# ex: set textwidth=72 :