1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
package Statistics::R::IO::ParserState;
# ABSTRACT: Current state of the IO parser
$Statistics::R::IO::ParserState::VERSION = '1.0002';
use 5.010;
use Class::Tiny::Antlers;
use namespace::clean;
has data => (
is => 'ro',
default => sub { [] },
);
has position => (
is => 'ro',
default => sub { 0 },
);
has singletons => (
is => 'ro',
default => sub { [] },
);
sub BUILDARGS {
my $class = shift;
my $attributes = {};
if ( scalar @_ == 1) {
if ( ref $_[0] eq 'HASH' ) {
$attributes = $_[0]
}
else {
$attributes->{name} = $_[0]
}
}
elsif ( @_ % 2 ) {
die "The new() method for $class expects a hash reference or a key/value list."
. " You passed an odd number of arguments\n";
}
else {
$attributes = { @_ };
}
# split strings into a list of individual characters
if (defined $attributes->{data} && !ref($attributes->{data})) {
$attributes->{data} = [split //, $attributes->{data}];
}
$attributes
}
sub BUILD {
my $self = shift;
die 'foo' unless ref($self->data) eq 'ARRAY'
}
sub at {
my $self = shift;
$self->data->[$self->position]
}
sub next {
my $self = shift;
ref($self)->new(data => $self->data,
position => $self->position+1,
singletons => [ @{$self->singletons} ])
}
sub add_singleton {
my ($self, $singleton) = (shift, shift);
my @new_singletons = @{$self->singletons};
push @new_singletons, $singleton;
ref($self)->new(data => $self->data,
position => $self->position,
singletons => [ @new_singletons ])
}
sub get_singleton {
my ($self, $singleton_id) = (shift, shift);
$self->singletons->[$singleton_id]
}
sub eof {
my $self = shift;
$self->position >= scalar @{$self->data};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Statistics::R::IO::ParserState - Current state of the IO parser
=head1 VERSION
version 1.0002
=head1 SYNOPSIS
use Statistics::R::IO::ParserState;
my $state = Statistics::R::IO::ParserState->new(
data => 'file.rds'
);
say $state->at
say $state->next->at;
=head1 DESCRIPTION
You shouldn't create instances of this class, it exists mainly to
handle deserialization of R data files by the C<IO> classes.
=head1 METHODS
=head2 ACCESSORS
=over
=item data
An array reference to the data being parsed. The constructs accepts a
scalar, which will be L<split> into individual characters.
=item position
Position of the next data element to be processed.
=item at
Returns the element (byte) at the current C<position>.
=item eof
Returns true if the cursor (C<position>) is at the end of the C<data>.
=item singletons
An array reference in which unserialized data that should be exists as
singletons can be "stashed" by the parser for later reference.
=item get_singleton $id
Return the singleton data object with the given C<$id>.
=back
=head2 MUTATORS
C<ParserState> is intended to be immutable, so the "mutator" methods
actually return a new instance with appropriately modified values of
the attributes.
=over
=item next
Returns a new ParserState instance with C<position> advanced by one.
=item add_singleton $singleton
Returns a new ParserState instance with C<$singleton> argument
appended to the instance's C<singletons>.
=back
=head1 BUGS AND LIMITATIONS
Instances of this class are intended to be immutable. Please do not
try to change their value or attributes.
There are no known bugs in this module. Please see
L<Statistics::R::IO> for bug reporting.
=head1 SUPPORT
See L<Statistics::R::IO> for support and contact information.
=for Pod::Coverage BUILDARGS BUILD
=head1 AUTHOR
Davor Cubranic <cubranic@stat.ubc.ca>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2017 by University of British Columbia.
This is free software, licensed under:
The GNU General Public License, Version 3, June 2007
=cut
|