HH::Unispool::Config::File::Tokenizer - file tokenizer module


HH-Unispool-Config documentation Contained in the HH-Unispool-Config distribution.

Index


Code Index:

NAME

Top

HH::Unispool::Config::File::Tokenizer - file tokenizer module

SYNOPSIS

Top

Application programmers don't need to use this class and API programmers read code.

ABSTRACT

Top

file tokenizer module

DESCRIPTION

Top

HH::Unispool::Config::File::Tokenizer generates HH::Unispool::Config::File::Token objects from an IO::Handle.

CONSTRUCTOR

Top

new(OPT_HASH_REF)

Creates a new HH::Unispool::Config::File::Tokenizer object. OPT_HASH_REF is a hash reference used to pass initialization options. OPT_HASH_REF is mandatory. On error an exception Error::Simple is thrown.

Options for OPT_HASH_REF may include:

file

Passed to set_file(). Mandatory option.

got_buffer

Passed to set_got_buffer(). Must be an ARRAY reference.

unget_buffer

Passed to set_unget_buffer(). Must be an ARRAY reference.

METHODS

Top

exists_got_buffer(ARRAY)

Returns the count of items in ARRAY that are in the got buffer.

exists_unget_buffer(ARRAY)

Returns the count of items in ARRAY that are in the unget buffer.

get()

Either unget a HH::Unispool::Config::File::Token from the unget buffer and return it or read a line from the file, make a HH::Unispool::Config::File::Token out of it and return it. On error a Error::Simple exception is thrown.

get_file()

Returns the file handle to read from.

get_got_buffer( [ INDEX_ARRAY ] )

Returns an ARRAY containing the got buffer. INDEX_ARRAY is an optional list of indexes which when specified causes only the indexed elements in the ordered list to be returned. If not specified, all elements are returned.

get_unget_buffer( [ INDEX_ARRAY ] )

Returns an ARRAY containing the unget buffer. INDEX_ARRAY is an optional list of indexes which when specified causes only the indexed elements in the ordered list to be returned. If not specified, all elements are returned.

input_line_number()

Return the last read input line number.

pop_got_buffer()

Pop and return an element off the got buffer. On error an exception Error::Simple is thrown.

pop_unget_buffer()

Pop and return an element off the unget buffer. On error an exception Error::Simple is thrown.

push_got_buffer(ARRAY)

Push additional values on the got buffer. ARRAY is the list value. On error an exception Error::Simple is thrown.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

push_unget_buffer(ARRAY)

Push additional values on the unget buffer. ARRAY is the list value. On error an exception Error::Simple is thrown.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

set_file(VALUE)

Set the file handle to read from. VALUE is the value. VALUE may not be undef. On error an exception Error::Simple is thrown.

VALUE must be a (sub)class of:

IO::Handle

set_got_buffer(ARRAY)

Set the got buffer absolutely. ARRAY is the list value. On error an exception Error::Simple is thrown.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

set_idx_got_buffer( INDEX, VALUE )

Set value in the got buffer. INDEX is the integer index which is greater than or equal to 0. VALUE is the value.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

set_idx_unget_buffer( INDEX, VALUE )

Set value in the unget buffer. INDEX is the integer index which is greater than or equal to 0. VALUE is the value.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

set_num_got_buffer( NUMBER, VALUE )

Set value in the got buffer. NUMBER is the integer index which is greater than 0. VALUE is the value.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

set_num_unget_buffer( NUMBER, VALUE )

Set value in the unget buffer. NUMBER is the integer index which is greater than 0. VALUE is the value.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

set_unget_buffer(ARRAY)

Set the unget buffer absolutely. ARRAY is the list value. On error an exception Error::Simple is thrown.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

shift_got_buffer()

Shift and return an element off the got buffer. On error an exception Error::Simple is thrown.

shift_unget_buffer()

Shift and return an element off the unget buffer. On error an exception Error::Simple is thrown.

unget()

Unget the last token by moving it from the got buffer to the unget buffer. On error a Error::Simple exception is thrown.

unshift_got_buffer(ARRAY)

Unshift additional values on the got buffer. ARRAY is the list value. On error an exception Error::Simple is thrown.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

unshift_unget_buffer(ARRAY)

Unshift additional values on the unget buffer. ARRAY is the list value. On error an exception Error::Simple is thrown.

The values in ARRAY must be a (sub)class of:

HH::Unispool::Config::File::Token

SEE ALSO

Top

HH::Unispool::Config, HH::Unispool::Config::DateFormat, HH::Unispool::Config::Entry, HH::Unispool::Config::Entry::Device, HH::Unispool::Config::Entry::Device::0, HH::Unispool::Config::Entry::Device::1, HH::Unispool::Config::Entry::Device::2, HH::Unispool::Config::Entry::Device::3, HH::Unispool::Config::Entry::Device::4, HH::Unispool::Config::Entry::Device::5, HH::Unispool::Config::Entry::Device::6, HH::Unispool::Config::Entry::Device::7, HH::Unispool::Config::Entry::Filter, HH::Unispool::Config::Entry::Numbered, HH::Unispool::Config::Entry::RemoteSystem, HH::Unispool::Config::Entry::RemoteSystem::3, HH::Unispool::Config::Entry::RemoteSystem::6, HH::Unispool::Config::Entry::RemoteSystem::7, HH::Unispool::Config::Entry::RemoteSystem::8, HH::Unispool::Config::Entry::System, HH::Unispool::Config::ExecPri, HH::Unispool::Config::File::Token, HH::Unispool::Config::File::Token::Comment, HH::Unispool::Config::File::Token::Comment::Cs, HH::Unispool::Config::File::Token::Comment::Date, HH::Unispool::Config::File::Token::Comment::Device, HH::Unispool::Config::File::Token::Comment::Filter, HH::Unispool::Config::File::Token::Comment::Group, HH::Unispool::Config::File::Token::Comment::Head, HH::Unispool::Config::File::Token::Comment::Misc, HH::Unispool::Config::File::Token::Comment::NetDesc, HH::Unispool::Config::File::Token::Comment::RSys, HH::Unispool::Config::File::Token::Comment::Scope, HH::Unispool::Config::File::Token::Comment::Tail, HH::Unispool::Config::File::Token::Factory, HH::Unispool::Config::File::Token::Numbered, HH::Unispool::Config::File::Token::Numbered::Device, HH::Unispool::Config::File::Token::Numbered::Device::0, HH::Unispool::Config::File::Token::Numbered::Device::1, HH::Unispool::Config::File::Token::Numbered::Device::2, HH::Unispool::Config::File::Token::Numbered::Device::3, HH::Unispool::Config::File::Token::Numbered::Device::4, HH::Unispool::Config::File::Token::Numbered::Device::5, HH::Unispool::Config::File::Token::Numbered::Device::6, HH::Unispool::Config::File::Token::Numbered::Device::7, HH::Unispool::Config::File::Token::Numbered::Device::Group, HH::Unispool::Config::File::Token::Numbered::Device::Info, HH::Unispool::Config::File::Token::Numbered::Device::P, HH::Unispool::Config::File::Token::Numbered::Network, HH::Unispool::Config::File::Token::Numbered::System, HH::Unispool::Config::File::Token::Numbered::System::3, HH::Unispool::Config::File::Token::Numbered::System::6, HH::Unispool::Config::File::Token::Numbered::System::7, HH::Unispool::Config::File::Token::Numbered::System::8, HH::Unispool::Config::File::Token::Numbered::System::Info, HH::Unispool::Config::File::Token::Numbered::X, HH::Unispool::Config::File::Token::Unnumbered, HH::Unispool::Config::File::Token::Unnumbered::Bcs, HH::Unispool::Config::File::Token::Unnumbered::CSN, HH::Unispool::Config::File::Token::Unnumbered::Cs, HH::Unispool::Config::File::Token::Unnumbered::CsBcs, HH::Unispool::Config::File::Token::Unnumbered::Filter, HH::Unispool::Config::File::Token::Unnumbered::Host, HH::Unispool::Config::File::Token::Unnumbered::Misc, HH::Unispool::Config::FilterType, HH::Unispool::Config::OS, HH::Unispool::Config::Scope

BUGS

Top

None known (yet.)

HISTORY

Top

First development: February 2003 Last update: September 2003

AUTHOR

Top

Vincenzo Zocca

COPYRIGHT

Top

LICENSE

Top

This file is part of the HH::Unispool::Config module hierarchy for Perl by Vincenzo Zocca.

The HH::Unispool::Config module hierarchy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

The HH::Unispool::Config module hierarchy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the HH::Unispool::Config module hierarchy; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


HH-Unispool-Config documentation Contained in the HH-Unispool-Config distribution.

package HH::Unispool::Config::File::Tokenizer;

use 5.006;
use strict;
use warnings;
use AutoLoader qw(AUTOLOAD);
use Error qw(:try);

# Used by _value_is_allowed
our %ALLOW_ISA = (
    'file' => [ 'IO::Handle' ],
    'got_buffer' => [ 'HH::Unispool::Config::File::Token' ],
    'unget_buffer' => [ 'HH::Unispool::Config::File::Token' ],
);

# Used by _value_is_allowed
our %ALLOW_REF = (
);

# Used by _value_is_allowed
our %ALLOW_RX = (
);

# Used by _value_is_allowed
our %ALLOW_VALUE = (
);

# Package version
our ($VERSION) = '$Revision: 0.3 $' =~ /\$Revision:\s+([^\s]+)/;

1;

__END__

sub new {
    my $class = shift;

    my $self = {};
    bless( $self, ( ref($class) || $class ) );
    return( $self->_initialize(@_) );
}

sub _initialize {
    my $self = shift;
    my $opt = defined($_[0]) ? shift : {};

    # Check $opt
    ref($opt) eq 'HASH' || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::_initialize, first argument must be 'HASH' reference.");

    # file, SINGLE, mandatory
    exists( $opt->{file} ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::_initialize, option 'file' is mandatory.");
    $self->set_file( $opt->{file} );

    # got_buffer, MULTI
    if ( exists( $opt->{got_buffer} ) ) {
        ref( $opt->{got_buffer} ) eq 'ARRAY' || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::_initialize, specified value for option 'got_buffer' must be an 'ARRAY' reference.");
        $self->set_got_buffer( @{ $opt->{got_buffer} } );
    }
    else {
        $self->set_got_buffer();
    }

    # unget_buffer, MULTI
    if ( exists( $opt->{unget_buffer} ) ) {
        ref( $opt->{unget_buffer} ) eq 'ARRAY' || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::_initialize, specified value for option 'unget_buffer' must be an 'ARRAY' reference.");
        $self->set_unget_buffer( @{ $opt->{unget_buffer} } );
    }
    else {
        $self->set_unget_buffer();
    }

    # Return $self
    return($self);
}

sub _value_is_allowed {
    my $name = shift;

    # Value is allowed if no ALLOW clauses exist for the named attribute
    if ( ! exists( $ALLOW_ISA{$name} ) && ! exists( $ALLOW_REF{$name} ) && ! exists( $ALLOW_RX{$name} ) && ! exists( $ALLOW_VALUE{$name} ) ) {
        return(1);
    }

    # At this point, all values in @_ must to be allowed
    CHECK_VALUES:
    foreach my $val (@_) {
        # Check ALLOW_ISA
        if ( ref($val) && exists( $ALLOW_ISA{$name} ) ) {
            foreach my $class ( @{ $ALLOW_ISA{$name} } ) {
                &UNIVERSAL::isa( $val, $class ) && next CHECK_VALUES;
            }
        }

        # Check ALLOW_REF
        if ( ref($val) && exists( $ALLOW_REF{$name} ) ) {
            exists( $ALLOW_REF{$name}{ ref($val) } ) && next CHECK_VALUES;
        }

        # Check ALLOW_RX
        if ( defined($val) && ! ref($val) && exists( $ALLOW_RX{$name} ) ) {
            foreach my $rx ( @{ $ALLOW_RX{$name} } ) {
                $val =~ /$rx/ && next CHECK_VALUES;
            }
        }

        # Check ALLOW_VALUE
        if ( ! ref($val) && exists( $ALLOW_VALUE{$name} ) ) {
            exists( $ALLOW_VALUE{$name}{$val} ) && next CHECK_VALUES;
        }

        # We caught a not allowed value
        return(0);
    }

    # OK, all values are allowed
    return(1);
}

sub exists_got_buffer {
    my $self = shift;

    # Count occurrences
    my $count = 0;
    foreach my $val1 (@_) {
        foreach my $val2 ( @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} } ) {
            ( $val1 eq $val2 ) && $count ++;
        }
    }
    return($count);
}

sub exists_unget_buffer {
    my $self = shift;

    # Count occurrences
    my $count = 0;
    foreach my $val1 (@_) {
        foreach my $val2 ( @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} } ) {
            ( $val1 eq $val2 ) && $count ++;
        }
    }
    return($count);
}

sub get {
    my $self = shift;

    # The token to get
    my $tok;

    # Shift an token from the unget buffer
    if ( $tok = $self->pop_unget_buffer() ) {
        $self->push_got_buffer($tok);
    }

    # Or try making a token from the file
    else {
        # Read a non empty line
        my $line = undef;
        while ( $line = $self->get_file()->getline() ) {
            ( $line =~ /\S/ ) && last;
        }

        # Make a token out of the line
        if ($line) {
            use HH::Unispool::Config::File::Token::Factory;
            my $fact = HH::Unispool::Config::File::Token::Factory->instance();
            $tok = $fact->create_token( $line, $self->get_file()->input_line_number() );
            $self->push_got_buffer($tok);
        }
    }

    # Return the token
    return($tok);
}

sub get_file {
    my $self = shift;

    return( $self->{HH_Unispool_Config_File_Tokenizer}{file} );
}

sub get_got_buffer {
    my $self = shift;

    if ( scalar(@_) ) {
        my @ret = ();
        foreach my $i (@_) {
            push( @ret, $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer}[ int($i) ] );
        }
        return(@ret);
    }
    else {
        # Return the full list
        return( @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} } );
    }
}

sub get_unget_buffer {
    my $self = shift;

    if ( scalar(@_) ) {
        my @ret = ();
        foreach my $i (@_) {
            push( @ret, $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer}[ int($i) ] );
        }
        return(@ret);
    }
    else {
        # Return the full list
        return( @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} } );
    }
}

sub input_line_number {
    my $self = shift;

    my $prev = $self->pop_got_buffer();
    if ( defined($prev) ) {
        $self->push_got_buffer($prev);
        return( $prev->get_input_line_number() );
    }
    else {
        return(0);
    }
}

sub pop_got_buffer {
    my $self = shift;

    # Pop an element from the list
    return( pop( @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} } ) );
}

sub pop_unget_buffer {
    my $self = shift;

    # Pop an element from the list
    return( pop( @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} } ) );
}

sub push_got_buffer {
    my $self = shift;

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'got_buffer', @_ ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::push_got_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Push the list
    push( @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} }, @_ );
}

sub push_unget_buffer {
    my $self = shift;

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'unget_buffer', @_ ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::push_unget_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Push the list
    push( @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} }, @_ );
}

sub set_file {
    my $self = shift;
    my $val = shift;

    # Value for 'file' is not allowed to be empty
    defined($val) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_file, value may not be empty.");

    # Check if isa/ref/rx/value is allowed
    &_value_is_allowed( 'file', $val ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_file, the specified value '$val' is not allowed.");

    # Assignment
    $self->{HH_Unispool_Config_File_Tokenizer}{file} = $val;
}

sub set_got_buffer {
    my $self = shift;

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'got_buffer', @_ ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_got_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Set the list
    @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} } = @_;
}

sub set_idx_got_buffer {
    my $self = shift;
    my $idx = shift;
    my $val = shift;

    # Check if index is a positive integer or zero
    ( $idx == int($idx) ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_idx_got_buffer, the specified index '$idx' is not an integer.");
    ( $idx >= 0 ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_idx_got_buffer, the specified index '$idx' is not a positive integer or zero.");

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'got_buffer', $val ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_idx_got_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Set the value in the list
    $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer}[$idx] = $val;
}

sub set_idx_unget_buffer {
    my $self = shift;
    my $idx = shift;
    my $val = shift;

    # Check if index is a positive integer or zero
    ( $idx == int($idx) ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_idx_unget_buffer, the specified index '$idx' is not an integer.");
    ( $idx >= 0 ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_idx_unget_buffer, the specified index '$idx' is not a positive integer or zero.");

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'unget_buffer', $val ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_idx_unget_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Set the value in the list
    $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer}[$idx] = $val;
}

sub set_num_got_buffer {
    my $self = shift;
    my $num = shift;

    # Check if index is an integer
    ( $num == int($num) ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_num_got_buffer, the specified number '$num' is not an integer.");

    # Call set_idx_got_buffer
    $self->set_idx_got_buffer( $num - 1, @_ );
}

sub set_num_unget_buffer {
    my $self = shift;
    my $num = shift;

    # Check if index is an integer
    ( $num == int($num) ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_num_unget_buffer, the specified number '$num' is not an integer.");

    # Call set_idx_unget_buffer
    $self->set_idx_unget_buffer( $num - 1, @_ );
}

sub set_unget_buffer {
    my $self = shift;

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'unget_buffer', @_ ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::set_unget_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Set the list
    @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} } = @_;
}

sub shift_got_buffer {
    my $self = shift;

    # Shift an element from the list
    return( shift( @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} } ) );
}

sub shift_unget_buffer {
    my $self = shift;

    # Shift an element from the list
    return( shift( @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} } ) );
}

sub unget {
    my $self = shift;

    my $tok = $self->pop_got_buffer();
    defined($tok) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::unget, got buffer is empty.");
    $self->push_unget_buffer($tok);
}

sub unshift_got_buffer {
    my $self = shift;

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'got_buffer', @_ ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::unshift_got_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Unshift the list
    unshift( @{ $self->{HH_Unispool_Config_File_Tokenizer}{got_buffer} }, @_ );
}

sub unshift_unget_buffer {
    my $self = shift;

    # Check if isas/refs/rxs/values are allowed
    &_value_is_allowed( 'unget_buffer', @_ ) || throw Error::Simple("ERROR: HH::Unispool::Config::File::Tokenizer::unshift_unget_buffer, one or more specified value(s) '@_' is/are not allowed.");

    # Unshift the list
    unshift( @{ $self->{HH_Unispool_Config_File_Tokenizer}{unget_buffer} }, @_ );
}