Created
May 8, 2014 12:06
-
-
Save dginev/44993a4cbb946ad92085 to your computer and use it in GitHub Desktop.
Simple Marpa events example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.010; | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use English qw( -no_match_vars ); | |
use Marpa::R2; | |
my $rules = <<'END_OF_GRAMMAR'; | |
:start ::= sequence | |
sequence ::= FIRST SECOND | |
action => OK | |
FIRST ::= A B C D E F G | |
SECOND ::= H I J K L | |
A ::= 'a' | |
B ::= 'b' | |
C ::= 'c' | |
D ::= 'd' | |
E ::= 'e' | |
F ::= 'f' | |
G ::= 'g' | |
H ::= 'h' | |
I ::= 'i' | |
J ::= 'j' | |
K ::= 'k' | |
L ::= 'l' | |
event 'FIRST' = completed FIRST | |
event 'SECOND' = completed SECOND | |
END_OF_GRAMMAR | |
my %pos_by_event = (); | |
my @events; | |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); | |
my $string = "abcdefghijkl"; | |
my $actual_events = []; | |
my $slr = Marpa::R2::Scanless::R->new({ grammar => $grammar, semantics_package => 'My_Actions' } ); | |
my $length = length $string; | |
my $pos = $slr->read( \$string ); | |
READ: while (1) { | |
my @current_events = (); | |
EVENT: | |
for my $event ( @{ $slr->events() } ) { | |
my ($name) = @{$event}; | |
push @current_events, $name; | |
} | |
if (@current_events) { | |
push @$actual_events, [$pos, @current_events]; | |
} | |
last READ if $pos >= $length; | |
$pos = $slr->resume($pos); | |
} ## end READ: while (1) | |
my $value_ref = $slr->value(); | |
if ( not defined $value_ref ) { | |
die "No parse\n"; | |
} | |
my $actual_value = ${$value_ref}; | |
print STDERR "Actual value: $actual_value\n"; | |
print STDERR "Actual events: ",Dumper($actual_events); | |
sub My_Actions::OK { return 1792 }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment