my $tabsize = $self->[_tabsize_];
my $user_formatter = $self->[_user_formatter_];
- # create a source object for the buffer
- my $source_object = Perl::Tidy::LineSource->new(
- input_file => \$buf,
- rOpts => $rOpts,
- );
+ # create a source for the tokenizer .. we can use a string for efficiency.
+ my $source_buffer = $buf;
# make a debugger object if requested
my $debugger_object;
# create the tokenizer for this file
#-----------------------------------
my $tokenizer = Perl::Tidy::Tokenizer->new(
- source_object => $source_object,
+ source_object => \$source_buffer,
logger_object => $logger_object,
debugger_object => $debugger_object,
diagnostics_object => $diagnostics_object,
#---------------------------------
$self->process_single_case( $tokenizer, $formatter );
- #-----------------------------------------
- # close the input source and report errors
- #-----------------------------------------
- $source_object->close_input_file();
+ #--------------
+ # report errors
+ #--------------
# see if the formatter is converged
if ( $max_iterations > 1
if ( $iter < $max_iterations ) {
$sink_object->close_output_file();
- $source_object = Perl::Tidy::LineSource->new(
- input_file => \$sink_buffer,
- rOpts => $rOpts,
- );
+ $source_buffer = $sink_buffer;
# stop iterations if errors or converged
my $stop_now = $self->[_input_copied_verbatim_];
# we are stopping the iterations early;
# copy the output stream to its final destination
$sink_object = $sink_object_final;
- while ( my $line = $source_object->get_line() ) {
+ foreach my $line ( split /^/, $source_buffer ) {
$sink_object->write_line($line);
}
- $source_object->close_input_file();
last;
}
} ## end if ( $iter < $max_iterations)
_saw_data_ => $i++,
_saw_negative_indentation_ => $i++,
_started_tokenizing_ => $i++,
- _line_buffer_object_ => $i++,
_debugger_object_ => $i++,
_diagnostics_object_ => $i++,
_logger_object_ => $i++,
_rOpts_logfile_ => $i++,
_rOpts_ => $i++,
_calculate_ci_ => $i++,
+ _rinput_lines_ => $i++,
+ _input_line_index_next_ => $i++,
};
} ## end BEGIN
my $source_object = $args{source_object};
my $rOpts = $args{rOpts};
- # we create another object with a get_line() and peek_ahead() method
- my $line_buffer_object = Perl::Tidy::LineBuffer->new($source_object);
-
# Tokenizer state data is as follows:
# _rhere_target_list_ reference to list of here-doc targets
# _here_doc_target_ the target string for a here document
# _in_attribute_list_ flag telling if we are looking for attributes
# _in_quote_ flag telling if we are chasing a quote
# _starting_level_ indentation level of first line
- # _line_buffer_object_ object with get_line() method to supply source code
# _diagnostics_object_ place to write debugging information
# _unexpected_error_count_ error count used to limit output
# _lower_case_labels_at_ line numbers where lower case labels seen
$self->[_saw_data_] = 0;
$self->[_saw_negative_indentation_] = 0;
$self->[_started_tokenizing_] = 0;
- $self->[_line_buffer_object_] = $line_buffer_object;
$self->[_debugger_object_] = $args{debugger_object};
$self->[_diagnostics_object_] = $args{diagnostics_object};
$self->[_logger_object_] = $args{logger_object};
bless $self, $class;
- $self->prepare_for_a_new_file();
+ $self->prepare_for_a_new_file($source_object);
$self->find_starting_indentation_level();
# This is not a full class yet, so die if an attempt is made to
return $is_keyword{$str};
}
+#----------------------------------------------------------------
+# Line input routines, previously handled by the LineBuffer class
+#----------------------------------------------------------------
+sub make_source_array {
+
+ my ( $self, $line_source_object ) = @_;
+
+ # Convert the source into an array of lines
+ my $rinput_lines = [];
+
+ my $rsource = ref($line_source_object);
+
+ # handle a string
+ if ( !$rsource ) {
+ my @lines = split /^/, $line_source_object;
+ $rinput_lines = \@lines;
+ }
+
+ # handle an ARRAY ref
+ elsif ( $rsource eq 'ARRAY' ) {
+ $rinput_lines = $line_source_object;
+ }
+
+ # handle a SCALAR ref
+ elsif ( $rsource eq 'SCALAR' ) {
+ my @lines = split /^/, ${$line_source_object};
+ $rinput_lines = \@lines;
+ }
+
+ # handle an object - must have a get_line method
+ else {
+ while ( my $line = $line_source_object->get_line() ) {
+ push( @{$rinput_lines}, $line );
+ }
+ }
+
+ $self->[_rinput_lines_] = $rinput_lines;
+ $self->[_input_line_index_next_] = 0;
+ return;
+} ## end sub make_source_array
+
+sub get_next_line {
+
+ my $self = shift;
+
+ # return the next line from the input stream
+
+ my $rinput_lines = $self->[_rinput_lines_];
+ my $line_index = $self->[_input_line_index_next_];
+ my $line;
+ if ( $line_index < @{$rinput_lines} ) {
+ $line = $rinput_lines->[ $line_index++ ];
+ $self->[_input_line_index_next_] = $line_index;
+ }
+ return $line;
+} ## end sub get_next_line
+
+sub peek_ahead {
+ my ( $self, $buffer_index ) = @_;
+
+ # look $buffer_index lines ahead of the current location without disturbing
+ # the input
+ my $line;
+ my $rinput_lines = $self->[_rinput_lines_];
+ my $line_index = $buffer_index + $self->[_input_line_index_next_];
+ if ( $line_index < @{$rinput_lines} ) {
+ $line = $rinput_lines->[$line_index];
+ }
+ return $line;
+} ## end sub peek_ahead
+
#-----------------------------------------
# interface to Perl::Tidy::Logger routines
#-----------------------------------------
# USES GLOBAL VARIABLES:
# $brace_depth, $square_bracket_depth, $paren_depth
- my $input_line = $self->[_line_buffer_object_]->get_line();
+ my $input_line = $self->get_next_line();
$self->[_line_of_text_] = $input_line;
return unless ($input_line);
# keep looking at lines until we find a hash bang or piece of code
my $msg = EMPTY_STRING;
- while ( $line = $self->[_line_buffer_object_]->peek_ahead( $i++ ) ) {
+ while ( $line = $self->peek_ahead( $i++ ) ) {
# if first line is #! then assume starting level is zero
if ( $i == 1 && $line =~ /^\#\!/ ) {
sub prepare_for_a_new_file {
- my $self = shift;
+ my ( $self, $source_object ) = @_;
+
+ # copy the source object lines to an array of lines
+ $self->make_source_array($source_object);
# previous tokens needed to determine what to expect next
$last_nonblank_token = ';'; # the only possible starting state which
my $i = 0;
my ( $rpre_tokens, $rmap, $rpre_types );
- while ( $line = $self->[_line_buffer_object_]->peek_ahead( $i++ ) ) {
+ while ( $line = $self->peek_ahead( $i++ ) ) {
$line =~ s/^\s*//; # trim leading blanks
next if ( length($line) <= 0 ); # skip blank
next if ( $line =~ /^#/ ); # skip comment
my $line;
my $i = 0;
- while ( $line = $self->[_line_buffer_object_]->peek_ahead( $i++ ) ) {
+ while ( $line = $self->peek_ahead( $i++ ) ) {
$line =~ s/^\s*//; # trim leading blanks
next if ( length($line) <= 0 ); # skip blank
next if ( $line =~ /^#/ ); # skip comment
my $k = 0;
my $msg = "checking <<";
- while ( $line = $self->[_line_buffer_object_]->peek_ahead( $k++ ) ) {
+ while ( $line = $self->peek_ahead( $k++ ) ) {
chomp $line;
if ( $line =~ /^$next_token$/ ) {