Subject: | regex with single backslash char not recognised |
If you try to use this one-line grammar:
whatever : /\\/ | /whatever/
you'll get the following error:
Warning (line 1): Token pattern "m/\\/ | /" may not be a valid regular
expression
ERROR (line 1): Untranslatable item encountered: "/"
(Hint: Set $::RD_HINT (or -RD_HINT if you're using "perl -s")
for hints on fixing these problems.)
but the first regex is obviously correct.
If you're wondering how I discovered this, I'm trying to write an
automatic translator for grammars written in ABNF into grammars for
P::RD. Quoted strings in ABNF are case insensitive, so I'm translating
quoted stuff into /whatever/i. The following workaround... works:
whatever : /(?:\\)/ | /whatever/
I'm attaching the best test case script I managed to write (had to do
weird redirection of STDERR to a pipe to make it work properly).
Subject: | backslash.t |
# vim: filetype=perl :
use strict;
use warnings;
use Test::More tests => 1; # last test to print
my ($grammar, $msg) = @_;
pipe my($r, $w);
open STDERR, '>&', $w;
require Parse::RecDescent;
my $parser = Parse::RecDescent->new('x: /\\\\/ | /x/');
print {$w} "STOPPED\n";
close $w;
my $stderr;
while (<$r>) {
last if $_ eq "STOPPED\n";
$stderr .= $_;
}
is($stderr, undef, defined($msg) ? $msg : "grammar is ok");