| Parse-RecDescent-Consumer documentation | view source | Contained in the Parse-RecDescent-Consumer distribution. |
Parse::RecDescent::Consumer - reveal text matched through n token transitions.
use Parse::RecDescent::Consumer;
# then in a Parse::RecDescent grammar...
url: <rulevar: $C> url: { $C = Consumer($text) } httpurl { REBOL::url->new(value => $C->($text)) } | { $C = Consumer($text) } ftpurl { REBOL::url->new(value => $C->($text)) }
A common need when writing grammars is to know how much text was
consumed at different points in a parse. Usually, this involves a lot
of brain-twisting unwinding of of highly nested list-of-lists (of
lists...). Instead this module allows you to take the low-road
approach. You simply create a Consumer which records the current
text about to be parsed.
After you have successfully transitioned through the desired tokens,
you simply re-call your Consumer and it gives you the text that was
consumed during the token transitions without you having to unravel a
highly nested list-of-lists (of lists...).
when you first call Consumer(), you are returned a closure which has the current text remaining to be parsed in it. When you evaluate the closure, passing it the (more or less consumed) new text, the closure calculates the difference in length between the two texts, and returns a substring of the first equating to the amount of text consumed between calls:
sub Parse::RecDescent::Consumer {
my $text=shift;
my $closure = sub {
my $new_length=length($_[0]);
my $original_text = $text;
my $original_length = length($text);
return substr($original_text, 0, ($original_length-$new_length));
}
}
None by default.
T. M. Brannon, <tbone@cpan.org>
perl(1).
| Parse-RecDescent-Consumer documentation | view source | Contained in the Parse-RecDescent-Consumer distribution. |