jtbraun / Parse-RecDescent

Perl module for generating recursive-descent parsers
http://search.cpan.org/~dconway/Parse-RecDescent/lib/Parse/RecDescent.pm
11 stars 8 forks source link

/\s+/ not working in grammar definition? #1

Closed woosley closed 12 years ago

woosley commented 12 years ago

Please see https://gist.github.com/3168999 for this issue.

simplely if I use /\s+/ to match blanks, the match would fail, I have to use /\s*/ for temporary solution. Version is 1.967009

jtbraun commented 12 years ago

Modify $skip, or supply prior to whitespace becoming important in your grammar. By default, R::RD skips spaces and tabs in between tokens. $Parse::RecDescent::skip lets you modify this behavior, see the POD.

Also, rules that succeed returned a defined value, not a true value.
Your test for success should be defined $parser->line() or die;

On 7/24/2012 2:20 AM, WoOsley wrote:

simple if I use /\s+/ to match blanks, the match would fail, I have to use /\s*/ for temporary solution. Version is 1.967009


woosley commented 12 years ago

Aha, thanks for the clarification, can't believe I missed this part in the document.

thoughtstream commented 12 years ago

simple if I use /\s+/ to match blanks, the match would fail, I have to use /\s*/ for temporary solution. Version is 1.967009

By default, P::RD grammars automatically skip any whitespace between tokens. In other words, before everything you explicitly tell them to match, they inject an extra invisible /\s*/ match.

That injected matcher is eating all the whitespace and preventing your /\s+/ from matching. You need to use a skip:... directive (as described in the modules manpage, under 'Terminal Separators' and 'Skipping between terminals') to change this default behaviour.

All the best,

Damian