Attean
view release on metacpan or search on metacpan
lib/AtteanX/API/Lexer.pm view on Meta::CPAN
=item C<< linebuffer >>
=item C<< line >>
=item C<< column >>
=item C<< buffer >>
=item C<< start_column >>
=item C<< start_line >>
=back
=head1 METHODS
=over 4
=cut
package AtteanX::API::Lexer 0.039 {
use strict;
use Types::Standard qw(FileHandle Ref Str Int ArrayRef HashRef ConsumerOf InstanceOf);
use Moo::Role;
has file => ( is => 'ro', isa => FileHandle, required => 1, );
has linebuffer => ( is => 'rw', isa => Str, default => '', );
has line => ( is => 'rw', isa => Int, default => 1, );
has column => ( is => 'rw', isa => Int, default => 1, );
has buffer => ( is => 'rw', isa => Str, default => '', );
has start_column => ( is => 'rw', isa => Int, default => -1, );
has start_line => ( is => 'rw', isa => Int, default => -1, );
around 'BUILDARGS' => sub {
my $orig = shift;
my $class = shift;
return { file => shift } if (scalar(@_) == 1);
return $orig->( $class, @_ );
};
=item C<< fill_buffer >>
Fills the buffer with a new line from the underlying filehandle.
=cut
sub fill_buffer {
my $self = shift;
unless (length($self->buffer)) {
my $line = $self->file->getline;
# use Data::Dumper;
# warn Dumper({buffer_line => $line});
$self->{buffer} .= $line if (defined($line));
}
}
=item C<< check_for_bom >>
Remove a BOM character if one appears at the start of the buffer.
=cut
sub check_for_bom {
my $self = shift;
my $c = $self->peek_char();
$self->get_char if (defined($c) and $c eq "\x{FEFF}");
}
=item C<< get_char_safe( $char ) >>
Consume the single character C<< $char >> from the buffer.
Throw an error if C<< $char >> is not at the start of the buffer.
=cut
sub get_char_safe {
my $self = shift;
my $char = shift;
my $c = $self->get_char;
$self->_throw_error("Expected '$char' but got '$c'") if ($c ne $char);
return $c;
}
=item C<< get_char( $char ) >>
Consume and return a single character from the buffer.
=cut
sub get_char {
my $self = shift;
my $c = substr($self->{buffer}, 0, 1, '');
if ($c eq "\n") {
# $self->{linebuffer} = '';
$self->{line} = 1+$self->{line};
$self->{column} = 1;
} else {
# $self->{linebuffer} .= $c;
$self->{column} = 1+$self->{column};
}
return $c;
}
=item C<< peek_char( $char ) >>
Return a single character from the start of the buffer.
=cut
sub peek_char {
my $self = shift;
if (length($self->{buffer}) == 0) {
$self->fill_buffer;
return if (length($self->{buffer}) == 0);
}
return substr($self->{buffer}, 0, 1);
}
=item C<< read_word( $word ) >>
( run in 0.913 second using v1.01-cache-2.11-cpan-bbe5e583499 )