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.
Consumer
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).
To install Parse::RecDescent::Consumer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parse::RecDescent::Consumer
CPAN shell
perl -MCPAN -e shell install Parse::RecDescent::Consumer
For more information on module installation, please visit the detailed CPAN module installation guide.