RE: <skip: over comments> and misleading <error> messages

[email protected] ("Orton, Yves")
Newsgroups perl.recdescent
Message-ID <71B318898201D311845C0008C75DAD1C06F1E4B2@defra1ex2>
.....

> 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

Well, there is such a variable, but its not called $::RD_SKIP its called 

$Parse::RecDescent::skip

(I assume that this variable is defined in the Parse::RecDescent namespace
because it has a default value)

Note that it appears this value is a string, and not a regex (i could be
wrong, I havent checked the source code)

So if you rewrite your example to be (with a couple of extra comments for
testing)

#!/usr/bin/perl -w
use Parse::RecDescent;
# Ignore continuous whitespace or # and everything after it.
$Parse::RecDescent::skip=qr/
                            (
                             ^\s+   # ignore continuous whitespace
                            |       # or 
                             \#.*$  # Comment to the end of the line (the \
is mandatory)
                            )+      # One or many times.
                          /x;       # xtended regex syntax, ignore
whitespace and comments in regex
my $grammar =<<'EOGRAMMAR';
	file 	: int(s) /\z/
			| <error>

	int		: /[+-]?\d+/
			| <error>
EOGRAMMAR

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

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

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

__END__

The I get the following result


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

       ERROR (line 1): Invalid file: Was expecting int


Which is what I believe you wanted.

Incidentally the regex you posted qr{(\s*(#.*\n)*)*} scares the *sh*t* out
of me.  (The )*)* is a construct that may not be wrong in this case but
often _is_ very wrong, so when I see it my spidey sense goes crazy, and I
replace it.  (Not everything a programmer does is logical...)

I rewrote it as /(^\s+|#.*)+$/ which to me is much safer and easier to
understand too.

Yves
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.