Statis...:Parser(3pm)
π Statis...:Parser(3pm) β Statistics::R::IO::Parser π
okay bestie, so this is a User Contributed Perl Documentation page for Statistics::R::IO::Parser β yes the header truncates it to Statis...:Parser(3pm) because terminals are dramatic like that π
. It's the Functions for parsing R data files situation. Let's get into it π¦
β¨ NAME β¨
Statistics::R::IO::Parser β functions for parsing R data files π
β¨ VERSION β¨
version 1.0002 β
β¨ SYNOPSIS β¨
use Statistics::R::IO::ParserState;
use Statistics::R::IO::Parser;
my $state = Statistics::R::IO::ParserState->new(
data => 'file.rds'
);
say $state->at
say $state->next->at;
β¨ DESCRIPTION β¨
no cap, you should not be creating instances of this class yourself π. It exists mainly to handle deserialization of R data files by the "IO" classes. It's giving backstage crew energy β does the work, doesn't want the spotlight π
β¨ FUNCTIONS β¨
This library is inspired by monadic parser frameworks from the Haskell world, like Packrat http://bford.info/packrat/ or Parsec http://hackage.haskell.org/package/parsec π What that means, bestie, is that parsers get constructed by combining simpler parsers. Little parsers making bigger parsers, it's giving friendship bracelet π₯
The library offers a selection of basic parsers and combinators. Each of these is a function (think of it as a factory) that returns another function (the actual parser) which receives the current parsing state (Statistics::R::IO::ParserState) as the argument and returns a two-element array reference (called for brevity "a pair" in the following text) with the result of the parser in the first element and the new parser state in the second element. If the parser fails, say if the current state is "a" where a number is expected, it returns "undef" to signal failure π
The descriptions of individual functions below use a shorthand because the above mechanism is implied. Thus, when "any_char" is described as "parses any character", it really means that calling "any_char" will return a function that when called with the current state will return "a pair of the character...", etc. π§
πΈ CHARACTER PARSERS πΈ
any_charβ parses any character, returning a pair of the character at the current State's position and the new state, advanced by one from the starting state. If the state is at the end ($state->eofis true), returnsundefto signal failure. πchar $cβ parses the given character$c, returning a pair of the character at the current State's position if it is equal to$cand the new state, advanced by one from the starting state. If the state is at the end ($state->eofis true) or the character at the current position is not$c, returnsundefto signal failure. π«Άstring $sβ parses the given string$s, returning a pair of the sequence of characters starting at the current State's position if it is equal to$sand the new state, advanced bylength($s)from the starting state. If the state is at the end ($state->eofis true) or the string starting at the current position is not$s, returnsundefto signal failure. β¨
π’ NUMBER PARSERS π’
endianness [$end]β when the$endargument is given, this functions sets the byte order used by parsers in the module to be little- (when$endis"<") or big-endian ($endis">"). This function changes the module's state and remains in effect until the next change. π
When called with no arguments, "endianness" returns the current byte order in effect. The starting byte order is big-endian. πΈ
any_uint8,any_uint16,any_uint24,any_uint32β parses an 8-, 16-, 24-, and 32-bit unsigned integer, returning a pair of the integer starting at the current State's position and the new state, advanced by 1, 2, 3, or 4 bytes from the starting state, depending on the parser. The integer value is determined by the current value of"endianness". If there are not enough elements left in the data from the current position, returnsundefto signal failure. πuint8 $n,uint16 $n,uint24 $n,uint32 $nβ parses the specified 8-, 16-, 24-, and 32-bit unsigned integer$n, returning a pair of the integer at the current State's position if it is equal$nand the new state. The new state is advanced by 1, 2, 3, or 4 bytes from the starting state, depending on the parser. The integer value is determined by the current value of"endianness". If there are not enough elements left in the data from the current position or the current position is not$n, returnsundefto signal failure. πany_int8,any_int16,any_int24,any_int32β parses an 8-, 16-, 24-, and 32-bit signed integer, returning a pair of the integer starting at the current State's position and the new state, advanced by 1, 2, 3, or 4 bytes from the starting state, depending on the parser. The integer value is determined by the current value of"endianness". If there are not enough elements left in the data from the current position, returnsundefto signal failure. πint8 $n,int16 $n,int24 $n,int32 $nβ parses the specified 8-, 16-, 24-, and 32-bit signed integer$n, returning a pair of the integer at the current State's position if it is equal$nand the new state. The new state is advanced by 1, 2, 3, or 4 bytes from the starting state, depending on the parser. The integer value is determined by the current value of"endianness". If there are not enough elements left in the data from the current position or the current position is not$n, returnsundefto signal failure. π¦any_real32,any_real64β parses an 32- or 64-bit real number, returning a pair of the number starting at the current State's position and the new state, advanced by 4 or 8 bytes from the starting state, depending on the parser. The real value is determined by the current value of"endianness". If there are not enough elements left in the data from the current position, returnsundefto signal failure. πany_int32_na,any_real64_naβ parses a 32-bit signed integer or 64-bit real number, respectively, but recognizing R-style missing values (NAs):INT_MINfor integers and a special NaN bit pattern for reals. Returns a pair of the number value ("undef"if a NA) and the new state, advanced by 4 or 8 bytes from the starting state, depending on the parser. If there are not enough elements left in the data from the current position, returnsundefto signal failure. π
π SEQUENCING π
seq $p1, ...β this combinator applies parsers$p1, ...in sequence, using the returned parse state of$p1as the input parse state to$p2, etc. Returns a pair of the concatenation of all the parsers' results and the parsing state returned by the final parser. If any of the parsers returnsundef,"seq"will return it immediately without attempting to apply any further parsers. πmany_till $p, $endβ this combinator applies a parser$puntil parser$endsucceeds. It does this by alternating applications of$endand$p; once$endsucceeds, the function returns the concatenation of results of preceding applications of$p. (Thus, if$endsucceeds immediately, the 'result' is an empty list.) Otherwise,$pis applied and must succeed, and the procedure repeats. Returns a pair of the concatenation of all the$p's results and the parsing state returned by the final parser. If any applications of$preturnsundef,"many_till"will return it immediately. π§count $n, $pβ this combinator applies the parser$pexactly$ntimes in sequence, threading the parse state through each call. Returns a pair of the concatenation of all the parsers' results and the parsing state returned by the final application. If any application of$preturnsundef,"count"will return it immediately without attempting any more applications. βwith_count [$num_p = any_uint32], $pβ this combinator first applies parser$num_pto get the number of times that$pshould be applied in sequence. If only one argument is given,"any_uint32"is used as the default value of$num_p. (So"with_count"works by getting a number$nby applying$num_pand then calling"count $n, $p".) Returns a pair of the concatenation of all the parsers' results and the parsing state returned by the final application. If the initial application of$num_por any application of$preturnsundef,"with_count"will return it immediately without attempting any more applications. π₯choose $p1, ...β this combinator applies parsers$p1, ...in sequence, until one of them succeeds, when it immediately returns the parser's result. If all of the parsers fail,"choose"fails and returnsundefπ
π COMBINATORS π
bind $p1, $fβ this combinator applies parser$p1and, if it succeeds, calls function$fusing the first element of$p1's result as the argument. The call to$fneeds to return a parser, which"bind"applies to the parsing state after$p1's application. π«Ά
The "bind" combinator is an essential building block for most combinators described so far. For instance, "with_count" can be written as:
bind($num_p,
sub {
my $n = shift;
count $n, $p;
})
mreturn $valueβ returns a parser that when applied returns$valuewithout changing the parsing state. β¨error $messageβ returns a parser that when applied croaks with the$messageand the current parsing state. π
π SINGLETONS π
These functions are an interface to ParseState's singleton-related functions, "add_singleton" in ParseState and "get_singleton" in ParseState. They exist because certain types of objects in R data files, for instance environments, have to exist as unique instances, and any subsequent objects that include them refer to them by a "reference id". π
add_singleton $singletonβ adds the$singletonto the current parsing state. Returns a pair of$singletonand the new parsing state. πget_singleton $ref_idβ retrieves from the current parse state the singleton identified by$ref_id, returning a pair of the singleton and the (unchanged) state. πΈreserve_singleton $pβ preallocates a space for a singleton before running a given parser, and then assigns the parser's value to the singleton. Returns a pair of the singleton and the new parse state. π¦
β¨ BUGS AND LIMITATIONS β¨
Instances of this class are intended to be immutable. Please do not try to change their value or attributes. π βοΈ like, hands off, bestie.
There are no known bugs in this module. Please see Statistics::R::IO for bug reporting. π
β¨ SUPPORT β¨
See Statistics::R::IO for support and contact information. π«Ά
β¨ AUTHOR β¨
Davor Cubranic <cubranic@stat.ubc.ca> π
β¨ 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
and that's the whole page, bestie π this was documented by perl v5.34.0 on 2022-02-10, so shoutout to that exact little timestamp for making it official. mwah, go parse something πβ¨
π¦ where this page comes from
- debian bookworm: libstatistics-r-io-perl 1.0002-2 Β· GPL-3.0-only
- debian trixie: libstatistics-r-io-perl 1.0002-2 Β· GPL-3.0-only
- ubuntu jammy: libstatistics-r-io-perl 1.0002-2 Β· GPL-3.0-only
- ubuntu noble: libstatistics-r-io-perl 1.0002-2 Β· GPL-3.0-only
- ubuntu resolute: libstatistics-r-io-perl 1.0002-2 Β· GPL-3.0-only
- ubuntu stonking: libstatistics-r-io-perl 1.0002-2 Β· GPL-3.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms