]> git.donarmstrong.com Git - perltidy.git/blob - lib/Perl/Tidy/LineBuffer.pm
New upstream version 20181120
[perltidy.git] / lib / Perl / Tidy / LineBuffer.pm
1 #####################################################################
2 #
3 # The Perl::Tidy::LineBuffer class supplies a 'get_line()'
4 # method for returning the next line to be parsed, as well as a
5 # 'peek_ahead()' method
6 #
7 # The input parameter is an object with a 'get_line()' method
8 # which returns the next line to be parsed
9 #
10 #####################################################################
11
12 package Perl::Tidy::LineBuffer;
13 use strict;
14 use warnings;
15 our $VERSION = '20181120';
16
17 sub new {
18
19     my ( $class, $line_source_object ) = @_;
20
21     return bless {
22         _line_source_object => $line_source_object,
23         _rlookahead_buffer  => [],
24     }, $class;
25 }
26
27 sub peek_ahead {
28     my ( $self, $buffer_index ) = @_;
29     my $line               = undef;
30     my $line_source_object = $self->{_line_source_object};
31     my $rlookahead_buffer  = $self->{_rlookahead_buffer};
32     if ( $buffer_index < scalar( @{$rlookahead_buffer} ) ) {
33         $line = $rlookahead_buffer->[$buffer_index];
34     }
35     else {
36         $line = $line_source_object->get_line();
37         push( @{$rlookahead_buffer}, $line );
38     }
39     return $line;
40 }
41
42 sub get_line {
43     my $self               = shift;
44     my $line               = undef;
45     my $line_source_object = $self->{_line_source_object};
46     my $rlookahead_buffer  = $self->{_rlookahead_buffer};
47
48     if ( scalar( @{$rlookahead_buffer} ) ) {
49         $line = shift @{$rlookahead_buffer};
50     }
51     else {
52         $line = $line_source_object->get_line();
53     }
54     return $line;
55 }
56 1;
57