Hi P::RD lovers,

I got a small problem regarding skipping over comments and misleading
generated error messages, see the example (sure in my real world problem
this is hidden in much deeper rulesets, this example is just so simple
to show the key point, therefore please don't reply how I could circumvent
this problem in just this example if it is not the solution to the general
problem described below):

#!/usr/bin/perl -w
use Parse::RecDescent;
my $grammar =<<'EOGRAMMAR';
        file            : <skip: qr{(\s*(#.*\n)*)*} >  # skip over comments
                         int(s) /\z/
                        | <error>
                         
        int             : /[+-]?\d+/
                        | <error>
EOGRAMMAR

my $parser = Parse::RecDescent->new($grammar);

my $text = <<'EOTEXT';
        # comment
        .123
EOTEXT

my $result = $parser->file($text);


and here is the result:


       ERROR (line 2): Invalid int: Was expecting /[+-]?\\d+/

       ERROR (line 1): Invalid file: Was expecting int but found "# comment"
                       instead

fine, the ERROR generated for int: "Was expecting /[+-]?\\d+/"

hits the problem, but the ERROR for file:

"Was expecting int but found "# comment" instead

is misleading the user as he must be thinking something is wrong with 
his comments (and even worse, in (line 1), therefore he is thinking
the ERROR for (line 2) is just an aftereffect (the user knows nothing
about RecDescent!).

The problem is, (as far as I understand) that the <skip> must be
bound to a rule, and when the rule isn't matched, then the already
skipped text is stuffed back to the $text stream from where the
<error> directive is generating the message "but found ....".

If Damian had spent a Package Variable:

$RD_SKIP

then we could have a general SKIP pattern, not bound to any rule
and the error messages would be cleaner because nothing must get
stuffed back if a rule didn't match because the global SKIP is done
without any relation to the current rule. Sure, it must be the same
as with the $RD_AUTOACTION, if any <skip> is defined in a rule, it
has precedence.

Best Regards, any comment/discussion welcome

        Charly

-- 
Karl Gaissmaier          Computing Center,University of Ulm,Germany
Email:[EMAIL PROTECTED]          Network Administration
Tel.: ++49 731 50-22499

Reply via email to