Terrence Brannon > Parse-RecDescent-Consumer > Parse::RecDescent::Consumer

Download:
Parse-RecDescent-Consumer-1.03.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.03   Source  

NAME ^

Parse::RecDescent::Consumer - reveal text matched through n token transitions.

SYNOPSIS ^

  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)) }

DESCRIPTION ^

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...).

IMPLEMENTATION ^

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));
  }
 }

EXPORT

None by default.

AUTHOR ^

T. M. Brannon, <tbone@cpan.org>

SEE ALSO ^

perl(1).

syntax highlighting: