jeffreykegler / Marpa--R2

Parse any language you can describe in BNF
https://jeffreykegler.github.io/Marpa-web-site/
Other
157 stars 23 forks source link

bless_package #258

Closed jddurand closed 9 years ago

jddurand commented 9 years ago

Wondering if this is normal behaviour: with blessing a counted RHS is not propagated as an array to the actions, but is seen like a reference to a blessed array. The later blessing being the one of the LHS, as if blessing was done before the action is called.

#!env perl
use strict;
use diagnostics;
use Marpa::R2;
use Data::Dumper;

our $DATA = do {local $/; <DATA>;};
my $data1 = $DATA; $data1 =~ s/\$BLESS_PRAGMA//;
my $data2 = $DATA; $data2 =~ s/\$BLESS_PRAGMA/:default ::= bless => ::lhs/;

our $G1 = Marpa::R2::Scanless::G->new({source => \$data1});
our $G2 = Marpa::R2::Scanless::G->new({source => \$data2, bless_package => 'test'});

my $input = 'XX';
my $r;

print "\$Marpa::R2::VERSION: $Marpa::R2::VERSION\n";

print "Without blesssing:\n";
$r = Marpa::R2::Scanless::R->new({grammar =>$G1});
$r->read(\$input);
$r->value();

print "With blesssing:\n";
$r = Marpa::R2::Scanless::R->new({grammar =>$G2});
$r->read(\$input);
$r->value();

sub test {
  print Dumper(\@_);
}

__DATA__
$BLESS_PRAGMA
:start ::= test
test ::= x* action => main::test
x ~ 'X'

Output:

$Marpa::R2::VERSION: 3
Without blesssing:
$VAR1 = [
          {},
          'X',
          'X'
        ];
With blesssing:
$VAR1 = [
          {},
          bless( [
                   'X',
                   'X'
                 ], 'test::test' )
        ];
jeffreykegler commented 9 years ago

This is as documented. The results of a rule evaluation closure are the 2nd and later items in a list. In Perl, you can't bless just part of a list. Marpa has to have something it can bless, so it creates an array for that purpose.

jddurand commented 9 years ago

Yep - sorry & thanks.