in reply to Using Parse::RecDescent to parse Perl-ish strings without resorting to string eval

Don't. It might be a lot of work to convert from Perl, but it's even more work to parse Perl. If you insist, you probably should start by taking a look at String::Interpolate (uses a Safe compartment to confine the interpolation to specified VAR => $value pairings) and PPI (a Perl parser).

quoted_string: '"' m{(([\\]"|[^"])*)} '"'

I hope your never have spaces after the first """ in the text you are parsing, or did you change <skip>?

It's best to avoid separating a token into multiple items. It's rarely necessary or even easier.

  • Comment on Re: Using Parse::RecDescent to parse Perl-ish strings without resorting to string eval
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Using Parse::RecDescent to parse Perl-ish strings without resorting to string eval
by polypompholyx (Chaplain) on Feb 29, 2008 at 20:08 UTC

    Yes, in the original code, there's some fiddling with <skip> (since whitespace has syntactic significance in the grammar), and unescaping of Unicode hex character escapes. I left them out for clarity.

    I can probably live with handrolled code that parses its own definition of a double quoted string, since most of the interpolation is unnecessary, but I'm not sure of any way out with regexes: I'd like to be able to do something like:

    $text = qr/$text/$modifiers

    but $modifiers doesn't interpolate, and I can't think of any easy way to simulate this without having a very large switch statement.

      my $mods_p = ''; my $mods_m = ''; ($opts{$_} ? $mods_p : $mods_m) .= $_ for qw( x i s m ); $re = qr/(?$mods_p-$mods_m:$re)/;
        Ah, of course. I am an idiot. ikegami++