Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing binary structure with Perl6 Grammar

Tags:

raku

What is the best option to parse a binary structure with Perl6 .

in Perl5 we have the pack/unpack methods on Perl6 they seems experimental

is it possible to use Perl6 grammar to parse binary data let's say i have a file which have records on the following binary format :

struct record {
short int ut_type;

char ut_line[UT_LINESIZE];
char ut_id[4];
char ut_user[UT_NAMESIZE];
char ut_host[UT_HOSTSIZE];


}

is it possible to parse this file with Perl6 grammar ?

like image 384
smith Avatar asked Dec 23 '22 11:12

smith


1 Answers

What is the best option to parse a binary structure with Perl6?

Especially given that you know about P5's pack/unpack, the new P5pack module is plausibly the appropriate solution. (I haven't tested it. It's new. Aiui it doesn't implement everything and it doesn't mimic P5's pack slavishly. But it's Liz.)


If the new pure P6 implementation of P5's pack interface linked above does not do what you need done, another obvious solution is using the original P5 functions, executed by a regular perl 5 binary, in your P6 code. The following is incomplete / untested but I mean something roughly like:

use Inline::Perl5 ; my \P5 = Inline::Perl5.new ;

my $mem = Buf ... ;

my $hex = P5.call('unpack', 'H*', $mem) ;

(Or, conversely, write the mainline as P5 code, adding P6 code via Inline::Perl6.)


In the current version of P6, which is 6.c, grammars can only process text.


2 years ago P6er "skids" wrote:

"There are enough people wanting binary grammars that I think it will come to be" (written in 2016).

At that time they also put together the following related links:

  • skid's then new idea for a packing foo { template bar { ... } } construct plus their much older "very old, low level types thoughts".

  • ShimmerFairy's "thoughts on possible binary grammars in P6 (incomplete)"

  • Juerd's "RFC: A more Perl6-esque "unpack"".

  • smls's "Binary Parsing in Perl 6".

  • masak's "A first approach to pack/unpack in Perl 6".

  • jnthn's "talk on Grammar::Generative" (video).

like image 183
raiph Avatar answered Jan 10 '23 21:01

raiph