2

I've got a token like such:

<delim2=((?{ $MATCH{delim} }))>

and what I want to happen is for delim2 to capture and be set to the value of delim. When I run this, delim2 is set, but the capture is never done. I think this is an error in my reasoning: I'm trying to chain this form:

<ALIAS= ( PATTERN )>     Match pattern, save match in $MATCH{ALIAS}

and this form: (?{ MATCH{delim} }) into something like this

<ALIAS= ( (?{MATCH{delim}) )>     Matches the value of $MATCH{delim} save to $MATCH{delim2}

but this simply doesn't seem valid. I can verify my original token works <delim2=((?{ die $MATCH{delim} }))> will die with the value, and, if I hard code it, I get the right capture and everything works <delim2=(')>? So how do I go about achieving sane results, while having a dynamic pattern?

Svante
  • 50,694
  • 11
  • 78
  • 122
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468

1 Answers1

4

(?{ $MATCH{delim} }) doesn't assert that $MATCH{delim} appears here in the input; only that it's a true value. Regexp::Grammars should have a "named-backreference" construct like perl \k<NAME> but it doesn't (and you can't use \k<NAME> because Regexp::Grammars stores its results somewhere entirely different).

You could do something like

(??{ quotemeta $MATCH{delim} })<delim2=(?{ $MATCH{delim} })>

which is horrible but seems to work in testing. Or you could give in and go to Parse::RecDescent which has better support for this kind of thing. Or you could start hacking on R::G.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • I would upvote this one twice if I could. I actually had the answer from D yesterday, but your addition of `quotemeta` just solved a problem I've been working on for a while. Thanks -- In fact, I'll take it one step further, I hereby take everything negative I've said about you in the past month back. hobbs++ – Evan Carroll Jun 16 '10 at 15:32
  • Damian says, «In the next release of Regexp::Grammars, you're going to be able to use what I'm calling "subrule backreferences".» – Evan Carroll Jun 16 '10 at 20:02
  • I haven't even heard from you in the past month. Shit, that means you're talking behind my back ;) Anyway, I'm glad to hear that Damian is working on it. – hobbs Jun 16 '10 at 21:18