Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Created May 6, 2013 21:17
Show Gist options
  • Save pstuifzand/5528278 to your computer and use it in GitHub Desktop.
Save pstuifzand/5528278 to your computer and use it in GitHub Desktop.
Example of completion events in Marpa.
use 5.10.0;
use Marpa::R2;
use Data::Dumper;
my $grammar = Marpa::R2::Scanless::G->new({
source => \<<'SOURCE',
:start ::= numbers
event number_found = completed number
numbers ::= number+ action => ::array
number ::= digits action => ::first
digits ~ [\d]+
:discard ~ ws
ws ~ [\s]+
SOURCE
});
my $re = Marpa::R2::Scanless::R->new({
grammar => $grammar,
});
my $input = <<'INPUT';
100 200 300 400 500
INPUT
my $pos = $re->read(\$input);
while ($pos < length $input) {
for (my $i = 0; my $event = $re->event($i); $i++) {
print Dumper($event);
}
$re->lexeme_read('digits', $pos, 0, '999999');
$pos = $re->resume($pos);
}
my $v = $re->value;
print Dumper($$v);
sub show_last_subtext {
my ($slr) = @_;
my ( $start, $end ) = $slr->last_completed_range('number');
return 'No expression was successfully parsed' if not defined $start;
return $slr->range_to_string( $start, $end );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment